EditProfileDialog.cpp 46.6 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 "SessionManager.h"
55
#include "ShellCommand.h"
56
#include "TabTitleFormatAction.h"
57
#include "WindowSystemInfo.h"
58

59
60
using namespace Konsole;

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

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

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

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

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

79
80
81
82
83
84
85
86
87
88
    // 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
89
    _pageNeedsUpdate.resize(_ui->tabWidget->count());
90
    connect(_ui->tabWidget, SIGNAL(currentChanged(int)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
91
            SLOT(preparePage(int)));
92

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
104
    SessionManager::instance()->changeProfile(_profile, _tempProfile->setProperties());
105
106
107

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

114
115
    createTempProfile();

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

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

175
176
    _profile = profile;

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

180
181
182
183
184
    // 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
185
    preparePage(_ui->tabWidget->currentIndex());
186

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

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

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

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

220
        _pageNeedsUpdate[page] = false;
221
    }
Robert Knight's avatar
   
Robert Knight committed
222
223

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

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

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

            _ui->emptyNameWarningWidget->setVisible(false);
253
254
        }
    }
255

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
269
    _ui->dirSelectButton->setIcon(KIcon("folder-open"));
Jekyll Wu's avatar
Jekyll Wu committed
270
271
    _ui->iconSelectButton->setIcon(KIcon(profile->icon()));
    _ui->startInSameDirButton->setChecked(profile->property<bool>(Profile::StartInCurrentSessionDir));
272

273
    // window options
Jekyll Wu's avatar
Jekyll Wu committed
274
275
    _ui->showTerminalSizeHintButton->setChecked(profile->property<bool>(Profile::ShowTerminalSizeHint));
    _ui->saveGeometryOnExitButton->setChecked(profile->property<bool>(Profile::SaveGeometryOnExit));
276
277

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
300
301
    QWeakPointer<KDialog> dialog = new KDialog(this);
    KTextEdit* edit = new KTextEdit(dialog.data());
302

Jekyll Wu's avatar
Jekyll Wu committed
303
    QStringList currentEnvironment = profile->property<QStringList>(Profile::Environment);
304

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
308
309
    dialog.data()->setPlainCaption(i18n("Edit Environment"));
    dialog.data()->setMainWidget(edit);
310

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
316
    delete dialog.data();
317
}
Jekyll Wu's avatar
Jekyll Wu committed
318
void EditProfileDialog::setupTabsPage(const Profile::Ptr profile)
319
{
320
321
322
    // tab title format
    _ui->tabTitleEdit->setClearButtonShown(true);
    _ui->remoteTabTitleEdit->setClearButtonShown(true);
Jekyll Wu's avatar
Jekyll Wu committed
323
    _ui->tabTitleEdit->setText(profile->property<QString>(Profile::LocalTabTitleFormat));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
324
    _ui->remoteTabTitleEdit->setText(
Jekyll Wu's avatar
Jekyll Wu committed
325
        profile->property<QString>(Profile::RemoteTabTitleFormat));
326

Jekyll Wu's avatar
Jekyll Wu committed
327
    // tab monitoring
Jekyll Wu's avatar
Jekyll Wu committed
328
    int silenceSeconds = profile->property<int>(Profile::SilenceSeconds);
Jekyll Wu's avatar
Jekyll Wu committed
329
    _ui->silenceSecondsSpinner->setValue(silenceSeconds);
330
    _ui->silenceSecondsSpinner->setSuffix(ki18ncp("Unit of time", " second", " seconds"));
Jekyll Wu's avatar
Jekyll Wu committed
331

332
    connect(_ui->tabTitleEdit, SIGNAL(textChanged(QString)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
333
            SLOT(tabTitleFormatChanged(QString)));
334
    connect(_ui->remoteTabTitleEdit, SIGNAL(textChanged(QString)), this,
Laurent Montel's avatar
Laurent Montel committed
335
            SLOT(remoteTabTitleFormatChanged(QString)));
336

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

340
341
342
343
    // menus for local and remote tab title dynamic elements
    TabTitleFormatAction* localTabTitleAction = new TabTitleFormatAction(this);
    localTabTitleAction->setContext(Session::LocalTabTitle);
    _ui->tabTitleEditButton->setMenu(localTabTitleAction->menu());
344
345
    connect(localTabTitleAction, SIGNAL(dynamicElementSelected(QString)),
            this, SLOT(insertTabTitleText(QString)));
346
347
348
349

    TabTitleFormatAction* remoteTabTitleAction = new TabTitleFormatAction(this);
    remoteTabTitleAction->setContext(Session::RemoteTabTitle);
    _ui->remoteTabTitleEditButton->setMenu(remoteTabTitleAction->menu());
350
351
    connect(remoteTabTitleAction, SIGNAL(dynamicElementSelected(QString)),
            this, SLOT(insertRemoteTabTitleText(QString)));
352
}
353
354
355
356
357
358
359
360
void EditProfileDialog::insertTabTitleText(const QString& text)
{
    _ui->tabTitleEdit->insert(text);
}
void EditProfileDialog::insertRemoteTabTitleText(const QString& text)
{
    _ui->remoteTabTitleEdit->insert(text);
}
361
void EditProfileDialog::saveGeometryOnExit(bool value)
362
{
363
    updateTempProfileProperty(Profile::SaveGeometryOnExit, value);
364
}
365
void EditProfileDialog::showTerminalSizeHint(bool value)
366
{
367
    updateTempProfileProperty(Profile::ShowTerminalSizeHint, value);
368
}
369
370
void EditProfileDialog::tabTitleFormatChanged(const QString& format)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
371
    updateTempProfileProperty(Profile::LocalTabTitleFormat, format);
372
373
374
}
void EditProfileDialog::remoteTabTitleFormatChanged(const QString& format)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
375
    updateTempProfileProperty(Profile::RemoteTabTitleFormat, format);
376
377
}

378
379
380
381
382
void EditProfileDialog::silenceSecondsChanged(int seconds)
{
    updateTempProfileProperty(Profile::SilenceSeconds, seconds);
}

383
384
void EditProfileDialog::selectIcon()
{
Craig Drummond's avatar
Craig Drummond committed
385
    const QString& icon = KIconDialog::getIcon(KIconLoader::Desktop, KIconLoader::Application,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
386
387
388
389
                          false, 0, false, this);
    if (!icon.isEmpty()) {
        _ui->iconSelectButton->setIcon(KIcon(icon));
        updateTempProfileProperty(Profile::Icon, icon);
390
391
392
393
    }
}
void EditProfileDialog::profileNameChanged(const QString& text)
{
394
395
    _ui->emptyNameWarningWidget->setVisible(text.isEmpty());

Kurt Hindenburg's avatar
Kurt Hindenburg committed
396
    updateTempProfileProperty(Profile::Name, text);
397
    updateCaption(_tempProfile);
398
}
399
400
void EditProfileDialog::startInSameDir(bool sameDir)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
401
    updateTempProfileProperty(Profile::StartInCurrentSessionDir, sameDir);
402
}
403
404
void EditProfileDialog::initialDirChanged(const QString& dir)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
405
    updateTempProfileProperty(Profile::Directory, dir);
406
407
408
}
void EditProfileDialog::commandChanged(const QString& command)
{
409
410
    ShellCommand shellCommand(command);

Kurt Hindenburg's avatar
Kurt Hindenburg committed
411
412
    updateTempProfileProperty(Profile::Command, shellCommand.command());
    updateTempProfileProperty(Profile::Arguments, shellCommand.arguments());
413
414
415
}
void EditProfileDialog::selectInitialDir()
{
416
    const KUrl url = KFileDialog::getExistingDirectoryUrl(_ui->initialDirEdit->text(),
Kurt Hindenburg's avatar
Kurt Hindenburg committed
417
418
                     this,
                     i18n("Select Initial Directory"));
419

Kurt Hindenburg's avatar
Kurt Hindenburg committed
420
    if (!url.isEmpty())
421
        _ui->initialDirEdit->setText(url.path());
422
}
Jekyll Wu's avatar
Jekyll Wu committed
423
void EditProfileDialog::setupAppearancePage(const Profile::Ptr profile)
424
{
425
    ColorSchemeViewDelegate* delegate = new ColorSchemeViewDelegate(this);
426
    _ui->colorSchemeList->setItemDelegate(delegate);
427

Kurt Hindenburg's avatar
Kurt Hindenburg committed
428
    _colorSchemeAnimationTimeLine = new QTimeLine(500 , this);
Robert Knight's avatar
   
Robert Knight committed
429
    delegate->setEntryTimeLine(_colorSchemeAnimationTimeLine);
430

431
    connect(_colorSchemeAnimationTimeLine, SIGNAL(valueChanged(qreal)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
432
            SLOT(colorSchemeAnimationUpdate()));
433

Robert Knight's avatar
   
Robert Knight committed
434
    _ui->transparencyWarningWidget->setVisible(false);
435
436
437
    _ui->transparencyWarningWidget->setWordWrap(true);
    _ui->transparencyWarningWidget->setCloseButtonVisible(false);
    _ui->transparencyWarningWidget->setMessageType(KMessageWidget::Warning);
438

Robert Knight's avatar
   
Robert Knight committed
439
440
441
    _ui->editColorSchemeButton->setEnabled(false);
    _ui->removeColorSchemeButton->setEnabled(false);

442
443
    // setup color list
    updateColorSchemeList(true);
444

445
446
    _ui->colorSchemeList->setMouseTracking(true);
    _ui->colorSchemeList->installEventFilter(this);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
447
    _ui->colorSchemeList->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
448

449
450
451
452
    connect(_ui->colorSchemeList->selectionModel(),
            SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
            this, SLOT(colorSchemeSelected()));
    connect(_ui->colorSchemeList, SIGNAL(entered(QModelIndex)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
453
            SLOT(previewColorScheme(QModelIndex)));
454

Robert Knight's avatar
   
Robert Knight committed
455
    updateColorSchemeButtons();
456

457
    connect(_ui->editColorSchemeButton, SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
458
            SLOT(editColorScheme()));
459
    connect(_ui->removeColorSchemeButton, SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
460
            SLOT(removeColorScheme()));
461
    connect(_ui->newColorSchemeButton, SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
462
            SLOT(newColorScheme()));
463

464
    // setup font preview
Jekyll Wu's avatar
Jekyll Wu committed
465
    bool antialias = profile->property<bool>(Profile::AntiAliasFonts);
466

467
468
    QFont profileFont = profile->font();
    profileFont.setStyleStrategy(antialias ? QFont::PreferAntialias : QFont::NoAntialias);
469

470
    _ui->fontPreviewLabel->installEventFilter(this);
471
472
473
    _ui->fontPreviewLabel->setFont(profileFont);
    setFontSliderRange(profileFont);
    setFontSliderValue(profileFont);
474

475
    connect(_ui->fontSizeSlider, SIGNAL(valueChanged(int)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
476
            SLOT(setFontSize(int)));
477
    connect(_ui->editFontButton, SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
478
            SLOT(showFontDialog()));
479

Kurt Hindenburg's avatar
Kurt Hindenburg committed
480
    // setup font smoothing
481
    _ui->antialiasTextButton->setChecked(antialias);
482
    connect(_ui->antialiasTextButton, SIGNAL(toggled(bool)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
483
            SLOT(setAntialiasText(bool)));
484

Jekyll Wu's avatar
Jekyll Wu committed
485
    bool boldIntense = profile->property<bool>(Profile::BoldIntense);
486
    _ui->boldIntenseButton->setChecked(boldIntense);
487
    connect(_ui->boldIntenseButton, SIGNAL(toggled(bool)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
488
            SLOT(setBoldIntense(bool)));
489
490
491
}
void EditProfileDialog::setAntialiasText(bool enable)
{
492
493
    QFont profileFont = _ui->fontPreviewLabel->font();
    profileFont.setStyleStrategy(enable ? QFont::PreferAntialias : QFont::NoAntialias);
494

495
    // update preview to reflect text smoothing state
496
    fontSelected(profileFont);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
497
    updateTempProfileProperty(Profile::AntiAliasFonts, enable);
498
}
499
500
void EditProfileDialog::setBoldIntense(bool enable)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
501
502
    preview(Profile::BoldIntense, enable);
    updateTempProfileProperty(Profile::BoldIntense, enable);
503
}
504
505
506
507
void EditProfileDialog::colorSchemeAnimationUpdate()
{
    QAbstractItemModel* model = _ui->colorSchemeList->model();

Kurt Hindenburg's avatar
Kurt Hindenburg committed
508
509
    for (int i = model->rowCount() ; i >= 0 ; i--)
        _ui->colorSchemeList->update(model->index(i, 0));
510
}
511
void EditProfileDialog::updateColorSchemeList(bool selectCurrentScheme)
512
{
Robert Knight's avatar
   
Robert Knight committed
513
514
    if (!_ui->colorSchemeList->model())
        _ui->colorSchemeList->setModel(new QStandardItemModel(this));
515

516
    const QString& name = lookupProfile()->colorScheme();
517
518
    const ColorScheme* currentScheme = ColorSchemeManager::instance()->findColorScheme(name);

Robert Knight's avatar
   
Robert Knight committed
519
520
521
    QStandardItemModel* model = qobject_cast<QStandardItemModel*>(_ui->colorSchemeList->model());

    Q_ASSERT(model);
522

Robert Knight's avatar
   
Robert Knight committed
523
524
    model->clear();

525
526
    QStandardItem* selectedItem = 0;

527
528
529
530
531
    QList<const ColorScheme*> schemeList = ColorSchemeManager::instance()->allColorSchemes();

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

534
        if (currentScheme == scheme)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
535
            selectedItem = item;
Robert Knight's avatar
   
Robert Knight committed
536

537
538
539
        model->appendRow(item);
    }

540
541
    model->sort(0);

Kurt Hindenburg's avatar
Kurt Hindenburg committed
542
    if (selectCurrentScheme && selectedItem) {
543
        _ui->colorSchemeList->updateGeometry();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
544
545
        _ui->colorSchemeList->selectionModel()->setCurrentIndex(selectedItem->index() ,
                QItemSelectionModel::Select);
Robert Knight's avatar
   
Robert Knight committed
546
547
548

        // update transparency warning label
        updateTransparencyWarning();
549
    }
550
}
551
void EditProfileDialog::updateKeyBindingsList(bool selectCurrentTranslator)
552
{
Robert Knight's avatar
   
Robert Knight committed
553
554
    if (!_ui->keyBindingList->model())
        _ui->keyBindingList->setModel(new QStandardItemModel(this));
555

556
    const QString& name = lookupProfile()
Kurt Hindenburg's avatar
Kurt Hindenburg committed
557
                          ->property<QString>(Profile::KeyBindings);
558

559
    KeyboardTranslatorManager* keyManager = KeyboardTranslatorManager::instance();
560
    const KeyboardTranslator* currentTranslator = keyManager->findTranslator(name);
561

Robert Knight's avatar
   
Robert Knight committed
562
563
564
565
566
    QStandardItemModel* model = qobject_cast<QStandardItemModel*>(_ui->keyBindingList->model());

    Q_ASSERT(model);

    model->clear();
567

568
569
    QStandardItem* selectedItem = 0;

570
    QList<QString> translatorNames = keyManager->allTranslators();
571
572
    foreach (const QString& translatorName, translatorNames) {
        const KeyboardTranslator* translator = keyManager->findTranslator(translatorName);
573
574

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
578
        if (translator == currentTranslator)
579
            selectedItem = item;
580
581
582
583

        model->appendRow(item);
    }

584
    model->sort(0);
585

Kurt Hindenburg's avatar
Kurt Hindenburg committed
586
587
588
    if (selectCurrentTranslator && selectedItem) {
        _ui->keyBindingList->selectionModel()->setCurrentIndex(selectedItem->index() ,
                QItemSelectionModel::Select);
589
    }
590
}
591
bool EditProfileDialog::eventFilter(QObject* watched , QEvent* aEvent)
592
{
593
    if (watched == _ui->colorSchemeList && aEvent->type() == QEvent::Leave) {
Kurt Hindenburg's avatar
Kurt Hindenburg committed
594
595
        if (_tempProfile->isPropertySet(Profile::ColorScheme))
            preview(Profile::ColorScheme, _tempProfile->colorScheme());
596
597
        else
            unpreview(Profile::ColorScheme);
598
    }
599
    if (watched == _ui->fontPreviewLabel && aEvent->type() == QEvent::FontChange) {
600
        const QFont& labelFont = _ui->fontPreviewLabel->font();
601
602
        qreal fontSizeF = labelFont.pointSizeF();
        QString fontSize  = KGlobal::locale()->formatNumber(fontSizeF, fontSizeF == floor(fontSizeF) ? 0 : 1);
603
        _ui->fontPreviewLabel->setText(i18n("%1, size %2", labelFont.family(), fontSize));
604
    }
605

606
    return KDialog::eventFilter(watched, aEvent);
607
}
608
void EditProfileDialog::unpreviewAll()
609
{
610
611
612
    _delayedPreviewTimer->stop();
    _delayedPreviewProperties.clear();

Kurt Hindenburg's avatar
Kurt Hindenburg committed
613
614
615
    QHash<Profile::Property, QVariant> map;
    QHashIterator<int, QVariant> iter(_previewedProperties);
    while (iter.hasNext()) {
616
        iter.next();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
617
        map.insert((Profile::Property)iter.key(), iter.value());
618
619
620
    }

    // undo any preview changes
Kurt Hindenburg's avatar
Kurt Hindenburg committed
621
622
    if (!map.isEmpty())
        SessionManager::instance()->changeProfile(_profile, map, false);
623
}
624
void EditProfileDialog::unpreview(int aProperty)
625
{
626
    _delayedPreviewProperties.remove(aProperty);
627

628
    if (!_previewedProperties.contains(aProperty))
629
        return;
630

Kurt Hindenburg's avatar
Kurt Hindenburg committed
631
    QHash<Profile::Property, QVariant> map;
632
    map.insert((Profile::Property)aProperty, _previewedProperties[aProperty]);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
633
    SessionManager::instance()->changeProfile(_profile, map, false);
634

635
    _previewedProperties.remove(aProperty);
636
}
637
void EditProfileDialog::delayedPreview(int aProperty , const QVariant& value)
638
{
639
    _delayedPreviewProperties.insert(aProperty, value);
640
641

    _delayedPreviewTimer->stop();
642
    _delayedPreviewTimer->start(300);
643
644
645
}
void EditProfileDialog::delayedPreviewActivate()
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
646
    Q_ASSERT(qobject_cast<QTimer*>(sender()));
647

Kurt Hindenburg's avatar
Kurt Hindenburg committed
648
649
    QMutableHashIterator<int, QVariant> iter(_delayedPreviewProperties);
    if (iter.hasNext()) {
650
        iter.next();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
651
        preview(iter.key(), iter.value());
652
653
    }
}
654
void EditProfileDialog::preview(int aProperty , const QVariant& value)
655
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
656
    QHash<Profile::Property, QVariant> map;
657
    map.insert((Profile::Property)aProperty, value);
658

659
    _delayedPreviewProperties.remove(aProperty);
660

661
    const Profile::Ptr original = lookupProfile();
662

663
664
665
666
667
668
    // 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 &&
669
            original->property<QVariant>((Profile::Property)aProperty).isNull())
670
671
        return;

672
673
    if (!_previewedProperties.contains(aProperty)) {
        _previewedProperties.insert(aProperty , original->property<QVariant>((Profile::Property)aProperty));
674
    }
675
676

    // temporary change to color scheme
Kurt Hindenburg's avatar
Kurt Hindenburg committed
677
    SessionManager::instance()->changeProfile(_profile , map , false);
678
679
680
}
void EditProfileDialog::previewColorScheme(const QModelIndex& index)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
681
    const QString& name = index.data(Qt::UserRole + 1).value<const ColorScheme*>()->name();
682

Kurt Hindenburg's avatar
Kurt Hindenburg committed
683
    delayedPreview(Profile::ColorScheme , name);
684
}
685
686
void EditProfileDialog::removeColorScheme()
{
687
688
    QModelIndexList selected = _ui->colorSchemeList->selectionModel()->selectedIndexes();

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
692
        if (ColorSchemeManager::instance()->deleteColorScheme(name))
693
            _ui->colorSchemeList->model()->removeRow(selected.first().row());
694
    }
695
}
696
void EditProfileDialog::showColorSchemeEditor(bool isNewScheme)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
697
{
698
699
    QModelIndexList selected = _ui->colorSchemeList->selectionModel()->selectedIndexes();

700
    QAbstractItemModel* model = _ui->colorSchemeList->model();
Robert Knight's avatar
   
Robert Knight committed
701
    const ColorScheme* colors = 0;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
702
703
    if (!selected.isEmpty())
        colors = model->data(selected.first(), Qt::UserRole + 1).value<const ColorScheme*>();
704
    else
Robert Knight's avatar
   
Robert Knight committed
705
        colors = ColorSchemeManager::instance()->defaultColorScheme();
706

Robert Knight's avatar
   
Robert Knight committed
707
    Q_ASSERT(colors);
708

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
711