forked from mrkite/minutor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdimensionidentifier.cpp
180 lines (162 loc) · 4.72 KB
/
dimensionidentifier.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
/** Copyright (c) 2013, Sean Kasun */
#include <QDirIterator>
#include <QtWidgets/QMenu>
#include "./dimensionidentifier.h"
#include "./json.h"
class DimensionDef {
public:
DimensionDef() {}
QString name;
QString path;
int scale;
bool regex;
bool enabled;
};
// --------- --------- --------- ---------
// DimensionIdentifier
// --------- --------- --------- ---------
DimensionIdentifier::DimensionIdentifier() {
group = NULL;
}
DimensionIdentifier::~DimensionIdentifier() {
for (int i = 0; i < packs.length(); i++) {
for (int j = 0; j < packs[i].length(); j++)
delete packs[i][j];
}
}
DimensionIdentifier& DimensionIdentifier::Instance() {
static DimensionIdentifier singleton;
return singleton;
}
void DimensionIdentifier::enableDefinitions(int pack) {
if (pack < 0) return;
int len = packs[pack].length();
for (int i = 0; i < len; i++)
packs[pack][i]->enabled = true;
}
void DimensionIdentifier::disableDefinitions(int pack) {
if (pack < 0) return;
int len = packs[pack].length();
for (int i = 0; i < len; i++)
packs[pack][i]->enabled = false;
}
int DimensionIdentifier::addDefinitions(JSONArray *defs, int pack) {
if (pack == -1) {
pack = packs.length();
packs.append(QList<DimensionDef*>());
}
int len = defs->length();
for (int i = 0; i < len; i++) {
JSONObject *d = dynamic_cast<JSONObject *>(defs->at(i));
DimensionDef *dim = new DimensionDef();
dim->enabled = true;
if (d->has("name"))
dim->name = d->at("name")->asString();
else
dim->name = "Unknown";
if (d->has("path"))
dim->path = d->at("path")->asString();
else
dim->path = ".";
if (d->has("scale"))
dim->scale = d->at("scale")->asNumber();
else
dim->scale = 1;
if (d->has("regex"))
dim->regex = d->at("regex")->asBool();
else
dim->regex = false;
definitions.append(dim);
packs[pack].append(dim);
}
return pack;
}
void DimensionIdentifier::removeDimensions(QMenu *menu) {
for (int i = 0; i < items.count(); i++) {
menu->removeAction(items[i]);
delete items[i];
}
items.clear();
dimensions.clear();
foundDimensions.clear();
menu->setEnabled(false);
if (group != NULL) {
delete group;
group = NULL;
}
}
void DimensionIdentifier::getDimensions(QDir path, QMenu *menu,
QObject *parent) {
// first get the currently selected dimension so it doesn't change
QString current;
for (int i = 0; i < items.length(); i++)
if (items[i]->isChecked())
current = dimensions[i].path;
removeDimensions(menu);
group = new QActionGroup(parent);
for (int i = 0; i < definitions.length(); i++) {
if (definitions[i]->enabled) {
// check path for regex
if (definitions[i]->regex) {
QDirIterator it(path.absolutePath(), QDir::Dirs);
QRegExp rx(definitions[i]->path);
while (it.hasNext()) {
it.next();
if (rx.indexIn(it.fileName()) != -1) {
QString name = definitions[i]->name;
for (int c = 0; c < rx.captureCount(); c++)
name = name.arg(rx.cap(c + 1));
addDimension(path, it.fileName(), name, definitions[i]->scale,
parent);
}
}
} else {
addDimension(path, definitions[i]->path, definitions[i]->name,
definitions[i]->scale, parent);
}
}
}
menu->addActions(items);
if (items.count() > 0) {
bool changed = true;
// locate our old selected item
for (int i = 0; i < items.length(); i++) {
if (dimensions[items[i]->data().toInt()].path == current) {
items[i]->setChecked(true);
changed = false;
break;
}
}
if (changed) {
items.first()->setChecked(true);
emit dimensionChanged(dimensions[items.first()->data().toInt()]);
}
menu->setEnabled(true);
}
}
void DimensionIdentifier::addDimension(QDir path, QString dir, QString name,
int scale, QObject *parent) {
if (!path.exists(dir))
return;
if (foundDimensions.contains(dir))
return;
path.cd(dir);
if (path.exists("region")) { // is it a used dimension?
QAction *d = new QAction(parent);
d->setText(name);
d->setData(dimensions.count());
dimensions.append(DimensionInfo(path.absolutePath(), scale, name));
d->setCheckable(true);
parent->connect(d, SIGNAL(triggered()),
this, SLOT(viewDimension()));
group->addAction(d);
items.append(d);
foundDimensions.insert(dir, true);
}
path.cdUp();
}
void DimensionIdentifier::viewDimension() {
QAction *action = qobject_cast<QAction*>(sender());
if (action)
emit dimensionChanged(dimensions[action->data().toInt()]);
}