1 /** \file TocMainToolbar.cpp
2 * \brief Implementation of TocMainToolbar class
4 * Tieto Open Communicator - Client for the Telepathy communications framework.
5 * Copyright (c) 2010, Tieto Corporation
8 * Redistribution and use in source and binary forms, with or without modification,
9 * are permitted provided that the following conditions are met:
11 * Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * Neither the name of the Tieto Corporation nor the names of its contributors
17 * may be used to endorse or promote products derived from this software without
18 * specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "TocMainToolbar"
34 #include <QPushButton>
35 #include <QHBoxLayout>
43 TocMainToolbar::TocMainToolbar(QWidget * parent, Qt::WindowFlags flags)
44 : QWidget(parent, flags) {
46 setObjectName("mainToolbar");
47 QHBoxLayout* horizontalLayout = new QHBoxLayout( this );
48 horizontalLayout->setSpacing( 0 );
49 setContentsMargins( -10, -10, -10, -10 );
51 statusButton = new QPushButton( QIcon(":/offline.png"), "", this );
52 statusButton->setObjectName("statusButton");
53 statusButton->setIconSize( QSize( 72, 72 ) );
54 horizontalLayout->addWidget( statusButton );
55 statusMenu = new QMenu( this );
56 statusMenu->setObjectName("statusMenu");
57 onlineAction = statusMenu->addAction( QIcon(":/online.png"), tr("Online") );
58 onlineAction->setData( QVariant::fromValue( Available ) );
59 awayAction = statusMenu->addAction( QIcon(":/away.png"), tr("Away") );
60 awayAction->setData( QVariant::fromValue( Away ) );
61 busyAction = statusMenu->addAction( QIcon(":/busy.png"), tr("Do not Disturb") );
62 busyAction->setData( QVariant::fromValue( Busy ) );
63 hiddenAction = statusMenu->addAction( QIcon(":/hidden.png"), tr("Hidden") );
64 hiddenAction->setData( QVariant::fromValue( Hidden ) );
65 offlineAction = statusMenu->addAction( QIcon(":/offline.png"), tr("Offline") );
66 offlineAction->setData( QVariant::fromValue( Offline ) );
67 statusButton->setMenu( statusMenu );
68 CONNECT( statusMenu, SIGNAL(triggered(QAction*)), this, SLOT(onTriggeredPresence(QAction*)) );
70 statusAreaLabel = new QLabel( this );
71 statusAreaLabel->setObjectName("statusAreaLabel");
72 statusAreaLabel->setWordWrap( true );
73 statusAreaLabel->setProperty("checked", false);
74 statusAreaLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
75 horizontalLayout->addWidget( statusAreaLabel );
77 statusAreaButton = new QPushButton( this );
78 statusAreaButton->setObjectName("statusAreaButton");
79 statusAreaButton->setEnabled( false );
80 statusAreaButton->setCheckable( true );
81 horizontalLayout->addWidget( statusAreaButton );
83 activeSessionsButton = new QPushButton( QIcon(":/activesessions.png"), "", this );
84 activeSessionsButton->setObjectName("activeSessionsButton");
85 activeSessionsButton->setIconSize( QSize( 72, 72 ) );
86 activeSessionsButton->setEnabled(false);
87 horizontalLayout->addWidget( activeSessionsButton );
90 _pActiveSessions = new ActiveSessions(this);
92 activeSessionsButton->setMenu( _pActiveSessions->menu() );
93 activeSessionsButton->setObjectName("activeSessionsButton");
95 settingsButton = new QPushButton( QIcon(":/settings.png"), "", this );
96 settingsButton->setObjectName("settingsButton");
97 settingsButton->setIconSize( QSize( 72, 72 ) );
98 settingsButton->setCheckable( true );
99 horizontalLayout->addWidget( settingsButton );
100 CONNECT( settingsButton, SIGNAL(clicked()), this, SLOT(onSettingsClicked()) );
102 contactsButton = new QPushButton( QIcon(":/contacts.png"), "", this );
103 contactsButton->setObjectName("contactsButton");
104 contactsButton->setIconSize( QSize( 72, 72 ) );
105 contactsButton->setCheckable( true );
107 horizontalLayout->addWidget( contactsButton );
108 CONNECT( contactsButton, SIGNAL(clicked()), this, SLOT(onContactsClicked()) );
109 highlightButton(contactsButton);
114 TocMainToolbar::~TocMainToolbar() {
117 void TocMainToolbar::highlightButton(const QPushButton* button) {
119 contactsButton->setChecked(button == contactsButton);
120 settingsButton->setChecked(button == settingsButton);
121 statusAreaButton->setChecked(button == statusAreaButton);
122 statusAreaLabel->setEnabled(button == statusAreaButton);
125 void TocMainToolbar::restoreCurrentSession() {
127 if( QAction* action = _pActiveSessions->disabledAction() ) {
129 highlightButton(statusAreaButton);
131 emit activeSessionTriggered( action->data().toString(), action->text() );
133 statusAreaButton->setIcon( QIcon(":/close.png"));
135 disconnect( statusAreaButton, SIGNAL(clicked()), this, SLOT(restoreCurrentSession()) );
136 disconnect( statusAreaButton, SIGNAL(clicked()), this, SLOT(closeCurrentSession()) );
137 CONNECT( statusAreaButton, SIGNAL(clicked()), this, SLOT(closeCurrentSession()) );
141 void TocMainToolbar::closeCurrentSession() {
142 qDebug() << __PRETTY_FUNCTION__;
144 QAction* pAction = _pActiveSessions->disabledAction();
148 QAction *pNewCurrentAction = _pActiveSessions->closeCurrentSession();
150 if( pNewCurrentAction ) {
152 statusAreaButton->setChecked(true);
153 statusAreaLabel->setText( pNewCurrentAction->text() );
155 emit activeSessionTriggered( pNewCurrentAction->data().toString(), pNewCurrentAction->text() );
159 activeSessionsButton->setEnabled(false);
160 statusAreaLabel->setText("");
161 statusAreaButton->setIcon( QIcon() );
162 statusAreaButton->setEnabled( false );
163 disconnect( statusAreaButton, SIGNAL(clicked()), this, SLOT(closeCurrentSession()) );
164 highlightButton(contactsButton);
165 emit contactsClicked();
168 emit finishedSession( pAction->data().toString() );
173 void TocMainToolbar::closeAllSessions() {
175 activeSessionsButton->setEnabled(false);
177 _pActiveSessions->clear();
179 statusAreaLabel->setText("");
181 statusAreaButton->setIcon( QIcon() );
182 statusAreaButton->setEnabled( false );
184 disconnect( statusAreaButton, SIGNAL(clicked()), this, SLOT(restoreCurrentSession()) );
185 disconnect( statusAreaButton, SIGNAL(clicked()), this, SLOT(closeCurrentSession()) );
187 emit finishedAllSessions();
189 highlightButton(contactsButton);
191 emit contactsClicked();
194 void TocMainToolbar::clearAllSessions() {
196 activeSessionsButton->setEnabled(false);
197 _pActiveSessions->clear();
199 statusAreaLabel->setText("");
200 statusAreaButton->setIcon( QIcon() );
201 statusAreaButton->setEnabled( false );
203 disconnect( statusAreaButton, SIGNAL(clicked()), this, SLOT(restoreCurrentSession()) );
204 disconnect( statusAreaButton, SIGNAL(clicked()), this, SLOT(closeCurrentSession()) );
208 void TocMainToolbar::onTriggeredPresence(QAction* action) {
210 emit presenceUpdate( action->data().value<Presence>(), "opisik" );
213 void TocMainToolbar::onPresenceUpdate(Presence presence, const QString& description) {
215 Q_UNUSED(description);
217 if( presence == onlineAction->data().value<Presence>() )
218 statusButton->setIcon( onlineAction->icon() );
220 else if( presence == awayAction->data().value<Presence>() )
221 statusButton->setIcon( awayAction->icon() );
223 else if( presence == busyAction->data().value<Presence>() )
224 statusButton->setIcon( busyAction->icon() );
226 else if( presence == hiddenAction->data().value<Presence>() )
227 statusButton->setIcon( hiddenAction->icon() );
229 else if( presence == offlineAction->data().value<Presence>() )
230 statusButton->setIcon( offlineAction->icon() );
233 void TocMainToolbar::onContactPresenceUpdate(const QString& uid, Presence presence) {
235 _pActiveSessions->onContactPresenceUpdate(uid, presence);
239 void TocMainToolbar::onNewMessage(const QString& uid, const QString& name, Presence presence) {
241 qDebug()<<__PRETTY_FUNCTION__;
243 if( QAction* pAction = _pActiveSessions->action( uid ) ) {
245 if( pAction != _pActiveSessions->disabledAction() ) {
247 activeSessionsButton->setChecked( true );
248 //TODO: activeSessionsButton->startBlinking()
252 if( statusAreaButton->icon().cacheKey() == QIcon( ":/restore.png" ).cacheKey() )//
253 statusAreaButton->setChecked( true );
258 activeSessionsButton->setEnabled(true);
259 //TODO: activeSessionsButton->startBlinking()
262 _pActiveSessions->onNewMessage( uid, name, presence );
265 void TocMainToolbar::onNewSession(const QString& uid, const QString& name, Presence presence) {
267 qDebug()<<__PRETTY_FUNCTION__;
269 highlightButton(statusAreaButton);
271 QAction* pDisabledAction = _pActiveSessions->disabledAction();
273 if( QAction* pAction = _pActiveSessions->action( uid ) ) {
274 if( pAction != pDisabledAction ) {
276 statusAreaLabel->setText( pAction->text() );
277 statusAreaButton->setIcon( QIcon(":/close.png"));
278 statusAreaButton->setEnabled( true );
280 disconnect( statusAreaButton, SIGNAL(clicked()),
281 this, SLOT(restoreCurrentSession()) );
282 disconnect( statusAreaButton, SIGNAL(clicked()),
283 this, SLOT(closeCurrentSession()) );
284 CONNECT( statusAreaButton, SIGNAL(clicked()),
285 this, SLOT(closeCurrentSession()) );
289 statusAreaButton->setIcon( QIcon(":/close.png"));
291 disconnect( statusAreaButton, SIGNAL(clicked()),
292 this, SLOT(restoreCurrentSession()) );
293 disconnect( statusAreaButton, SIGNAL(clicked()),
294 this, SLOT(closeCurrentSession()) );
295 CONNECT( statusAreaButton, SIGNAL(clicked()),
296 this, SLOT(closeCurrentSession()) );
301 statusAreaLabel->setText( name );
303 statusAreaButton->setIcon( QIcon(":/close.png"));
304 statusAreaButton->setEnabled( true );
305 activeSessionsButton->setEnabled(true);
307 disconnect( statusAreaButton, SIGNAL(clicked()),
308 this, SLOT(restoreCurrentSession()) );
309 disconnect( statusAreaButton, SIGNAL(clicked()),
310 this, SLOT(closeCurrentSession()) );
311 CONNECT( statusAreaButton, SIGNAL(clicked()),
312 this, SLOT(closeCurrentSession()) );
315 _pActiveSessions->onNewSession( uid, name, presence );
319 void TocMainToolbar::onCurrentView(ViewType viewType, const QString& text) {
321 if( QAction* pAction = _pActiveSessions->disabledAction() ) { // If there is an active window
323 if( viewType == Contacts ) {
325 statusAreaButton->setIcon( QIcon(":/restore.png") );
326 disconnect( statusAreaButton, SIGNAL(clicked()), this, SLOT(restoreCurrentSession()) );
327 disconnect( statusAreaButton, SIGNAL(clicked()), this, SLOT(closeCurrentSession()) );
328 CONNECT( statusAreaButton, SIGNAL(clicked()), this, SLOT(restoreCurrentSession()) );
329 highlightButton(contactsButton);
332 pAction->setEnabled( true );
333 statusAreaLabel->setText( text );
334 statusAreaButton->setIcon( QIcon() );
335 statusAreaButton->setEnabled( false );
336 disconnect( statusAreaButton, SIGNAL(clicked()), this, SLOT(restoreCurrentSession()) );
337 disconnect( statusAreaButton, SIGNAL(clicked()), this, SLOT(closeCurrentSession()) );
338 // Deselect highlight from all buttons but leave settingsButton as is (highlighted or not)
339 contactsButton->setChecked(false);
340 contactsButton->setChecked(false);
341 statusAreaButton->setChecked(false);
342 statusAreaLabel->setEnabled(false);
344 } else if( viewType == Contacts ) {
345 highlightButton(contactsButton);
346 statusAreaLabel->setText( text );
348 else { // If there is no conversation at that time and the viewType != contacts
349 statusAreaLabel->setText( text );
350 statusAreaButton->setIcon( QIcon() );
351 statusAreaButton->setEnabled( false );
352 disconnect( statusAreaButton, SIGNAL(clicked()), this, SLOT(restoreCurrentSession()) );
353 disconnect( statusAreaButton, SIGNAL(clicked()), this, SLOT(closeCurrentSession()) );
354 // Deselect highlight from all buttons but leave settingsButton as is (highlighted or not)
355 contactsButton->setChecked(false);
356 contactsButton->setChecked(false);
357 statusAreaButton->setChecked(false);
358 statusAreaLabel->setEnabled(false);
362 void TocMainToolbar::onDisplayedNameChange(const QString& uid, const QString& name) {
364 if( QAction* pAction = _pActiveSessions->action( uid ) )
365 pAction->setText( name );
368 void TocMainToolbar::onContactsClicked() {
370 highlightButton(contactsButton);
372 if( _pActiveSessions->disabledAction() ) {
374 statusAreaButton->setIcon( QIcon(":/restore.png") );
375 disconnect( statusAreaButton, SIGNAL(clicked()), this, SLOT(restoreCurrentSession()) );
376 disconnect( statusAreaButton, SIGNAL(clicked()), this, SLOT(closeCurrentSession()) );
377 CONNECT( statusAreaButton, SIGNAL(clicked()), this, SLOT(restoreCurrentSession()) );
381 statusAreaLabel->setText("");
382 statusAreaButton->setIcon( QIcon() );
383 statusAreaButton->setEnabled( false );
386 emit contactsClicked();
389 void TocMainToolbar::onSettingsClicked() {
391 highlightButton(settingsButton);
393 if( QAction* pAction = _pActiveSessions->disabledAction() ) {
395 pAction->setEnabled( true );
397 disconnect( statusAreaButton, SIGNAL(clicked()), this, SLOT(restoreCurrentSession()) );
398 disconnect( statusAreaButton, SIGNAL(clicked()), this, SLOT(closeCurrentSession()) );
401 statusAreaLabel->setText( tr("Settings") );
402 statusAreaButton->setIcon( QIcon() );
403 statusAreaButton->setEnabled( false );
405 emit settingsClicked();
408 void TocMainToolbar::onActiveSessionTriggered(QAction* pAction)
410 if(pAction->text() == "")
413 if( QAction* pDisabledAction = _pActiveSessions->disabledAction() )
414 pDisabledAction->setEnabled( true );
416 pAction->setChecked( false );
417 pAction->setEnabled( false );
419 statusAreaLabel->setText( pAction->text() );
420 statusAreaButton->setIcon( QIcon(":/close.png"));
421 statusAreaButton->setEnabled( true );
423 disconnect( statusAreaButton, SIGNAL(clicked()), this, SLOT(restoreCurrentSession()) );
424 disconnect( statusAreaButton, SIGNAL(clicked()), this, SLOT(closeCurrentSession()) );
425 CONNECT( statusAreaButton, SIGNAL(clicked()), this, SLOT(closeCurrentSession()) );
427 highlightButton(statusAreaButton);
429 emit activeSessionTriggered( pAction->data().toString(), pAction->text() );
433 void TocMainToolbar::setupStyles() {
435 statusButton->setStyleSheet(
445 statusAreaLabel->setStyleSheet(" QLabel {\
454 statusAreaButton->setStyleSheet(" QPushButton {\
463 activeSessionsButton->setStyleSheet(" QPushButton {\
470 " QPushButton:checked {\
471 background-color: qlineargradient(x1: 0, y1: 1, x2: 0, y2: 0,\
472 stop: 0 #999900, stop: 0.55 #aaaa00,\
473 stop: 0.56 #bbbb00, stop: 1 #ffff00);}"
476 settingsButton->setStyleSheet(" QPushButton {\
485 contactsButton->setStyleSheet(" QPushButton {\
494 statusMenu->setStyleSheet("QMenu {\
506 padding: 0px 10px 0px 40px;\
522 " min-width: 180px; "
530 padding: 0px 10px 0px 20px;\
537 QMenu::item:checked {\
538 background-color: qlineargradient(x1: 0, y1: 1, x2: 0, y2: 0,\
539 stop: 0 #999900, stop: 0.55 #aaaa00,\
540 stop: 0.56 #bbbb00, stop: 1 #ffff00);}\
541 QMenu::item:disabled {\
542 background-color: qlineargradient(x1: 0, y1: 1, x2: 0, y2: 0,\
543 stop: 0 #999, stop: 0.55 #aaa,\
544 stop: 0.56 #bbb, stop: 1 #fff);}\
552 _pActiveSessions->setStyleSheet(
556 "border-radius: 5px;"
568 //--------------------------------------------------------
570 //--------------------------------------------------------
572 ActiveSessions::ActiveSessions(QWidget * parent):
574 _visibleActionsAllowed(3),
575 _firstVisibleAction(0),
576 _lastVisibleAction(0)
578 _pActiveSessionsMenu = new QMenu( this );
579 _pActiveSessionsMenu->setObjectName("activeSessionsMenu");
581 _pSeparatorAction = new QAction(this);
582 _pSeparatorAction->setSeparator(true);
584 _pCloseAllSessionsAction = new QAction( QIcon(":/close.png"), tr("Close all"), this );
586 CONNECT( _pCloseAllSessionsAction, SIGNAL(triggered()), parent, SLOT(closeAllSessions()) );
587 CONNECT( _pActiveSessionsMenu, SIGNAL(triggered(QAction*)), parent, SLOT(onActiveSessionTriggered(QAction*)) );
589 _pScrollAction = new QWidgetAction(this);
590 _pScrollWidget = new ScrollWidget(this);
591 _pScrollAction->setDefaultWidget( _pScrollWidget );
593 CONNECT( _pScrollWidget->scrollUpButton(), SIGNAL(clicked()), this, SLOT(scrollUp()) );
594 CONNECT( _pScrollWidget->scrollDownButton(), SIGNAL(clicked()), this, SLOT(scrollDown()) );
598 QAction* ActiveSessions::closeCurrentSession() {
600 qDebug()<<__PRETTY_FUNCTION__;
602 QAction* pAction = disabledAction();
606 removeAction( pAction );
608 QAction* pNewCurrentAction = 0;
611 if( _actionsList.count() > 0 )
613 pNewCurrentAction = _actionsList.last();
614 pNewCurrentAction->setEnabled( false );
617 if( _actionsList.size() == 1 )
619 _pActiveSessionsMenu->removeAction( _pCloseAllSessionsAction );
620 _pActiveSessionsMenu->removeAction( _pSeparatorAction );
623 return pNewCurrentAction;
630 void ActiveSessions::clear() {
632 _pActiveSessionsMenu->clear();
633 _actionsList.clear();
636 QList<QAction*> ActiveSessions::actions() const {
638 return _pActiveSessionsMenu->actions();
641 QAction* ActiveSessions::disabledAction() const {
643 foreach( QAction* pAction, _actionsList ) {
645 if( !pAction->isEnabled() )
651 QAction* ActiveSessions::action( const QString& uid ) const {
653 if(uid.isEmpty()) return 0;
655 QList<QAction*> actionsList = _pActiveSessionsMenu->actions();
657 foreach( QAction* pAction, actionsList ) {
659 if( pAction->data().toString() == uid )
665 void ActiveSessions::onContactPresenceUpdate(const QString& uid, Presence presence) {
667 if( QAction* pAction = action( uid ) ) {
672 pAction->setIcon( QIcon(":/online.png") );
676 pAction->setIcon( QIcon(":/away.png") );
679 pAction->setIcon( QIcon(":/busy.png") );
682 pAction->setIcon( QIcon(":/hidden.png") );
685 pAction->setIcon( QIcon(":/offline.png") );
688 pAction->setIcon( QIcon() );
693 void ActiveSessions::onNewMessage(const QString& uid, const QString& name, Presence presence) {
695 //If such action already exists - set it checked
696 if( QAction* pAction = action( uid ) ) {
698 if( pAction != disabledAction() )
699 pAction->setChecked( true );
701 } else { //create a new one
708 icon = QIcon(":/online.png");
712 icon = QIcon(":/away.png");
715 icon = QIcon(":/busy.png");
718 icon = QIcon(":/hidden.png");
721 icon = QIcon(":/offline.png");
725 pAction = new QAction( icon, name, this );
726 pAction->setData( QVariant::fromValue( uid ) );
727 pAction->setCheckable( true );
728 pAction->setChecked( true );
730 addAction( pAction );
733 if( _pActiveSessionsMenu->actions().count() == 2) {
735 _pActiveSessionsMenu->addAction( _pSeparatorAction );
736 _pActiveSessionsMenu->addAction( _pCloseAllSessionsAction );
742 void ActiveSessions::onNewSession(const QString& uid, const QString& name, Presence presence) {
744 QAction* pDisabledAction = disabledAction();
746 if( QAction* pAction = action( uid ) ) {
748 if( pAction != pDisabledAction ) {
751 pDisabledAction->setEnabled( true );
753 pAction->setChecked( false );
754 pAction->setEnabled( false );
763 icon = QIcon(":/online.png");
767 icon = QIcon(":/away.png");
770 icon = QIcon(":/busy.png");
773 icon = QIcon(":/hidden.png");
776 icon = QIcon(":/offline.png");
779 icon = QIcon(":/offline.png");
782 if( pDisabledAction )
783 pDisabledAction->setEnabled( true );
785 pAction = new QAction( icon, name, this );
786 pAction->setData( QVariant::fromValue( uid ) );
787 pAction->setCheckable( true );
788 pAction->setEnabled( false );
790 addAction( pAction );
792 _pActiveSessionsMenu->setEnabled(true);
795 if( _pActiveSessionsMenu->actions().count() == 2) {
797 _pActiveSessionsMenu->addAction( _pSeparatorAction );
798 _pActiveSessionsMenu->addAction( _pCloseAllSessionsAction );
804 void ActiveSessions::addAction(QAction *pAction) {
806 QList<QAction*> currentlyVisibleActions = _pActiveSessionsMenu->actions();
808 if( currentlyVisibleActions.count( _pScrollAction ) )
809 _pActiveSessionsMenu->insertAction( _pScrollAction, pAction );
811 else if( currentlyVisibleActions.count( _pSeparatorAction ) )
812 _pActiveSessionsMenu->insertAction( _pSeparatorAction, pAction );
814 _pActiveSessionsMenu->addAction( pAction );
816 _actionsList.append(pAction);
819 _lastVisibleAction = _actionsList.end() - 1;
821 if( _actionsList.size() > _visibleActionsAllowed )
822 _firstVisibleAction = _actionsList.end() - _visibleActionsAllowed;
824 _firstVisibleAction = _actionsList.begin();
827 if( _actionsList.size() > _visibleActionsAllowed )
830 _pActiveSessionsMenu->insertAction( _pSeparatorAction, _pScrollAction );
832 //remove one action from top to keep constant menu size
833 if( _actionsList.size() > _visibleActionsAllowed )
835 QAction *pRemoveMe = *( _firstVisibleAction - 1 );
837 _pActiveSessionsMenu->removeAction( pRemoveMe );
840 checkScrollButtonsState();
845 void ActiveSessions::removeAction(QAction *pAction) {
847 _actionsList.removeOne(pAction);
849 _lastVisibleAction = _actionsList.end() - 1;
852 if( _actionsList.size() > _visibleActionsAllowed )
853 _firstVisibleAction = _actionsList.end() - _visibleActionsAllowed;
855 _firstVisibleAction = _actionsList.begin();
857 //Remove all stuff above _pScrollAction or _pSeparatorAction
858 QList<QAction*> visibleItems = _pActiveSessionsMenu->actions();
859 QList<QAction*>::iterator it = visibleItems.begin();
861 while( it != visibleItems.end() )
863 if( *it == _pScrollAction || *it == _pSeparatorAction) break;
865 _pActiveSessionsMenu->removeAction((*it));
870 //...and add it back again
871 it = _firstVisibleAction;
873 while( it != (_lastVisibleAction + 1) )
875 if( visibleItems.count( _pScrollAction ) )
876 _pActiveSessionsMenu->insertAction( _pScrollAction, *it );
878 else if( visibleItems.count( _pSeparatorAction ) )
879 _pActiveSessionsMenu->insertAction( _pSeparatorAction, *it );
881 _pActiveSessionsMenu->addAction( *it );
886 if( _actionsList.count() <= _visibleActionsAllowed && _pScrollAction->isVisible())
887 _pActiveSessionsMenu->removeAction( _pScrollAction );
890 if( _actionsList.isEmpty() ) return;
892 //Reset visible actions
893 QList<QAction*> visibleActions = _pActiveSessionsMenu->actions();
894 QAction *pFirstVisible = visibleActions[0];
897 _firstVisibleAction = _actionsList.begin() + _actionsList.indexOf(pFirstVisible);
899 if(_actionsList.count() >= _visibleActionsAllowed )
900 _lastVisibleAction = _firstVisibleAction + _visibleActionsAllowed - 1;
902 _lastVisibleAction = _firstVisibleAction + _actionsList.size() - 1;
904 checkScrollButtonsState();
908 void ActiveSessions::scrollDown() {
910 //Is there something below?
911 if( _lastVisibleAction == (_actionsList.end() - 1) ) return;
913 _pActiveSessionsMenu->removeAction( *_firstVisibleAction );
915 //Add an item from the bottom
916 if( _pScrollAction->isVisible() )
917 _pActiveSessionsMenu->insertAction( _pScrollAction, *(_lastVisibleAction + 1) );
919 _pActiveSessionsMenu->addAction( *(_lastVisibleAction + 1) );
922 ++_firstVisibleAction;
923 ++_lastVisibleAction;
925 checkScrollButtonsState();
928 void ActiveSessions::scrollUp() {
930 //Is there something above?
931 if( _firstVisibleAction == _actionsList.begin() ) return;
933 //Add an item from the top
934 _pActiveSessionsMenu->insertAction(*_firstVisibleAction, *(_firstVisibleAction - 1) );
936 //Hide an item from the bottom
937 _pActiveSessionsMenu->removeAction( *_lastVisibleAction );
940 --_firstVisibleAction;
941 --_lastVisibleAction;
943 checkScrollButtonsState();
946 void ActiveSessions::checkScrollButtonsState() {
948 if( _firstVisibleAction != _actionsList.begin() )
949 _pScrollWidget->scrollUpButton()->setEnabled( true );
951 _pScrollWidget->scrollUpButton()->setEnabled( false );
954 if( _lastVisibleAction != _actionsList.end() - 1)
955 _pScrollWidget->scrollDownButton()->setEnabled( true );
957 _pScrollWidget->scrollDownButton()->setEnabled( false );
960 //--------------------------------------------------------
962 //--------------------------------------------------------
963 ScrollWidget::ScrollWidget(QWidget* parent):
966 setObjectName("scrollWidget");
967 setContentsMargins( -10, -10, -10, -10 );
969 _pLayout = new QHBoxLayout(this);
970 _pLayout->setSpacing(0);
971 _pScrollUpButton = new QPushButton(tr("up"), this);
972 _pScrollUpButton->setObjectName("scrollUpButton");
973 _pScrollDownButton = new QPushButton(tr("down"), this);
974 _pScrollDownButton->setObjectName("scrollDownButton");
976 _pLayout->addWidget(_pScrollUpButton);
977 _pLayout->addWidget(_pScrollDownButton);