EditProfileDialog.cpp 48.1 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);
*/
Kurt Hindenburg's avatar
Kurt Hindenburg committed
298
    _ui->initialDirEdit->setClearButtonEnabled(true);
299

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    Q_ASSERT(model);
506

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

509 510
    QStandardItem* selectedItem = 0;

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

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

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

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

525 526
    model->sort(0);

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

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

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

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

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

    Q_ASSERT(model);

    model->clear();
551

552 553
    QStandardItem* selectedItem = 0;

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

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

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

        model->appendRow(item);
    }

571
    model->sort(0);
572

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

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

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

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

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

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

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

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

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

644
    _delayedPreviewProperties.remove(aProperty);
645

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

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

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

    // temporary change to color scheme