-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcwidgetscomlist.cpp
More file actions
433 lines (321 loc) · 14.2 KB
/
cwidgetscomlist.cpp
File metadata and controls
433 lines (321 loc) · 14.2 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#include "cwidgetscomlist.h"
CWidgetScomList::CWidgetScomList(QWidget *parent) : QWidget(parent)
{
QGridLayout *qGridLayoutMain = new QGridLayout();
QVBoxLayout *qVBoxLayout = new QVBoxLayout();
qVBoxLayout->addStretch(1);
//(0,0) Insert Button
qPushButtonInsert = new QPushButton(this);
qPushButtonInsert->setText("Insert");
qVBoxLayout->addWidget(qPushButtonInsert, 0);
qVBoxLayout->addStretch(1);
//(1, 0) Copy Button
qPushButtonCopy = new QPushButton(this);
qPushButtonCopy->setText("Copy");
qVBoxLayout->addWidget(qPushButtonCopy, 0);
qVBoxLayout->addStretch(1);
//(2,0) Remove Button
qPushButtonRemove = new QPushButton(this);
qPushButtonRemove->setText("Remove");
qVBoxLayout->addWidget(qPushButtonRemove, 0);
qVBoxLayout->addStretch(4);
qGridLayoutMain->addLayout(qVBoxLayout, 0, 0);
//(0,1) Scom List table
QGridLayout *qGridLayoutSub1 = new QGridLayout();
QHBoxLayout *qHBoxLayout = new QHBoxLayout();
qHBoxLayout->addStretch(3);
qPushButtonLoad = new QPushButton(this);
qPushButtonLoad->setText("Load");
qHBoxLayout->addWidget(qPushButtonLoad, 0);
qHBoxLayout->addStretch(8);
qPushButtonSave = new QPushButton(this);
qPushButtonSave->setText("Save");
qHBoxLayout->addWidget(qPushButtonSave, 0);
qGridLayoutSub1->addLayout(qHBoxLayout, 0, 0, 1, 1);
qTableViewScomList = new QTableWidget(this);
qTableViewScomList->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
// qTableViewScomList->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
// qTableViewScomList->horizontalHeader()->setHighlightSections(true);
// qTableViewScomList->horizontalHeader()->setStretchLastSection(true);
qTableViewScomList->horizontalHeader()->resizeSections(QHeaderView::Stretch);
sListCommandListHeader << "CMDID" << "ADDR" << "DATA" << "FILE"
<< "EXPACK" << "EXPDATA"
<< "DELAY" << "TIMES" << "WAIT" << "CRITICAL";
qTableViewScomList->setRowCount(1);
qTableViewScomList->setColumnCount(sListCommandListHeader.size());
qTableViewScomList->setHorizontalHeaderLabels(sListCommandListHeader);
qGridLayoutSub1->addWidget(qTableViewScomList, 1, 0, 1, 1);
qGridLayoutMain->addLayout(qGridLayoutSub1, 0, 1, 1, 1);
//Finish layout setting
setLayout(qGridLayoutMain);
//Connect Signals
connect(qPushButtonInsert, SIGNAL(clicked(bool)), this, SLOT(PrivateSlotInsertScomQueueItem()));
connect(qPushButtonRemove, SIGNAL(clicked(bool)), this, SLOT(PrivateSlotRemoveScomQueueItem()));
connect(qPushButtonCopy, SIGNAL(clicked(bool)), this, SLOT(PrivateSlotCopyScomQueueItem()));
connect(qPushButtonSave,SIGNAL(clicked(bool)), this, SLOT(PrivateSlotSaveList()));
connect(qPushButtonLoad, SIGNAL(clicked(bool)), this, SLOT(PrivateSlotLoadList()));
//Initialize command list
bCommandListRun = false;
}
void CWidgetScomList::OutputDebugLog(QString sFunction, QString sType, e_Error Code)
{
QString sStringOutput;
sStringOutput = QString("[%3]%1[%2][0x%4]").arg(CScomList_DEBUG_PREFIX, sFunction, sType, QString::number(Code,16));
qDebug() << sStringOutput;
//Emit signal to log box receiving slot
emit SignalLogOutput(sStringOutput);
return;
}
void CWidgetScomList::OutputDebugLog(QString sFunction, QString sType, QString sText)
{
QString sStringOutput;
sStringOutput = QString("[%3]%1[%2][%4]").arg(CScomList_DEBUG_PREFIX, sFunction, sType, sText);
qDebug() << sStringOutput;
//Emit signal to log box receiving slot
emit SignalLogOutput(sStringOutput);
return;
}
void CWidgetScomList::PrivateSlotInsertScomQueueItem()
{
emit SignalAskForScomQueueItemFromEditorToList();
}
void CWidgetScomList::PrivateSlotRemoveScomQueueItem()
{
if(!qTableViewScomList->item(qTableViewScomList->currentRow(),0) || qTableViewScomList->item(qTableViewScomList->currentRow(),0)->text().isEmpty())
{
//Empty command ID, error?
}
else
{
qTableViewScomList->removeRow(qTableViewScomList->currentRow());
}
}
bool CWidgetScomList::PrivateSlotCopyScomQueueItem()
{
ScomEditData mScomEditData;
bool bRet = true;
//Get current selected item intable
if(qTableViewScomList->currentRow() < 0)
{
qTableViewScomList->setCurrentCell(0,0);
}
if(!qTableViewScomList->item(qTableViewScomList->currentRow(),0) || qTableViewScomList->item(qTableViewScomList->currentRow(),0)->text().isEmpty())
{
//Empty command ID, error?
bRet = false;
}
else
{
//Collect all items in the current selected row and emit signal
mScomEditData.qStringCmdID = qTableViewScomList->item(qTableViewScomList->currentRow(),sListCommandListHeader.indexOf("CMDID"))->text();
mScomEditData.qStringAddr = qTableViewScomList->item(qTableViewScomList->currentRow(),sListCommandListHeader.indexOf("ADDR"))->text();
mScomEditData.qStringData = qTableViewScomList->item(qTableViewScomList->currentRow(),sListCommandListHeader.indexOf("DATA"))->text();
mScomEditData.qStringFile = qTableViewScomList->item(qTableViewScomList->currentRow(),sListCommandListHeader.indexOf("FILE"))->text();
mScomEditData.qStringExpACK = qTableViewScomList->item(qTableViewScomList->currentRow(),sListCommandListHeader.indexOf("EXPACK"))->text();
mScomEditData.qStringExpData = qTableViewScomList->item(qTableViewScomList->currentRow(),sListCommandListHeader.indexOf("EXPDATA"))->text();
mScomEditData.qStringDelayAfter = qTableViewScomList->item(qTableViewScomList->currentRow(),sListCommandListHeader.indexOf("DELAY"))->text();
mScomEditData.qStringMaxRepeatTimes = qTableViewScomList->item(qTableViewScomList->currentRow(),sListCommandListHeader.indexOf("TIMES"))->text();
QString qStringTmp = qTableViewScomList->item(qTableViewScomList->currentRow(),sListCommandListHeader.indexOf("CRITICAL"))->text();
mScomEditData.bCritical = qStringTmp.contains('Y');
qStringTmp = qTableViewScomList->item(qTableViewScomList->currentRow(),sListCommandListHeader.indexOf("WAIT"))->text();
mScomEditData.bWaitUntil = qStringTmp.contains('Y');
//Emit signal to scom edit box
emit SignalMoveScomQueueItemFromListToEditor(mScomEditData);
}
return bRet;
}
void CWidgetScomList::PrivateSlotSaveList()
{
QString qStringFilename = QFileDialog::getSaveFileName(
this,
"Save Command List",
QDir::currentPath(),
"Command List (*.scomlst) ;; All files (*.*)");
QFile QFileSaved(qStringFilename);
if (!QFileSaved.open(QIODevice::WriteOnly | QIODevice::Text))
{
OutputDebugLog(__FUNCTION__,"ERROR","Unable to save list.");
return;
}
QTextStream out(&QFileSaved);
out << "SCOM_COMMAND_LIST\n";
out << "1.0\n";
out << QDate::currentDate().toString("dd.MM.yyyy") << " " << QTime::currentTime().toString("hh:mm:ss") << "\n";
QStringList qStringListRow;
for(int iTmpRow = 0; iTmpRow < qTableViewScomList->rowCount() - 1; iTmpRow++ )
{
qStringListRow.clear();
for (int iTmpColumn = 0; iTmpColumn < qTableViewScomList->columnCount(); iTmpColumn++)
{
if (qTableViewScomList->item(iTmpRow, iTmpColumn)->text().length() > 0)
{
qStringListRow.append("\"" + qTableViewScomList->item(iTmpRow, iTmpColumn)->text() + "\"");
}
else
{
qStringListRow.append("");
}
}
out << qStringListRow.join(";") + "\n";
}
QFileSaved.close();
OutputDebugLog(__FUNCTION__,"STATUS","List is saved.");
}
void CWidgetScomList::PrivateSlotLoadList()
{
QString qStringFilename = QFileDialog::getOpenFileName(
this,
"Load Command List",
QDir::currentPath(),
"Command List (*.scomlst) ;; All files (*.*)");
QFile QFileLoaded(qStringFilename);
if (!QFileLoaded.open(QIODevice::ReadOnly | QIODevice::Text))
{
OutputDebugLog(__FUNCTION__,"ERROR","Can't open list file.");
return;
}
QTextStream in(&QFileLoaded);
QString qString1stLine = in.readLine();
if(!qString1stLine.contains("SCOM_COMMAND_LIST"))
{
//Invalid Scom list file
OutputDebugLog(__FUNCTION__,"ERROR","Invalid List File.");
return;
}
qString1stLine = in.readLine();
int iRowCount = 0;
if(qString1stLine.contains("1.0"))
{
//Read extra line for date
in.readLine();
//Clear current table view
qTableViewScomList->setRowCount(0);
//Leave first row as blank
qTableViewScomList->setRowCount(1);
while (!in.atEnd())
{
// read one line from textstream(separated by "\n")
QString qStringRow = in.readLine();
//Insert one row to list
qTableViewScomList->insertRow(iRowCount);
// parse the read line into separate pieces(tokens) with "," as the delimiter
QStringList qStringListRow = qStringRow.split(";");
// load parsed data to model accordingly
for (int iTmp = 0; iTmp < qStringListRow.size(); iTmp++) {
QString qStringValue = qStringListRow.at(iTmp);
qStringValue.remove('\"');
qTableViewScomList->setItem(iRowCount, iTmp ,new QTableWidgetItem(qStringValue));
}
iRowCount++;
}
OutputDebugLog(__FUNCTION__,"STATUS","List is loaded.");
}
else
{
OutputDebugLog(__FUNCTION__,"ERROR","Invalid List File.");
}
return;
}
void CWidgetScomList::PublicSlotResetWidgets()
{
}
void CWidgetScomList::PublicSlotInsertScomQueueItemFromEditor(ScomEditData mScomEditData)
{
QTableWidgetItem *QTableWidgetItemNew = new QTableWidgetItem(0);
//insert row in tableview
if(qTableViewScomList->currentRow() < 0)
{
qTableViewScomList->setCurrentCell(0, 0);
}
//Insert row
qTableViewScomList->insertRow(qTableViewScomList->currentRow());
qTableViewScomList->setItem(qTableViewScomList->currentRow()-1, sListCommandListHeader.indexOf("CMDID") ,new QTableWidgetItem(mScomEditData.qStringCmdID));
qTableViewScomList->setItem(qTableViewScomList->currentRow()-1, sListCommandListHeader.indexOf("ADDR") ,new QTableWidgetItem(mScomEditData.qStringAddr));
qTableViewScomList->setItem(qTableViewScomList->currentRow()-1, sListCommandListHeader.indexOf("DATA") ,new QTableWidgetItem(mScomEditData.qStringData));
qTableViewScomList->setItem(qTableViewScomList->currentRow()-1, sListCommandListHeader.indexOf("FILE") ,new QTableWidgetItem(mScomEditData.qStringFile));
qTableViewScomList->setItem(qTableViewScomList->currentRow()-1, sListCommandListHeader.indexOf("EXPACK") ,new QTableWidgetItem(mScomEditData.qStringExpACK));
qTableViewScomList->setItem(qTableViewScomList->currentRow()-1, sListCommandListHeader.indexOf("EXPDATA") ,new QTableWidgetItem(mScomEditData.qStringExpData));
qTableViewScomList->setItem(qTableViewScomList->currentRow()-1, sListCommandListHeader.indexOf("DELAY") ,new QTableWidgetItem(mScomEditData.qStringDelayAfter));
qTableViewScomList->setItem(qTableViewScomList->currentRow()-1, sListCommandListHeader.indexOf("TIMES") ,new QTableWidgetItem(mScomEditData.qStringMaxRepeatTimes));
if(mScomEditData.bCritical)
{
qTableViewScomList->setItem(qTableViewScomList->currentRow()-1, sListCommandListHeader.indexOf("CRITICAL") ,new QTableWidgetItem("Y"));
}
else
{
qTableViewScomList->setItem(qTableViewScomList->currentRow()-1, sListCommandListHeader.indexOf("CRITICAL") ,new QTableWidgetItem("N"));
}
if(mScomEditData.bWaitUntil)
{
qTableViewScomList->setItem(qTableViewScomList->currentRow()-1, sListCommandListHeader.indexOf("WAIT") ,new QTableWidgetItem("Y"));
}
else
{
qTableViewScomList->setItem(qTableViewScomList->currentRow()-1, sListCommandListHeader.indexOf("WAIT") ,new QTableWidgetItem("N"));
}
}
//Public slot of copy scom queue item from list to editor
void CWidgetScomList::PublicSlotCopyScomQueueItemToEditor(void)
{
//Call copy
if(!PrivateSlotCopyScomQueueItem())
{
//No data in current row
//notify the list is finished.
OutputDebugLog(__FUNCTION__, "STATUS" , "SCOM List Run is over!");
emit SignalScomQueueItemEmpty(true);
}
else
{
//emit signal: Editor data ready
emit SignalScomQueueItemToEditorReady();
}
}
void CWidgetScomList::PublicSlotScomQueueItemFinish(e_Error eRet)
{
//Color the item
if(eRet == e_OK)
{
qTableViewScomList->item(qTableViewScomList->currentRow(),sListCommandListHeader.indexOf("CMDID"))->setBackgroundColor(Qt::green);
}
else
{
qTableViewScomList->item(qTableViewScomList->currentRow(),sListCommandListHeader.indexOf("CMDID"))->setBackgroundColor(Qt::red);
}
if(qTableViewScomList->item(qTableViewScomList->currentRow(),sListCommandListHeader.indexOf("CRITICAL"))->text().contains('Y'))
{
emit SignalScomQueueItemEmpty(true);
OutputDebugLog(__FUNCTION__, "STATUS" , "SCOM List Run is cancelled, fail in critical task!");
}
else
{
//call public slot: get next item
qTableViewScomList->setCurrentCell(qTableViewScomList->currentRow() + 1, 0);
if(!bCommandListRun)
{
OutputDebugLog(__FUNCTION__, "STATUS" , "SCOM List Run is cancelled!");
}
else
{
PublicSlotCopyScomQueueItemToEditor();
}
}
}
void CWidgetScomList::PublicSlotScomQueueRunCancel(bool bRunCancel)
{
bCommandListRun = bRunCancel;
if(true == bCommandListRun)
{
OutputDebugLog(__FUNCTION__, "STATUS" , "SCOM List Run is started!");
//Clear the colors
for(int i = 0; i < qTableViewScomList->rowCount()-1;i ++)
{
qTableViewScomList->item(i,sListCommandListHeader.indexOf("CMDID"))->setBackgroundColor(Qt::white);
}
PublicSlotCopyScomQueueItemToEditor();
}
else
{
OutputDebugLog(__FUNCTION__, "STATUS" , "SCOM List Run is cancelled!");
}
}