Salome HOME
Initial version
[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 SUIT_ToolButton::SUIT_ToolButton( QWidget *parent, 
8                                         const char *name,
9                                         bool changeItemAfterClick)
10  : QToolButton(  parent, name ),
11    myChangeItemAfterClick( changeItemAfterClick )
12 {
13   initialize();
14 }
15
16
17 SUIT_ToolButton::SUIT_ToolButton( const QPixmap & pm,
18                                         const QString &textLabel,
19                                         const QString& grouptext,
20                                         QObject * receiver,
21                                         const char* slot,
22                                         QToolBar * parent,
23                                         const char* name,
24                                         bool changeItemAfterClick)
25  :QToolButton(pm, textLabel, grouptext, receiver, slot, parent, name),
26   myChangeItemAfterClick( changeItemAfterClick )
27 {
28   initialize();
29 }
30
31
32 //********************************************************************************
33 void SUIT_ToolButton::initialize()
34 {
35   mySignal = NULL;
36   myPopup = new QPopupMenu( this );
37   setPopup(myPopup);
38   connect( myPopup, SIGNAL(activated(int)), SLOT(OnSelectAction(int)) );
39   setPopupDelay(250);
40 }
41
42 //********************************************************************************
43 void SUIT_ToolButton::drawButton( QPainter * p )
44 {
45   QToolButton::drawButton(p);
46
47 //draw DownArrow
48   int x, y, w, h;
49   QStyle::visualRect(QRect(0, 0, width(), height()), this).rect( &x, &y, &w, &h );
50   style().drawPrimitive( QStyle::PE_ArrowDown,
51     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)
52     colorGroup(), isEnabled() );
53 }
54
55
56 //********************************************************************************
57 void SUIT_ToolButton::AddAction(QAction* theAction)
58 {
59   bool aIsFirst = false;
60   if ( myPopup->count() == 0 ) 
61   {
62     aIsFirst = true;
63     setPixmap(theAction->iconSet().pixmap());
64     setTextLabel(theAction->text());
65     theAction->addTo( myPopup );
66     QMenuItem* aItem = myPopup->findItem(myPopup->idAt(0));
67     if (aItem != NULL) 
68     {
69       mySignal = aItem->signal();
70     }
71   }
72   else
73     theAction->addTo( myPopup );
74 }
75
76
77 //********************************************************************************
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 //********************************************************************************
96 void SUIT_ToolButton::OnSelectAction(int theItemID)
97 {
98   if (myChangeItemAfterClick)
99   {
100     // Protection against unexpected null pointers returned
101     if ( myPopup->iconSet(theItemID) )
102       setPixmap(myPopup->iconSet(theItemID)->pixmap());
103     setTextLabel(myPopup->text(theItemID));
104     QMenuItem* aItem = myPopup->findItem(theItemID);
105     if (aItem != NULL) 
106     {
107       mySignal = aItem->signal();
108     }
109   }
110 }
111
112
113
114 //********************************************************************************
115 void SUIT_ToolButton::mouseReleaseEvent ( QMouseEvent * theEvent)
116 {
117   QToolButton::mouseReleaseEvent(theEvent);
118   if (mySignal != NULL) 
119   {
120     mySignal->activate();
121   }
122 }
123
124