00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "ManageProfilesDialog.h"
00022
00023
00024 #include <QtGui/QCheckBox>
00025 #include <QtGui/QHeaderView>
00026 #include <QtGui/QItemDelegate>
00027 #include <QtGui/QItemEditorCreator>
00028 #include <QtCore/QMetaEnum>
00029 #include <QtGui/QScrollBar>
00030 #include <QtGui/QShowEvent>
00031 #include <QtGui/QStandardItem>
00032
00033
00034 #include <KKeySequenceWidget>
00035 #include <KDebug>
00036
00037
00038 #include "EditProfileDialog.h"
00039 #include "SessionManager.h"
00040 #include "ui_ManageProfilesDialog.h"
00041
00042 using namespace Konsole;
00043
00044 ManageProfilesDialog::ManageProfilesDialog(QWidget* parent)
00045 : KDialog(parent)
00046 , _sessionModel(new QStandardItemModel(this))
00047 {
00048 setCaption(i18n("Manage Profiles"));
00049 setButtons( KDialog::Close );
00050
00051 _ui = new Ui::ManageProfilesDialog();
00052 _ui->setupUi(mainWidget());
00053
00054
00055 _ui->sessionTable->verticalHeader()->hide();
00056 _ui->sessionTable->setItemDelegateForColumn(FavoriteStatusColumn,new FavoriteItemDelegate(this));
00057 _ui->sessionTable->setItemDelegateForColumn(ShortcutColumn,new ShortcutItemDelegate(this));
00058 _ui->sessionTable->setEditTriggers(_ui->sessionTable->editTriggers() | QAbstractItemView::SelectedClicked);
00059
00060
00061 connect( SessionManager::instance() , SIGNAL(profileAdded(Profile::Ptr)) , this,
00062 SLOT(addItems(Profile::Ptr)) );
00063 connect( SessionManager::instance() , SIGNAL(profileRemoved(Profile::Ptr)) , this,
00064 SLOT(removeItems(Profile::Ptr)) );
00065 connect( SessionManager::instance() , SIGNAL(profileChanged(Profile::Ptr)) , this,
00066 SLOT(updateItems(Profile::Ptr)) );
00067 connect( SessionManager::instance() ,
00068 SIGNAL(favoriteStatusChanged(Profile::Ptr,bool)) , this ,
00069 SLOT(updateFavoriteStatus(Profile::Ptr,bool)) );
00070 populateTable();
00071
00072
00073 _ui->sessionTable->horizontalHeader()->setHighlightSections(false);
00074 _ui->sessionTable->resizeColumnsToContents();
00075
00076
00077
00078
00079 _ui->sessionTable->setColumnWidth(ShortcutColumn,
00080 _ui->sessionTable->columnWidth(ShortcutColumn)+100);
00081
00082
00083 connect( _ui->newSessionButton , SIGNAL(clicked()) , this , SLOT(newType()) );
00084 connect( _ui->editSessionButton , SIGNAL(clicked()) , this , SLOT(editSelected()) );
00085 connect( _ui->deleteSessionButton , SIGNAL(clicked()) , this , SLOT(deleteSelected()) );
00086 connect( _ui->setAsDefaultButton , SIGNAL(clicked()) , this , SLOT(setSelectedAsDefault()) );
00087 }
00088
00089 void ManageProfilesDialog::showEvent(QShowEvent*)
00090 {
00091 Q_ASSERT( _ui->sessionTable->model() );
00092
00093
00094
00095
00096 int totalWidth = 0;
00097 int columnCount = _ui->sessionTable->model()->columnCount();
00098
00099 for ( int i = 0 ; i < columnCount ; i++ )
00100 totalWidth += _ui->sessionTable->columnWidth(i);
00101
00102
00103
00104
00105 int margin = style()->pixelMetric( QStyle::PM_HeaderGripMargin ) * columnCount;
00106 _ui->sessionTable->setMinimumWidth( totalWidth + margin );
00107 _ui->sessionTable->horizontalHeader()->setStretchLastSection(true);
00108 }
00109
00110 ManageProfilesDialog::~ManageProfilesDialog()
00111 {
00112 delete _ui;
00113 }
00114 void ManageProfilesDialog::itemDataChanged(QStandardItem* item)
00115 {
00116 if ( item->column() == ShortcutColumn )
00117 {
00118 QKeySequence sequence = QKeySequence::fromString(item->text());
00119 SessionManager::instance()->setShortcut(item->data(ShortcutRole).value<Profile::Ptr>(),
00120 sequence);
00121 }
00122 }
00123 int ManageProfilesDialog::rowForProfile(const Profile::Ptr info) const
00124 {
00125 for (int i=0;i<_sessionModel->rowCount();i++)
00126 {
00127 if (_sessionModel->item(i,ProfileNameColumn)->data(ProfileKeyRole)
00128 .value<Profile::Ptr>() == info)
00129 {
00130 return i;
00131 }
00132 }
00133 return -1;
00134 }
00135 void ManageProfilesDialog::removeItems(const Profile::Ptr info)
00136 {
00137 int row = rowForProfile(info);
00138 if (row < 0)
00139 return;
00140 _sessionModel->removeRow(row);
00141 }
00142 void ManageProfilesDialog::updateItems(const Profile::Ptr info)
00143 {
00144 int row = rowForProfile(info);
00145 if (row < 0)
00146 return;
00147
00148 QList<QStandardItem*> items;
00149 items << _sessionModel->item(row,ProfileNameColumn);
00150 items << _sessionModel->item(row,FavoriteStatusColumn);
00151 items << _sessionModel->item(row,ShortcutColumn);
00152 updateItemsForProfile(info,items);
00153 }
00154 void ManageProfilesDialog::updateItemsForProfile(const Profile::Ptr info, QList<QStandardItem*>& items) const
00155 {
00156
00157 items[ProfileNameColumn]->setText(info->name());
00158 if ( !info->icon().isEmpty() )
00159 items[ProfileNameColumn]->setIcon( KIcon(info->icon()) );
00160
00161 items[ProfileNameColumn]->setData(QVariant::fromValue(info),ProfileKeyRole);
00162
00163
00164 const bool isFavorite = SessionManager::instance()->findFavorites().contains(info);
00165 if ( isFavorite )
00166 items[FavoriteStatusColumn]->setData(KIcon("favorites"),Qt::DecorationRole);
00167 else
00168 items[FavoriteStatusColumn]->setData(KIcon(),Qt::DecorationRole);
00169 items[FavoriteStatusColumn]->setData(QVariant::fromValue(info),ProfileKeyRole);
00170
00171
00172 QString shortcut = SessionManager::instance()->shortcut(info).
00173 toString();
00174 items[ShortcutColumn]->setText(shortcut);
00175 items[ShortcutColumn]->setData(QVariant::fromValue(info),ShortcutRole);
00176 }
00177 void ManageProfilesDialog::addItems(const Profile::Ptr profile)
00178 {
00179 if (profile->isHidden())
00180 return;
00181
00182 QList<QStandardItem*> items;
00183 for (int i=0;i<3;i++)
00184 items << new QStandardItem;
00185 updateItemsForProfile(profile,items);
00186 _sessionModel->appendRow(items);
00187 }
00188 void ManageProfilesDialog::populateTable()
00189 {
00190 Q_ASSERT(!_ui->sessionTable->model());
00191
00192 _ui->sessionTable->setModel(_sessionModel);
00193
00194
00195
00196 SessionManager::instance()->loadAllProfiles();
00197
00198
00199 _sessionModel->setHorizontalHeaderLabels( QStringList() << i18n("Name")
00200 << i18n("Show in Menu")
00201 << i18n("Shortcut") );
00202 foreach(const Profile::Ptr info,SessionManager::instance()->loadedProfiles())
00203 {
00204 addItems(info);
00205 }
00206 updateDefaultItem();
00207
00208 connect( _sessionModel , SIGNAL(itemChanged(QStandardItem*)) , this ,
00209 SLOT(itemDataChanged(QStandardItem*)) );
00210
00211
00212
00213
00214
00215
00216 connect( _ui->sessionTable->selectionModel() ,
00217 SIGNAL(selectionChanged(const QItemSelection&,const QItemSelection&)) , this ,
00218 SLOT(tableSelectionChanged(const QItemSelection&)) );
00219
00220 tableSelectionChanged( _ui->sessionTable->selectionModel()->selection() );
00221 }
00222 void ManageProfilesDialog::updateDefaultItem()
00223 {
00224 Profile::Ptr defaultProfile = SessionManager::instance()->defaultProfile();
00225
00226 for ( int i = 0 ; i < _sessionModel->rowCount() ; i++ )
00227 {
00228 QStandardItem* item = _sessionModel->item(i);
00229 QFont font = item->font();
00230
00231 bool isDefault = ( defaultProfile == item->data().value<Profile::Ptr>() );
00232
00233 if ( isDefault && !font.bold() )
00234 {
00235 font.setBold(true);
00236 item->setFont(font);
00237 }
00238 else if ( !isDefault && font.bold() )
00239 {
00240 font.setBold(false);
00241 item->setFont(font);
00242 }
00243 }
00244 }
00245 void ManageProfilesDialog::tableSelectionChanged(const QItemSelection&)
00246 {
00247 const int selectedRows = _ui->sessionTable->selectionModel()->selectedRows().count();
00248 const SessionManager* manager = SessionManager::instance();
00249 const bool isNotDefault = (selectedRows > 0) && currentProfile() != manager->defaultProfile();
00250
00251 _ui->newSessionButton->setEnabled(selectedRows < 2);
00252 _ui->editSessionButton->setEnabled(selectedRows > 0);
00253
00254 _ui->deleteSessionButton->setEnabled(isNotDefault);
00255 _ui->setAsDefaultButton->setEnabled(isNotDefault && (selectedRows < 2));
00256 }
00257 void ManageProfilesDialog::deleteSelected()
00258 {
00259 foreach(Profile::Ptr profile, selectedProfiles())
00260 {
00261 if (profile != SessionManager::instance()->defaultProfile())
00262 SessionManager::instance()->deleteProfile(profile);
00263 }
00264 }
00265 void ManageProfilesDialog::setSelectedAsDefault()
00266 {
00267 SessionManager::instance()->setDefaultProfile(currentProfile());
00268
00269 _ui->deleteSessionButton->setEnabled(false);
00270 _ui->setAsDefaultButton->setEnabled(false);
00271
00272
00273 updateDefaultItem();
00274 }
00275 void ManageProfilesDialog::newType()
00276 {
00277 EditProfileDialog dialog(this);
00278
00279
00280
00281 Profile::Ptr sourceProfile;
00282
00283 Profile::Ptr selectedProfile = currentProfile();
00284 if ( !selectedProfile )
00285 sourceProfile = SessionManager::instance()->defaultProfile();
00286 else
00287 sourceProfile = selectedProfile;
00288
00289 Q_ASSERT( sourceProfile );
00290
00291 Profile::Ptr newProfile = Profile::Ptr(new Profile(SessionManager::instance()->fallbackProfile()));
00292 newProfile->clone(sourceProfile,true);
00293 newProfile->setProperty(Profile::Name,i18n("New Profile"));
00294
00295 dialog.setProfile(newProfile);
00296 dialog.selectProfileName();
00297
00298 if ( dialog.exec() == QDialog::Accepted )
00299 {
00300 SessionManager::instance()->addProfile(newProfile);
00301 SessionManager::instance()->setFavorite(newProfile,true);
00302 }
00303 }
00304 void ManageProfilesDialog::editSelected()
00305 {
00306 EditProfileDialog dialog(this);
00307
00308 ProfileGroup* group = new ProfileGroup;
00309 foreach(Profile::Ptr profile,selectedProfiles())
00310 group->addProfile(profile);
00311 group->updateValues();
00312
00313 dialog.setProfile(Profile::Ptr(group));
00314 dialog.exec();
00315 }
00316 QList<Profile::Ptr> ManageProfilesDialog::selectedProfiles() const
00317 {
00318 QList<Profile::Ptr> list;
00319 QItemSelectionModel* selection = _ui->sessionTable->selectionModel();
00320 if (!selection)
00321 return list;
00322
00323 foreach(const QModelIndex& index, selection->selectedIndexes())
00324 {
00325 if (index.column() == ProfileNameColumn)
00326 list << index.data(ProfileKeyRole).value<Profile::Ptr>();
00327 }
00328
00329 return list;
00330 }
00331 Profile::Ptr ManageProfilesDialog::currentProfile() const
00332 {
00333 QItemSelectionModel* selection = _ui->sessionTable->selectionModel();
00334
00335 if ( !selection || selection->selectedRows().count() != 1 )
00336 return Profile::Ptr();
00337
00338 return selection->
00339 selectedIndexes().first().data(ProfileKeyRole).value<Profile::Ptr>();
00340 }
00341 void ManageProfilesDialog::updateFavoriteStatus(Profile::Ptr profile, bool favorite)
00342 {
00343 Q_ASSERT( _sessionModel );
00344
00345 int rowCount = _sessionModel->rowCount();
00346 for (int i=0;i < rowCount;i++)
00347 {
00348 QModelIndex index = _sessionModel->index(i,FavoriteStatusColumn);
00349 if (index.data(ProfileKeyRole).value<Profile::Ptr>() ==
00350 profile )
00351 {
00352 const KIcon icon = favorite ? KIcon("favorites") : KIcon();
00353 _sessionModel->setData(index,icon,Qt::DecorationRole);
00354 }
00355 }
00356 }
00357 void ManageProfilesDialog::setShortcutEditorVisible(bool visible)
00358 {
00359 _ui->sessionTable->setColumnHidden(ShortcutColumn,!visible);
00360 }
00361 void StyledBackgroundPainter::drawBackground(QPainter* painter, const QStyleOptionViewItem& option,
00362 const QModelIndex&)
00363 {
00364 const QStyleOptionViewItemV3* v3option = qstyleoption_cast<const QStyleOptionViewItemV3*>(&option);
00365 const QWidget* widget = v3option ? v3option->widget : 0;
00366
00367 QStyle* style = widget ? widget->style() : QApplication::style();
00368
00369 style->drawPrimitive(QStyle::PE_PanelItemViewItem,&option,painter,widget);
00370 }
00371
00372 FavoriteItemDelegate::FavoriteItemDelegate(QObject* parent)
00373 : QStyledItemDelegate(parent)
00374 {
00375 }
00376 void FavoriteItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
00377 {
00378
00379 QStyleOptionViewItemV4 opt = option;
00380 initStyleOption(&opt,index);
00381
00382 StyledBackgroundPainter::drawBackground(painter,opt,index);
00383
00384 int margin = (opt.rect.height()-opt.decorationSize.height())/2;
00385 margin++;
00386
00387 opt.rect.setTop(opt.rect.top()+margin);
00388 opt.rect.setBottom(opt.rect.bottom()-margin);
00389
00390 QIcon icon = index.data(Qt::DecorationRole).value<QIcon>();
00391 icon.paint(painter,opt.rect,Qt::AlignCenter);
00392 }
00393
00394 bool FavoriteItemDelegate::editorEvent(QEvent* event,QAbstractItemModel*,
00395 const QStyleOptionViewItem&,const QModelIndex& index)
00396 {
00397 if ( event->type() == QEvent::MouseButtonPress || event->type() == QEvent::KeyPress
00398 || event->type() == QEvent::MouseButtonDblClick )
00399 {
00400 Profile::Ptr profile = index.data(ManageProfilesDialog::ProfileKeyRole).value<Profile::Ptr>();
00401 const bool isFavorite = !SessionManager::instance()->findFavorites().contains(profile);
00402
00403 SessionManager::instance()->setFavorite(profile,
00404 isFavorite);
00405 }
00406
00407 return true;
00408 }
00409 ShortcutItemDelegate::ShortcutItemDelegate(QObject* parent)
00410 : QStyledItemDelegate(parent)
00411 {
00412 }
00413 void ShortcutItemDelegate::editorModified(const QKeySequence& keys)
00414 {
00415 kDebug() << keys.toString();
00416
00417 KKeySequenceWidget* editor = qobject_cast<KKeySequenceWidget*>(sender());
00418 Q_ASSERT(editor);
00419 _modifiedEditors.insert(editor);
00420 }
00421 void ShortcutItemDelegate::setModelData(QWidget* editor, QAbstractItemModel* model,
00422 const QModelIndex& index) const
00423 {
00424 _itemsBeingEdited.remove(index);
00425
00426 if (!_modifiedEditors.contains(editor))
00427 return;
00428
00429 QString shortcut = qobject_cast<KKeySequenceWidget*>(editor)->keySequence().toString();
00430 model->setData(index,shortcut,Qt::DisplayRole);
00431
00432 _modifiedEditors.remove(editor);
00433 }
00434
00435 QWidget* ShortcutItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem&, const QModelIndex& index) const
00436 {
00437 _itemsBeingEdited.insert(index);
00438
00439 KKeySequenceWidget* editor = new KKeySequenceWidget(parent);
00440 editor->setFocusPolicy(Qt::StrongFocus);
00441 editor->setModifierlessAllowed(false);
00442 QString shortcutString = index.data(Qt::DisplayRole).toString();
00443 editor->setKeySequence(QKeySequence::fromString(shortcutString));
00444 connect(editor,SIGNAL(keySequenceChanged(QKeySequence)),this,SLOT(editorModified(QKeySequence)));
00445 editor->captureKeySequence();
00446 return editor;
00447 }
00448 void ShortcutItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,
00449 const QModelIndex& index) const
00450 {
00451 if (_itemsBeingEdited.contains(index))
00452 StyledBackgroundPainter::drawBackground(painter,option,index);
00453 else
00454 QStyledItemDelegate::paint(painter,option,index);
00455 }
00456
00457 #include "ManageProfilesDialog.moc"