EditProfileDialog.cpp 48.8 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>
Michal Humpula's avatar
Michal Humpula committed
33
#include <QtGui/QIcon>
34
#include <QtCore/QTimer>
35 36
#include <QCompleter>
#include <QFileSystemModel>
Kurt Hindenburg's avatar
Kurt Hindenburg committed
37
#include <QtCore/QUrl>
38
#include <QFontDialog>
39
#include <QFileDialog>
40
#include <QInputDialog>
41
#include <QDialog>
Laurent Montel's avatar
Laurent Montel committed
42
#include <QDebug>
43
// KDE
44 45
#include <KCodecAction>

46
#include <KIconDialog>
47
#include <KUrlCompletion>
48
#include <KWindowSystem>
Rick Xing's avatar
Rick Xing committed
49
#include <KTextEdit>
50
#include <KMessageBox>
51
#include <KLocalizedString>
Laurent Montel's avatar
Laurent Montel committed
52 53 54 55
#include <KConfigGroup>
#include <QDialogButtonBox>
#include <QPushButton>
#include <QVBoxLayout>
56 57 58

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

70 71
using namespace Konsole;

72
EditProfileDialog::EditProfileDialog(QWidget* aParent)
Laurent Montel's avatar
Laurent Montel committed
73
    : QDialog(aParent)
74
    , _delayedPreviewTimer(new QTimer(this))
75
    , _colorDialog(0)
76
{
Laurent Montel's avatar
Laurent Montel committed
77 78 79 80 81 82 83 84 85 86 87
    setWindowTitle(i18n("Edit Profile"));
    mButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel|QDialogButtonBox::Apply);
    QWidget *mainWidget = new QWidget(this);
    QVBoxLayout *mainLayout = new QVBoxLayout;
    setLayout(mainLayout);
    mainLayout->addWidget(mainWidget);
    QPushButton *okButton = mButtonBox->button(QDialogButtonBox::Ok);
    okButton->setDefault(true);
    okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
    connect(mButtonBox, SIGNAL(accepted()), this, SLOT(accept()));
    connect(mButtonBox, SIGNAL(rejected()), this, SLOT(reject()));
88

89
    // disable the apply button , since no modification has been made
Laurent Montel's avatar
Laurent Montel committed
90
    mButtonBox->button(QDialogButtonBox::Apply)->setEnabled(false);
91

Laurent Montel's avatar
Laurent Montel committed
92
    connect(mButtonBox->button(QDialogButtonBox::Apply), &QPushButton::clicked, this, &Konsole::EditProfileDialog::save);
Jekyll Wu's avatar
Jekyll Wu committed
93

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

96
    _ui = new Ui::EditProfileDialog();
Laurent Montel's avatar
Laurent Montel committed
97 98
    _ui->setupUi(mainWidget);
    mainLayout->addWidget(mButtonBox);
99

100 101 102 103 104 105 106 107 108 109
    // 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
110
    _pageNeedsUpdate.resize(_ui->tabWidget->count());
Laurent Montel's avatar
Laurent Montel committed
111
    connect(_ui->tabWidget, &QTabWidget::currentChanged, this, &Konsole::EditProfileDialog::preparePage);
112

113
    createTempProfile();
114
}
115
EditProfileDialog::~EditProfileDialog()
116 117
{
    delete _ui;
118
}
119
void EditProfileDialog::save()
120
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
121
    if (_tempProfile->isEmpty())
122
        return;
123

124
    ProfileManager::instance()->changeProfile(_profile, _tempProfile->setProperties());
125 126 127

    // ensure that these settings are not undone by a call
    // to unpreview()
Kurt Hindenburg's avatar
Kurt Hindenburg committed
128 129
    QHashIterator<Profile::Property, QVariant> iter(_tempProfile->setProperties());
    while (iter.hasNext()) {
130 131 132
        iter.next();
        _previewedProperties.remove(iter.key());
    }
133

134 135
    createTempProfile();

Laurent Montel's avatar
Laurent Montel committed
136
    mButtonBox->button(QDialogButtonBox::Apply)->setEnabled(false);
137 138 139 140
}
void EditProfileDialog::reject()
{
    unpreviewAll();
Laurent Montel's avatar
Laurent Montel committed
141
    QDialog::reject();
142 143 144
}
void EditProfileDialog::accept()
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
145 146
    Q_ASSERT(_profile);
    Q_ASSERT(_tempProfile);
147

Kurt Hindenburg's avatar
Kurt Hindenburg committed
148 149 150
    if ((_tempProfile->isPropertySet(Profile::Name) &&
            _tempProfile->name().isEmpty())
            || (_profile->name().isEmpty() && _tempProfile->name().isEmpty())) {
151
        KMessageBox::sorry(this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
152 153
                           i18n("<p>Each profile must have a name before it can be saved "
                                "into disk.</p>"));
154 155
        return;
    }
156
    save();
157
    unpreviewAll();
Laurent Montel's avatar
Laurent Montel committed
158
    QDialog::accept();
159
}
160
QString EditProfileDialog::groupProfileNames(const ProfileGroup::Ptr group, int maxLength)
Robert Knight's avatar
 
Robert Knight committed
161
{
162 163
    QString caption;
    int count = group->profiles().count();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
164
    for (int i = 0; i < count; i++) {
165
        caption += group->profiles()[i]->name();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
166
        if (i < (count - 1)) {
167 168
            caption += ',';
            // limit caption length to prevent very long window titles
Kurt Hindenburg's avatar
Kurt Hindenburg committed
169
            if (maxLength > 0 && caption.length() > maxLength) {
170 171 172 173 174 175 176 177 178 179
                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
180 181 182
    ProfileGroup::Ptr group = profile->asGroup();
    if (group && group->profiles().count() > 1) {
        QString caption = groupProfileNames(group, MAX_GROUP_CAPTION_LENGTH);
Laurent Montel's avatar
Laurent Montel committed
183
        setWindowTitle(i18np("Editing profile: %2",
Kurt Hindenburg's avatar
Kurt Hindenburg committed
184 185 186 187
                         "Editing %1 profiles: %2",
                         group->profiles().count(),
                         caption));
    } else {
Laurent Montel's avatar
Laurent Montel committed
188
        setWindowTitle(i18n("Edit Profile \"%1\"", profile->name()));
189
    }
Robert Knight's avatar
 
Robert Knight committed
190
}
191
void EditProfileDialog::setProfile(Profile::Ptr profile)
192
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
193
    Q_ASSERT(profile);
194

195 196
    _profile = profile;

197
    // update caption
198
    updateCaption(profile);
199

200 201 202 203 204
    // 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
205
    preparePage(_ui->tabWidget->currentIndex());
206

Kurt Hindenburg's avatar
Kurt Hindenburg committed
207
    if (_tempProfile) {
208
        createTempProfile();
209
    }
210
}
211
const Profile::Ptr EditProfileDialog::lookupProfile() const
212
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
213
    return _profile;
214
}
Robert Knight's avatar
 
Robert Knight committed
215
void EditProfileDialog::preparePage(int page)
216
{
Jekyll Wu's avatar
Jekyll Wu committed
217
    const Profile::Ptr profile = lookupProfile();
218

Kurt Hindenburg's avatar
Kurt Hindenburg committed
219
    Q_ASSERT(_pageNeedsUpdate.count() > page);
Jekyll Wu's avatar
Jekyll Wu committed
220
    Q_ASSERT(profile);
221

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
224 225
    if (_pageNeedsUpdate[page]) {
        if (pageWidget == _ui->generalTab)
Jekyll Wu's avatar
Jekyll Wu committed
226
            setupGeneralPage(profile);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
227
        else if (pageWidget == _ui->tabsTab)
Jekyll Wu's avatar
Jekyll Wu committed
228
            setupTabsPage(profile);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
229
        else if (pageWidget == _ui->appearanceTab)
Jekyll Wu's avatar
Jekyll Wu committed
230
            setupAppearancePage(profile);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
231
        else if (pageWidget == _ui->scrollingTab)
Jekyll Wu's avatar
Jekyll Wu committed
232
            setupScrollingPage(profile);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
233
        else if (pageWidget == _ui->keyboardTab)
Jekyll Wu's avatar
Jekyll Wu committed
234
            setupKeyboardPage(profile);
235 236
        else if (pageWidget == _ui->mouseTab)
            setupMousePage(profile);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
237
        else if (pageWidget == _ui->advancedTab)
Jekyll Wu's avatar
Jekyll Wu committed
238
            setupAdvancedPage(profile);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
239 240
        else
            Q_ASSERT(false);
241

242
        _pageNeedsUpdate[page] = false;
243
    }
Robert Knight's avatar
 
Robert Knight committed
244 245 246 247
}
void EditProfileDialog::selectProfileName()
{
    _ui->profileNameEdit->setFocus();
248
    _ui->profileNameEdit->selectAll();
249
}
Jekyll Wu's avatar
Jekyll Wu committed
250
void EditProfileDialog::setupGeneralPage(const Profile::Ptr profile)
251
{
252
    // basic profile options
253
    {
254 255 256 257
        _ui->emptyNameWarningWidget->setWordWrap(false);
        _ui->emptyNameWarningWidget->setCloseButtonVisible(false);
        _ui->emptyNameWarningWidget->setMessageType(KMessageWidget::Warning);

Jekyll Wu's avatar
Jekyll Wu committed
258
        ProfileGroup::Ptr group = profile->asGroup();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
259
        if (!group || group->profiles().count() < 2) {
Jekyll Wu's avatar
Jekyll Wu committed
260
            _ui->profileNameEdit->setText(profile->name());
Kurt Hindenburg's avatar
Kurt Hindenburg committed
261
            _ui->profileNameEdit->setClearButtonEnabled(true);
262

Jekyll Wu's avatar
Jekyll Wu committed
263
            _ui->emptyNameWarningWidget->setVisible(profile->name().isEmpty());
264
            _ui->emptyNameWarningWidget->setText(i18n("Profile name is empty."));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
265 266
        } else {
            _ui->profileNameEdit->setText(groupProfileNames(group, -1));
267
            _ui->profileNameEdit->setEnabled(false);
268
            _ui->profileNameLabel->setEnabled(false);
269 270

            _ui->emptyNameWarningWidget->setVisible(false);
271 272
        }
    }
273

Jekyll Wu's avatar
Jekyll Wu committed
274
    ShellCommand command(profile->command() , profile->arguments());
Kurt Hindenburg's avatar
Kurt Hindenburg committed
275
    _ui->commandEdit->setText(command.fullCommand());
276

Kurt Hindenburg's avatar
Kurt Hindenburg committed
277 278 279 280 281
#pragma message("Look at this setCompletionObject again")
//    KUrlCompletion* exeCompletion = new KUrlCompletion(KUrlCompletion::ExeCompletion);
//    exeCompletion->setParent(this);
//    exeCompletion->setDir(QUrl());
//    _ui->commandEdit->setCompletionObject(exeCompletion);
282

283
/* The below causes a noticeable delay when opening the dialog - I'm not entirely sure
284 285 286 287 288 289 290 291 292
   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);
Michal Humpula's avatar
Michal Humpula committed
293
    commandEditCompleter->setModel(commandEditDirModel);
294 295 296 297 298
    _ui->commandEdit->setCompleter(commandEditCompleter);

    QFileSystemModel *initialEditDirModel = new QFileSystemModel(this);
    initialEditDirModel->setFilter(QDir::AllEntries);
    initialEditDirModel->setRootPath(QString('/'));
Jekyll Wu's avatar
Jekyll Wu committed
299
    _ui->initialDirEdit->setText(profile->defaultWorkingDirectory());
300
    QCompleter *initialDirCompleter = new QCompleter(this);
Michal Humpula's avatar
Michal Humpula committed
301
    initialDirCompleter->setModel(initialEditDirModel);
302 303
    _ui->initialDirEdit->setCompleter(initialDirCompleter);
*/
Kurt Hindenburg's avatar
Kurt Hindenburg committed
304
    _ui->initialDirEdit->setClearButtonEnabled(true);
305

Michal Humpula's avatar
Michal Humpula committed
306 307
    _ui->dirSelectButton->setIcon(QIcon::fromTheme(QStringLiteral("folder-open")));
    _ui->iconSelectButton->setIcon(QIcon::fromTheme(profile->icon()));
308
    _ui->startInSameDirButton->setChecked(profile->startInCurrentSessionDir());
309

310 311 312 313
    // terminal options
    _ui->terminalColumnsEntry->setValue(profile->terminalColumns());
    _ui->terminalRowsEntry->setValue(profile->terminalRows());

314
    // window options
315
    _ui->showTerminalSizeHintButton->setChecked(profile->showTerminalSizeHint());
316 317

    // signals and slots
318 319
    connect(_ui->dirSelectButton, &QToolButton::clicked, this, &Konsole::EditProfileDialog::selectInitialDir);
    connect(_ui->iconSelectButton, &QPushButton::clicked, this, &Konsole::EditProfileDialog::selectIcon);
Laurent Montel's avatar
Laurent Montel committed
320 321 322 323 324 325
    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);

Laurent Montel's avatar
Laurent Montel committed
326 327
    connect(_ui->terminalColumnsEntry, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &Konsole::EditProfileDialog::terminalColumnsEntryChanged);
    connect(_ui->terminalRowsEntry, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &Konsole::EditProfileDialog::terminalRowsEntryChanged);
Laurent Montel's avatar
Laurent Montel committed
328 329

    connect(_ui->showTerminalSizeHintButton, &QCheckBox::toggled, this, &Konsole::EditProfileDialog::showTerminalSizeHint);
330 331 332
}
void EditProfileDialog::showEnvironmentEditor()
{
333
    bool ok;
Jekyll Wu's avatar
Jekyll Wu committed
334
    const Profile::Ptr profile = lookupProfile();
335
    QStringList currentEnvironment = profile->environment();
336

337 338 339 340 341
    QString text = QInputDialog::getMultiLineText(this, 
            i18n("Edit Environment"),
            i18n("One environment variable per line"),
            currentEnvironment.join("\n"),
            &ok);
342

343 344
    if (ok && !text.isEmpty()) {
        QStringList newEnvironment = text.split('\n');
Kurt Hindenburg's avatar
Kurt Hindenburg committed
345
        updateTempProfileProperty(Profile::Environment, newEnvironment);
346 347
    }
}
Jekyll Wu's avatar
Jekyll Wu committed
348
void EditProfileDialog::setupTabsPage(const Profile::Ptr profile)
349
{
350
    // tab title format
351 352 353
    _ui->renameTabWidget->setTabTitleText(profile->localTabTitleFormat());
    _ui->renameTabWidget->setRemoteTabTitleText(profile->remoteTabTitleFormat());

Laurent Montel's avatar
Laurent Montel committed
354 355
    connect(_ui->renameTabWidget, &Konsole::RenameTabWidget::tabTitleFormatChanged, this, &Konsole::EditProfileDialog::tabTitleFormatChanged);
    connect(_ui->renameTabWidget, &Konsole::RenameTabWidget::remoteTabTitleFormatChanged, this, &Konsole::EditProfileDialog::remoteTabTitleFormatChanged);
356

Jekyll Wu's avatar
Jekyll Wu committed
357
    // tab monitoring
Jekyll Wu's avatar
Jekyll Wu committed
358
    const int silenceSeconds = profile->silenceSeconds();
Jekyll Wu's avatar
Jekyll Wu committed
359
    _ui->silenceSecondsSpinner->setValue(silenceSeconds);
360
    _ui->silenceSecondsSpinner->setSuffix(ki18ncp("Unit of time", " second", " seconds"));
Jekyll Wu's avatar
Jekyll Wu committed
361

Laurent Montel's avatar
Laurent Montel committed
362
    connect(_ui->silenceSecondsSpinner, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &Konsole::EditProfileDialog::silenceSecondsChanged);
363
}
364

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

386 387 388 389 390
void EditProfileDialog::silenceSecondsChanged(int seconds)
{
    updateTempProfileProperty(Profile::SilenceSeconds, seconds);
}

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
420 421
    updateTempProfileProperty(Profile::Command, shellCommand.command());
    updateTempProfileProperty(Profile::Arguments, shellCommand.arguments());
422 423 424
}
void EditProfileDialog::selectInitialDir()
{
425 426 427
    const QUrl url = QFileDialog::getExistingDirectoryUrl(this,
                     i18n("Select Initial Directory"),
                     QUrl::fromUserInput(_ui->initialDirEdit->text()));
428

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

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

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

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

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

Laurent Montel's avatar
Laurent Montel committed
452 453
    connect(_ui->colorSchemeList->selectionModel(), &QItemSelectionModel::selectionChanged, this, &Konsole::EditProfileDialog::colorSchemeSelected);
    connect(_ui->colorSchemeList, &QListView::entered, this, &Konsole::EditProfileDialog::previewColorScheme);
454

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

Laurent Montel's avatar
Laurent Montel committed
457 458 459
    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);
460

461
    // setup font preview
Jekyll Wu's avatar
Jekyll Wu committed
462
    const bool antialias = profile->antiAliasFonts();
463

464 465
    QFont profileFont = profile->font();
    profileFont.setStyleStrategy(antialias ? QFont::PreferAntialias : QFont::NoAntialias);
466

467
    _ui->fontPreviewLabel->installEventFilter(this);
468
    _ui->fontPreviewLabel->setFont(profileFont);
469
    setFontInputValue(profileFont);
470

Laurent Montel's avatar
Laurent Montel committed
471
    connect(_ui->fontSizeInput, SIGNAL(valueChanged(double)), this, SLOT(setFontSize(double)));
Laurent Montel's avatar
Laurent Montel committed
472
    connect(_ui->selectFontButton, &QPushButton::clicked, this, &Konsole::EditProfileDialog::showFontDialog);
473

Kurt Hindenburg's avatar
Kurt Hindenburg committed
474
    // setup font smoothing
475
    _ui->antialiasTextButton->setChecked(antialias);
Laurent Montel's avatar
Laurent Montel committed
476
    connect(_ui->antialiasTextButton, &QCheckBox::toggled, this, &Konsole::EditProfileDialog::setAntialiasText);
477

Jekyll Wu's avatar
Jekyll Wu committed
478
    _ui->boldIntenseButton->setChecked(profile->boldIntense());
Laurent Montel's avatar
Laurent Montel committed
479
    connect(_ui->boldIntenseButton, &QCheckBox::toggled, this, &Konsole::EditProfileDialog::setBoldIntense);
480
    _ui->enableMouseWheelZoomButton->setChecked(profile->mouseWheelZoomEnabled());
Laurent Montel's avatar
Laurent Montel committed
481
    connect(_ui->enableMouseWheelZoomButton, &QCheckBox::toggled, this, &Konsole::EditProfileDialog::toggleMouseWheelZoom);
482 483 484
}
void EditProfileDialog::setAntialiasText(bool enable)
{
485 486
    QFont profileFont = _ui->fontPreviewLabel->font();
    profileFont.setStyleStrategy(enable ? QFont::PreferAntialias : QFont::NoAntialias);
487

488
    // update preview to reflect text smoothing state
489
    fontSelected(profileFont);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
490
    updateTempProfileProperty(Profile::AntiAliasFonts, enable);
491
}
492 493
void EditProfileDialog::setBoldIntense(bool enable)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
494 495
    preview(Profile::BoldIntense, enable);
    updateTempProfileProperty(Profile::BoldIntense, enable);
496
}
497 498 499 500
void EditProfileDialog::toggleMouseWheelZoom(bool enable)
{
    updateTempProfileProperty(Profile::MouseWheelZoomEnabled, enable);
}
501
void EditProfileDialog::updateColorSchemeList(bool selectCurrentScheme)
502
{
Robert Knight's avatar
 
Robert Knight committed
503 504
    if (!_ui->colorSchemeList->model())
        _ui->colorSchemeList->setModel(new QStandardItemModel(this));
505

506
    const QString& name = lookupProfile()->colorScheme();
507 508
    const ColorScheme* currentScheme = ColorSchemeManager::instance()->findColorScheme(name);

Robert Knight's avatar
 
Robert Knight committed
509 510 511
    QStandardItemModel* model = qobject_cast<QStandardItemModel*>(_ui->colorSchemeList->model());

    Q_ASSERT(model);
512

Robert Knight's avatar
 
Robert Knight committed
513 514
    model->clear();

515 516
    QStandardItem* selectedItem = 0;

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

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

524
        if (currentScheme == scheme)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
525
            selectedItem = item;
Robert Knight's avatar
 
Robert Knight committed
526

527 528 529
        model->appendRow(item);
    }

530 531
    model->sort(0);

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

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

546
    const QString& name = lookupProfile()->keyBindings();
547

548
    KeyboardTranslatorManager* keyManager = KeyboardTranslatorManager::instance();
549
    const KeyboardTranslator* currentTranslator = keyManager->findTranslator(name);
550

Robert Knight's avatar
 
Robert Knight committed
551 552 553 554 555
    QStandardItemModel* model = qobject_cast<QStandardItemModel*>(_ui->keyBindingList->model());

    Q_ASSERT(model);

    model->clear();
556

557 558
    QStandardItem* selectedItem = 0;

Jekyll Wu's avatar
Jekyll Wu committed
559
    QStringList translatorNames = keyManager->allTranslators();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
560
    foreach(const QString& translatorName, translatorNames) {
561
        const KeyboardTranslator* translator = keyManager->findTranslator(translatorName);
Laurent Montel's avatar
Laurent Montel committed
562
        if (!translator) continue;
563 564

        QStandardItem* item = new QStandardItem(translator->description());
565
        item->setEditable(false);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
566
        item->setData(QVariant::fromValue(translator), Qt::UserRole + 1);
Michal Humpula's avatar
Michal Humpula committed
567
        item->setIcon(QIcon::fromTheme(QStringLiteral("preferences-desktop-keyboard")));
568

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

        model->appendRow(item);
    }

575
    model->sort(0);
576

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

Laurent Montel's avatar
Laurent Montel committed
595
    return QDialog::eventFilter(watched, aEvent);
596
}
597
void EditProfileDialog::unpreviewAll()
598
{
599 600 601
    _delayedPreviewTimer->stop();
    _delayedPreviewProperties.clear();

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

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

617
    if (!_previewedProperties.contains(aProperty))
618
        return;
619

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

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

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

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

648
    _delayedPreviewProperties.remove(aProperty);
649

650
    const Profile::Ptr original = lookupProfile();
651

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

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

    // temporary change to color scheme
666
    ProfileManager::instance()->changeProfile(_profile , map , false);
667 668