2 Copyright 2006-2008 by Robert Knight <robertknight@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include "ProfileList.h"
24 #include <QtGui/QAction>
25 #include <QtGui/QActionGroup>
30 #include <KLocalizedString>
33 #include "SessionManager.h"
35 using namespace Konsole
;
37 ProfileList::ProfileList(bool addShortcuts
, QObject
* parent
)
39 , _addShortcuts(addShortcuts
)
42 SessionManager
* manager
= SessionManager::instance();
44 // construct the list of favorite session types
45 _group
= new QActionGroup(this);
47 // disabled action to be shown only when the list is empty
48 _emptyListAction
= new QAction(i18n("No profiles available"),_group
);
49 _emptyListAction
->setEnabled(false);
51 // TODO Sort list in alphabetical order
52 QList
<Profile::Ptr
> list
= manager
->findFavorites().toList();
53 QListIterator
<Profile::Ptr
> iter(list
);
55 while (iter
.hasNext())
57 favoriteChanged(iter
.next(),true);
60 connect( _group
, SIGNAL(triggered(QAction
*)) , this , SLOT(triggered(QAction
*)) );
63 // listen for future changes to the session list
64 connect( manager
, SIGNAL(favoriteStatusChanged(Profile::Ptr
,bool)) , this ,
65 SLOT(favoriteChanged(Profile::Ptr
,bool)) );
66 connect( manager
, SIGNAL(shortcutChanged(Profile::Ptr
,QKeySequence
)) , this ,
67 SLOT(shortcutChanged(Profile::Ptr
,QKeySequence
)) );
68 connect( manager
, SIGNAL(profileChanged(Profile::Ptr
)) , this ,
69 SLOT(profileChanged(Profile::Ptr
)) );
71 void ProfileList::updateEmptyAction()
74 Q_ASSERT( _emptyListAction
);
76 // show empty list action when it is the only action
78 const bool showEmptyAction
= _group
->actions().count() == 1;
80 if ( showEmptyAction
!= _emptyListAction
->isVisible() )
81 _emptyListAction
->setVisible(showEmptyAction
);
83 QAction
* ProfileList::actionForKey(Profile::Ptr key
) const
85 QListIterator
<QAction
*> iter(_group
->actions());
86 while ( iter
.hasNext() )
88 QAction
* next
= iter
.next();
89 if ( next
->data().value
<Profile::Ptr
>() == key
)
92 return 0; // not found
95 void ProfileList::profileChanged(Profile::Ptr key
)
97 QAction
* action
= actionForKey(key
);
99 updateAction(action
,key
);
102 void ProfileList::updateAction(QAction
* action
, Profile::Ptr info
)
107 action
->setText(info
->name());
108 action
->setIcon(KIcon(info
->icon()));
110 void ProfileList::shortcutChanged(Profile::Ptr info
,const QKeySequence
& sequence
)
112 if ( !_addShortcuts
)
115 QAction
* action
= actionForKey(info
);
119 action
->setShortcut(sequence
);
122 void ProfileList::syncWidgetActions(QWidget
* widget
, bool sync
)
126 _registeredWidgets
.remove(widget
);
130 _registeredWidgets
.insert(widget
);
132 const QList
<QAction
*> currentActions
= widget
->actions();
133 foreach(QAction
* currentAction
, currentActions
)
134 widget
->removeAction(currentAction
);
136 widget
->addActions(_group
->actions());
138 void ProfileList::favoriteChanged(Profile::Ptr info
,bool isFavorite
)
140 SessionManager
* manager
= SessionManager::instance();
144 QAction
* action
= new QAction(_group
);
145 action
->setData( QVariant::fromValue(info
) );
149 action
->setShortcut(manager
->shortcut(info
));
152 updateAction(action
,info
);
154 foreach(QWidget
* widget
,_registeredWidgets
)
155 widget
->addAction(action
);
156 emit
actionsChanged(_group
->actions());
160 QAction
* action
= actionForKey(info
);
164 _group
->removeAction(action
);
165 foreach(QWidget
* widget
,_registeredWidgets
)
166 widget
->removeAction(action
);
167 emit
actionsChanged(_group
->actions());
173 void ProfileList::triggered(QAction
* action
)
175 emit
profileSelected( action
->data().value
<Profile::Ptr
>() );
178 QList
<QAction
*> ProfileList::actions()
180 return _group
->actions();
183 #include "ProfileList.moc"