forked from albertlauncher/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodulesmodel.cpp
More file actions
179 lines (143 loc) · 6.54 KB
/
Copy pathmodulesmodel.cpp
File metadata and controls
179 lines (143 loc) · 6.54 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (c) 2017-2018 Manuel Schneider
#include "pythonmodulev1.h"
#include <QIcon>
#include <QStandardPaths>
#include <QFile>
#include <QDir>
#include "modulesmodel.h"
#include "extension.h"
namespace {
enum class Section{Name, Trigger, Version, Author, Count};
}
/** ***************************************************************************/
Python::ModulesModel::ModulesModel(Extension *extension, QObject *parent)
: QAbstractTableModel(parent), extension(extension) {
connect(extension, &Extension::modulesChanged,
this, &ModulesModel::endResetModel);
}
/** ***************************************************************************/
int Python::ModulesModel::rowCount(const QModelIndex &) const {
return static_cast<int>(extension->modules().size());
}
/** ***************************************************************************/
int Python::ModulesModel::columnCount(const QModelIndex &) const {
return static_cast<int>(Section::Count);
}
/** ***************************************************************************/
QVariant Python::ModulesModel::headerData(int section, Qt::Orientation orientation, int role) const {
// No sanity check necessary since
if ( section<0 || static_cast<int>(Section::Count)<=section )
return QVariant();
if (orientation == Qt::Horizontal){
switch (static_cast<Section>(section)) {
case Section::Name:{
switch (role) {
case Qt::DisplayRole: return "Name";
case Qt::ToolTipRole: return "The name of the module.";
default: return QVariant();
}
}
case Section::Trigger:{
switch (role) {
case Qt::DisplayRole: return "Trigger";
case Qt::ToolTipRole: return "The term that triggers this extension.";
default: return QVariant();
}
}
case Section::Version:{
switch (role) {
case Qt::DisplayRole: return "Version";
case Qt::ToolTipRole: return "The version of this extension.";
default: return QVariant();
}
}
case Section::Author:{
switch (role) {
case Qt::DisplayRole: return "Author";
case Qt::ToolTipRole: return "The author of this extension.";
default: return QVariant();
}
}
default: return QVariant();
}
}
return QVariant();
}
/** ***************************************************************************/
QVariant Python::ModulesModel::data(const QModelIndex &index, int role) const {
if (!index.isValid()
|| index.row() >= static_cast<int>(extension->modules().size())
|| index.column() >= static_cast<int>(static_cast<int>(Section::Count)))
return QVariant();
switch (role) {
case Qt::DecorationRole: {
if ( static_cast<Section>(index.column()) == Section::Name ) {
switch (extension->modules()[static_cast<size_t>(index.row())]->state()) {
case PythonModuleV1::State::Unloaded:
return QIcon(":plugin_notloaded");
case PythonModuleV1::State::Loaded:
return QIcon(":plugin_loaded");
case PythonModuleV1::State::InvalidMetadata:
case PythonModuleV1::State::Error:
return QIcon(":plugin_error");
}
}
return QVariant();
}
case Qt::DisplayRole: {
const PythonModuleV1 *module = extension->modules()[static_cast<size_t>(index.row())].get();
switch (static_cast<Section>(index.column())) {
case Section::Name: return module->name();
case Section::Trigger: return module->trigger();
case Section::Version: return module->version();
case Section::Author: return module->author();
default: return QVariant();
}
}
case Qt::ToolTipRole: {
QString toolTip;
const PythonModuleV1 *module = extension->modules()[static_cast<size_t>(index.row())].get();
toolTip = QString("<p><b>Name:</b> %1</p>").arg(module->name().toHtmlEscaped());
if (!module->dependencies().empty())
toolTip.append(QString("<p><b>Dependencies:</b> %1</p>").arg(module->dependencies().join(", ").toHtmlEscaped()));
if (!module->description().isEmpty())
toolTip.append(QString("<p><b>Description:</b> %1</p>").arg(QString(module->description().toHtmlEscaped()).replace("\n","<br>")));
// if (!module->version().isEmpty())
// toolTip.append(QString("\nVersion: %1").arg(module->version()));
// if (!module->author().isEmpty())
// toolTip.append(QString("\nAuthor: %1").arg(module->author()));
toolTip.append(QString("<p><b>Path:</b>%1</p>").arg(module->path().toHtmlEscaped()));
if ((module->state() == PythonModuleV1::State::Error || module->state() == PythonModuleV1::State::InvalidMetadata)
&& !module->errorString().isEmpty())
toolTip.append(QString("<p><font color=red><b>ERROR:</b>%1</font></p>").arg(QString(module->errorString().toHtmlEscaped()).replace("\n","<br>")));
return QString("<html><head/><body>%1</body></html>").arg(toolTip);
}
case Qt::CheckStateRole:
if ( static_cast<Section>(index.column()) == Section::Name )
return (extension->isEnabled(*extension->modules()[static_cast<size_t>(index.row())].get()))
? Qt::Checked : Qt::Unchecked;
}
return QVariant();
}
/** ***************************************************************************/
bool Python::ModulesModel::setData(const QModelIndex &index, const QVariant &value, int role) {
if (!index.isValid() || index.row() < 0 || rowCount() <= index.row())
return false;
switch (role) {
case Qt::CheckStateRole:
if ( static_cast<Section>(index.column()) == Section::Name ) {
extension->setEnabled(*extension->modules()[static_cast<size_t>(index.row())].get(),
value == Qt::Checked);
dataChanged(this->index(index.row(),0),
this->index(index.row(),static_cast<int>(Section::Count)));
return true;
}
}
return false;
}
/** ***************************************************************************/
Qt::ItemFlags Python::ModulesModel::flags(const QModelIndex &index) const {
if (!index.isValid() || index.row() >= static_cast<int>(extension->modules().size()) )
return Qt::NoItemFlags;
return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemNeverHasChildren;
}