]> SALOME platform Git repositories - modules/gui.git/blob - src/SUIT/SUIT_ToolButton.cxx
Salome HOME
f0434db3c47985980d46f6d26cb52ba9fa1f6413
[modules/gui.git] / src / SUIT / SUIT_ToolButton.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
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.
7 // 
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/
18 //
19
20 #include "SUIT_ToolButton.h"
21
22 #include <qpopupmenu.h>
23 #include <qstyle.h>
24
25 /*!Constructor.*/
26 SUIT_ToolButton::SUIT_ToolButton( QWidget *parent, 
27                                         const char *name,
28                                         bool changeItemAfterClick)
29  : QToolButton(  parent, name ),
30    myChangeItemAfterClick( changeItemAfterClick )
31 {
32   initialize();
33 }
34
35 /*!Constructor.*/
36 SUIT_ToolButton::SUIT_ToolButton( const QPixmap & pm,
37                                         const QString &textLabel,
38                                         const QString& grouptext,
39                                         QObject * receiver,
40                                         const char* slot,
41                                         QToolBar * parent,
42                                         const char* name,
43                                         bool changeItemAfterClick)
44  :QToolButton(pm, textLabel, grouptext, receiver, slot, parent, name),
45   myChangeItemAfterClick( changeItemAfterClick )
46 {
47   initialize();
48 }
49
50
51 /*!Initialize tool buttons.*/
52 void SUIT_ToolButton::initialize()
53 {
54   mySignal = NULL;
55   myPopup = new QPopupMenu( this );
56   setPopup(myPopup);
57   connect( myPopup, SIGNAL(activated(int)), SLOT(OnSelectAction(int)) );
58   setPopupDelay(250);
59 }
60
61 /*!drawButton is redefined to draw DownArrow*/
62 void SUIT_ToolButton::drawButton( QPainter * p )
63 {
64   QToolButton::drawButton(p);
65
66 //draw DownArrow
67   int x, y, w, h;
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() );
72 }
73
74
75 /*! Add action into popup*/
76 void SUIT_ToolButton::AddAction(QAction* theAction)
77 {
78   bool aIsFirst = false;
79   if ( myPopup->count() == 0 ) 
80   {
81     aIsFirst = true;
82     setPixmap(theAction->iconSet().pixmap());
83     setTextLabel(theAction->text());
84     theAction->addTo( myPopup );
85     QMenuItem* aItem = myPopup->findItem(myPopup->idAt(0));
86     if (aItem != NULL) 
87     {
88       mySignal = aItem->signal();
89     }
90   }
91   else
92     theAction->addTo( myPopup );
93 }
94
95 /*! Sets myPopup item with theIndex as current*/
96 void SUIT_ToolButton::SetItem(int theIndex)
97 {
98   int anId = myPopup->idAt(theIndex);
99   if (anId != -1)
100   {
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);
106     if (aItem != NULL) 
107     {
108       mySignal = aItem->signal();
109     }
110   }
111 }
112
113 /*!Public SLOT.
114  * On select action (icon and text set with id = \a theItemID)
115  */
116 void SUIT_ToolButton::OnSelectAction(int theItemID)
117 {
118   if (myChangeItemAfterClick)
119   {
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);
125     if (aItem != NULL) 
126     {
127       mySignal = aItem->signal();
128     }
129   }
130 }
131
132
133
134 /*!On mouse release event.*/
135 void SUIT_ToolButton::mouseReleaseEvent ( QMouseEvent * theEvent)
136 {
137   QToolButton::mouseReleaseEvent(theEvent);
138   if (mySignal != NULL) 
139   {
140     mySignal->activate();
141   }
142 }
143
144