-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmdlistmodel.cpp
248 lines (221 loc) · 6.35 KB
/
cmdlistmodel.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
#include "cmdlistmodel.h"
#include <QDir>
#include <QMimeData>
#include <QRegularExpression>
#include <QSettings>
#include <QSqlError>
#include <QSqlQuery>
CmdListModel::CmdListModel(QObject *parent)
: QAbstractListModel { parent }
{
reloadDB();
}
int CmdListModel::rowCount(const QModelIndex &parent) const
{
return cmdList.count();
}
QVariant CmdListModel::data(const QModelIndex &index, int role) const
{
const Cmd &cmd = cmdList[index.row()];
if (role == Qt::DisplayRole) {
return cmd.name;
}
if (role == StateRole) {
return procMap.contains(cmd.id);
}
if (role == DataRole) {
return QVariant::fromValue(cmd);
}
if (role == FileRole) {
static QRegularExpression sReg("[^\\d\\w]");
return QDir::temp().filePath(QString(cmd.name).replace(sReg, "_"));
}
return QVariant();
}
bool CmdListModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
qDebug() << "setData " << index << " " << value << " " << role;
if (role == StateRole) {
toggleProc(index);
return true;
}
if (role == DataRole) {
const Cmd &cmd = value.value<Cmd>();
cmdList.replace(index.row(), cmd);
updateDB(cmd);
emit dataChanged(this->index(index.row()), this->index(index.row()), { Qt::DisplayRole });
return true;
}
return false;
}
Qt::ItemFlags CmdListModel::flags(const QModelIndex &index) const
{
return QAbstractListModel::flags(index) | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
}
bool CmdListModel::insertRows(int row, int count, const QModelIndex &parent)
{
qDebug() << "insertRows " << row << " " << count;
beginInsertRows(QModelIndex(), row, row + count - 1);
for (int r = 0; r < count; ++r) {
Cmd cmd;
insertDB(cmd);
cmdList.insert(row, cmd);
}
assignPositions();
endInsertRows();
return true;
}
bool CmdListModel::removeRows(int row, int count, const QModelIndex &parent)
{
qDebug() << "removeRows " << row << " " << count;
beginRemoveRows(QModelIndex(), row, row + count - 1);
for (int r = row; r < row + count; ++r) {
Cmd cmd = cmdList.takeAt(r);
qDebug() << "removeRows " << cmd << cmdList.size();
deleteDB(cmd);
if (procMap.contains(cmd.id)) {
procMap.value(cmd.id)->kill();
}
}
endRemoveRows();
return true;
}
Qt::DropActions CmdListModel::supportedDropActions() const
{
return Qt::CopyAction;
}
QStringList CmdListModel::mimeTypes() const
{
QStringList types;
types << QStringLiteral("application/x-gloggcat-cmd-row");
return types;
}
QMimeData *CmdListModel::mimeData(const QModelIndexList &indexes) const
{
QMimeData *data = new QMimeData();
QByteArray encoded;
QDataStream stream(&encoded, QDataStream::WriteOnly);
const Cmd &cmd = indexes.at(0).data(DataRole).value<Cmd>();
stream << indexes.at(0).row();
data->setData(mimeTypes().at(0), encoded);
return data;
}
bool CmdListModel::dropMimeData(
const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
{
if (row < 0) {
return false;
}
QByteArray encoded = data->data(mimeTypes().at(0));
QDataStream stream(&encoded, QDataStream::ReadOnly);
int sourceRow;
stream >> sourceRow;
int targetRow = row > sourceRow ? row - 1 : row;
const Cmd &source = cmdList.takeAt(sourceRow);
cmdList.insert(targetRow, source);
assignPositions();
return true;
}
bool CmdListModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row,
int column, const QModelIndex &parent) const
{
return data->hasFormat(mimeTypes().at(0));
}
void CmdListModel::assignPositions()
{
for (int i = 0; i < cmdList.size(); i++) {
Cmd &cmd = cmdList[i];
cmd.pos = i;
updateDB(cmd);
}
}
void CmdListModel::reloadDB()
{
cmdList.clear();
QSqlQuery query("SELECT * FROM cmd ORDER BY pos ASC");
while (query.next()) {
Cmd cmd;
cmd.id = query.value(0).toUInt();
cmd.name = query.value(1).toString();
cmd.exec = query.value(2).toString();
cmd.pos = query.value(3).toUInt();
cmdList.append(cmd);
}
}
void CmdListModel::updateDB(const Cmd &cmd)
{
QSqlQuery query;
query.prepare("UPDATE cmd SET name=:name, exec=:exec, pos=:pos WHERE id=:id");
query.bindValue(":name", cmd.name);
query.bindValue(":exec", cmd.exec);
query.bindValue(":pos", cmd.pos);
query.bindValue(":id", cmd.id);
query.exec();
}
void CmdListModel::insertDB(Cmd &cmd)
{
QSqlQuery query;
query.prepare("INSERT INTO cmd DEFAULT VALUES");
query.exec();
cmd.id = query.lastInsertId().toUInt();
}
void CmdListModel::deleteDB(const Cmd &cmd)
{
QSqlQuery query;
query.prepare("DELETE FROM cmd WHERE id=:id");
query.bindValue(":id", cmd.id);
query.exec();
}
void CmdListModel::toggleProc(const QModelIndex &index)
{
const Cmd &cmd = index.data(DataRole).value<Cmd>();
if (cmd.exec.isEmpty()) {
return;
}
QProcess *proc = procMap.value(cmd.id, nullptr);
if (proc == nullptr) {
proc = new QProcess(this);
procMap[cmd.id] = proc;
connect(proc, &QProcess::stateChanged, this, &CmdListModel::procStateChanged);
proc->setStandardOutputFile(index.data(FileRole).toString());
proc->startCommand(cmd.exec);
} else {
proc->kill();
}
}
void CmdListModel::procStateChanged(QProcess::ProcessState state)
{
QProcess *proc = qobject_cast<QProcess *>(sender());
int id = procMap.key(proc);
if (state == QProcess::NotRunning) {
procMap.remove(id);
proc->deleteLater();
}
int row = -1;
for (int i = 0; i < cmdList.count(); ++i) {
if (cmdList[i].id == id) {
row = i;
break;
}
}
if (row == -1) {
return;
}
if (state == QProcess::Running) {
if (QSettings().value("auto_open", true).toBool()) {
const QString &file = data(index(row), FileRole).toString();
QProcess process;
process.startDetached("glogg", { file });
}
}
emit dataChanged(index(row), index(row));
}
QDebug operator<<(QDebug dbg, const Cmd &c)
{
QDebugStateSaver saver(dbg);
dbg.nospace() << "Cmd(" << c.id << ", " << c.name << ", " << c.pos << ")";
return dbg;
}
Cmd::~Cmd()
{
}