File: pindialog.cpp

package info (click to toggle)
veroroute 2.38-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,044 kB
  • sloc: cpp: 21,512; xml: 89; sh: 65; lisp: 20; makefile: 5
file content (168 lines) | stat: -rw-r--r-- 5,852 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
	VeroRoute - Qt based Veroboard/Perfboard/PCB layout & routing application.

	Copyright (C) 2017  Alex Lawrow    ( dralx@users.sourceforge.net )

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "pindialog.h"
#include "ui_pindialog.h"
#include "mainwindow.h"

PinDialog::PinDialog(QWidget* parent)
: QWidget(parent)
, ui(new Ui_PinDialog)
, m_pMainWindow(nullptr)
{
	ui->setupUi( reinterpret_cast<QDialog*>(this) );
#ifdef VEROROUTE_ANDROID
	ui->tableWidget->verticalScrollBar()->setStyleSheet( ANDROID_VSCROLL_WIDTH );
	ui->tableWidget->horizontalScrollBar()->setStyleSheet( ANDROID_HSCROLL_HEIGHT );
#endif
}

PinDialog::~PinDialog()
{
	delete ui;
}

void PinDialog::SetMainWindow(MainWindow* p)
{
	m_pMainWindow = p;
	QObject::connect(ui->tableWidget,	SIGNAL(cellChanged(int,int)),	this,	SLOT(CellChanged(int,int)));
}

Component* PinDialog::GetUserComp() const
{
	if ( m_pMainWindow->m_board.GetGroupMgr().GetNumUserComps() != 1 ) return nullptr;	// Need single component selected
	Component& comp = m_pMainWindow->m_board.GetUserComponent();
	return ( (comp.GetPinFlags() & PIN_LABELS) > 0 ) ? &comp : nullptr;	// Component must allow pin labels to be drawn
}

void PinDialog::CellChanged(int row, int col)
{
	if ( col == 0 ) return;

	const bool& bCompEdit = m_pMainWindow->m_board.GetCompEdit();

	CompDefiner*	pDef	= bCompEdit ? &m_pMainWindow->m_board.GetCompDefiner() : nullptr;
	Component*		pComp	= bCompEdit ? nullptr : GetUserComp();
	assert(pDef != nullptr || pComp != nullptr);

	QTableWidgetItem*	pItemLabel	= ui->tableWidget->item(row, col);
	const size_t		iPinIndex	= static_cast<size_t>(row);
	const std::string	strLabel	= pItemLabel->text().toStdString();
	const int			objId		= static_cast<int>(iPinIndex) + ( pComp ? ( pComp->GetId() * 1000000 ) : 0);

	if ( col == 1 )
	{
		const std::string& s = pDef ? pDef->GetPinLabel(iPinIndex) : pComp->GetPinLabel(iPinIndex);
		if ( s != strLabel ) // If changed
		{
			if ( pDef  ) pDef->SetPinLabel(iPinIndex, strLabel);
			if ( pComp ) pComp->SetPinLabel(iPinIndex, strLabel);
			m_pMainWindow->UpdateHistory("change pin label", objId);
			m_pMainWindow->RepaintSkipRouting();
		}
	}
	if ( col == 2)
	{
		const int iAlign = ( strLabel == "L" || strLabel == "l" ) ? Qt::AlignLeft  :
						   ( strLabel == "R" || strLabel == "r" ) ? Qt::AlignRight : Qt::AlignHCenter;
		const int& i = pDef ? pDef->GetPinAlign(iPinIndex) : pComp->GetPinAlign(iPinIndex);
		if ( i != iAlign ) // If changed
		{
			if ( pDef )  pDef->SetPinAlign(iPinIndex, iAlign);
			if ( pComp ) pComp->SetPinAlign(iPinIndex, iAlign);
			m_pMainWindow->UpdateHistory("change pin label alignment", objId);
			m_pMainWindow->RepaintSkipRouting();
		}
		if ( strLabel != "L" && strLabel != "R" && strLabel != "C" )
			Update();	// Enforce L,R,C in GUI
	}
}

void PinDialog::Update()
{
	const bool& bCompEdit = m_pMainWindow->m_board.GetCompEdit();

	CompDefiner*	pDef	= bCompEdit ? &m_pMainWindow->m_board.GetCompDefiner() : nullptr;
	Component*		pComp	= bCompEdit ? nullptr : GetUserComp();
	const size_t	numPins	= pDef ? pDef->GetNumPins() : pComp ? pComp->GetNumPins() : 0;
	assert(pDef != nullptr || pComp != nullptr || numPins == 0);

	// Set up the table
	ui->tableWidget->clear();
	ui->tableWidget->setRowCount(static_cast<int>(numPins));
	ui->tableWidget->setColumnCount(3);
	ui->tableWidget->setColumnWidth(0,40);
	ui->tableWidget->setColumnWidth(1,105);
	ui->tableWidget->setColumnWidth(2,65);	// Allow for vertical scroll bar
	m_tableHeader << "Pin" << "Label" << "Align";
	ui->tableWidget->setHorizontalHeaderLabels(m_tableHeader);
	ui->tableWidget->verticalHeader()->setVisible(false);
	ui->tableWidget->setEditTriggers(QAbstractItemView::AllEditTriggers);
	ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
	ui->tableWidget->setSelectionMode(QAbstractItemView::NoSelection);
	ui->tableWidget->setShowGrid(true);

	// Populate the table with data
	for (size_t iPinIndex = 0; iPinIndex < numPins; iPinIndex++)
	{
		const int& iAlign = pDef ? pDef->GetPinAlign(iPinIndex) : pComp->GetPinAlign(iPinIndex);
		for (int iCol = 0; iCol < 3; iCol++)
		{
			std::string str;
			switch( iCol )
			{
				case 0:	str = CompTypes::GetDefaultPinLabel(iPinIndex);	break;
				case 1:	str = pDef ? pDef->GetPinLabel(iPinIndex) : pComp->GetPinLabel(iPinIndex);	break;
				case 2:	str = ( iAlign == Qt::AlignLeft  ) ? "L" :
							  ( iAlign == Qt::AlignRight ) ? "R" :"C";	break;
			}
			auto pItem = new QTableWidgetItem(QString::fromStdString(str));
			if ( iCol != 1 )
				pItem->setData(Qt::TextAlignmentRole, Qt::AlignCenter);

			if ( iCol == 0 )
				pItem->setFlags(Qt::NoItemFlags);
			else
				pItem->setFlags(Qt::ItemIsEditable | Qt::ItemIsEnabled);

			ui->tableWidget->setItem(static_cast<int>(iPinIndex), iCol, pItem);
		}
	}
}

void PinDialog::keyPressEvent(QKeyEvent* event)
{
#ifndef VEROROUTE_ANDROID
	m_pMainWindow->specialKeyPressEvent(event);
#endif
	QWidget::keyPressEvent(event);
	event->accept();
}

void PinDialog::keyReleaseEvent(QKeyEvent* event)
{
#ifdef VEROROUTE_ANDROID
	if ( event->key() == Qt::Key_Back )
		return m_pMainWindow->keyReleaseEvent(event);	// Try Undo operation
#else
	m_pMainWindow->commonKeyReleaseEvent(event);
#endif
	QWidget::keyReleaseEvent(event);
	event->accept();
}