Child: [f34994] (diff)

Download this file

confgui.cpp    456 lines (393 with data), 12.2 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
/* Copyright (C) 2005 J.F.Dockes
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <qglobal.h>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QFrame>
#include <QListWidget>
#include <qobject.h>
#include <qlayout.h>
#include <qsize.h>
#include <qsizepolicy.h>
#include <qlabel.h>
#include <qspinbox.h>
#include <qtooltip.h>
#include <qlineedit.h>
#include <qcheckbox.h>
#include <qinputdialog.h>
#include <qpushbutton.h>
#include <qstringlist.h>
#include <qcombobox.h>
#include "confgui.h"
#include "smallut.h"
#include "debuglog.h"
#include "rcldb.h"
#include "guiutils.h"
#include <list>
#include <vector>
using std::list;
using std::vector;
namespace confgui {
static const int spacing = 2;
static const int margin = 2;
void ConfParamW::setValue(const QString& value)
{
if (m_fsencoding)
m_cflink->set(string((const char *)value.toLocal8Bit()));
else
m_cflink->set(string((const char *)value.toUtf8()));
}
void ConfParamW::setValue(int value)
{
char buf[30];
sprintf(buf, "%d", value);
m_cflink->set(string(buf));
}
void ConfParamW::setValue(bool value)
{
char buf[30];
sprintf(buf, "%d", value);
m_cflink->set(string(buf));
}
void setSzPol(QWidget *w, QSizePolicy::Policy hpol,
QSizePolicy::Policy vpol,
int hstretch, int vstretch)
{
QSizePolicy policy(hpol, vpol);
policy.setHorizontalStretch(hstretch);
policy.setVerticalStretch(vstretch);
policy.setHeightForWidth(w->sizePolicy().hasHeightForWidth());
w->setSizePolicy(policy);
}
bool ConfParamW::createCommon(const QString& lbltxt, const QString& tltptxt)
{
m_hl = new QHBoxLayout(this);
m_hl->setSpacing(spacing);
QLabel *tl = new QLabel(this);
setSzPol(tl, QSizePolicy::Preferred, QSizePolicy::Fixed, 0, 0);
tl->setText(lbltxt);
tl->setToolTip(tltptxt);
m_hl->addWidget(tl);
return true;
}
ConfParamIntW::ConfParamIntW(QWidget *parent, ConfLink cflink,
const QString& lbltxt,
const QString& tltptxt,
int minvalue,
int maxvalue,
int defaultvalue)
: ConfParamW(parent, cflink), m_defaultvalue(defaultvalue)
{
if (!createCommon(lbltxt, tltptxt))
return;
m_sb = new QSpinBox(this);
m_sb->setMinimum(minvalue);
m_sb->setMaximum(maxvalue);
setSzPol(m_sb, QSizePolicy::Fixed, QSizePolicy::Fixed, 0, 0);
m_hl->addWidget(m_sb);
QFrame *fr = new QFrame(this);
setSzPol(fr, QSizePolicy::Preferred, QSizePolicy::Fixed, 0, 0);
m_hl->addWidget(fr);
loadValue();
QObject::connect(m_sb, SIGNAL(valueChanged(int)),
this, SLOT(setValue(int)));
}
void ConfParamIntW::loadValue()
{
string s;
if (m_cflink->get(s))
m_sb->setValue(atoi(s.c_str()));
else
m_sb->setValue(m_defaultvalue);
}
ConfParamStrW::ConfParamStrW(QWidget *parent, ConfLink cflink,
const QString& lbltxt,
const QString& tltptxt)
: ConfParamW(parent, cflink)
{
if (!createCommon(lbltxt, tltptxt))
return;
m_le = new QLineEdit(this);
setSzPol(m_le, QSizePolicy::Preferred, QSizePolicy::Fixed, 1, 0);
m_hl->addWidget(m_le);
loadValue();
QObject::connect(m_le, SIGNAL(textChanged(const QString&)),
this, SLOT(setValue(const QString&)));
}
void ConfParamStrW::loadValue()
{
string s;
m_cflink->get(s);
if (m_fsencoding)
m_le->setText(QString::fromLocal8Bit(s.c_str()));
else
m_le->setText(QString::fromUtf8(s.c_str()));
}
ConfParamCStrW::ConfParamCStrW(QWidget *parent, ConfLink cflink,
const QString& lbltxt,
const QString& tltptxt,
const QStringList &sl
)
: ConfParamW(parent, cflink)
{
if (!createCommon(lbltxt, tltptxt))
return;
m_cmb = new QComboBox(this);
m_cmb->setEditable(FALSE);
m_cmb->insertItems(0, sl);
setSzPol(m_cmb, QSizePolicy::Preferred, QSizePolicy::Fixed, 1, 0);
m_hl->addWidget(m_cmb);
loadValue();
QObject::connect(m_cmb, SIGNAL(activated(const QString&)),
this, SLOT(setValue(const QString&)));
}
void ConfParamCStrW::loadValue()
{
string s;
m_cflink->get(s);
QString cs;
if (m_fsencoding)
cs = QString::fromLocal8Bit(s.c_str());
else
cs = QString::fromUtf8(s.c_str());
for (int i = 0; i < m_cmb->count(); i++) {
if (!cs.compare(m_cmb->itemText(i))) {
m_cmb->setCurrentIndex(i);
break;
}
}
}
ConfParamBoolW::ConfParamBoolW(QWidget *parent, ConfLink cflink,
const QString& lbltxt,
const QString& tltptxt)
: ConfParamW(parent, cflink)
{
// No createCommon because the checkbox has a label
m_hl = new QHBoxLayout(this);
m_hl->setSpacing(spacing);
m_cb = new QCheckBox(lbltxt, this);
setSzPol(m_cb, QSizePolicy::Fixed, QSizePolicy::Fixed, 0, 0);
m_cb->setToolTip(tltptxt);
m_hl->addWidget(m_cb);
QFrame *fr = new QFrame(this);
setSzPol(fr, QSizePolicy::Preferred, QSizePolicy::Fixed, 1, 0);
m_hl->addWidget(fr);
loadValue();
QObject::connect(m_cb, SIGNAL(toggled(bool)), this, SLOT(setValue(bool)));
}
void ConfParamBoolW::loadValue()
{
string s;
m_cflink->get(s);
m_cb->setChecked(stringToBool(s));
}
ConfParamFNW::ConfParamFNW(QWidget *parent, ConfLink cflink,
const QString& lbltxt,
const QString& tltptxt,
bool isdir
)
: ConfParamW(parent, cflink), m_isdir(isdir)
{
if (!createCommon(lbltxt, tltptxt))
return;
m_fsencoding = true;
m_le = new QLineEdit(this);
m_le->setMinimumSize(QSize(150, 0 ));
setSzPol(m_le, QSizePolicy::Preferred, QSizePolicy::Fixed, 1, 0);
m_hl->addWidget(m_le);
m_pb = new QPushButton(this);
setSzPol(m_pb, QSizePolicy::Fixed, QSizePolicy::Fixed, 1, 0);
m_hl->addWidget(m_pb);
loadValue();
QObject::connect(m_pb, SIGNAL(clicked()), this, SLOT(showBrowserDialog()));
QObject::connect(m_le, SIGNAL(textChanged(const QString&)),
this, SLOT(setValue(const QString&)));
}
void ConfParamFNW::loadValue()
{
string s;
m_cflink->get(s);
m_le->setText(QString::fromLocal8Bit(s.c_str()));
}
void ConfParamFNW::showBrowserDialog()
{
QString s = myGetFileName(m_isdir);
if (!s.isEmpty())
m_le->setText(s);
}
class SmallerListWidget: public QListWidget
{
public:
SmallerListWidget(QWidget *parent)
: QListWidget(parent) {}
virtual QSize sizeHint() {return QSize(150, 40);}
};
ConfParamSLW::ConfParamSLW(QWidget *parent, ConfLink cflink,
const QString& lbltxt,
const QString& tltptxt)
: ConfParamW(parent, cflink)
{
// Can't use createCommon here cause we want the buttons below the label
m_hl = new QHBoxLayout(this);
m_hl->setSpacing(spacing);
QVBoxLayout *vl1 = new QVBoxLayout();
QHBoxLayout *hl1 = new QHBoxLayout();
QLabel *tl = new QLabel(this);
setSzPol(tl, QSizePolicy::Preferred, QSizePolicy::Fixed, 0, 0);
tl->setText(lbltxt);
tl->setToolTip(tltptxt);
vl1->addWidget(tl);
QPushButton *pbA = new QPushButton(this);
pbA->setText(tr("+"));
setSzPol(pbA, QSizePolicy::Fixed, QSizePolicy::Fixed, 0, 0);
hl1->addWidget(pbA);
QPushButton *pbD = new QPushButton(this);
setSzPol(pbD, QSizePolicy::Fixed, QSizePolicy::Fixed, 0, 0);
pbD->setText(tr("-"));
hl1->addWidget(pbD);
vl1->addLayout(hl1);
m_hl->addLayout(vl1);
m_lb = new SmallerListWidget(this);
m_lb->setSelectionMode(QAbstractItemView::ExtendedSelection);
setSzPol(m_lb, QSizePolicy::Preferred, QSizePolicy::Preferred, 1, 1);
m_hl->addWidget(m_lb);
setSzPol(this, QSizePolicy::Preferred, QSizePolicy::Preferred, 1, 1);
loadValue();
QObject::connect(pbA, SIGNAL(clicked()), this, SLOT(showInputDialog()));
QObject::connect(pbD, SIGNAL(clicked()), this, SLOT(deleteSelected()));
}
void ConfParamSLW::loadValue()
{
string s;
m_cflink->get(s);
list<string> ls;
stringToStrings(s, ls);
QStringList qls;
for (list<string>::const_iterator it = ls.begin(); it != ls.end(); it++) {
if (m_fsencoding)
qls.push_back(QString::fromLocal8Bit(it->c_str()));
else
qls.push_back(QString::fromUtf8(it->c_str()));
}
m_lb->clear();
m_lb->insertItems(0, qls);
}
void ConfParamSLW::showInputDialog()
{
bool ok;
QString s = QInputDialog::getText (this,
"", // title
"", // label,
QLineEdit::Normal, // EchoMode mode
"", // const QString & text
&ok);
if (ok && !s.isEmpty()) {
QList<QListWidgetItem *>items =
m_lb->findItems(s, Qt::MatchFixedString|Qt::MatchCaseSensitive);
if (items.empty()) {
m_lb->insertItem(0, s);
m_lb->sortItems();
listToConf();
}
}
}
void ConfParamSLW::listToConf()
{
list<string> ls;
for (int i = 0; i < m_lb->count(); i++) {
// General parameters are encoded as utf-8. File names as
// local8bit There is no hope for 8bit file names anyway
// except for luck: the original encoding is unknown.
QString text = m_lb->item(i)->text();
if (m_fsencoding)
ls.push_back((const char *)(text.toLocal8Bit()));
else
ls.push_back((const char *)(text.toUtf8()));
}
string s;
stringsToString(ls, s);
m_cflink->set(s);
}
void ConfParamSLW::deleteSelected()
{
// We used to repeatedly go through the list and delete the first
// found selected item (then restart from the beginning). But it
// seems (probably depends on the qt version), that, when deleting
// a selected item, qt will keep the selection active at the same
// index (now containing the next item), so that we'd end up
// deleting the whole list.
//
// Instead, we now build a list of indices, and delete it starting
// from the top so as not to invalidate lower indices
vector<int> idxes;
for (int i = 0; i < m_lb->count(); i++) {
if (m_lb->item(i)->isSelected()) {
idxes.push_back(i);
}
}
for (vector<int>::reverse_iterator it = idxes.rbegin();
it != idxes.rend(); it++) {
LOGDEB0(("deleteSelected: %d was selected\n", *it));
QListWidgetItem *item = m_lb->takeItem(*it);
emit entryDeleted(item->text());
delete item;
}
listToConf();
}
// "Add entry" dialog for a file name list
void ConfParamDNLW::showInputDialog()
{
QString s = myGetFileName(true);
if (!s.isEmpty()) {
QList<QListWidgetItem *>items =
m_lb->findItems(s, Qt::MatchFixedString|Qt::MatchCaseSensitive);
if (items.empty()) {
m_lb->insertItem(0, s);
m_lb->sortItems();
QList<QListWidgetItem *>items =
m_lb->findItems(s, Qt::MatchFixedString|Qt::MatchCaseSensitive);
if (m_lb->selectionMode() == QAbstractItemView::SingleSelection &&
!items.empty())
m_lb->setCurrentItem(*items.begin());
listToConf();
}
}
}
// "Add entry" dialog for a constrained string list
void ConfParamCSLW::showInputDialog()
{
bool ok;
QString s = QInputDialog::getItem (this, // parent
"", // title
"", // label,
m_sl, // items,
0, // current = 0
FALSE, // editable = true,
&ok);
if (ok && !s.isEmpty()) {
QList<QListWidgetItem *>items =
m_lb->findItems(s, Qt::MatchFixedString|Qt::MatchCaseSensitive);
if (items.empty()) {
m_lb->insertItem(0, s);
m_lb->sortItems();
listToConf();
}
}
}
} // Namespace confgui