ManageProfilesDialog.cpp 20 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 "ManageProfilesDialog.h"

23
// Qt
24
#include <QtCore/QFileInfo>
25
#include <QStandardItem>
26

27 28
// KDE
#include <KKeySequenceWidget>
29
#include <KStandardDirs>
30 31
#include <KDebug>

32
// Konsole
33
#include "EditProfileDialog.h"
34
#include "ProfileManager.h"
35 36 37 38
#include "Session.h"
#include "TerminalDisplay.h"
#include "SessionManager.h"
#include "SessionController.h"
39
#include "ui_ManageProfilesDialog.h"
40 41 42

using namespace Konsole;

43 44
ManageProfilesDialog::ManageProfilesDialog(QWidget* aParent)
    : KDialog(aParent)
45
    , _sessionModel(new QStandardItemModel(this))
46
{
47
    setCaption(i18nc("@title:window", "Manage Profiles"));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
48
    setButtons(KDialog::Close);
49

50
    connect(this, SIGNAL(finished()),
51
            ProfileManager::instance(), SLOT(saveSettings()));
52

53
    _ui = new Ui::ManageProfilesDialog();
54 55 56 57
    _ui->setupUi(mainWidget());

    // hide vertical header
    _ui->sessionTable->verticalHeader()->hide();
58 59
    _ui->sessionTable->setShowGrid(false);

Kurt Hindenburg's avatar
Kurt Hindenburg committed
60 61
    _ui->sessionTable->setItemDelegateForColumn(FavoriteStatusColumn, new FavoriteItemDelegate(this));
    _ui->sessionTable->setItemDelegateForColumn(ShortcutColumn, new ShortcutItemDelegate(this));
62
    _ui->sessionTable->setEditTriggers(_ui->sessionTable->editTriggers() | QAbstractItemView::SelectedClicked);
63

64 65 66 67
    // populate the table with profiles
    populateTable();

    // listen for changes to profiles
68
    connect(ProfileManager::instance(), SIGNAL(profileAdded(Profile::Ptr)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
69
            SLOT(addItems(Profile::Ptr)));
70
    connect(ProfileManager::instance(), SIGNAL(profileRemoved(Profile::Ptr)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
71
            SLOT(removeItems(Profile::Ptr)));
72
    connect(ProfileManager::instance(), SIGNAL(profileChanged(Profile::Ptr)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
73
            SLOT(updateItems(Profile::Ptr)));
74
    connect(ProfileManager::instance() ,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
75 76
            SIGNAL(favoriteStatusChanged(Profile::Ptr,bool)), this,
            SLOT(updateFavoriteStatus(Profile::Ptr,bool)));
77

78 79
    // resize the session table to the full width of the table
    _ui->sessionTable->horizontalHeader()->setHighlightSections(false);
Robert Knight's avatar
 
Robert Knight committed
80
    _ui->sessionTable->resizeColumnsToContents();
81

Kurt Hindenburg's avatar
Kurt Hindenburg committed
82
    // allow a larger width for the shortcut column to account for the
83 84 85
    // increased with needed by the shortcut editor compared with just
    // displaying the text of the shortcut
    _ui->sessionTable->setColumnWidth(ShortcutColumn,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
86
                                      _ui->sessionTable->columnWidth(ShortcutColumn) + 100);
Robert Knight's avatar
 
Robert Knight committed
87

88
    // setup buttons
89 90 91 92
    connect(_ui->newProfileButton, SIGNAL(clicked()), this, SLOT(createProfile()));
    connect(_ui->editProfileButton, SIGNAL(clicked()), this, SLOT(editSelected()));
    connect(_ui->deleteProfileButton, SIGNAL(clicked()), this, SLOT(deleteSelected()));
    connect(_ui->setAsDefaultButton, SIGNAL(clicked()), this, SLOT(setSelectedAsDefault()));
93
}
94

Robert Knight's avatar
 
Robert Knight committed
95
void ManageProfilesDialog::showEvent(QShowEvent*)
96
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
97
    Q_ASSERT(_ui->sessionTable->model());
Robert Knight's avatar
 
Robert Knight committed
98 99 100 101 102

    // try to ensure that all the text in all the columns is visible initially.
    // FIXME:  this is not a good solution, look for a more correct way to do this

    int totalWidth = 0;
103
    const int columnCount = _ui->sessionTable->model()->columnCount();
Robert Knight's avatar
 
Robert Knight committed
104

Kurt Hindenburg's avatar
Kurt Hindenburg committed
105
    for (int i = 0 ; i < columnCount ; i++)
Robert Knight's avatar
 
Robert Knight committed
106 107 108
        totalWidth += _ui->sessionTable->columnWidth(i);

    // the margin is added to account for the space taken by the resize grips
Kurt Hindenburg's avatar
Kurt Hindenburg committed
109
    // between the columns, this ensures that a horizontal scroll bar is not added
Robert Knight's avatar
 
Robert Knight committed
110
    // automatically
Kurt Hindenburg's avatar
Kurt Hindenburg committed
111 112
    int margin = style()->pixelMetric(QStyle::PM_HeaderGripMargin) * columnCount;
    _ui->sessionTable->setMinimumWidth(totalWidth + margin);
Robert Knight's avatar
 
Robert Knight committed
113
    _ui->sessionTable->horizontalHeader()->setStretchLastSection(true);
114 115
}

116
ManageProfilesDialog::~ManageProfilesDialog()
117 118 119
{
    delete _ui;
}
120

121 122
void ManageProfilesDialog::itemDataChanged(QStandardItem* item)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
123
    if (item->column() == ShortcutColumn) {
124
        QKeySequence sequence = QKeySequence::fromString(item->text());
125
        ProfileManager::instance()->setShortcut(item->data(ShortcutRole).value<Profile::Ptr>(),
Kurt Hindenburg's avatar
Kurt Hindenburg committed
126 127
                                                sequence);
    } else if (item->column() == ProfileNameColumn) {
128 129
        QString newName = item->text();
        Profile::Ptr profile = item->data(ProfileKeyRole).value<Profile::Ptr>();
130 131
        QString oldName = profile->name();

Kurt Hindenburg's avatar
Kurt Hindenburg committed
132 133
        if (newName != oldName) {
            QHash<Profile::Property, QVariant> properties;
134
            properties.insert(Profile::Name, newName);
135
            properties.insert(Profile::UntranslatedName, newName);
136

137
            ProfileManager::instance()->changeProfile(profile, properties);
138
        }
Kurt Hindenburg's avatar
Kurt Hindenburg committed
139
    }
140
}
141

Jekyll Wu's avatar
Jekyll Wu committed
142
int ManageProfilesDialog::rowForProfile(const Profile::Ptr profile) const
143
{
144 145
    const int rowCount = _sessionModel->rowCount();
    for (int i = 0; i < rowCount; i++) {
Kurt Hindenburg's avatar
Kurt Hindenburg committed
146 147
        if (_sessionModel->item(i, ProfileNameColumn)->data(ProfileKeyRole)
                .value<Profile::Ptr>() == profile) {
148 149 150 151 152
            return i;
        }
    }
    return -1;
}
Jekyll Wu's avatar
Jekyll Wu committed
153
void ManageProfilesDialog::removeItems(const Profile::Ptr profile)
154
{
Jekyll Wu's avatar
Jekyll Wu committed
155
    int row = rowForProfile(profile);
156 157
    if (row < 0)
        return;
Jekyll Wu's avatar
Jekyll Wu committed
158

159 160
    _sessionModel->removeRow(row);
}
Jekyll Wu's avatar
Jekyll Wu committed
161
void ManageProfilesDialog::updateItems(const Profile::Ptr profile)
162
{
163
    const int row = rowForProfile(profile);
164 165 166 167
    if (row < 0)
        return;

    QList<QStandardItem*> items;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
168 169 170
    items << _sessionModel->item(row, ProfileNameColumn);
    items << _sessionModel->item(row, FavoriteStatusColumn);
    items << _sessionModel->item(row, ShortcutColumn);
Jekyll Wu's avatar
Jekyll Wu committed
171

Kurt Hindenburg's avatar
Kurt Hindenburg committed
172
    updateItemsForProfile(profile, items);
173
}
Jekyll Wu's avatar
Jekyll Wu committed
174
void ManageProfilesDialog::updateItemsForProfile(const Profile::Ptr profile, QList<QStandardItem*>& items) const
175 176
{
    // Profile Name
Jekyll Wu's avatar
Jekyll Wu committed
177
    items[ProfileNameColumn]->setText(profile->name());
Kurt Hindenburg's avatar
Kurt Hindenburg committed
178 179 180
    if (!profile->icon().isEmpty())
        items[ProfileNameColumn]->setIcon(KIcon(profile->icon()));
    items[ProfileNameColumn]->setData(QVariant::fromValue(profile), ProfileKeyRole);
181
    items[ProfileNameColumn]->setToolTip(i18nc("@info:tooltip", "Click to rename profile"));
182 183

    // Favorite Status
184
    const bool isFavorite = ProfileManager::instance()->findFavorites().contains(profile);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
185 186
    if (isFavorite)
        items[FavoriteStatusColumn]->setData(KIcon("dialog-ok-apply"), Qt::DecorationRole);
187
    else
Kurt Hindenburg's avatar
Kurt Hindenburg committed
188 189
        items[FavoriteStatusColumn]->setData(KIcon(), Qt::DecorationRole);
    items[FavoriteStatusColumn]->setData(QVariant::fromValue(profile), ProfileKeyRole);
190
    items[FavoriteStatusColumn]->setToolTip(i18nc("@info:tooltip", "Click to toggle status"));
191 192

    // Shortcut
193
    QString shortcut = ProfileManager::instance()->shortcut(profile).toString();
194
    items[ShortcutColumn]->setText(shortcut);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
195
    items[ShortcutColumn]->setData(QVariant::fromValue(profile), ShortcutRole);
196
    items[ShortcutColumn]->setToolTip(i18nc("@info:tooltip", "Double click to change shortcut"));
197
}
Kurt Hindenburg's avatar
Kurt Hindenburg committed
198
void ManageProfilesDialog::addItems(const Profile::Ptr profile)
199 200 201 202 203
{
    if (profile->isHidden())
        return;

    QList<QStandardItem*> items;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
204
    for (int i = 0; i < 3; i++)
205
        items << new QStandardItem;
Jekyll Wu's avatar
Jekyll Wu committed
206

Kurt Hindenburg's avatar
Kurt Hindenburg committed
207
    updateItemsForProfile(profile, items);
208 209 210 211 212
    _sessionModel->appendRow(items);
}
void ManageProfilesDialog::populateTable()
{
    Q_ASSERT(!_ui->sessionTable->model());
213

214
    _ui->sessionTable->setModel(_sessionModel);
215

216
    _sessionModel->clear();
217
    // setup session table
Kurt Hindenburg's avatar
Kurt Hindenburg committed
218 219 220
    _sessionModel->setHorizontalHeaderLabels(QStringList() << i18nc("@title:column Profile label", "Name")
            << i18nc("@title:column Display profile in file menu", "Show in Menu")
            << i18nc("@title:column Profile shortcut text", "Shortcut"));
221

222 223
    QList<Profile::Ptr> profiles = ProfileManager::instance()->allProfiles();
    ProfileManager::instance()->sortProfiles(profiles);
224

225
    foreach(const Profile::Ptr& profile, profiles) {
Jekyll Wu's avatar
Jekyll Wu committed
226
        addItems(profile);
227
    }
228
    updateDefaultItem();
229

230
    connect(_sessionModel, SIGNAL(itemChanged(QStandardItem*)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
231
            SLOT(itemDataChanged(QStandardItem*)));
232

233 234 235 236 237
    // listen for changes in the table selection and update the state of the form's buttons
    // accordingly.
    //
    // it appears that the selection model is changed when the model itself is replaced,
    // so the signals need to be reconnected each time the model is updated.
238
    connect(_ui->sessionTable->selectionModel(),
Kurt Hindenburg's avatar
Kurt Hindenburg committed
239
            SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
240
            SLOT(tableSelectionChanged(QItemSelection)));
241

242
    _ui->sessionTable->selectRow(0);
243
}
244 245
void ManageProfilesDialog::updateDefaultItem()
{
246
    Profile::Ptr defaultProfile = ProfileManager::instance()->defaultProfile();
247

248 249
    const int rowCount = _sessionModel->rowCount();
    for (int i = 0; i < rowCount; i++) {
250
        QStandardItem* item = _sessionModel->item(i);
251
        QFont itemFont = item->font();
252

Kurt Hindenburg's avatar
Kurt Hindenburg committed
253
        bool isDefault = (defaultProfile == item->data().value<Profile::Ptr>());
254

255
        if (isDefault && !itemFont.bold()) {
Kurt Hindenburg's avatar
Kurt Hindenburg committed
256
            item->setIcon(KIcon(defaultProfile->icon(), 0, QStringList("emblem-favorite")));
257 258 259
            itemFont.setBold(true);
            item->setFont(itemFont);
        } else if (!isDefault && itemFont.bold()) {
260
            item->setIcon(KIcon(defaultProfile->icon()));
261 262
            itemFont.setBold(false);
            item->setFont(itemFont);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
263
        }
264 265
    }
}
266
void ManageProfilesDialog::tableSelectionChanged(const QItemSelection&)
267
{
268
    const int selectedRows = _ui->sessionTable->selectionModel()->selectedRows().count();
269
    const ProfileManager* manager = ProfileManager::instance();
270
    const bool isNotDefault = (selectedRows > 0) && currentProfile() != manager->defaultProfile();
271 272
    const bool isDeletable = (selectedRows > 1) ||
                             (selectedRows == 1 && isProfileDeletable(currentProfile()));
273

Jekyll Wu's avatar
Jekyll Wu committed
274 275
    _ui->newProfileButton->setEnabled(selectedRows < 2);
    _ui->editProfileButton->setEnabled(selectedRows > 0);
276
    // do not allow the default session type to be removed
Jekyll Wu's avatar
Jekyll Wu committed
277
    _ui->deleteProfileButton->setEnabled(isDeletable && isNotDefault);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
278
    _ui->setAsDefaultButton->setEnabled(isNotDefault && (selectedRows < 2));
279
}
280
void ManageProfilesDialog::deleteSelected()
281
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
282
    foreach(const Profile::Ptr & profile, selectedProfiles()) {
283 284
        if (profile != ProfileManager::instance()->defaultProfile())
            ProfileManager::instance()->deleteProfile(profile);
285
    }
286
}
287
void ManageProfilesDialog::setSelectedAsDefault()
288
{
289
    ProfileManager::instance()->setDefaultProfile(currentProfile());
290
    // do not allow the new default session type to be removed
Jekyll Wu's avatar
Jekyll Wu committed
291
    _ui->deleteProfileButton->setEnabled(false);
292 293 294
    _ui->setAsDefaultButton->setEnabled(false);

    // update font of new default item
Kurt Hindenburg's avatar
Kurt Hindenburg committed
295
    updateDefaultItem();
296
}
297 298 299 300 301 302 303

void ManageProfilesDialog::moveUpSelected()
{
    Q_ASSERT(_sessionModel);

    const int rowIndex = _ui->sessionTable->currentIndex().row();
    const QList<QStandardItem*>items = _sessionModel->takeRow(rowIndex);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
304 305
    _sessionModel->insertRow(rowIndex - 1, items);
    _ui->sessionTable->selectRow(rowIndex - 1);
306 307 308 309 310 311 312 313
}

void ManageProfilesDialog::moveDownSelected()
{
    Q_ASSERT(_sessionModel);

    const int rowIndex = _ui->sessionTable->currentIndex().row();
    const QList<QStandardItem*>items = _sessionModel->takeRow(rowIndex);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
314 315
    _sessionModel->insertRow(rowIndex + 1, items);
    _ui->sessionTable->selectRow(rowIndex + 1);
316 317
}

Jekyll Wu's avatar
Jekyll Wu committed
318
void ManageProfilesDialog::createProfile()
319
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
320
    // setup a temporary profile which is a clone of the selected profile
321
    // or the default if no profile is selected
322
    Profile::Ptr sourceProfile;
323

324
    Profile::Ptr selectedProfile = currentProfile();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
325
    if (!selectedProfile)
326
        sourceProfile = ProfileManager::instance()->defaultProfile();
327
    else
Kurt Hindenburg's avatar
Kurt Hindenburg committed
328
        sourceProfile = selectedProfile;
329

Kurt Hindenburg's avatar
Kurt Hindenburg committed
330
    Q_ASSERT(sourceProfile);
331

332
    Profile::Ptr newProfile = Profile::Ptr(new Profile(ProfileManager::instance()->fallbackProfile()));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
333
    newProfile->clone(sourceProfile, true);
334
    newProfile->setProperty(Profile::Name, i18nc("@item This will be used as part of the file name", "New Profile"));
335
    newProfile->setProperty(Profile::UntranslatedName, "New Profile");
336
    newProfile->setProperty(Profile::MenuIndex, QString("0"));
337

338 339 340
    QWeakPointer<EditProfileDialog> dialog = new EditProfileDialog(this);
    dialog.data()->setProfile(newProfile);
    dialog.data()->selectProfileName();
Robert Knight's avatar
 
Robert Knight committed
341

342
    if (dialog.data()->exec() == QDialog::Accepted) {
343 344 345
        ProfileManager::instance()->addProfile(newProfile);
        ProfileManager::instance()->setFavorite(newProfile, true);
        ProfileManager::instance()->changeProfile(newProfile, newProfile->setProperties());
Robert Knight's avatar
 
Robert Knight committed
346
    }
347
    delete dialog.data();
348
}
349
void ManageProfilesDialog::editSelected()
350
{
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
    QList<Profile::Ptr> profiles(selectedProfiles());

    foreach (Session* session, SessionManager::instance()->sessions()) {
         foreach (TerminalDisplay* terminal, session->views()) {
             // Searching for opened profiles
             if (terminal->sessionController()->profileDialogPointer() != NULL) {
                 foreach (const Profile::Ptr & profile, profiles) {
                     if (profile->name() == terminal->sessionController()->profileDialogPointer()->lookupProfile()->name()
                         && terminal->sessionController()->profileDialogPointer()->isVisible()) {
                         // close opened edit dialog
                         terminal->sessionController()->profileDialogPointer()->close();
                     }
                 }
             }
         }
    }

368
    EditProfileDialog dialog(this);
369 370
    // the dialog will delete the profile group when it is destroyed
    ProfileGroup* group = new ProfileGroup;
371
    foreach (const Profile::Ptr & profile, profiles) {
Jekyll Wu's avatar
Jekyll Wu committed
372 373
        group->addProfile(profile);
    }
374 375 376
    group->updateValues();

    dialog.setProfile(Profile::Ptr(group));
377 378
    dialog.exec();
}
379 380 381 382 383
QList<Profile::Ptr> ManageProfilesDialog::selectedProfiles() const
{
    QList<Profile::Ptr> list;
    QItemSelectionModel* selection = _ui->sessionTable->selectionModel();
    if (!selection)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
384
        return list;
385

Kurt Hindenburg's avatar
Kurt Hindenburg committed
386
    foreach(const QModelIndex & index, selection->selectedIndexes()) {
387 388 389 390 391 392 393
        if (index.column() == ProfileNameColumn)
            list << index.data(ProfileKeyRole).value<Profile::Ptr>();
    }

    return list;
}
Profile::Ptr ManageProfilesDialog::currentProfile() const
394
{
395
    QItemSelectionModel* selection = _ui->sessionTable->selectionModel();
396

Kurt Hindenburg's avatar
Kurt Hindenburg committed
397
    if (!selection || selection->selectedRows().count() != 1)
398
        return Profile::Ptr();
399 400

    return  selection->
401
            selectedIndexes().first().data(ProfileKeyRole).value<Profile::Ptr>();
402
}
403 404
bool ManageProfilesDialog::isProfileDeletable(Profile::Ptr profile) const
{
405 406
    static const QString systemDataLocation = KStandardDirs::installPath("data") + "konsole/";

Kurt Hindenburg's avatar
Kurt Hindenburg committed
407
    if (profile) {
408 409
        QFileInfo fileInfo(profile->path());

Kurt Hindenburg's avatar
Kurt Hindenburg committed
410
        if (fileInfo.exists()) {
411 412
            // never remove a system wide profile, no matter whether the
            // current user has enough permission
Kurt Hindenburg's avatar
Kurt Hindenburg committed
413
            if (profile->path().startsWith(systemDataLocation)) {
414 415 416 417
                return false;
            }

            // check whether user has enough permission
418 419
            QFileInfo dirInfo(fileInfo.path());
            return dirInfo.isWritable();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
420
        } else {
421 422
            return true;
        }
Kurt Hindenburg's avatar
Kurt Hindenburg committed
423
    } else {
424 425 426
        return true;
    }
}
427
void ManageProfilesDialog::updateFavoriteStatus(Profile::Ptr profile, bool favorite)
Robert Knight's avatar
 
Robert Knight committed
428
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
429
    Q_ASSERT(_sessionModel);
Robert Knight's avatar
 
Robert Knight committed
430

431
    const int rowCount = _sessionModel->rowCount();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
432 433 434
    for (int i = 0; i < rowCount; i++) {
        QModelIndex index = _sessionModel->index(i, FavoriteStatusColumn);
        if (index.data(ProfileKeyRole).value<Profile::Ptr>() == profile) {
Kurt Hindenburg's avatar
 
Kurt Hindenburg committed
435
            const KIcon icon = favorite ? KIcon("dialog-ok-apply") : KIcon();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
436
            _sessionModel->setData(index, icon, Qt::DecorationRole);
437 438
        }
    }
Robert Knight's avatar
 
Robert Knight committed
439
}
440 441
void ManageProfilesDialog::setShortcutEditorVisible(bool visible)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
442
    _ui->sessionTable->setColumnHidden(ShortcutColumn, !visible);
443
}
444
void StyledBackgroundPainter::drawBackground(QPainter* painter, const QStyleOptionViewItem& option,
445
        const QModelIndex&)
446
{
447 448
    const QStyleOptionViewItemV3* v3option = qstyleoption_cast<const QStyleOptionViewItemV3*>(&option);
    const QWidget* widget = v3option ? v3option->widget : 0;
449

450
    QStyle* style = widget ? widget->style() : QApplication::style();
451

Kurt Hindenburg's avatar
Kurt Hindenburg committed
452
    style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, widget);
453 454
}

455 456
FavoriteItemDelegate::FavoriteItemDelegate(QObject* aParent)
    : QStyledItemDelegate(aParent)
457 458
{
}
459
void FavoriteItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
460
{
461 462
    // See implementation of QStyledItemDelegate::paint()
    QStyleOptionViewItemV4 opt = option;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
463
    initStyleOption(&opt, index);
464

Kurt Hindenburg's avatar
Kurt Hindenburg committed
465
    StyledBackgroundPainter::drawBackground(painter, opt, index);
466

Kurt Hindenburg's avatar
Kurt Hindenburg committed
467
    int margin = (opt.rect.height() - opt.decorationSize.height()) / 2;
468
    margin++;
469

Kurt Hindenburg's avatar
Kurt Hindenburg committed
470 471
    opt.rect.setTop(opt.rect.top() + margin);
    opt.rect.setBottom(opt.rect.bottom() - margin);
472

473
    QIcon icon = index.data(Qt::DecorationRole).value<QIcon>();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
474
    icon.paint(painter, opt.rect, Qt::AlignCenter);
475
}
476

477
bool FavoriteItemDelegate::editorEvent(QEvent* aEvent, QAbstractItemModel*,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
478
                                       const QStyleOptionViewItem&, const QModelIndex& index)
479
{
480 481 482
    if (aEvent->type() == QEvent::MouseButtonPress ||
            aEvent->type() == QEvent::KeyPress ||
            aEvent->type() == QEvent::MouseButtonDblClick) {
Kurt Hindenburg's avatar
Kurt Hindenburg committed
483
        Profile::Ptr profile = index.data(ManageProfilesDialog::ProfileKeyRole).value<Profile::Ptr>();
484
        const bool isFavorite = ProfileManager::instance()->findFavorites().contains(profile);
Jekyll Wu's avatar
Jekyll Wu committed
485

486
        ProfileManager::instance()->setFavorite(profile, !isFavorite);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
487
    }
Jekyll Wu's avatar
Jekyll Wu committed
488

Kurt Hindenburg's avatar
Kurt Hindenburg committed
489
    return true;
490
}
491 492
ShortcutItemDelegate::ShortcutItemDelegate(QObject* aParent)
    : QStyledItemDelegate(aParent)
493 494 495 496
{
}
void ShortcutItemDelegate::editorModified(const QKeySequence& keys)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
497
    Q_UNUSED(keys);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
498
    //kDebug() << keys.toString();
499

500 501 502
    KKeySequenceWidget* editor = qobject_cast<KKeySequenceWidget*>(sender());
    Q_ASSERT(editor);
    _modifiedEditors.insert(editor);
503 504
    emit commitData(editor);
    emit closeEditor(editor);
505 506
}
void ShortcutItemDelegate::setModelData(QWidget* editor, QAbstractItemModel* model,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
507
                                        const QModelIndex& index) const
508
{
509
    _itemsBeingEdited.remove(index);
510

511 512
    if (!_modifiedEditors.contains(editor))
        return;
513

514
    QString shortcut = qobject_cast<KKeySequenceWidget*>(editor)->keySequence().toString();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
515
    model->setData(index, shortcut, Qt::DisplayRole);
516

517
    _modifiedEditors.remove(editor);
518 519
}

520
QWidget* ShortcutItemDelegate::createEditor(QWidget* aParent, const QStyleOptionViewItem&, const QModelIndex& index) const
521
{
522 523
    _itemsBeingEdited.insert(index);

524
    KKeySequenceWidget* editor = new KKeySequenceWidget(aParent);
525 526 527 528
    editor->setFocusPolicy(Qt::StrongFocus);
    editor->setModifierlessAllowed(false);
    QString shortcutString = index.data(Qt::DisplayRole).toString();
    editor->setKeySequence(QKeySequence::fromString(shortcutString));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
529
    connect(editor, SIGNAL(keySequenceChanged(QKeySequence)), this, SLOT(editorModified(QKeySequence)));
530 531
    editor->captureKeySequence();
    return editor;
532
}
Kurt Hindenburg's avatar
Kurt Hindenburg committed
533 534
void ShortcutItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,
                                 const QModelIndex& index) const
535
{
536
    if (_itemsBeingEdited.contains(index))
Kurt Hindenburg's avatar
Kurt Hindenburg committed
537
        StyledBackgroundPainter::drawBackground(painter, option, index);
538
    else
Kurt Hindenburg's avatar
Kurt Hindenburg committed
539
        QStyledItemDelegate::paint(painter, option, index);
540 541
}

542
#include "ManageProfilesDialog.moc"