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

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

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

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

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

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

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

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

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

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

69 70
using namespace Konsole;

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

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

82
    connect(this, &Konsole::EditProfileDialog::applyClicked, this, &Konsole::EditProfileDialog::save);
Jekyll Wu's avatar
Jekyll Wu committed
83

84
    connect(_delayedPreviewTimer, &QTimer::timeout, this, &Konsole::EditProfileDialog::delayedPreviewActivate);
Jekyll Wu's avatar
Jekyll Wu committed
85

86
    _ui = new Ui::EditProfileDialog();
87
    _ui->setupUi(mainWidget());
88

89 90 91 92 93 94 95 96 97 98
    // 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
99
    _pageNeedsUpdate.resize(_ui->tabWidget->count());
100 101
    connect(_ui->tabWidget, &QTabWidget::currentChanged, this,
            &Konsole::EditProfileDialog::preparePage);
102

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

114
    ProfileManager::instance()->changeProfile(_profile, _tempProfile->setProperties());
115 116 117

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

124 125
    createTempProfile();

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

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

185 186
    _profile = profile;

187
    // update caption
188
    updateCaption(profile);
189

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

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

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

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

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

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

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

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

            _ui->emptyNameWarningWidget->setVisible(false);
261 262
        }
    }
263

Jekyll Wu's avatar
Jekyll Wu committed
264
    ShellCommand command(profile->command() , profile->arguments());
Kurt Hindenburg's avatar
Kurt Hindenburg committed
265
    _ui->commandEdit->setText(command.fullCommand());
266

Kurt Hindenburg's avatar
Kurt Hindenburg committed
267 268 269 270 271
#pragma message("Look at this setCompletionObject again")
//    KUrlCompletion* exeCompletion = new KUrlCompletion(KUrlCompletion::ExeCompletion);
//    exeCompletion->setParent(this);
//    exeCompletion->setDir(QUrl());
//    _ui->commandEdit->setCompletionObject(exeCompletion);
272

273
/* The below causes a noticeable delay when opening the dialog - I'm not entirely sure
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
   this is the best way to handle this.
   Issue is that QLineEdit->SetCompleter() won't work w/ KDE's KUrlCompletion

    QFileSystemModel *commandEditDirModel = new QFileSystemModel(this);
    commandEditDirModel->setFilter(QDir::AllEntries);
    QFileInfo commandFileInfo(profile->command());
    // If command is /usr/bin/zsh, start at /usr/bin for completion
    commandEditDirModel->setRootPath(commandFileInfo.absolutePath());
    QCompleter *commandEditCompleter = new QCompleter(this);
    commandEditCompleter->setModel(commandEditDirModel); 
    _ui->commandEdit->setCompleter(commandEditCompleter);

    QFileSystemModel *initialEditDirModel = new QFileSystemModel(this);
    initialEditDirModel->setFilter(QDir::AllEntries);
    initialEditDirModel->setRootPath(QString('/'));
Jekyll Wu's avatar
Jekyll Wu committed
289
    _ui->initialDirEdit->setText(profile->defaultWorkingDirectory());
290 291 292 293
    QCompleter *initialDirCompleter = new QCompleter(this);
    initialDirCompleter->setModel(initialEditDirModel); 
    _ui->initialDirEdit->setCompleter(initialDirCompleter);
*/
Kurt Hindenburg's avatar
Kurt Hindenburg committed
294
    _ui->initialDirEdit->setClearButtonEnabled(true);
295

Kurt Hindenburg's avatar
Kurt Hindenburg committed
296
    _ui->dirSelectButton->setIcon(KIcon("folder-open"));
Jekyll Wu's avatar
Jekyll Wu committed
297
    _ui->iconSelectButton->setIcon(KIcon(profile->icon()));
298
    _ui->startInSameDirButton->setChecked(profile->startInCurrentSessionDir());
299

300 301 302 303
    // terminal options
    _ui->terminalColumnsEntry->setValue(profile->terminalColumns());
    _ui->terminalRowsEntry->setValue(profile->terminalRows());

304
    // window options
305
    _ui->showTerminalSizeHintButton->setChecked(profile->showTerminalSizeHint());
306 307

    // signals and slots
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
    connect(_ui->dirSelectButton, &QToolButton::clicked, this, &Konsole::EditProfileDialog::selectInitialDir);
    connect(_ui->iconSelectButton, &QPushButton::clicked, this, &Konsole::EditProfileDialog::selectIcon);
    connect(_ui->startInSameDirButton, &QCheckBox::toggled, this ,
            &Konsole::EditProfileDialog::startInSameDir);
    connect(_ui->profileNameEdit, &QLineEdit::textChanged, this,
            &Konsole::EditProfileDialog::profileNameChanged);
    connect(_ui->initialDirEdit, &QLineEdit::textChanged, this,
            &Konsole::EditProfileDialog::initialDirChanged);
    connect(_ui->commandEdit, &QLineEdit::textChanged, this,
            &Konsole::EditProfileDialog::commandChanged);
    connect(_ui->environmentEditButton , &QPushButton::clicked, this,
            &Konsole::EditProfileDialog::showEnvironmentEditor);

    connect(_ui->terminalColumnsEntry, static_cast<void(KIntSpinBox::*)(int)>(&KIntSpinBox::valueChanged),
            this, &Konsole::EditProfileDialog::terminalColumnsEntryChanged);
    connect(_ui->terminalRowsEntry, static_cast<void(KIntSpinBox::*)(int)>(&KIntSpinBox::valueChanged),
            this, &Konsole::EditProfileDialog::terminalRowsEntryChanged);

    connect(_ui->showTerminalSizeHintButton, &QCheckBox::toggled, this,
            &Konsole::EditProfileDialog::showTerminalSizeHint);
328 329 330
}
void EditProfileDialog::showEnvironmentEditor()
{
Jekyll Wu's avatar
Jekyll Wu committed
331
    const Profile::Ptr profile = lookupProfile();
332

Kurt Hindenburg's avatar
Kurt Hindenburg committed
333 334
    QWeakPointer<KDialog> dialog = new KDialog(this);
    KTextEdit* edit = new KTextEdit(dialog.data());
335

336
    QStringList currentEnvironment = profile->environment();
337

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
341 342
    dialog.data()->setPlainCaption(i18n("Edit Environment"));
    dialog.data()->setMainWidget(edit);
343

Kurt Hindenburg's avatar
Kurt Hindenburg committed
344
    if (dialog.data()->exec() == QDialog::Accepted) {
345
        QStringList newEnvironment = edit->toPlainText().split('\n');
Kurt Hindenburg's avatar
Kurt Hindenburg committed
346
        updateTempProfileProperty(Profile::Environment, newEnvironment);
347 348
    }

Kurt Hindenburg's avatar
Kurt Hindenburg committed
349
    delete dialog.data();
350
}
Jekyll Wu's avatar
Jekyll Wu committed
351
void EditProfileDialog::setupTabsPage(const Profile::Ptr profile)
352
{
353
    // tab title format
354 355 356
    _ui->renameTabWidget->setTabTitleText(profile->localTabTitleFormat());
    _ui->renameTabWidget->setRemoteTabTitleText(profile->remoteTabTitleFormat());

357 358 359 360
    connect(_ui->renameTabWidget, &Konsole::RenameTabWidget::tabTitleFormatChanged, this,
            &Konsole::EditProfileDialog::tabTitleFormatChanged);
    connect(_ui->renameTabWidget, &Konsole::RenameTabWidget::remoteTabTitleFormatChanged, this,
            &Konsole::EditProfileDialog::remoteTabTitleFormatChanged);
361

Jekyll Wu's avatar
Jekyll Wu committed
362
    // tab monitoring
Jekyll Wu's avatar
Jekyll Wu committed
363
    const int silenceSeconds = profile->silenceSeconds();
Jekyll Wu's avatar
Jekyll Wu committed
364
    _ui->silenceSecondsSpinner->setValue(silenceSeconds);
365
    _ui->silenceSecondsSpinner->setSuffix(ki18ncp("Unit of time", " second", " seconds"));
Jekyll Wu's avatar
Jekyll Wu committed
366

367 368
    connect(_ui->silenceSecondsSpinner, static_cast<void(KIntSpinBox::*)(int)>(&KIntSpinBox::valueChanged),
            this, &Konsole::EditProfileDialog::silenceSecondsChanged);
369
}
370

371 372 373 374 375
void EditProfileDialog::terminalColumnsEntryChanged(int value)
{
    updateTempProfileProperty(Profile::TerminalColumns, value);
}
void EditProfileDialog::terminalRowsEntryChanged(int value)
376
{
377
    updateTempProfileProperty(Profile::TerminalRows, value);
378
}
379
void EditProfileDialog::showTerminalSizeHint(bool value)
380
{
381
    updateTempProfileProperty(Profile::ShowTerminalSizeHint, value);
382
}
383 384
void EditProfileDialog::tabTitleFormatChanged(const QString& format)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
385
    updateTempProfileProperty(Profile::LocalTabTitleFormat, format);
386 387 388
}
void EditProfileDialog::remoteTabTitleFormatChanged(const QString& format)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
389
    updateTempProfileProperty(Profile::RemoteTabTitleFormat, format);
390 391
}

392 393 394 395 396
void EditProfileDialog::silenceSecondsChanged(int seconds)
{
    updateTempProfileProperty(Profile::SilenceSeconds, seconds);
}

397 398
void EditProfileDialog::selectIcon()
{
Craig Drummond's avatar
Craig Drummond committed
399
    const QString& icon = KIconDialog::getIcon(KIconLoader::Desktop, KIconLoader::Application,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
400 401 402 403
                          false, 0, false, this);
    if (!icon.isEmpty()) {
        _ui->iconSelectButton->setIcon(KIcon(icon));
        updateTempProfileProperty(Profile::Icon, icon);
404 405 406 407
    }
}
void EditProfileDialog::profileNameChanged(const QString& text)
{
408 409
    _ui->emptyNameWarningWidget->setVisible(text.isEmpty());

Kurt Hindenburg's avatar
Kurt Hindenburg committed
410
    updateTempProfileProperty(Profile::Name, text);
411
    updateTempProfileProperty(Profile::UntranslatedName, text);
412
    updateCaption(_tempProfile);
413
}
414 415
void EditProfileDialog::startInSameDir(bool sameDir)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
416
    updateTempProfileProperty(Profile::StartInCurrentSessionDir, sameDir);
417
}
418 419
void EditProfileDialog::initialDirChanged(const QString& dir)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
420
    updateTempProfileProperty(Profile::Directory, dir);
421 422 423
}
void EditProfileDialog::commandChanged(const QString& command)
{
424 425
    ShellCommand shellCommand(command);

Kurt Hindenburg's avatar
Kurt Hindenburg committed
426 427
    updateTempProfileProperty(Profile::Command, shellCommand.command());
    updateTempProfileProperty(Profile::Arguments, shellCommand.arguments());
428 429 430
}
void EditProfileDialog::selectInitialDir()
{
Alex Richardson's avatar
Alex Richardson committed
431
    const KUrl url = KFileDialog::getExistingDirectoryUrl(KUrl(_ui->initialDirEdit->text()),
Kurt Hindenburg's avatar
Kurt Hindenburg committed
432 433
                     this,
                     i18n("Select Initial Directory"));
434

Kurt Hindenburg's avatar
Kurt Hindenburg committed
435
    if (!url.isEmpty())
436
        _ui->initialDirEdit->setText(url.path());
437
}
Jekyll Wu's avatar
Jekyll Wu committed
438
void EditProfileDialog::setupAppearancePage(const Profile::Ptr profile)
439
{
440
    ColorSchemeViewDelegate* delegate = new ColorSchemeViewDelegate(this);
441
    _ui->colorSchemeList->setItemDelegate(delegate);
442

Robert Knight's avatar
 
Robert Knight committed
443
    _ui->transparencyWarningWidget->setVisible(false);
444 445 446
    _ui->transparencyWarningWidget->setWordWrap(true);
    _ui->transparencyWarningWidget->setCloseButtonVisible(false);
    _ui->transparencyWarningWidget->setMessageType(KMessageWidget::Warning);
447

Robert Knight's avatar
 
Robert Knight committed
448 449 450
    _ui->editColorSchemeButton->setEnabled(false);
    _ui->removeColorSchemeButton->setEnabled(false);

451 452
    // setup color list
    updateColorSchemeList(true);
453

454 455
    _ui->colorSchemeList->setMouseTracking(true);
    _ui->colorSchemeList->installEventFilter(this);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
456
    _ui->colorSchemeList->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
457

458
    connect(_ui->colorSchemeList->selectionModel(),
459 460 461 462
            &QItemSelectionModel::selectionChanged,
            this, &Konsole::EditProfileDialog::colorSchemeSelected);
    connect(_ui->colorSchemeList, &QListView::entered, this,
            &Konsole::EditProfileDialog::previewColorScheme);
463

Robert Knight's avatar
 
Robert Knight committed
464
    updateColorSchemeButtons();
465

466 467 468 469 470 471
    connect(_ui->editColorSchemeButton, &QPushButton::clicked, this,
            &Konsole::EditProfileDialog::editColorScheme);
    connect(_ui->removeColorSchemeButton, &QPushButton::clicked, this,
            &Konsole::EditProfileDialog::removeColorScheme);
    connect(_ui->newColorSchemeButton, &QPushButton::clicked, this,
            &Konsole::EditProfileDialog::newColorScheme);
472

473
    // setup font preview
Jekyll Wu's avatar
Jekyll Wu committed
474
    const bool antialias = profile->antiAliasFonts();
475

476 477
    QFont profileFont = profile->font();
    profileFont.setStyleStrategy(antialias ? QFont::PreferAntialias : QFont::NoAntialias);
478

479
    _ui->fontPreviewLabel->installEventFilter(this);
480
    _ui->fontPreviewLabel->setFont(profileFont);
481
    setFontInputValue(profileFont);
482

483 484 485 486
    connect(_ui->fontSizeInput, &KDoubleNumInput::valueChanged, this,
            &Konsole::EditProfileDialog::setFontSize);
    connect(_ui->selectFontButton, &QPushButton::clicked, this,
            &Konsole::EditProfileDialog::showFontDialog);
487

Kurt Hindenburg's avatar
Kurt Hindenburg committed
488
    // setup font smoothing
489
    _ui->antialiasTextButton->setChecked(antialias);
490 491
    connect(_ui->antialiasTextButton, &QCheckBox::toggled, this,
            &Konsole::EditProfileDialog::setAntialiasText);
492

Jekyll Wu's avatar
Jekyll Wu committed
493
    _ui->boldIntenseButton->setChecked(profile->boldIntense());
494 495
    connect(_ui->boldIntenseButton, &QCheckBox::toggled, this,
            &Konsole::EditProfileDialog::setBoldIntense);
496
    _ui->enableMouseWheelZoomButton->setChecked(profile->mouseWheelZoomEnabled());
497 498
    connect(_ui->enableMouseWheelZoomButton, &QCheckBox::toggled, this,
            &Konsole::EditProfileDialog::toggleMouseWheelZoom);
499 500 501
}
void EditProfileDialog::setAntialiasText(bool enable)
{
502 503
    QFont profileFont = _ui->fontPreviewLabel->font();
    profileFont.setStyleStrategy(enable ? QFont::PreferAntialias : QFont::NoAntialias);
504

505
    // update preview to reflect text smoothing state
506
    fontSelected(profileFont);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
507
    updateTempProfileProperty(Profile::AntiAliasFonts, enable);
508
}
509 510
void EditProfileDialog::setBoldIntense(bool enable)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
511 512
    preview(Profile::BoldIntense, enable);
    updateTempProfileProperty(Profile::BoldIntense, enable);
513
}
514 515 516 517
void EditProfileDialog::toggleMouseWheelZoom(bool enable)
{
    updateTempProfileProperty(Profile::MouseWheelZoomEnabled, enable);
}
518
void EditProfileDialog::updateColorSchemeList(bool selectCurrentScheme)
519
{
Robert Knight's avatar
 
Robert Knight committed
520 521
    if (!_ui->colorSchemeList->model())
        _ui->colorSchemeList->setModel(new QStandardItemModel(this));
522

523
    const QString& name = lookupProfile()->colorScheme();
524 525
    const ColorScheme* currentScheme = ColorSchemeManager::instance()->findColorScheme(name);

Robert Knight's avatar
 
Robert Knight committed
526 527 528
    QStandardItemModel* model = qobject_cast<QStandardItemModel*>(_ui->colorSchemeList->model());

    Q_ASSERT(model);
529

Robert Knight's avatar
 
Robert Knight committed
530 531
    model->clear();

532 533
    QStandardItem* selectedItem = 0;

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

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

541
        if (currentScheme == scheme)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
542
            selectedItem = item;
Robert Knight's avatar
 
Robert Knight committed
543

544 545 546
        model->appendRow(item);
    }

547 548
    model->sort(0);

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

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

563
    const QString& name = lookupProfile()->keyBindings();
564

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

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

    Q_ASSERT(model);

    model->clear();
573

574 575
    QStandardItem* selectedItem = 0;

Jekyll Wu's avatar
Jekyll Wu committed
576
    QStringList translatorNames = keyManager->allTranslators();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
577
    foreach(const QString& translatorName, translatorNames) {
578
        const KeyboardTranslator* translator = keyManager->findTranslator(translatorName);
579 580

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
585
        if (translator == currentTranslator)
586
            selectedItem = item;
587 588 589 590

        model->appendRow(item);
    }

591
    model->sort(0);
592

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

611
    return KDialog::eventFilter(watched, aEvent);
612
}
613
void EditProfileDialog::unpreviewAll()
614
{
615 616 617
    _delayedPreviewTimer->stop();
    _delayedPreviewProperties.clear();

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

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

633
    if (!_previewedProperties.contains(aProperty))
634
        return;
635

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

640
    _previewedProperties.remove(aProperty);
641
}
642
void EditProfileDialog::delayedPreview(int aProperty , const QVariant& value)
643
{
644
    _delayedPreviewProperties.insert(aProperty, value);
645 646

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

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

664
    _delayedPreviewProperties.remove(aProperty);
665

666
    const Profile::Ptr original = lookupProfile();
667

668 669 670 671 672 673
    // 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 &&
674
            original->property<QVariant>((Profile::Property)aProperty).isNull())
675 676
        return;

677 678
    if (!_previewedProperties.contains(aProperty)) {
        _previewedProperties.insert(aProperty , original->property<QVariant>((Profile::Property)aProperty));
679
    }
680 681

    // temporary change to color scheme
682
    ProfileManager::instance()->changeProfile(_profile , map , false);
683 684 685
}
void EditProfileDialog::previewColorScheme(const QModelIndex& index)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
686
    const QString& name = index.data(Qt::UserRole + 1).value<const ColorScheme*>()->name();
687

Kurt Hindenburg's avatar
Kurt Hindenburg committed
688
    delayedPreview(Profile::ColorScheme , name);
689
}