• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Konsole

ManageProfilesDialog.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or
00007     (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU General Public License for more details.
00013 
00014     You should have received a copy of the GNU General Public License
00015     along with this program; if not, write to the Free Software
00016     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017     02110-1301  USA.
00018 */
00019 
00020 // Own
00021 #include "ManageProfilesDialog.h"
00022 
00023 // Qt
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 // KDE
00034 #include <KKeySequenceWidget>
00035 #include <KDebug>
00036 
00037 // Konsole
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     // hide vertical header
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     // update table and listen for changes to the session types
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     // resize the session table to the full width of the table
00073     _ui->sessionTable->horizontalHeader()->setHighlightSections(false);
00074     _ui->sessionTable->resizeColumnsToContents();
00075     
00076     // allow a larger width for the shortcut column to account for the 
00077     // increased with needed by the shortcut editor compared with just
00078     // displaying the text of the shortcut
00079     _ui->sessionTable->setColumnWidth(ShortcutColumn,
00080                 _ui->sessionTable->columnWidth(ShortcutColumn)+100);
00081 
00082     // setup buttons
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     // try to ensure that all the text in all the columns is visible initially.
00094     // FIXME:  this is not a good solution, look for a more correct way to do this
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     // the margin is added to account for the space taken by the resize grips
00103     // between the columns, this ensures that a horizontal scroll bar is not added 
00104     // automatically
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     // Profile Name
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     // Favorite Status
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     // Shortcut
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     // ensure profiles list is complete
00194     // this may be expensive, but will only be done the first time
00195     // that the dialog is shown. 
00196     SessionManager::instance()->loadAllProfiles();
00197 
00198     // setup session table
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     // listen for changes in the table selection and update the state of the form's buttons
00212     // accordingly.
00213     //
00214     // it appears that the selection model is changed when the model itself is replaced,
00215     // so the signals need to be reconnected each time the model is updated.
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     // do not allow the default session type to be removed
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     // do not allow the new default session type to be removed
00269     _ui->deleteSessionButton->setEnabled(false);
00270     _ui->setAsDefaultButton->setEnabled(false);
00271 
00272     // update font of new default item
00273     updateDefaultItem(); 
00274 }
00275 void ManageProfilesDialog::newType()
00276 {
00277     EditProfileDialog dialog(this);
00278  
00279     // setup a temporary profile which is a clone of the selected profile 
00280     // or the default if no profile is selected
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     // the dialog will delete the profile group when it is destroyed
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     // See implementation of QStyledItemDelegate::paint()
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"

Konsole

Skip menu "Konsole"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • Konsole
  • Libraries
  •   libkonq
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal