source: trunk/examples/menu/menu.cpp@ 16

Last change on this file since 16 was 8, checked in by dmik, 20 years ago

Transferred Qt for OS/2 version 3.3.1-rc5 sources from the CVS

  • Property svn:keywords set to Id
File size: 9.5 KB
Line 
1/****************************************************************************
2** $Id: menu.cpp 8 2005-11-16 19:36:46Z 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
155 options->polish(); // adjust system settings
156 QFont of( options->font().family(), options->font().pointSize() );
157 int normID = options->insertItem( new MyMenuItem( "&Normal Font", of ) );
158 options->connectItem( normID, this, SLOT(normal()) );
159 options->insertSeparator();
160 QFont f = QFont( of );
161 f.setBold( TRUE );
162 boldID = options->insertItem( new MyMenuItem( "Bold", f ) );
163 options->setAccel( CTRL+Key_B, boldID );
164 options->connectItem( boldID, this, SLOT(bold()) );
165 f = QFont( of );
166 f.setUnderline( TRUE );
167 underlineID = options->insertItem( new MyMenuItem( "Underline", f ) );
168 options->setAccel( CTRL+Key_U, underlineID );
169 options->connectItem( underlineID, this, SLOT(underline()) );
170
171 isBold = FALSE;
172 isUnderline = FALSE;
173 options->setCheckable( TRUE );
174
175
176 QPopupMenu *help = new QPopupMenu( this );
177 Q_CHECK_PTR( help );
178 help->insertItem( "&About", this, SLOT(about()), CTRL+Key_H );
179 help->insertItem( "About &Qt", this, SLOT(aboutQt()) );
180
181 // If we used a QMainWindow we could use its built-in menuBar().
182 menu = new QMenuBar( this );
183 Q_CHECK_PTR( menu );
184 menu->insertItem( "&File", file );
185 menu->insertItem( "&Edit", edit );
186 menu->insertItem( "&Options", options );
187 menu->insertSeparator();
188 menu->insertItem( "&Help", help );
189 menu->setSeparator( QMenuBar::InWindowsStyle );
190
191
192 QLabel *msg = new QLabel( this );
193 Q_CHECK_PTR( msg );
194 msg->setText( "A context menu is available.\n"
195 "Invoke it by right-clicking or by"
196 " pressing the 'context' button." );
197 msg->setGeometry( 0, height() - 60, width(), 60 );
198 msg->setAlignment( AlignCenter );
199
200 label = new QLabel( this );
201 Q_CHECK_PTR( label );
202 label->setGeometry( 20, rect().center().y()-20, width()-40, 40 );
203 label->setFrameStyle( QFrame::Box | QFrame::Raised );
204 label->setLineWidth( 1 );
205 label->setAlignment( AlignCenter );
206
207 connect( this, SIGNAL(explain(const QString&)),
208 label, SLOT(setText(const QString&)) );
209
210 setMinimumSize( 100, 80 );
211 setFocusPolicy( QWidget::ClickFocus );
212}
213
214
215void MenuExample::contextMenuEvent( QContextMenuEvent * )
216{
217 QPopupMenu* contextMenu = new QPopupMenu( this );
218 Q_CHECK_PTR( contextMenu );
219 QLabel *caption = new QLabel( "<font color=darkblue><u><b>"
220 "Context Menu</b></u></font>", this );
221 caption->setAlignment( Qt::AlignCenter );
222 contextMenu->insertItem( caption );
223 contextMenu->insertItem( "&New", this, SLOT(news()), CTRL+Key_N );
224 contextMenu->insertItem( "&Open...", this, SLOT(open()), CTRL+Key_O );
225 contextMenu->insertItem( "&Save", this, SLOT(save()), CTRL+Key_S );
226 QPopupMenu *submenu = new QPopupMenu( this );
227 Q_CHECK_PTR( submenu );
228 submenu->insertItem( "&Print to printer", this, SLOT(printer()) );
229 submenu->insertItem( "Print to &file", this, SLOT(file()) );
230 submenu->insertItem( "Print to fa&x", this, SLOT(fax()) );
231 contextMenu->insertItem( "&Print", submenu );
232 contextMenu->exec( QCursor::pos() );
233 delete contextMenu;
234}
235
236
237void MenuExample::open()
238{
239 emit explain( "File/Open selected" );
240}
241
242
243void MenuExample::news()
244{
245 emit explain( "File/New selected" );
246}
247
248void MenuExample::save()
249{
250 emit explain( "File/Save selected" );
251}
252
253
254void MenuExample::closeDoc()
255{
256 emit explain( "File/Close selected" );
257}
258
259
260void MenuExample::undo()
261{
262 emit explain( "Edit/Undo selected" );
263}
264
265
266void MenuExample::redo()
267{
268 emit explain( "Edit/Redo selected" );
269}
270
271
272void MenuExample::normal()
273{
274 isBold = FALSE;
275 isUnderline = FALSE;
276 QFont font;
277 label->setFont( font );
278 menu->setItemChecked( boldID, isBold );
279 menu->setItemChecked( underlineID, isUnderline );
280 emit explain( "Options/Normal selected" );
281}
282
283
284void MenuExample::bold()
285{
286 isBold = !isBold;
287 QFont font;
288 font.setBold( isBold );
289 font.setUnderline( isUnderline );
290 label->setFont( font );
291 menu->setItemChecked( boldID, isBold );
292 emit explain( "Options/Bold selected" );
293}
294
295
296void MenuExample::underline()
297{
298 isUnderline = !isUnderline;
299 QFont font;
300 font.setBold( isBold );
301 font.setUnderline( isUnderline );
302 label->setFont( font );
303 menu->setItemChecked( underlineID, isUnderline );
304 emit explain( "Options/Underline selected" );
305}
306
307
308void MenuExample::about()
309{
310 QMessageBox::about( this, "Qt Menu Example",
311 "This example demonstrates simple use of Qt menus.\n"
312 "You can cut and paste lines from it to your own\n"
313 "programs." );
314}
315
316
317void MenuExample::aboutQt()
318{
319 QMessageBox::aboutQt( this, "Qt Menu Example" );
320}
321
322
323void MenuExample::printer()
324{
325 emit explain( "File/Printer/Print selected" );
326}
327
328void MenuExample::file()
329{
330 emit explain( "File/Printer/Print To File selected" );
331}
332
333void MenuExample::fax()
334{
335 emit explain( "File/Printer/Print To Fax selected" );
336}
337
338void MenuExample::printerSetup()
339{
340 emit explain( "File/Printer/Printer Setup selected" );
341}
342
343
344void MenuExample::resizeEvent( QResizeEvent * )
345{
346 label->setGeometry( 20, rect().center().y()-20, width()-40, 40 );
347}
348
349
350int main( int argc, char ** argv )
351{
352 QApplication a( argc, argv );
353 MenuExample m;
354 m.setCaption("Qt Examples - Menus");
355 a.setMainWidget( &m );
356 m.show();
357 return a.exec();
358}
Note: See TracBrowser for help on using the repository browser.