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
#include <QtGui/QBrush>
#include <QtGui/QPainter>
29
#include <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
38
39
40
#include <kdeversion.h>
#if KDE_IS_VERSION(4, 9, 1)
#include <KCodecAction>
#else
41
#include <kcodecaction.h>
42
43
#endif

44
45
#include <KFontDialog>
#include <KIcon>
46
#include <KIconDialog>
47
#include <KFileDialog>
48
#include <KUrlCompletion>
49
#include <KWindowSystem>
Rick Xing's avatar
Rick Xing committed
50
#include <KTextEdit>
51
#include <KMessageBox>
52
53
54

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

66
67
using namespace Konsole;

68
69
EditProfileDialog::EditProfileDialog(QWidget* aParent)
    : KDialog(aParent)
Robert Knight's avatar
   
Robert Knight committed
70
    , _colorSchemeAnimationTimeLine(0)
71
    , _delayedPreviewTimer(new QTimer(this))
72
{
73
    setCaption(i18n("Edit Profile"));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
74
    setButtons(KDialog::Ok | KDialog::Cancel | KDialog::Apply);
75

76
77
78
    // disable the apply button , since no modification has been made
    enableButtonApply(false);

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

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

83
    _ui = new Ui::EditProfileDialog();
84
    _ui->setupUi(mainWidget());
85

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

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

111
    ProfileManager::instance()->changeProfile(_profile, _tempProfile->setProperties());
112
113
114

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

121
122
    createTempProfile();

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

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

182
183
    _profile = profile;

184
    // update caption
185
    updateCaption(profile);
186

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

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

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

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

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

229
        _pageNeedsUpdate[page] = false;
230
    }
Robert Knight's avatar
   
Robert Knight committed
231
232

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

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

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

            _ui->emptyNameWarningWidget->setVisible(false);
262
263
        }
    }
264

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
278
    _ui->dirSelectButton->setIcon(KIcon("folder-open"));
Jekyll Wu's avatar
Jekyll Wu committed
279
    _ui->iconSelectButton->setIcon(KIcon(profile->icon()));
280
    _ui->startInSameDirButton->setChecked(profile->startInCurrentSessionDir());
281

282
    // window options
283
284
    _ui->showTerminalSizeHintButton->setChecked(profile->showTerminalSizeHint());
    _ui->saveGeometryOnExitButton->setChecked(profile->saveGeometryOnExit());
285
286

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
309
310
    QWeakPointer<KDialog> dialog = new KDialog(this);
    KTextEdit* edit = new KTextEdit(dialog.data());
311

312
    QStringList currentEnvironment = profile->environment();
313

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
317
318
    dialog.data()->setPlainCaption(i18n("Edit Environment"));
    dialog.data()->setMainWidget(edit);
319

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
325
    delete dialog.data();
326
}
Jekyll Wu's avatar
Jekyll Wu committed
327
void EditProfileDialog::setupTabsPage(const Profile::Ptr profile)
328
{
329
    // tab title format
330
331
332
333
334
335
336
    _ui->renameTabWidget->setTabTitleText(profile->localTabTitleFormat());
    _ui->renameTabWidget->setRemoteTabTitleText(profile->remoteTabTitleFormat());

    connect(_ui->renameTabWidget, SIGNAL(tabTitleFormatChanged(QString)), this,
            SLOT(tabTitleFormatChanged(QString)));
    connect(_ui->renameTabWidget, SIGNAL(remoteTabTitleFormatChanged(QString)), this,
            SLOT(remoteTabTitleFormatChanged(QString)));
337

Jekyll Wu's avatar
Jekyll Wu committed
338
    // tab monitoring
Jekyll Wu's avatar
Jekyll Wu committed
339
    const int silenceSeconds = profile->silenceSeconds();
Jekyll Wu's avatar
Jekyll Wu committed
340
    _ui->silenceSecondsSpinner->setValue(silenceSeconds);
341
    _ui->silenceSecondsSpinner->setSuffix(ki18ncp("Unit of time", " second", " seconds"));
Jekyll Wu's avatar
Jekyll Wu committed
342

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

347
void EditProfileDialog::saveGeometryOnExit(bool value)
348
{
349
    updateTempProfileProperty(Profile::SaveGeometryOnExit, value);
350
}
351
void EditProfileDialog::showTerminalSizeHint(bool value)
352
{
353
    updateTempProfileProperty(Profile::ShowTerminalSizeHint, value);
354
}
355
356
void EditProfileDialog::tabTitleFormatChanged(const QString& format)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
357
    updateTempProfileProperty(Profile::LocalTabTitleFormat, format);
358
359
360
}
void EditProfileDialog::remoteTabTitleFormatChanged(const QString& format)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
361
    updateTempProfileProperty(Profile::RemoteTabTitleFormat, format);
362
363
}

364
365
366
367
368
void EditProfileDialog::silenceSecondsChanged(int seconds)
{
    updateTempProfileProperty(Profile::SilenceSeconds, seconds);
}

369
370
void EditProfileDialog::selectIcon()
{
Craig Drummond's avatar
Craig Drummond committed
371
    const QString& icon = KIconDialog::getIcon(KIconLoader::Desktop, KIconLoader::Application,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
372
373
374
375
                          false, 0, false, this);
    if (!icon.isEmpty()) {
        _ui->iconSelectButton->setIcon(KIcon(icon));
        updateTempProfileProperty(Profile::Icon, icon);
376
377
378
379
    }
}
void EditProfileDialog::profileNameChanged(const QString& text)
{
380
381
    _ui->emptyNameWarningWidget->setVisible(text.isEmpty());

Kurt Hindenburg's avatar
Kurt Hindenburg committed
382
    updateTempProfileProperty(Profile::Name, text);
383
    updateTempProfileProperty(Profile::UntranslatedName, text);
384
    updateCaption(_tempProfile);
385
}
386
387
void EditProfileDialog::startInSameDir(bool sameDir)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
388
    updateTempProfileProperty(Profile::StartInCurrentSessionDir, sameDir);
389
}
390
391
void EditProfileDialog::initialDirChanged(const QString& dir)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
392
    updateTempProfileProperty(Profile::Directory, dir);
393
394
395
}
void EditProfileDialog::commandChanged(const QString& command)
{
396
397
    ShellCommand shellCommand(command);

Kurt Hindenburg's avatar
Kurt Hindenburg committed
398
399
    updateTempProfileProperty(Profile::Command, shellCommand.command());
    updateTempProfileProperty(Profile::Arguments, shellCommand.arguments());
400
401
402
}
void EditProfileDialog::selectInitialDir()
{
403
    const KUrl url = KFileDialog::getExistingDirectoryUrl(_ui->initialDirEdit->text(),
Kurt Hindenburg's avatar
Kurt Hindenburg committed
404
405
                     this,
                     i18n("Select Initial Directory"));
406

Kurt Hindenburg's avatar
Kurt Hindenburg committed
407
    if (!url.isEmpty())
408
        _ui->initialDirEdit->setText(url.path());
409
}
Jekyll Wu's avatar
Jekyll Wu committed
410
void EditProfileDialog::setupAppearancePage(const Profile::Ptr profile)
411
{
412
    ColorSchemeViewDelegate* delegate = new ColorSchemeViewDelegate(this);
413
    _ui->colorSchemeList->setItemDelegate(delegate);
414

Kurt Hindenburg's avatar
Kurt Hindenburg committed
415
    _colorSchemeAnimationTimeLine = new QTimeLine(500 , this);
Robert Knight's avatar
   
Robert Knight committed
416
    delegate->setEntryTimeLine(_colorSchemeAnimationTimeLine);
417

418
    connect(_colorSchemeAnimationTimeLine, SIGNAL(valueChanged(qreal)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
419
            SLOT(colorSchemeAnimationUpdate()));
420

Robert Knight's avatar
   
Robert Knight committed
421
    _ui->transparencyWarningWidget->setVisible(false);
422
423
424
    _ui->transparencyWarningWidget->setWordWrap(true);
    _ui->transparencyWarningWidget->setCloseButtonVisible(false);
    _ui->transparencyWarningWidget->setMessageType(KMessageWidget::Warning);
425

Robert Knight's avatar
   
Robert Knight committed
426
427
428
    _ui->editColorSchemeButton->setEnabled(false);
    _ui->removeColorSchemeButton->setEnabled(false);

429
430
    // setup color list
    updateColorSchemeList(true);
431

432
433
    _ui->colorSchemeList->setMouseTracking(true);
    _ui->colorSchemeList->installEventFilter(this);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
434
    _ui->colorSchemeList->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
435

436
    connect(_ui->colorSchemeList->selectionModel(),
Kurt Hindenburg's avatar
Kurt Hindenburg committed
437
            SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
438
439
            this, SLOT(colorSchemeSelected()));
    connect(_ui->colorSchemeList, SIGNAL(entered(QModelIndex)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
440
            SLOT(previewColorScheme(QModelIndex)));
441

Robert Knight's avatar
   
Robert Knight committed
442
    updateColorSchemeButtons();
443

444
    connect(_ui->editColorSchemeButton, SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
445
            SLOT(editColorScheme()));
446
    connect(_ui->removeColorSchemeButton, SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
447
            SLOT(removeColorScheme()));
448
    connect(_ui->newColorSchemeButton, SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
449
            SLOT(newColorScheme()));
450

451
    // setup font preview
Jekyll Wu's avatar
Jekyll Wu committed
452
    const bool antialias = profile->antiAliasFonts();
453

454
455
    QFont profileFont = profile->font();
    profileFont.setStyleStrategy(antialias ? QFont::PreferAntialias : QFont::NoAntialias);
456

457
    _ui->fontPreviewLabel->installEventFilter(this);
458
    _ui->fontPreviewLabel->setFont(profileFont);
459
    setFontInputValue(profileFont);
460

461
462
    connect(_ui->fontSizeInput, SIGNAL(valueChanged(double)), this,
            SLOT(setFontSize(double)));
463
    connect(_ui->selectFontButton, SIGNAL(clicked()), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
464
            SLOT(showFontDialog()));
465

Kurt Hindenburg's avatar
Kurt Hindenburg committed
466
    // setup font smoothing
467
    _ui->antialiasTextButton->setChecked(antialias);
468
    connect(_ui->antialiasTextButton, SIGNAL(toggled(bool)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
469
            SLOT(setAntialiasText(bool)));
470

Jekyll Wu's avatar
Jekyll Wu committed
471
    _ui->boldIntenseButton->setChecked(profile->boldIntense());
472
    connect(_ui->boldIntenseButton, SIGNAL(toggled(bool)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
473
            SLOT(setBoldIntense(bool)));
474
475
476
}
void EditProfileDialog::setAntialiasText(bool enable)
{
477
478
    QFont profileFont = _ui->fontPreviewLabel->font();
    profileFont.setStyleStrategy(enable ? QFont::PreferAntialias : QFont::NoAntialias);
479

480
    // update preview to reflect text smoothing state
481
    fontSelected(profileFont);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
482
    updateTempProfileProperty(Profile::AntiAliasFonts, enable);
483
}
484
485
void EditProfileDialog::setBoldIntense(bool enable)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
486
487
    preview(Profile::BoldIntense, enable);
    updateTempProfileProperty(Profile::BoldIntense, enable);
488
}
489
490
491
492
void EditProfileDialog::colorSchemeAnimationUpdate()
{
    QAbstractItemModel* model = _ui->colorSchemeList->model();

Kurt Hindenburg's avatar
Kurt Hindenburg committed
493
494
    for (int i = model->rowCount() ; i >= 0 ; i--)
        _ui->colorSchemeList->update(model->index(i, 0));
495
}
496
void EditProfileDialog::updateColorSchemeList(bool selectCurrentScheme)
497
{
Robert Knight's avatar
   
Robert Knight committed
498
499
    if (!_ui->colorSchemeList->model())
        _ui->colorSchemeList->setModel(new QStandardItemModel(this));
500

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

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

    Q_ASSERT(model);
507

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

510
511
    QStandardItem* selectedItem = 0;

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
514
    foreach(const ColorScheme* scheme, schemeList) {
515
516
        QStandardItem* item = new QStandardItem(scheme->description());
        item->setData(QVariant::fromValue(scheme) ,  Qt::UserRole + 1);
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);
557
558

        QStandardItem* item = new QStandardItem(translator->description());
559
        item->setEditable(false);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
560
561
        item->setData(QVariant::fromValue(translator), Qt::UserRole + 1);
        item->setIcon(KIcon("preferences-desktop-keyboard"));
562

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

        model->appendRow(item);
    }

569
    model->sort(0);
570

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

589
    return KDialog::eventFilter(watched, aEvent);
590
}
591
void EditProfileDialog::unpreviewAll()
592
{
593
594
595
    _delayedPreviewTimer->stop();
    _delayedPreviewProperties.clear();

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

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

611
    if (!_previewedProperties.contains(aProperty))
612
        return;
613

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

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

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

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

642
    _delayedPreviewProperties.remove(aProperty);
643

644
    const Profile::Ptr original = lookupProfile();
645

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

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

    // temporary change to color scheme
660
    ProfileManager::instance()->changeProfile(_profile , map , false);
661
662
663
}
void EditProfileDialog::previewColorScheme(const QModelIndex& index)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
664
    const QString& name = index.data(Qt::UserRole + 1).value<const ColorScheme*>()->name();
665

Kurt Hindenburg's avatar
Kurt Hindenburg committed
666
    delayedPreview(Profile::ColorScheme , name);
667
}
668
669
void EditProfileDialog::removeColorScheme()
{
670
671
    QModelIndexList selected = _ui->colorSchemeList->selectionModel()->selectedIndexes();

Kurt Hindenburg's avatar
Kurt Hindenburg committed
672
673
    if (!selected.isEmpty()) {
        const QString& name = selected.first().data(Qt::UserRole + 1).value<const ColorScheme*>()->name();
674

Kurt Hindenburg's avatar
Kurt Hindenburg committed
675
        if (ColorSchemeManager::instance()->deleteColorScheme(name))
676
            _ui->colorSchemeList->model()->removeRow(selected.first().row());
677
    }
678
}
679
void EditProfileDialog::showColorSchemeEditor(bool isNewScheme)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
680
{
681
682
    QModelIndexList selected = _ui->colorSchemeList->selectionModel()->selectedIndexes();

683
    QAbstractItemModel* model = _ui->colorSchemeList->model();
Robert Knight's avatar
   
Robert Knight committed
684
    const ColorScheme* colors = 0;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
685
686
    if (!selected.isEmpty())
        colors = model->data(selected.first(), Qt::UserRole + 1).value<const ColorScheme*>();
687
    else
Robert Knight's avatar
   
Robert Knight committed
688
        colors = ColorSchemeManager::instance()->defaultColorScheme();
689

Robert Knight's avatar
   
Robert Knight committed
690
    Q_ASSERT(colors);
691

Kurt Hindenburg's avatar
Kurt Hindenburg committed
692
    QWeakPointer<KDialog> dialog = new KDialog(this);
693

Kurt Hindenburg's avatar
Kurt Hindenburg committed
694
    if (isNewScheme)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
695
        dialog.data()->setCaption(i18n("New Color Scheme"));
696
    else
Kurt Hindenburg's avatar
Kurt Hindenburg committed
697
        dialog.data()->setCaption(i18n("Edit Color Scheme"));
698
699

    ColorSchemeEditor* editor = new ColorSchemeEditor;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
700
    dialog.data()->setMainWidget(editor);
701
702
    editor->setup(colors);

Kurt Hindenburg's avatar
Kurt Hindenburg committed
703
    if (isNewScheme)
704
        editor->setDescription(i18n("New Color Scheme"));
705

Kurt Hindenburg's avatar
Kurt Hindenburg committed
706
    if (dialog.data()->exec() == QDialog::Accepted) {
707
        ColorScheme* newScheme = new ColorScheme(*editor->colorScheme());
708

709
        // if this is a new color scheme, pick a name based on the description
Kurt Hindenburg's avatar
Kurt Hindenburg committed
710
        if (isNewScheme)
711
            newScheme->setName(newScheme->description());
712

Kurt Hindenburg's avatar
Kurt Hindenburg committed
713
        ColorSchemeManager::instance()->addColorScheme(newScheme);
714

Robert Knight's avatar
   
Robert Knight committed
715
        updateColorSchemeList(true);