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.

188 lines
5.5 KiB

  1. /**
  2. * @file fp_conflict_assignment_selector.cpp
  3. */
  4. /*
  5. * This program source code file is part of KICAD, a free EDA CAD application.
  6. *
  7. * Copyright (C) 2010-2014 Jean-Pierre Charras <jp.charras at wanadoo.fr>
  8. * Copyright (C) 1992-2014 Kicad Developers, see AUTHORS.TXT for contributors.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, you may find one here:
  22. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  23. * or you may search the http://www.gnu.org website for the version 2 license,
  24. * or you may write to the Free Software Foundation, Inc.,
  25. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  26. */
  27. #include <common.h>
  28. #include <fp_conflict_assignment_selector.h>
  29. DIALOG_FP_CONFLICT_ASSIGNMENT_SELECTOR::DIALOG_FP_CONFLICT_ASSIGNMENT_SELECTOR( wxWindow* aParent )
  30. : DIALOG_FP_CONFLICT_ASSIGNMENT_SELECTOR_BASE( aParent )
  31. {
  32. m_listFp->AppendColumn( _( "Ref" ) );
  33. m_listFp->AppendColumn( _( "Schematic assignment" ) );
  34. m_listFp->AppendColumn( wxT( "<=" ) );
  35. m_listFp->AppendColumn( wxT( "=>" ) );
  36. m_listFp->AppendColumn( _( "Cmp file assignment" ) );
  37. m_lineCount = 0;
  38. }
  39. void DIALOG_FP_CONFLICT_ASSIGNMENT_SELECTOR::Add( const wxString& aRef, const wxString& aFpSchName,
  40. const wxString& aFpCmpName )
  41. {
  42. long idx = m_listFp->InsertItem(m_lineCount, aRef );
  43. m_listFp->SetItem(idx, COL_FPSCH, aFpSchName );
  44. m_listFp->SetItem(idx, COL_SELSCH, wxT("") );
  45. m_listFp->SetItem(idx, COL_SELCMP, wxT("X") );
  46. m_listFp->SetItem(idx, COL_FPCMP, aFpCmpName );
  47. m_lineCount ++;
  48. }
  49. int DIALOG_FP_CONFLICT_ASSIGNMENT_SELECTOR::GetSelection( const wxString& aReference )
  50. {
  51. // Find Reference
  52. for( int ii = 0; ii < m_listFp->GetItemCount(); ii++ )
  53. {
  54. if( m_listFp->GetItemText( ii, COL_REF ) == aReference )
  55. {
  56. if( m_listFp->GetItemText( ii, COL_SELSCH ) != wxT("X") )
  57. return 1;
  58. return 0;
  59. }
  60. }
  61. return -1;
  62. }
  63. void DIALOG_FP_CONFLICT_ASSIGNMENT_SELECTOR::OnColumnClick( wxListEvent& event )
  64. {
  65. // When clicking on the column title:
  66. // when it is the COL_SELCMP column, set all item choices to cmp file assignment.
  67. // when it is the COL_SELSCH column, set all item choices to schematic assignment.
  68. int column = event.GetColumn();
  69. int colclr, colset;
  70. switch( column )
  71. {
  72. case COL_SELSCH:
  73. colclr = COL_SELCMP;
  74. colset = COL_SELSCH;
  75. break;
  76. case COL_SELCMP:
  77. colclr = COL_SELSCH;
  78. colset = COL_SELCMP;
  79. break;
  80. default:
  81. return;
  82. }
  83. for( int i = 0; i < m_listFp->GetItemCount(); i++ )
  84. {
  85. m_listFp->SetItem( i, colclr, wxT("") );
  86. m_listFp->SetItem( i, colset, wxT("X") );
  87. }
  88. }
  89. void DIALOG_FP_CONFLICT_ASSIGNMENT_SELECTOR::OnItemClicked( wxMouseEvent& event )
  90. {
  91. wxPoint pos = event.GetPosition();
  92. int flgs = wxLIST_HITTEST_ONITEMLABEL;
  93. long idx = m_listFp->HitTest( pos, flgs );
  94. // Try to find the column clicked (must be COL_SELCMP or COL_SELSCH)
  95. int colclr = -1, colset = -1;
  96. int minpx = m_listFp->GetColumnWidth( 0 ) + m_listFp->GetColumnWidth( 1 );
  97. int maxpx = minpx + m_listFp->GetColumnWidth( 2 );
  98. if( pos.x > minpx && pos.x < maxpx )
  99. {
  100. colclr = COL_SELCMP;
  101. colset = COL_SELSCH;
  102. }
  103. else
  104. {
  105. minpx = maxpx;
  106. maxpx = minpx + m_listFp->GetColumnWidth( 3 );
  107. if( pos.x > minpx && pos.x < maxpx )
  108. {
  109. colclr = COL_SELSCH;
  110. colset = COL_SELCMP;
  111. }
  112. }
  113. if( colclr < 0 )
  114. return;
  115. // Move selection to schematic or cmp file choice
  116. // according to the column position (COL_SELCMP or COL_SELSCH)
  117. m_listFp->SetItem( idx, colclr, wxT("") );
  118. m_listFp->SetItem( idx, colset, wxT("X") );
  119. event.Skip();
  120. }
  121. void DIALOG_FP_CONFLICT_ASSIGNMENT_SELECTOR::OnSize( wxSizeEvent& aEvent )
  122. {
  123. recalculateColumns();
  124. aEvent.Skip();
  125. }
  126. void DIALOG_FP_CONFLICT_ASSIGNMENT_SELECTOR::recalculateColumns()
  127. {
  128. const int margin = 16;
  129. int totalLength = 0;
  130. int sel_length = KIUI::GetTextSize( wxT( "XX" ), m_listFp ).x;
  131. int maxRefLength = KIUI::GetTextSize( wxT( "XXX" ), m_listFp ).x;
  132. sel_length += margin;
  133. m_listFp->SetColumnWidth( COL_SELSCH, sel_length );
  134. m_listFp->SetColumnWidth( COL_SELCMP, sel_length );
  135. // Find max character width of column Reference
  136. for( int i = 0; i < m_listFp->GetItemCount(); i++ )
  137. {
  138. int length = KIUI::GetTextSize( m_listFp->GetItemText( i, COL_REF ), m_listFp ).x;
  139. if( length > maxRefLength )
  140. maxRefLength = length;
  141. }
  142. // Use the lengths of column texts to create a scale of the max list width
  143. // to set the column widths
  144. maxRefLength += margin;
  145. totalLength = maxRefLength + sel_length + sel_length;
  146. int cwidth = (GetClientSize().x - totalLength) / 2;
  147. m_listFp->SetColumnWidth( COL_REF, maxRefLength );
  148. m_listFp->SetColumnWidth( COL_FPSCH, cwidth - 2 );
  149. m_listFp->SetColumnWidth( COL_FPCMP, cwidth );
  150. }