source: vendor/trolltech/current/examples/menu/menu.cpp

Last change on this file was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

  • Property svn:keywords set to Id
File size: 9.4 KB
Line 
1/****************************************************************************
2** $Id: menu.cpp 2 2005-11-16 15:49:26Z dmik $
3**
4** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
5**
6** This file is part of an example program for Qt. This example
7** program may be used, distributed and modified without limitation.
8**
9*****************************************************************************/
10
11#include "menu.h"
12#include <qcursor.h>
13#include <qpopupmenu.h>
14#include <qapplication.h>
15#include <qmessagebox.h>
16#include <qpixmap.h>
17#include <qpainter.h>
18
19/* XPM */
20static const char * p1_xpm[] = {
21"16 16 3 1",
22" c None",
23". c #000000000000",
24"X c #FFFFFFFF0000",
25" ",
26" ",
27" .... ",
28" .XXXX. ",
29" .............. ",
30" .XXXXXXXXXXXX. ",
31" .XXXXXXXXXXXX. ",
32" .XXXXXXXXXXXX. ",
33" .XXXXXXXXXXXX. ",
34" .XXXXXXXXXXXX. ",
35" .XXXXXXXXXXXX. ",
36" .XXXXXXXXXXXX. ",
37" .XXXXXXXXXXXX. ",
38" .XXXXXXXXXXXX. ",
39" .............. ",
40" "};
41
42/* XPM */
43static const char * p2_xpm[] = {
44"16 16 3 1",
45" c None",
46". c #000000000000",
47"X c #FFFFFFFFFFFF",
48" ",
49" ...... ",
50" .XXX.X. ",
51" .XXX.XX. ",
52" .XXX.XXX. ",
53" .XXX..... ",
54" .XXXXXXX. ",
55" .XXXXXXX. ",
56" .XXXXXXX. ",
57" .XXXXXXX. ",
58" .XXXXXXX. ",
59" .XXXXXXX. ",
60" .XXXXXXX. ",
61" ......... ",
62" ",
63" "};
64
65/* XPM */
66static const char * p3_xpm[] = {
67"16 16 3 1",
68" c None",
69". c #000000000000",
70"X c #FFFFFFFFFFFF",
71" ",
72" ",
73" ......... ",
74" ........... ",
75" ........ .. ",
76" ........... ",
77" ........... ",
78" ........... ",
79" ........... ",
80" ...XXXXX... ",
81" ...XXXXX... ",
82" ...XXXXX... ",
83" ...XXXXX... ",
84" ......... ",
85" ",
86" "};
87
88
89/*
90 Auxiliary class to provide fancy menu items with different
91 fonts. Used for the "bold" and "underline" menu items in the options
92 menu.
93 */
94class MyMenuItem : public QCustomMenuItem
95{
96public:
97 MyMenuItem( const QString& s, const QFont& f )
98 : string( s ), font( f ){};
99 ~MyMenuItem(){}
100
101 void paint( QPainter* p, const QColorGroup& /*cg*/, bool /*act*/, bool /*enabled*/, int x, int y, int w, int h )
102 {
103 p->setFont ( font );
104 p->drawText( x, y, w, h, AlignLeft | AlignVCenter | DontClip | ShowPrefix, string );
105 }
106
107 QSize sizeHint()
108 {
109 return QFontMetrics( font ).size( AlignLeft | AlignVCenter | ShowPrefix | DontClip, string );
110 }
111private:
112 QString string;
113 QFont font;
114};
115
116
117MenuExample::MenuExample( QWidget *parent, const char *name )
118 : QWidget( parent, name )
119{
120 QPixmap p1( p1_xpm );
121 QPixmap p2( p2_xpm );
122 QPixmap p3( p3_xpm );
123 QPopupMenu *print = new QPopupMenu( this );
124 Q_CHECK_PTR( print );
125 print->insertTearOffHandle();
126 print->insertItem( "&Print to printer", this, SLOT(printer()) );
127 print->insertItem( "Print to &file", this, SLOT(file()) );
128 print->insertItem( "Print to fa&x", this, SLOT(fax()) );
129 print->insertSeparator();
130 print->insertItem( "Printer &Setup", this, SLOT(printerSetup()) );
131
132 QPopupMenu *file = new QPopupMenu( this );
133 Q_CHECK_PTR( file );
134 file->insertItem( p1, "&Open", this, SLOT(open()), CTRL+Key_O );
135 file->insertItem( p2, "&New", this, SLOT(news()), CTRL+Key_N );
136 file->insertItem( p3, "&Save", this, SLOT(save()), CTRL+Key_S );
137 file->insertItem( "&Close", this, SLOT(closeDoc()), CTRL+Key_W );
138 file->insertSeparator();
139 file->insertItem( "&Print", print, CTRL+Key_P );
140 file->insertSeparator();
141 file->insertItem( "E&xit", qApp, SLOT(quit()), CTRL+Key_Q );
142
143 QPopupMenu *edit = new QPopupMenu( this );
144 Q_CHECK_PTR( edit );
145 int undoID = edit->insertItem( "&Undo", this, SLOT(undo()) );
146 int redoID = edit->insertItem( "&Redo", this, SLOT(redo()) );
147 edit->setItemEnabled( undoID, FALSE );
148 edit->setItemEnabled( redoID, FALSE );
149
150 QPopupMenu* options = new QPopupMenu( this );
151 Q_CHECK_PTR( options );
152 options->insertTearOffHandle();
153 options->setCaption("Options");
154 options->insertItem( "&Normal Font", this, SLOT(normal()) );
155 options->insertSeparator();
156
157 options->polish(); // adjust system settings
158 QFont f = options->font();
159 f.setBold( TRUE );
160 boldID = options->insertItem( new MyMenuItem( "Bold", f ) );
161 options->setAccel( CTRL+Key_B, boldID );
162 options->connectItem( boldID, this, SLOT(bold()) );
163 f = font();
164 f.setUnderline( TRUE );
165 underlineID = options->insertItem( new MyMenuItem( "Underline", f ) );
166 options->setAccel( CTRL+Key_U, underlineID );
167 options->connectItem( underlineID, this, SLOT(underline()) );
168
169 isBold = FALSE;
170 isUnderline = FALSE;
171 options->setCheckable( TRUE );
172
173
174 QPopupMenu *help = new QPopupMenu( this );
175 Q_CHECK_PTR( help );
176 help->insertItem( "&About", this, SLOT(about()), CTRL+Key_H );
177 help->insertItem( "About &Qt", this, SLOT(aboutQt()) );
178
179 // If we used a QMainWindow we could use its built-in menuBar().
180 menu = new QMenuBar( this );
181 Q_CHECK_PTR( menu );
182 menu->insertItem( "&File", file );
183 menu->insertItem( "&Edit", edit );
184 menu->insertItem( "&Options", options );
185 menu->insertSeparator();
186 menu->insertItem( "&Help", help );
187 menu->setSeparator( QMenuBar::InWindowsStyle );
188
189
190 QLabel *msg = new QLabel( this );
191 Q_CHECK_PTR( msg );
192 msg->setText( "A context menu is available.\n"
193 "Invoke it by right-clicking or by"
194 " pressing the 'context' button." );
195 msg->setGeometry( 0, height() - 60, width(), 60 );
196 msg->setAlignment( AlignCenter );
197
198 label = new QLabel( this );
199 Q_CHECK_PTR( label );
200 label->setGeometry( 20, rect().center().y()-20, width()-40, 40 );
201 label->setFrameStyle( QFrame::Box | QFrame::Raised );
202 label->setLineWidth( 1 );
203 label->setAlignment( AlignCenter );
204
205 connect( this, SIGNAL(explain(const QString&)),
206 label, SLOT(setText(const QString&)) );
207
208 setMinimumSize( 100, 80 );
209 setFocusPolicy( QWidget::ClickFocus );
210}
211
212
213void MenuExample::contextMenuEvent( QContextMenuEvent * )
214{
215 QPopupMenu* contextMenu = new QPopupMenu( this );
216 Q_CHECK_PTR( contextMenu );
217 QLabel *caption = new QLabel( "<font color=darkblue><u><b>"
218 "Context Menu</b></u></font>", this );
219 caption->setAlignment( Qt::AlignCenter );
220 contextMenu->insertItem( caption );
221 contextMenu->insertItem( "&New", this, SLOT(news()), CTRL+Key_N );
222 contextMenu->insertItem( "&Open...", this, SLOT(open()), CTRL+Key_O );
223 contextMenu->insertItem( "&Save", this, SLOT(save()), CTRL+Key_S );
224 QPopupMenu *submenu = new QPopupMenu( this );
225 Q_CHECK_PTR( submenu );
226 submenu->insertItem( "&Print to printer", this, SLOT(printer()) );
227 submenu->insertItem( "Print to &file", this, SLOT(file()) );
228 submenu->insertItem( "Print to fa&x", this, SLOT(fax()) );
229 contextMenu->insertItem( "&Print", submenu );
230 contextMenu->exec( QCursor::pos() );
231 delete contextMenu;
232}
233
234
235void MenuExample::open()
236{
237 emit explain( "File/Open selected" );
238}
239
240
241void MenuExample::news()
242{
243 emit explain( "File/New selected" );
244}
245
246void MenuExample::save()
247{
248 emit explain( "File/Save selected" );
249}
250
251
252void MenuExample::closeDoc()
253{
254 emit explain( "File/Close selected" );
255}
256
257
258void MenuExample::undo()
259{
260 emit explain( "Edit/Undo selected" );
261}
262
263
264void MenuExample::redo()
265{
266 emit explain( "Edit/Redo selected" );
267}
268
269
270void MenuExample::normal()
271{
272 isBold = FALSE;
273 isUnderline = FALSE;
274 QFont font;
275 label->setFont( font );
276 menu->setItemChecked( boldID, isBold );
277 menu->setItemChecked( underlineID, isUnderline );
278 emit explain( "Options/Normal selected" );
279}
280
281
282void MenuExample::bold()
283{
284 isBold = !isBold;
285 QFont font;
286 font.setBold( isBold );
287 font.setUnderline( isUnderline );
288 label->setFont( font );
289 menu->setItemChecked( boldID, isBold );
290 emit explain( "Options/Bold selected" );
291}
292
293
294void MenuExample::underline()
295{
296 isUnderline = !isUnderline;
297 QFont font;
298 font.setBold( isBold );
299 font.setUnderline( isUnderline );
300 label->setFont( font );
301 menu->setItemChecked( underlineID, isUnderline );
302 emit explain( "Options/Underline selected" );
303}
304
305
306void MenuExample::about()
307{
308 QMessageBox::about( this, "Qt Menu Example",
309 "This example demonstrates simple use of Qt menus.\n"
310 "You can cut and paste lines from it to your own\n"
311 "programs." );
312}
313
314
315void MenuExample::aboutQt()
316{
317 QMessageBox::aboutQt( this, "Qt Menu Example" );
318}
319
320
321void MenuExample::printer()
322{
323 emit explain( "File/Printer/Print selected" );
324}
325
326void MenuExample::file()
327{
328 emit explain( "File/Printer/Print To File selected" );
329}
330
331void MenuExample::fax()
332{
333 emit explain( "File/Printer/Print To Fax selected" );
334}
335
336void MenuExample::printerSetup()
337{
338 emit explain( "File/Printer/Printer Setup selected" );
339}
340
341
342void MenuExample::resizeEvent( QResizeEvent * )
343{
344 label->setGeometry( 20, rect().center().y()-20, width()-40, 40 );
345}
346
347
348int main( int argc, char ** argv )
349{
350 QApplication a( argc, argv );
351 MenuExample m;
352 m.setCaption("Qt Examples - Menus");
353 a.setMainWidget( &m );
354 m.show();
355 return a.exec();
356}
Note: See TracBrowser for help on using the repository browser.