-
Notifications
You must be signed in to change notification settings - Fork 1
/
settingswidget.cpp
364 lines (347 loc) · 12.6 KB
/
settingswidget.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
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
/*
Copyright (c) 2010 by Stanislav (proDOOMman) Kosolapov <[email protected]>
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
*/
#include "settingswidget.h"
#include <QDeclarativeContext>
#define QMLHOST QString("http://qmlsaver.shell.tor.hu/")
SettingsWidget::SettingsWidget(QWidget *parent) :
QWidget(parent){
net = new QNetworkAccessManager(this);
setupUi(this);
qmlapp = new QmlApplicationView("",previewPlaceHolder);
connect(qmlapp,SIGNAL(sceneResized(QSize)),this,SLOT(sceneResized(QSize)));
emit updateList();
((QVBoxLayout*)previewPlaceHolder->layout())->addWidget(qmlapp);
}
void SettingsWidget::sceneResized(QSize s)
{
qDebug() << s;
qmlapp->updateSize(s);
}
void SettingsWidget::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
retranslateUi(this);
break;
default:
break;
}
}
void SettingsWidget::updateList()
{
QNetworkRequest req(QMLHOST+"list.php");
QNetworkReply *rep = net->get(req);
QByteArray result;
while(!rep->isFinished())
{
rep->waitForReadyRead(100);
qApp->processEvents();
}
result = rep->readAll();
rep->deleteLater();
QList<QString> urllist = QString(result).split('\n',QString::SkipEmptyParts);
foreach(QString s,urllist)
{
QListWidgetItem *item = new QListWidgetItem(quirkList);
item->setText("[web] "+s);
item->setData(Qt::UserRole,QMLHOST+"qmls/"+s);
}
QSettings s("qmlsaver");
QString qmldir = s.value("dir","").toString();
QStringList dirs;
dirs << "/usr/share/qmlsaver/qmls/" << QFileInfo(s.fileName()).absoluteDir().absolutePath()+"/qmlsaver/";
foreach(QString s, dirs)
{
QDir d(s);
if(!d.exists())
continue;
foreach(QFileInfo fi, d.entryInfoList(QDir::AllDirs|QDir::NoDotAndDotDot))
{
if(QDir(fi.absoluteFilePath()).exists("main.qml"))
{
QListWidgetItem *item = new QListWidgetItem(quirkList);
item->setText("[local] "+fi.fileName());
item->setData(Qt::UserRole,QUrl::fromLocalFile(fi.absoluteFilePath()));
if(!fi.absoluteFilePath().compare(qmldir))
quirkList->setCurrentItem(item);
}
}
}
}
void SettingsWidget::on_quirkList_currentItemChanged(QListWidgetItem* current, QListWidgetItem* previous)
{
if(previous)
saveSettings(previous);
createSettings(current);
loadSettings(current);
qmlapp->setSource(current->data(Qt::UserRole).toUrl().toString()+"/main.qml");
qmlapp->loadSettings();
}
void SettingsWidget::saveSettings(QListWidgetItem* it)
{
QSettings s("qmlsaver");
s.beginGroup((it->data(Qt::UserRole).toUrl().toString()+"/main.qml").toLocal8Bit().toPercentEncoding());
foreach(SetItem item, settingsItems)
{
if(!item.type.compare("integer"))
{
s.setValue(item.name,qobject_cast<QSpinBox*>(item.widget)->value());
}
else if(!item.type.compare("float"))
{
s.setValue(item.name,qobject_cast<QDoubleSpinBox*>(item.widget)->value());
}
else if(!item.type.compare("color"))
{
QPalette p = qobject_cast<QToolButton*>(item.widget)->palette();
s.setValue(item.name,p.color(QPalette::Button));
}
else if(!item.type.compare("file"))
{
QLineEdit *le = qobject_cast<QLineEdit*>(item.widget->layout()->itemAt(0)->widget());
// QToolButton *tb = qobject_cast<QToolButton*>(item.widget->layout()->itemAt(1)->widget());d
s.setValue(item.name,le->text());
}
else if(!item.type.compare("string"))
{
s.setValue(item.name,qobject_cast<QLineEdit*>(item.widget)->text());
}
}
}
void SettingsWidget::loadSettings(QListWidgetItem*)
{
QSettings s("qmlsaver");
s.beginGroup((quirkList->currentItem()->data(Qt::UserRole).toUrl().toString()+"/main.qml").toLocal8Bit().toPercentEncoding());
foreach(SetItem item, settingsItems)
{
QVariant value = s.value(item.name,item.def);
if(!item.type.compare("integer"))
{
(qobject_cast<QSpinBox*>(item.widget))->setValue(value.toInt());
}
else if(!item.type.compare("float"))
{
(qobject_cast<QDoubleSpinBox*>(item.widget))->setValue(value.toFloat());
}
else if(!item.type.compare("color"))
{
QToolButton *pb = qobject_cast<QToolButton*>(item.widget);
QPalette p = pb->palette();
p.setColor(QPalette::Button,QColor(value.toString()));
pb->setPalette(p);
}
else if(!item.type.compare("file"))
{
QLineEdit *le = qobject_cast<QLineEdit*>(item.widget->layout()->itemAt(0)->widget());
// QToolButton *tb = qobject_cast<QToolButton*>(item.widget->layout()->itemAt(1)->widget());d
le->setText(value.toString());
}
else if(!item.type.compare("string"))
{
(qobject_cast<QLineEdit*>(item.widget))->setText(value.toString());
}
}
setSettings();
}
void SettingsWidget::createSettings(QListWidgetItem* item)
{
settingsItems.clear();
QFormLayout *l = (QFormLayout*)settingsAreaContents->layout();
for(int i = l->count()-1; i>=0; i--)
{
delete l->itemAt(i)->widget();
l->removeItem(l->itemAt(i));
}
QUrl url = item->data(Qt::UserRole).toUrl();
QDomDocument doc;
if(!url.scheme().compare("file"))
{
//local file
QString dirname = url.toString(QUrl::RemoveScheme);
if(!QFile::exists(dirname+"/settings.xml"))
return;
QFile f(dirname+"/settings.xml");
if(!f.open(QIODevice::ReadOnly|QIODevice::Text))
return;
if(!doc.setContent(f.readAll()))
return;
}
else
{
//network
QNetworkReply* rep = net->get(QNetworkRequest(QUrl(url.toString()+"/settings.xml")));
while(!rep->isFinished())
{
rep->waitForReadyRead(100);
qApp->processEvents();
}
doc.setContent(rep->readAll());
rep->deleteLater();
}
QDomNode rootNode = doc.documentElement();
QDomNode node = rootNode.firstChild();
while(!node.isNull())
{
QDomElement elem = node.toElement();
node = node.nextSibling();
if(elem.tagName().compare("item"))
continue;
QString name = elem.attribute("name");
if(name.isEmpty())
continue;
SetItem sItem;
sItem.name = name;
sItem.widget = 0;
sItem.type = elem.attribute("type","string");
sItem.def = elem.attribute("default","");
sItem.text = elem.attribute(tr("trans_en","Attribute that contains localized string"),elem.text());
if(!sItem.type.compare("integer"))
{
QSpinBox *sb = new QSpinBox();
sb->setMaximum(99999);
sb->setMinimum(-9999);
sItem.widget = (QWidget*)sb;
connect(sb,SIGNAL(valueChanged(int)),this,SLOT(setSettings()));
}
else if(!sItem.type.compare("float"))
{
QDoubleSpinBox *sb = new QDoubleSpinBox();
sb->setMaximum(9999);
sb->setMinimum(-9999);
sItem.widget = (QWidget*)sb;
connect(sb,SIGNAL(valueChanged(double)),this,SLOT(setSettings()));
}
else if(!sItem.type.compare("color"))
{
QToolButton *tb = new QToolButton();
tb->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred);
tb->setAutoRaise(true);
tb->setAutoFillBackground(true);
connect(tb,SIGNAL(released()),this,SLOT(setToolButtonColor()));
sItem.widget = tb;
}
else if(!sItem.type.compare("file"))
{
sItem.widget = new QWidget();
sItem.widget->setLayout(new QHBoxLayout());
sItem.widget->layout()->addWidget(new QLineEdit());
QToolButton *tb = new QToolButton(sItem.widget);
connect(tb,SIGNAL(released()),this,SLOT(setLineEditFile()));
tb->setText("...");
sItem.widget->layout()->addWidget(tb);
}
else if(!sItem.type.compare("string"))
{
QLineEdit *le = new QLineEdit();
sItem.widget = le;
connect(le,SIGNAL(textChanged(QString)),this,SLOT(setSettings()));
}
if(!sItem.widget)
continue;
l->addRow(sItem.text,sItem.widget);
settingsItems[name] = sItem;
}
}
void SettingsWidget::on_buttonBox_clicked(QAbstractButton* button)
{
QSettings s("qmlsaver");
switch(buttonBox->standardButton(button))
{
case QDialogButtonBox::Save:
s.setValue("dir",quirkList->currentItem()->data(Qt::UserRole).toUrl().toString());
saveSettings(quirkList->currentItem());
qApp->exit(0);
break;
case QDialogButtonBox::Discard:
qApp->exit(0);
break;
case QDialogButtonBox::Reset:
foreach(SetItem item, settingsItems)
{
QVariant value = item.def;
if(!item.type.compare("integer"))
{
(qobject_cast<QSpinBox*>(item.widget))->setValue(value.toInt());
}
else if(!item.type.compare("float"))
{
(qobject_cast<QDoubleSpinBox*>(item.widget))->setValue(value.toFloat());
}
else if(!item.type.compare("color"))
{
QToolButton *pb = qobject_cast<QToolButton*>(item.widget);
QPalette p = pb->palette();
p.setColor(QPalette::Button,QColor(value.toString()));
pb->setPalette(p);
}
else if(!item.type.compare("file"))
{
QLineEdit *le = qobject_cast<QLineEdit*>(item.widget->layout()->itemAt(0)->widget());
le->setText(value.toString());
}
else if(!item.type.compare("string"))
{
(qobject_cast<QLineEdit*>(item.widget))->setText(value.toString());
}
}
setSettings();
break;
default:
break;
}
}
void SettingsWidget::setToolButtonColor()
{
QToolButton *tb = qobject_cast<QToolButton*>(sender());
QPalette p = tb->palette();
p.setColor(QPalette::Button,QColorDialog::getColor(p.color(QPalette::Button),
this,
tr("Select color"),QColorDialog::ShowAlphaChannel));
tb->setPalette(p);
setSettings();
}
void SettingsWidget::setLineEditFile()
{
QWidget *w = qobject_cast<QWidget*>(sender()->parent());
QLineEdit *le = qobject_cast<QLineEdit*>(w->layout()->itemAt(0)->widget());
le->setText("file://"+QFileDialog::getOpenFileName(this,tr("Select file"),QFileInfo(le->text()).absoluteDir().path()));
setSettings();
}
void SettingsWidget::setSettings()
{
foreach(SetItem item, settingsItems)
{
if(!item.type.compare("integer"))
{
qmlapp->rootContext()->setContextProperty(item.name,qobject_cast<QSpinBox*>(item.widget)->value());
}
else if(!item.type.compare("float"))
{
qmlapp->rootContext()->setContextProperty(item.name,qobject_cast<QDoubleSpinBox*>(item.widget)->value());
}
else if(!item.type.compare("color"))
{
QPalette p = qobject_cast<QToolButton*>(item.widget)->palette();
qmlapp->rootContext()->setContextProperty(item.name,p.color(QPalette::Button));
}
else if(!item.type.compare("file"))
{
QLineEdit *le = qobject_cast<QLineEdit*>(item.widget->layout()->itemAt(0)->widget());
qmlapp->rootContext()->setContextProperty(item.name,le->text());
}
else if(!item.type.compare("string"))
{
qmlapp->rootContext()->setContextProperty(item.name,qobject_cast<QLineEdit*>(item.widget)->text());
}
}
}