EditProfileDialog.cpp 48.4 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
#include <QtCore/QTimeLine>

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

44
45
#include <KFontDialog>
#include <KIcon>
46
#include <KIconDialog>
47
#include <KFileDialog>
48
#include <KUrlCompletion>
49
#include <KWindowSystem>
Rick Xing's avatar
Rick Xing committed
50
#include <KTextEdit>
51
#include <KMessageBox>
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)
Robert Knight's avatar
   
Robert Knight committed
70
    , _colorSchemeAnimationTimeLine(0)
71
    , _delayedPreviewTimer(new QTimer(this))
72
    , _colorDialog(0)
73
{
74
    setCaption(i18n("Edit Profile"));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
75
    setButtons(KDialog::Ok | KDialog::Cancel | KDialog::Apply);
76

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

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

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

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

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

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

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

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

122
123
    createTempProfile();

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

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

183
184
    _profile = profile;

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

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

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

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

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

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

230
        _pageNeedsUpdate[page] = false;
231
    }
Robert Knight's avatar
   
Robert Knight committed
232
233

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

Jekyll Wu's avatar
Jekyll Wu committed
250
        ProfileGroup::Ptr group = profile->asGroup();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
251
        if (!group || group->profiles().count() < 2) {
Jekyll Wu's avatar
Jekyll Wu committed
252
            _ui->profileNameEdit->setText(profile->name());
253
            _ui->profileNameEdit->setClearButtonShown(true);
254

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

            _ui->emptyNameWarningWidget->setVisible(false);
263
264
        }
    }
265

Jekyll Wu's avatar
Jekyll Wu committed
266
    ShellCommand command(profile->command() , profile->arguments());
Kurt Hindenburg's avatar
Kurt Hindenburg committed
267
    _ui->commandEdit->setText(command.fullCommand());
268
    KUrlCompletion* exeCompletion = new KUrlCompletion(KUrlCompletion::ExeCompletion);
269
    exeCompletion->setParent(this);
270
    exeCompletion->setDir(QString());
Kurt Hindenburg's avatar
Kurt Hindenburg committed
271
    _ui->commandEdit->setCompletionObject(exeCompletion);
272

Jekyll Wu's avatar
Jekyll Wu committed
273
    _ui->initialDirEdit->setText(profile->defaultWorkingDirectory());
274
275
    KUrlCompletion* dirCompletion = new KUrlCompletion(KUrlCompletion::DirCompletion);
    dirCompletion->setParent(this);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
276
    _ui->initialDirEdit->setCompletionObject(dirCompletion);
277
    _ui->initialDirEdit->setClearButtonShown(true);
278

Kurt Hindenburg's avatar
Kurt Hindenburg committed
279
    _ui->dirSelectButton->setIcon(KIcon("folder-open"));
Jekyll Wu's avatar
Jekyll Wu committed
280
    _ui->iconSelectButton->setIcon(KIcon(profile->icon()));
281
    _ui->startInSameDirButton->setChecked(profile->startInCurrentSessionDir());
282

283
    // window options
284
285
    _ui->showTerminalSizeHintButton->setChecked(profile->showTerminalSizeHint());
    _ui->saveGeometryOnExitButton->setChecked(profile->saveGeometryOnExit());
286
287

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
310
311
    QWeakPointer<KDialog> dialog = new KDialog(this);
    KTextEdit* edit = new KTextEdit(dialog.data());
312

313
    QStringList currentEnvironment = profile->environment();
314

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
318
319
    dialog.data()->setPlainCaption(i18n("Edit Environment"));
    dialog.data()->setMainWidget(edit);
320

Kurt Hindenburg's avatar
Kurt Hindenburg committed
321
    if (dialog.data()->exec() == QDialog::Accepted) {
322
        QStringList newEnvironment = edit->toPlainText().split('\n');
Kurt Hindenburg's avatar
Kurt Hindenburg committed
323
        updateTempProfileProperty(Profile::Environment, newEnvironment);
324
325
    }

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

Jekyll Wu's avatar
Jekyll Wu committed
339
    // tab monitoring
Jekyll Wu's avatar
Jekyll Wu committed
340
    const int silenceSeconds = profile->silenceSeconds();
Jekyll Wu's avatar
Jekyll Wu committed
341
    _ui->silenceSecondsSpinner->setValue(silenceSeconds);
342
    _ui->silenceSecondsSpinner->setSuffix(ki18ncp("Unit of time", " second", " seconds"));
Jekyll Wu's avatar
Jekyll Wu committed
343

344
345
    connect(_ui->silenceSecondsSpinner, SIGNAL(valueChanged(int)),
            this, SLOT(silenceSecondsChanged(int)));
346
}
347

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

365
366
367
368
369
void EditProfileDialog::silenceSecondsChanged(int seconds)
{
    updateTempProfileProperty(Profile::SilenceSeconds, seconds);
}

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
399
400
    updateTempProfileProperty(Profile::Command, shellCommand.command());
    updateTempProfileProperty(Profile::Arguments, shellCommand.arguments());
401
402
403
}
void EditProfileDialog::selectInitialDir()
{
404
    const KUrl url = KFileDialog::getExistingDirectoryUrl(_ui->initialDirEdit->text(),
Kurt Hindenburg's avatar
Kurt Hindenburg committed
405
406
                     this,
                     i18n("Select Initial Directory"));
407

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
416
    _colorSchemeAnimationTimeLine = new QTimeLine(500 , this);
Robert Knight's avatar
   
Robert Knight committed
417
    delegate->setEntryTimeLine(_colorSchemeAnimationTimeLine);
418

419
    connect(_colorSchemeAnimationTimeLine, SIGNAL(valueChanged(qreal)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
420
            SLOT(colorSchemeAnimationUpdate()));
421

Robert Knight's avatar
   
Robert Knight committed
422
    _ui->transparencyWarningWidget->setVisible(false);
423
424
425
    _ui->transparencyWarningWidget->setWordWrap(true);
    _ui->transparencyWarningWidget->setCloseButtonVisible(false);
    _ui->transparencyWarningWidget->setMessageType(KMessageWidget::Warning);
426

Robert Knight's avatar
   
Robert Knight committed
427
428
429
    _ui->editColorSchemeButton->setEnabled(false);
    _ui->removeColorSchemeButton->setEnabled(false);

430
431
    // setup color list
    updateColorSchemeList(true);
432

433
434
    _ui->colorSchemeList->setMouseTracking(true);
    _ui->colorSchemeList->installEventFilter(this);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
435
    _ui->colorSchemeList->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
436

437
    connect(_ui->colorSchemeList->selectionModel(),
Kurt Hindenburg's avatar
Kurt Hindenburg committed
438
            SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
439
440
            this, SLOT(colorSchemeSelected()));
    connect(_ui->colorSchemeList, SIGNAL(entered(QModelIndex)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
441
            SLOT(previewColorScheme(QModelIndex)));
442

Robert Knight's avatar
   
Robert Knight committed
443
    updateColorSchemeButtons();
444

445
    connect(_ui->editColorSchemeButton, SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
446
            SLOT(editColorScheme()));
447
    connect(_ui->removeColorSchemeButton, SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
448
            SLOT(removeColorScheme()));
449
    connect(_ui->newColorSchemeButton, SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
450
            SLOT(newColorScheme()));
451

452
    // setup font preview
Jekyll Wu's avatar
Jekyll Wu committed
453
    const bool antialias = profile->antiAliasFonts();
454

455
456
    QFont profileFont = profile->font();
    profileFont.setStyleStrategy(antialias ? QFont::PreferAntialias : QFont::NoAntialias);
457

458
    _ui->fontPreviewLabel->installEventFilter(this);
459
    _ui->fontPreviewLabel->setFont(profileFont);
460
    setFontInputValue(profileFont);
461

462
463
    connect(_ui->fontSizeInput, SIGNAL(valueChanged(double)), this,
            SLOT(setFontSize(double)));
464
    connect(_ui->selectFontButton, SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
465
            SLOT(showFontDialog()));
466

Kurt Hindenburg's avatar
Kurt Hindenburg committed
467
    // setup font smoothing
468
    _ui->antialiasTextButton->setChecked(antialias);
469
    connect(_ui->antialiasTextButton, SIGNAL(toggled(bool)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
470
            SLOT(setAntialiasText(bool)));
471

Jekyll Wu's avatar
Jekyll Wu committed
472
    _ui->boldIntenseButton->setChecked(profile->boldIntense());
473
    connect(_ui->boldIntenseButton, SIGNAL(toggled(bool)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
474
            SLOT(setBoldIntense(bool)));
475
476
477
    _ui->enableMouseWheelZoomButton->setChecked(profile->mouseWheelZoomEnabled());
    connect(_ui->enableMouseWheelZoomButton, SIGNAL(toggled(bool)), this,
            SLOT(toggleMouseWheelZoom(bool)));
478
479
480
}
void EditProfileDialog::setAntialiasText(bool enable)
{
481
482
    QFont profileFont = _ui->fontPreviewLabel->font();
    profileFont.setStyleStrategy(enable ? QFont::PreferAntialias : QFont::NoAntialias);
483

484
    // update preview to reflect text smoothing state
485
    fontSelected(profileFont);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
486
    updateTempProfileProperty(Profile::AntiAliasFonts, enable);
487
}
488
489
void EditProfileDialog::setBoldIntense(bool enable)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
490
491
    preview(Profile::BoldIntense, enable);
    updateTempProfileProperty(Profile::BoldIntense, enable);
492
}
493
494
495
496
void EditProfileDialog::toggleMouseWheelZoom(bool enable)
{
    updateTempProfileProperty(Profile::MouseWheelZoomEnabled, enable);
}
497
498
499
500
void EditProfileDialog::colorSchemeAnimationUpdate()
{
    QAbstractItemModel* model = _ui->colorSchemeList->model();

Kurt Hindenburg's avatar
Kurt Hindenburg committed
501
502
    for (int i = model->rowCount() ; i >= 0 ; i--)
        _ui->colorSchemeList->update(model->index(i, 0));
503
}
504
void EditProfileDialog::updateColorSchemeList(bool selectCurrentScheme)
505
{
Robert Knight's avatar
   
Robert Knight committed
506
507
    if (!_ui->colorSchemeList->model())
        _ui->colorSchemeList->setModel(new QStandardItemModel(this));
508

509
    const QString& name = lookupProfile()->colorScheme();
510
511
    const ColorScheme* currentScheme = ColorSchemeManager::instance()->findColorScheme(name);

Robert Knight's avatar
   
Robert Knight committed
512
513
514
    QStandardItemModel* model = qobject_cast<QStandardItemModel*>(_ui->colorSchemeList->model());

    Q_ASSERT(model);
515

Robert Knight's avatar
   
Robert Knight committed
516
517
    model->clear();

518
519
    QStandardItem* selectedItem = 0;

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
522
    foreach(const ColorScheme* scheme, schemeList) {
523
524
        QStandardItem* item = new QStandardItem(scheme->description());
        item->setData(QVariant::fromValue(scheme) ,  Qt::UserRole + 1);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
525
        item->setFlags(item->flags());
526

527
        if (currentScheme == scheme)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
528
            selectedItem = item;
Robert Knight's avatar
   
Robert Knight committed
529

530
531
532
        model->appendRow(item);
    }

533
534
    model->sort(0);

Kurt Hindenburg's avatar
Kurt Hindenburg committed
535
    if (selectCurrentScheme && selectedItem) {
536
        _ui->colorSchemeList->updateGeometry();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
537
538
        _ui->colorSchemeList->selectionModel()->setCurrentIndex(selectedItem->index() ,
                QItemSelectionModel::Select);
Robert Knight's avatar
   
Robert Knight committed
539
540
541

        // update transparency warning label
        updateTransparencyWarning();
542
    }
543
}
544
void EditProfileDialog::updateKeyBindingsList(bool selectCurrentTranslator)
545
{
Robert Knight's avatar
   
Robert Knight committed
546
547
    if (!_ui->keyBindingList->model())
        _ui->keyBindingList->setModel(new QStandardItemModel(this));
548

549
    const QString& name = lookupProfile()->keyBindings();
550

551
    KeyboardTranslatorManager* keyManager = KeyboardTranslatorManager::instance();
552
    const KeyboardTranslator* currentTranslator = keyManager->findTranslator(name);
553

Robert Knight's avatar
   
Robert Knight committed
554
555
556
557
558
    QStandardItemModel* model = qobject_cast<QStandardItemModel*>(_ui->keyBindingList->model());

    Q_ASSERT(model);

    model->clear();
559

560
561
    QStandardItem* selectedItem = 0;

Jekyll Wu's avatar
Jekyll Wu committed
562
    QStringList translatorNames = keyManager->allTranslators();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
563
    foreach(const QString& translatorName, translatorNames) {
564
        const KeyboardTranslator* translator = keyManager->findTranslator(translatorName);
565
566

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
571
        if (translator == currentTranslator)
572
            selectedItem = item;
573
574
575
576

        model->appendRow(item);
    }

577
    model->sort(0);
578

Kurt Hindenburg's avatar
Kurt Hindenburg committed
579
580
581
    if (selectCurrentTranslator && selectedItem) {
        _ui->keyBindingList->selectionModel()->setCurrentIndex(selectedItem->index() ,
                QItemSelectionModel::Select);
582
    }
583
}
584
bool EditProfileDialog::eventFilter(QObject* watched , QEvent* aEvent)
585
{
586
    if (watched == _ui->colorSchemeList && aEvent->type() == QEvent::Leave) {
Kurt Hindenburg's avatar
Kurt Hindenburg committed
587
588
        if (_tempProfile->isPropertySet(Profile::ColorScheme))
            preview(Profile::ColorScheme, _tempProfile->colorScheme());
589
590
        else
            unpreview(Profile::ColorScheme);
591
    }
592
    if (watched == _ui->fontPreviewLabel && aEvent->type() == QEvent::FontChange) {
593
        const QFont& labelFont = _ui->fontPreviewLabel->font();
594
        _ui->fontPreviewLabel->setText(i18n("%1", labelFont.family()));
595
    }
596

597
    return KDialog::eventFilter(watched, aEvent);
598
}
599
void EditProfileDialog::unpreviewAll()
600
{
601
602
603
    _delayedPreviewTimer->stop();
    _delayedPreviewProperties.clear();

Kurt Hindenburg's avatar
Kurt Hindenburg committed
604
605
606
    QHash<Profile::Property, QVariant> map;
    QHashIterator<int, QVariant> iter(_previewedProperties);
    while (iter.hasNext()) {
607
        iter.next();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
608
        map.insert((Profile::Property)iter.key(), iter.value());
609
610
611
    }

    // undo any preview changes
Kurt Hindenburg's avatar
Kurt Hindenburg committed
612
    if (!map.isEmpty())
613
        ProfileManager::instance()->changeProfile(_profile, map, false);
614
}
615
void EditProfileDialog::unpreview(int aProperty)
616
{
617
    _delayedPreviewProperties.remove(aProperty);
618

619
    if (!_previewedProperties.contains(aProperty))
620
        return;
621

Kurt Hindenburg's avatar
Kurt Hindenburg committed
622
    QHash<Profile::Property, QVariant> map;
623
    map.insert((Profile::Property)aProperty, _previewedProperties[aProperty]);
624
    ProfileManager::instance()->changeProfile(_profile, map, false);
625

626
    _previewedProperties.remove(aProperty);
627
}
628
void EditProfileDialog::delayedPreview(int aProperty , const QVariant& value)
629
{
630
    _delayedPreviewProperties.insert(aProperty, value);
631
632

    _delayedPreviewTimer->stop();
633
    _delayedPreviewTimer->start(300);
634
635
636
}
void EditProfileDialog::delayedPreviewActivate()
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
637
    Q_ASSERT(qobject_cast<QTimer*>(sender()));
638

Kurt Hindenburg's avatar
Kurt Hindenburg committed
639
640
    QMutableHashIterator<int, QVariant> iter(_delayedPreviewProperties);
    if (iter.hasNext()) {
641
        iter.next();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
642
        preview(iter.key(), iter.value());
643
644
    }
}
645
void EditProfileDialog::preview(int aProperty , const QVariant& value)
646
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
647
    QHash<Profile::Property, QVariant> map;
648
    map.insert((Profile::Property)aProperty, value);
649

650
    _delayedPreviewProperties.remove(aProperty);
651

652
    const Profile::Ptr original = lookupProfile();
653

654
655
656
657
658
659
    // 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 &&
660
            original->property<QVariant>((Profile::Property)aProperty).isNull())
661
662
        return;

663
664
    if (!_previewedProperties.contains(aProperty)) {
        _previewedProperties.insert(aProperty , original->property<QVariant>((Profile::Property)aProperty));
665
    }
666
667

    // temporary change to color scheme
668
    ProfileManager::instance()->changeProfile(_profile , map , false);
669
670
671
}
void EditProfileDialog::previewColorScheme(const QModelIndex& index)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
672
    const QString& name = index.data(Qt::UserRole + 1).value<const ColorScheme*>()->name();
673

Kurt Hindenburg's avatar
Kurt Hindenburg committed
674
    delayedPreview(Profile::ColorScheme , name);
675
}
676
677
void EditProfileDialog::removeColorScheme()
{
678
679
    QModelIndexList selected = _ui->colorSchemeList->selectionModel()->selectedIndexes();

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
683
        if (ColorSchemeManager::instance()->deleteColorScheme(name))
684
            _ui->colorSchemeList->model()->removeRow(selected.first().row());
685
    }
686
}
687
void EditProfileDialog::showColorSchemeEditor(bool isNewScheme)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
688
{
689
    // Finding selected ColorScheme
690
    QModelIndexList selected = _ui->colorSchemeList->selectionModel()->selectedIndexes();
691
    QAbstractItemModel* model = _ui->colorSchemeList->model();
Robert Knight's avatar
   
Robert Knight committed
692
    const ColorScheme* colors = 0;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
693
694
    if (!selected.isEmpty())
        colors = model->data(selected.first(), Qt::UserRole + 1).value<const ColorScheme*>();
695
    else
Robert Knight's avatar
   
Robert Knight committed
696
        colors = ColorSchemeManager::instance()->defaultColorScheme();
697

Robert Knight's avatar
   
Robert Knight committed
698
    Q_ASSERT(colors);
699

700
    // Setting up ColorSchemeEditor ui
701
702
703
704
705
    // close any running ColorSchemeEditor
    if (_colorDialog) {
        closeColorSchemeEditor();
    }
    _colorDialog = new ColorSchemeEditor(this);
706

707
708
    connect(_colorDialog, SIGNAL(colorSchemeSaveRequested(ColorScheme, bool)),
            this, SLOT(saveColorScheme(ColorScheme,bool)));
709
    _colorDialog->setup(colors, isNewScheme);
710

711
712
713
714
    // Hide the parent dialog while ColorScheme is open
    this->hide();
    connect(_colorDialog, SIGNAL(finished()), this, SLOT(show()));

715
716
717
718
719
720
721
    _colorDialog->show();
}
void EditProfileDialog::closeColorSchemeEditor()
{
    if (_colorDialog) {
        _colorDialog->close();
        delete _colorDialog;
722
723
724
725
    }
}
void EditProfileDialog::newColorScheme()
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
726
    showColorSchemeEditor(true);
727
728
729
730
731
}
void EditProfileDialog::editColorScheme()
{
    showColorSchemeEditor(false);
}
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
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());
}