-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.cpp
More file actions
513 lines (409 loc) · 15.3 KB
/
project.cpp
File metadata and controls
513 lines (409 loc) · 15.3 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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#include <QTextStream>
#include "project.h"
#include "parser.h"
#include "pjlinkdevice.h"
#include "itemmanager.h"
#include <QDebug>
#include "comdevice.h"
#include "textmesc.h"
#include "buttonmecs.h"
#include "togglebuttonmecs.h"
QString Project::PathToProject;
QString Project::Codepage = "UTF-8";
const QString Project::m_LircdConfPath = "etc/lirc/lircd.conf";
const QString Project::ImagesDirectory = QString("Images");
const QString Project::ControllerDirectory = QString("/controller");
QMap<QString, Lirc *> Project::m_lircConfiges;
QMap<QString, Device*> Project::m_devices;
Project* Project::m_instance = 0;
Project::Project()
{
}
QStringList Project::GetImages()
{
QDir *dir = new QDir(Project::PathToProject + Project::ImagesDirectory);
// TODO throw exception
//if (!dir->exists())
// throw ;
// dir->setFilter(QDir::NoDot | QDir::NoDotDot);
QStringList list = dir->entryList();
if (list.empty())
{
qDebug() << dir->absolutePath();
return QStringList();
}
list.removeFirst();
list.removeFirst();
return list;
}
QStringList Project::GetLircComands(QString name)
{
if (m_lircConfiges.isEmpty())
Project::updateLircConfiges();
if (Project::m_lircConfiges.contains(name))
{
return Project::m_lircConfiges.value(name)->GetCommands();
}
return QStringList();
}
QStringList Project::GetLircDevices()
{
if (m_lircConfiges.isEmpty())
Project::updateLircConfiges();
return QStringList(m_lircConfiges.keys());
}
Lirc *Project::GetLirc(QString name)
{
if (m_lircConfiges.isEmpty())
Project::updateLircConfiges();
if (Project::m_lircConfiges.contains(name))
{
return Project::m_lircConfiges.value(name);
}
return NULL;
}
void Project::updateLircConfiges()
{
QStringList list = Parser::Parse(Project::LircdConfPath(), Lirc::CommandSeparator);
foreach (QString rawString, list)
{
// QString rawString = list.last();
Lirc *lirc = new Lirc();
lirc->Parse(rawString);
m_lircConfiges[lirc->Name()] = lirc;
}
}
void Project::generateDeviceFile(Device *device)
{
QFile file(Project::PathToDevices() + device->GetFileName());
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&file);
out.setCodec(Project::Codepage.toLatin1());
out << device->Save();
file.close();
}
void Project::AddDevice(Device *device)
{
m_devices.insert(device->GetName(), device);
emit DevicesUpdated(Project::GetDevices());
}
QStringList Project::GetDevices()
{
if (m_devices.isEmpty())
return QStringList();
return QStringList(m_devices.keys());
}
Device *Project::GetDevice(QString deviceName)
{
return m_devices[deviceName];
}
QStringList Project::GetDeviceCommands(QString deviceName)
{
if (m_devices.contains(deviceName))
return m_devices[deviceName]->GetCommands();
return QStringList();
}
void Project::GenerateDevicesFile()
{
QFile file(Project::PathToProject + "protocols all");
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&file);
out.setCodec(Project::Codepage.toLatin1());
foreach (Device *device, m_devices) {
generateDeviceFile(device);
out << "controller/" + device->GetFileName() << endl;
}
file.close();
}
Project *Project::Instance()
{
static QMutex mutex;
if (!m_instance)
{
mutex.lock();
if (!m_instance)
m_instance = new Project;
mutex.unlock();
}
return m_instance;
}
bool Project::ParseInterface()
{
qDebug() <<Project::PathToProject + "/interface";
QString rawData = readFile(Project::PathToProject + "/interface");
int i = 0;
QRegExp rawType("\\[(\\w+)\\]");
while ((i = rawType.indexIn(rawData, i)) >= 0)
{
i += rawType.matchedLength();
QString type = rawType.cap(1);
if (type.compare("Page") == 0)
{
QRegExp rawName("Name = (\\w+)");
rawName.indexIn(rawData, i);
QString name= rawName.cap(1);
QRegExp rawBackground("Background = (\\w+.)/(\\w+)\\.(\\w+)");
rawBackground.indexIn(rawData, i);
QString background= rawBackground.cap(1) + "/" + rawBackground.cap(2) + "." + rawBackground.cap(3);
ItemManager::Instance()->AddItem(new Page(name, background));
}
else if (type.compare("Text") == 0)
{
/*
[Text]
Align = left
Pages = Extron
Left = 250
Top = 25
Font = Images/arial.ttf
FontSize = 20
Color = white
Width = 200
Height = 50
DefaultText = Extron
*/
QRegExp rawAlign("Align = (\\w+)");
rawAlign.indexIn(rawData, i);
QString Align= rawAlign.cap(1);
QRegExp rawPages("Pages = (\\w+)");
rawPages.indexIn(rawData, i);
QString Pages= rawPages.cap(1);
QRegExp rawLeft("Left = (\\d+)");
rawLeft.indexIn(rawData, i);
QString Left= rawLeft.cap(1);
QRegExp rawTop("Top = (\\d+)");
rawTop.indexIn(rawData, i);
QString Top= rawTop.cap(1);
QRegExp rawFontSize("FontSize = (\\d+)");
rawFontSize.indexIn(rawData, i);
QString FontSize= rawFontSize.cap(1);
QRegExp rawWidth("Width = (\\d+)");
rawWidth.indexIn(rawData, i);
QString Width= rawWidth.cap(1);
QRegExp rawHeight("Height = (\\d+)");
rawHeight.indexIn(rawData, i);
QString Height= rawHeight.cap(1);
QRegExp rawFont("Font = (\\w+)/(\\w+)\\.(\\w+)");
rawFont.indexIn(rawData, i);
QString Font= rawFont.cap(1) + "/" + rawFont.cap(2) + "." + rawFont.cap(3);
QRegExp rawColor("Color = (\\w+)");
rawColor.indexIn(rawData, i);
QString Color= rawColor.cap(1);
QRegExp rawDefaultText("DefaultText = (\\w+)");
rawDefaultText.indexIn(rawData, i);
QString DefaultText= rawDefaultText.cap(1);
TextMESC *text = new TextMESC(Left.toInt(), Top.toInt());
text->setWidth(Width.toInt());
text->setHeight(Height.toInt());
text->setText(DefaultText);
text->setPage(Pages);
ItemManager::Instance()->AddItem(text);
}
else if (type.compare("Button") == 0)
{
// [Button]
// Caption = Включить
// Pages = Projectors
// Left = 25
// Top = 150
// Font = Images/arial.ttf
// FontSize = 20
// Color = white
// UpImage = Images/menu_btn.png
// DownImage = Images/menu_light_btn.png
// HeldImage = Images/menu_light_btn.png
// OnClick = Command Mitsubishil poweron
// OnHold = Command Mitsubishil poweron
QRegExp rawUpImage("UpImage = (\\w+.)/(\\w+)\\.(\\w+)");
rawUpImage.indexIn(rawData, i);
QString UpImage= rawUpImage.cap(1) + "/" + rawUpImage.cap(2) + "." + rawUpImage.cap(3);
QRegExp rawDownImage("DownImage = (\\w+.)/(\\w+)\\.(\\w+)");
rawDownImage.indexIn(rawData, i);
QString DownImage= rawDownImage.cap(1) + "/" + rawDownImage.cap(2) + "." + rawDownImage.cap(3);
QRegExp rawHeldImage("HeldImage = (\\w+.)/(\\w+)\\.(\\w+)");
rawHeldImage.indexIn(rawData, i);
QString HeldImage= rawHeldImage.cap(1) + "/" + rawHeldImage.cap(2) + "." + rawHeldImage.cap(3);
QRegExp rawOnClick("OnClick\\s*=\\s*(\\w+)\\s*(\\w+)([ \\w]*)");
rawOnClick.indexIn(rawData, i);
QString commandType = rawOnClick.cap(1).trimmed();
QString target = rawOnClick.cap(2).trimmed();
QString command = rawOnClick.cap(3).trimmed();
// QRegExp rawOnHold("OnHold\\s*=\\s*(\\w+)\\s*(\\w+)([ \\w]*)");
// rawOnHold.indexIn(rawData, i);
// QString holdType = rawOnHold.cap(1).trimmed();
// QString holdtarget = rawOnHold.cap(2).trimmed();
// QString holdcommand = rawOnHold.cap(3).trimmed();
QRegExp rawLeft("Left = (\\d+)");
rawLeft.indexIn(rawData, i);
QString Left= rawLeft.cap(1);
QRegExp rawTop("Top = (\\d+)");
rawTop.indexIn(rawData, i);
QString Top= rawTop.cap(1);
QRegExp rawCaption("Caption = ([\\w \\+-]*)");
rawCaption.indexIn(rawData, i);
QString Caption= rawCaption.cap(1);
QRegExp rawPages("Pages = (\\w+)");
rawPages.indexIn(rawData, i);
QString Pages= rawPages.cap(1);
QRegExp rawFont("Font = (\\w+)/(\\w+)\\.(\\w+)");
rawFont.indexIn(rawData, i);
QString Font= rawFont.cap(1) + "/" + rawFont.cap(2) + "." + rawFont.cap(3);
QRegExp rawColor("Color = (\\w+)");
rawColor.indexIn(rawData, i);
QString Color= rawColor.cap(1);
QRegExp rawFontSize("FontSize = (\\d+)");
rawFontSize.indexIn(rawData, i);
QString FontSize= rawFontSize.cap(1);
ButtonMECS *button = new ButtonMECS(Left.toInt(), Top.toInt());
button->setText(Caption);
button->setBackgroundImage(UpImage);
button->SetDownImage(DownImage);
button->SetHeldImage(HeldImage);
button->SetOnClickAction(commandType, target, command);
button->setPage(Pages);
ItemManager::Instance()->AddItem(button);
}
else if (type.compare("ToggleButton") == 0)
{
// [ToggleButton]
// Caption = MuteL
// Pages = Main
// Left = 25
// Top = 400
// Font = Images/arial.ttf
// FontSize = 20
// Color = white
// UpImage = Images/menu_btn.png
// DownImage = Images/menu_light_btn.png
// HeldImage = Images/menu_light_btn.png
// OnDown = Command Mitsubishil muteon
// OnUp = Command Mitsubishil muteoff
QRegExp rawUpImage("UpImage = (\\w+.)/(\\w+)\\.(\\w+)");
rawUpImage.indexIn(rawData, i);
QString UpImage= rawUpImage.cap(1) + "/" + rawUpImage.cap(2) + "." + rawUpImage.cap(3);
QRegExp rawDownImage("DownImage = (\\w+.)/(\\w+)\\.(\\w+)");
rawDownImage.indexIn(rawData, i);
QString DownImage= rawDownImage.cap(1) + "/" + rawDownImage.cap(2) + "." + rawDownImage.cap(3);
QRegExp rawHeldImage("HeldImage = (\\w+.)/(\\w+)\\.(\\w+)");
rawHeldImage.indexIn(rawData, i);
QString HeldImage= rawHeldImage.cap(1) + "/" + rawHeldImage.cap(2) + "." + rawHeldImage.cap(3);
QRegExp rawOnDown("OnDown\\s*=\\s*(\\w+)\\s*(\\w+)([ \\w]*)");
rawOnDown.indexIn(rawData, i);
QString commandTypeDown = rawOnDown.cap(1).trimmed();
QString targetDown = rawOnDown.cap(2).trimmed();
QString commandDown = rawOnDown.cap(3).trimmed();
QRegExp rawOnUp("OnUp\\s*=\\s*(\\w+)\\s*(\\w+)([ \\w]*)");
rawOnUp.indexIn(rawData, i);
QString commandTypeUp = rawOnUp.cap(1).trimmed();
QString targetUp = rawOnUp.cap(2).trimmed();
QString commandUp = rawOnUp.cap(3).trimmed();
QRegExp rawLeft("Left = (\\d+)");
rawLeft.indexIn(rawData, i);
QString Left= rawLeft.cap(1);
QRegExp rawTop("Top = (\\d+)");
rawTop.indexIn(rawData, i);
QString Top= rawTop.cap(1);
QRegExp rawCaption("Caption = ([\\w ]+)");
rawCaption.indexIn(rawData, i);
QString Caption= rawCaption.cap(1);
QRegExp rawPages("Pages = (\\w+)");
rawPages.indexIn(rawData, i);
QString Pages= rawPages.cap(1);
QRegExp rawFont("Font = (\\w+)/(\\w+)\\.(\\w+)");
rawFont.indexIn(rawData, i);
QString Font= rawFont.cap(1) + "/" + rawFont.cap(2) + "." + rawFont.cap(3);
QRegExp rawColor("Color = (\\w+)");
rawColor.indexIn(rawData, i);
QString Color= rawColor.cap(1);
QRegExp rawFontSize("FontSize = (\\d+)");
rawFontSize.indexIn(rawData, i);
QString FontSize= rawFontSize.cap(1);
ToggleButtonMECS *button = new ToggleButtonMECS(Left.toInt(), Top.toInt());
button->setText(Caption);
button->setBackgroundImage(UpImage);
button->SetDownImage(DownImage);
button->SetHeldImage(HeldImage);
button->SetOnUpAction(commandTypeUp, targetUp, commandUp);
button->SetOnDownAction(commandTypeDown, targetDown, commandDown);
button->setPage(Pages);
ItemManager::Instance()->AddItem(button);
}
}
return true;
}
void Project::ParseAllDevicesFromProjectRoot()
{
QDir dir(Project::PathToProject + "controller/");
QStringList filter;
filter << "*.pjlink" << "*.rj45" << "*.rs232" << "*.local";// << "*.lirc";
QFileInfoList dirContent = dir.entryInfoList(filter, QDir::Files | QDir::NoDotAndDotDot);
foreach (QFileInfo info, dirContent)
{
qDebug() << info.filePath();
Project::Instance()->ParseDevice(info.filePath());
}
}
QString Project::readFile(QString filename)
{
QFile file(filename);
file.open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream inStream(&file);
inStream.setCodec(Project::Codepage.toLatin1());
// inStream.setCodec("UTF-8");
QString rawData = inStream.readAll();
file.close();
return rawData;
}
bool Project::ParseDevice(QString filename)
{
QString rawData = readFile(filename);
QFileInfo info(filename);
if (!rawData.startsWith("protocol"))
return false;
QRegExp rawType("protocol (\\w+): (\\w+)");
rawType.indexIn(rawData, 0);
Device *parsedDevice;
QString type = rawType.cap(2);
QString name = rawType.cap(1);
if ((type.compare("com") == 0) || (type.compare("usb") == 0))
{
QRegExp rawPort("port: (0|1)");
rawPort.indexIn(rawData, 0);
QString port = rawPort.cap(1);
QRegExp rawSpeed("speed: (\\d+)");
rawSpeed.indexIn(rawData, 0);
QString speed = rawSpeed.cap(1);
QRegExp rawcharacterSize("character-size: (\\d+)");
rawcharacterSize.indexIn(rawData, 0);
QString characterSize = rawcharacterSize.cap(1);
QRegExp rawParity("parity: (none|.+\r\n)");
rawParity.indexIn(rawData, 0);
QString parity = rawParity.cap(1);
parsedDevice = new ComDevice(name, port, speed, parity, characterSize);
int i = 0;
QRegExp rawMethod("command (\\w+)\n\\{\n\\s+send \\w+ \\(([^\n]+)\\)");
while ((i = rawMethod.indexIn(rawData, i)) > 0)
{
i += rawMethod.matchedLength();
parsedDevice->addCommand(rawMethod.cap(1), rawMethod.cap(2));
}
}
else if (type.compare("tcp") == 0)
{
QRegExp rawTargetIp("target-ip: \"([\\d|\\.]+)\\:(\\d+)");
rawTargetIp.indexIn(rawData, 0);
QString targetIp = rawTargetIp.cap(1);
QString targetPort = rawTargetIp.cap(2);
parsedDevice = new PJLinkDevice(name, targetIp, targetPort);
int i = 0;
QRegExp rawMethod("command (\\w+)\n\\{\n\\s+send \\w+ \\(([^\n]+)\\)");
while ((i = rawMethod.indexIn(rawData, i)) > 0)
{
i += rawMethod.matchedLength();
parsedDevice->addCommand(rawMethod.cap(1), rawMethod.cap(2));
}
}
parsedDevice->SetProtocolName(info.completeSuffix());
this->AddDevice(parsedDevice);
return true;
}