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

39
// KDE
40 41 42
#include <kdeversion.h>
#include <KCodecAction>

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

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

65 66
using namespace Konsole;

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

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

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

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

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

85 86 87 88 89 90 91 92 93 94
    // 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
95
    _pageNeedsUpdate.resize(_ui->tabWidget->count());
Laurent Montel's avatar
Laurent Montel committed
96
    connect(_ui->tabWidget, &QTabWidget::currentChanged, this, &Konsole::EditProfileDialog::preparePage);
97

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

109
    ProfileManager::instance()->changeProfile(_profile, _tempProfile->setProperties());
110 111 112

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

119 120
    createTempProfile();

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

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

180 181
    _profile = profile;

182
    // update caption
183
    updateCaption(profile);
184

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

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

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

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

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

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

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

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

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

Jekyll Wu's avatar
Jekyll Wu committed
259
    ShellCommand command(profile->command() , profile->arguments());
Kurt Hindenburg's avatar
Kurt Hindenburg committed
260
    _ui->commandEdit->setText(command.fullCommand());
261

Kurt Hindenburg's avatar
Kurt Hindenburg committed
262 263 264 265 266
#pragma message("Look at this setCompletionObject again")
//    KUrlCompletion* exeCompletion = new KUrlCompletion(KUrlCompletion::ExeCompletion);
//    exeCompletion->setParent(this);
//    exeCompletion->setDir(QUrl());
//    _ui->commandEdit->setCompletionObject(exeCompletion);
267

268
/* The below causes a noticeable delay when opening the dialog - I'm not entirely sure
269 270 271 272 273 274 275 276 277
   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
278
    commandEditCompleter->setModel(commandEditDirModel);
279 280 281 282 283
    _ui->commandEdit->setCompleter(commandEditCompleter);

    QFileSystemModel *initialEditDirModel = new QFileSystemModel(this);
    initialEditDirModel->setFilter(QDir::AllEntries);
    initialEditDirModel->setRootPath(QString('/'));
Jekyll Wu's avatar
Jekyll Wu committed
284
    _ui->initialDirEdit->setText(profile->defaultWorkingDirectory());
285
    QCompleter *initialDirCompleter = new QCompleter(this);
Michal Humpula's avatar
Michal Humpula committed
286
    initialDirCompleter->setModel(initialEditDirModel);
287 288
    _ui->initialDirEdit->setCompleter(initialDirCompleter);
*/
Kurt Hindenburg's avatar
Kurt Hindenburg committed
289
    _ui->initialDirEdit->setClearButtonEnabled(true);
290

Michal Humpula's avatar
Michal Humpula committed
291 292
    _ui->dirSelectButton->setIcon(QIcon::fromTheme(QStringLiteral("folder-open")));
    _ui->iconSelectButton->setIcon(QIcon::fromTheme(profile->icon()));
293
    _ui->startInSameDirButton->setChecked(profile->startInCurrentSessionDir());
294

295 296 297 298
    // terminal options
    _ui->terminalColumnsEntry->setValue(profile->terminalColumns());
    _ui->terminalRowsEntry->setValue(profile->terminalRows());

299
    // window options
300
    _ui->showTerminalSizeHintButton->setChecked(profile->showTerminalSizeHint());
301 302

    // signals and slots
303 304
    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
305 306 307 308 309 310 311 312 313 314
    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);
315 316 317
}
void EditProfileDialog::showEnvironmentEditor()
{
Jekyll Wu's avatar
Jekyll Wu committed
318
    const Profile::Ptr profile = lookupProfile();
319

Kurt Hindenburg's avatar
Kurt Hindenburg committed
320 321
    QWeakPointer<KDialog> dialog = new KDialog(this);
    KTextEdit* edit = new KTextEdit(dialog.data());
322

323
    QStringList currentEnvironment = profile->environment();
324

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
328 329
    dialog.data()->setPlainCaption(i18n("Edit Environment"));
    dialog.data()->setMainWidget(edit);
330

Kurt Hindenburg's avatar
Kurt Hindenburg committed
331
    if (dialog.data()->exec() == QDialog::Accepted) {
332
        QStringList newEnvironment = edit->toPlainText().split('\n');
Kurt Hindenburg's avatar
Kurt Hindenburg committed
333
        updateTempProfileProperty(Profile::Environment, newEnvironment);
334 335
    }

Kurt Hindenburg's avatar
Kurt Hindenburg committed
336
    delete dialog.data();
337
}
Jekyll Wu's avatar
Jekyll Wu committed
338
void EditProfileDialog::setupTabsPage(const Profile::Ptr profile)
339
{
340
    // tab title format
341 342 343
    _ui->renameTabWidget->setTabTitleText(profile->localTabTitleFormat());
    _ui->renameTabWidget->setRemoteTabTitleText(profile->remoteTabTitleFormat());

Laurent Montel's avatar
Laurent Montel committed
344 345
    connect(_ui->renameTabWidget, &Konsole::RenameTabWidget::tabTitleFormatChanged, this, &Konsole::EditProfileDialog::tabTitleFormatChanged);
    connect(_ui->renameTabWidget, &Konsole::RenameTabWidget::remoteTabTitleFormatChanged, this, &Konsole::EditProfileDialog::remoteTabTitleFormatChanged);
346

Jekyll Wu's avatar
Jekyll Wu committed
347
    // tab monitoring
Jekyll Wu's avatar
Jekyll Wu committed
348
    const int silenceSeconds = profile->silenceSeconds();
Jekyll Wu's avatar
Jekyll Wu committed
349
    _ui->silenceSecondsSpinner->setValue(silenceSeconds);
350
    _ui->silenceSecondsSpinner->setSuffix(ki18ncp("Unit of time", " second", " seconds"));
Jekyll Wu's avatar
Jekyll Wu committed
351

Laurent Montel's avatar
Laurent Montel committed
352
    connect(_ui->silenceSecondsSpinner, static_cast<void(KIntSpinBox::*)(int)>(&KIntSpinBox::valueChanged), this, &Konsole::EditProfileDialog::silenceSecondsChanged);
353
}
354

355 356 357 358 359
void EditProfileDialog::terminalColumnsEntryChanged(int value)
{
    updateTempProfileProperty(Profile::TerminalColumns, value);
}
void EditProfileDialog::terminalRowsEntryChanged(int value)
360
{
361
    updateTempProfileProperty(Profile::TerminalRows, value);
362
}
363
void EditProfileDialog::showTerminalSizeHint(bool value)
364
{
365
    updateTempProfileProperty(Profile::ShowTerminalSizeHint, value);
366
}
367 368
void EditProfileDialog::tabTitleFormatChanged(const QString& format)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
369
    updateTempProfileProperty(Profile::LocalTabTitleFormat, format);
370 371 372
}
void EditProfileDialog::remoteTabTitleFormatChanged(const QString& format)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
373
    updateTempProfileProperty(Profile::RemoteTabTitleFormat, format);
374 375
}

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

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

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

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

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

Robert Knight's avatar
 
Robert Knight committed
427
    _ui->transparencyWarningWidget->setVisible(false);
428 429 430
    _ui->transparencyWarningWidget->setWordWrap(true);
    _ui->transparencyWarningWidget->setCloseButtonVisible(false);
    _ui->transparencyWarningWidget->setMessageType(KMessageWidget::Warning);
431

Robert Knight's avatar
 
Robert Knight committed
432 433 434
    _ui->editColorSchemeButton->setEnabled(false);
    _ui->removeColorSchemeButton->setEnabled(false);

435 436
    // setup color list
    updateColorSchemeList(true);
437

438 439
    _ui->colorSchemeList->setMouseTracking(true);
    _ui->colorSchemeList->installEventFilter(this);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
440
    _ui->colorSchemeList->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
441

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

Robert Knight's avatar
 
Robert Knight committed
445
    updateColorSchemeButtons();
446

Laurent Montel's avatar
Laurent Montel committed
447 448 449
    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);
450

451
    // setup font preview
Jekyll Wu's avatar
Jekyll Wu committed
452
    const bool antialias = profile->antiAliasFonts();
453

454 455
    QFont profileFont = profile->font();
    profileFont.setStyleStrategy(antialias ? QFont::PreferAntialias : QFont::NoAntialias);
456

457
    _ui->fontPreviewLabel->installEventFilter(this);
458
    _ui->fontPreviewLabel->setFont(profileFont);
459
    setFontInputValue(profileFont);
460

Laurent Montel's avatar
Laurent Montel committed
461 462
    connect(_ui->fontSizeInput, &KDoubleNumInput::valueChanged, this, &Konsole::EditProfileDialog::setFontSize);
    connect(_ui->selectFontButton, &QPushButton::clicked, this, &Konsole::EditProfileDialog::showFontDialog);
463

Kurt Hindenburg's avatar
Kurt Hindenburg committed
464
    // setup font smoothing
465
    _ui->antialiasTextButton->setChecked(antialias);
Laurent Montel's avatar
Laurent Montel committed
466
    connect(_ui->antialiasTextButton, &QCheckBox::toggled, this, &Konsole::EditProfileDialog::setAntialiasText);
467

Jekyll Wu's avatar
Jekyll Wu committed
468
    _ui->boldIntenseButton->setChecked(profile->boldIntense());
Laurent Montel's avatar
Laurent Montel committed
469
    connect(_ui->boldIntenseButton, &QCheckBox::toggled, this, &Konsole::EditProfileDialog::setBoldIntense);
470
    _ui->enableMouseWheelZoomButton->setChecked(profile->mouseWheelZoomEnabled());
Laurent Montel's avatar
Laurent Montel committed
471
    connect(_ui->enableMouseWheelZoomButton, &QCheckBox::toggled, this, &Konsole::EditProfileDialog::toggleMouseWheelZoom);
472 473 474
}
void EditProfileDialog::setAntialiasText(bool enable)
{
475 476
    QFont profileFont = _ui->fontPreviewLabel->font();
    profileFont.setStyleStrategy(enable ? QFont::PreferAntialias : QFont::NoAntialias);
477

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

496
    const QString& name = lookupProfile()->colorScheme();
497 498
    const ColorScheme* currentScheme = ColorSchemeManager::instance()->findColorScheme(name);

Robert Knight's avatar
 
Robert Knight committed
499 500 501
    QStandardItemModel* model = qobject_cast<QStandardItemModel*>(_ui->colorSchemeList->model());

    Q_ASSERT(model);
502

Robert Knight's avatar
 
Robert Knight committed
503 504
    model->clear();

505 506
    QStandardItem* selectedItem = 0;

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

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

514
        if (currentScheme == scheme)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
515
            selectedItem = item;
Robert Knight's avatar
 
Robert Knight committed
516

517 518 519
        model->appendRow(item);
    }

520 521
    model->sort(0);

Kurt Hindenburg's avatar
Kurt Hindenburg committed
522
    if (selectCurrentScheme && selectedItem) {
523
        _ui->colorSchemeList->updateGeometry();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
524 525
        _ui->colorSchemeList->selectionModel()->setCurrentIndex(selectedItem->index() ,
                QItemSelectionModel::Select);
Robert Knight's avatar
 
Robert Knight committed
526 527 528

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

536
    const QString& name = lookupProfile()->keyBindings();
537

538
    KeyboardTranslatorManager* keyManager = KeyboardTranslatorManager::instance();
539
    const KeyboardTranslator* currentTranslator = keyManager->findTranslator(name);
540

Robert Knight's avatar
 
Robert Knight committed
541 542 543 544 545
    QStandardItemModel* model = qobject_cast<QStandardItemModel*>(_ui->keyBindingList->model());

    Q_ASSERT(model);

    model->clear();
546

547 548
    QStandardItem* selectedItem = 0;

Jekyll Wu's avatar
Jekyll Wu committed
549
    QStringList translatorNames = keyManager->allTranslators();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
550
    foreach(const QString& translatorName, translatorNames) {
551
        const KeyboardTranslator* translator = keyManager->findTranslator(translatorName);
552 553

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
558
        if (translator == currentTranslator)
559
            selectedItem = item;
560 561 562 563

        model->appendRow(item);
    }

564
    model->sort(0);
565

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

584
    return KDialog::eventFilter(watched, aEvent);
585
}
586
void EditProfileDialog::unpreviewAll()
587
{
588 589 590
    _delayedPreviewTimer->stop();
    _delayedPreviewProperties.clear();

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

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

606
    if (!_previewedProperties.contains(aProperty))
607
        return;
608

Kurt Hindenburg's avatar
Kurt Hindenburg committed
609
    QHash<Profile::Property, QVariant> map;
610
    map.insert((Profile::Property)aProperty, _previewedProperties[aProperty]);
611
    ProfileManager::instance()->changeProfile(_profile, map, false);
612

613
    _previewedProperties.remove(aProperty);
614
}
615
void EditProfileDialog::delayedPreview(int aProperty , const QVariant& value)
616
{
617
    _delayedPreviewProperties.insert(aProperty, value);
618 619

    _delayedPreviewTimer->stop();
620
    _delayedPreviewTimer->start(300);
621 622 623
}
void EditProfileDialog::delayedPreviewActivate()
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
624
    Q_ASSERT(qobject_cast<QTimer*>(sender()));
625

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

637
    _delayedPreviewProperties.remove(aProperty);
638

639
    const Profile::Ptr original = lookupProfile();
640

641 642 643 644 645 646
    // 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 &&
647
            original->property<QVariant>((Profile::Property)aProperty).isNull())
648 649
        return;

650 651
    if (!_previewedProperties.contains(aProperty)) {
        _previewedProperties.insert(aProperty , original->property<QVariant>((Profile::Property)aProperty));
652
    }
653 654

    // temporary change to color scheme
655
    ProfileManager::instance()->changeProfile(_profile , map , false);
656 657 658
}
void EditProfileDialog::previewColorScheme(const QModelIndex& index)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
659
    const QString& name = index.data(Qt::UserRole + 1).value<const ColorScheme*>()->name();
660

Kurt Hindenburg's avatar
Kurt Hindenburg committed
661
    delayedPreview(Profile::ColorScheme , name);
662
}
663