Konsole
ProfileList.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "ProfileList.h"
00022
00023
00024 #include <QtGui/QAction>
00025 #include <QtGui/QActionGroup>
00026 #include <KDebug>
00027
00028
00029 #include <KIcon>
00030 #include <KLocalizedString>
00031
00032
00033 #include "SessionManager.h"
00034
00035 using namespace Konsole;
00036
00037 ProfileList::ProfileList(bool addShortcuts , QObject* parent)
00038 : QObject(parent)
00039 , _addShortcuts(addShortcuts)
00040 , _emptyListAction(0)
00041 {
00042 SessionManager* manager = SessionManager::instance();
00043
00044
00045 _group = new QActionGroup(this);
00046
00047
00048 _emptyListAction = new QAction(i18n("No profiles available"),_group);
00049 _emptyListAction->setEnabled(false);
00050
00051
00052 QList<Profile::Ptr> list = manager->findFavorites().toList();
00053 QListIterator<Profile::Ptr> iter(list);
00054
00055 while (iter.hasNext())
00056 {
00057 favoriteChanged(iter.next(),true);
00058 }
00059
00060 connect( _group , SIGNAL(triggered(QAction*)) , this , SLOT(triggered(QAction*)) );
00061
00062
00063
00064 connect( manager , SIGNAL(favoriteStatusChanged(Profile::Ptr,bool)) , this ,
00065 SLOT(favoriteChanged(Profile::Ptr,bool)) );
00066 connect( manager , SIGNAL(shortcutChanged(Profile::Ptr,QKeySequence)) , this ,
00067 SLOT(shortcutChanged(Profile::Ptr,QKeySequence)) );
00068 connect( manager , SIGNAL(profileChanged(Profile::Ptr)) , this ,
00069 SLOT(profileChanged(Profile::Ptr)) );
00070 }
00071 void ProfileList::updateEmptyAction()
00072 {
00073 Q_ASSERT( _group );
00074 Q_ASSERT( _emptyListAction );
00075
00076
00077
00078 const bool showEmptyAction = _group->actions().count() == 1;
00079
00080 if ( showEmptyAction != _emptyListAction->isVisible() )
00081 _emptyListAction->setVisible(showEmptyAction);
00082 }
00083 QAction* ProfileList::actionForKey(Profile::Ptr key) const
00084 {
00085 QListIterator<QAction*> iter(_group->actions());
00086 while ( iter.hasNext() )
00087 {
00088 QAction* next = iter.next();
00089 if ( next->data().value<Profile::Ptr>() == key )
00090 return next;
00091 }
00092 return 0;
00093 }
00094
00095 void ProfileList::profileChanged(Profile::Ptr key)
00096 {
00097 QAction* action = actionForKey(key);
00098 if ( action )
00099 updateAction(action,key);
00100 }
00101
00102 void ProfileList::updateAction(QAction* action , Profile::Ptr info)
00103 {
00104 Q_ASSERT(action);
00105 Q_ASSERT(info);
00106
00107 action->setText(info->name());
00108 action->setIcon(KIcon(info->icon()));
00109 }
00110 void ProfileList::shortcutChanged(Profile::Ptr info,const QKeySequence& sequence)
00111 {
00112 if ( !_addShortcuts )
00113 return;
00114
00115 QAction* action = actionForKey(info);
00116
00117 if ( action )
00118 {
00119 action->setShortcut(sequence);
00120 }
00121 }
00122 void ProfileList::syncWidgetActions(QWidget* widget, bool sync)
00123 {
00124 if (!sync)
00125 {
00126 _registeredWidgets.remove(widget);
00127 return;
00128 }
00129
00130 _registeredWidgets.insert(widget);
00131
00132 const QList<QAction*> currentActions = widget->actions();
00133 foreach(QAction* currentAction, currentActions)
00134 widget->removeAction(currentAction);
00135
00136 widget->addActions(_group->actions());
00137 }
00138 void ProfileList::favoriteChanged(Profile::Ptr info,bool isFavorite)
00139 {
00140 SessionManager* manager = SessionManager::instance();
00141
00142 if ( isFavorite )
00143 {
00144 QAction* action = new QAction(_group);
00145 action->setData( QVariant::fromValue(info) );
00146
00147 if ( _addShortcuts )
00148 {
00149 action->setShortcut(manager->shortcut(info));
00150 }
00151
00152 updateAction(action,info);
00153
00154 foreach(QWidget* widget,_registeredWidgets)
00155 widget->addAction(action);
00156 emit actionsChanged(_group->actions());
00157 }
00158 else
00159 {
00160 QAction* action = actionForKey(info);
00161
00162 if ( action )
00163 {
00164 _group->removeAction(action);
00165 foreach(QWidget* widget,_registeredWidgets)
00166 widget->removeAction(action);
00167 emit actionsChanged(_group->actions());
00168 }
00169 }
00170
00171 updateEmptyAction();
00172 }
00173 void ProfileList::triggered(QAction* action)
00174 {
00175 emit profileSelected( action->data().value<Profile::Ptr>() );
00176 }
00177
00178 QList<QAction*> ProfileList::actions()
00179 {
00180 return _group->actions();
00181 }
00182
00183 #include "ProfileList.moc"