-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathvariable_model.cpp
More file actions
164 lines (143 loc) · 3.51 KB
/
Copy pathvariable_model.cpp
File metadata and controls
164 lines (143 loc) · 3.51 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
#include "common.hpp"
#include <sstream>
#include "common/variable.h"
#include "model/variable_model.h"
shelllet::model::VariableModel::VariableModel(const YAML::Node& node, QObject* parent)
: QAbstractTableModel(parent)
, node_(node)
{
#ifdef _DEBUG
std::ostringstream ss;
ss << node;
LOG_DEBUG("rpa") << "# variables: \n" << ss.str();
#endif // _DEBUG
}
shelllet::model::VariableModel::~VariableModel()
{
}
int shelllet::model::VariableModel::columnCount(const QModelIndex& parent) const
{
Q_UNUSED(parent);
return Columns::Count;
}
QVariant shelllet::model::VariableModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::DisplayRole || role == Qt::EditRole)
{
//Var var = node_[index.row()].as<Var>();
switch (index.column()) {
case Columns::Name:
case Columns::Type:
case Columns::Value:
case Columns::Desc:
{
try
{
return QString::fromStdString(node_[index.row()][Var::keys[static_cast<Columns>(index.column())]].as<std::string>());
}
catch (const std::exception& err)
{
LOG_WARNING("rpa") << "# variables error: " << err.what();
}
break;
}
case Columns::Ref:
try
{
return node_[index.row()][Var::keys[static_cast<Columns>(index.column())]].as<std::int32_t>();
}
catch (const std::exception&)
{
return 0;
}
break;
default:
break;
}
}
return QVariant();
}
Qt::ItemFlags shelllet::model::VariableModel::flags(const QModelIndex& index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
if (index.column() == Columns::Ref) {
return QAbstractTableModel::flags(index);
}
return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
}
QVariant shelllet::model::VariableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
switch (section) {
case Columns::Name:
return tr("Name");
case Columns::Type:
return tr("Type");
case Columns::Value:
return tr("Value");
case Columns::Ref:
return tr("Ref");
case Columns::Desc:
return tr("Desc");
default:
break;
}
}
return QVariant();
}
int shelllet::model::VariableModel::rowCount(const QModelIndex& parent) const
{
return node_.size();
}
YAML::Node shelllet::model::VariableModel::node() const
{
return node_;
}
bool shelllet::model::VariableModel::setData(const QModelIndex& index, const QVariant& value, int role /*= Qt::EditRole*/)
{
if (!index.isValid())
return false;
if (role == Qt::EditRole) {
switch (index.column()) {
case Columns::Name:
case Columns::Type:
case Columns::Value:
case Columns::Desc:
{
node_[index.row()][Var::keys[static_cast<Columns>(index.column())]] = value.toString().toStdString();
break;
}
default:
break;
}
#ifdef _DEBUG
std::ostringstream ss;
ss << node_;
LOG_DEBUG("rpa") << "# variables: \n" << ss.str();
#endif // _DEBUG
emit dataChanged(index, index, { Qt::DisplayRole });
return true;
}
return false;
}
bool shelllet::model::VariableModel::insertRows(int row, int count, const QModelIndex& parent /*= QModelIndex()*/)
{
std::string name = "name";
if (rowCount() != 0) {
name.append(std::to_string(rowCount()).c_str());
}
beginInsertRows(parent, row, row);
node_.push_back(Var{ name, "Auto", "", 0, "" });
endInsertRows();
return true;
}
bool shelllet::model::VariableModel::removeRows(int row, int count, const QModelIndex& parent /*= QModelIndex()*/)
{
beginRemoveRows(parent, row, row);
node_.remove(node_[row]);
endRemoveRows();
return true;
}