-
Notifications
You must be signed in to change notification settings - Fork 0
/
grouptable.cpp
117 lines (94 loc) · 4.22 KB
/
grouptable.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
#include "grouptable.h"
#include "mainwindow.h"
GroupTable::GroupTable(QWidget *parent) : QTableWidget(parent)
{
QStringList labels;
labels << "Name";
labels << "Status";
labels << "Farbe";
labels << "Helligkeit";
labels << "ColorLoop";
labels << "ID";
this->setSelectionMode(QTableWidget::NoSelection);
this->setColumnCount(6);
this->setHorizontalHeaderLabels(labels);
this->verticalHeader()->hide();
this->setSortingEnabled(true);
this->hideColumn(5);
connect(this, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(doubleClick(QModelIndex)));
}
GroupTable::~GroupTable()
{
}
void GroupTable::init(HueWrapper *hue)
{
this->hue = hue;
connect(hue, SIGNAL(dataGroupsUpdated(QByteArray)), this, SLOT(update(QByteArray)));
}
void GroupTable::update(QByteArray json)
{
this->sortItems(-1);
this->clearContents();
this->setRowCount(0);
this->hideColumn(5);
QJsonDocument doc = QJsonDocument::fromJson(json);
QHash<QString, QVariant> hash = doc.object().toVariantHash();
QStringList key_list = hash.uniqueKeys();
for (int i = 0; i < key_list.length(); i++)
{
this->setRowCount(this->rowCount()+1);
this->setItem(this->rowCount()-1,0, new QTableWidgetItem(
QString("%1: %2").arg(key_list.at(i),2, '0').arg(doc.object().value(key_list.at(i)).toObject().value("name").toString())));
this->setItem(this->rowCount()-1,1, new QTableWidgetItem(
(doc.object().value(key_list.at(i)).toObject().value("state").toObject().value("all_on").toBool()?"ein":"aus")));
int bri = doc.object().value(key_list.at(i)).toObject().value("action").toObject().value("bri").toInt();
int hue = doc.object().value(key_list.at(i)).toObject().value("action").toObject().value("hue").toInt();
int sat = doc.object().value(key_list.at(i)).toObject().value("action").toObject().value("sat").toInt();
QColor col = QColor::fromHsv((int)(hue/65535.0 * 360),sat, bri);
QTableWidgetItem *item = new QTableWidgetItem();
item->setBackgroundColor(col);
this->setItem(this->rowCount()-1,2, item);
this->setItem(this->rowCount()-1,3, new QTableWidgetItem(
QString::number(qCeil((doc.object().value(key_list.at(i)).toObject().value("action").toObject().value("bri").toInt())/2.54)) + "\%"));
this->setItem(this->rowCount()-1,4, new QTableWidgetItem(
(doc.object().value(key_list.at(i)).toObject().value("action").toObject().value("effect").toString()=="colorloop"?"ein":"aus")));
this->setItem(this->rowCount()-1,5, new QTableWidgetItem(QString("%1").arg(key_list.at(i),2, '0')));
}
this->resizeColumnsToContents();
this->sortItems(0, Qt::AscendingOrder);
for (int row = 0; row < this->rowCount(); row++)
{
for (int col = 0; col < this->columnCount(); col++)
{
this->item(row,col)->setFlags(this->item(row,col)->flags() & ~Qt::ItemIsEditable);
}
}
}
void GroupTable::doubleClick(const QModelIndex index)
{
if (index.column() == 1)
{
hue->setGroupOn(this->item(this->currentRow(),5)->text().toInt(),
(this->item(this->currentRow(),1)->text() == "ein"?false:true) );
}
else if (index.column() == 2)
{
QColorDialog dia(this->currentItem()->backgroundColor(),this);
dia.exec();
if (dia.selectedColor().isValid())
hue->setGroupColor(this->item(this->currentRow(),5)->text().toInt(),dia.selectedColor());
}
else if (index.column() == 3)
{
bool ok;
int bri = QInputDialog::getInt(this, "Helligkeit", "Geben Sie die Helligkeit an", this->item(this->currentRow(),3)->text().replace("\%","").toInt(),0, 100, 10, &ok);
if (ok)
hue->setGroupBri(this->item(this->currentRow(),5)->text().toInt(), bri*2.54 );
}
else if (index.column() == 4)
{
hue->setGroupColorLoop(this->item(this->currentRow(),5)->text().toInt(),
(this->item(this->currentRow(),4)->text() == "ein"?false:true) );
}
QTimer::singleShot(100, this->hue, SLOT(doUpdateGroups()));
}