EditProfileDialog.cpp 48.7 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>
Laurent Montel's avatar
Laurent Montel committed
39
#include <QDebug>
40
// KDE
41 42
#include <KCodecAction>

43
#include <KDialog>
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>
Laurent Montel's avatar
Laurent Montel committed
51 52 53 54
#include <KConfigGroup>
#include <QDialogButtonBox>
#include <QPushButton>
#include <QVBoxLayout>
55 56 57

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

69 70
using namespace Konsole;

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

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

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

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

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

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

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

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

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

133 134
    createTempProfile();

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

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

194 195
    _profile = profile;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Laurent Montel's avatar
Laurent Montel committed
358 359
    connect(_ui->renameTabWidget, &Konsole::RenameTabWidget::tabTitleFormatChanged, this, &Konsole::EditProfileDialog::tabTitleFormatChanged);
    connect(_ui->renameTabWidget, &Konsole::RenameTabWidget::remoteTabTitleFormatChanged, this, &Konsole::EditProfileDialog::remoteTabTitleFormatChanged);
360

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

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

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

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

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

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

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

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

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

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

449 450
    // setup color list
    updateColorSchemeList(true);
451

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

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

Robert Knight's avatar
 
Robert Knight committed
459
    updateColorSchemeButtons();
460

Laurent Montel's avatar
Laurent Montel committed
461 462 463
    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);
464

465
    // setup font preview
Jekyll Wu's avatar
Jekyll Wu committed
466
    const bool antialias = profile->antiAliasFonts();
467

468 469
    QFont profileFont = profile->font();
    profileFont.setStyleStrategy(antialias ? QFont::PreferAntialias : QFont::NoAntialias);
470

471
    _ui->fontPreviewLabel->installEventFilter(this);
472
    _ui->fontPreviewLabel->setFont(profileFont);
473
    setFontInputValue(profileFont);
474

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

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

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

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

510
    const QString& name = lookupProfile()->colorScheme();
511 512
    const ColorScheme* currentScheme = ColorSchemeManager::instance()->findColorScheme(name);

Robert Knight's avatar
 
Robert Knight committed
513 514 515
    QStandardItemModel* model = qobject_cast<QStandardItemModel*>(_ui->colorSchemeList->model());

    Q_ASSERT(model);
516

Robert Knight's avatar
 
Robert Knight committed
517 518
    model->clear();

519 520
    QStandardItem* selectedItem = 0;

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

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

528
        if (currentScheme == scheme)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
529
            selectedItem = item;
Robert Knight's avatar
 
Robert Knight committed
530

531 532 533
        model->appendRow(item);
    }

534 535
    model->sort(0);

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

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

550
    const QString& name = lookupProfile()->keyBindings();
551

552
    KeyboardTranslatorManager* keyManager = KeyboardTranslatorManager::instance();
553
    const KeyboardTranslator* currentTranslator = keyManager->findTranslator(name);
554

Robert Knight's avatar
 
Robert Knight committed
555 556 557 558 559
    QStandardItemModel* model = qobject_cast<QStandardItemModel*>(_ui->keyBindingList->model());

    Q_ASSERT(model);

    model->clear();
560

561 562
    QStandardItem* selectedItem = 0;

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
573
        if (translator == currentTranslator)
574
            selectedItem = item;
575 576 577 578

        model->appendRow(item);
    }

579
    model->sort(0);
580

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

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

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

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

621
    if (!_previewedProperties.contains(aProperty))
622
        return;
623

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

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

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

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

652
    _delayedPreviewProperties.remove(aProperty);
653

654
    const Profile::Ptr original = lookupProfile();
655

656 657 658 659 660 661
    // 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 &&