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

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

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

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

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

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

26
// Qt
Dirk Mueller's avatar
Dirk Mueller committed
27 28
#include <QtGui/QBrush>
#include <QtGui/QPainter>
29
#include <QStandardItem>
30
#include <QtCore/QTextCodec>
31 32
#include <QtGui/QLinearGradient>
#include <QtGui/QRadialGradient>
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>
42
// KDE
43 44
#include <KCodecAction>

45
#include <KIconDialog>
46
#include <KUrlCompletion>
47
#include <KWindowSystem>
48
#include <KMessageBox>
49
#include <KLocalizedString>
Laurent Montel's avatar
Laurent Montel committed
50 51 52
#include <QDialogButtonBox>
#include <QPushButton>
#include <QVBoxLayout>
53 54

// Konsole
55
#include "ColorSchemeManager.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

64 65
using namespace Konsole;

66
EditProfileDialog::EditProfileDialog(QWidget* aParent)
Laurent Montel's avatar
Laurent Montel committed
67
    : QDialog(aParent)
68
    , _delayedPreviewTimer(new QTimer(this))
69
    , _colorDialog(0)
70
{
Laurent Montel's avatar
Laurent Montel committed
71 72 73 74 75 76 77 78 79
    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);
80 81
    connect(mButtonBox, &QDialogButtonBox::accepted, this, &Konsole::EditProfileDialog::accept);
    connect(mButtonBox, &QDialogButtonBox::rejected, this, &Konsole::EditProfileDialog::reject);
82

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

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

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

90
    _ui = new Ui::EditProfileDialog();
Laurent Montel's avatar
Laurent Montel committed
91 92
    _ui->setupUi(mainWidget);
    mainLayout->addWidget(mButtonBox);
93

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

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

118
    ProfileManager::instance()->changeProfile(_profile, _tempProfile->setProperties());
119 120 121

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

128 129
    createTempProfile();

Laurent Montel's avatar
Laurent Montel committed
130
    mButtonBox->button(QDialogButtonBox::Apply)->setEnabled(false);
131 132 133 134
}
void EditProfileDialog::reject()
{
    unpreviewAll();
Laurent Montel's avatar
Laurent Montel committed
135
    QDialog::reject();
136 137 138
}
void EditProfileDialog::accept()
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
139 140
    Q_ASSERT(_profile);
    Q_ASSERT(_tempProfile);
141

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

189 190
    _profile = profile;

191
    // update caption
192
    updateCaption(profile);
193

194 195 196 197 198
    // 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
199
    preparePage(_ui->tabWidget->currentIndex());
200

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
213
    Q_ASSERT(_pageNeedsUpdate.count() > page);
Jekyll Wu's avatar
Jekyll Wu committed
214
    Q_ASSERT(profile);
215

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

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

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

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

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

            _ui->emptyNameWarningWidget->setVisible(false);
265 266
        }
    }
267

Jekyll Wu's avatar
Jekyll Wu committed
268
    ShellCommand command(profile->command() , profile->arguments());
Kurt Hindenburg's avatar
Kurt Hindenburg committed
269
    _ui->commandEdit->setText(command.fullCommand());
270

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

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

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

Michal Humpula's avatar
Michal Humpula committed
301 302
    _ui->dirSelectButton->setIcon(QIcon::fromTheme(QStringLiteral("folder-open")));
    _ui->iconSelectButton->setIcon(QIcon::fromTheme(profile->icon()));
303
    _ui->startInSameDirButton->setChecked(profile->startInCurrentSessionDir());
304

305 306 307 308
    // terminal options
    _ui->terminalColumnsEntry->setValue(profile->terminalColumns());
    _ui->terminalRowsEntry->setValue(profile->terminalRows());

309
    // window options
310
    _ui->showTerminalSizeHintButton->setChecked(profile->showTerminalSizeHint());
311 312

    // signals and slots
313 314
    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
315 316 317 318 319 320
    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
321 322
    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
323 324

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

332 333 334
    QString text = QInputDialog::getMultiLineText(this, 
            i18n("Edit Environment"),
            i18n("One environment variable per line"),
335
            currentEnvironment.join(QStringLiteral("\n")),
336
            &ok);
337

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

Laurent Montel's avatar
Laurent Montel committed
349 350
    connect(_ui->renameTabWidget, &Konsole::RenameTabWidget::tabTitleFormatChanged, this, &Konsole::EditProfileDialog::tabTitleFormatChanged);
    connect(_ui->renameTabWidget, &Konsole::RenameTabWidget::remoteTabTitleFormatChanged, this, &Konsole::EditProfileDialog::remoteTabTitleFormatChanged);
351

Jekyll Wu's avatar
Jekyll Wu committed
352
    // tab monitoring
Jekyll Wu's avatar
Jekyll Wu committed
353
    const int silenceSeconds = profile->silenceSeconds();
Jekyll Wu's avatar
Jekyll Wu committed
354
    _ui->silenceSecondsSpinner->setValue(silenceSeconds);
355
    _ui->silenceSecondsSpinner->setSuffix(ki18ncp("Unit of time", " second", " seconds"));
Jekyll Wu's avatar
Jekyll Wu committed
356

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

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

381 382 383 384 385
void EditProfileDialog::silenceSecondsChanged(int seconds)
{
    updateTempProfileProperty(Profile::SilenceSeconds, seconds);
}

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

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

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

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

Robert Knight's avatar
 
Robert Knight committed
432
    _ui->transparencyWarningWidget->setVisible(false);
433 434 435
    _ui->transparencyWarningWidget->setWordWrap(true);
    _ui->transparencyWarningWidget->setCloseButtonVisible(false);
    _ui->transparencyWarningWidget->setMessageType(KMessageWidget::Warning);
436

Robert Knight's avatar
 
Robert Knight committed
437 438 439
    _ui->editColorSchemeButton->setEnabled(false);
    _ui->removeColorSchemeButton->setEnabled(false);

440 441
    // setup color list
    updateColorSchemeList(true);
442

443 444
    _ui->colorSchemeList->setMouseTracking(true);
    _ui->colorSchemeList->installEventFilter(this);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
445
    _ui->colorSchemeList->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
446

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

Robert Knight's avatar
 
Robert Knight committed
450
    updateColorSchemeButtons();
451

Laurent Montel's avatar
Laurent Montel committed
452 453 454
    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);
455

456
    // setup font preview
Jekyll Wu's avatar
Jekyll Wu committed
457
    const bool antialias = profile->antiAliasFonts();
458

459 460
    QFont profileFont = profile->font();
    profileFont.setStyleStrategy(antialias ? QFont::PreferAntialias : QFont::NoAntialias);
461

462
    _ui->fontPreviewLabel->installEventFilter(this);
463
    _ui->fontPreviewLabel->setFont(profileFont);
464
    setFontInputValue(profileFont);
465

466
    connect(_ui->fontSizeInput, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged), this, &Konsole::EditProfileDialog::setFontSize);
Laurent Montel's avatar
Laurent Montel committed
467
    connect(_ui->selectFontButton, &QPushButton::clicked, this, &Konsole::EditProfileDialog::showFontDialog);
468

Kurt Hindenburg's avatar
Kurt Hindenburg committed
469
    // setup font smoothing
470
    _ui->antialiasTextButton->setChecked(antialias);
Laurent Montel's avatar
Laurent Montel committed
471
    connect(_ui->antialiasTextButton, &QCheckBox::toggled, this, &Konsole::EditProfileDialog::setAntialiasText);
472

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

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

501
    const QString& name = lookupProfile()->colorScheme();
502 503
    const ColorScheme* currentScheme = ColorSchemeManager::instance()->findColorScheme(name);

Robert Knight's avatar
 
Robert Knight committed
504 505 506
    QStandardItemModel* model = qobject_cast<QStandardItemModel*>(_ui->colorSchemeList->model());

    Q_ASSERT(model);
507

Robert Knight's avatar
 
Robert Knight committed
508 509
    model->clear();

510 511
    QStandardItem* selectedItem = 0;

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
514
    foreach(const ColorScheme* scheme, schemeList) {
515 516
        QStandardItem* item = new QStandardItem(scheme->description());
        item->setData(QVariant::fromValue(scheme) ,  Qt::UserRole + 1);
517
        item->setData(QVariant::fromValue(_profile->font()),  Qt::UserRole + 2);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
518
        item->setFlags(item->flags());
519

520
        if (currentScheme == scheme)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
521
            selectedItem = item;
Robert Knight's avatar
 
Robert Knight committed
522

523 524 525
        model->appendRow(item);
    }

526 527
    model->sort(0);

Kurt Hindenburg's avatar
Kurt Hindenburg committed
528
    if (selectCurrentScheme && selectedItem) {
529
        _ui->colorSchemeList->updateGeometry();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
530 531
        _ui->colorSchemeList->selectionModel()->setCurrentIndex(selectedItem->index() ,
                QItemSelectionModel::Select);
Robert Knight's avatar
 
Robert Knight committed
532 533 534

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

542
    const QString& name = lookupProfile()->keyBindings();
543

544
    KeyboardTranslatorManager* keyManager = KeyboardTranslatorManager::instance();
545
    const KeyboardTranslator* currentTranslator = keyManager->findTranslator(name);
546

Robert Knight's avatar
 
Robert Knight committed
547 548 549 550 551
    QStandardItemModel* model = qobject_cast<QStandardItemModel*>(_ui->keyBindingList->model());

    Q_ASSERT(model);

    model->clear();
552

553 554
    QStandardItem* selectedItem = 0;

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
566
        if (translator == currentTranslator)
567
            selectedItem = item;
568 569 570 571

        model->appendRow(item);
    }

572
    model->sort(0);
573

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

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

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

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

614
    if (!_previewedProperties.contains(aProperty))
615
        return;
616

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

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

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

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

645
    _delayedPreviewProperties.remove(aProperty);
646

647
    const Profile::Ptr original = lookupProfile();
648

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

658 659
    if (!_previewedProperties.contains(aProperty)) {
        _previewedProperties.insert(aProperty , original->property<QVariant>((Profile::Property)aProperty));
660
    }