-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathledlamplistwidget.cpp
984 lines (696 loc) · 29.8 KB
/
ledlamplistwidget.cpp
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
#include "ledlamplistwidget.h"
#include "ui_ledlamplistwidget.h"
#include <QDebug>
#include "definedpollcodes.h"
#include "templates/settemporarypwr4groups.h"
#include "templates/addminutesdialog.h"
#include "insertmeterdialog.h"
#include "moji_defy.h"
//--------------------------------------------------------------
LedLampListWidget::LedLampListWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::LedLampListWidget)
{
ui->setupUi(this);
setProtocolVersion(MATILDA_PROTOCOL_VERSION_V6);
setCanWriteOperatorCommands(false);
initPage();
}
//--------------------------------------------------------------
LedLampListWidget::~LedLampListWidget()
{
delete ui;
}
void LedLampListWidget::setupObjectTv(QTableView *tv)
{
tv->setAlternatingRowColors(true);
tv->setSelectionBehavior(QAbstractItemView::SelectRows);
tv->setEditTriggers(QAbstractItemView::NoEditTriggers);
tv->setSortingEnabled(true);
tv->setCornerButtonEnabled(true);
tv->setCornerWidget(new QWidget());
tv->setContextMenuPolicy(Qt::CustomContextMenu);
}
//--------------------------------------------------------------
QString LedLampListWidget::map2jsonLine(const QVariantMap &map)
{
return QJsonDocument(QJsonObject::fromVariantMap(map)).toJson(QJsonDocument::Compact);
}
//--------------------------------------------------------------
QVariantMap LedLampListWidget::getPower2groups(const QMap<int, int> &groupId2power)
{
return getPower2groupsExt(groupId2power, true);
}
//--------------------------------------------------------------
QVariantMap LedLampListWidget::getPower2groupsExt(const QMap<int, int> &groupId2power, const bool &isPowerInPercents)
{
const QList<int> lk = groupId2power.keys();
QVariantMap map;
const int multiplier = isPowerInPercents ? 254 : 100;
for(int i = 0, imax = lk.size(); i < imax; i++){
const QString grp = QString::number(lk.at(i));
const QString pwr = QString::number( groupId2power.value(lk.at(i)) * multiplier / 100) ;//convert percents to 0-0xFE
QStringList l = map.value(pwr).toStringList();
l.append(grp);
map.insert(pwr, l);
}
return map;
}
//--------------------------------------------------------------
QStringList LedLampListWidget::getKeysLedLampV2()
{
//
return QString("mdl|NI|grpw|pstrtw|pnaw|tnaw|crdnts|pll|strt|memo|type|msecexhngd|mseclanswr"
"|pwr|wtt|pstrt|pna|tna|grp|ampers|refpowerwt|prog|uptime|useuref|usens|uref").split("|");
//|sync|jsnother - they are not used here
}
//--------------------------------------------------------------
int LedLampListWidget::getProtocolVersion()
{
return protocolVersion;
}
//--------------------------------------------------------------
int LedLampListWidget::getMatches4ThisCellText(const int &col, const QString &celltxt, const int &limit)
{
return getMatches4TheseCellsText(QList<int>() << col, QStringList() << celltxt, limit);
}
//--------------------------------------------------------------
int LedLampListWidget::getMatches4TheseCellsText(const QList<int> &cols, const QStringList &celltxtl, const int &limit)
{
if(cols.size() < 0)
return 0;
int counter = 0;
for(int i = 0, iMax = model->rowCount(), cmax = cols.size(); i < iMax && counter != limit; i++){
QStringList l;
for(int c = 0; c < cmax; c++)
l.append(model->item(i, cols.at(c))->text());
if(l == celltxtl)
counter++;
}
return counter;
}
//--------------------------------------------------------------
QStringList LedLampListWidget::getSelectedRowsText(const int &col)
{
QStringList list;
const QModelIndexList l = ui->tvTable->selectionModel()->selectedRows(col);
// QStandardItemModel m = table->model();
for(int i = 0, iMax = l.size(); i < iMax ; i++)
list.append(l.at(i).data(Qt::DisplayRole).toString());
return list;
}
//--------------------------------------------------------------
void LedLampListWidget::setModelHorizontalHeaderItems(const QStringList &header)
{
model->clear();
for(int i = 0, iMax = header.size(); i < iMax; i++)
model->setHorizontalHeaderItem(i, new QStandardItem(header.at(i)));
}
//--------------------------------------------------------------
bool LedLampListWidget::getSelectedNI4lampsSmart(QString &s)
{
const QStringList list = getSelectedRowsText( 1 );
if(list.isEmpty()){
emit showMessage(tr("There are no lamps("));
return false;
}
// if(gHelper->managerEnDisBttn.pbWriteDis){
// emit showMessage(tr("This action is not allowed("));
// return false;
// }
s = list.join(" ");
return true;
}
//--------------------------------------------------------------
QStringList LedLampListWidget::getSelectedNis(const int &maxsize)
{
const QStringList l = getSelectedRowsText(1);//ni column
const int ls = l.size();
if(ls > maxsize){
emit showMessage(tr("The maximum size is %1. The count of LCUs is %2.<br>%3 LCUs will be omitted").arg(maxsize).arg(ls).arg(ls-maxsize));
return l.mid(0, maxsize);
}
return l;
}
//--------------------------------------------------------------
QStringList LedLampListWidget::getUniqGroupIds()
{
const QStringList inl = getSelectedRowsText(2);
QStringList out;
for(int i = 0, imax = inl.size(); i < imax; i++){
const QString grpid = inl.at(i).split(">").last();
if(grpid.isEmpty())
continue;
if(out.contains(grpid))
continue;
out.append(grpid);
}
return out;
}
//--------------------------------------------------------------
QJsonObject LedLampListWidget::getPageSett(int &row, int &rowCount, const int &maxLen)
{
QJsonObject jobj;
if(row < 0)
row = 0;
rowCount = model->rowCount();
if(row >= rowCount)
return jobj;
// return QString("mdl NI grpw pstrtw pnaw tnaw crdnts pll strt memo type")
const QStringList k = QString("mdl NI grpw pstrtw pnaw tnaw crdnts pll strt memo type").split(' ', QString::SkipEmptyParts);//only these keys can be written
const QStringList ktable = getKeysLedLampV2();//all keys from table
QVariantList listVarM;
for(int j = 0, colMax = qMin(k.size(), model->columnCount()), cMax = ktable.size(), bytes = 50; row < rowCount; row++, j++){
QMap<QString,QString> maponerow;
for(int c = 0; c < cMax; c++)
maponerow.insert(ktable.at(c), model->item(row, c)->text());
if(maponerow.value("mdl") == "Auto")
maponerow.insert("mdl", "");//it is Auto, so wipe the value
QVariantList l;
QStringList bytesl;
for(int col = 0; col < colMax; col++){
l.append(maponerow.value(k.at(col)));
bytesl.append(maponerow.value(k.at(col)));
}
bytes += bytesl.join("___").size(); // "",
if(bytes > maxLen){
row--;
break;
}
listVarM.append(QVariant(l));
}
jobj.insert("m", QJsonArray::fromVariantList(listVarM));
row++;
jobj.insert("i", (row >= rowCount) ? -1 : row);
return jobj;
}
//--------------------------------------------------------------
QStringList LedLampListWidget::varList2strList(const QVariantList &list)
{
QStringList l;
for(int i = 0, iMax = list.size(); i < iMax; i++)
l.append(list.at(i).toString());
return l;
}
//--------------------------------------------------------------
void LedLampListWidget::initPage()
{
model = new QStandardItemModel(ui->tvTable);
proxy_model = new MySortFilterProxyModel(ui->tvTable);
proxy_model->setSourceModel(model);
proxy_model->setDynamicSortFilter(true);
connect(ui->leFilter, SIGNAL(textChanged(QString) ), proxy_model , SLOT(setNewFileterStr(QString)) );
ui->tvTable->setModel(proxy_model);
setupObjectTv(ui->tvTable);
editWdgt = new AddEditLampDlg(this);
connect(editWdgt, &AddEditLampDlg::addLamp, this, &LedLampListWidget::addLamp);
}
//--------------------------------------------------------------
void LedLampListWidget::clearPage()
{
setModelHorizontalHeaderItems(getKeysLedLampV2());
editWdgt->clearPage();
}
//--------------------------------------------------------------
void LedLampListWidget::onReadingFinished()
{
ui->tvTable->resizeColumnsToContents();
}
//--------------------------------------------------------------
void LedLampListWidget::setPageSett(const QJsonObject &jobj)
{
if(jobj.contains("c"))
lastColumnsSequence = jobj.value("c").toString().split("|");
const QVariantList lVar = jobj.value("m").toArray().toVariantList();
const QStringList k = getKeysLedLampV2();
const int iMax = lVar.size();
for(int i = 0, colMax = k.size(), devc = lastColumnsSequence.size(); i < iMax; i++){
const QVariantList oneitem = lVar.at(i).toList();
QMap<QString,QString> mapDev2gui;
for(int c = 0, ccmax = oneitem.size(); c < ccmax && c < devc; c++)
mapDev2gui.insert(lastColumnsSequence.at(c), oneitem.at(c).toString());
// QStringList lh = varList2strList(lVar.at(i).toList());
QList<QStandardItem*> m ;
for(int c = 0; c < colMax ; c++){
if(k.at(c).startsWith("msec")){
bool ok;
const qint64 msecsinceepoch = mapDev2gui.value(k.at(c)).toLongLong(&ok);
if(ok){
//convert to human readable
mapDev2gui.insert(k.at(c), QDateTime::fromMSecsSinceEpoch(msecsinceepoch).toString("yyyy-MM-dd hh:mm:ss.zzz"));
}
}else{
if(k.at(c) == "mdl")
mapDev2gui.insert(k.at(c), "Auto");
}
m.append(new QStandardItem(mapDev2gui.value(k.at(c))));// (col < lhMax) ? lh.at(col) : "---"));
}
model->appendRow(m);
}
if(jobj.value("i").toInt() < 1)
onReadingFinished();
// qDebug() << "LedLampListWidget " << model->rowCount() << rowF << lastIndx << iMax ;
}
//--------------------------------------------------------------
void LedLampListWidget::onActSelectedPollOnAddLedLampTable()
{
const int pllcol = getKeysLedLampV2().indexOf("pll");
foreach (QModelIndex i, ui->tvTable->selectionModel()->selectedRows()) {
int row = proxy_model->mapToSource(i).row();
model->item(row,pllcol)->setText("+");
}
}
//--------------------------------------------------------------
void LedLampListWidget::onActSelectedPollOffAddLedLampTable()
{
const int pllcol = getKeysLedLampV2().indexOf("pll");
foreach (QModelIndex i, ui->tvTable->selectionModel()->selectedRows()) {
int row = proxy_model->mapToSource(i).row();
model->item(row,pllcol)->setText("-");
}
}
//--------------------------------------------------------------
void LedLampListWidget::addLamp(QString lamp_ni, int grp_id, QString crdnts, QString memo, QString lampmodel, QVariant modelData, QString sbAddLampPstart, QString sbAddLampPna, QString sbAddLampTna, QString cbAddLampOnOffPoll, QString leAddLampStreet, bool quite)
{
int niRow = -1;
int rowCount = model->rowCount();
QString messageStrr;
if(lamp_ni.isEmpty() || lamp_ni.length() > 32)
messageStrr.append(tr("The lamp NI is invalid<br>"));
if(grp_id < 0 || grp_id > 127)
messageStrr.append(tr("The Group ID is invalid<br>"));
// if(!crdnts.isEmpty() && !GeoCoordinatesHelper::isCoordinatesValid(crdnts, crdnts))
// messageStrr.append(tr("Coordinate is invalid<br>"));
if(!messageStrr.isEmpty()){
emit showMessage(tr("Couldn't add the lamp.<br>%1").arg(messageStrr));
return;
}
for(int i = 0; i < rowCount; i++){
// TableHeaders::getColNamesLedLampV2();// lamp_ni,grp_id,crdnts,pll,memo
if(model->item(i, 1)->text() == lamp_ni){
niRow = i;
break;
}
}
QList<QStandardItem*> li;
// if(true){
// QStandardItem *item = new QStandardItem(lampmodel);
// item->setData(modelData);
// li.append(item);
// }
//
// return QString("mdl|NI|grpw|pstrtw|pnaw|tnaw|crdnts|pll|strt|memo|type|msecexhngd|mseclanswr"
// "|pwr|wtt|pstrt|pna|tna|grp|ampers|refpowerwt|prog|uptime|useuref|usens|uref").split("|");
QMap<QString,QString> mapDev2gui;
mapDev2gui.insert("mdl", "Auto");
mapDev2gui.insert("NI", lamp_ni);
mapDev2gui.insert("grpw", QString::number(grp_id));
mapDev2gui.insert("pstrtw", sbAddLampPstart);
mapDev2gui.insert("pnaw", sbAddLampPna);
mapDev2gui.insert("tnaw", sbAddLampTna);
mapDev2gui.insert("crdnts", crdnts);
mapDev2gui.insert("pll", cbAddLampOnOffPoll);
mapDev2gui.insert("strt", leAddLampStreet);
mapDev2gui.insert("memo", memo);
const QStringList k = getKeysLedLampV2();
for(int c = 0, colMax = k.size(); c < colMax ; c++){
li.append(new QStandardItem(mapDev2gui.value(k.at(c))));// (col < lhMax) ? lh.at(col) : "---"));
}
if(niRow >= 0){
// if(quite || true){
model->removeRow(niRow);
model->insertRow(niRow, li);
if(proxy_model->mapFromSource(model->index(niRow,0)).isValid())
ui->tvTable->selectRow(proxy_model->mapFromSource(model->index(niRow,0)).row());
// }
return;
}
model->appendRow(li);
if(proxy_model->mapFromSource(model->index(rowCount,0)).isValid())
ui->tvTable->selectRow(proxy_model->mapFromSource(model->index(rowCount,0)).row());
onReadingFinished();
}
//--------------------------------------------------------------
void LedLampListWidget::showDlgSetupTempPower4schedule()
{
SetTemporaryPwr4groups *d = new SetTemporaryPwr4groups("yyyy-MM-dd", "hh:mm:ss", getUniqGroupIds() ,this);
connect(d, &SetTemporaryPwr4groups::setTempSchedule4theseGroups, this, &LedLampListWidget::setTempSchedule4theseGroups);
d->exec();
d->deleteLater();
}
//--------------------------------------------------------------
void LedLampListWidget::resetSelectedTempPower4schedule()
{
sendCOMMAND_WRITE_ADD_DEL_TEMPSCHEDULE(QStringList(), QStringList(), getUniqGroupIds());
}
//--------------------------------------------------------------
void LedLampListWidget::resetAllTempPower4schedule()
{
QStringList l;
for(int i = 0; i < 256; i++)
l.append(QString::number(i));
sendCOMMAND_WRITE_ADD_DEL_TEMPSCHEDULE(QStringList(), QStringList(), l);
}
//--------------------------------------------------------------
void LedLampListWidget::setTempSchedule4theseGroups(QStringList groups, int power, int minutesfrom, int minutesto)
{
const QDateTime currdt = QDateTime::currentDateTime();
const QDateTime currdtzero = QDateTime(currdt.date(), QTime(0,0,0,0));
const QDateTime dtfrom = currdtzero.addSecs(minutesfrom*60);
const QDateTime dtto = (minutesfrom >= minutesto) ? currdtzero.addDays(1).addSecs(minutesto*60) : currdtzero.addSecs(minutesto*60);
if(!dtfrom.isValid() || !dtto.isValid() || groups.isEmpty()){
emit showMessage(tr("bad parameters"));
return;
}
const int minutesfromvalid = currdtzero.secsTo(dtfrom)/60;
const qint64 msecto = dtto.toMSecsSinceEpoch();
QStringList sdp, dtsl;
for(int i = 0, imax = groups.size(); i < imax; i++){
sdp.append(QString("%1\t%2-%3").arg(groups.at(i)).arg(minutesfromvalid).arg(power));
dtsl.append(QString("%1\t%2").arg(groups.at(i)).arg(msecto));
}
sendCOMMAND_WRITE_ADD_DEL_TEMPSCHEDULE(sdp, dtsl, QStringList());
}
//--------------------------------------------------------------
void LedLampListWidget::sendCOMMAND_WRITE_ADD_DEL_TEMPSCHEDULE(const QStringList &sdp, const QStringList &dtsl, const QStringList &idsr)
{
QJsonObject json;
json.insert("idsr", idsr.join(" "));// QJsonArray::fromStringList(idsr));
json.insert("sdp", QJsonArray::fromStringList(sdp));
json.insert("dtsl", QJsonArray::fromStringList(dtsl));
emit mWrite2RemoteDev(COMMAND_WRITE_ADD_DEL_TEMPSCHEDULE, json);
}
//--------------------------------------------------------------
void LedLampListWidget::setProtocolVersion(int version)
{
protocolVersion = version;
}
void LedLampListWidget::setCanWriteOperatorCommands(bool canwrite)
{
canWriteOperatorCommands = canwrite;
}
//--------------------------------------------------------------
void LedLampListWidget::setPower4selectedExt(int value, int tag, QVariant data)
{
Q_UNUSED(tag);
QJsonObject json = QJsonObject::fromVariantHash(data.toHash());
// QVariantMap map = hash.value("map").toMap();
const int power = (value < 5) ? 0 : value;
switch(json.value("cmcode").toInt()){
case POLL_CODE_FF_WRITE_POWER_TO_GROUPS:{
QStringList grps = json.value("cmns").toString().split(" ", QString::SkipEmptyParts);
QMap<int, int> groupId2power;
for(int i = 0, imax = grps.size(); i < imax; i++)
groupId2power.insert(grps.at(i).toInt(), power);
json.insert("cmns", QString("this_is_a_unique_ni_for_power2groups")); // !empty qstringlist
json.insert("cmoperation", map2jsonLine(getPower2groups(groupId2power)));
break;}
case POLL_CODE_FF_WRITE_POWER_TO_LAMP:{
QVariantMap pwrmap ;
pwrmap.insert("lastLampPower", power);
json.insert("cmoperation", map2jsonLine(pwrmap));
break;}
}
emit mWrite2RemoteDev(COMMAND_WRITE_FIREFLY_START_POLL, json);
}
//--------------------------------------------------------------
void LedLampListWidget::on_pbAddLamp_clicked()
{
editLampFromRow(-1);
}
//--------------------------------------------------------------
void LedLampListWidget::on_tvTable_doubleClicked(const QModelIndex &index)
{
editLampFromRow(proxy_model->mapToSource(index).row());
}
//--------------------------------------------------------------
void LedLampListWidget::on_tvTable_customContextMenuRequested(const QPoint &pos)
{
QTableView *tv = ui->tvTable;
if(!tv)
return;
// const bool enbl = (proxy_model->rowCount() > 0);
const int selRowCount = tv->selectionModel()->selectedRows().size();
const bool hasSelItems = (selRowCount > 0 );
const bool youHaveApower = canWriteOperatorCommands;
// gHelper->updateLastDataType(CLBRD_LEDLAMP_LISTV2);
QMenu *menu = new QMenu(tv);
QAction *actEd = new QAction(tr("Edit") , menu);
actEd->setIcon(QIcon(":/katynko/svg/document-edit.svg"));
actEd->setEnabled(hasSelItems);
connect(actEd, SIGNAL(triggered(bool)), this, SLOT(onActEditLedLampTable()) );
menu->addAction(actEd);
// menu->addAction(createSetFilterAct(menu, 1));
menu->addSeparator();
if(youHaveApower && hasSelItems && getProtocolVersion() >= MATILDA_PROTOCOL_VERSION_V4 ){
QAction *actCheckState = new QAction(tr("Selected: read the state") , menu);
actCheckState->setIcon(QIcon(":/katynko/svg/flag-green.svg"));
connect(actCheckState, SIGNAL(triggered(bool)), this, SLOT(checkLampState4selected())) ;
// actCheckState->setEnabled(!gHelper->managerEnDisBttn.pbWriteDis);
menu->addAction(actCheckState);
QAction *actBlym = new QAction(tr("Selected: activate the beacon mode"), menu);
actBlym->setIcon(QIcon(":/katynko/svg4/lc_extrusionlightingfloater.svg"));
connect(actBlym, SIGNAL(triggered(bool)), this, SLOT(blymBlym4selected()) );
// actBlym->setEnabled(!gHelper->managerEnDisBttn.pbWriteDis);
menu->addAction(actBlym);
if(getProtocolVersion() >= MATILDA_PROTOCOL_VERSION_V6 ){
menu->addMenu(getMenuSelected(menu, youHaveApower, hasSelItems));
menu->addMenu(getMenuTempSchedule(menu, youHaveApower, hasSelItems));
}
menu->addSeparator();
}
QAction *actRs = new QAction(tr("Reset sorting") , menu);
actRs->setIcon(QIcon(":/katynko/svg/view-refresh.svg"));
connect(actRs, SIGNAL(triggered(bool)), this, SLOT(onResetSortingAct()) );
menu->addAction(actRs);
menu->addSeparator();
QAction *actSpon = new QAction(tr("Selected: Poll On") , menu);
actSpon->setIcon(QIcon(":/katynko/svg/draw-cross.svg"));
actSpon->setEnabled(hasSelItems);
connect(actSpon, SIGNAL(triggered(bool)), this, SLOT(onActSelectedPollOnAddLedLampTable()) );
menu->addAction(actSpon);
QAction *actSpoff = new QAction(tr("Selected: Poll Off") , menu);
actSpoff->setIcon(QIcon(":/katynko/svg/dialog-cancel.svg"));
actSpoff->setEnabled(hasSelItems);
connect(actSpoff, SIGNAL(triggered(bool)), this, SLOT(onActSelectedPollOffAddLedLampTable()) );
menu->addAction(actSpoff);
menu->addSeparator();
QAction *actDs = new QAction(tr("Delete selected") , menu);
actDs->setIcon(QIcon(":/katynko/svg3/user-trash-full.svg"));
actDs->setEnabled(hasSelItems);
connect(actDs, SIGNAL(triggered(bool)), this, SLOT(onDeleteSelectedAct()) );
// connect(actDs, SIGNAL(triggered(bool)), this, SLOT(sayModelChangedLater()) );
menu->addAction(actDs);
menu->exec(tv->mapToGlobal(pos));
menu->deleteLater();
}
//--------------------------------------------------------------
void LedLampListWidget::onActEditLedLampTable()
{
on_tvTable_doubleClicked(ui->tvTable->currentIndex());
}
//--------------------------------------------------------------
void LedLampListWidget::editLampFromRow(int row)
{
/*void setPageSett(const QString &cbxAddLampModel,
* const QString &leAddLampNI,
* const QString &cbAddLampPhysicalGroup,
* const QString &sbAddLampPstart,
* const QString &sbAddLampPna,
* const QString &sbAddLampTna,
* const QString &leAddLampCoordinates,
* const QString &cbAddLampOnOffPoll,
* const QString &leAddLampStreet,
const QString &leAddLampMemo);
*/
if(row >= 0){
editWdgt->setPageSett(model->item(row, 0)->data().toString(), //mdl Auto
model->item(row, 1)->text(), //NI
model->item(row, 2)->text(), //grpw
model->item(row, 3)->text(), //pstw
model->item(row, 4)->text(),//pnaw
model->item(row, 5)->text(), //tna
model->item(row, 6)->text(),//crdn
model->item(row, 7)->text(), //pll
model->item(row, 8)->text(), //strt
model->item(row, 9)->text()); //memo
}else{
editWdgt->addNewDevice();
}
editWdgt->showLater();
}
//--------------------------------------------------------------
void LedLampListWidget::checkLampState4selected()
{
if(getProtocolVersion() < MATILDA_PROTOCOL_VERSION_V6){
//do not do anything
return;
}
//a normal format
const QStringList nis = getSelectedRowsText(1);
if(nis.length() > 0){
QAction *a = qobject_cast<QAction *>(QObject::sender());
if(a)
tryToSendAdditionalCommand(QVariantList() << POLL_CODE_FF_WRITE_LAMP_CONFIG_SMART << nis, a->text());
}
}
//--------------------------------------------------------------
void LedLampListWidget::blymBlym4selected()
{
QString s;
if(!getSelectedNI4lampsSmart(s))
return;
QJsonObject json;
json.insert("cmns", s); //NI space separeted
emit mWrite2RemoteDev(COMMAND_WRITE_ACTVT_BEACON_MODE, json);
}
//--------------------------------------------------------------
void LedLampListWidget::onActWriteSeleted()
{
const QStringList nis = getSelectedNis(50);
if(nis.isEmpty())
return;
int rowCount = 0;
int row = 0;
const QVariantList meters = getPageSett(row, rowCount, MAX_PACKET_LEN).toVariantMap().value("m").toList();
QVariantHash ni2sett;
for(int i = 0, imax = meters.size(); i < imax; i++){
const QVariantList onelcu = meters.at(i).toList();
if(nis.contains(onelcu.at(1).toString()))
ni2sett.insert(onelcu.at(1).toString(), meters.at(i));
}
QVariantList l;
for(int i = 0, imax = nis.size(), j = 0; i < imax && j < 50; i++, j++){
if(ni2sett.contains(nis.at(i))){
l.append(ni2sett.value(nis.at(i)));
j++;
}
}
InsertMeterDialog *dialog = new InsertMeterDialog(COMMAND_WRITE_FIREFLY_INSERT_LAMPS, l, this);
connect(dialog, SIGNAL(data2matilda(quint16,QJsonObject)), this, SIGNAL(mWrite2RemoteDev(quint16,QJsonObject))) ;
dialog->exec();
dialog->deleteLater();
}
//--------------------------------------------------------------
void LedLampListWidget::onActWriteSeletedOn()
{
QStringList nis = getSelectedNis(MAX_METER_COUNT);
if(nis.isEmpty())
return;
// nis.prepend("2");//2 mode by ni ? 1 mode by SN
QJsonObject json;
json.insert("cmns", nis.join(" "));
emit mWrite2RemoteDev(COMMAND_WRITE_FIREFLY_LIST_POLL_ON, json);
}
//--------------------------------------------------------------
void LedLampListWidget::onActWriteSeletedOff()
{
QStringList nis = getSelectedNis(MAX_METER_COUNT);
if(nis.isEmpty())
return;
// nis.prepend("2");//2 mode by ni ? 1 mode by SN
QJsonObject json;
json.insert("cmns", nis.join(" "));
emit mWrite2RemoteDev(COMMAND_WRITE_FIREFLY_LIST_POLL_OFF, json);
}
//--------------------------------------------------------------
void LedLampListWidget::onActWriteSeletedDeletePart()
{
QStringList l = getSelectedRowsText(1);
int maxSize = MAX_METER_COUNT;
if(l.size() > maxSize){
emit showMessage(tr("Max size is %1. LCUs count is %2").arg(maxSize).arg(l.size()));
}else{
if(l.isEmpty())
return;
l.removeDuplicates();
// l.prepend("2");//2 mode by ni ? 1 mode by SN
QJsonObject json;
json.insert("cmns", l.join(" ")); //single line
emit mWrite2RemoteDev(COMMAND_WRITE_FIREFLY_REMOVE_NIS, json);
}
}
//--------------------------------------------------------------
void LedLampListWidget::tryToSendAdditionalCommand(const QVariantList &varl, const QString &text)
{
if(varl.isEmpty())
return;//error
QJsonObject json;// hash;
json.insert("cmcode", varl.at(0).toInt());
if(varl.size() > 1){
json.insert("cmns", varl.at(1).toStringList().join(" "));
// QVariantMap map;
// map.insert("-ns", varl.at(1).toStringList());
// hash.insert("map", map);
switch(varl.at(0).toInt()){
case POLL_CODE_FF_WRITE_POWER_TO_GROUPS :
case POLL_CODE_FF_WRITE_POWER_TO_LAMP :{
AddMinutesDialog *d = new AddMinutesDialog( 4, 100, tr("Power [%]"), tr("Off"), this);
connect(d, &AddMinutesDialog::setInteger4selectedExt, this, &LedLampListWidget::setPower4selectedExt);
json.insert("cmname", text);
d->setThisData(json.toVariantHash());
d->exec();
return;}
}
emit mWrite2RemoteDev(COMMAND_WRITE_FIREFLY_START_POLL, json);
}
}
//--------------------------------------------------------------
void LedLampListWidget::onDeleteSelectedAct()
{
QTableView *tv = ui->tvTable;
QList<int> l;
const QModelIndexList li = tv->selectionModel()->selectedRows();
for(int i = 0, iMax = li.size(); i < iMax; i++)
l.append(proxy_model->mapToSource(li.at(i)).row());
std::sort(l.begin(), l.end());
while(!l.isEmpty())
model->removeRow(l.takeLast());
}
//--------------------------------------------------------------
void LedLampListWidget::onResetSortingAct()
{
proxy_model->sort(-1);
}
//--------------------------------------------------------------
QMenu *LedLampListWidget::getMenuSelected(QWidget *prnt, const bool &youHaveApower, const bool &hasSelItems)
{
QMenu *menu = new QMenu(tr("Remote device (for selected only)"), prnt);
// menu->setIcon(QIcon(":/katynko/svg3/relay-load-command.svg"));
QAction *actInsrt = new QAction(tr("Insert to the remote device") , menu);
actInsrt->setEnabled(hasSelItems && youHaveApower);
connect(actInsrt, SIGNAL(triggered(bool)), SLOT(onActWriteSeleted()) );
menu->addAction(actInsrt);
QAction *actInsrtOn = new QAction(tr("Insert to the remote device with Poll On") , menu);
actInsrtOn->setEnabled(hasSelItems && youHaveApower);
connect(actInsrtOn, SIGNAL(triggered(bool)), SLOT(onActWriteSeletedOn()) );
menu->addAction(actInsrtOn);
QAction *actInsrtOff = new QAction(tr("Insert to the remote list device Poll Off") , menu);
actInsrtOff->setEnabled(hasSelItems && youHaveApower);
connect(actInsrtOff, SIGNAL(triggered(bool)), SLOT(onActWriteSeletedOff()) );
menu->addAction(actInsrtOff);
QAction *actInsrtDel = new QAction(tr("Delete from the remote list") , menu);
actInsrtDel->setEnabled(hasSelItems && youHaveApower);
connect(actInsrtDel, SIGNAL(triggered(bool)), SLOT(onActWriteSeletedDeletePart()) );
menu->addAction(actInsrtDel);
return menu;
}
//--------------------------------------------------------------
QMenu *LedLampListWidget::getMenuTempSchedule(QWidget *prnt, const bool &youHaveApower, const bool &hasSelItems)
{
QMenu *menu = new QMenu(tr("Temporary schedule"), prnt);
QAction *actTempSchedule = new QAction(tr("Set temporary schedule for selected"), menu);
connect(actTempSchedule, SIGNAL(triggered(bool)), this, SLOT(showDlgSetupTempPower4schedule()));
actTempSchedule->setEnabled(youHaveApower && hasSelItems);
menu->addAction(actTempSchedule);
QAction *actTempSchedule2 = new QAction(tr("Reset temporary schedule for selected"), menu);
connect(actTempSchedule2, SIGNAL(triggered(bool)), this, SLOT(resetSelectedTempPower4schedule()));
actTempSchedule2->setEnabled(youHaveApower);
menu->addAction(actTempSchedule2);
QAction *actTempSchedule3 = new QAction(tr("Reset temporary schedule for all"), menu);
connect(actTempSchedule3, SIGNAL(triggered(bool)), this, SLOT(resetAllTempPower4schedule()));
actTempSchedule3->setEnabled(youHaveApower);
menu->addAction(actTempSchedule3);
return menu;
}
//--------------------------------------------------------------