1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
/****************************************************************************
**
** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
**
** This file is part of an example program for Qt. This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/
#include "canvas.h"
#include <qapplication.h>
#include <qpainter.h>
#include <qevent.h>
#include <qrect.h>
const bool no_writing = FALSE;
Canvas::Canvas( QWidget *parent, const char *name, WFlags fl )
: QWidget( parent, name, WStaticContents | fl ),
pen( Qt::red, 3 ), polyline(3),
mousePressed( FALSE ), oldPressure( 0 ), saveColor( red ),
buffer( width(), height() )
{
if ((qApp->argc() > 0) && !buffer.load(qApp->argv()[1]))
buffer.fill( colorGroup().base() );
setBackgroundMode( QWidget::PaletteBase );
#ifndef QT_NO_CURSOR
setCursor( Qt::crossCursor );
#endif
}
void Canvas::save( const QString &filename, const QString &format )
{
if ( !no_writing )
buffer.save( filename, format.upper() );
}
void Canvas::clearScreen()
{
buffer.fill( colorGroup().base() );
repaint( FALSE );
}
void Canvas::mousePressEvent( QMouseEvent *e )
{
mousePressed = TRUE;
polyline[2] = polyline[1] = polyline[0] = e->pos();
}
void Canvas::mouseReleaseEvent( QMouseEvent * )
{
mousePressed = FALSE;
}
void Canvas::mouseMoveEvent( QMouseEvent *e )
{
if ( mousePressed ) {
QPainter painter;
painter.begin( &buffer );
painter.setPen( pen );
polyline[2] = polyline[1];
polyline[1] = polyline[0];
polyline[0] = e->pos();
painter.drawPolyline( polyline );
painter.end();
QRect r = polyline.boundingRect();
r = r.normalize();
r.setLeft( r.left() - penWidth() );
r.setTop( r.top() - penWidth() );
r.setRight( r.right() + penWidth() );
r.setBottom( r.bottom() + penWidth() );
bitBlt( this, r.x(), r.y(), &buffer, r.x(), r.y(), r.width(), r.height() );
}
}
void Canvas::tabletEvent( QTabletEvent *e )
{
e->accept();
// change the width based on range of pressure
if ( e->device() == QTabletEvent::Stylus ) {
if ( e->pressure() >= 0 && e->pressure() <= 32 )
pen.setColor( saveColor.light(175) );
else if ( e->pressure() > 32 && e->pressure() <= 64 )
pen.setColor( saveColor.light(150) );
else if ( e->pressure() > 64 && e->pressure() <= 96 )
pen.setColor( saveColor.light(125) );
else if ( e->pressure() > 96 && e->pressure() <= 128 )
pen.setColor( saveColor );
else if ( e->pressure() > 128 && e->pressure() <= 160 )
pen.setColor( saveColor.dark(150) );
else if ( e->pressure() > 160 && e->pressure() <= 192 )
pen.setColor( saveColor.dark(200) );
else if ( e->pressure() > 192 && e->pressure() <= 224 )
pen.setColor( saveColor.dark(250) );
else // pressure > 224
pen.setColor( saveColor.dark(300) );
} else if ( e->device() == QTabletEvent::Eraser
&& pen.color() != backgroundColor() ) {
pen.setColor( backgroundColor() );
}
int xt = e->xTilt();
int yt = e->yTilt();
if ( ( xt > -15 && xt < 15 ) && ( yt > -15 && yt < 15 ) )
pen.setWidth( 3 );
else if ( ((xt < -15 && xt > -30) || (xt > 15 && xt < 30)) &&
((yt < -15 && yt > -30) || (yt > 15 && yt < 30 )) )
pen.setWidth( 6 );
else if ( ((xt < -30 && xt > -45) || (xt > 30 && xt < 45)) &&
((yt < -30 && yt > -45) || (yt > 30 && yt < 45)) )
pen.setWidth( 9 );
else if ( (xt < -45 || xt > 45 ) && ( yt < -45 || yt > 45 ) )
pen.setWidth( 12 );
switch ( e->type() ) {
case QEvent::TabletPress:
mousePressed = TRUE;
polyline[2] = polyline[1] = polyline[0] = e->pos();
break;
case QEvent::TabletRelease:
mousePressed = FALSE;
break;
case QEvent::TabletMove:
if ( mousePressed ) {
QPainter painter;
painter.begin( &buffer );
painter.setPen( pen );
polyline[2] = polyline[1];
polyline[1] = polyline[0];
polyline[0] = e->pos();
painter.drawPolyline( polyline );
painter.end();
QRect r = polyline.boundingRect();
r = r.normalize();
r.setLeft( r.left() - penWidth() );
r.setTop( r.top() - penWidth() );
r.setRight( r.right() + penWidth() );
r.setBottom( r.bottom() + penWidth() );
bitBlt( this, r.x(), r.y(), &buffer, r.x(), r.y(), r.width(),
r.height() );
}
break;
default:
break;
}
}
void Canvas::resizeEvent( QResizeEvent *e )
{
QWidget::resizeEvent( e );
int w = width() > buffer.width() ?
width() : buffer.width();
int h = height() > buffer.height() ?
height() : buffer.height();
QPixmap tmp( buffer );
buffer.resize( w, h );
buffer.fill( colorGroup().base() );
bitBlt( &buffer, 0, 0, &tmp, 0, 0, tmp.width(), tmp.height() );
}
void Canvas::paintEvent( QPaintEvent *e )
{
QWidget::paintEvent( e );
QMemArray<QRect> rects = e->region().rects();
for ( uint i = 0; i < rects.count(); i++ ) {
QRect r = rects[(int)i];
bitBlt( this, r.x(), r.y(), &buffer, r.x(), r.y(), r.width(), r.height() );
}
}
|