-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpropertyobserver.cpp
More file actions
74 lines (61 loc) · 2.23 KB
/
propertyobserver.cpp
File metadata and controls
74 lines (61 loc) · 2.23 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
#include <exception>
#include "propertyobserver.h"
#include "command/command.h"
PropertyObserver::PropertyObserver(QTableWidget *propertyWidget,
QObject *parent)
:QObject(parent), _propertyWidget(propertyWidget) {
if (!propertyWidget) {
throw std::invalid_argument("nullptr QTableWidget");
}
connect(_propertyWidget, SIGNAL(cellChanged(int,int)),
this, SLOT(writeProperties()));
}
void PropertyObserver::loadProperties(Properties *item) {
_propertyItem = item;
if (item) {
fillList();
} else {
_propertyWidget->clearContents();
}
}
void PropertyObserver::writeProperties() {
QMap<QString, double> lst;
for (int i = 0; i < _propertyWidget->rowCount(); ++i) {
lst.insert(_propertyWidget->item(i, 0)->text(),
_propertyWidget->item(i, 1)->text().toDouble());
}
if (_propertyItem) {
_propertyItem->setProperties(lst);
// TODO: а вот это костыльный костыль
VarList vars;
for (const auto &key : lst.keys()) {
vars.emplace_back(key.toStdString(), lst[key]);
}
emit invoke(Command::ChangeValues(_propertyItem->getId(),
std::move(vars)));
// TODO снова костыль
emit changed();
} else {
while (_propertyWidget->rowCount()){
_propertyWidget->removeRow(0);
}
}
}
void PropertyObserver::fillList() {
// чтобы не пытаться записать в пропертис только что выгруженные данные
_propertyWidget->blockSignals(true);
while (_propertyWidget->rowCount()) {
_propertyWidget->removeRow(0);
}
int counter = 0;
auto lst = _propertyItem->getProperties();
for (auto iter = lst.begin(); iter != lst.end(); ++iter) {
_propertyWidget->insertRow(counter);
_propertyWidget->setItem(counter, 0,
new QTableWidgetItem(iter.key()));
_propertyWidget->setItem(counter, 1,
new QTableWidgetItem(QString::number(iter.value())));
++counter;
}
_propertyWidget->blockSignals(false);
}