You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

457 lines
11 KiB

* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2004-2020 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <pgm_base.h>
  25. #include <eda_item.h>
  26. #include <gr_text.h>
  27. #include <kicad_string.h>
  28. #include <sch_draw_panel.h>
  29. #include <eda_draw_frame.h>
  30. #include <plotter.h>
  31. #include <trigo.h>
  32. #include <base_units.h>
  33. #include <msgpanel.h>
  34. #include <bitmaps.h>
  35. #include <general.h>
  36. #include <class_libentry.h>
  37. #include <transform.h>
  38. #include <lib_field.h>
  39. #include <template_fieldnames.h>
  40. #include <settings/color_settings.h>
  41. LIB_FIELD::LIB_FIELD(LIB_PART * aParent, int idfield ) :
  42. LIB_ITEM( LIB_FIELD_T, aParent )
  43. {
  44. Init( idfield );
  45. }
  46. LIB_FIELD::LIB_FIELD( int idfield ) :
  47. LIB_ITEM( LIB_FIELD_T, NULL )
  48. {
  49. Init( idfield );
  50. }
  51. LIB_FIELD::LIB_FIELD( int aID, const wxString& aName ) :
  52. LIB_ITEM( LIB_FIELD_T, NULL )
  53. {
  54. Init( aID );
  55. m_name = aName;
  56. }
  57. LIB_FIELD::~LIB_FIELD()
  58. {
  59. }
  60. LIB_FIELD& LIB_FIELD::operator=( const LIB_FIELD& field )
  61. {
  62. m_id = field.m_id;
  63. m_name = field.m_name;
  64. m_Parent = field.m_Parent;
  65. SetText( field.GetText() );
  66. SetEffects( field );
  67. return *this;
  68. }
  69. void LIB_FIELD::Init( int id )
  70. {
  71. m_id = id;
  72. SetTextAngle( TEXT_ANGLE_HORIZ ); // constructor already did this.
  73. // Fields in RAM must always have names, because we are trying to get less dependent on
  74. // field ids and more dependent on names. Plus assumptions are made in the field editors.
  75. m_name = TEMPLATE_FIELDNAME::GetDefaultFieldName( id );
  76. // By contrast, VALUE and REFERENCE are are always constructed as initially visible, and
  77. // template fieldsnames' initial visibility is controlled by the template fieldname config.
  78. if( id == DATASHEET || id == FOOTPRINT )
  79. SetVisible( false );
  80. }
  81. int LIB_FIELD::GetPenWidth() const
  82. {
  83. return GetEffectiveTextPenWidth();
  84. }
  85. void LIB_FIELD::print( RENDER_SETTINGS* aSettings, const wxPoint& aOffset, void* aData,
  86. const TRANSFORM& aTransform )
  87. {
  88. wxDC* DC = aSettings->GetPrintDC();
  89. COLOR4D color = aSettings->GetLayerColor( IsVisible() ? GetDefaultLayer() : LAYER_HIDDEN );
  90. int penWidth = std::max( GetPenWidth(), aSettings->GetDefaultPenWidth() );
  91. wxPoint text_pos = aTransform.TransformCoordinate( GetTextPos() ) + aOffset;
  92. wxString text = aData ? *static_cast<wxString*>( aData ) : GetText();
  93. GRText( DC, text_pos, color, text, GetTextAngle(), GetTextSize(), GetHorizJustify(),
  94. GetVertJustify(), penWidth, IsItalic(), IsBold() );
  95. }
  96. bool LIB_FIELD::HitTest( const wxPoint& aPosition, int aAccuracy ) const
  97. {
  98. // Because HitTest is mainly used to select the field return false if it is empty
  99. if( GetText().IsEmpty() )
  100. return false;
  101. // Build a temporary copy of the text for hit testing
  102. EDA_TEXT tmp_text( *this );
  103. // Reference designator text has one or 2 additional character (displays U? or U?A)
  104. if( m_id == REFERENCE )
  105. {
  106. const LIB_PART* parent = dynamic_cast<const LIB_PART*>( m_Parent );
  107. wxString extended_text = tmp_text.GetText();
  108. extended_text.Append('?');
  109. if ( parent && parent->GetUnitCount() > 1 )
  110. extended_text.Append('A');
  111. tmp_text.SetText( extended_text );
  112. }
  113. tmp_text.SetTextPos( DefaultTransform.TransformCoordinate( GetTextPos() ) );
  114. // The text orientation may need to be flipped if the transformation matrix causes xy axes
  115. // to be flipped. This simple algo works only for schematic matrix (rot 90 or/and mirror)
  116. bool t1 = ( DefaultTransform.x1 != 0 ) ^ ( GetTextAngle() != 0 );
  117. tmp_text.SetTextAngle( t1 ? TEXT_ANGLE_HORIZ : TEXT_ANGLE_VERT );
  118. return tmp_text.TextHitTest( aPosition, aAccuracy );
  119. }
  120. EDA_ITEM* LIB_FIELD::Clone() const
  121. {
  122. LIB_FIELD* newfield = new LIB_FIELD( m_id );
  123. Copy( newfield );
  124. return (EDA_ITEM*) newfield;
  125. }
  126. void LIB_FIELD::Copy( LIB_FIELD* aTarget ) const
  127. {
  128. aTarget->m_name = m_name;
  129. aTarget->CopyText( *this );
  130. aTarget->SetEffects( *this );
  131. aTarget->SetParent( m_Parent );
  132. }
  133. int LIB_FIELD::compare( const LIB_ITEM& aOther, LIB_ITEM::COMPARE_FLAGS aCompareFlags ) const
  134. {
  135. wxASSERT( aOther.Type() == LIB_FIELD_T );
  136. int retv = LIB_ITEM::compare( aOther, aCompareFlags );
  137. if( retv )
  138. return retv;
  139. const LIB_FIELD* tmp = ( LIB_FIELD* ) &aOther;
  140. if( m_id != tmp->m_id )
  141. return m_id - tmp->m_id;
  142. int result = GetText().CmpNoCase( tmp->GetText() );
  143. if( result != 0 )
  144. return result;
  145. if( GetTextPos().x != tmp->GetTextPos().x )
  146. return GetTextPos().x - tmp->GetTextPos().x;
  147. if( GetTextPos().y != tmp->GetTextPos().y )
  148. return GetTextPos().y - tmp->GetTextPos().y;
  149. if( GetTextWidth() != tmp->GetTextWidth() )
  150. return GetTextWidth() - tmp->GetTextWidth();
  151. if( GetTextHeight() != tmp->GetTextHeight() )
  152. return GetTextHeight() - tmp->GetTextHeight();
  153. return 0;
  154. }
  155. void LIB_FIELD::Offset( const wxPoint& aOffset )
  156. {
  157. EDA_TEXT::Offset( aOffset );
  158. }
  159. void LIB_FIELD::MoveTo( const wxPoint& newPosition )
  160. {
  161. EDA_TEXT::SetTextPos( newPosition );
  162. }
  163. void LIB_FIELD::MirrorHorizontal( const wxPoint& center )
  164. {
  165. int x = GetTextPos().x;
  166. x -= center.x;
  167. x *= -1;
  168. x += center.x;
  169. SetTextX( x );
  170. }
  171. void LIB_FIELD::MirrorVertical( const wxPoint& center )
  172. {
  173. int y = GetTextPos().y;
  174. y -= center.y;
  175. y *= -1;
  176. y += center.y;
  177. SetTextY( y );
  178. }
  179. void LIB_FIELD::Rotate( const wxPoint& center, bool aRotateCCW )
  180. {
  181. int rot_angle = aRotateCCW ? -900 : 900;
  182. wxPoint pt = GetTextPos();
  183. RotatePoint( &pt, center, rot_angle );
  184. SetTextPos( pt );
  185. SetTextAngle( GetTextAngle() != 0.0 ? 0 : 900 );
  186. }
  187. void LIB_FIELD::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
  188. const TRANSFORM& aTransform )
  189. {
  190. if( GetText().IsEmpty() )
  191. return;
  192. // Calculate the text orientation, according to the component orientation/mirror
  193. int orient = (int) GetTextAngle();
  194. if( aTransform.y1 ) // Rotate component 90 deg.
  195. {
  196. if( orient == TEXT_ANGLE_HORIZ )
  197. orient = TEXT_ANGLE_VERT;
  198. else
  199. orient = TEXT_ANGLE_HORIZ;
  200. }
  201. EDA_RECT BoundaryBox = GetBoundingBox();
  202. BoundaryBox.RevertYAxis();
  203. EDA_TEXT_HJUSTIFY_T hjustify = GR_TEXT_HJUSTIFY_CENTER;
  204. EDA_TEXT_VJUSTIFY_T vjustify = GR_TEXT_VJUSTIFY_CENTER;
  205. wxPoint textpos = aTransform.TransformCoordinate( BoundaryBox.Centre() ) + aOffset;
  206. COLOR4D color;
  207. if( aPlotter->GetColorMode() )
  208. color = aPlotter->RenderSettings()->GetLayerColor( GetDefaultLayer() );
  209. else
  210. color = COLOR4D::BLACK;
  211. int penWidth = std::max( GetPenWidth(),aPlotter->RenderSettings()->GetMinPenWidth() );
  212. aPlotter->Text( textpos, color, GetShownText(), orient, GetTextSize(), hjustify, vjustify,
  213. penWidth, IsItalic(), IsBold() );
  214. }
  215. wxString LIB_FIELD::GetFullText( int unit ) const
  216. {
  217. if( m_id != REFERENCE )
  218. return GetText();
  219. wxString text = GetText();
  220. text << wxT( "?" );
  221. wxCHECK( GetParent(), text );
  222. if( GetParent()->IsMulti() )
  223. text << LIB_PART::SubReference( unit );
  224. return text;
  225. }
  226. const EDA_RECT LIB_FIELD::GetBoundingBox() const
  227. {
  228. /* Y coordinates for LIB_ITEMS are bottom to top, so we must invert the Y position when
  229. * calling GetTextBox() that works using top to bottom Y axis orientation.
  230. */
  231. EDA_RECT rect = GetTextBox( -1, true );
  232. rect.RevertYAxis();
  233. // We are using now a bottom to top Y axis.
  234. wxPoint orig = rect.GetOrigin();
  235. wxPoint end = rect.GetEnd();
  236. RotatePoint( &orig, GetTextPos(), -GetTextAngle() );
  237. RotatePoint( &end, GetTextPos(), -GetTextAngle() );
  238. rect.SetOrigin( orig );
  239. rect.SetEnd( end );
  240. // We are using now a top to bottom Y axis:
  241. rect.RevertYAxis();
  242. return rect;
  243. }
  244. void LIB_FIELD::ViewGetLayers( int aLayers[], int& aCount ) const
  245. {
  246. aCount = 2;
  247. switch( m_id )
  248. {
  249. case REFERENCE: aLayers[0] = LAYER_REFERENCEPART; break;
  250. case VALUE: aLayers[0] = LAYER_VALUEPART; break;
  251. default: aLayers[0] = LAYER_FIELDS; break;
  252. }
  253. aLayers[1] = LAYER_SELECTION_SHADOWS;
  254. }
  255. SCH_LAYER_ID LIB_FIELD::GetDefaultLayer()
  256. {
  257. switch( m_id )
  258. {
  259. case REFERENCE: return LAYER_REFERENCEPART;
  260. case VALUE: return LAYER_VALUEPART;
  261. default: return LAYER_FIELDS;
  262. }
  263. }
  264. wxString LIB_FIELD::GetName( bool aUseDefaultName ) const
  265. {
  266. if( m_name.IsEmpty() && aUseDefaultName )
  267. return TEMPLATE_FIELDNAME::GetDefaultFieldName( m_id );
  268. return m_name;
  269. }
  270. wxString LIB_FIELD::GetCanonicalName() const
  271. {
  272. switch( m_id )
  273. {
  274. case REFERENCE: return wxT( "Reference" );
  275. case VALUE: return wxT( "Value" );
  276. case FOOTPRINT: return wxT( "Footprint" );
  277. case DATASHEET: return wxT( "Datasheet" );
  278. }
  279. return m_name;
  280. }
  281. void LIB_FIELD::SetName( const wxString& aName )
  282. {
  283. // Mandatory field names are fixed.
  284. if( IsMandatory() )
  285. {
  286. wxFAIL_MSG( "trying to set a MANDATORY_FIELD's name\n" );
  287. return;
  288. }
  289. if( m_name != aName )
  290. {
  291. m_name = aName;
  292. SetModified();
  293. }
  294. }
  295. wxString LIB_FIELD::GetSelectMenuText( EDA_UNITS aUnits ) const
  296. {
  297. return wxString::Format( "%s '%s'",
  298. GetName(),
  299. ShortenedShownText() );
  300. }
  301. void LIB_FIELD::BeginEdit( const wxPoint aPosition )
  302. {
  303. SetTextPos( aPosition );
  304. }
  305. void LIB_FIELD::CalcEdit( const wxPoint& aPosition )
  306. {
  307. SetTextPos( aPosition );
  308. }
  309. void LIB_FIELD::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, MSG_PANEL_ITEMS& aList )
  310. {
  311. wxString msg;
  312. LIB_ITEM::GetMsgPanelInfo( aFrame, aList );
  313. // Display style:
  314. msg = GetTextStyleName();
  315. aList.push_back( MSG_PANEL_ITEM( _( "Style" ), msg, MAGENTA ) );
  316. msg = MessageTextFromValue( aFrame->GetUserUnits(), GetTextWidth() );
  317. aList.push_back( MSG_PANEL_ITEM( _( "Width" ), msg, BLUE ) );
  318. msg = MessageTextFromValue( aFrame->GetUserUnits(), GetTextHeight() );
  319. aList.push_back( MSG_PANEL_ITEM( _( "Height" ), msg, BLUE ) );
  320. // Display field name (ref, value ...)
  321. aList.push_back( MSG_PANEL_ITEM( _( "Field" ), GetName(), BROWN ) );
  322. // Display field text:
  323. aList.push_back( MSG_PANEL_ITEM( _( "Value" ), GetShownText(), BROWN ) );
  324. }
  325. BITMAP_DEF LIB_FIELD::GetMenuImage() const
  326. {
  327. return move_xpm;
  328. }
  329. bool LIB_FIELD::IsMandatory() const
  330. {
  331. return m_id >= 0 && m_id < MANDATORY_FIELDS;
  332. }