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

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301  USA.
*/

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

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

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

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

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

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

66 67
using namespace Konsole;

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

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

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

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

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

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

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

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

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

121 122
    createTempProfile();

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

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

182 183
    _profile = profile;

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

187 188 189 190 191
    // mark each page of the dialog as out of date
    // and force an update of the currently visible page
    //
    // the other pages will be updated as necessary
    _pageNeedsUpdate.fill(true);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
192
    preparePage(_ui->tabWidget->currentIndex());
193

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    Q_ASSERT(model);
498

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

501 502
    QStandardItem* selectedItem = 0;

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

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

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

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

516 517
    model->sort(0);

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

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

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

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

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

    Q_ASSERT(model);

    model->clear();
542

543 544
    QStandardItem* selectedItem = 0;

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

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

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

        model->appendRow(item);
    }

560
    model->sort(0);
561

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

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

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

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

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

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

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

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

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

633
    _delayedPreviewProperties.remove(aProperty);
634

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

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