1 /*
2 VeroRoute - Qt based Veroboard/Perfboard/PCB layout & routing application.
3
4 Copyright (C) 2017 Alex Lawrow ( dralx@users.sourceforge.net )
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "pindialog.h"
21 #include "ui_pindialog.h"
22 #include "mainwindow.h"
23
PinDialog(MainWindow * parent)24 PinDialog::PinDialog(MainWindow* parent)
25 : QDialog(parent)
26 , ui(new Ui_PinDialog)
27 , m_pMainWindow(parent)
28 {
29 ui->setupUi(this);
30
31 QObject::connect(ui->tableWidget, SIGNAL(cellChanged(int,int)), this, SLOT(CellChanged(int,int)));
32 }
33
~PinDialog()34 PinDialog::~PinDialog()
35 {
36 delete ui;
37 }
38
GetUserComp() const39 Component* PinDialog::GetUserComp() const
40 {
41 if ( m_pMainWindow->m_board.GetGroupMgr().GetNumUserComps() != 1 ) return nullptr; // Need single component selected
42 Component& comp = m_pMainWindow->m_board.GetUserComponent();
43 return ( (comp.GetPinFlags() & PIN_LABELS) > 0 ) ? &comp : nullptr; // Component must allow pin labels to be drawn
44 }
45
CellChanged(int row,int col)46 void PinDialog::CellChanged(int row, int col)
47 {
48 if ( col == 0 ) return;
49
50 Component* pComp = GetUserComp(); assert( pComp );
51 QTableWidgetItem* pItemLabel = ui->tableWidget->item(row, col);
52 const size_t iPinIndex = static_cast<size_t>(row);
53 const std::string strLabel = pItemLabel->text().toStdString();
54
55 if ( col == 1 )
56 {
57 if ( pComp->GetPinLabel(iPinIndex) != strLabel ) // If changed
58 {
59 pComp->SetPinLabel(iPinIndex, strLabel);
60 m_pMainWindow->RepaintSkipRouting();
61 }
62 }
63 if ( col == 2)
64 {
65 const int iAlign = ( strLabel == "L" || strLabel == "l" ) ? Qt::AlignLeft :
66 ( strLabel == "R" || strLabel == "r" ) ? Qt::AlignRight : Qt::AlignHCenter;
67 if ( pComp->GetPinAlign(iPinIndex) != iAlign ) // If changed
68 {
69 pComp->SetPinAlign(iPinIndex, iAlign);
70 m_pMainWindow->RepaintSkipRouting();
71 }
72 if ( strLabel != "L" && strLabel != "R" && strLabel != "C" )
73 Update(); // Enforce L,R,C in GUI
74 }
75 }
76
Update()77 void PinDialog::Update()
78 {
79 Component* pComp = GetUserComp();
80 const size_t numPins = ( pComp ) ? pComp->GetNumPins() : 0;
81 // Set up the table
82 ui->tableWidget->clear();
83 ui->tableWidget->setRowCount(static_cast<int>(numPins));
84 ui->tableWidget->setColumnCount(3);
85 ui->tableWidget->setColumnWidth(0,40);
86 ui->tableWidget->setColumnWidth(1,105);
87 ui->tableWidget->setColumnWidth(2,50);
88 m_tableHeader << "Pin" << "Label" << "Align";
89 ui->tableWidget->setHorizontalHeaderLabels(m_tableHeader);
90 ui->tableWidget->verticalHeader()->setVisible(false);
91 ui->tableWidget->setEditTriggers(QAbstractItemView::AllEditTriggers);
92 ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
93 ui->tableWidget->setSelectionMode(QAbstractItemView::NoSelection);
94 ui->tableWidget->setShowGrid(true);
95
96 // Populate the table with data
97 for (size_t iPinIndex = 0; iPinIndex < numPins; iPinIndex++)
98 {
99 const int& iAlign = pComp->GetPinAlign(iPinIndex);
100 for (int iCol = 0; iCol < 3; iCol++)
101 {
102 std::string str;
103 switch( iCol )
104 {
105 case 0: str = CompTypes::GetDefaultPinLabel(iPinIndex); break;
106 case 1: str = pComp->GetPinLabel(iPinIndex); break;
107 case 2: str = ( iAlign == Qt::AlignLeft ) ? "L" :
108 ( iAlign == Qt::AlignRight ) ? "R" :"C"; break;
109 }
110 auto pItem = new QTableWidgetItem(QString::fromStdString(str));
111 if ( iCol != 1 )
112 pItem->setData(Qt::TextAlignmentRole, Qt::AlignCenter);
113
114 if ( iCol == 0 )
115 pItem->setFlags(Qt::NoItemFlags);
116 else
117 pItem->setFlags(Qt::ItemIsEditable | Qt::ItemIsEnabled);
118
119 ui->tableWidget->setItem(static_cast<int>(iPinIndex), iCol, pItem);
120 }
121 }
122 }
123
keyPressEvent(QKeyEvent * event)124 void PinDialog::keyPressEvent(QKeyEvent* event)
125 {
126 m_pMainWindow->specialKeyPressEvent(event);
127 QDialog::keyPressEvent(event);
128 }
129
keyReleaseEvent(QKeyEvent * event)130 void PinDialog::keyReleaseEvent(QKeyEvent* event)
131 {
132 m_pMainWindow->commonKeyReleaseEvent(event);
133 QDialog::keyReleaseEvent(event);
134 }
135