1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/
20 #include "SUIT_ToolButton.h"
22 #include <qpopupmenu.h>
26 SUIT_ToolButton::SUIT_ToolButton( QWidget *parent,
28 bool changeItemAfterClick)
29 : QToolButton( parent, name ),
30 myChangeItemAfterClick( changeItemAfterClick )
36 SUIT_ToolButton::SUIT_ToolButton( const QPixmap & pm,
37 const QString &textLabel,
38 const QString& grouptext,
43 bool changeItemAfterClick)
44 :QToolButton(pm, textLabel, grouptext, receiver, slot, parent, name),
45 myChangeItemAfterClick( changeItemAfterClick )
51 /*!Initialize tool buttons.*/
52 void SUIT_ToolButton::initialize()
55 myPopup = new QPopupMenu( this );
57 connect( myPopup, SIGNAL(activated(int)), SLOT(OnSelectAction(int)) );
61 /*!drawButton is redefined to draw DownArrow*/
62 void SUIT_ToolButton::drawButton( QPainter * p )
64 QToolButton::drawButton(p);
68 QStyle::visualRect(QRect(0, 0, width(), height()), this).rect( &x, &y, &w, &h );
69 style().drawPrimitive( QStyle::PE_ArrowDown,
70 p, QRect(x+w/2+3, y+h/2+3, w/2, h/2), //QRect(x+(w-x)/2, y+(h-y)/2, w, h)
71 colorGroup(), isEnabled() );
75 /*! Add action into popup*/
76 void SUIT_ToolButton::AddAction(QAction* theAction)
78 bool aIsFirst = false;
79 if ( myPopup->count() == 0 )
82 setPixmap(theAction->iconSet().pixmap());
83 setTextLabel(theAction->text());
84 theAction->addTo( myPopup );
85 QMenuItem* aItem = myPopup->findItem(myPopup->idAt(0));
88 mySignal = aItem->signal();
92 theAction->addTo( myPopup );
95 /*! Sets myPopup item with theIndex as current*/
96 void SUIT_ToolButton::SetItem(int theIndex)
98 int anId = myPopup->idAt(theIndex);
101 // Protection against unexpected null pointers returned
102 if ( myPopup->iconSet(anId) )
103 setPixmap(myPopup->iconSet(anId)->pixmap());
104 setTextLabel(myPopup->text(anId));
105 QMenuItem* aItem = myPopup->findItem(anId);
108 mySignal = aItem->signal();
114 * On select action (icon and text set with id = \a theItemID)
116 void SUIT_ToolButton::OnSelectAction(int theItemID)
118 if (myChangeItemAfterClick)
120 // Protection against unexpected null pointers returned
121 if ( myPopup->iconSet(theItemID) )
122 setPixmap(myPopup->iconSet(theItemID)->pixmap());
123 setTextLabel(myPopup->text(theItemID));
124 QMenuItem* aItem = myPopup->findItem(theItemID);
127 mySignal = aItem->signal();
134 /*!On mouse release event.*/
135 void SUIT_ToolButton::mouseReleaseEvent ( QMouseEvent * theEvent)
137 QToolButton::mouseReleaseEvent(theEvent);
138 if (mySignal != NULL)
140 mySignal->activate();