-
Notifications
You must be signed in to change notification settings - Fork 2
/
gsysPortViewer.cc
196 lines (177 loc) · 6.55 KB
/
gsysPortViewer.cc
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
** This file is part of gSysC.
**
** gSysC 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 2 of the License, or
** (at your option) any later version.
**
** gSysC 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 gSysC with the file ``LICENSE''; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "gsysPortViewer.moc"
#include "gsysPortViewer.h"
#include "gsysPort.h"
#include "gsysMain.h"
#include "gsysSimulator.h"
/*
* Constructs a gsysPortViewer as a child of 'parent', with the
* name 'name' and widget flags set to 'f'.
*
* The dialog will by default be modeless, unless you set 'modal' to
* TRUE to construct a modal dialog.
*/
gsysPortViewer::gsysPortViewer( QWidget* parent, const char* name, bool modal, Qt::WindowFlags fl )
: QDialog( parent, fl )
{
setModal(modal);
if ( !name ) setObjectName( "gsysPortViewer" );
gsysPortViewerLayout = new QVBoxLayout( this );
gsysPortViewerLayout->setObjectName("gsysPortViewerLayout");
gsysPortViewerLayout->setSpacing(6);
gsysPortViewerLayout->setMargin(11);
portVec.clear();
table1 = new QTableWidget( this );
table1->setObjectName("table1");
table1->setRowCount( 0 );
table1->setColumnCount( 3 );
table1->setHorizontalHeaderLabels(QStringList(QList<QString> {"Port name", "Value", "ID"}));
table1->setEditTriggers(QAbstractItemView::NoEditTriggers);
table1->setSelectionMode((QTableWidget::SelectionMode) 2); // SingleRow
gsysPortViewerLayout->addWidget( table1 );
frame4 = new QFrame( this );
frame4->setObjectName("frame4");
frame4->setFrameShape( QFrame::StyledPanel );
frame4->setFrameShadow( QFrame::Raised );
frame4Layout = new QHBoxLayout( frame4);
frame4Layout->setObjectName("frame4Layout");
frame4Layout->setSpacing(6);
frame4Layout->setMargin(11);
comboBox1 = new QComboBox( frame4 );
comboBox1->setObjectName("comboBox1");
comboBox1->insertItem(0, tr("all") );
frame4Layout->addWidget( comboBox1 );
pushButton1 = new QPushButton( "pushButton1", frame4 );
frame4Layout->addWidget( pushButton1 );
QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Fixed, QSizePolicy::Minimum );
frame4Layout->addItem( spacer );
pushButton2 = new QPushButton( "pushButton2", frame4 );
frame4Layout->addWidget( pushButton2 );
gsysPortViewerLayout->addWidget( frame4 );
languageChange();
resize( QSize(409, 289).expandedTo(minimumSizeHint()) );
// connect signals and slots
connect( pushButton1, SIGNAL( clicked() ), this, SLOT( pushButton1_clicked() ) );
connect( pushButton2, SIGNAL( clicked() ), this, SLOT( pushButton2_clicked() ) );
}
/*
* Destroys the object and frees any allocated resources
*/
gsysPortViewer::~gsysPortViewer()
{
// no need to delete child widgets, Qt does it all for us
this->hide();
portVec.clear();
}
/*
* add the chosen port in the comboBox to the table of ports
*/
void gsysPortViewer::addPort(gsysPort* port)
{
comboBox1->insertItem(0, port->getName());
portVec.push_back(port);
}
/*
* refresh all port values in the table
*/
void gsysPortViewer::refresh()
{
#ifdef DEBUG_GSYSC
cout<<"Function refresh() called!"<<endl;
#endif
if(this->isVisible())
{
gsysPort* aktPort;
for(int i=0; i<table1->rowCount(); i++)
{
aktPort = (gsysPort*) atoi(table1->item(i,2)->text().toStdString().c_str());
table1->setItem(i,1,new QTableWidgetItem( QString( aktPort->getValue() ) ) );
#ifdef DEBUG_GSYSC
cout<<"REFRESH => Port "<<aktPort->getName()<<" ("<<aktPort<<") has value "<<aktPort->getValue()<<endl;
#endif
}
aktPort = 0;
}
#ifdef DEBUG_GSYSC
cout<<"Funktion refresh() finished!"<<endl;
#endif
}
/*
* Sets the strings of the subwidgets using the current
* language.
*/
void gsysPortViewer::languageChange()
{
setWindowTitle( tr( "Port observation" ) );
table1->setHorizontalHeaderLabels(QStringList(QList<QString> {"Port name", "Value"}));
pushButton1->setText( tr( "&Add" ) );
pushButton2->setText( tr( "&Remove" ) );
comboBox1->setItemText( 0, tr("all") );
}
/*
* EventHandler of the add-button
*/
void gsysPortViewer::pushButton1_clicked()
{
if(comboBox1->currentIndex() == 0) // add all ports, if wanted
{
#ifdef DEBUG_GSYSC
cout << "Add all items in the table. That means "<<portVec.size()<<" elements!"<<endl;
#endif
gsysPort* aktPort;
int maxIdx = portVec.size();
for(int idx=0; idx<maxIdx; idx++)
{
aktPort = portVec.front();
comboBox1->removeItem(1);
table1->setRowCount( table1->rowCount() + 1 );
table1->setItem(table1->rowCount()-1, 0, new QTableWidgetItem( QString( aktPort->getName() ) ) );
table1->setItem(table1->rowCount()-1, 1, new QTableWidgetItem( QString( aktPort->getValue() ) ) );
table1->setItem(table1->rowCount()-1, 2, new QTableWidgetItem( QString( (new gsysMain())->getSimulator()->asChar((int64_t)aktPort))));
portVec.erase(portVec.begin());
}
aktPort = 0;
}
else // anderen Port hinzufügen
{
int idx = comboBox1->currentIndex();
gsysPort* aktPort = portVec[idx-1];
comboBox1->removeItem(idx);
table1->setRowCount( table1->rowCount() + 1 );
table1->setItem(table1->rowCount()-1, 0, new QTableWidgetItem( QString( aktPort->getName() ) ) );
table1->setItem(table1->rowCount()-1, 1, new QTableWidgetItem( QString( aktPort->getValue() ) ) );
table1->setItem(table1->rowCount()-1, 2, new QTableWidgetItem( QString( (new gsysMain())->getSimulator()->asChar((int64_t)aktPort) ) ) );
portVec.erase(portVec.begin()+idx-1);
}
}
/*
* EventHandler of the remove-button
*/
void gsysPortViewer::pushButton2_clicked()
{
#ifdef DEBUG_GSYSC
cout<<"Removal in row "<<table1->currentRow()<<"; Name: "<<(QString) table1->item(table1->currentRow(),0)<<"; ID: "<<(QString) table1->item(table1->currentRow(),2)<<endl;
#endif
gsysPort* aktPort;
int addr = atoi(table1->item(table1->currentRow(),2)->text().toStdString().c_str());
comboBox1->insertItem(0, table1->item(table1->currentRow(),0)->text());
aktPort = (gsysPort*)addr;
portVec.push_back(aktPort);
table1->removeRow(table1->currentRow());
}