EditProfileDialog.cpp 48.6 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 <KDialog>
47
#include <KIconDialog>
48
#include <KUrlCompletion>
49
#include <KWindowSystem>
Rick Xing's avatar
Rick Xing committed
50
#include <KTextEdit>
51
#include <KMessageBox>
52
#include <KLocalizedString>
Laurent Montel's avatar
Laurent Montel committed
53 54 55 56
#include <KConfigGroup>
#include <QDialogButtonBox>
#include <QPushButton>
#include <QVBoxLayout>
57 58 59

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

71 72
using namespace Konsole;

73
EditProfileDialog::EditProfileDialog(QWidget* aParent)
Laurent Montel's avatar
Laurent Montel committed
74
    : QDialog(aParent)
75
    , _delayedPreviewTimer(new QTimer(this))
76
    , _colorDialog(0)
77
{
Laurent Montel's avatar
Laurent Montel committed
78 79 80 81 82 83 84 85 86 87 88
    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()));
89

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

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

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

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

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

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

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

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

135 136
    createTempProfile();

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

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

196 197
    _profile = profile;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    // signals and slots
319 320
    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
321 322 323 324 325 326
    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
327 328
    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
329 330

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    Q_ASSERT(model);
513

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

516 517
    QStandardItem* selectedItem = 0;

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

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

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

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

531 532
    model->sort(0);

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

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

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

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

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

    Q_ASSERT(model);

    model->clear();
557

558 559
    QStandardItem* selectedItem = 0;

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

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

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

        model->appendRow(item);
    }

576
    model->sort(0);
577

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

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

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

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

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

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

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

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

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

649
    _delayedPreviewProperties.remove(aProperty);
650

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

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

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

    // temporary change to color scheme