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

Konsole

ProfileList.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright 2006-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 "ProfileList.h"
00022 
00023 // Qt
00024 #include <QtGui/QAction>
00025 #include <QtGui/QActionGroup>
00026 #include <KDebug>
00027 
00028 // KDE
00029 #include <KIcon>
00030 #include <KLocalizedString>
00031 
00032 // Konsole
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     // construct the list of favorite session types
00045     _group = new QActionGroup(this);
00046     
00047     // disabled action to be shown only when the list is empty
00048     _emptyListAction = new QAction(i18n("No profiles available"),_group);
00049     _emptyListAction->setEnabled(false);
00050     
00051     // TODO Sort list in alphabetical order
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     // listen for future changes to the session list
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     // show empty list action when it is the only action
00077     // in the group
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; // not found
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"

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