Salome HOME
The slot "onTextChanged" is disconnect, but value is updated in method value().
[modules/gui.git] / src / Qtx / QtxAction.cxx
1 // File:      QtxAction.cxx
2 // Author:    Sergey TELKOV
3
4 #include "QtxAction.h"
5
6 #include <qpopupmenu.h>
7
8 /*!
9         Name: QtxAction [public]
10         Desc: Constructs an action with given parent and name. If toggle is true the
11                   action will be a toggle action, otherwise it will be a command action.
12 */
13
14 QtxAction::QtxAction( QObject* parent, const char* name, bool toggle )
15 : QAction( parent, name, toggle )
16 {
17 }
18
19 /*!
20         Name: QtxAction [public]
21         Desc: This constructor creates an action with the following properties: the
22                   description text, the icon or iconset icon, the menu text and keyboard
23                   accelerator. It is a child of given parent and named specified name.
24                   If toggle is true the action will be a toggle action, otherwise it will
25                   be a command action.
26 */
27
28 QtxAction::QtxAction( const QString& text, const QIconSet& icon,
29                       const QString& menuText, int accel,
30                       QObject* parent, const char* name, bool toggle )
31 : QAction( text, icon, menuText, accel, parent, name, toggle )
32 {
33 }
34
35 /*!
36         Name: QtxAction [public]
37         Desc: This constructor creates an action with the following properties: the
38                   description text, the menu text and keyboard accelerator. It is a child
39                   of given parent and named specified name. If toggle is true the action
40                   will be a toggle action, otherwise it will be a command action.
41 */
42
43 QtxAction::QtxAction( const QString& text, const QString& menuText, int accel,
44                       QObject* parent, const char* name, bool toggle )
45 : QAction( text, menuText, accel, parent, name, toggle )
46 {
47 }
48
49 /*!
50         Name: ~QtxAction [virtual public]
51         Desc: Destructor.
52 */
53
54 QtxAction::~QtxAction()
55 {
56 }
57
58 /*!
59         Name: addTo [virtual public]
60         Desc: Adds this action to widget. Returns true if the action was added
61                   successfully and false otherwise.
62 */
63
64 bool QtxAction::addTo( QWidget* w )
65 {
66   return QAction::addTo( w );
67 }
68
69 /*!
70         Name: addTo [virtual public]
71         Desc: Adds this action to widget. If widget is QPopupMenu given index will
72                   be used for menu item inserting. Returns true if the action was added
73                   successfully and false otherwise.
74 */
75
76 bool QtxAction::addTo( QWidget* w, int index )
77 {
78   if ( !addTo( w ) )
79     return false;
80
81   if ( w->inherits( "QPopupMenu" ) )
82   {
83     QPopupMenu* popup = (QPopupMenu*)w;
84     if ( index < (int)popup->count() - 1 )
85     {
86       int id = popup->idAt( popup->count() - 1 );
87       if ( id != -1 )
88       {
89                           QMenuItem* item = popup->findItem( id );
90                                 if ( item && item->isSeparator() )
91                                 {
92                                         popup->removeItem( id );
93           popup->insertSeparator( index );
94                                 }
95                                 else
96                                 {
97                                         QPopupMenu* p = item ? item->popup() : 0;
98                                         int accel = popup->accel( id );
99                                         bool isOn = popup->isItemEnabled( id );
100                                         QString text = popup->text( id );
101                                         QIconSet icon;
102                                         if ( popup->iconSet( id ) )
103                                                 icon = *popup->iconSet( id );
104                                         popup->removeItem( id );
105                                         int pos;
106                                         if ( icon.isNull() )
107                                                 if ( p )
108                                                         pos = popup->indexOf( popup->insertItem( text, p, id, index ) );
109                                                 else
110                                                         pos = popup->indexOf( popup->insertItem( text, id, index ) );
111                                         else
112                                                 if ( p )
113                                                         pos = popup->indexOf( popup->insertItem( icon, text, p, id, index ) );
114                                                 else
115                                                         pos = popup->indexOf( popup->insertItem( icon, text, p, id, index ) );
116                                         popup->setId( pos, id );
117                                         popup->setAccel( accel, id );
118                                         popup->setItemEnabled( id, isOn );
119                                         if ( !p )
120                                                 popup->connectItem( id, this, SLOT( internalActivation() ) );
121                                 }
122       }
123     }
124   }
125
126   return true;
127 }
128
129 /*!
130         Name: setPopup [virtual public]
131         Desc: Set or unset the sub popup menu for item with specified id in the given popup.
132 */
133
134 void QtxAction::setPopup( QPopupMenu* popup, const int id, QPopupMenu* subPopup ) const
135 {
136   if ( !popup )
137     return;
138
139   QMenuData* md = 0;
140   const QMenuData* pmd = popup;
141   QMenuItem* item = popup->findItem( id, &md );
142   if ( !item || md != pmd )
143     return;
144
145   QPopupMenu* oldPopup = item->popup();
146   if ( oldPopup == subPopup )
147     return;
148
149   int accel = popup->accel( id );
150   bool isOn = popup->isItemEnabled( id );
151   QString text = popup->text( id );
152   QIconSet icon;
153         if ( popup->iconSet( id ) )
154     icon = *popup->iconSet( id );
155   popup->removeItem( id );
156
157   int pos;
158   if ( icon.isNull() )
159     pos = popup->indexOf( subPopup ? popup->insertItem( text, subPopup ) : popup->insertItem( text ) );
160   else
161     pos = popup->indexOf( subPopup ? popup->insertItem( icon, text, subPopup ) : popup->insertItem( icon, text ) );
162
163   popup->setId( pos, id );
164   popup->setAccel( accel, id );
165   popup->setItemEnabled( id, isOn );
166
167   delete oldPopup;
168 }