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