EditProfileDialog.cpp 47.1 KB
Newer Older
1
/*
2
    Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

    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., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301  USA.
*/

20
21
22
// Own
#include "EditProfileDialog.h"

Jekyll Wu's avatar
Jekyll Wu committed
23
24
25
// Standard
#include <cmath>

26
// Qt
Dirk Mueller's avatar
Dirk Mueller committed
27
28
29
#include <QtGui/QBrush>
#include <QtGui/QPainter>
#include <QtGui/QStandardItem>
30
#include <QtCore/QTextCodec>
31
32
#include <QtGui/QLinearGradient>
#include <QtGui/QRadialGradient>
33
#include <QtCore/QTimer>
34
35
#include <QtCore/QTimeLine>

36
// KDE
37
#include <kcodecaction.h>
38
39
#include <KFontDialog>
#include <KIcon>
40
#include <KIconDialog>
41
#include <KFileDialog>
42
#include <KUrlCompletion>
43
#include <KWindowSystem>
Rick Xing's avatar
Rick Xing committed
44
#include <KTextEdit>
45
#include <KMessageBox>
46
47
48

// Konsole
#include "ColorScheme.h"
49
#include "ColorSchemeManager.h"
50
#include "ColorSchemeEditor.h"
51
#include "ui_EditProfileDialog.h"
52
53
#include "KeyBindingEditor.h"
#include "KeyboardTranslator.h"
54
#include "KeyboardTranslatorManager.h"
55
#include "ProfileManager.h"
56
#include "ShellCommand.h"
57
#include "WindowSystemInfo.h"
58
#include "Enumeration.h"
59

60
61
using namespace Konsole;

62
63
EditProfileDialog::EditProfileDialog(QWidget* aParent)
    : KDialog(aParent)
Robert Knight's avatar
   
Robert Knight committed
64
    , _colorSchemeAnimationTimeLine(0)
65
    , _delayedPreviewTimer(new QTimer(this))
66
{
67
    setCaption(i18n("Edit Profile"));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
68
    setButtons(KDialog::Ok | KDialog::Cancel | KDialog::Apply);
69

70
71
72
    // disable the apply button , since no modification has been made
    enableButtonApply(false);

73
    connect(this, SIGNAL(applyClicked()), this, SLOT(save()));
Jekyll Wu's avatar
Jekyll Wu committed
74

75
    connect(_delayedPreviewTimer, SIGNAL(timeout()), this, SLOT(delayedPreviewActivate()));
Jekyll Wu's avatar
Jekyll Wu committed
76

77
    _ui = new Ui::EditProfileDialog();
78
    _ui->setupUi(mainWidget());
79

80
81
82
83
84
85
86
87
88
89
    // there are various setupXYZPage() methods to load the items
    // for each page and update their states to match the profile
    // being edited.
    //
    // these are only called when needed ( ie. when the user clicks
    // the tab to move to that page ).
    //
    // the _pageNeedsUpdate vector keeps track of the pages that have
    // not been updated since the last profile change and will need
    // to be refreshed when the user switches to them
Kurt Hindenburg's avatar
Kurt Hindenburg committed
90
    _pageNeedsUpdate.resize(_ui->tabWidget->count());
91
    connect(_ui->tabWidget, SIGNAL(currentChanged(int)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
92
            SLOT(preparePage(int)));
93

94
    createTempProfile();
95
}
96
EditProfileDialog::~EditProfileDialog()
97
98
{
    delete _ui;
99
}
100
void EditProfileDialog::save()
101
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
102
    if (_tempProfile->isEmpty())
103
        return;
104

105
    ProfileManager::instance()->changeProfile(_profile, _tempProfile->setProperties());
106
107
108

    // ensure that these settings are not undone by a call
    // to unpreview()
Kurt Hindenburg's avatar
Kurt Hindenburg committed
109
110
    QHashIterator<Profile::Property, QVariant> iter(_tempProfile->setProperties());
    while (iter.hasNext()) {
111
112
113
        iter.next();
        _previewedProperties.remove(iter.key());
    }
114

115
116
    createTempProfile();

117
    enableButtonApply(false);
118
119
120
121
122
}
void EditProfileDialog::reject()
{
    unpreviewAll();
    KDialog::reject();
123
124
125
}
void EditProfileDialog::accept()
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
126
127
    Q_ASSERT(_profile);
    Q_ASSERT(_tempProfile);
128

Kurt Hindenburg's avatar
Kurt Hindenburg committed
129
130
131
    if ((_tempProfile->isPropertySet(Profile::Name) &&
            _tempProfile->name().isEmpty())
            || (_profile->name().isEmpty() && _tempProfile->name().isEmpty())) {
132
        KMessageBox::sorry(this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
133
134
                           i18n("<p>Each profile must have a name before it can be saved "
                                "into disk.</p>"));
135
136
        return;
    }
137
    save();
138
    unpreviewAll();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
139
    KDialog::accept();
140
}
141
QString EditProfileDialog::groupProfileNames(const ProfileGroup::Ptr group, int maxLength)
Robert Knight's avatar
   
Robert Knight committed
142
{
143
144
    QString caption;
    int count = group->profiles().count();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
145
    for (int i = 0; i < count; i++) {
146
        caption += group->profiles()[i]->name();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
147
        if (i < (count - 1)) {
148
149
            caption += ',';
            // limit caption length to prevent very long window titles
Kurt Hindenburg's avatar
Kurt Hindenburg committed
150
            if (maxLength > 0 && caption.length() > maxLength) {
151
152
153
154
155
156
157
158
159
160
                caption += "...";
                break;
            }
        }
    }
    return caption;
}
void EditProfileDialog::updateCaption(const Profile::Ptr profile)
{
    const int MAX_GROUP_CAPTION_LENGTH = 25;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
161
162
163
164
165
166
167
168
169
    ProfileGroup::Ptr group = profile->asGroup();
    if (group && group->profiles().count() > 1) {
        QString caption = groupProfileNames(group, MAX_GROUP_CAPTION_LENGTH);
        setCaption(i18np("Editing profile: %2",
                         "Editing %1 profiles: %2",
                         group->profiles().count(),
                         caption));
    } else {
        setCaption(i18n("Edit Profile \"%1\"", profile->name()));
170
    }
Robert Knight's avatar
   
Robert Knight committed
171
}
172
void EditProfileDialog::setProfile(Profile::Ptr profile)
173
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
174
    Q_ASSERT(profile);
175

176
177
    _profile = profile;

178
    // update caption
179
    updateCaption(profile);
180

181
182
183
184
185
    // mark each page of the dialog as out of date
    // and force an update of the currently visible page
    //
    // the other pages will be updated as necessary
    _pageNeedsUpdate.fill(true);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
186
    preparePage(_ui->tabWidget->currentIndex());
187

Kurt Hindenburg's avatar
Kurt Hindenburg committed
188
    if (_tempProfile) {
189
        createTempProfile();
190
    }
191
}
192
const Profile::Ptr EditProfileDialog::lookupProfile() const
193
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
194
    return _profile;
195
}
Robert Knight's avatar
   
Robert Knight committed
196
void EditProfileDialog::preparePage(int page)
197
{
Jekyll Wu's avatar
Jekyll Wu committed
198
    const Profile::Ptr profile = lookupProfile();
199

Kurt Hindenburg's avatar
Kurt Hindenburg committed
200
    Q_ASSERT(_pageNeedsUpdate.count() > page);
Jekyll Wu's avatar
Jekyll Wu committed
201
    Q_ASSERT(profile);
202

Robert Knight's avatar
   
Robert Knight committed
203
    QWidget* pageWidget = _ui->tabWidget->widget(page);
204

Kurt Hindenburg's avatar
Kurt Hindenburg committed
205
206
    if (_pageNeedsUpdate[page]) {
        if (pageWidget == _ui->generalTab)
Jekyll Wu's avatar
Jekyll Wu committed
207
            setupGeneralPage(profile);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
208
        else if (pageWidget == _ui->tabsTab)
Jekyll Wu's avatar
Jekyll Wu committed
209
            setupTabsPage(profile);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
210
        else if (pageWidget == _ui->appearanceTab)
Jekyll Wu's avatar
Jekyll Wu committed
211
            setupAppearancePage(profile);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
212
        else if (pageWidget == _ui->scrollingTab)
Jekyll Wu's avatar
Jekyll Wu committed
213
            setupScrollingPage(profile);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
214
        else if (pageWidget == _ui->keyboardTab)
Jekyll Wu's avatar
Jekyll Wu committed
215
            setupKeyboardPage(profile);
216
217
        else if (pageWidget == _ui->mouseTab)
            setupMousePage(profile);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
218
        else if (pageWidget == _ui->advancedTab)
Jekyll Wu's avatar
Jekyll Wu committed
219
            setupAdvancedPage(profile);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
220
221
        else
            Q_ASSERT(false);
222

223
        _pageNeedsUpdate[page] = false;
224
    }
Robert Knight's avatar
   
Robert Knight committed
225
226

    // start page entry animation for color schemes
Kurt Hindenburg's avatar
Kurt Hindenburg committed
227
228
    if (pageWidget == _ui->appearanceTab)
        _colorSchemeAnimationTimeLine->start();
Robert Knight's avatar
   
Robert Knight committed
229
230
231
232
}
void EditProfileDialog::selectProfileName()
{
    _ui->profileNameEdit->setFocus();
233
    _ui->profileNameEdit->selectAll();
234
}
Jekyll Wu's avatar
Jekyll Wu committed
235
void EditProfileDialog::setupGeneralPage(const Profile::Ptr profile)
236
{
237
    // basic profile options
238
    {
239
240
241
242
        _ui->emptyNameWarningWidget->setWordWrap(false);
        _ui->emptyNameWarningWidget->setCloseButtonVisible(false);
        _ui->emptyNameWarningWidget->setMessageType(KMessageWidget::Warning);

Jekyll Wu's avatar
Jekyll Wu committed
243
        ProfileGroup::Ptr group = profile->asGroup();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
244
        if (!group || group->profiles().count() < 2) {
Jekyll Wu's avatar
Jekyll Wu committed
245
            _ui->profileNameEdit->setText(profile->name());
246
            _ui->profileNameEdit->setClearButtonShown(true);
247

Jekyll Wu's avatar
Jekyll Wu committed
248
            _ui->emptyNameWarningWidget->setVisible(profile->name().isEmpty());
249
            _ui->emptyNameWarningWidget->setText(i18n("Profile name is empty."));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
250
251
        } else {
            _ui->profileNameEdit->setText(groupProfileNames(group, -1));
252
            _ui->profileNameEdit->setEnabled(false);
253
            _ui->profileNameLabel->setEnabled(false);
254
255

            _ui->emptyNameWarningWidget->setVisible(false);
256
257
        }
    }
258

Jekyll Wu's avatar
Jekyll Wu committed
259
    ShellCommand command(profile->command() , profile->arguments());
Kurt Hindenburg's avatar
Kurt Hindenburg committed
260
    _ui->commandEdit->setText(command.fullCommand());
261
    KUrlCompletion* exeCompletion = new KUrlCompletion(KUrlCompletion::ExeCompletion);
262
    exeCompletion->setParent(this);
263
    exeCompletion->setDir(QString());
Kurt Hindenburg's avatar
Kurt Hindenburg committed
264
    _ui->commandEdit->setCompletionObject(exeCompletion);
265

Jekyll Wu's avatar
Jekyll Wu committed
266
    _ui->initialDirEdit->setText(profile->defaultWorkingDirectory());
267
268
    KUrlCompletion* dirCompletion = new KUrlCompletion(KUrlCompletion::DirCompletion);
    dirCompletion->setParent(this);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
269
    _ui->initialDirEdit->setCompletionObject(dirCompletion);
270
    _ui->initialDirEdit->setClearButtonShown(true);
271

Kurt Hindenburg's avatar
Kurt Hindenburg committed
272
    _ui->dirSelectButton->setIcon(KIcon("folder-open"));
Jekyll Wu's avatar
Jekyll Wu committed
273
    _ui->iconSelectButton->setIcon(KIcon(profile->icon()));
274
    _ui->startInSameDirButton->setChecked(profile->startInCurrentSessionDir());
275

276
    // window options
277
278
    _ui->showTerminalSizeHintButton->setChecked(profile->showTerminalSizeHint());
    _ui->saveGeometryOnExitButton->setChecked(profile->saveGeometryOnExit());
279
280

    // signals and slots
281
282
283
    connect(_ui->dirSelectButton, SIGNAL(clicked()), this, SLOT(selectInitialDir()));
    connect(_ui->iconSelectButton, SIGNAL(clicked()), this, SLOT(selectIcon()));
    connect(_ui->startInSameDirButton, SIGNAL(toggled(bool)), this ,
284
            SLOT(startInSameDir(bool)));
285
    connect(_ui->profileNameEdit, SIGNAL(textChanged(QString)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
286
            SLOT(profileNameChanged(QString)));
287
    connect(_ui->initialDirEdit, SIGNAL(textChanged(QString)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
288
            SLOT(initialDirChanged(QString)));
289
    connect(_ui->commandEdit, SIGNAL(textChanged(QString)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
290
            SLOT(commandChanged(QString)));
291
    connect(_ui->environmentEditButton , SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
292
            SLOT(showEnvironmentEditor()));
Jekyll Wu's avatar
Jekyll Wu committed
293

294
    connect(_ui->saveGeometryOnExitButton, SIGNAL(toggled(bool)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
295
            SLOT(saveGeometryOnExit(bool)));
296
    connect(_ui->showTerminalSizeHintButton, SIGNAL(toggled(bool)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
297
            SLOT(showTerminalSizeHint(bool)));
298
299
300
}
void EditProfileDialog::showEnvironmentEditor()
{
Jekyll Wu's avatar
Jekyll Wu committed
301
    const Profile::Ptr profile = lookupProfile();
302

Kurt Hindenburg's avatar
Kurt Hindenburg committed
303
304
    QWeakPointer<KDialog> dialog = new KDialog(this);
    KTextEdit* edit = new KTextEdit(dialog.data());
305

306
    QStringList currentEnvironment = profile->environment();
307

Kurt Hindenburg's avatar
Kurt Hindenburg committed
308
309
    edit->setPlainText(currentEnvironment.join("\n"));
    edit->setToolTip(i18n("One environment variable per line"));
310

Kurt Hindenburg's avatar
Kurt Hindenburg committed
311
312
    dialog.data()->setPlainCaption(i18n("Edit Environment"));
    dialog.data()->setMainWidget(edit);
313

Kurt Hindenburg's avatar
Kurt Hindenburg committed
314
    if (dialog.data()->exec() == QDialog::Accepted) {
315
        QStringList newEnvironment = edit->toPlainText().split('\n');
Kurt Hindenburg's avatar
Kurt Hindenburg committed
316
        updateTempProfileProperty(Profile::Environment, newEnvironment);
317
318
    }

Kurt Hindenburg's avatar
Kurt Hindenburg committed
319
    delete dialog.data();
320
}
Jekyll Wu's avatar
Jekyll Wu committed
321
void EditProfileDialog::setupTabsPage(const Profile::Ptr profile)
322
{
323
    // tab title format
324
325
326
327
328
329
330
    _ui->renameTabWidget->setTabTitleText(profile->localTabTitleFormat());
    _ui->renameTabWidget->setRemoteTabTitleText(profile->remoteTabTitleFormat());

    connect(_ui->renameTabWidget, SIGNAL(tabTitleFormatChanged(QString)), this,
            SLOT(tabTitleFormatChanged(QString)));
    connect(_ui->renameTabWidget, SIGNAL(remoteTabTitleFormatChanged(QString)), this,
            SLOT(remoteTabTitleFormatChanged(QString)));
331

Jekyll Wu's avatar
Jekyll Wu committed
332
    // tab monitoring
Jekyll Wu's avatar
Jekyll Wu committed
333
    const int silenceSeconds = profile->silenceSeconds();
Jekyll Wu's avatar
Jekyll Wu committed
334
    _ui->silenceSecondsSpinner->setValue(silenceSeconds);
335
    _ui->silenceSecondsSpinner->setSuffix(ki18ncp("Unit of time", " second", " seconds"));
Jekyll Wu's avatar
Jekyll Wu committed
336

337
338
    connect(_ui->silenceSecondsSpinner, SIGNAL(valueChanged(int)),
            this, SLOT(silenceSecondsChanged(int)));
339
}
340

341
void EditProfileDialog::saveGeometryOnExit(bool value)
342
{
343
    updateTempProfileProperty(Profile::SaveGeometryOnExit, value);
344
}
345
void EditProfileDialog::showTerminalSizeHint(bool value)
346
{
347
    updateTempProfileProperty(Profile::ShowTerminalSizeHint, value);
348
}
349
350
void EditProfileDialog::tabTitleFormatChanged(const QString& format)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
351
    updateTempProfileProperty(Profile::LocalTabTitleFormat, format);
352
353
354
}
void EditProfileDialog::remoteTabTitleFormatChanged(const QString& format)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
355
    updateTempProfileProperty(Profile::RemoteTabTitleFormat, format);
356
357
}

358
359
360
361
362
void EditProfileDialog::silenceSecondsChanged(int seconds)
{
    updateTempProfileProperty(Profile::SilenceSeconds, seconds);
}

363
364
void EditProfileDialog::selectIcon()
{
Craig Drummond's avatar
Craig Drummond committed
365
    const QString& icon = KIconDialog::getIcon(KIconLoader::Desktop, KIconLoader::Application,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
366
367
368
369
                          false, 0, false, this);
    if (!icon.isEmpty()) {
        _ui->iconSelectButton->setIcon(KIcon(icon));
        updateTempProfileProperty(Profile::Icon, icon);
370
371
372
373
    }
}
void EditProfileDialog::profileNameChanged(const QString& text)
{
374
375
    _ui->emptyNameWarningWidget->setVisible(text.isEmpty());

Kurt Hindenburg's avatar
Kurt Hindenburg committed
376
    updateTempProfileProperty(Profile::Name, text);
377
    updateTempProfileProperty(Profile::UntranslatedName, text);
378
    updateCaption(_tempProfile);
379
}
380
381
void EditProfileDialog::startInSameDir(bool sameDir)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
382
    updateTempProfileProperty(Profile::StartInCurrentSessionDir, sameDir);
383
}
384
385
void EditProfileDialog::initialDirChanged(const QString& dir)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
386
    updateTempProfileProperty(Profile::Directory, dir);
387
388
389
}
void EditProfileDialog::commandChanged(const QString& command)
{
390
391
    ShellCommand shellCommand(command);

Kurt Hindenburg's avatar
Kurt Hindenburg committed
392
393
    updateTempProfileProperty(Profile::Command, shellCommand.command());
    updateTempProfileProperty(Profile::Arguments, shellCommand.arguments());
394
395
396
}
void EditProfileDialog::selectInitialDir()
{
397
    const KUrl url = KFileDialog::getExistingDirectoryUrl(_ui->initialDirEdit->text(),
Kurt Hindenburg's avatar
Kurt Hindenburg committed
398
399
                     this,
                     i18n("Select Initial Directory"));
400

Kurt Hindenburg's avatar
Kurt Hindenburg committed
401
    if (!url.isEmpty())
402
        _ui->initialDirEdit->setText(url.path());
403
}
Jekyll Wu's avatar
Jekyll Wu committed
404
void EditProfileDialog::setupAppearancePage(const Profile::Ptr profile)
405
{
406
    ColorSchemeViewDelegate* delegate = new ColorSchemeViewDelegate(this);
407
    _ui->colorSchemeList->setItemDelegate(delegate);
408

Kurt Hindenburg's avatar
Kurt Hindenburg committed
409
    _colorSchemeAnimationTimeLine = new QTimeLine(500 , this);
Robert Knight's avatar
   
Robert Knight committed
410
    delegate->setEntryTimeLine(_colorSchemeAnimationTimeLine);
411

412
    connect(_colorSchemeAnimationTimeLine, SIGNAL(valueChanged(qreal)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
413
            SLOT(colorSchemeAnimationUpdate()));
414

Robert Knight's avatar
   
Robert Knight committed
415
    _ui->transparencyWarningWidget->setVisible(false);
416
417
418
    _ui->transparencyWarningWidget->setWordWrap(true);
    _ui->transparencyWarningWidget->setCloseButtonVisible(false);
    _ui->transparencyWarningWidget->setMessageType(KMessageWidget::Warning);
419

Robert Knight's avatar
   
Robert Knight committed
420
421
422
    _ui->editColorSchemeButton->setEnabled(false);
    _ui->removeColorSchemeButton->setEnabled(false);

423
424
    // setup color list
    updateColorSchemeList(true);
425

426
427
    _ui->colorSchemeList->setMouseTracking(true);
    _ui->colorSchemeList->installEventFilter(this);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
428
    _ui->colorSchemeList->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
429

430
    connect(_ui->colorSchemeList->selectionModel(),
Kurt Hindenburg's avatar
Kurt Hindenburg committed
431
            SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
432
433
            this, SLOT(colorSchemeSelected()));
    connect(_ui->colorSchemeList, SIGNAL(entered(QModelIndex)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
434
            SLOT(previewColorScheme(QModelIndex)));
435

Robert Knight's avatar
   
Robert Knight committed
436
    updateColorSchemeButtons();
437

438
    connect(_ui->editColorSchemeButton, SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
439
            SLOT(editColorScheme()));
440
    connect(_ui->removeColorSchemeButton, SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
441
            SLOT(removeColorScheme()));
442
    connect(_ui->newColorSchemeButton, SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
443
            SLOT(newColorScheme()));
444

445
    // setup font preview
Jekyll Wu's avatar
Jekyll Wu committed
446
    const bool antialias = profile->antiAliasFonts();
447

448
449
    QFont profileFont = profile->font();
    profileFont.setStyleStrategy(antialias ? QFont::PreferAntialias : QFont::NoAntialias);
450

451
    _ui->fontPreviewLabel->installEventFilter(this);
452
453
454
    _ui->fontPreviewLabel->setFont(profileFont);
    setFontSliderRange(profileFont);
    setFontSliderValue(profileFont);
455

456
    connect(_ui->fontSizeSlider, SIGNAL(valueChanged(int)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
457
            SLOT(setFontSize(int)));
458
    connect(_ui->selectFontButton, SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
459
            SLOT(showFontDialog()));
460

Kurt Hindenburg's avatar
Kurt Hindenburg committed
461
    // setup font smoothing
462
    _ui->antialiasTextButton->setChecked(antialias);
463
    connect(_ui->antialiasTextButton, SIGNAL(toggled(bool)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
464
            SLOT(setAntialiasText(bool)));
465

Jekyll Wu's avatar
Jekyll Wu committed
466
    _ui->boldIntenseButton->setChecked(profile->boldIntense());
467
    connect(_ui->boldIntenseButton, SIGNAL(toggled(bool)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
468
            SLOT(setBoldIntense(bool)));
469
470
471
}
void EditProfileDialog::setAntialiasText(bool enable)
{
472
473
    QFont profileFont = _ui->fontPreviewLabel->font();
    profileFont.setStyleStrategy(enable ? QFont::PreferAntialias : QFont::NoAntialias);
474

475
    // update preview to reflect text smoothing state
476
    fontSelected(profileFont);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
477
    updateTempProfileProperty(Profile::AntiAliasFonts, enable);
478
}
479
480
void EditProfileDialog::setBoldIntense(bool enable)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
481
482
    preview(Profile::BoldIntense, enable);
    updateTempProfileProperty(Profile::BoldIntense, enable);
483
}
484
485
486
487
void EditProfileDialog::colorSchemeAnimationUpdate()
{
    QAbstractItemModel* model = _ui->colorSchemeList->model();

Kurt Hindenburg's avatar
Kurt Hindenburg committed
488
489
    for (int i = model->rowCount() ; i >= 0 ; i--)
        _ui->colorSchemeList->update(model->index(i, 0));
490
}
491
void EditProfileDialog::updateColorSchemeList(bool selectCurrentScheme)
492
{
Robert Knight's avatar
   
Robert Knight committed
493
494
    if (!_ui->colorSchemeList->model())
        _ui->colorSchemeList->setModel(new QStandardItemModel(this));
495

496
    const QString& name = lookupProfile()->colorScheme();
497
498
    const ColorScheme* currentScheme = ColorSchemeManager::instance()->findColorScheme(name);

Robert Knight's avatar
   
Robert Knight committed
499
500
501
    QStandardItemModel* model = qobject_cast<QStandardItemModel*>(_ui->colorSchemeList->model());

    Q_ASSERT(model);
502

Robert Knight's avatar
   
Robert Knight committed
503
504
    model->clear();

505
506
    QStandardItem* selectedItem = 0;

507
508
    QList<const ColorScheme*> schemeList = ColorSchemeManager::instance()->allColorSchemes();

Kurt Hindenburg's avatar
Kurt Hindenburg committed
509
    foreach(const ColorScheme* scheme, schemeList) {
510
511
        QStandardItem* item = new QStandardItem(scheme->description());
        item->setData(QVariant::fromValue(scheme) ,  Qt::UserRole + 1);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
512
        item->setFlags(item->flags());
513

514
        if (currentScheme == scheme)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
515
            selectedItem = item;
Robert Knight's avatar
   
Robert Knight committed
516

517
518
519
        model->appendRow(item);
    }

520
521
    model->sort(0);

Kurt Hindenburg's avatar
Kurt Hindenburg committed
522
    if (selectCurrentScheme && selectedItem) {
523
        _ui->colorSchemeList->updateGeometry();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
524
525
        _ui->colorSchemeList->selectionModel()->setCurrentIndex(selectedItem->index() ,
                QItemSelectionModel::Select);
Robert Knight's avatar
   
Robert Knight committed
526
527
528

        // update transparency warning label
        updateTransparencyWarning();
529
    }
530
}
531
void EditProfileDialog::updateKeyBindingsList(bool selectCurrentTranslator)
532
{
Robert Knight's avatar
   
Robert Knight committed
533
534
    if (!_ui->keyBindingList->model())
        _ui->keyBindingList->setModel(new QStandardItemModel(this));
535

536
    const QString& name = lookupProfile()->keyBindings();
537

538
    KeyboardTranslatorManager* keyManager = KeyboardTranslatorManager::instance();
539
    const KeyboardTranslator* currentTranslator = keyManager->findTranslator(name);
540

Robert Knight's avatar
   
Robert Knight committed
541
542
543
544
545
    QStandardItemModel* model = qobject_cast<QStandardItemModel*>(_ui->keyBindingList->model());

    Q_ASSERT(model);

    model->clear();
546

547
548
    QStandardItem* selectedItem = 0;

Jekyll Wu's avatar
Jekyll Wu committed
549
    QStringList translatorNames = keyManager->allTranslators();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
550
    foreach(const QString& translatorName, translatorNames) {
551
        const KeyboardTranslator* translator = keyManager->findTranslator(translatorName);
552
553

        QStandardItem* item = new QStandardItem(translator->description());
Kurt Hindenburg's avatar
Kurt Hindenburg committed
554
555
        item->setData(QVariant::fromValue(translator), Qt::UserRole + 1);
        item->setIcon(KIcon("preferences-desktop-keyboard"));
556

Kurt Hindenburg's avatar
Kurt Hindenburg committed
557
        if (translator == currentTranslator)
558
            selectedItem = item;
559
560
561
562

        model->appendRow(item);
    }

563
    model->sort(0);
564

Kurt Hindenburg's avatar
Kurt Hindenburg committed
565
566
567
    if (selectCurrentTranslator && selectedItem) {
        _ui->keyBindingList->selectionModel()->setCurrentIndex(selectedItem->index() ,
                QItemSelectionModel::Select);
568
    }
569
}
570
bool EditProfileDialog::eventFilter(QObject* watched , QEvent* aEvent)
571
{
572
    if (watched == _ui->colorSchemeList && aEvent->type() == QEvent::Leave) {
Kurt Hindenburg's avatar
Kurt Hindenburg committed
573
574
        if (_tempProfile->isPropertySet(Profile::ColorScheme))
            preview(Profile::ColorScheme, _tempProfile->colorScheme());
575
576
        else
            unpreview(Profile::ColorScheme);
577
    }
578
    if (watched == _ui->fontPreviewLabel && aEvent->type() == QEvent::FontChange) {
579
        const QFont& labelFont = _ui->fontPreviewLabel->font();
580
581
        qreal fontSizeF = labelFont.pointSizeF();
        QString fontSize  = KGlobal::locale()->formatNumber(fontSizeF, fontSizeF == floor(fontSizeF) ? 0 : 1);
582
        _ui->fontPreviewLabel->setText(i18n("%1, size %2", labelFont.family(), fontSize));
583
    }
584

585
    return KDialog::eventFilter(watched, aEvent);
586
}
587
void EditProfileDialog::unpreviewAll()
588
{
589
590
591
    _delayedPreviewTimer->stop();
    _delayedPreviewProperties.clear();

Kurt Hindenburg's avatar
Kurt Hindenburg committed
592
593
594
    QHash<Profile::Property, QVariant> map;
    QHashIterator<int, QVariant> iter(_previewedProperties);
    while (iter.hasNext()) {
595
        iter.next();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
596
        map.insert((Profile::Property)iter.key(), iter.value());
597
598
599
    }

    // undo any preview changes
Kurt Hindenburg's avatar
Kurt Hindenburg committed
600
    if (!map.isEmpty())
601
        ProfileManager::instance()->changeProfile(_profile, map, false);
602
}
603
void EditProfileDialog::unpreview(int aProperty)
604
{
605
    _delayedPreviewProperties.remove(aProperty);
606

607
    if (!_previewedProperties.contains(aProperty))
608
        return;
609

Kurt Hindenburg's avatar
Kurt Hindenburg committed
610
    QHash<Profile::Property, QVariant> map;
611
    map.insert((Profile::Property)aProperty, _previewedProperties[aProperty]);
612
    ProfileManager::instance()->changeProfile(_profile, map, false);
613

614
    _previewedProperties.remove(aProperty);
615
}
616
void EditProfileDialog::delayedPreview(int aProperty , const QVariant& value)
617
{
618
    _delayedPreviewProperties.insert(aProperty, value);
619
620

    _delayedPreviewTimer->stop();
621
    _delayedPreviewTimer->start(300);
622
623
624
}
void EditProfileDialog::delayedPreviewActivate()
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
625
    Q_ASSERT(qobject_cast<QTimer*>(sender()));
626

Kurt Hindenburg's avatar
Kurt Hindenburg committed
627
628
    QMutableHashIterator<int, QVariant> iter(_delayedPreviewProperties);
    if (iter.hasNext()) {
629
        iter.next();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
630
        preview(iter.key(), iter.value());
631
632
    }
}
633
void EditProfileDialog::preview(int aProperty , const QVariant& value)
634
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
635
    QHash<Profile::Property, QVariant> map;
636
    map.insert((Profile::Property)aProperty, value);
637

638
    _delayedPreviewProperties.remove(aProperty);
639

640
    const Profile::Ptr original = lookupProfile();
641

642
643
644
645
646
647
    // skip previews for profile groups if the profiles in the group
    // have conflicting original values for the property
    //
    // TODO - Save the original values for each profile and use to unpreview properties
    ProfileGroup::Ptr group = original->asGroup();
    if (group && group->profiles().count() > 1 &&
648
            original->property<QVariant>((Profile::Property)aProperty).isNull())
649
650
        return;

651
652
    if (!_previewedProperties.contains(aProperty)) {
        _previewedProperties.insert(aProperty , original->property<QVariant>((Profile::Property)aProperty));
653
    }
654
655

    // temporary change to color scheme
656
    ProfileManager::instance()->changeProfile(_profile , map , false);
657
658
659
}
void EditProfileDialog::previewColorScheme(const QModelIndex& index)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
660
    const QString& name = index.data(Qt::UserRole + 1).value<const ColorScheme*>()->name();
661

Kurt Hindenburg's avatar
Kurt Hindenburg committed
662
    delayedPreview(Profile::ColorScheme , name);
663
}
664
665
void EditProfileDialog::removeColorScheme()
{
666
667
    QModelIndexList selected = _ui->colorSchemeList->selectionModel()->selectedIndexes();

Kurt Hindenburg's avatar
Kurt Hindenburg committed
668
669
    if (!selected.isEmpty()) {
        const QString& name = selected.first().data(Qt::UserRole + 1).value<const ColorScheme*>()->name();
670

Kurt Hindenburg's avatar
Kurt Hindenburg committed
671
        if (ColorSchemeManager::instance()->deleteColorScheme(name))
672
            _ui->colorSchemeList->model()->removeRow(selected.first().row());
673
    }
674
}
675
void EditProfileDialog::showColorSchemeEditor(bool isNewScheme)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
676
{
677
678
    QModelIndexList selected = _ui->colorSchemeList->selectionModel()->selectedIndexes();

679
    QAbstractItemModel* model = _ui->colorSchemeList->model();
Robert Knight's avatar
   
Robert Knight committed
680
    const ColorScheme* colors = 0;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
681
682
    if (!selected.isEmpty())
        colors = model->data(selected.first(), Qt::UserRole + 1).value<const ColorScheme*>();
683
    else
Robert Knight's avatar
   
Robert Knight committed
684
        colors = ColorSchemeManager::instance()->defaultColorScheme();
685

Robert Knight's avatar
   
Robert Knight committed
686
    Q_ASSERT(colors);
687

Kurt Hindenburg's avatar
Kurt Hindenburg committed
688
    QWeakPointer<KDialog> dialog = new KDialog(this);
689

Kurt Hindenburg's avatar
Kurt Hindenburg committed
690
    if (isNewScheme)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
691
        dialog.data()->setCaption(i18n("New Color Scheme"));
692
    else
Kurt Hindenburg's avatar
Kurt Hindenburg committed
693
        dialog.data()->setCaption(i18n("Edit Color Scheme"));
694
695

    ColorSchemeEditor* editor = new ColorSchemeEditor;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
696
    dialog.data()->setMainWidget(editor);
697
698
    editor->setup(colors);

Kurt Hindenburg's avatar
Kurt Hindenburg committed
699
    if (isNewScheme)
700
        editor->setDescription(i18n("New Color Scheme"));
701

Kurt Hindenburg's avatar
Kurt Hindenburg committed
702
    if (dialog.data()->exec() == QDialog::Accepted) {
703
        ColorScheme* newScheme = new ColorScheme(*editor->colorScheme());
704

705
        // if this is a new color scheme, pick a name based on the description
Kurt Hindenburg's avatar
Kurt Hindenburg committed
706
        if (isNewScheme)
707
            newScheme->setName(newScheme->description());
708

Kurt Hindenburg's avatar
Kurt Hindenburg committed
709
        ColorSchemeManager::instance()->addColorScheme(newScheme);
710

Robert Knight's avatar
   
Robert Knight committed
711
        updateColorSchemeList(true);
712

Kurt Hindenburg's avatar
Kurt Hindenburg committed
713
        preview(Profile::ColorScheme, newScheme->name());
714
    }
Kurt Hindenburg's avatar
Kurt Hindenburg committed
715
    delete dialog.data();