EditProfileDialog.cpp 48 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 55

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

67 68
using namespace Konsole;

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

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

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

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

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

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

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

121
    ProfileManager::instance()->changeProfile(_profile, _tempProfile->setProperties());
122 123 124

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

131 132
    createTempProfile();

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

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

192 193
    _profile = profile;

194
    // update caption
195
    updateCaption(profile);
196

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

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

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

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

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

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

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

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

            _ui->emptyNameWarningWidget->setVisible(false);
268 269
        }
    }
270

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

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

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

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

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

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

311
    // window options
312
    _ui->showTerminalSizeHintButton->setChecked(profile->showTerminalSizeHint());
313 314

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

442 443
    // setup color list
    updateColorSchemeList(true);
444

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

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

Robert Knight's avatar
 
Robert Knight committed
452
    updateColorSchemeButtons();
453

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

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

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

464
    _ui->fontPreviewLabel->installEventFilter(this);
465
    _ui->fontPreviewLabel->setFont(profileFont);
466
    setFontInputValue(profileFont);
467

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

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

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

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

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

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

    Q_ASSERT(model);
509

Robert Knight's avatar
 
Robert Knight committed
510 511
    model->clear();

512 513
    QStandardItem* selectedItem = 0;

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

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

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

525 526 527
        model->appendRow(item);
    }

528 529
    model->sort(0);

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

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

544
    const QString& name = lookupProfile()->keyBindings();
545

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

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

    Q_ASSERT(model);

    model->clear();
554

555 556
    QStandardItem* selectedItem = 0;

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

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

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

        model->appendRow(item);
    }

574
    model->sort(0);
575

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

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

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

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

616
    if (!_previewedProperties.contains(aProperty))
617
        return;
618

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

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

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

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

647
    _delayedPreviewProperties.remove(aProperty);
648

649
    const Profile::Ptr original = lookupProfile();
650

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

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

    // temporary change to color scheme
665
    ProfileManager::instance()->changeProfile(_profile , map , false);