-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtablemodel.cpp
More file actions
152 lines (126 loc) · 3.6 KB
/
Copy pathtablemodel.cpp
File metadata and controls
152 lines (126 loc) · 3.6 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
#include "tablemodel.h"
#include <QJsonDocument>
#include <QFile>
#include <QJsonObject>
#include <QDir>
TableModel::TableModel(QObject *parent)
: QAbstractTableModel(parent)
{
}
void TableModel::save()
{
QFile file(ModelFileName());
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)){
emit reported(file.errorString());
return;
}
file.write(QJsonDocument(array_).toJson());
}
void TableModel::load()
{
QFile file(ModelFileName());
if (!file.exists())
return;
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
emit reported(file.errorString());
return;
}
QJsonParseError error;
auto document= QJsonDocument::fromJson(file.readAll(), &error);
if (error.error != QJsonParseError::NoError){
emit reported(error.errorString());
return;
}
if (!document.isArray()){
emit reported("Model root is not array");
return;
}
array_ = document.array();
}
void TableModel::add(const QString &name, const QString &cmd, const QString& env)
{
QJsonObject object = {
{"name", name},
{"cmd", cmd},
{"env", env}
};
int row = array_.size();
this->beginInsertRows(QModelIndex(), row, row);
array_.push_back(object);
this->setData(createIndex(row, Column_Name), name, Qt::DisplayRole);
this->setData(createIndex(row, Column_Cmd), cmd, Qt::DisplayRole);
this->setData(createIndex(row, Column_Env), env, Qt::DisplayRole);
this->endInsertRows();
this->save();
}
void TableModel::editCurrent(int row, const QString &name, const QString &cmd, const QString& env)
{
array_[row] = QJsonObject {
{"name", name},
{"cmd", cmd},
{"env", env}
};
auto index1 = createIndex(row, Column_Name);
auto index2 = createIndex(row, Column_Cmd);
auto index3 = createIndex(row, Column_Env);
this->setData(index1, name, Qt::DisplayRole);
this->setData(index2, cmd, Qt::DisplayRole);
this->setData(index3, env, Qt::DisplayRole);
this->submit();
this->save();
}
void TableModel::delCurrent(int row)
{
this->beginRemoveRows(QModelIndex(), row, row);
array_.removeAt(row);
this->removeRow(row);
this->endRemoveRows();
this->save();
}
QString TableModel::ModelFileName()
{
return QDir::cleanPath(QDir::homePath() + QDir::separator() + "shelllet.com" + QDir::separator() +"model.json");
}
QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
Q_UNUSED(orientation)
if (section == 0 && role == Qt::DisplayRole){
return "Name";
}
if (section == 1 && role == Qt::DisplayRole){
return "Cmd";
}
if (section == 2 && role == Qt::DisplayRole){
return "Env";
}
return QVariant();
}
int TableModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return array_.size();
}
int TableModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return 3;
}
QVariant TableModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || role != Qt::DisplayRole)
return QVariant();
int row = index.row();
if (row >= array_.size()|| row < 0){
return QVariant();
}
if (index.column() == Column_Name) {
return array_[row].toObject().value("name").toString();
} else if (index.column() == Column_Cmd) {
return array_[row].toObject().value("cmd").toString();
} else if (index.column() == Column_Env) {
return array_[row].toObject().value("env").toString();
}
return QVariant();
}