kprefsdialog.cpp 23.1 KB
Newer Older
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
/*
  This file is part of libkdepim.

  Copyright (c) 2001,2003 Cornelius Schumacher <schumacher@kde.org>
  Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
  Copyright (C) 2005,2008,2011 Allen Winter <winter@kde.org>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Library General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library 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
  Library General Public License for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; see the file COPYING.LIB.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  Boston, MA 02110-1301, USA.
*/
//krazy:excludeall=tipsandthis

#include "kprefsdialog.h"

#include <KColorButton>
#include <KComboBox>
#include <KConfigSkeleton>
#include <KDateComboBox>
#include "libkdepim_debug.h"
#include <QFontDialog>
#include <KLineEdit>
#include <KLocalizedString>
#include <KMessageBox>
#include <KTimeComboBox>
#include <KUrlRequester>
#include <QUrl>

#include <QFont>
#include <QFrame>
#include <QCheckBox>
#include <QGridLayout>
#include <QLabel>
#include <QPushButton>
#include <QRadioButton>
#include <QSpinBox>
#include <QTimeEdit>
#include <QButtonGroup>
#include <QGroupBox>

using namespace KPIM;

Laurent Montel's avatar
Laurent Montel committed
54
namespace KPrefsWidFactory {
55
56
KPrefsWid *create(KConfigSkeletonItem *item, QWidget *parent)
{
Laurent Montel's avatar
Laurent Montel committed
57
58
    KConfigSkeleton::ItemBool *boolItem
        = dynamic_cast<KConfigSkeleton::ItemBool *>(item);
59
60
61
62
    if (boolItem) {
        return new KPrefsWidBool(boolItem, parent);
    }

Laurent Montel's avatar
Laurent Montel committed
63
64
    KConfigSkeleton::ItemString *stringItem
        = dynamic_cast<KConfigSkeleton::ItemString *>(item);
65
66
67
68
    if (stringItem) {
        return new KPrefsWidString(stringItem, parent);
    }

Laurent Montel's avatar
Laurent Montel committed
69
70
    KConfigSkeleton::ItemEnum *enumItem
        = dynamic_cast<KConfigSkeleton::ItemEnum *>(item);
71
72
73
74
    if (enumItem) {
        QList<KConfigSkeleton::ItemEnum::Choice> choices = enumItem->choices();
        if (choices.isEmpty()) {
            qCritical() << "Enum has no choices.";
Laurent Montel's avatar
Laurent Montel committed
75
            return nullptr;
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
        } else {
            KPrefsWidRadios *radios = new KPrefsWidRadios(enumItem, parent);
            QList<KConfigSkeleton::ItemEnum::Choice>::ConstIterator it;
            int value = 0;
            QList<KConfigSkeleton::ItemEnum::Choice>::ConstIterator end(choices.constEnd());
            for (it = choices.constBegin(); it != end; ++it) {
                radios->addRadio(value++, (*it).label);
            }
            return radios;
        }
    }

    KConfigSkeleton::ItemInt *intItem = dynamic_cast<KConfigSkeleton::ItemInt *>(item);
    if (intItem) {
        return new KPrefsWidInt(intItem, parent);
    }

Laurent Montel's avatar
Laurent Montel committed
93
    return nullptr;
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
}
} // namespace KPrefsWidFactory

QList<QWidget *> KPrefsWid::widgets() const
{
    return QList<QWidget *>();
}

KPrefsWidBool::KPrefsWidBool(KConfigSkeleton::ItemBool *item, QWidget *parent)
    : mItem(item)
{
    mCheck = new QCheckBox(mItem->label(), parent);
    connect(mCheck, &QCheckBox::clicked, this, &KPrefsWidBool::changed);
    QString toolTip = mItem->toolTip();
    if (!toolTip.isEmpty()) {
        mCheck->setToolTip(toolTip);
    }
    QString whatsThis = mItem->whatsThis();
    if (!whatsThis.isEmpty()) {
        mCheck->setWhatsThis(whatsThis);
    }
}

void KPrefsWidBool::readConfig()
{
    mCheck->setChecked(mItem->value());
}

void KPrefsWidBool::writeConfig()
{
    mItem->setValue(mCheck->isChecked());
}

QCheckBox *KPrefsWidBool::checkBox()
{
    return mCheck;
}

QList<QWidget *> KPrefsWidBool::widgets() const
{
    QList<QWidget *> widgets;
    widgets.append(mCheck);
    return widgets;
}

KPrefsWidInt::KPrefsWidInt(KConfigSkeleton::ItemInt *item, QWidget *parent)
    : mItem(item)
{
    mLabel = new QLabel(mItem->label() + QLatin1Char(':'), parent);
    mSpin = new QSpinBox(parent);
    if (!mItem->minValue().isNull()) {
        mSpin->setMinimum(mItem->minValue().toInt());
    }
    if (!mItem->maxValue().isNull()) {
        mSpin->setMaximum(mItem->maxValue().toInt());
    }
Laurent Montel's avatar
Laurent Montel committed
150
    connect(mSpin, qOverload<int>(&QSpinBox::valueChanged), this, &KPrefsWidInt::changed);
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
    mLabel->setBuddy(mSpin);
    QString toolTip = mItem->toolTip();
    if (!toolTip.isEmpty()) {
        mLabel->setToolTip(toolTip);
        mSpin->setToolTip(toolTip);
    }
    QString whatsThis = mItem->whatsThis();
    if (!whatsThis.isEmpty()) {
        mLabel->setWhatsThis(whatsThis);
        mSpin->setWhatsThis(whatsThis);
    }
}

void KPrefsWidInt::readConfig()
{
    mSpin->setValue(mItem->value());
}

void KPrefsWidInt::writeConfig()
{
    mItem->setValue(mSpin->value());
}

QLabel *KPrefsWidInt::label() const
{
    return mLabel;
}

QSpinBox *KPrefsWidInt::spinBox()
{
    return mSpin;
}

QList<QWidget *> KPrefsWidInt::widgets() const
{
    QList<QWidget *> widgets;
    widgets.append(mLabel);
    widgets.append(mSpin);
    return widgets;
}

KPrefsWidColor::KPrefsWidColor(KConfigSkeleton::ItemColor *item, QWidget *parent)
    : mItem(item)
{
    mButton = new KColorButton(parent);
    connect(mButton, &KColorButton::changed, this, &KPrefsWidColor::changed);
    mLabel = new QLabel(mItem->label() + QLatin1Char(':'), parent);
    mLabel->setBuddy(mButton);
    QString toolTip = mItem->toolTip();
    if (!toolTip.isEmpty()) {
        mButton->setToolTip(toolTip);
    }
    QString whatsThis = mItem->whatsThis();
    if (!whatsThis.isEmpty()) {
        mButton->setWhatsThis(whatsThis);
    }
}

KPrefsWidColor::~KPrefsWidColor()
{
}

void KPrefsWidColor::readConfig()
{
    mButton->setColor(mItem->value());
}

void KPrefsWidColor::writeConfig()
{
    mItem->setValue(mButton->color());
}

QLabel *KPrefsWidColor::label()
{
    return mLabel;
}

KColorButton *KPrefsWidColor::button()
{
    return mButton;
}

Laurent Montel's avatar
Laurent Montel committed
233
KPrefsWidFont::KPrefsWidFont(KConfigSkeleton::ItemFont *item, QWidget *parent, const QString &sampleText)
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
    : mItem(item)
{
    mLabel = new QLabel(mItem->label() + QLatin1Char(':'), parent);

    mPreview = new QLabel(sampleText, parent);
    mPreview->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);

    mButton = new QPushButton(i18n("Choose..."), parent);
    connect(mButton, &QPushButton::clicked, this, &KPIM::KPrefsWidFont::selectFont);
    QString toolTip = mItem->toolTip();
    if (!toolTip.isEmpty()) {
        mPreview->setToolTip(toolTip);
        mButton->setToolTip(toolTip);
    }
    QString whatsThis = mItem->whatsThis();
    if (!whatsThis.isEmpty()) {
        mPreview->setWhatsThis(whatsThis);
        mButton->setWhatsThis(whatsThis);
    }
}

KPrefsWidFont::~KPrefsWidFont()
{
}

void KPrefsWidFont::readConfig()
{
    mPreview->setFont(mItem->value());
}

void KPrefsWidFont::writeConfig()
{
    mItem->setValue(mPreview->font());
}

QLabel *KPrefsWidFont::label()
{
    return mLabel;
}

QFrame *KPrefsWidFont::preview()
{
    return mPreview;
}

QPushButton *KPrefsWidFont::button()
{
    return mButton;
}

void KPrefsWidFont::selectFont()
{
#ifndef QT_NO_FONTDIALOG
    bool ok;
    QFont myFont = QFontDialog::getFont(&ok, mPreview->font());
    if (ok) {
        mPreview->setFont(myFont);
        Q_EMIT changed();
    }
#endif
}

KPrefsWidTime::KPrefsWidTime(KConfigSkeleton::ItemDateTime *item, QWidget *parent)
    : mItem(item)
{
    mLabel = new QLabel(mItem->label() + QLatin1Char(':'), parent);
    mTimeEdit = new KTimeComboBox(parent);
    mLabel->setBuddy(mTimeEdit);
    connect(mTimeEdit, &KTimeComboBox::timeEdited, this, &KPrefsWidTime::changed);
    connect(mTimeEdit, &KTimeComboBox::timeEntered, this, &KPrefsWidTime::changed);
    QString toolTip = mItem->toolTip();
    if (!toolTip.isEmpty()) {
        mTimeEdit->setToolTip(toolTip);
    }
    QString whatsThis = mItem->whatsThis();
    if (!whatsThis.isEmpty()) {
        mTimeEdit->setWhatsThis(whatsThis);
    }
}

void KPrefsWidTime::readConfig()
{
    mTimeEdit->setTime(mItem->value().time());
}

void KPrefsWidTime::writeConfig()
{
    // Don't overwrite the date value of the QDateTime, so we can use a
    // KPrefsWidTime and a KPrefsWidDate on the same config entry!
    QDateTime dt(mItem->value());
    dt.setTime(mTimeEdit->time());
    mItem->setValue(dt);
}

QLabel *KPrefsWidTime::label()
{
    return mLabel;
}

KTimeComboBox *KPrefsWidTime::timeEdit()
{
    return mTimeEdit;
}

Laurent Montel's avatar
Laurent Montel committed
338
KPrefsWidDuration::KPrefsWidDuration(KConfigSkeleton::ItemDateTime *item, const QString &format, QWidget *parent)
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
    : mItem(item)
{
    mLabel = new QLabel(mItem->label() + QLatin1Char(':'), parent);
    mTimeEdit = new QTimeEdit(parent);
    mLabel->setBuddy(mTimeEdit);
    if (format.isEmpty()) {
        mTimeEdit->setDisplayFormat(QStringLiteral("hh:mm:ss"));
    } else {
        mTimeEdit->setDisplayFormat(format);
    }
    mTimeEdit->setMinimumTime(QTime(0, 1));     // [1 min]
    mTimeEdit->setMaximumTime(QTime(24, 0));     // [24 hr]
    connect(mTimeEdit, &QTimeEdit::timeChanged, this, &KPrefsWidDuration::changed);
    QString toolTip = mItem->toolTip();
    if (!toolTip.isEmpty()) {
        mTimeEdit->setToolTip(toolTip);
    }
    QString whatsThis = mItem->whatsThis();
    if (!whatsThis.isEmpty()) {
        mTimeEdit->setWhatsThis(whatsThis);
    }
}

void KPrefsWidDuration::readConfig()
{
    mTimeEdit->setTime(mItem->value().time());
}

void KPrefsWidDuration::writeConfig()
{
    QDateTime dt(mItem->value());
    dt.setTime(mTimeEdit->time());
    mItem->setValue(dt);
}

QLabel *KPrefsWidDuration::label()
{
    return mLabel;
}

QTimeEdit *KPrefsWidDuration::timeEdit()
{
    return mTimeEdit;
}

KPrefsWidDate::KPrefsWidDate(KConfigSkeleton::ItemDateTime *item, QWidget *parent)
    : mItem(item)
{
    mLabel = new QLabel(mItem->label() + QLatin1Char(':'), parent);
    mDateEdit = new KDateComboBox(parent);
    mLabel->setBuddy(mDateEdit);
    connect(mDateEdit, &KDateComboBox::dateEdited, this, &KPrefsWidDate::changed);
    QString toolTip = mItem->toolTip();
    if (!toolTip.isEmpty()) {
        mDateEdit->setToolTip(toolTip);
    }
    QString whatsThis = mItem->whatsThis();
    if (!whatsThis.isEmpty()) {
        mDateEdit->setWhatsThis(whatsThis);
    }
}

void KPrefsWidDate::readConfig()
{
    if (!mItem->value().date().isValid()) {
        mItem->setValue(QDateTime::currentDateTime());
    }
Laurent Montel's avatar
Laurent Montel committed
406
407
    mDateEdit->setDate(mItem->value().date().isValid()
                       ? mItem->value().date() : QDate::currentDate());
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
}

void KPrefsWidDate::writeConfig()
{
    QDateTime dt(mItem->value());
    dt.setDate(mDateEdit->date());
    mItem->setValue(dt);
    if (!mItem->value().date().isValid()) {
        mItem->setValue(QDateTime::currentDateTime());
    }
}

QLabel *KPrefsWidDate::label()
{
    return mLabel;
}

KDateComboBox *KPrefsWidDate::dateEdit()
{
    return mDateEdit;
}

KPrefsWidRadios::KPrefsWidRadios(KConfigSkeleton::ItemEnum *item, QWidget *parent)
    : mItem(item)
{
    mBox = new QGroupBox(mItem->label(), parent);
    new QVBoxLayout(mBox);
    mGroup = new QButtonGroup(parent);
Laurent Montel's avatar
Laurent Montel committed
436
    connect(mGroup, qOverload<int>(&QButtonGroup::buttonClicked), this, &KPrefsWidRadios::changed);
437
438
439
440
441
442
}

KPrefsWidRadios::~KPrefsWidRadios()
{
}

Laurent Montel's avatar
Laurent Montel committed
443
void KPrefsWidRadios::addRadio(int value, const QString &text, const QString &toolTip, const QString &whatsThis)
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
{
    QRadioButton *r = new QRadioButton(text, mBox);
    mBox->layout()->addWidget(r);
    mGroup->addButton(r, value);
    if (!toolTip.isEmpty()) {
        r->setToolTip(toolTip);
    }
    if (!whatsThis.isEmpty()) {
        r->setWhatsThis(whatsThis);
    }
}

QGroupBox *KPrefsWidRadios::groupBox() const
{
    return mBox;
}

void KPrefsWidRadios::readConfig()
{
    if (!mGroup->button(mItem->value())) {
        return;
    }
    mGroup->button(mItem->value())->setChecked(true);
}

void KPrefsWidRadios::writeConfig()
{
    mItem->setValue(mGroup->checkedId());
}

QList<QWidget *> KPrefsWidRadios::widgets() const
{
    QList<QWidget *> w;
    w.append(mBox);
    return w;
}

KPrefsWidCombo::KPrefsWidCombo(KConfigSkeleton::ItemEnum *item, QWidget *parent)
    : mItem(item)
{
484
485
    mLabel = new QLabel(mItem->label(), parent);
    mCombo = new KComboBox(parent);
Laurent Montel's avatar
Laurent Montel committed
486
    connect(mCombo, qOverload<int>(&KComboBox::activated), this, &KPrefsWidCombo::changed);
487
488
489
490
491
492
493
494
495
496
497
    mLabel->setBuddy(mCombo);
    QString toolTip = mItem->toolTip();
    if (!toolTip.isEmpty()) {
        mLabel->setToolTip(toolTip);
        mCombo->setToolTip(toolTip);
    }
    QString whatsThis = mItem->whatsThis();
    if (!whatsThis.isEmpty()) {
        mLabel->setWhatsThis(whatsThis);
        mCombo->setWhatsThis(whatsThis);
    }
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
}

KPrefsWidCombo::~KPrefsWidCombo()
{
}

void KPrefsWidCombo::readConfig()
{
    mCombo->setCurrentIndex(mItem->value());
}

void KPrefsWidCombo::writeConfig()
{
    mItem->setValue(mCombo->currentIndex());
}

QList<QWidget *> KPrefsWidCombo::widgets() const
{
    QList<QWidget *> w;
    w.append(mCombo);
    return w;
}

KComboBox *KPrefsWidCombo::comboBox()
{
    return mCombo;
}

QLabel *KPrefsWidCombo::label() const
{
    return mLabel;
}

Laurent Montel's avatar
Laurent Montel committed
531
KPrefsWidString::KPrefsWidString(KConfigSkeleton::ItemString *item, QWidget *parent, KLineEdit::EchoMode echomode)
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
    : mItem(item)
{
    mLabel = new QLabel(mItem->label() + QLatin1Char(':'), parent);
    mEdit = new KLineEdit(parent);
    mLabel->setBuddy(mEdit);
    connect(mEdit, &KLineEdit::textChanged, this, &KPrefsWidString::changed);
    mEdit->setEchoMode(echomode);
    QString toolTip = mItem->toolTip();
    if (!toolTip.isEmpty()) {
        mEdit->setToolTip(toolTip);
    }
    QString whatsThis = mItem->whatsThis();
    if (!whatsThis.isEmpty()) {
        mEdit->setWhatsThis(whatsThis);
    }
}

KPrefsWidString::~KPrefsWidString()
{
}

void KPrefsWidString::readConfig()
{
    mEdit->setText(mItem->value());
}

void KPrefsWidString::writeConfig()
{
    mItem->setValue(mEdit->text());
}

QLabel *KPrefsWidString::label()
{
    return mLabel;
}

KLineEdit *KPrefsWidString::lineEdit()
{
    return mEdit;
}

QList<QWidget *> KPrefsWidString::widgets() const
{
    QList<QWidget *> widgets;
    widgets.append(mLabel);
    widgets.append(mEdit);
    return widgets;
}

Laurent Montel's avatar
Laurent Montel committed
581
KPrefsWidPath::KPrefsWidPath(KConfigSkeleton::ItemPath *item, QWidget *parent, const QString &filter, KFile::Modes mode)
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
    : mItem(item)
{
    mLabel = new QLabel(mItem->label() + QLatin1Char(':'), parent);
    mURLRequester = new KUrlRequester(parent);
    mLabel->setBuddy(mURLRequester);
    mURLRequester->setMode(mode);
    mURLRequester->setFilter(filter);
    connect(mURLRequester, &KUrlRequester::textChanged, this, &KPrefsWidPath::changed);
    QString toolTip = mItem->toolTip();
    if (!toolTip.isEmpty()) {
        mURLRequester->setToolTip(toolTip);
    }
    QString whatsThis = mItem->whatsThis();
    if (!whatsThis.isEmpty()) {
        mURLRequester->setWhatsThis(whatsThis);
    }
}

KPrefsWidPath::~KPrefsWidPath()
{
}

void KPrefsWidPath::readConfig()
{
    mURLRequester->setUrl(QUrl(mItem->value()));
}

void KPrefsWidPath::writeConfig()
{
    mItem->setValue(mURLRequester->url().path());
}

QLabel *KPrefsWidPath::label()
{
    return mLabel;
}

KUrlRequester *KPrefsWidPath::urlRequester()
{
    return mURLRequester;
}

QList<QWidget *> KPrefsWidPath::widgets() const
{
    QList<QWidget *> widgets;
    widgets.append(mLabel);
    widgets.append(mURLRequester);
    return widgets;
}

KPrefsWidManager::KPrefsWidManager(KConfigSkeleton *prefs)
    : mPrefs(prefs)
{
}

KPrefsWidManager::~KPrefsWidManager()
{
    qDeleteAll(mPrefsWids);
    mPrefsWids.clear();
}

void KPrefsWidManager::addWid(KPrefsWid *wid)
{
    mPrefsWids.append(wid);
}

Laurent Montel's avatar
Laurent Montel committed
648
KPrefsWidBool *KPrefsWidManager::addWidBool(KConfigSkeleton::ItemBool *item, QWidget *parent)
649
650
651
652
653
654
{
    KPrefsWidBool *w = new KPrefsWidBool(item, parent);
    addWid(w);
    return w;
}

Laurent Montel's avatar
Laurent Montel committed
655
KPrefsWidTime *KPrefsWidManager::addWidTime(KConfigSkeleton::ItemDateTime *item, QWidget *parent)
656
657
658
659
660
661
{
    KPrefsWidTime *w = new KPrefsWidTime(item, parent);
    addWid(w);
    return w;
}

Laurent Montel's avatar
Laurent Montel committed
662
KPrefsWidDuration *KPrefsWidManager::addWidDuration(KConfigSkeleton::ItemDateTime *item, const QString &format, QWidget *parent)
663
664
665
666
667
668
{
    KPrefsWidDuration *w = new KPrefsWidDuration(item, format, parent);
    addWid(w);
    return w;
}

Laurent Montel's avatar
Laurent Montel committed
669
KPrefsWidDate *KPrefsWidManager::addWidDate(KConfigSkeleton::ItemDateTime *item, QWidget *parent)
670
671
672
673
674
675
{
    KPrefsWidDate *w = new KPrefsWidDate(item, parent);
    addWid(w);
    return w;
}

Laurent Montel's avatar
Laurent Montel committed
676
KPrefsWidColor *KPrefsWidManager::addWidColor(KConfigSkeleton::ItemColor *item, QWidget *parent)
677
678
679
680
681
682
{
    KPrefsWidColor *w = new KPrefsWidColor(item, parent);
    addWid(w);
    return w;
}

Laurent Montel's avatar
Laurent Montel committed
683
KPrefsWidRadios *KPrefsWidManager::addWidRadios(KConfigSkeleton::ItemEnum *item, QWidget *parent)
684
685
686
687
688
{
    KPrefsWidRadios *w = new KPrefsWidRadios(item, parent);
    QList<KConfigSkeleton::ItemEnum::Choice2> choices;
    choices = item->choices2();
    QList<KConfigSkeleton::ItemEnum::Choice2>::ConstIterator it;
Laurent Montel's avatar
Minor    
Laurent Montel committed
689
    QList<KConfigSkeleton::ItemEnum::Choice2>::ConstIterator end(choices.constEnd());
690
    int value = 0;
Laurent Montel's avatar
Minor    
Laurent Montel committed
691
    for (it = choices.constBegin(); it != end; ++it) {
692
693
694
695
696
697
        w->addRadio(value++, (*it).label, (*it).toolTip, (*it).whatsThis);
    }
    addWid(w);
    return w;
}

Laurent Montel's avatar
Laurent Montel committed
698
KPrefsWidCombo *KPrefsWidManager::addWidCombo(KConfigSkeleton::ItemEnum *item, QWidget *parent)
699
700
701
702
{
    KPrefsWidCombo *w = new KPrefsWidCombo(item, parent);
    QList<KConfigSkeleton::ItemEnum::Choice> choices;
    choices = item->choices();
Laurent Montel's avatar
Minor    
Laurent Montel committed
703
704
    QList<KConfigSkeleton::ItemEnum::Choice>::ConstIterator end(choices.constEnd());
    for (QList<KConfigSkeleton::ItemEnum::Choice>::ConstIterator it = choices.constBegin(); it != end; ++it) {
705
706
707
708
709
710
        w->comboBox()->addItem((*it).label);
    }
    addWid(w);
    return w;
}

Laurent Montel's avatar
Laurent Montel committed
711
KPrefsWidString *KPrefsWidManager::addWidString(KConfigSkeleton::ItemString *item, QWidget *parent)
712
713
714
715
716
717
{
    KPrefsWidString *w = new KPrefsWidString(item, parent, KLineEdit::Normal);
    addWid(w);
    return w;
}

Laurent Montel's avatar
Laurent Montel committed
718
KPrefsWidPath *KPrefsWidManager::addWidPath(KConfigSkeleton::ItemPath *item, QWidget *parent, const QString &filter, KFile::Modes mode)
719
720
721
722
723
724
{
    KPrefsWidPath *w = new KPrefsWidPath(item, parent, filter, mode);
    addWid(w);
    return w;
}

Laurent Montel's avatar
Laurent Montel committed
725
KPrefsWidString *KPrefsWidManager::addWidPassword(KConfigSkeleton::ItemString *item, QWidget *parent)
726
727
728
729
730
731
{
    KPrefsWidString *w = new KPrefsWidString(item, parent, KLineEdit::Password);
    addWid(w);
    return w;
}

Laurent Montel's avatar
Laurent Montel committed
732
KPrefsWidFont *KPrefsWidManager::addWidFont(KConfigSkeleton::ItemFont *item, QWidget *parent, const QString &sampleText)
733
734
735
736
737
738
{
    KPrefsWidFont *w = new KPrefsWidFont(item, parent, sampleText);
    addWid(w);
    return w;
}

Laurent Montel's avatar
Laurent Montel committed
739
KPrefsWidInt *KPrefsWidManager::addWidInt(KConfigSkeleton::ItemInt *item, QWidget *parent)
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
{
    KPrefsWidInt *w = new KPrefsWidInt(item, parent);
    addWid(w);
    return w;
}

void KPrefsWidManager::setWidDefaults()
{
    bool tmp = mPrefs->useDefaults(true);
    readWidConfig();
    mPrefs->useDefaults(tmp);
}

void KPrefsWidManager::readWidConfig()
{
    QList<KPrefsWid *>::Iterator it;
    for (it = mPrefsWids.begin(); it != mPrefsWids.end(); ++it) {
        (*it)->readConfig();
    }
}

void KPrefsWidManager::writeWidConfig()
{
    QList<KPrefsWid *>::Iterator it;
    for (it = mPrefsWids.begin(); it != mPrefsWids.end(); ++it) {
        (*it)->writeConfig();
    }

    mPrefs->save();
}

KPrefsDialog::KPrefsDialog(KConfigSkeleton *prefs, QWidget *parent, bool modal)
Laurent Montel's avatar
Laurent Montel committed
772
773
    : KPageDialog(parent)
    , KPrefsWidManager(prefs)
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
{
    setFaceType(List);
    setWindowTitle(i18n("Preferences"));
    setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Apply | QDialogButtonBox::Cancel | QDialogButtonBox::RestoreDefaults);
    button(QDialogButtonBox::Ok)->setDefault(true);
    setModal(modal);
    connect(button(QDialogButtonBox::Ok), &QPushButton::clicked, this, &KPrefsDialog::slotOk);
    connect(button(QDialogButtonBox::Apply), &QPushButton::clicked, this, &KPrefsDialog::slotApply);
    connect(button(QDialogButtonBox::RestoreDefaults), &QPushButton::clicked, this, &KPrefsDialog::slotDefault);
    connect(button(QDialogButtonBox::Cancel), &QPushButton::clicked, this, &KPrefsDialog::reject);
}

KPrefsDialog::~KPrefsDialog()
{
}

void KPrefsDialog::autoCreate()
{
    KConfigSkeletonItem::List items = prefs()->items();

    QMap<QString, QWidget *> mGroupPages;
    QMap<QString, QGridLayout *> mGroupLayouts;
    QMap<QString, int> mCurrentRows;

    KConfigSkeletonItem::List::ConstIterator it;
    for (it = items.constBegin(); it != items.constEnd(); ++it) {
        QString group = (*it)->group();

Laurent Montel's avatar
Laurent Montel committed
802
803
        QWidget *page = nullptr;
        QGridLayout *layout = nullptr;
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
        int currentRow;
        if (!mGroupPages.contains(group)) {
            page = new QWidget(this);
            addPage(page, group);
            layout = new QGridLayout(page);
            mGroupPages.insert(group, page);
            mGroupLayouts.insert(group, layout);
            currentRow = 0;
            mCurrentRows.insert(group, currentRow);
        } else {
            page = mGroupPages[ group ];
            layout = mGroupLayouts[ group ];
            currentRow = mCurrentRows[ group ];
        }

        KPrefsWid *wid = KPrefsWidFactory::create(*it, page);

        if (wid) {
            QList<QWidget *> widgets = wid->widgets();
            if (widgets.count() == 1) {
                layout->addWidget(widgets[ 0 ], currentRow, currentRow, 0, 1);
            } else if (widgets.count() == 2) {
                layout->addWidget(widgets[ 0 ], currentRow, 0);
                layout->addWidget(widgets[ 1 ], currentRow, 1);
            } else {
                qCritical() << "More widgets than expected:" << widgets.count();
            }

            if ((*it)->isImmutable()) {
                QList<QWidget *>::Iterator it2;
                for (it2 = widgets.begin(); it2 != widgets.end(); ++it2) {
                    (*it2)->setEnabled(false);
                }
            }
            addWid(wid);
            mCurrentRows.insert(group, ++currentRow);
        }
    }

    readConfig();
}

void KPrefsDialog::setDefaults()
{
    setWidDefaults();
}

void KPrefsDialog::readConfig()
{
    readWidConfig();
    usrReadConfig();
}

void KPrefsDialog::writeConfig()
{
    writeWidConfig();
    usrWriteConfig();
    readConfig();
}

void KPrefsDialog::slotApply()
{
    writeConfig();

    Q_EMIT configChanged();
}

void KPrefsDialog::slotOk()
{
    slotApply();
    accept();
}

void KPrefsDialog::slotDefault()
{
    if (KMessageBox::warningContinueCancel(
Laurent Montel's avatar
Laurent Montel committed
880
881
882
883
884
            this,
            i18n("You are about to set all preferences to default values. "
                 "All custom modifications will be lost."),
            i18n("Setting Default Preferences"),
            KGuiItem(i18n("Reset to Defaults"))) == KMessageBox::Continue) {
885
886
887
888
        setDefaults();
    }
}

Laurent Montel's avatar
Laurent Montel committed
889
890
891
KPrefsModule::KPrefsModule(KConfigSkeleton *prefs, QWidget *parent, const QVariantList &args)
    : KCModule(parent, args)
    , KPrefsWidManager(prefs)
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
{
    Q_EMIT changed(false);
}

void KPrefsModule::addWid(KPrefsWid *wid)
{
    KPrefsWidManager::addWid(wid);
    connect(wid, &KPrefsWid::changed, this, &KPrefsModule::slotWidChanged);
}

void KPrefsModule::slotWidChanged()
{
    Q_EMIT changed(true);
}

void KPrefsModule::load()
{
    readWidConfig();
    usrReadConfig();

    Q_EMIT changed(false);
}

void KPrefsModule::save()
{
    writeWidConfig();
    usrWriteConfig();
}

void KPrefsModule::defaults()
{
    setWidDefaults();

    Q_EMIT changed(true);
}