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

Konsole

ManageProfilesDialog.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2007 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 #include <KDebug>
00034 
00035 // Konsole
00036 #include "EditProfileDialog.h"
00037 #include "SessionManager.h"
00038 #include "ui_ManageProfilesDialog.h"
00039 
00040 using namespace Konsole;
00041 
00042 ManageProfilesDialog::ManageProfilesDialog(QWidget* parent)
00043     : KDialog(parent)
00044 {
00045     setCaption(i18n("Manage Profiles"));
00046     setButtons( KDialog::Close ); 
00047 
00048     _ui = new Ui::ManageProfilesDialog();
00049     _ui->setupUi(mainWidget());
00050 
00051     // hide vertical header
00052     _ui->sessionTable->verticalHeader()->hide();
00053     _ui->sessionTable->setItemDelegateForColumn(FavoriteStatusColumn,new ProfileItemDelegate(this));
00054 
00055     // update table and listen for changes to the session types
00056     updateTableModel();
00057     connect( SessionManager::instance() , SIGNAL(profileAdded(const QString&)) , this,
00058              SLOT(updateTableModel()) );
00059     connect( SessionManager::instance() , SIGNAL(profileRemoved(const QString&)) , this,
00060              SLOT(updateTableModel()) );
00061     connect( SessionManager::instance() , SIGNAL(profileChanged(const QString&)) , this,
00062              SLOT(updateTableModel()) );
00063     connect( SessionManager::instance() , 
00064                 SIGNAL(favoriteStatusChanged(const QString&,bool)) , this ,
00065                 SLOT(updateFavoriteStatus(const QString&,bool)) );
00066 
00067     // resize the session table to the full width of the table
00068     _ui->sessionTable->horizontalHeader()->setHighlightSections(false);
00069 
00070     _ui->sessionTable->resizeColumnsToContents();
00071 
00072     // setup buttons
00073     connect( _ui->newSessionButton , SIGNAL(clicked()) , this , SLOT(newType()) );
00074     connect( _ui->editSessionButton , SIGNAL(clicked()) , this , SLOT(editSelected()) );
00075     connect( _ui->deleteSessionButton , SIGNAL(clicked()) , this , SLOT(deleteSelected()) );
00076     connect( _ui->setAsDefaultButton , SIGNAL(clicked()) , this , SLOT(setSelectedAsDefault()) );
00077 
00078 }
00079 
00080 void ManageProfilesDialog::showEvent(QShowEvent*)
00081 {
00082     Q_ASSERT( _ui->sessionTable->model() );
00083 
00084     // try to ensure that all the text in all the columns is visible initially.
00085     // FIXME:  this is not a good solution, look for a more correct way to do this
00086 
00087     int totalWidth = 0;
00088     int columnCount = _ui->sessionTable->model()->columnCount();
00089 
00090     for ( int i = 0 ; i < columnCount ; i++ )
00091         totalWidth += _ui->sessionTable->columnWidth(i);
00092 
00093     // the margin is added to account for the space taken by the resize grips
00094     // between the columns, this ensures that a horizontal scroll bar is not added 
00095     // automatically
00096     int margin = style()->pixelMetric( QStyle::PM_HeaderGripMargin ) * columnCount;
00097     _ui->sessionTable->setMinimumWidth( totalWidth + margin );
00098     _ui->sessionTable->horizontalHeader()->setStretchLastSection(true);
00099 }
00100 
00101 ManageProfilesDialog::~ManageProfilesDialog()
00102 {
00103     delete _ui;
00104 }
00105 void ManageProfilesDialog::itemDataChanged(QStandardItem* item)
00106 {
00107    if ( item->column() == ShortcutColumn )
00108    {
00109         QKeySequence sequence = QKeySequence::fromString(item->text());
00110 
00111         kDebug() << "New key sequence: " << item->text(); 
00112 
00113         SessionManager::instance()->setShortcut(item->data(ShortcutRole).value<QString>(),
00114                                                 sequence); 
00115    } 
00116 }
00117 void ManageProfilesDialog::updateTableModel()
00118 {
00119     // ensure profiles list is complete
00120     // this may be EXPENSIVE, but will only be done the first time
00121     // that the dialog is shown. 
00122     SessionManager::instance()->loadAllProfiles();
00123 
00124     // setup session table
00125     _sessionModel = new QStandardItemModel(this);
00126     _sessionModel->setHorizontalHeaderLabels( QStringList() << i18n("Name")
00127                                                             << i18n("Show in Menu") 
00128                                                             << i18n("Shortcut") );
00129     QListIterator<QString> keyIter( SessionManager::instance()->loadedProfiles() );
00130     while ( keyIter.hasNext() )
00131     {
00132         const QString& key = keyIter.next();
00133 
00134         Profile* info = SessionManager::instance()->profile(key);
00135 
00136         if ( info->isHidden() )
00137             continue;
00138 
00139         QList<QStandardItem*> itemList;
00140         QStandardItem* item = new QStandardItem( info->name() );
00141 
00142         if ( !info->icon().isEmpty() )
00143             item->setIcon( KIcon(info->icon()) );
00144         item->setData(key,ProfileKeyRole);
00145 
00146         const bool isFavorite = SessionManager::instance()->findFavorites().contains(key);
00147 
00148         // favorite column
00149         QStandardItem* favoriteItem = new QStandardItem();
00150         if ( isFavorite )
00151             favoriteItem->setData(KIcon("favorites"),Qt::DecorationRole);
00152         else
00153             favoriteItem->setData(KIcon(),Qt::DecorationRole);
00154 
00155         favoriteItem->setData(key,ProfileKeyRole);
00156 
00157         // shortcut column
00158         QStandardItem* shortcutItem = new QStandardItem();
00159         QString shortcut = SessionManager::instance()->shortcut(key).
00160                                 toString();
00161         shortcutItem->setText(shortcut);
00162         shortcutItem->setData(key,ShortcutRole);
00163 
00164         itemList << item << favoriteItem << shortcutItem;
00165 
00166         _sessionModel->appendRow(itemList);
00167     }
00168     updateDefaultItem();
00169 
00170     connect( _sessionModel , SIGNAL(itemChanged(QStandardItem*)) , this , 
00171             SLOT(itemDataChanged(QStandardItem*)) );
00172 
00173     _ui->sessionTable->setModel(_sessionModel);
00174 
00175     // listen for changes in the table selection and update the state of the form's buttons
00176     // accordingly.
00177     //
00178     // it appears that the selection model is changed when the model itself is replaced,
00179     // so the signals need to be reconnected each time the model is updated.
00180     //
00181     // the view ( _ui->sessionTable ) has a selectionChanged() signal which it would be
00182     // preferable to connect to instead of the one belonging to the view's current 
00183     // selection model, but QAbstractItemView::selectionChanged() is protected
00184     //
00185     connect( _ui->sessionTable->selectionModel() , 
00186             SIGNAL(selectionChanged(const QItemSelection&,const QItemSelection&)) , this ,
00187             SLOT(tableSelectionChanged(const QItemSelection&)) );
00188 
00189     tableSelectionChanged( _ui->sessionTable->selectionModel()->selection() );
00190 
00191 }
00192 void ManageProfilesDialog::updateDefaultItem()
00193 {
00194     const QString& defaultKey = SessionManager::instance()->defaultProfileKey();
00195 
00196     for ( int i = 0 ; i < _sessionModel->rowCount() ; i++ )
00197     {
00198         QStandardItem* item = _sessionModel->item(i);
00199         QFont font = item->font();
00200 
00201         bool isDefault = ( defaultKey == item->data().value<QString>() );
00202 
00203         if ( isDefault && !font.bold() )
00204         {
00205             font.setBold(true);
00206             item->setFont(font);
00207         } 
00208         else if ( !isDefault && font.bold() )
00209         {
00210             font.setBold(false);
00211             item->setFont(font);
00212         } 
00213     }
00214 }
00215 void ManageProfilesDialog::tableSelectionChanged(const QItemSelection& selection)
00216 {
00217     bool enable = !selection.indexes().isEmpty();
00218     const SessionManager* manager = SessionManager::instance();
00219     const bool isNotDefault = enable && selectedKey() != manager->defaultProfileKey();
00220 
00221     _ui->editSessionButton->setEnabled(enable);
00222     // do not allow the default session type to be removed
00223     _ui->deleteSessionButton->setEnabled(isNotDefault);
00224     _ui->setAsDefaultButton->setEnabled(isNotDefault); 
00225 }
00226 void ManageProfilesDialog::deleteSelected()
00227 {
00228     // TODO - Possibly add a warning here if the selected profile is the parent of 
00229     // one or more other profiles since deleting a profile will change the settings
00230     // of the profiles which inherit from it.
00231     
00232     Q_ASSERT( !selectedKey().isEmpty()  );
00233     Q_ASSERT( selectedKey() != SessionManager::instance()->defaultProfileKey() );
00234 
00235     SessionManager::instance()->deleteProfile(selectedKey());
00236 }
00237 void ManageProfilesDialog::setSelectedAsDefault()
00238 {
00239     SessionManager::instance()->setDefaultProfile(selectedKey());
00240     // do not allow the new default session type to be removed
00241     _ui->deleteSessionButton->setEnabled(false);
00242     _ui->setAsDefaultButton->setEnabled(false);
00243 
00244     // update font of new default item
00245     updateDefaultItem(); 
00246 }
00247 void ManageProfilesDialog::newType()
00248 {
00249     EditProfileDialog dialog(this);
00250  
00251     // setup a temporary profile, inheriting from the selected profile
00252     // or the default if no profile is selected
00253     Profile* parentProfile = 0;
00254 
00255     QString selectedProfileKey = selectedKey();
00256     if ( selectedProfileKey.isEmpty() ) 
00257         parentProfile = SessionManager::instance()->defaultProfile();
00258     else
00259         parentProfile = SessionManager::instance()->profile(selectedProfileKey);
00260 
00261     Q_ASSERT( parentProfile );
00262 
00263     Profile* newProfile = new Profile(parentProfile);
00264     newProfile->setProperty(Profile::Name,i18n("New Profile"));
00265     const QString& key = SessionManager::instance()->addProfile(newProfile);
00266 
00267     kDebug() << "Key for new profile" << key;
00268 
00269     dialog.setProfile(key); 
00270     dialog.selectProfileName();
00271 
00272     // if the user doesn't accept the dialog, remove the temporary profile
00273     // if they do accept the dialog, it will become a permanent profile
00274     if ( dialog.exec() != QDialog::Accepted )
00275         SessionManager::instance()->deleteProfile(key);
00276     else
00277     {
00278         SessionManager::instance()->setFavorite(key,true);
00279     }
00280 }
00281 void ManageProfilesDialog::editSelected()
00282 {
00283     Q_ASSERT( !selectedKey().isEmpty() );
00284 
00285     EditProfileDialog dialog(this);
00286     dialog.setProfile(selectedKey());
00287     dialog.exec();
00288 }
00289 QString ManageProfilesDialog::selectedKey() const
00290 {
00291     QItemSelectionModel* selection = _ui->sessionTable->selectionModel();
00292 
00293     if ( !selection || selection->selectedRows().count() != 1 )
00294         return QString();
00295 
00296     // TODO There has to be an easier way of getting the data
00297     // associated with the currently selected item
00298     return  selection->
00299             selectedIndexes().first().data( Qt::UserRole + 1 ).value<QString>();
00300 }
00301 void ManageProfilesDialog::updateFavoriteStatus(const QString& key , bool favorite)
00302 {
00303     Q_ASSERT( _sessionModel );
00304 
00305     const QModelIndex topIndex = _sessionModel->index(0,FavoriteStatusColumn);
00306 
00307     QModelIndexList list = _sessionModel->match( topIndex , ProfileKeyRole,
00308                                                  key );
00309 
00310     foreach( QModelIndex index , list )
00311     {
00312         const KIcon icon = favorite ? KIcon("favorites") : KIcon();
00313         _sessionModel->setData(index,icon,Qt::DecorationRole);
00314     }
00315 }
00316 void ManageProfilesDialog::setShortcutEditorVisible(bool visible)
00317 {
00318     _ui->sessionTable->setColumnHidden(ShortcutColumn,!visible);    
00319 }
00320 ProfileItemDelegate::ProfileItemDelegate(QObject* parent)
00321     : QItemDelegate(parent)
00322 {
00323 }
00324 // Is there a simpler way of centering the decoration than re-implementing
00325 // drawDecoration?
00326 void ProfileItemDelegate::drawDecoration(QPainter* painter,
00327                                          const QStyleOptionViewItem& option,
00328                                          const QRect& rect,const QPixmap& pixmap) const
00329 {
00330     QStyleOptionViewItem centeredOption(option);
00331     centeredOption.decorationAlignment = Qt::AlignCenter;
00332     QItemDelegate::drawDecoration(painter,
00333                                   centeredOption,
00334                                   QStyle::alignedRect(Qt::LeftToRight,Qt::AlignCenter,rect.size(),option.rect),
00335                                   pixmap);
00336 }
00337 bool ProfileItemDelegate::editorEvent(QEvent* event,QAbstractItemModel*,
00338                                     const QStyleOptionViewItem&,const QModelIndex& index)
00339 {
00340      if ( event->type() == QEvent::MouseButtonPress || event->type() == QEvent::KeyPress )
00341      {
00342          const QString& key = index.data(ManageProfilesDialog::ProfileKeyRole).value<QString>();
00343          const bool isFavorite = !SessionManager::instance()->findFavorites().contains(key);
00344                                                 
00345         SessionManager::instance()->setFavorite(key,
00346                                             isFavorite);
00347      }
00348      
00349      return true; 
00350 }
00351 
00352 #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.4
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