EditProfileDialog.cpp 46.9 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 29
#include <QtGui/QBrush>
#include <QtGui/QPainter>
#include <QtGui/QStandardItem>
30
#include <QtCore/QTextCodec>
31 32
#include <QtGui/QLinearGradient>
#include <QtGui/QRadialGradient>
33
#include <QtCore/QTimer>
34 35
#include <QtCore/QTimeLine>

36
// KDE
37
#include <kcodecaction.h>
38 39
#include <KFontDialog>
#include <KIcon>
40
#include <KIconDialog>
41
#include <KFileDialog>
42
#include <KUrlCompletion>
43
#include <KWindowSystem>
Rick Xing's avatar
Rick Xing committed
44
#include <KTextEdit>
45
#include <KMessageBox>
46 47 48

// Konsole
#include "ColorScheme.h"
49
#include "ColorSchemeEditor.h"
50
#include "ui_EditProfileDialog.h"
51 52
#include "KeyBindingEditor.h"
#include "KeyboardTranslator.h"
53
#include "SessionManager.h"
54
#include "ShellCommand.h"
55
#include "TabTitleFormatAction.h"
56

57 58 59 60
// FIXME: it feels bad to include a big header file just
// in order to use one of its static members.
#include "TerminalDisplay.h"

61 62
using namespace Konsole;

63
EditProfileDialog::EditProfileDialog(QWidget* parent)
64
    : KDialog(parent)
Robert Knight's avatar
 
Robert Knight committed
65
    , _colorSchemeAnimationTimeLine(0)
66
    , _delayedPreviewTimer(new QTimer(this))
67
{
68
    setCaption(i18n("Edit Profile"));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
69
    setButtons(KDialog::Ok | KDialog::Cancel | KDialog::Apply);
70

71 72 73
    // disable the apply button , since no modification has been made
    enableButtonApply(false);

74
    connect(this, SIGNAL(applyClicked()), this, SLOT(save()));
Jekyll Wu's avatar
Jekyll Wu committed
75

76
    connect(_delayedPreviewTimer, SIGNAL(timeout()), this, SLOT(delayedPreviewActivate()));
Jekyll Wu's avatar
Jekyll Wu committed
77

78
    _ui = new Ui::EditProfileDialog();
79
    _ui->setupUi(mainWidget());
80

Jekyll Wu's avatar
Jekyll Wu committed
81
    // TODO: Re-enable in a later KDE 4.x release when this feature works again
82
    _ui->enableResizeWindowButton->setVisible(false);
Jekyll Wu's avatar
Jekyll Wu committed
83

84 85 86 87 88 89 90 91 92 93
    // 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
94
    _pageNeedsUpdate.resize(_ui->tabWidget->count());
95
    connect(_ui->tabWidget, SIGNAL(currentChanged(int)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
96
            SLOT(preparePage(int)));
97

98
    createTempProfile();
99
}
100
EditProfileDialog::~EditProfileDialog()
101 102
{
    delete _ui;
103
}
104
void EditProfileDialog::save()
105
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
106
    if (_tempProfile->isEmpty())
107
        return;
108

Kurt Hindenburg's avatar
Kurt Hindenburg committed
109
    SessionManager::instance()->changeProfile(_profile, _tempProfile->setProperties());
110 111 112

    // ensure that these settings are not undone by a call
    // to unpreview()
Kurt Hindenburg's avatar
Kurt Hindenburg committed
113 114
    QHashIterator<Profile::Property, QVariant> iter(_tempProfile->setProperties());
    while (iter.hasNext()) {
115 116 117
        iter.next();
        _previewedProperties.remove(iter.key());
    }
118

119 120
    createTempProfile();

121
    enableButtonApply(false);
122 123 124 125 126
}
void EditProfileDialog::reject()
{
    unpreviewAll();
    KDialog::reject();
127 128 129
}
void EditProfileDialog::accept()
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
130 131
    Q_ASSERT(_profile);
    Q_ASSERT(_tempProfile);
132

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

180 181
    _profile = profile;

182
    // update caption
183
    updateCaption(profile);
184

185 186 187 188 189
    // 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
190
    preparePage(_ui->tabWidget->currentIndex());
191

Kurt Hindenburg's avatar
Kurt Hindenburg committed
192
    if (_tempProfile) {
193
        createTempProfile();
194
    }
195
}
196
const Profile::Ptr EditProfileDialog::lookupProfile() const
197
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
198
    return _profile;
199
}
Robert Knight's avatar
 
Robert Knight committed
200
void EditProfileDialog::preparePage(int page)
201
{
Jekyll Wu's avatar
Jekyll Wu committed
202
    const Profile::Ptr profile = lookupProfile();
203

Kurt Hindenburg's avatar
Kurt Hindenburg committed
204
    Q_ASSERT(_pageNeedsUpdate.count() > page);
Jekyll Wu's avatar
Jekyll Wu committed
205
    Q_ASSERT(profile);
206

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
209 210
    if (_pageNeedsUpdate[page]) {
        if (pageWidget == _ui->generalTab)
Jekyll Wu's avatar
Jekyll Wu committed
211
            setupGeneralPage(profile);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
212
        else if (pageWidget == _ui->tabsTab)
Jekyll Wu's avatar
Jekyll Wu committed
213
            setupTabsPage(profile);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
214
        else if (pageWidget == _ui->appearanceTab)
Jekyll Wu's avatar
Jekyll Wu committed
215
            setupAppearancePage(profile);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
216
        else if (pageWidget == _ui->scrollingTab)
Jekyll Wu's avatar
Jekyll Wu committed
217
            setupScrollingPage(profile);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
218
        else if (pageWidget == _ui->keyboardTab)
Jekyll Wu's avatar
Jekyll Wu committed
219
            setupKeyboardPage(profile);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
220
        else if (pageWidget == _ui->advancedTab)
Jekyll Wu's avatar
Jekyll Wu committed
221
            setupAdvancedPage(profile);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
222 223
        else
            Q_ASSERT(false);
224

225
        _pageNeedsUpdate[page] = false;
226
    }
Robert Knight's avatar
 
Robert Knight committed
227 228

    // start page entry animation for color schemes
Kurt Hindenburg's avatar
Kurt Hindenburg committed
229 230
    if (pageWidget == _ui->appearanceTab)
        _colorSchemeAnimationTimeLine->start();
Robert Knight's avatar
 
Robert Knight committed
231 232 233 234
}
void EditProfileDialog::selectProfileName()
{
    _ui->profileNameEdit->setFocus();
235
    _ui->profileNameEdit->selectAll();
236
}
Jekyll Wu's avatar
Jekyll Wu committed
237
void EditProfileDialog::setupGeneralPage(const Profile::Ptr profile)
238
{
239
    // basic profile options
240
    {
241 242 243 244
        _ui->emptyNameWarningWidget->setWordWrap(false);
        _ui->emptyNameWarningWidget->setCloseButtonVisible(false);
        _ui->emptyNameWarningWidget->setMessageType(KMessageWidget::Warning);

Jekyll Wu's avatar
Jekyll Wu committed
245
        ProfileGroup::Ptr group = profile->asGroup();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
246
        if (!group || group->profiles().count() < 2) {
Jekyll Wu's avatar
Jekyll Wu committed
247
            _ui->profileNameEdit->setText(profile->name());
248
            _ui->profileNameEdit->setClearButtonShown(true);
249

Jekyll Wu's avatar
Jekyll Wu committed
250
            _ui->emptyNameWarningWidget->setVisible(profile->name().isEmpty());
251
            _ui->emptyNameWarningWidget->setText(i18n("Profile name is empty."));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
252 253
        } else {
            _ui->profileNameEdit->setText(groupProfileNames(group, -1));
254
            _ui->profileNameEdit->setEnabled(false);
255
            _ui->profileNameLabel->setEnabled(false);
256 257

            _ui->emptyNameWarningWidget->setVisible(false);
258 259
        }
    }
260

261

Jekyll Wu's avatar
Jekyll Wu committed
262
    ShellCommand command(profile->command() , profile->arguments());
Kurt Hindenburg's avatar
Kurt Hindenburg committed
263
    _ui->commandEdit->setText(command.fullCommand());
264
    KUrlCompletion* exeCompletion = new KUrlCompletion(KUrlCompletion::ExeCompletion);
265
    exeCompletion->setParent(this);
266
    exeCompletion->setDir(QString());
Kurt Hindenburg's avatar
Kurt Hindenburg committed
267
    _ui->commandEdit->setCompletionObject(exeCompletion);
268

Jekyll Wu's avatar
Jekyll Wu committed
269
    _ui->initialDirEdit->setText(profile->defaultWorkingDirectory());
270 271
    KUrlCompletion* dirCompletion = new KUrlCompletion(KUrlCompletion::DirCompletion);
    dirCompletion->setParent(this);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
272
    _ui->initialDirEdit->setCompletionObject(dirCompletion);
273
    _ui->initialDirEdit->setClearButtonShown(true);
274

Kurt Hindenburg's avatar
Kurt Hindenburg committed
275
    _ui->dirSelectButton->setIcon(KIcon("folder-open"));
Jekyll Wu's avatar
Jekyll Wu committed
276 277
    _ui->iconSelectButton->setIcon(KIcon(profile->icon()));
    _ui->startInSameDirButton->setChecked(profile->property<bool>(Profile::StartInCurrentSessionDir));
278

279
    // window options
Jekyll Wu's avatar
Jekyll Wu committed
280 281
    _ui->showTerminalSizeHintButton->setChecked(profile->property<bool>(Profile::ShowTerminalSizeHint));
    _ui->saveGeometryOnExitButton->setChecked(profile->property<bool>(Profile::SaveGeometryOnExit));
282 283

    // signals and slots
284 285 286
    connect(_ui->dirSelectButton, SIGNAL(clicked()), this, SLOT(selectInitialDir()));
    connect(_ui->iconSelectButton, SIGNAL(clicked()), this, SLOT(selectIcon()));
    connect(_ui->startInSameDirButton, SIGNAL(toggled(bool)), this ,
287
            SLOT(startInSameDir(bool)));
288
    connect(_ui->profileNameEdit, SIGNAL(textChanged(QString)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
289
            SLOT(profileNameChanged(QString)));
290
    connect(_ui->initialDirEdit, SIGNAL(textChanged(QString)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
291
            SLOT(initialDirChanged(QString)));
292
    connect(_ui->commandEdit, SIGNAL(textChanged(QString)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
293
            SLOT(commandChanged(QString)));
294
    connect(_ui->environmentEditButton , SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
295
            SLOT(showEnvironmentEditor()));
Jekyll Wu's avatar
Jekyll Wu committed
296

297
    connect(_ui->saveGeometryOnExitButton, SIGNAL(toggled(bool)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
298
            SLOT(saveGeometryOnExit(bool)));
299
    connect(_ui->showTerminalSizeHintButton, SIGNAL(toggled(bool)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
300
            SLOT(showTerminalSizeHint(bool)));
301 302 303
}
void EditProfileDialog::showEnvironmentEditor()
{
Jekyll Wu's avatar
Jekyll Wu committed
304
    const Profile::Ptr profile = lookupProfile();
305

Kurt Hindenburg's avatar
Kurt Hindenburg committed
306 307
    QWeakPointer<KDialog> dialog = new KDialog(this);
    KTextEdit* edit = new KTextEdit(dialog.data());
308

Jekyll Wu's avatar
Jekyll Wu committed
309
    QStringList currentEnvironment = profile->property<QStringList>(Profile::Environment);
310

Kurt Hindenburg's avatar
Kurt Hindenburg committed
311 312
    edit->setPlainText(currentEnvironment.join("\n"));
    edit->setToolTip(i18n("One environment variable per line"));
313

Kurt Hindenburg's avatar
Kurt Hindenburg committed
314 315
    dialog.data()->setPlainCaption(i18n("Edit Environment"));
    dialog.data()->setMainWidget(edit);
316

Kurt Hindenburg's avatar
Kurt Hindenburg committed
317
    if (dialog.data()->exec() == QDialog::Accepted) {
318
        QStringList newEnvironment = edit->toPlainText().split('\n');
Kurt Hindenburg's avatar
Kurt Hindenburg committed
319
        updateTempProfileProperty(Profile::Environment, newEnvironment);
320 321
    }

Kurt Hindenburg's avatar
Kurt Hindenburg committed
322
    delete dialog.data();
323
}
Jekyll Wu's avatar
Jekyll Wu committed
324
void EditProfileDialog::setupTabsPage(const Profile::Ptr profile)
325
{
326 327 328
    // tab title format
    _ui->tabTitleEdit->setClearButtonShown(true);
    _ui->remoteTabTitleEdit->setClearButtonShown(true);
Jekyll Wu's avatar
Jekyll Wu committed
329
    _ui->tabTitleEdit->setText(profile->property<QString>(Profile::LocalTabTitleFormat));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
330
    _ui->remoteTabTitleEdit->setText(
Jekyll Wu's avatar
Jekyll Wu committed
331
        profile->property<QString>(Profile::RemoteTabTitleFormat));
332

Jekyll Wu's avatar
Jekyll Wu committed
333
    // tab monitoring
Jekyll Wu's avatar
Jekyll Wu committed
334
    int silenceSeconds = profile->property<int>(Profile::SilenceSeconds);
Jekyll Wu's avatar
Jekyll Wu committed
335
    _ui->silenceSecondsSpinner->setValue(silenceSeconds);
336
    _ui->silenceSecondsSpinner->setSuffix(ki18ncp("Unit of time", " second", " seconds"));
Jekyll Wu's avatar
Jekyll Wu committed
337

338
    connect(_ui->tabTitleEdit, SIGNAL(textChanged(QString)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
339
            SLOT(tabTitleFormatChanged(QString)));
340
    connect(_ui->remoteTabTitleEdit, SIGNAL(textChanged(QString)), this,
Laurent Montel's avatar
Laurent Montel committed
341
            SLOT(remoteTabTitleFormatChanged(QString)));
342

343 344
    connect(_ui->silenceSecondsSpinner, SIGNAL(valueChanged(int)),
            this, SLOT(silenceSecondsChanged(int)));
345

346 347 348 349
    // menus for local and remote tab title dynamic elements
    TabTitleFormatAction* localTabTitleAction = new TabTitleFormatAction(this);
    localTabTitleAction->setContext(Session::LocalTabTitle);
    _ui->tabTitleEditButton->setMenu(localTabTitleAction->menu());
350 351
    connect(localTabTitleAction, SIGNAL(dynamicElementSelected(QString)),
            this, SLOT(insertTabTitleText(QString)));
352 353 354 355

    TabTitleFormatAction* remoteTabTitleAction = new TabTitleFormatAction(this);
    remoteTabTitleAction->setContext(Session::RemoteTabTitle);
    _ui->remoteTabTitleEditButton->setMenu(remoteTabTitleAction->menu());
356 357
    connect(remoteTabTitleAction, SIGNAL(dynamicElementSelected(QString)),
            this, SLOT(insertRemoteTabTitleText(QString)));
358
}
359 360 361 362 363 364 365 366
void EditProfileDialog::insertTabTitleText(const QString& text)
{
    _ui->tabTitleEdit->insert(text);
}
void EditProfileDialog::insertRemoteTabTitleText(const QString& text)
{
    _ui->remoteTabTitleEdit->insert(text);
}
367 368
void EditProfileDialog::saveGeometryOnExit(bool save)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
369
    updateTempProfileProperty(Profile::SaveGeometryOnExit, save);
370
}
371
void EditProfileDialog::showTerminalSizeHint(bool show)
372
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
373
    updateTempProfileProperty(Profile::ShowTerminalSizeHint, show);
374
}
375 376
void EditProfileDialog::tabTitleFormatChanged(const QString& format)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
377
    updateTempProfileProperty(Profile::LocalTabTitleFormat, format);
378 379 380
}
void EditProfileDialog::remoteTabTitleFormatChanged(const QString& format)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
381
    updateTempProfileProperty(Profile::RemoteTabTitleFormat, format);
382 383
}

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
402
    updateTempProfileProperty(Profile::Name, 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
    const KUrl url = KFileDialog::getExistingDirectoryUrl(_ui->initialDirEdit->text(),
Kurt Hindenburg's avatar
Kurt Hindenburg committed
423 424
                     this,
                     i18n("Select Initial Directory"));
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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
434
    _colorSchemeAnimationTimeLine = new QTimeLine(500 , this);
Robert Knight's avatar
 
Robert Knight committed
435
    delegate->setEntryTimeLine(_colorSchemeAnimationTimeLine);
436

437
    connect(_colorSchemeAnimationTimeLine, SIGNAL(valueChanged(qreal)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
438
            SLOT(colorSchemeAnimationUpdate()));
439

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

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

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

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

455 456 457 458
    connect(_ui->colorSchemeList->selectionModel(),
            SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
            this, SLOT(colorSchemeSelected()));
    connect(_ui->colorSchemeList, SIGNAL(entered(QModelIndex)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
459
            SLOT(previewColorScheme(QModelIndex)));
460

Robert Knight's avatar
 
Robert Knight committed
461
    updateColorSchemeButtons();
462

463
    connect(_ui->editColorSchemeButton, SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
464
            SLOT(editColorScheme()));
465
    connect(_ui->removeColorSchemeButton, SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
466
            SLOT(removeColorScheme()));
467
    connect(_ui->newColorSchemeButton, SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
468
            SLOT(newColorScheme()));
469

470
    // setup font preview
Jekyll Wu's avatar
Jekyll Wu committed
471
    bool antialias = profile->property<bool>(Profile::AntiAliasFonts);
472

Jekyll Wu's avatar
Jekyll Wu committed
473
    QFont font = profile->font();
474
    font.setStyleStrategy(antialias ? QFont::PreferAntialias : QFont::NoAntialias);
475

476 477
    _ui->fontPreviewLabel->installEventFilter(this);
    _ui->fontPreviewLabel->setFont(font);
478 479
    setFontSliderRange(font);
    setFontSliderValue(font);
480

481
    connect(_ui->fontSizeSlider, SIGNAL(valueChanged(int)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
482
            SLOT(setFontSize(int)));
483
    connect(_ui->editFontButton, SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
484
            SLOT(showFontDialog()));
485

Kurt Hindenburg's avatar
Kurt Hindenburg committed
486
    // setup font smoothing
487
    _ui->antialiasTextButton->setChecked(antialias);
488
    connect(_ui->antialiasTextButton, SIGNAL(toggled(bool)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
489
            SLOT(setAntialiasText(bool)));
490

Jekyll Wu's avatar
Jekyll Wu committed
491
    bool boldIntense = profile->property<bool>(Profile::BoldIntense);
492
    _ui->boldIntenseButton->setChecked(boldIntense);
493
    connect(_ui->boldIntenseButton, SIGNAL(toggled(bool)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
494
            SLOT(setBoldIntense(bool)));
495 496 497
}
void EditProfileDialog::setAntialiasText(bool enable)
{
498 499 500
    QFont font = _ui->fontPreviewLabel->font();
    font.setStyleStrategy(enable ? QFont::PreferAntialias : QFont::NoAntialias);

501
    // update preview to reflect text smoothing state
502
    fontSelected(font);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
503
    updateTempProfileProperty(Profile::AntiAliasFonts, enable);
504
}
505 506
void EditProfileDialog::setBoldIntense(bool enable)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
507 508
    preview(Profile::BoldIntense, enable);
    updateTempProfileProperty(Profile::BoldIntense, enable);
509
}
510 511 512 513
void EditProfileDialog::colorSchemeAnimationUpdate()
{
    QAbstractItemModel* model = _ui->colorSchemeList->model();

Kurt Hindenburg's avatar
Kurt Hindenburg committed
514 515
    for (int i = model->rowCount() ; i >= 0 ; i--)
        _ui->colorSchemeList->update(model->index(i, 0));
516
}
517
void EditProfileDialog::updateColorSchemeList(bool selectCurrentScheme)
518
{
Robert Knight's avatar
 
Robert Knight committed
519 520
    if (!_ui->colorSchemeList->model())
        _ui->colorSchemeList->setModel(new QStandardItemModel(this));
521

522
    const QString& name = lookupProfile()->colorScheme();
523 524
    const ColorScheme* currentScheme = ColorSchemeManager::instance()->findColorScheme(name);

Robert Knight's avatar
 
Robert Knight committed
525 526 527
    QStandardItemModel* model = qobject_cast<QStandardItemModel*>(_ui->colorSchemeList->model());

    Q_ASSERT(model);
528

Robert Knight's avatar
 
Robert Knight committed
529 530
    model->clear();

531 532
    QStandardItem* selectedItem = 0;

533 534 535 536 537
    QList<const ColorScheme*> schemeList = ColorSchemeManager::instance()->allColorSchemes();

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

540
        if (currentScheme == scheme)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
541
            selectedItem = item;
Robert Knight's avatar
 
Robert Knight committed
542

543 544 545
        model->appendRow(item);
    }

546 547
    model->sort(0);

Kurt Hindenburg's avatar
Kurt Hindenburg committed
548
    if (selectCurrentScheme && selectedItem) {
549
        _ui->colorSchemeList->updateGeometry();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
550 551
        _ui->colorSchemeList->selectionModel()->setCurrentIndex(selectedItem->index() ,
                QItemSelectionModel::Select);
Robert Knight's avatar
 
Robert Knight committed
552 553 554

        // update transparency warning label
        updateTransparencyWarning();
555
    }
556
}
557
void EditProfileDialog::updateKeyBindingsList(bool selectCurrentTranslator)
558
{
Robert Knight's avatar
 
Robert Knight committed
559 560
    if (!_ui->keyBindingList->model())
        _ui->keyBindingList->setModel(new QStandardItemModel(this));
561

562
    const QString& name = lookupProfile()
Kurt Hindenburg's avatar
Kurt Hindenburg committed
563
                          ->property<QString>(Profile::KeyBindings);
564

565
    KeyboardTranslatorManager* keyManager = KeyboardTranslatorManager::instance();
566
    const KeyboardTranslator* currentTranslator = keyManager->findTranslator(name);
567

Robert Knight's avatar
 
Robert Knight committed
568 569 570 571 572
    QStandardItemModel* model = qobject_cast<QStandardItemModel*>(_ui->keyBindingList->model());

    Q_ASSERT(model);

    model->clear();
573

574 575
    QStandardItem* selectedItem = 0;

576
    QList<QString> translatorNames = keyManager->allTranslators();
577
    foreach ( const QString& name, translatorNames) {
578
        const KeyboardTranslator* translator = keyManager->findTranslator(name);
579 580

        QStandardItem* item = new QStandardItem(translator->description());
Kurt Hindenburg's avatar
Kurt Hindenburg committed
581 582
        item->setData(QVariant::fromValue(translator), Qt::UserRole + 1);
        item->setIcon(KIcon("preferences-desktop-keyboard"));
583

Kurt Hindenburg's avatar
Kurt Hindenburg committed
584
        if (translator == currentTranslator)
585
            selectedItem = item;
586 587 588 589

        model->appendRow(item);
    }

590
    model->sort(0);
591

Kurt Hindenburg's avatar
Kurt Hindenburg committed
592 593 594
    if (selectCurrentTranslator && selectedItem) {
        _ui->keyBindingList->selectionModel()->setCurrentIndex(selectedItem->index() ,
                QItemSelectionModel::Select);
595
    }
596
}
Kurt Hindenburg's avatar
Kurt Hindenburg committed
597
bool EditProfileDialog::eventFilter(QObject* watched , QEvent* event)
598
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
599 600 601
    if (watched == _ui->colorSchemeList && event->type() == QEvent::Leave) {
        if (_tempProfile->isPropertySet(Profile::ColorScheme))
            preview(Profile::ColorScheme, _tempProfile->colorScheme());
602 603
        else
            unpreview(Profile::ColorScheme);
604
    }
Kurt Hindenburg's avatar
Kurt Hindenburg committed
605
    if (watched == _ui->fontPreviewLabel && event->type() == QEvent::FontChange) {
606
        const QFont& labelFont = _ui->fontPreviewLabel->font();
607
        qreal size = labelFont.pointSizeF();
608 609
        QString fontSize  = KGlobal::locale()->formatNumber(size, size == floor(size) ? 0 : 1);
        _ui->fontPreviewLabel->setText(i18n("%1, size %2", labelFont.family(), fontSize));
610
    }
611

Kurt Hindenburg's avatar
Kurt Hindenburg committed
612
    return KDialog::eventFilter(watched, event);
613
}
614
void EditProfileDialog::unpreviewAll()
615
{
616 617 618
    _delayedPreviewTimer->stop();
    _delayedPreviewProperties.clear();

Kurt Hindenburg's avatar
Kurt Hindenburg committed
619 620 621
    QHash<Profile::Property, QVariant> map;
    QHashIterator<int, QVariant> iter(_previewedProperties);
    while (iter.hasNext()) {
622
        iter.next();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
623
        map.insert((Profile::Property)iter.key(), iter.value());
624 625 626
    }

    // undo any preview changes
Kurt Hindenburg's avatar
Kurt Hindenburg committed
627 628
    if (!map.isEmpty())
        SessionManager::instance()->changeProfile(_profile, map, false);
629 630 631
}
void EditProfileDialog::unpreview(int property)
{
632
    _delayedPreviewProperties.remove(property);
633

634 635
    if (!_previewedProperties.contains(property))
        return;
636

Kurt Hindenburg's avatar
Kurt Hindenburg committed
637 638 639
    QHash<Profile::Property, QVariant> map;
    map.insert((Profile::Property)property, _previewedProperties[property]);
    SessionManager::instance()->changeProfile(_profile, map, false);
640 641 642

    _previewedProperties.remove(property);
}
643 644
void EditProfileDialog::delayedPreview(int property , const QVariant& value)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
645
    _delayedPreviewProperties.insert(property, value);
646 647

    _delayedPreviewTimer->stop();
648
    _delayedPreviewTimer->start(300);
649 650 651
}
void EditProfileDialog::delayedPreviewActivate()
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
652
    Q_ASSERT(qobject_cast<QTimer*>(sender()));
653

Kurt Hindenburg's avatar
Kurt Hindenburg committed
654 655
    QMutableHashIterator<int, QVariant> iter(_delayedPreviewProperties);
    if (iter.hasNext()) {
656
        iter.next();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
657
        preview(iter.key(), iter.value());
658 659
    }
}
Robert Knight's avatar
Robert Knight committed
660
void EditProfileDialog::preview(int property , const QVariant& value)
661
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
662 663