Salome HOME
9abc1d47288165ee8406b1f34ca066abb3b0df9b
[modules/gui.git] / src / Qtx / QtxPopupMenu.cxx
1 #include "QtxPopupMenu.h"
2
3 #include <qpainter.h>
4
5 /*!
6     Class: QtxPopupMenu::TitleMenuItem [Internal]
7     Descr: Custom menu item for popup title.
8 */
9
10 class QtxPopupMenu::TitleMenuItem : public QCustomMenuItem
11 {
12 public:
13   TitleMenuItem( const QString&, const QIconSet&, const int );
14   virtual ~TitleMenuItem();
15
16   virtual bool  fullSpan() const;
17   virtual bool  isSeparator() const;
18   virtual void  setFont( const QFont& );
19
20   virtual void  paint( QPainter*, const QColorGroup&, bool, bool, int, int, int, int );
21   virtual QSize sizeHint();
22
23 private:
24   QString       myText;
25   QIconSet      myIcon;
26   QFont         myFont;
27   int           myAlign;
28 };
29
30 QtxPopupMenu::TitleMenuItem::TitleMenuItem( const QString& txt, const QIconSet& ico, const int align )
31 : QCustomMenuItem(),
32 myText( txt ),
33 myIcon( ico ),
34 myAlign( align )
35 {
36 }
37
38 QtxPopupMenu::TitleMenuItem::~TitleMenuItem()
39 {
40 }
41
42 bool QtxPopupMenu::TitleMenuItem::fullSpan() const
43 {
44   return true;
45 }
46
47 bool QtxPopupMenu::TitleMenuItem::isSeparator() const
48 {
49   return false;
50 }
51
52 void QtxPopupMenu::TitleMenuItem::setFont( const QFont& font )
53 {
54   myFont = font;
55   myFont.setBold( true );
56 }
57
58 void QtxPopupMenu::TitleMenuItem::paint( QPainter* p, const QColorGroup& cg,
59                                          bool act, bool enabled, int x, int y, int w, int h )
60 {
61   QFont f = p->font();
62   p->setFont( myFont );
63
64   p->fillRect( x, y, w, h, cg.brush( QColorGroup::Dark ) );
65
66   p->setPen( cg.shadow() );
67   p->drawRect( x, y, w, h );
68
69   int m = 3;
70   int s = 3;
71   int iw = p->fontMetrics().width( myText ) + ( myIcon.isNull() ? 0 : myIcon.pixmap().width() + s );
72   int ih = QMAX( ( myIcon.isNull() ? 0 : myIcon.pixmap().height() ), p->fontMetrics().height() );
73
74   int ix = x;
75   int iy = y + m;
76
77   if ( myAlign & AlignLeft )
78     ix = x;
79   else if ( myAlign & AlignRight )
80     ix = x + ( w - iw );
81   else if ( myAlign & AlignHCenter )
82     ix = x + ( w - iw ) / 2;
83
84   if ( myAlign & AlignTop )
85     iy = y;
86   else if ( myAlign & AlignBottom )
87     iy = y + ( h - ih - m );
88   else if ( myAlign & AlignVCenter )
89     iy = y + ( h - ih ) / 2;
90
91   if ( !myIcon.isNull() )
92   {
93     p->drawPixmap( ix, iy + ( ih - myIcon.pixmap().height() ) / 2, myIcon.pixmap() );
94     ix += myIcon.pixmap().width() + s;
95   }
96
97   p->setPen( cg.brightText() );
98   p->drawText( ix, iy + ( ih - p->fontMetrics().height() ) / 2 +
99                p->fontMetrics().ascent(), myText, 0, -1 );
100
101   p->setFont( f );
102 }
103
104 QSize QtxPopupMenu::TitleMenuItem::sizeHint()
105 {
106   QFontMetrics fM( myFont );
107
108   int m = 3;
109   int s = 3;
110   int w = fM.width( myText ) + ( myIcon.isNull() ? 0 : myIcon.pixmap().width() + s );
111   int h = QMAX( ( myIcon.isNull() ? 0 : myIcon.pixmap().height() ), fM.height() ) + 2 * m;
112
113   return QSize( w, h );
114 }
115
116 /*!
117     Class: QtxPopupMenu [Public]
118     Descr: Popup menu item with title.
119 */
120
121 QtxPopupMenu::QtxPopupMenu( QWidget* parent, const char* name )
122 : QPopupMenu( parent, name ),
123 myId( -1 ),
124 myPolicy( TitleAuto ),
125 myAlign( AlignCenter )
126 {
127 }
128
129 QtxPopupMenu::~QtxPopupMenu()
130 {
131 }
132
133 QString QtxPopupMenu::titleText() const
134 {
135   return myText;
136 }
137
138 QIconSet QtxPopupMenu::titleIcon() const
139 {
140   return myIcon;
141 }
142
143 int QtxPopupMenu::titlePolicy() const
144 {
145   return myPolicy;
146 }
147
148 int QtxPopupMenu::titleAlignment() const
149 {
150   return myAlign;
151 }
152
153 void QtxPopupMenu::setTitleText( const QString& txt )
154 {
155   if ( myText == txt )
156     return;
157
158   myText = txt;
159
160   updateTitle();
161 }
162
163 void QtxPopupMenu::setTitleIcon( const QIconSet& ico )
164 {
165   myIcon = ico;
166
167   updateTitle();
168 }
169
170 void QtxPopupMenu::setTitlePolicy( const int p )
171 {
172   if ( myPolicy == p )
173     return;
174
175   myPolicy = p;
176
177   updateTitle();
178 }
179
180 void QtxPopupMenu::setTitleAlignment( const int a )
181 {
182   if ( myAlign == a )
183     return;
184
185   myAlign = a;
186
187   updateTitle();
188 }
189
190 void QtxPopupMenu::show()
191 {
192   insertTitle();
193
194   QPopupMenu::show();
195 }
196
197 void QtxPopupMenu::hide()
198 {
199   QPopupMenu::hide();
200
201   removeTitle();
202 }
203
204 QtxPopupMenu::TitleMenuItem* QtxPopupMenu::createTitleItem( const QString& txt, const QIconSet& ico,
205                                                             const int align ) const
206 {
207   return new TitleMenuItem( txt, ico, align );
208 }
209
210 void QtxPopupMenu::insertTitle()
211 {
212   if ( myId != -1 || titlePolicy() == TitleOff ||
213        ( titlePolicy() == TitleAuto && titleText().stripWhiteSpace().isEmpty() ) )
214     return;
215
216   TitleMenuItem* item = createTitleItem( titleText(), titleIcon(), titleAlignment() );
217
218   myId = insertItem( item, -1, 0 );
219   setItemEnabled( myId, false );
220 }
221
222 void QtxPopupMenu::removeTitle()
223 {
224   if ( myId == -1 )
225     return;
226
227   removeItem( myId );
228   myId = -1;
229 }
230
231 void QtxPopupMenu::updateTitle()
232 {
233   if ( myId != -1 )
234   {
235     removeTitle();
236     insertTitle();
237   }
238 }