Salome HOME
1fd8aac5bee2f0f1a2ca81828353134b1828fdb1
[modules/gui.git] / src / SUIT / SUIT_ToolButton.cxx
1
2 #include "SUIT_ToolButton.h"
3
4 #include <qpopupmenu.h>
5 #include <qstyle.h>
6
7 /*!Constructor.*/
8 SUIT_ToolButton::SUIT_ToolButton( QWidget *parent, 
9                                         const char *name,
10                                         bool changeItemAfterClick)
11  : QToolButton(  parent, name ),
12    myChangeItemAfterClick( changeItemAfterClick )
13 {
14   initialize();
15 }
16
17 /*!Constructor.*/
18 SUIT_ToolButton::SUIT_ToolButton( const QPixmap & pm,
19                                         const QString &textLabel,
20                                         const QString& grouptext,
21                                         QObject * receiver,
22                                         const char* slot,
23                                         QToolBar * parent,
24                                         const char* name,
25                                         bool changeItemAfterClick)
26  :QToolButton(pm, textLabel, grouptext, receiver, slot, parent, name),
27   myChangeItemAfterClick( changeItemAfterClick )
28 {
29   initialize();
30 }
31
32
33 /*!Initialize tool buttons.*/
34 void SUIT_ToolButton::initialize()
35 {
36   mySignal = NULL;
37   myPopup = new QPopupMenu( this );
38   setPopup(myPopup);
39   connect( myPopup, SIGNAL(activated(int)), SLOT(OnSelectAction(int)) );
40   setPopupDelay(250);
41 }
42
43 /*!drawButton is redefined to draw DownArrow*/
44 void SUIT_ToolButton::drawButton( QPainter * p )
45 {
46   QToolButton::drawButton(p);
47
48 //draw DownArrow
49   int x, y, w, h;
50   QStyle::visualRect(QRect(0, 0, width(), height()), this).rect( &x, &y, &w, &h );
51   style().drawPrimitive( QStyle::PE_ArrowDown,
52     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)
53     colorGroup(), isEnabled() );
54 }
55
56
57 /*! Add action into popup*/
58 void SUIT_ToolButton::AddAction(QAction* theAction)
59 {
60   bool aIsFirst = false;
61   if ( myPopup->count() == 0 ) 
62   {
63     aIsFirst = true;
64     setPixmap(theAction->iconSet().pixmap());
65     setTextLabel(theAction->text());
66     theAction->addTo( myPopup );
67     QMenuItem* aItem = myPopup->findItem(myPopup->idAt(0));
68     if (aItem != NULL) 
69     {
70       mySignal = aItem->signal();
71     }
72   }
73   else
74     theAction->addTo( myPopup );
75 }
76
77 /*! Sets myPopup item with theIndex as current*/
78 void SUIT_ToolButton::SetItem(int theIndex)
79 {
80   int anId = myPopup->idAt(theIndex);
81   if (anId != -1)
82   {
83     // Protection against unexpected null pointers returned
84     if ( myPopup->iconSet(anId) )
85       setPixmap(myPopup->iconSet(anId)->pixmap());
86     setTextLabel(myPopup->text(anId));
87     QMenuItem* aItem = myPopup->findItem(anId);
88     if (aItem != NULL) 
89     {
90       mySignal = aItem->signal();
91     }
92   }
93 }
94
95 /*!Public SLOT.
96  * On select action (icon and text set with id = \a theItemID)
97  */
98 void SUIT_ToolButton::OnSelectAction(int theItemID)
99 {
100   if (myChangeItemAfterClick)
101   {
102     // Protection against unexpected null pointers returned
103     if ( myPopup->iconSet(theItemID) )
104       setPixmap(myPopup->iconSet(theItemID)->pixmap());
105     setTextLabel(myPopup->text(theItemID));
106     QMenuItem* aItem = myPopup->findItem(theItemID);
107     if (aItem != NULL) 
108     {
109       mySignal = aItem->signal();
110     }
111   }
112 }
113
114
115
116 /*!On mouse release event.*/
117 void SUIT_ToolButton::mouseReleaseEvent ( QMouseEvent * theEvent)
118 {
119   QToolButton::mouseReleaseEvent(theEvent);
120   if (mySignal != NULL) 
121   {
122     mySignal->activate();
123   }
124 }
125
126