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
#include <QtGui/QBrush>
#include <QtGui/QPainter>
29
#include <QStandardItem>
30
#include <QtCore/QTextCodec>
31
32
#include <QtGui/QLinearGradient>
#include <QtGui/QRadialGradient>
33
#include <QtCore/QTimer>
34

35
// KDE
36
37
38
39
#include <kdeversion.h>
#if KDE_IS_VERSION(4, 9, 1)
#include <KCodecAction>
#else
40
#include <kcodecaction.h>
41
42
#endif

43
44
#include <KFontDialog>
#include <KIcon>
45
#include <KIconDialog>
46
#include <KFileDialog>
47
#include <KUrlCompletion>
48
#include <KWindowSystem>
Rick Xing's avatar
Rick Xing committed
49
#include <KTextEdit>
50
#include <KMessageBox>
Alex Richardson's avatar
Alex Richardson committed
51
#include <KUrl>
52
53
54

// Konsole
#include "ColorScheme.h"
55
#include "ColorSchemeManager.h"
56
#include "ColorSchemeEditor.h"
57
#include "ui_EditProfileDialog.h"
58
59
#include "KeyBindingEditor.h"
#include "KeyboardTranslator.h"
60
#include "KeyboardTranslatorManager.h"
61
#include "ProfileManager.h"
62
#include "ShellCommand.h"
63
#include "WindowSystemInfo.h"
64
#include "Enumeration.h"
65

66
67
using namespace Konsole;

68
69
EditProfileDialog::EditProfileDialog(QWidget* aParent)
    : KDialog(aParent)
70
    , _delayedPreviewTimer(new QTimer(this))
71
    , _colorDialog(0)
72
{
73
    setCaption(i18n("Edit Profile"));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
74
    setButtons(KDialog::Ok | KDialog::Cancel | KDialog::Apply);
75

76
77
78
    // disable the apply button , since no modification has been made
    enableButtonApply(false);

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

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

83
    _ui = new Ui::EditProfileDialog();
84
    _ui->setupUi(mainWidget());
85

86
87
88
89
90
91
92
93
94
95
    // 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
96
    _pageNeedsUpdate.resize(_ui->tabWidget->count());
97
    connect(_ui->tabWidget, SIGNAL(currentChanged(int)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
98
            SLOT(preparePage(int)));
99

100
    createTempProfile();
101
}
102
EditProfileDialog::~EditProfileDialog()
103
104
{
    delete _ui;
105
}
106
void EditProfileDialog::save()
107
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
108
    if (_tempProfile->isEmpty())
109
        return;
110

111
    ProfileManager::instance()->changeProfile(_profile, _tempProfile->setProperties());
112
113
114

    // ensure that these settings are not undone by a call
    // to unpreview()
Kurt Hindenburg's avatar
Kurt Hindenburg committed
115
116
    QHashIterator<Profile::Property, QVariant> iter(_tempProfile->setProperties());
    while (iter.hasNext()) {
117
118
119
        iter.next();
        _previewedProperties.remove(iter.key());
    }
120

121
122
    createTempProfile();

123
    enableButtonApply(false);
124
125
126
127
128
}
void EditProfileDialog::reject()
{
    unpreviewAll();
    KDialog::reject();
129
130
131
}
void EditProfileDialog::accept()
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
132
133
    Q_ASSERT(_profile);
    Q_ASSERT(_tempProfile);
134

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

182
183
    _profile = profile;

184
    // update caption
185
    updateCaption(profile);
186

187
188
189
190
191
    // 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
192
    preparePage(_ui->tabWidget->currentIndex());
193

Kurt Hindenburg's avatar
Kurt Hindenburg committed
194
    if (_tempProfile) {
195
        createTempProfile();
196
    }
197
}
198
const Profile::Ptr EditProfileDialog::lookupProfile() const
199
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
200
    return _profile;
201
}
Robert Knight's avatar
   
Robert Knight committed
202
void EditProfileDialog::preparePage(int page)
203
{
Jekyll Wu's avatar
Jekyll Wu committed
204
    const Profile::Ptr profile = lookupProfile();
205

Kurt Hindenburg's avatar
Kurt Hindenburg committed
206
    Q_ASSERT(_pageNeedsUpdate.count() > page);
Jekyll Wu's avatar
Jekyll Wu committed
207
    Q_ASSERT(profile);
208

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

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

229
        _pageNeedsUpdate[page] = false;
230
    }
Robert Knight's avatar
   
Robert Knight committed
231
232
233
234
}
void EditProfileDialog::selectProfileName()
{
    _ui->profileNameEdit->setFocus();
235
    _ui->profileNameEdit->selectAll();
236
}
Jekyll Wu's avatar
Jekyll Wu committed
237
void EditProfileDialog::setupGeneralPage(const Profile::Ptr profile)
238
{
239
    // basic profile options
240
    {
241
242
243
244
        _ui->emptyNameWarningWidget->setWordWrap(false);
        _ui->emptyNameWarningWidget->setCloseButtonVisible(false);
        _ui->emptyNameWarningWidget->setMessageType(KMessageWidget::Warning);

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

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

            _ui->emptyNameWarningWidget->setVisible(false);
258
259
        }
    }
260

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

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

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

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

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

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

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

308
    QStringList currentEnvironment = profile->environment();
309

Kurt Hindenburg's avatar
Kurt Hindenburg committed
310
    edit->setPlainText(currentEnvironment.join("\n"));
311
    edit->setToolTip(i18nc("@info:tooltip", "One environment variable per line"));
312

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
321
    delete dialog.data();
322
}
Jekyll Wu's avatar
Jekyll Wu committed
323
void EditProfileDialog::setupTabsPage(const Profile::Ptr profile)
324
{
325
    // tab title format
326
327
328
329
330
331
332
    _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)));
333

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

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

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

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

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

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

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

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

Robert Knight's avatar
   
Robert Knight committed
411
    _ui->transparencyWarningWidget->setVisible(false);
412
413
414
    _ui->transparencyWarningWidget->setWordWrap(true);
    _ui->transparencyWarningWidget->setCloseButtonVisible(false);
    _ui->transparencyWarningWidget->setMessageType(KMessageWidget::Warning);
415

Robert Knight's avatar
   
Robert Knight committed
416
417
418
    _ui->editColorSchemeButton->setEnabled(false);
    _ui->removeColorSchemeButton->setEnabled(false);

419
420
    // setup color list
    updateColorSchemeList(true);
421

422
423
    _ui->colorSchemeList->setMouseTracking(true);
    _ui->colorSchemeList->installEventFilter(this);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
424
    _ui->colorSchemeList->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
425

426
    connect(_ui->colorSchemeList->selectionModel(),
Kurt Hindenburg's avatar
Kurt Hindenburg committed
427
            SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
428
429
            this, SLOT(colorSchemeSelected()));
    connect(_ui->colorSchemeList, SIGNAL(entered(QModelIndex)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
430
            SLOT(previewColorScheme(QModelIndex)));
431

Robert Knight's avatar
   
Robert Knight committed
432
    updateColorSchemeButtons();
433

434
    connect(_ui->editColorSchemeButton, SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
435
            SLOT(editColorScheme()));
436
    connect(_ui->removeColorSchemeButton, SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
437
            SLOT(removeColorScheme()));
438
    connect(_ui->newColorSchemeButton, SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
439
            SLOT(newColorScheme()));
440

441
    // setup font preview
Jekyll Wu's avatar
Jekyll Wu committed
442
    const bool antialias = profile->antiAliasFonts();
443

444
445
    QFont profileFont = profile->font();
    profileFont.setStyleStrategy(antialias ? QFont::PreferAntialias : QFont::NoAntialias);
446

447
    _ui->fontPreviewLabel->installEventFilter(this);
448
    _ui->fontPreviewLabel->setFont(profileFont);
449
    setFontInputValue(profileFont);
450

451
452
    connect(_ui->fontSizeInput, SIGNAL(valueChanged(double)), this,
            SLOT(setFontSize(double)));
453
    connect(_ui->selectFontButton, SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
454
            SLOT(showFontDialog()));
455

Kurt Hindenburg's avatar
Kurt Hindenburg committed
456
    // setup font smoothing
457
    _ui->antialiasTextButton->setChecked(antialias);
458
    connect(_ui->antialiasTextButton, SIGNAL(toggled(bool)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
459
            SLOT(setAntialiasText(bool)));
460

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

473
    // update preview to reflect text smoothing state
474
    fontSelected(profileFont);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
475
    updateTempProfileProperty(Profile::AntiAliasFonts, enable);
476
}
477
478
void EditProfileDialog::setBoldIntense(bool enable)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
479
480
    preview(Profile::BoldIntense, enable);
    updateTempProfileProperty(Profile::BoldIntense, enable);
481
}
482
483
484
485
void EditProfileDialog::toggleMouseWheelZoom(bool enable)
{
    updateTempProfileProperty(Profile::MouseWheelZoomEnabled, enable);
}
486
void EditProfileDialog::updateColorSchemeList(bool selectCurrentScheme)
487
{
Robert Knight's avatar
   
Robert Knight committed
488
489
    if (!_ui->colorSchemeList->model())
        _ui->colorSchemeList->setModel(new QStandardItemModel(this));
490

491
    const QString& name = lookupProfile()->colorScheme();
492
493
    const ColorScheme* currentScheme = ColorSchemeManager::instance()->findColorScheme(name);

Robert Knight's avatar
   
Robert Knight committed
494
495
496
    QStandardItemModel* model = qobject_cast<QStandardItemModel*>(_ui->colorSchemeList->model());

    Q_ASSERT(model);
497

Robert Knight's avatar
   
Robert Knight committed
498
499
    model->clear();

500
501
    QStandardItem* selectedItem = 0;

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
504
    foreach(const ColorScheme* scheme, schemeList) {
505
506
        QStandardItem* item = new QStandardItem(scheme->description());
        item->setData(QVariant::fromValue(scheme) ,  Qt::UserRole + 1);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
507
        item->setFlags(item->flags());
508

509
        if (currentScheme == scheme)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
510
            selectedItem = item;
Robert Knight's avatar
   
Robert Knight committed
511

512
513
514
        model->appendRow(item);
    }

515
516
    model->sort(0);

Kurt Hindenburg's avatar
Kurt Hindenburg committed
517
    if (selectCurrentScheme && selectedItem) {
518
        _ui->colorSchemeList->updateGeometry();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
519
520
        _ui->colorSchemeList->selectionModel()->setCurrentIndex(selectedItem->index() ,
                QItemSelectionModel::Select);
Robert Knight's avatar
   
Robert Knight committed
521
522
523

        // update transparency warning label
        updateTransparencyWarning();
524
    }
525
}
526
void EditProfileDialog::updateKeyBindingsList(bool selectCurrentTranslator)
527
{
Robert Knight's avatar
   
Robert Knight committed
528
529
    if (!_ui->keyBindingList->model())
        _ui->keyBindingList->setModel(new QStandardItemModel(this));
530

531
    const QString& name = lookupProfile()->keyBindings();
532

533
    KeyboardTranslatorManager* keyManager = KeyboardTranslatorManager::instance();
534
    const KeyboardTranslator* currentTranslator = keyManager->findTranslator(name);
535

Robert Knight's avatar
   
Robert Knight committed
536
537
538
539
540
    QStandardItemModel* model = qobject_cast<QStandardItemModel*>(_ui->keyBindingList->model());

    Q_ASSERT(model);

    model->clear();
541

542
543
    QStandardItem* selectedItem = 0;

Jekyll Wu's avatar
Jekyll Wu committed
544
    QStringList translatorNames = keyManager->allTranslators();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
545
    foreach(const QString& translatorName, translatorNames) {
546
        const KeyboardTranslator* translator = keyManager->findTranslator(translatorName);
547
548

        QStandardItem* item = new QStandardItem(translator->description());
549
        item->setEditable(false);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
550
551
        item->setData(QVariant::fromValue(translator), Qt::UserRole + 1);
        item->setIcon(KIcon("preferences-desktop-keyboard"));
552

Kurt Hindenburg's avatar
Kurt Hindenburg committed
553
        if (translator == currentTranslator)
554
            selectedItem = item;
555
556
557
558

        model->appendRow(item);
    }

559
    model->sort(0);
560

Kurt Hindenburg's avatar
Kurt Hindenburg committed
561
562
563
    if (selectCurrentTranslator && selectedItem) {
        _ui->keyBindingList->selectionModel()->setCurrentIndex(selectedItem->index() ,
                QItemSelectionModel::Select);
564
    }
565
}
566
bool EditProfileDialog::eventFilter(QObject* watched , QEvent* aEvent)
567
{
568
    if (watched == _ui->colorSchemeList && aEvent->type() == QEvent::Leave) {
Kurt Hindenburg's avatar
Kurt Hindenburg committed
569
570
        if (_tempProfile->isPropertySet(Profile::ColorScheme))
            preview(Profile::ColorScheme, _tempProfile->colorScheme());
571
572
        else
            unpreview(Profile::ColorScheme);
573
    }
574
    if (watched == _ui->fontPreviewLabel && aEvent->type() == QEvent::FontChange) {
575
        const QFont& labelFont = _ui->fontPreviewLabel->font();
576
        _ui->fontPreviewLabel->setText(i18n("%1", labelFont.family()));
577
    }
578

579
    return KDialog::eventFilter(watched, aEvent);
580
}
581
void EditProfileDialog::unpreviewAll()
582
{
583
584
585
    _delayedPreviewTimer->stop();
    _delayedPreviewProperties.clear();

Kurt Hindenburg's avatar
Kurt Hindenburg committed
586
587
588
    QHash<Profile::Property, QVariant> map;
    QHashIterator<int, QVariant> iter(_previewedProperties);
    while (iter.hasNext()) {
589
        iter.next();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
590
        map.insert((Profile::Property)iter.key(), iter.value());
591
592
593
    }

    // undo any preview changes
Kurt Hindenburg's avatar
Kurt Hindenburg committed
594
    if (!map.isEmpty())
595
        ProfileManager::instance()->changeProfile(_profile, map, false);
596
}
597
void EditProfileDialog::unpreview(int aProperty)
598
{
599
    _delayedPreviewProperties.remove(aProperty);
600

601
    if (!_previewedProperties.contains(aProperty))
602
        return;
603

Kurt Hindenburg's avatar
Kurt Hindenburg committed
604
    QHash<Profile::Property, QVariant> map;
605
    map.insert((Profile::Property)aProperty, _previewedProperties[aProperty]);
606
    ProfileManager::instance()->changeProfile(_profile, map, false);
607

608
    _previewedProperties.remove(aProperty);
609
}
610
void EditProfileDialog::delayedPreview(int aProperty , const QVariant& value)
611
{
612
    _delayedPreviewProperties.insert(aProperty, value);
613
614

    _delayedPreviewTimer->stop();
615
    _delayedPreviewTimer->start(300);
616
617
618
}
void EditProfileDialog::delayedPreviewActivate()
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
619
    Q_ASSERT(qobject_cast<QTimer*>(sender()));
620

Kurt Hindenburg's avatar
Kurt Hindenburg committed
621
622
    QMutableHashIterator<int, QVariant> iter(_delayedPreviewProperties);
    if (iter.hasNext()) {
623
        iter.next();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
624
        preview(iter.key(), iter.value());
625
626
    }
}
627
void EditProfileDialog::preview(int aProperty , const QVariant& value)
628
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
629
    QHash<Profile::Property, QVariant> map;
630
    map.insert((Profile::Property)aProperty, value);
631

632
    _delayedPreviewProperties.remove(aProperty);
633

634
    const Profile::Ptr original = lookupProfile();
635

636
637
638
639
640
641
    // 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 &&
642
            original->property<QVariant>((Profile::Property)aProperty).isNull())
643
644
        return;

645
646
    if (!_previewedProperties.contains(aProperty)) {
        _previewedProperties.insert(aProperty , original->property<QVariant>((Profile::Property)aProperty));
647
    }
648
649

    // temporary change to color scheme
650
    ProfileManager::instance()->changeProfile(_profile , map , false);
651
652
653
}
void EditProfileDialog::previewColorScheme(const QModelIndex& index)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
654
    const QString& name = index.data(Qt::UserRole + 1).value<const ColorScheme*>()->name();
655

Kurt Hindenburg's avatar
Kurt Hindenburg committed
656
    delayedPreview(Profile::ColorScheme , name);
657
}
658
659
void EditProfileDialog::removeColorScheme()
{
660
661
    QModelIndexList selected = _ui->colorSchemeList->selectionModel()->selectedIndexes();

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
665
        if (ColorSchemeManager::instance()->deleteColorScheme(name))
666
            _ui->colorSchemeList->model()->removeRow(selected.first().row());
667
    }
668
}
669
void EditProfileDialog::showColorSchemeEditor(bool isNewScheme)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
670
{
671
    // Finding selected ColorScheme
672
    QModelIndexList selected = _ui->colorSchemeList->selectionModel()->selectedIndexes();
673
    QAbstractItemModel* model = _ui->colorSchemeList->model();
Robert Knight's avatar
   
Robert Knight committed
674
    const ColorScheme* colors = 0;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
675
676
    if (!selected.isEmpty())
        colors = model->data(selected.first(), Qt::UserRole + 1).value<const ColorScheme*>();
677
    else
Robert Knight's avatar
   
Robert Knight committed
678
        colors = ColorSchemeManager::instance()->defaultColorScheme();
679

Robert Knight's avatar
   
Robert Knight committed
680
    Q_ASSERT(colors);
681

682
    // Setting up ColorSchemeEditor ui
683
684
685
686
687
    // close any running ColorSchemeEditor
    if (_colorDialog) {
        closeColorSchemeEditor();
    }
    _colorDialog = new ColorSchemeEditor(this);
688

689
    connect(_colorDialog, SIGNAL(colorSchemeSaveRequested(ColorScheme,bool)),
690
            this, SLOT(saveColorScheme(ColorScheme,bool)));
691
    _colorDialog->setup(colors, isNewScheme);
692

693
694
695
696
    // Hide the parent dialog while ColorScheme is open
    this->hide();
    connect(_colorDialog, SIGNAL(finished()), this, SLOT(show()));

697
698
699
700
701
702
703
    _colorDialog->show();
}
void EditProfileDialog::closeColorSchemeEditor()
{
    if (_colorDialog) {
        _colorDialog->close();
        delete _colorDialog;
704
705
706
707
    }
}
void EditProfileDialog::newColorScheme()
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
708
    showColorSchemeEditor(true);
709
710
711
712
713
}
void EditProfileDialog::editColorScheme()
{
    showColorSchemeEditor(false);
}
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
void EditProfileDialog::saveColorScheme(const ColorScheme& scheme, bool isNewScheme)
{
    ColorScheme* newScheme = new ColorScheme(scheme);

    // if this is a new color scheme, pick a name based on the description
    if (isNewScheme) {
        newScheme->setName(newScheme->description());
    }

    ColorSchemeManager::instance()->addColorScheme(newScheme);

    updateColorSchemeList(true);

    preview(Profile::ColorScheme, newScheme->name());
}
729
730
731
732
void EditProfileDialog::colorSchemeSelected()
{
    QModelIndexList selected = _ui->colorSchemeList->selectionModel()->selectedIndexes();

Kurt Hindenburg's avatar
Kurt Hindenburg committed
733
    if (!selected.isEmpty()) {
734
        QAbstractItemModel* model = _ui->colorSchemeList->model();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
735
        const ColorScheme* colors = model->data(selected.first(), Qt::UserRole + 1).value<const ColorScheme*>();
736

Kurt Hindenburg's avatar
Kurt Hindenburg committed
737
        updateTempProfileProperty(Profile::ColorScheme, colors->name());
738
        previewColorScheme(selected.first());
Robert Knight's avatar
   
Robert Knight committed
739
740
741

        updateTransparencyWarning();
    }
Robert Knight's avatar
   
Robert Knight committed
742
743
744
745
746

    updateColorSchemeButtons();
}
void EditProfileDialog::updateColorSchemeButtons()
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
747
748
    enableIfNonEmptySelection(_ui->editColorSchemeButton, _ui->colorSchemeList->selectionModel());
    enableIfNonEmptySelection(_ui->removeColorSchemeButton, _ui->colorSchemeList->selectionModel());