1 | /****************************************************************************
|
---|
2 | ** $Id: connect.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 <qwidget.h>
|
---|
12 | #include <qpainter.h>
|
---|
13 | #include <qapplication.h>
|
---|
14 | #include <stdlib.h>
|
---|
15 |
|
---|
16 |
|
---|
17 | const int MAXPOINTS = 2000; // maximum number of points
|
---|
18 | const int MAXCOLORS = 40;
|
---|
19 |
|
---|
20 |
|
---|
21 | //
|
---|
22 | // ConnectWidget - draws connected lines
|
---|
23 | //
|
---|
24 |
|
---|
25 | class ConnectWidget : public QWidget
|
---|
26 | {
|
---|
27 | public:
|
---|
28 | ConnectWidget( QWidget *parent=0, const char *name=0 );
|
---|
29 | ~ConnectWidget();
|
---|
30 | protected:
|
---|
31 | void paintEvent( QPaintEvent * );
|
---|
32 | void mousePressEvent( QMouseEvent *);
|
---|
33 | void mouseReleaseEvent( QMouseEvent *);
|
---|
34 | void mouseMoveEvent( QMouseEvent *);
|
---|
35 | private:
|
---|
36 | QPoint *points; // point array
|
---|
37 | QColor *colors; // color array
|
---|
38 | int count; // count = number of points
|
---|
39 | bool down; // TRUE if mouse down
|
---|
40 | };
|
---|
41 |
|
---|
42 |
|
---|
43 | //
|
---|
44 | // Constructs a ConnectWidget.
|
---|
45 | //
|
---|
46 |
|
---|
47 | ConnectWidget::ConnectWidget( QWidget *parent, const char *name )
|
---|
48 | : QWidget( parent, name, WStaticContents )
|
---|
49 | {
|
---|
50 | setBackgroundColor( white ); // white background
|
---|
51 | count = 0;
|
---|
52 | down = FALSE;
|
---|
53 | points = new QPoint[MAXPOINTS];
|
---|
54 | colors = new QColor[MAXCOLORS];
|
---|
55 | for ( int i=0; i<MAXCOLORS; i++ ) // init color array
|
---|
56 | colors[i] = QColor( rand()&255, rand()&255, rand()&255 );
|
---|
57 | }
|
---|
58 |
|
---|
59 | ConnectWidget::~ConnectWidget()
|
---|
60 | {
|
---|
61 | delete[] points; // cleanup
|
---|
62 | delete[] colors;
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | //
|
---|
67 | // Handles paint events for the connect widget.
|
---|
68 | //
|
---|
69 |
|
---|
70 | void ConnectWidget::paintEvent( QPaintEvent * )
|
---|
71 | {
|
---|
72 | QPainter paint( this );
|
---|
73 | for ( int i=0; i<count-1; i++ ) { // connect all points
|
---|
74 | for ( int j=i+1; j<count; j++ ) {
|
---|
75 | paint.setPen( colors[rand()%MAXCOLORS] ); // set random pen color
|
---|
76 | paint.drawLine( points[i], points[j] ); // draw line
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | //
|
---|
83 | // Handles mouse press events for the connect widget.
|
---|
84 | //
|
---|
85 |
|
---|
86 | void ConnectWidget::mousePressEvent( QMouseEvent * )
|
---|
87 | {
|
---|
88 | down = TRUE;
|
---|
89 | count = 0; // start recording points
|
---|
90 | erase(); // erase widget contents
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | //
|
---|
95 | // Handles mouse release events for the connect widget.
|
---|
96 | //
|
---|
97 |
|
---|
98 | void ConnectWidget::mouseReleaseEvent( QMouseEvent * )
|
---|
99 | {
|
---|
100 | down = FALSE; // done recording points
|
---|
101 | update(); // draw the lines
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | //
|
---|
106 | // Handles mouse move events for the connect widget.
|
---|
107 | //
|
---|
108 |
|
---|
109 | void ConnectWidget::mouseMoveEvent( QMouseEvent *e )
|
---|
110 | {
|
---|
111 | if ( down && count < MAXPOINTS ) {
|
---|
112 | QPainter paint( this );
|
---|
113 | points[count++] = e->pos(); // add point
|
---|
114 | paint.drawPoint( e->pos() ); // plot point
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | //
|
---|
120 | // Create and display a ConnectWidget.
|
---|
121 | //
|
---|
122 |
|
---|
123 | int main( int argc, char **argv )
|
---|
124 | {
|
---|
125 | QApplication a( argc, argv );
|
---|
126 | ConnectWidget connect;
|
---|
127 | #ifndef QT_NO_WIDGET_TOPEXTRA // for Qt/Embedded minimal build
|
---|
128 | connect.setCaption( "Qt Example - Draw lines");
|
---|
129 | #endif
|
---|
130 | a.setMainWidget( &connect );
|
---|
131 | connect.show();
|
---|
132 | return a.exec();
|
---|
133 | }
|
---|