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 #include <KDebug>
00034
00035
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
00052 _ui->sessionTable->verticalHeader()->hide();
00053 _ui->sessionTable->setItemDelegateForColumn(FavoriteStatusColumn,new ProfileItemDelegate(this));
00054
00055
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
00068 _ui->sessionTable->horizontalHeader()->setHighlightSections(false);
00069
00070 _ui->sessionTable->resizeColumnsToContents();
00071
00072
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
00085
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
00094
00095
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
00120
00121
00122 SessionManager::instance()->loadAllProfiles();
00123
00124
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
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
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
00176
00177
00178
00179
00180
00181
00182
00183
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
00223 _ui->deleteSessionButton->setEnabled(isNotDefault);
00224 _ui->setAsDefaultButton->setEnabled(isNotDefault);
00225 }
00226 void ManageProfilesDialog::deleteSelected()
00227 {
00228
00229
00230
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
00241 _ui->deleteSessionButton->setEnabled(false);
00242 _ui->setAsDefaultButton->setEnabled(false);
00243
00244
00245 updateDefaultItem();
00246 }
00247 void ManageProfilesDialog::newType()
00248 {
00249 EditProfileDialog dialog(this);
00250
00251
00252
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
00273
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
00297
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
00325
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"