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

    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"

23
// Qt
Dirk Mueller's avatar
Dirk Mueller committed
24 25 26 27 28
#include <QtGui/QKeyEvent>
#include <QtGui/QBrush>
#include <QtGui/QPainter>
#include <QtGui/QStandardItem>
#include <QtCore/QDebug>
29
#include <QtCore/QTextCodec>
30
#include <QtGui/QTextEdit>
31 32 33
#include <QtGui/QLinearGradient>
#include <QtGui/QRadialGradient>

34
#include <QtCore/QTimer>
35 36
#include <QtCore/QTimeLine>

37
// KDE
38
#include <kcodecaction.h>
39 40
#include <KFontDialog>
#include <KIcon>
41
#include <KIconDialog>
42
#include <KFileDialog>
43
#include <KUrlCompletion>
44
#include <KWindowSystem>
45 46 47

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

using namespace Konsole;

59
EditProfileDialog::EditProfileDialog(QWidget* parent)
60
    : KDialog(parent)
Robert Knight's avatar
 
Robert Knight committed
61
    , _colorSchemeAnimationTimeLine(0)
62
    , _delayedPreviewTimer(new QTimer(this))
63
{
64
    setCaption(i18n("Edit Profile"));
65 66
    setButtons( KDialog::Ok | KDialog::Cancel | KDialog::Apply | KDialog::Default );

67
    connect( this , SIGNAL(applyClicked()) , this , SLOT(save()) );
68
    connect( _delayedPreviewTimer , SIGNAL(timeout()) , this , SLOT(delayedPreviewActivate()) );
69
    _ui = new Ui::EditProfileDialog();
70
    _ui->setupUi(mainWidget());
71

72 73 74 75 76 77 78 79 80 81 82
    // 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
    _pageNeedsUpdate.resize( _ui->tabWidget->count() );
83
    connect( _ui->tabWidget , SIGNAL(currentChanged(int)) , this , 
Robert Knight's avatar
 
Robert Knight committed
84
            SLOT(preparePage(int)) );
85

86
    _tempProfile = new Profile;
87
}
88
EditProfileDialog::~EditProfileDialog()
89 90
{
    delete _ui;
91 92
    delete _tempProfile;
}
93
void EditProfileDialog::save()
94
{
95 96
    if ( _tempProfile->isEmpty() )
        return;
97 98

    SessionManager::instance()->changeProfile(_profileKey,_tempProfile->setProperties());
99 100 101 102 103 104 105 106 107 108 109 110 111 112

    // ensure that these settings are not undone by a call
    // to unpreview()
    QHashIterator<Profile::Property,QVariant> iter(_tempProfile->setProperties());
    while ( iter.hasNext() )
    {
        iter.next();
        _previewedProperties.remove(iter.key());
    }
}
void EditProfileDialog::reject()
{
    unpreviewAll();
    KDialog::reject();
113 114 115 116
}
void EditProfileDialog::accept()
{
    save();
117
    unpreviewAll();
118
    KDialog::accept(); 
119
}
Robert Knight's avatar
 
Robert Knight committed
120 121 122 123
void EditProfileDialog::updateCaption(const QString& profileName)
{
    setCaption( i18n("Edit Profile \"%1\"",profileName) );
}
124
void EditProfileDialog::setProfile(const QString& key)
125
{
126
    _profileKey = key;
127

128
    const Profile* info = SessionManager::instance()->profile(key);
129 130 131

    Q_ASSERT( info );

132
    // update caption
Robert Knight's avatar
 
Robert Knight committed
133
    updateCaption(info->name());
134

135 136 137 138 139
    // 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);
Robert Knight's avatar
 
Robert Knight committed
140
    preparePage( _ui->tabWidget->currentIndex() );
141

142 143 144 145 146
    if ( _tempProfile )
    {
        delete _tempProfile;
        _tempProfile = new Profile;
    }
147
}
148 149 150 151
const Profile* EditProfileDialog::lookupProfile() const
{
    return SessionManager::instance()->profile(_profileKey);
}
Robert Knight's avatar
 
Robert Knight committed
152
void EditProfileDialog::preparePage(int page)
153
{
154
    const Profile* info = lookupProfile();
155

156
    Q_ASSERT( _pageNeedsUpdate.count() > page );
157 158
    Q_ASSERT( info );

Robert Knight's avatar
 
Robert Knight committed
159 160
    QWidget* pageWidget = _ui->tabWidget->widget(page);
    
161
    if ( _pageNeedsUpdate[page] )
162 163 164
    {
       if ( pageWidget == _ui->generalTab )
            setupGeneralPage(info);
165 166
       else if ( pageWidget == _ui->tabsTab )
            setupTabsPage(info);
167 168 169 170 171 172 173 174 175 176 177
       else if ( pageWidget == _ui->appearanceTab )
            setupAppearancePage(info);
       else if ( pageWidget == _ui->scrollingTab )
            setupScrollingPage(info);
       else if ( pageWidget == _ui->keyboardTab )
            setupKeyboardPage(info);
       else if ( pageWidget == _ui->advancedTab )
            setupAdvancedPage(info);
       else
           Q_ASSERT(false);

178
        _pageNeedsUpdate[page] = false;
179
    }
Robert Knight's avatar
 
Robert Knight committed
180 181 182 183 184 185 186 187 188

    // start page entry animation for color schemes
    if ( pageWidget == _ui->appearanceTab )
            _colorSchemeAnimationTimeLine->start();
}
void EditProfileDialog::selectProfileName()
{
    _ui->profileNameEdit->selectAll();
    _ui->profileNameEdit->setFocus();
189
}
190
void EditProfileDialog::setupGeneralPage(const Profile* info)
191
{
192 193

    // basic profile options
194
    _ui->profileNameEdit->setText( info->name() );
195 196 197

    ShellCommand command( info->command() , info->arguments() );
    _ui->commandEdit->setText( command.fullCommand() );
198 199

    KUrlCompletion* exeCompletion = new KUrlCompletion(KUrlCompletion::ExeCompletion);
200
    exeCompletion->setParent(this);
201
    exeCompletion->setDir(QString());
202
    _ui->commandEdit->setCompletionObject( exeCompletion );
203
    _ui->initialDirEdit->setText( info->defaultWorkingDirectory() );
204 205 206 207

    KUrlCompletion* dirCompletion = new KUrlCompletion(KUrlCompletion::DirCompletion);
    dirCompletion->setParent(this);
    _ui->initialDirEdit->setCompletionObject( dirCompletion );
208
    _ui->initialDirEdit->setClearButtonShown(true);
209
    _ui->dirSelectButton->setIcon( KIcon("folder-open") );
210
    _ui->iconSelectButton->setIcon( KIcon(info->icon()) );
211

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    // window options
    _ui->showMenuBarButton->setChecked( info->property(Profile::ShowMenuBar).value<bool>() );

    // signals and slots
    connect( _ui->dirSelectButton , SIGNAL(clicked()) , this , SLOT(selectInitialDir()) );
    connect( _ui->iconSelectButton , SIGNAL(clicked()) , this , SLOT(selectIcon()) );

    connect( _ui->profileNameEdit , SIGNAL(textChanged(const QString&)) , this ,
            SLOT(profileNameChanged(const QString&)) );
    connect( _ui->initialDirEdit , SIGNAL(textChanged(const QString&)) , this , 
            SLOT(initialDirChanged(const QString&)) );
    connect(_ui->commandEdit , SIGNAL(textChanged(const QString&)) , this ,
            SLOT(commandChanged(const QString&)) ); 
    
    connect(_ui->showMenuBarButton , SIGNAL(toggled(bool)) , this , 
            SLOT(showMenuBar(bool)) );

    connect(_ui->environmentEditButton , SIGNAL(clicked()) , this , 
            SLOT(showEnvironmentEditor()) );
}
void EditProfileDialog::showEnvironmentEditor()
{
    const Profile* info = lookupProfile();

    KDialog* dialog = new KDialog(this);
    QTextEdit* edit = new QTextEdit(dialog);

    QStringList currentEnvironment = info->property(Profile::Environment).value<QStringList>();

    edit->setPlainText( currentEnvironment.join("\n") );
    dialog->setPlainCaption(i18n("Edit Environment"));
    dialog->setMainWidget(edit);

    if ( dialog->exec() == QDialog::Accepted )
    {
        QStringList newEnvironment = edit->toPlainText().split('\n');
        _tempProfile->setProperty(Profile::Environment,newEnvironment);
    }

    dialog->deleteLater();
}
void EditProfileDialog::setupTabsPage(const Profile* info)
{
255 256 257
    // tab title format
    _ui->tabTitleEdit->setClearButtonShown(true);
    _ui->remoteTabTitleEdit->setClearButtonShown(true);
258 259 260 261
    _ui->tabTitleEdit->setText( info->property(Profile::LocalTabTitleFormat).value<QString>() );
    _ui->remoteTabTitleEdit->setText( 
            info->property(Profile::RemoteTabTitleFormat).value<QString>());

262
    // tab options
263
    int tabMode = info->property(Profile::TabBarMode).value<int>();
264
    int tabPosition = info->property(Profile::TabBarPosition).value<int>();
265

266 267 268 269 270 271 272 273 274 275 276 277 278 279
    // note: Items should be in the same order as the 
    // Profile::TabBarModeEnum enum
    _ui->tabBarVisibilityCombo->addItems( QStringList() << i18n("Always hide tab bar")
                                                        << i18n("Show tab bar when needed") 
                                                        << i18n("Always show tab bar") );
    _ui->tabBarVisibilityCombo->setCurrentIndex(tabMode);

    // note: Items should be in the same order as the
    // Profile::TabBarPositionEnum enum
    _ui->tabBarPositionCombo->addItems( QStringList() << i18n("Below terminal displays")
                                                      << i18n("Above terminal displays") );

    _ui->tabBarPositionCombo->setCurrentIndex(tabPosition);

280
    // signals and slots
281 282 283 284
    connect( _ui->tabBarVisibilityCombo , SIGNAL(activated(int)) , this , 
             SLOT(tabBarVisibilityChanged(int)) );
    connect( _ui->tabBarPositionCombo , SIGNAL(activated(int)) , this ,
             SLOT(tabBarPositionChanged(int)) );
285

286 287 288 289
    connect(_ui->tabTitleEdit , SIGNAL(textChanged(const QString&)) , this ,
            SLOT(tabTitleFormatChanged(const QString&)) );
    connect(_ui->remoteTabTitleEdit , SIGNAL(textChanged(const QString&)) , this ,
            SLOT(remoteTabTitleFormatChanged(const QString&)));
290

291 292 293 294 295 296 297 298 299 300 301 302
    // menus for local and remote tab title dynamic elements
    TabTitleFormatAction* localTabTitleAction = new TabTitleFormatAction(this);
    localTabTitleAction->setContext(Session::LocalTabTitle);
    _ui->tabTitleEditButton->setMenu(localTabTitleAction->menu());
    connect( localTabTitleAction , SIGNAL(dynamicElementSelected(const QString&)) , 
            this , SLOT(insertTabTitleText(const QString&)) );

    TabTitleFormatAction* remoteTabTitleAction = new TabTitleFormatAction(this);
    remoteTabTitleAction->setContext(Session::RemoteTabTitle);
    _ui->remoteTabTitleEditButton->setMenu(remoteTabTitleAction->menu());
    connect( remoteTabTitleAction , SIGNAL(dynamicElementSelected(const QString&)) ,
           this , SLOT(insertRemoteTabTitleText(const QString&)) ); 
303
}
304 305 306 307 308 309 310 311
void EditProfileDialog::tabBarVisibilityChanged(int newValue)
{
    _tempProfile->setProperty( Profile::TabBarMode , newValue );
}
void EditProfileDialog::tabBarPositionChanged(int newValue)
{
    _tempProfile->setProperty( Profile::TabBarPosition , newValue );
}
312 313 314 315 316 317 318 319
void EditProfileDialog::insertTabTitleText(const QString& text)
{
    _ui->tabTitleEdit->insert(text);
}
void EditProfileDialog::insertRemoteTabTitleText(const QString& text)
{
    _ui->remoteTabTitleEdit->insert(text);
}
320 321 322 323
void EditProfileDialog::showMenuBar(bool show)
{
    _tempProfile->setProperty(Profile::ShowMenuBar,show);
}
324 325 326 327 328 329 330 331 332 333 334
void EditProfileDialog::tabTitleFormatChanged(const QString& format)
{
    _tempProfile->setProperty(Profile::LocalTabTitleFormat,format);
}
void EditProfileDialog::remoteTabTitleFormatChanged(const QString& format)
{
    _tempProfile->setProperty(Profile::RemoteTabTitleFormat,format);
}

void EditProfileDialog::selectIcon()
{
Craig Drummond's avatar
Craig Drummond committed
335 336
    const QString& icon = KIconDialog::getIcon(KIconLoader::Desktop, KIconLoader::Application,
                                               false, 0, false, this);
337 338 339 340 341 342 343 344 345
    if (!icon.isEmpty())
    {
        _ui->iconSelectButton->setIcon( KIcon(icon) );
        _tempProfile->setProperty(Profile::Icon,icon);
    }
}
void EditProfileDialog::profileNameChanged(const QString& text)
{
    _tempProfile->setProperty(Profile::Name,text);
Robert Knight's avatar
 
Robert Knight committed
346
    updateCaption(_tempProfile->name());
347 348 349 350 351 352 353
}
void EditProfileDialog::initialDirChanged(const QString& dir)
{
    _tempProfile->setProperty(Profile::Directory,dir);
}
void EditProfileDialog::commandChanged(const QString& command)
{
354 355 356 357
    ShellCommand shellCommand(command);

    _tempProfile->setProperty(Profile::Command,shellCommand.command());
    _tempProfile->setProperty(Profile::Arguments,shellCommand.arguments());
358 359 360
}
void EditProfileDialog::selectInitialDir()
{
361 362 363
    const KUrl url = KFileDialog::getExistingDirectoryUrl(_ui->initialDirEdit->text(),
                                                          this,
                                                          i18n("Select Initial Directory"));
364 365 366

    if ( !url.isEmpty() )
        _ui->initialDirEdit->setText(url.path());
367
}
368
void EditProfileDialog::setupAppearancePage(const Profile* info)
369
{
370
    ColorSchemeViewDelegate* delegate = new ColorSchemeViewDelegate(this);
371 372
    _ui->colorSchemeList->setItemDelegate(delegate);
    
Robert Knight's avatar
 
Robert Knight committed
373 374
    _colorSchemeAnimationTimeLine = new QTimeLine( 500 , this );
    delegate->setEntryTimeLine(_colorSchemeAnimationTimeLine);
375
    
Robert Knight's avatar
 
Robert Knight committed
376
    connect( _colorSchemeAnimationTimeLine , SIGNAL(valueChanged(qreal)) , this ,
377
             SLOT(colorSchemeAnimationUpdate()) );
Robert Knight's avatar
 
Robert Knight committed
378 379
   
    _ui->transparencyWarningWidget->setVisible(false);
380 381 382 383
    
    // setup color list
    updateColorSchemeList(true);

384 385
    _ui->colorSchemeList->setMouseTracking(true);
    _ui->colorSchemeList->installEventFilter(this);
Robert Knight's avatar
 
Robert Knight committed
386
    _ui->colorSchemeList->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
387

388 389 390
    connect( _ui->colorSchemeList->selectionModel() , 
            SIGNAL(selectionChanged(const QItemSelection&,const QItemSelection&)) 
            , this , SLOT(colorSchemeSelected()) );
391 392 393
    connect( _ui->colorSchemeList , SIGNAL(entered(const QModelIndex&)) , this , 
            SLOT(previewColorScheme(const QModelIndex&)) );

394 395 396 397 398 399
    connect( _ui->editColorSchemeButton , SIGNAL(clicked()) , this , 
            SLOT(editColorScheme()) );
    connect( _ui->removeColorSchemeButton , SIGNAL(clicked()) , this ,
            SLOT(removeColorScheme()) );
    connect( _ui->newColorSchemeButton , SIGNAL(clicked()) , this , 
            SLOT(newColorScheme()) );
400

401
    // setup font preview
402
    const QFont& font = info->font();
403
    updateFontPreviewLabel(font);
404
    _ui->fontSizeSlider->setValue( font.pointSize() );
405
    _ui->fontSizeSlider->setMinimum( KGlobalSettings::smallestReadableFont().pointSize() );
406 407 408 409 410

    connect( _ui->fontSizeSlider , SIGNAL(valueChanged(int)) , this ,
             SLOT(setFontSize(int)) );
    connect( _ui->editFontButton , SIGNAL(clicked()) , this ,
             SLOT(showFontDialog()) );
411 412 413 414 415 416 417
}
void EditProfileDialog::colorSchemeAnimationUpdate()
{
    QAbstractItemModel* model = _ui->colorSchemeList->model();

    for ( int i = model->rowCount() ; i >= 0 ; i-- )
        _ui->colorSchemeList->update( model->index(i,0) );
418
}
419 420 421 422 423
void EditProfileDialog::updateFontPreviewLabel(const QFont& font)
{
    _ui->fontPreviewLabel->setFont(font);
    _ui->fontPreviewLabel->setText(i18n("%1, size %2",font.family(),font.pointSize()));
}
424
void EditProfileDialog::updateColorSchemeList(bool selectCurrentScheme)
425 426 427
{
    delete _ui->colorSchemeList->model();

428
    const QString& name = lookupProfile()->colorScheme();
429 430 431 432 433 434
    const ColorScheme* currentScheme = ColorSchemeManager::instance()->findColorScheme(name);

    QStandardItemModel* model = new QStandardItemModel(this);
    QList<const ColorScheme*> schemeList = ColorSchemeManager::instance()->allColorSchemes();
    QListIterator<const ColorScheme*> schemeIter(schemeList);

435 436
    QStandardItem* selectedItem = 0;

437 438 439 440 441
    while (schemeIter.hasNext())
    {
        const ColorScheme* colors = schemeIter.next();
        QStandardItem* item = new QStandardItem(colors->description());
        item->setData( QVariant::fromValue(colors) ,  Qt::UserRole + 1);
442
        item->setFlags( item->flags() );
443 444
       
        if ( currentScheme == colors ) 
Robert Knight's avatar
 
Robert Knight committed
445 446
            selectedItem = item;  

447 448 449
        model->appendRow(item);
    }

450 451
    model->sort(0);

452 453
    _ui->colorSchemeList->setModel(model);

454
    if ( selectCurrentScheme )
455 456 457 458 459 460
    {
        Q_ASSERT( selectedItem );

        _ui->colorSchemeList->updateGeometry();
        _ui->colorSchemeList->selectionModel()->setCurrentIndex( selectedItem->index() , 
                                                                 QItemSelectionModel::Select );
Robert Knight's avatar
 
Robert Knight committed
461 462 463

        // update transparency warning label
        updateTransparencyWarning();
464
    }
465
}
466
void EditProfileDialog::updateKeyBindingsList(bool selectCurrentTranslator)
467
{
468 469
    KeyboardTranslatorManager* keyManager = KeyboardTranslatorManager::instance();

470 471
    delete _ui->keyBindingList->model();

472
    const QString& name = lookupProfile()
473
                                    ->property(Profile::KeyBindings).value<QString>();
474

475
    const KeyboardTranslator* currentTranslator = keyManager->findTranslator(name);
476 477 478
    
    QStandardItemModel* model = new QStandardItemModel(this);

479 480
    QStandardItem* selectedItem = 0;

481
    QList<QString> translatorNames = keyManager->allTranslators();
482 483 484 485 486
    QListIterator<QString> iter(translatorNames);
    while (iter.hasNext())
    {
        const QString& name = iter.next();

487
        const KeyboardTranslator* translator = keyManager->findTranslator(name);
488 489 490

        QStandardItem* item = new QStandardItem(translator->description());
        item->setData(QVariant::fromValue(translator),Qt::UserRole+1);
491
        item->setIcon( KIcon("keyboard") );
492 493

        if ( translator == currentTranslator )
494
            selectedItem = item;
495 496 497 498

        model->appendRow(item);
    }

499
    model->sort(0);
500
    _ui->keyBindingList->setModel(model);
501 502 503 504 505 506 507 508
    
    if ( selectCurrentTranslator )
    {
        Q_ASSERT( selectedItem );

        _ui->keyBindingList->selectionModel()->setCurrentIndex( selectedItem->index() , 
                                                                  QItemSelectionModel::Select );
    }
509
}
510 511 512 513
bool EditProfileDialog::eventFilter( QObject* watched , QEvent* event )
{
    if ( watched == _ui->colorSchemeList && event->type() == QEvent::Leave )
    {
514 515 516 517
        if ( _tempProfile->isPropertySet(Profile::ColorScheme) )
            preview(Profile::ColorScheme,_tempProfile->colorScheme());
        else
            unpreview(Profile::ColorScheme);
518 519 520 521
    }

    return KDialog::eventFilter(watched,event);
}
522
void EditProfileDialog::unpreviewAll()
523
{
524 525 526
    _delayedPreviewTimer->stop();
    _delayedPreviewProperties.clear();

527 528 529 530 531
    QHash<Profile::Property,QVariant> map;
    QHashIterator<int,QVariant> iter(_previewedProperties);
    while ( iter.hasNext() )
    {
        iter.next();
532
        map.insert((Profile::Property)iter.key(),iter.value());
533 534 535
    }

    // undo any preview changes
536
    if ( !map.isEmpty() )
537 538 539 540
        SessionManager::instance()->changeProfile(_profileKey,map,false);
}
void EditProfileDialog::unpreview(int property)
{
541 542
    _delayedPreviewProperties.remove(property);
    
543 544
    if (!_previewedProperties.contains(property))
        return;
545
    
546 547 548 549 550 551
    QHash<Profile::Property,QVariant> map;
    map.insert((Profile::Property)property,_previewedProperties[property]);
    SessionManager::instance()->changeProfile(_profileKey,map,false); 

    _previewedProperties.remove(property);
}
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
void EditProfileDialog::delayedPreview(int property , const QVariant& value)
{
    _delayedPreviewProperties.insert(property,value); 
    QTimer* timer = new QTimer(this);
    timer->setSingleShot(true);

    _delayedPreviewTimer->stop();
    _delayedPreviewTimer->start(300);    
}
void EditProfileDialog::delayedPreviewActivate()
{
    Q_ASSERT( qobject_cast<QTimer*>(sender()) );

    QMutableHashIterator<int,QVariant> iter(_delayedPreviewProperties);
    if ( iter.hasNext() ) 
    {
        iter.next();
        preview(iter.key(),iter.value());
    }
}
Robert Knight's avatar
Robert Knight committed
572
void EditProfileDialog::preview(int property , const QVariant& value)
573 574 575
{
    QHash<Profile::Property,QVariant> map;
    map.insert((Profile::Property)property,value);
576 577
    
    _delayedPreviewProperties.remove(property);
578

579
    const Profile* original = lookupProfile();
580 581 582 583 584 585 586 587 588 589 590

    if (!_previewedProperties.contains(property))    
        _previewedProperties.insert(property , original->property((Profile::Property)property) );

    // temporary change to color scheme
    SessionManager::instance()->changeProfile( _profileKey , map , false);
}
void EditProfileDialog::previewColorScheme(const QModelIndex& index)
{
    const QString& name = index.data(Qt::UserRole+1).value<const ColorScheme*>()->name();

591
    delayedPreview( Profile::ColorScheme , name );
592
}
593 594
void EditProfileDialog::removeColorScheme()
{
595 596 597 598 599 600 601 602
    QModelIndexList selected = _ui->colorSchemeList->selectionModel()->selectedIndexes();

    if ( !selected.isEmpty() )
    {
        const QString& name = selected.first().data(Qt::UserRole+1).value<const ColorScheme*>()->name();
        ColorSchemeManager::instance()->deleteColorScheme(name);
        _ui->colorSchemeList->model()->removeRow(selected.first().row());
    }
603
}
604
void EditProfileDialog::showColorSchemeEditor(bool isNewScheme)
605 606 607
{    
    QModelIndexList selected = _ui->colorSchemeList->selectionModel()->selectedIndexes();

608 609
    QAbstractItemModel* model = _ui->colorSchemeList->model();
    QModelIndex index;
610
    if ( !selected.isEmpty() )
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
        index = selected.first();
    else
        index = model->index(0,0); // use the first item in the list

    const ColorScheme* colors = model->data(index,Qt::UserRole+1).value<const ColorScheme*>();

    KDialog* dialog = new KDialog(this);

    if ( isNewScheme )
        dialog->setCaption(i18n("New Color Scheme"));
    else
        dialog->setCaption(i18n("Edit Color Scheme"));

    ColorSchemeEditor* editor = new ColorSchemeEditor;
    dialog->setMainWidget(editor);
    editor->setup(colors);

    if ( isNewScheme )
        editor->setDescription(i18n("New Color Scheme"));
        
    if ( dialog->exec() == QDialog::Accepted )
632
    {
633
        ColorScheme* newScheme = new ColorScheme(*editor->colorScheme());
634

635 636 637
        // if this is a new color scheme, pick a name based on the description
        if ( isNewScheme )
            newScheme->setName(newScheme->description());
638

639 640
        ColorSchemeManager::instance()->addColorScheme( newScheme );
        
Robert Knight's avatar
 
Robert Knight committed
641
        updateColorSchemeList(true);
642

Robert Knight's avatar
 
Robert Knight committed
643
        preview(Profile::ColorScheme,newScheme->name());
644 645 646 647 648 649 650 651 652 653
    }
}
void EditProfileDialog::newColorScheme()
{
    showColorSchemeEditor(true);    
}
void EditProfileDialog::editColorScheme()
{
    showColorSchemeEditor(false);
}
654 655 656 657 658 659 660
void EditProfileDialog::colorSchemeSelected()
{
    QModelIndexList selected = _ui->colorSchemeList->selectionModel()->selectedIndexes();

    if ( !selected.isEmpty() )
    {
        QAbstractItemModel* model = _ui->colorSchemeList->model();
661
        const ColorScheme* colors = model->data(selected.first(),Qt::UserRole+1).value<const ColorScheme*>();
662 663

        _tempProfile->setProperty(Profile::ColorScheme,colors->name());
Robert Knight's avatar
 
Robert Knight committed
664 665 666 667 668 669 670 671 672 673 674 675 676

        updateTransparencyWarning();
    }
}
void EditProfileDialog::updateTransparencyWarning() 
{
    // zero or one indexes can be selected
    foreach( const QModelIndex& index , _ui->colorSchemeList->selectionModel()->selectedIndexes() ) 
    {
        bool hasTransparency = index.data(Qt::UserRole+1).value<const ColorScheme*>()->opacity() < 1.0;

        _ui->transparencyWarningWidget->setHidden(KWindowSystem::compositingActive() || !hasTransparency);
        _ui->transparencyWarningIcon->setPixmap( KIcon("dialog-warning").pixmap(QSize(48,48)) );
677 678
    }
}
Robert Knight's avatar
 
Robert Knight committed
679
void EditProfileDialog::setupKeyboardPage(const Profile* /* info */)
680 681
{
    // setup translator list
682
    updateKeyBindingsList(true); 
683

684 685 686
    connect( _ui->keyBindingList->selectionModel() , 
                SIGNAL(selectionChanged(const QItemSelection&,const QItemSelection&)),
                SLOT(keyBindingSelected()) );
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
    connect( _ui->newKeyBindingsButton , SIGNAL(clicked()) , this ,
            SLOT(newKeyBinding()) );
    connect( _ui->editKeyBindingsButton , SIGNAL(clicked()) , this , 
          SLOT(editKeyBinding()) );  
    connect( _ui->removeKeyBindingsButton , SIGNAL(clicked()) , this ,
            SLOT(removeKeyBinding()) );
}
void EditProfileDialog::keyBindingSelected()
{
    QModelIndexList selected = _ui->keyBindingList->selectionModel()->selectedIndexes();

    if ( !selected.isEmpty() )
    {
        QAbstractItemModel* model = _ui->keyBindingList->model();
        const KeyboardTranslator* translator = model->data(selected.first(),Qt::UserRole+1)
                                                .value<const KeyboardTranslator*>();
        _tempProfile->setProperty(Profile::KeyBindings,translator->name());
    }
}
void EditProfileDialog::removeKeyBinding()
{
    QModelIndexList selected = _ui->keyBindingList->selectionModel()->selectedIndexes();

    if ( !selected.isEmpty() )
    {
        const QString& name = selected.first().data(Qt::UserRole+1).value<const KeyboardTranslator*>()->name();
        KeyboardTranslatorManager::instance()->deleteTranslator(name);
        _ui->keyBindingList->model()->removeRow(selected.first().row());   
    }
}
717
void EditProfileDialog::showKeyBindingEditor(bool isNewTranslator)
718 719
{
    QModelIndexList selected = _ui->keyBindingList->selectionModel()->selectedIndexes();
720
    QAbstractItemModel* model = _ui->keyBindingList->model();
721

722
    QModelIndex index;
723
    if ( !selected.isEmpty() )
724 725 726
        index = selected.first();
    else
        index = model->index(0,0); // Use first item if there is no selection
727 728


729 730 731
    const KeyboardTranslator* translator = model->data(index,
                                            Qt::UserRole+1).value<const KeyboardTranslator*>();
    KDialog* dialog = new KDialog(this);
732

733 734 735 736
    if ( isNewTranslator )
        dialog->setCaption(i18n("New Key Binding List"));
    else
        dialog->setCaption(i18n("Edit Key Binding List"));
737

738 739
    KeyBindingEditor* editor = new KeyBindingEditor;
    dialog->setMainWidget(editor);
740 741 742
    
    if ( translator )
        editor->setup(translator);
743 744 745 746 747 748 749 750 751 752

    if ( isNewTranslator )
        editor->setDescription(i18n("New Key Binding List"));

    if ( dialog->exec() == QDialog::Accepted )
    {
        KeyboardTranslator* newTranslator = new KeyboardTranslator(*editor->translator());

        if ( isNewTranslator )
            newTranslator->setName(newTranslator->description());
753

754
        KeyboardTranslatorManager::instance()->addTranslator( newTranslator );
755

756 757
        updateKeyBindingsList();
        
758
        const QString& currentTranslator = lookupProfile()
759
                                        ->property(Profile::KeyBindings).value<QString>();
760

761 762 763
        if ( newTranslator->name() == currentTranslator )
        {
            _tempProfile->setProperty(Profile::KeyBindings,newTranslator->name());
764
        }
765 766
    }
}
767 768 769 770 771
void EditProfileDialog::newKeyBinding()
{
    showKeyBindingEditor(true);
}
void EditProfileDialog::editKeyBinding()
772
{
773
    showKeyBindingEditor(false);
774
}
Robert Knight's avatar
Robert Knight committed
775
void EditProfileDialog::setupCombo( ComboOption* options , const Profile* profile )
776
{
Robert Knight's avatar
Robert Knight committed
777
    while ( options->button != 0 )
778
    {
Robert Knight's avatar
Robert Knight committed
779 780 781 782 783 784 785 786 787 788 789 790
        options->button->setChecked( profile->property((Profile::Property)options->property).value<bool>() );
        connect( options->button , SIGNAL(toggled(bool)) , this , options->slot );

        ++options;
    }
}
void EditProfileDialog::setupRadio( RadioOption* possible , int actual )
{
    while (possible->button != 0)
    {
        if ( possible->property == actual )
            possible->button->setChecked(true);
791
        else
Robert Knight's avatar
Robert Knight committed
792 793 794 795
            possible->button->setChecked(false);
   
        connect( possible->button , SIGNAL(clicked()) , this , possible->slot );

796 797
        ++possible;
    }
798
}
799 800

void EditProfileDialog::setupScrollingPage(const Profile* profile)
801
{
802 803 804
    // setup scrollbar radio
    int scrollBarPosition = profile->property(Profile::ScrollBarPosition).value<int>();
   
Robert Knight's avatar
Robert Knight committed
805 806 807 808 809
    RadioOption positions[] = { {_ui->scrollBarHiddenButton,Profile::ScrollBarHidden,SLOT(hideScrollBar())},
                                {_ui->scrollBarLeftButton,Profile::ScrollBarLeft,SLOT(showScrollBarLeft())},
                                {_ui->scrollBarRightButton,Profile::ScrollBarRight,SLOT(showScrollBarRight())},
                                {0,0,0} 
                              }; 
810

Robert Knight's avatar
Robert Knight committed
811
    setupRadio( positions , scrollBarPosition );
812 813 814 815
   
    // setup scrollback type radio
    int scrollBackType = profile->property(Profile::HistoryMode).value<int>();
    
Robert Knight's avatar
Robert Knight committed
816 817 818 819 820
    RadioOption types[] = { {_ui->disableScrollbackButton,Profile::DisableHistory,SLOT(noScrollBack())},
                            {_ui->fixedScrollbackButton,Profile::FixedSizeHistory,SLOT(fixedScrollBack())},
                            {_ui->unlimitedScrollbackButton,Profile::UnlimitedHistory,SLOT(unlimitedScrollBack())},
                            {0,0,0} };
    setupRadio( types , scrollBackType ); 
821 822 823 824 825 826 827
    
    // setup scrollback line count spinner
    _ui->scrollBackLinesSpinner->setValue( profile->property(Profile::HistorySize).value<int>() );

    // signals and slots
    connect( _ui->scrollBackLinesSpinner , SIGNAL(valueChanged(int)) , this , 
            SLOT(scrollBackLinesChanged(int)) );
828 829
}

830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
void EditProfileDialog::scrollBackLinesChanged(int lineCount)
{
    _tempProfile->setProperty(Profile::HistorySize , lineCount);
}
void EditProfileDialog::noScrollBack()
{
    _tempProfile->setProperty(Profile::HistoryMode , Profile::DisableHistory);
}
void EditProfileDialog::fixedScrollBack()
{
    _tempProfile->setProperty(Profile::HistoryMode , Profile::FixedSizeHistory);
}
void EditProfileDialog::unlimitedScrollBack()
{
    _tempProfile->setProperty(Profile::HistoryMode , Profile::UnlimitedHistory );
}
void EditProfileDialog::hideScrollBar()
{
    _tempProfile->setProperty(Profile::ScrollBarPosition , Profile::ScrollBarHidden );
}
void EditProfileDialog::showScrollBarLeft()
{
    _tempProfile->setProperty(Profile::ScrollBarPosition , Profile::ScrollBarLeft );
}
void EditProfileDialog::showScrollBarRight()
{
    _tempProfile->setProperty(Profile::ScrollBarPosition , Profile::ScrollBarRight );
}
void EditProfileDialog::setupAdvancedPage(const Profile* profile)
{
Robert Knight's avatar
Robert Knight committed
860 861 862 863 864 865 866 867 868 869 870
    ComboOption  options[] = { { _ui->enableBlinkingTextButton , Profile::BlinkingTextEnabled , 
                                 SLOT(toggleBlinkingText(bool)) },
                               { _ui->enableFlowControlButton , Profile::FlowControlEnabled ,
                                 SLOT(toggleFlowControl(bool)) },
                               { _ui->enableResizeWindowButton , Profile::AllowProgramsToResizeWindow ,
                                 SLOT(toggleResizeWindow(bool)) },
                               { _ui->enableBlinkingCursorButton , Profile::BlinkingCursorEnabled ,
                                 SLOT(toggleBlinkingCursor(bool)) },
                               { 0 , 0 , 0 }
                             };
    setupCombo( options , profile );
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894

    // interaction options
    _ui->wordCharacterEdit->setText( profile->property(Profile::WordCharacters).value<QString>() );

    connect( _ui->wordCharacterEdit , SIGNAL(textChanged(const QString&)) , this , 
            SLOT(wordCharactersChanged(const QString&)) );

    // cursor options
    if ( profile->property(Profile::UseCustomCursorColor).value<bool>() )
        _ui->customCursorColorButton->setChecked(true);
    else
        _ui->autoCursorColorButton->setChecked(true);

    _ui->customColorSelectButton->setColor( profile->property(Profile::CustomCursorColor).value<QColor>() );

    connect( _ui->customCursorColorButton , SIGNAL(clicked()) , this , SLOT(customCursorColor()) );
    connect( _ui->autoCursorColorButton , SIGNAL(clicked()) , this , SLOT(autoCursorColor()) );
    connect( _ui->customColorSelectButton , SIGNAL(changed(const QColor&)) , 
            SLOT(customCursorColorChanged(const QColor&)) );

    int shape = profile->property(Profile::CursorShape).value<int>();
    _ui->cursorShapeCombo->setCurrentIndex(shape);

    connect( _ui->cursorShapeCombo , SIGNAL(activated(int)) , this , SLOT(setCursorShape(int)) ); 
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909

    // encoding options
    QAction* codecAction = new KCodecAction(this);
    _ui->selectEncodingButton->setMenu( codecAction->menu() );
    connect( codecAction , SIGNAL(triggered(QTextCodec*)) , this , SLOT(setDefaultCodec(QTextCodec*)) );

    _ui->characterEncodingLabel->setText( profile->property(Profile::DefaultEncoding).value<QString>() );

}
void EditProfileDialog::setDefaultCodec(QTextCodec* codec)
{
    QString name = QString(codec->name());

    _tempProfile->setProperty(Profile::DefaultEncoding,name);
    _ui->characterEncodingLabel->setText(codec->name());
910 911 912 913
}
void EditProfileDialog::customCursorColorChanged(const QColor& color)
{
    _tempProfile->setProperty(Profile::CustomCursorColor,color);
914 915 916

    // ensure that custom cursor colors are enabled
    _ui->customCursorColorButton->click();
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
}
void EditProfileDialog::wordCharactersChanged(const QString& text)
{
    _tempProfile->setProperty(Profile::WordCharacters,text);
}
void EditProfileDialog::autoCursorColor()
{
    _tempProfile->setProperty(Profile::UseCustomCursorColor,false);
}
void EditProfileDialog::customCursorColor()
{
    _tempProfile->setProperty(Profile::UseCustomCursorColor,true);
}
void EditProfileDialog::setCursorShape(int index)
{
    _tempProfile->setProperty(Profile::CursorShape,index);
933 934 935 936
}
void EditProfileDialog::toggleBlinkingCursor(bool enable)
{
    _tempProfile->setProperty(Profile::BlinkingCursorEnabled,enable);
937 938 939 940 941 942 943 944 945 946 947 948 949
}
void EditProfileDialog::toggleBlinkingText(bool enable)
{
    _tempProfile->setProperty(Profile::BlinkingTextEnabled,enable);
}
void EditProfileDialog::toggleFlowControl(bool enable)
{
    _tempProfile->setProperty(Profile::FlowControlEnabled,enable);
}
void EditProfileDialog::toggleResizeWindow(bool enable)
{
    _tempProfile->setProperty(Profile::AllowProgramsToResizeWindow,enable);
}
950
void EditProfileDialog::fontSelected(const QFont& font)
951
{
952 953 954 955 956 957 958 959 960
   QSlider* slider = _ui->fontSizeSlider;
   
   _ui->fontSizeSlider->setRange( qMin(slider->minimum(),font.pointSize()) ,
                                  qMax(slider->maximum(),font.pointSize()) );
   _ui->fontSizeSlider->setValue(font.pointSize());
   _ui->fontPreviewLabel->setFont(font);

   _tempProfile->setProperty(Profile::Font,font);
   preview(Profile::Font,font);
961
}
962
void EditProfileDialog::showFontDialog()
963 964
{
    QFont currentFont = _ui->fontPreviewLabel->font();
965
   
966 967
    KFontDialog* dialog = new KFontDialog(this, KFontChooser::FixedFontsOnly);
    dialog->setFont(currentFont, true);
968

969 970
    connect( dialog , SIGNAL(fontSelected(const QFont&)) , this , SLOT(fontSelected(const QFont&)) );
    dialog->show(); 
971
}
972
void EditProfileDialog::setFontSize(int pointSize)
973 974 975
{
    QFont newFont = _ui->fontPreviewLabel->font();
    newFont.setPointSize(pointSize);
976
    updateFontPreviewLabel(newFont);
977 978

    _tempProfile->setProperty(Profile::Font,newFont);
979 980

    preview(Profile::Font,newFont);
981 982 983