source: trunk/examples/drawdemo/drawdemo.cpp@ 203

Last change on this file since 203 was 165, checked in by dmik, 19 years ago

Examples: Added #ifndef QT_NO_PRINTER to allow building when no printer support is available.

  • Property svn:keywords set to Id
File size: 7.8 KB
Line 
1/****************************************************************************
2** $Id: drawdemo.cpp 165 2006-12-11 21:54:29Z 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 <qwidget.h>
12#include <qpainter.h>
13#include <qprinter.h>
14#include <qpushbutton.h>
15#include <qradiobutton.h>
16#include <qbuttongroup.h>
17#include <qapplication.h>
18#include <math.h>
19
20//
21// First we define the functionality our demo should present
22// to the user. You might add different demo-modes if you wish so.
23//
24
25//
26// This function draws a color wheel.
27// The coordinate system x=(0..500), y=(0..500) spans the paint device.
28//
29
30void drawColorWheel( QPainter *p )
31{
32 QFont f( "times", 18, QFont::Bold );
33 p->setFont( f );
34 p->setPen( Qt::black );
35 p->setWindow( 0, 0, 500, 500 ); // defines coordinate system
36
37 for ( int i=0; i<36; i++ ) { // draws 36 rotated rectangles
38
39 QWMatrix matrix;
40 matrix.translate( 250.0F, 250.0F ); // move to center
41 matrix.shear( 0.0F, 0.3F ); // twist it
42 matrix.rotate( (float)i*10 ); // rotate 0,10,20,.. degrees
43 p->setWorldMatrix( matrix ); // use this world matrix
44
45 QColor c;
46 c.setHsv( i*10, 255, 255 ); // rainbow effect
47 p->setBrush( c ); // solid fill with color c
48 p->drawRect( 70, -10, 80, 10 ); // draw the rectangle
49
50 QString n;
51 n.sprintf( "H=%d", i*10 );
52 p->drawText( 80+70+5, 0, n ); // draw the hue number
53 }
54}
55
56
57//
58// This function draws a few lines of text using different fonts.
59//
60
61void drawFonts( QPainter *p )
62{
63 static const char *fonts[] = { "Helvetica", "Courier", "Times", 0 };
64 static int sizes[] = { 10, 12, 18, 24, 36, 0 };
65 int f = 0;
66 int y = 0;
67 while ( fonts[f] ) {
68 int s = 0;
69 while ( sizes[s] ) {
70 QFont font( fonts[f], sizes[s] );
71 p->setFont( font );
72 QFontMetrics fm = p->fontMetrics();
73 y += fm.ascent();
74 p->drawText( 10, y, "Quartz Glyph Job Vex'd Cwm Finks" );
75 y += fm.descent();
76 s++;
77 }
78 f++;
79 }
80}
81
82
83//
84// This function draws some shapes
85//
86
87void drawShapes( QPainter *p )
88{
89 QBrush b1( Qt::blue );
90 QBrush b2( Qt::green, Qt::Dense6Pattern ); // green 12% fill
91 QBrush b3( Qt::NoBrush ); // void brush
92 QBrush b4( Qt::CrossPattern ); // black cross pattern
93
94 p->setPen( Qt::red );
95 p->setBrush( b1 );
96 p->drawRect( 10, 10, 200, 100 );
97 p->setBrush( b2 );
98 p->drawRoundRect( 10, 150, 200, 100, 20, 20 );
99 p->setBrush( b3 );
100 p->drawEllipse( 250, 10, 200, 100 );
101 p->setBrush( b4 );
102 p->drawPie( 250, 150, 200, 100, 45*16, 90*16 );
103}
104
105
106typedef void (*draw_func)(QPainter*);
107
108struct DrawThing {
109 draw_func f;
110 const char *name;
111};
112
113//
114// All previously implemented functions are collected
115// in the following "table".
116// If you implement different functionality, your new draw
117// function must be assigned here with a function pointer and
118// description.
119// Leave the zeros at the end, they will be used
120// as markers referring to the end of the array.
121//
122
123DrawThing ourDrawFunctions[] = {
124// name of the function, title presented to the user
125 { drawColorWheel, "Draw color wheel" },
126 { drawFonts, "Draw fonts" },
127 { drawShapes, "Draw shapes" },
128 { 0, 0 } };
129
130
131
132class DrawView : public QWidget
133{
134 Q_OBJECT
135public:
136 DrawView();
137 ~DrawView();
138public slots:
139 void updateIt( int );
140 void printIt();
141protected:
142 void drawIt( QPainter * );
143 void paintEvent( QPaintEvent * );
144 void resizeEvent( QResizeEvent * );
145private:
146 QPrinter *printer;
147 QButtonGroup *bgroup;
148 QPushButton *print;
149 int drawindex;
150 int maxindex;
151};
152
153
154//
155// Construct the DrawView with buttons.
156//
157
158DrawView::DrawView()
159{
160 setCaption( "Qt Draw Demo Application" );
161 setBackgroundMode(PaletteBase);
162
163 // Create a button group to contain all buttons
164 bgroup = new QButtonGroup( this );
165 bgroup->resize( 200, 200 );
166 connect( bgroup, SIGNAL(clicked(int)), SLOT(updateIt(int)) );
167
168 // Calculate the size for the radio buttons
169 int maxwidth = 80;
170 int maxheight = 10;
171 int i;
172 const char *n;
173 QFontMetrics fm = bgroup->fontMetrics();
174
175 // Find out the longest function description.
176 // Here we make use of the last "0,0"-entry in the
177 // ourDrawFunctions-array.
178 for ( i=0; (n=ourDrawFunctions[i].name) != 0; i++ ) {
179 int w = fm.width( n );
180 maxwidth = QMAX(w,maxwidth); // QMAX is a macro defined in qglobal.h
181 // and returns the biggest of to values.
182 // Due to its macro nature one should use it with care and with
183 // constant parameters only.
184 }
185
186 maxwidth = maxwidth + 30; // allow 30 pixels for radiobuttons
187
188 for ( i=0; (n=ourDrawFunctions[i].name) != 0; i++ ) {
189 QRadioButton *rb = new QRadioButton( n, bgroup );
190 rb->setGeometry( 10, i*30+10, maxwidth, 30 );
191
192 maxheight += 30;
193
194 if ( i == 0 )
195 rb->setChecked( TRUE );
196 }
197
198 maxheight += 10; // maxheight is now 10 pixels upper margin
199 // plus number_of_drawfunctions * 30
200 // plus 10 pixels lower margin
201
202 drawindex = 0; // draw first thing
203 maxindex = i;
204
205 maxwidth += 20; // add some margin, this results in the
206 // final width of bgroup
207
208 bgroup->resize( maxwidth, maxheight ); // resize bgroup to its final size
209 // when no printersupport is provided
210
211
212// If -- at compile time -- printer support will be disabled,
213// we won't set up printing functionality.
214
215#ifndef QT_NO_PRINTER
216
217 printer = new QPrinter;
218
219 // Create and setup the print button
220 print = new QPushButton( "Print...", bgroup );
221 print->resize( 80, 30 );
222 print->move( maxwidth/2 - print->width()/2, maxindex*30+20 );
223 connect( print, SIGNAL(clicked()), SLOT(printIt()) );
224
225 // Resize bgroup to its final size when printersupport is given.
226 bgroup->resize( maxwidth, print->y()+print->height()+10 );
227
228#endif
229
230 resize( 640,300 );
231}
232
233//
234// Clean up.
235//
236DrawView::~DrawView()
237{
238#ifndef QT_NO_PRINTER
239 delete printer;
240#endif
241}
242
243//
244// Called when a radio button is clicked.
245//
246
247void DrawView::updateIt( int index )
248{
249 if ( index < maxindex ) {
250 drawindex = index;
251 update();
252 }
253}
254
255//
256// Calls the drawing function as specified by the radio buttons.
257//
258
259void DrawView::drawIt( QPainter *p )
260{
261 (*ourDrawFunctions[drawindex].f)(p);
262}
263
264//
265// Called when the print button is clicked.
266//
267
268void DrawView::printIt()
269{
270#ifndef QT_NO_PRINTER
271 if ( printer->setup( this ) ) {
272 QPainter paint;
273 if( !paint.begin( printer ) )
274 return;
275 drawIt( &paint );
276 }
277#endif
278}
279
280//
281// Called when the widget needs to be updated.
282//
283
284void DrawView::paintEvent( QPaintEvent * )
285{
286 QPainter paint( this );
287 drawIt( &paint );
288}
289
290//
291// Called when the widget has been resized.
292// Moves the button group to the upper right corner
293// of the widget.
294
295void DrawView::resizeEvent( QResizeEvent * )
296{
297 bgroup->move( width()-bgroup->width(), 0 );
298}
299
300
301//
302// Create and display our widget.
303//
304
305#include "drawdemo.moc"
306
307int main( int argc, char **argv )
308{
309 QApplication app( argc, argv );
310 DrawView draw;
311 app.setMainWidget( &draw );
312 draw.setCaption("Qt Example - Drawdemo");
313 draw.show();
314 return app.exec();
315}
Note: See TracBrowser for help on using the repository browser.