EditProfileDialog.cpp 46.9 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
EditProfileDialog::EditProfileDialog(QWidget* parent)
62
    : KDialog(parent)
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

Jekyll Wu's avatar
Jekyll Wu committed
79
    // TODO: Re-enable in a later KDE 4.x release when this feature works again
80
    _ui->enableResizeWindowButton->setVisible(false);
Jekyll Wu's avatar
Jekyll Wu committed
81

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
107
    SessionManager::instance()->changeProfile(_profile, _tempProfile->setProperties());
108 109 110

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

117 118
    createTempProfile();

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

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

178 179
    _profile = profile;

180
    // update caption
181
    updateCaption(profile);
182

183 184 185 186 187
    // 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
188
    preparePage(_ui->tabWidget->currentIndex());
189

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

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

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

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

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

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

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

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

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

259

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
273
    _ui->dirSelectButton->setIcon(KIcon("folder-open"));
Jekyll Wu's avatar
Jekyll Wu committed
274 275
    _ui->iconSelectButton->setIcon(KIcon(profile->icon()));
    _ui->startInSameDirButton->setChecked(profile->property<bool>(Profile::StartInCurrentSessionDir));
276

277
    // window options
Jekyll Wu's avatar
Jekyll Wu committed
278 279
    _ui->showTerminalSizeHintButton->setChecked(profile->property<bool>(Profile::ShowTerminalSizeHint));
    _ui->saveGeometryOnExitButton->setChecked(profile->property<bool>(Profile::SaveGeometryOnExit));
280 281

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

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

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

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

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

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

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

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

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

336
    connect(_ui->tabTitleEdit, SIGNAL(textChanged(QString)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
337
            SLOT(tabTitleFormatChanged(QString)));
338
    connect(_ui->remoteTabTitleEdit, SIGNAL(textChanged(QString)), this,
Laurent Montel's avatar
Laurent Montel committed
339
            SLOT(remoteTabTitleFormatChanged(QString)));
340

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

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

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

382 383 384 385 386
void EditProfileDialog::silenceSecondsChanged(int seconds)
{
    updateTempProfileProperty(Profile::SilenceSeconds, seconds);
}

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

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

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
432
    _colorSchemeAnimationTimeLine = new QTimeLine(500 , this);
Robert Knight's avatar
 
Robert Knight committed
433
    delegate->setEntryTimeLine(_colorSchemeAnimationTimeLine);
434

435
    connect(_colorSchemeAnimationTimeLine, SIGNAL(valueChanged(qreal)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
436
            SLOT(colorSchemeAnimationUpdate()));
437

Robert Knight's avatar
 
Robert Knight committed
438
    _ui->transparencyWarningWidget->setVisible(false);
439 440 441
    _ui->transparencyWarningWidget->setWordWrap(true);
    _ui->transparencyWarningWidget->setCloseButtonVisible(false);
    _ui->transparencyWarningWidget->setMessageType(KMessageWidget::Warning);
442

Robert Knight's avatar
 
Robert Knight committed
443 444 445
    _ui->editColorSchemeButton->setEnabled(false);
    _ui->removeColorSchemeButton->setEnabled(false);

446 447
    // setup color list
    updateColorSchemeList(true);
448

449 450
    _ui->colorSchemeList->setMouseTracking(true);
    _ui->colorSchemeList->installEventFilter(this);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
451
    _ui->colorSchemeList->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
452

453 454 455 456
    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
457
            SLOT(previewColorScheme(QModelIndex)));
458

Robert Knight's avatar
 
Robert Knight committed
459
    updateColorSchemeButtons();
460

461
    connect(_ui->editColorSchemeButton, SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
462
            SLOT(editColorScheme()));
463
    connect(_ui->removeColorSchemeButton, SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
464
            SLOT(removeColorScheme()));
465
    connect(_ui->newColorSchemeButton, SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
466
            SLOT(newColorScheme()));
467

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

Jekyll Wu's avatar
Jekyll Wu committed
471
    QFont font = profile->font();
472
    font.setStyleStrategy(antialias ? QFont::PreferAntialias : QFont::NoAntialias);
473

474 475
    _ui->fontPreviewLabel->installEventFilter(this);
    _ui->fontPreviewLabel->setFont(font);
476 477
    setFontSliderRange(font);
    setFontSliderValue(font);
478

479
    connect(_ui->fontSizeSlider, SIGNAL(valueChanged(int)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
480
            SLOT(setFontSize(int)));
481
    connect(_ui->editFontButton, SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
482
            SLOT(showFontDialog()));
483

Kurt Hindenburg's avatar
Kurt Hindenburg committed
484
    // setup font smoothing
485
    _ui->antialiasTextButton->setChecked(antialias);
486
    connect(_ui->antialiasTextButton, SIGNAL(toggled(bool)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
487
            SLOT(setAntialiasText(bool)));
488

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

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

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

520
    const QString& name = lookupProfile()->colorScheme();
521 522
    const ColorScheme* currentScheme = ColorSchemeManager::instance()->findColorScheme(name);

Robert Knight's avatar
 
Robert Knight committed
523 524 525
    QStandardItemModel* model = qobject_cast<QStandardItemModel*>(_ui->colorSchemeList->model());

    Q_ASSERT(model);
526

Robert Knight's avatar
 
Robert Knight committed
527 528
    model->clear();

529 530
    QStandardItem* selectedItem = 0;

531 532 533 534 535
    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
536
        item->setFlags(item->flags());
537

538
        if (currentScheme == scheme)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
539
            selectedItem = item;
Robert Knight's avatar
 
Robert Knight committed
540

541 542 543
        model->appendRow(item);
    }

544 545
    model->sort(0);

Kurt Hindenburg's avatar
Kurt Hindenburg committed
546
    if (selectCurrentScheme && selectedItem) {
547
        _ui->colorSchemeList->updateGeometry();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
548 549
        _ui->colorSchemeList->selectionModel()->setCurrentIndex(selectedItem->index() ,
                QItemSelectionModel::Select);
Robert Knight's avatar
 
Robert Knight committed
550 551 552

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

560
    const QString& name = lookupProfile()
Kurt Hindenburg's avatar
Kurt Hindenburg committed
561
                          ->property<QString>(Profile::KeyBindings);
562

563
    KeyboardTranslatorManager* keyManager = KeyboardTranslatorManager::instance();
564
    const KeyboardTranslator* currentTranslator = keyManager->findTranslator(name);
565

Robert Knight's avatar
 
Robert Knight committed
566 567 568 569 570
    QStandardItemModel* model = qobject_cast<QStandardItemModel*>(_ui->keyBindingList->model());

    Q_ASSERT(model);

    model->clear();
571

572 573
    QStandardItem* selectedItem = 0;

574
    QList<QString> translatorNames = keyManager->allTranslators();
575
    foreach ( const QString& name, translatorNames) {
576
        const KeyboardTranslator* translator = keyManager->findTranslator(name);
577 578

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
582
        if (translator == currentTranslator)
583
            selectedItem = item;
584 585 586 587

        model->appendRow(item);
    }

588
    model->sort(0);
589

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
610
    return KDialog::eventFilter(watched, event);
611
}
612
void EditProfileDialog::unpreviewAll()
613
{
614 615 616
    _delayedPreviewTimer->stop();
    _delayedPreviewProperties.clear();

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

    // undo any preview changes
Kurt Hindenburg's avatar
Kurt Hindenburg committed
625 626
    if (!map.isEmpty())
        SessionManager::instance()->changeProfile(_profile, map, false);
627 628 629
}
void EditProfileDialog::unpreview(int property)
{
630
    _delayedPreviewProperties.remove(property);
631

632 633
    if (!_previewedProperties.contains(property))
        return;
634

Kurt Hindenburg's avatar
Kurt Hindenburg committed
635 636 637
    QHash<Profile::Property, QVariant> map;
    map.insert((Profile::Property)property, _previewedProperties[property]);
    SessionManager::instance()->changeProfile(_profile, map, false);
638 639 640

    _previewedProperties.remove(property);
}
641 642
void EditProfileDialog::delayedPreview(int property , const QVariant& value)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
643
    _delayedPreviewProperties.insert(property, value);
644 645

    _delayedPreviewTimer->stop();
646
    _delayedPreviewTimer->start(300);
647 648 649
}
void EditProfileDialog::delayedPreviewActivate()
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
650
    Q_ASSERT(qobject_cast<QTimer*>(sender()));
651

Kurt Hindenburg's avatar
Kurt Hindenburg committed
652 653
    QMutableHashIterator<int, QVariant> iter(_delayedPreviewProperties);
    if (iter.hasNext()) {
654
        iter.next();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
655
        preview(iter.key(), iter.value());
656 657
    }
}
Robert Knight's avatar
Robert Knight committed
658
void EditProfileDialog::preview(int property , const QVariant& value)
659
{