File: TabletCanvas.pm

package info (click to toggle)
qt4-perl 4.8.4-1.2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,636 kB
  • ctags: 8,100
  • sloc: perl: 42,963; cpp: 28,039; makefile: 160; xml: 98; sh: 4
file content (323 lines) | stat: -rw-r--r-- 7,489 bytes parent folder | download | duplicates (3)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package TabletCanvas;

use strict;
use warnings;

use QtCore4;
use QtGui4;
use QtCore4::isa qw( Qt::Widget );

use Exporter;
use base qw( Exporter );
our @EXPORT_OK = qw( AlphaPressure AlphaTilt NoAlpha SaturationVTilt 
    SaturationHTilt SaturationPressure NoSaturation LineWidthPressure
    LineWidthTilt NoLineWidth );

use constant {
    AlphaPressure => 1,
    AlphaTilt => 2,
    NoAlpha => 3,
};

use constant {
    SaturationVTilt => 1,
    SaturationHTilt => 2,
    SaturationPressure => 3,
    NoSaturation => 4,
};

use constant {
    LineWidthPressure => 1,
    LineWidthTilt => 2,
    NoLineWidth => 3,
};

sub setAlphaChannelType {
    this->{alphaChannelType} = shift;
}

sub setColorSaturationType {
    this->{colorSaturationType} = shift;
}

sub setLineWidthType {
    this->{lineWidthType} = shift;
}

sub setColor {
    this->{myColor} = Qt::Color(shift);
}

sub color {
    return this->myColor;
}

sub setTabletDevice {
    this->{myTabletDevice} = shift;
}

sub maximum {
    my ( $a, $b ) = @_;
    return $a > $b ? $a : $b;
}

sub alphaChannelType() {
    return this->{alphaChannelType};
}

sub colorSaturationType() {
    return this->{colorSaturationType};
}

sub lineWidthType() {
    return this->{lineWidthType};
}

sub pointerType() {
    return this->{pointerType};
}

sub myTabletDevice() {
    return this->{myTabletDevice};
}

sub myColor() {
    return this->{myColor};
}

sub image() {
    return this->{image};
}

sub myBrush() {
    return this->{myBrush};
}

sub myPen() {
    return this->{myPen};
}

sub deviceDown() {
    return this->{deviceDown};
}

sub polyLine() {
    return this->{polyLine};
}

# [0]
sub NEW {
    my ( $class ) = @_;
    $class->SUPER::NEW();
    this->resize(500, 500);
    this->{myBrush} = Qt::Brush();
    this->{myPen} = Qt::Pen();
    this->initImage();
    this->setAutoFillBackground(1);
    this->{deviceDown} = 0;
    this->setColor( Qt::red() );
    this->{myTabletDevice} = Qt::TabletEvent::Stylus();
    this->{alphaChannelType} = NoAlpha;
    this->{colorSaturationType} = NoSaturation;
    this->{lineWidthType} = LineWidthPressure;
}

sub initImage {
    my $newImage = Qt::Image(this->width(), this->height(), Qt::Image::Format_ARGB32());
    my $painter = Qt::Painter($newImage);
    $painter->fillRect(0, 0, $newImage->width(), $newImage->height(), Qt::Brush(Qt::white()));
    if (this->image && !this->image->isNull()) {
        $painter->drawImage(0, 0, this->image);
    }
    $painter->end();
    this->{image} = $newImage;
}
# [0]

# [1]
sub saveImage {
    my ($file) = @_;
    return this->image->save($file);
}
# [1]

# [2]
sub loadImage {
    my ($file) = @_;
    my $success = this->image->load($file);

    if ($success) {
        this->update();
        return 1;
    }
    return 0;
}
# [2]

# [3]
sub tabletEvent {
    my ($event) = @_;

    if ( $event->type() == Qt::Event::TabletPress() ) {
        if (!this->deviceDown) {
            this->{deviceDown} = 1;
        }
    }
    elsif ( $event->type() == Qt::Event::TabletRelease() ) {
        if (this->deviceDown) {
            this->{deviceDown} = 0;
        }
    }
    elsif ( $event->type() == Qt::Event::TabletMove() ) {
        unshift @{this->polyLine}, $event->pos();
        delete this->polyLine->[3];

        if (this->deviceDown) {
            this->updateBrush($event);
            my $painter = Qt::Painter(this->image);
            this->paintImage($painter, $event);
            $painter->end();
        }
    }
    this->update();
}
# [3]

# [4]
sub paintEvent {
    my $painter = Qt::Painter(this);
    $painter->drawImage(Qt::Point(0, 0), this->image);
    $painter->end();
}
# [4]

# [5]
sub paintImage {
    my ($painter, $event) = @_;
    my $brushAdjust = Qt::Point(10, 10);

    my $myTabletDevice = this->myTabletDevice;
    if ( $myTabletDevice == Qt::TabletEvent::Stylus() ) {
        $painter->setBrush(this->myBrush);
        $painter->setPen(this->myPen);
        $painter->drawLine(this->polyLine->[1], $event->pos());
    }
    elsif ( $myTabletDevice == Qt::TabletEvent::Airbrush() ) {
        this->myBrush->setColor(this->myColor);
        this->myBrush->setStyle(this->brushPattern($event->pressure()));
        $painter->setPen(Qt::NoPen());
        $painter->setBrush(this->myBrush);

        foreach my $i (0..2) {
            $painter->drawEllipse(Qt::Rect(this->polyLine->[$i] - $brushAdjust,
                                this->polyLine->[$i] + $brushAdjust));
        }
    }
    elsif ( $myTabletDevice == Qt::TabletEvent::Puck() ||
         $myTabletDevice == Qt::TabletEvent::FourDMouse() ||
         $myTabletDevice == Qt::TabletEvent::RotationStylus() ) {
        warn("This input device is not supported by the example.");
    }
    else {
        warn("Unknown tablet device.");
    }
}
# [5]

# [6]
sub brushPattern {
    my ($value) = @_;
    my $pattern = int(($value) * 100.0) % 7;

    if ( $pattern == 0 ) {
        return Qt::SolidPattern();
    }
    elsif ( $pattern == 1 ) {
        return Qt::Dense1Pattern();
    }
    elsif ( $pattern == 2 ) {
        return Qt::Dense2Pattern();
    }
    elsif ( $pattern == 3 ) {
        return Qt::Dense3Pattern();
    }
    elsif ( $pattern == 4 ) {
        return Qt::Dense4Pattern();
    }
    elsif ( $pattern == 5 ) {
        return Qt::Dense5Pattern();
    }
    elsif ( $pattern == 6 ) {
        return Qt::Dense6Pattern();
    }
    else {
        return Qt::Dense7Pattern();
    }
}
# [6]

# [7]
sub updateBrush {
    my ($event) = @_;
    my ( $hue, $saturation, $value, $alpha );
    this->myColor->getHsv($hue, $saturation, $value, $alpha);

    my $vValue = int((($event->yTilt() + 60.0) / 120.0) * 255);
    my $hValue = int((($event->xTilt() + 60.0) / 120.0) * 255);
# [7] //! [8]

    my $alphaChannelType = this->alphaChannelType;
    if ( $alphaChannelType == AlphaPressure ) {
        this->myColor->setAlpha(int($event->pressure() * 255.0));
    }
    elsif ( $alphaChannelType == AlphaTilt ) {
        this->myColor->setAlpha(maximum(abs($vValue - 127), abs($hValue - 127)));
    }
    else {
        this->myColor->setAlpha(255);
    }

# [8] //! [9]
    my $colorSaturationType = this->colorSaturationType;
    if ( $colorSaturationType == SaturationVTilt ) {
        this->myColor->setHsv($hue, $vValue, $value, $alpha);
    }
    elsif ( $colorSaturationType == SaturationHTilt ) {
        this->myColor->setHsv($hue, $hValue, $value, $alpha);
    }
    elsif ( $colorSaturationType == SaturationPressure ) {
        this->myColor->setHsv($hue, int($event->pressure() * 255.0), $value, $alpha);
    }

# [9] //! [10]
    my $lineWidthType = this->lineWidthType;
    if ( $lineWidthType == LineWidthPressure ) {
        this->myPen->setWidthF($event->pressure() * 10 + 1);
    }
    elsif ( $lineWidthType == LineWidthTilt ) {
        this->myPen->setWidthF(maximum(abs($vValue - 127), abs($hValue - 127)) / 12);
    }
    else {
        this->myPen->setWidthF(1);
    }

# [10] //! [11]
    if ($event->pointerType() == Qt::TabletEvent::Eraser()) {
        this->myBrush->setColor(Qt::white());
        this->myPen->setColor(Qt::white());
        this->myPen->setWidthF($event->pressure() * 10 + 1);
    } else {
        this->myBrush->setColor(this->myColor);
        this->myPen->setColor(this->myColor);
    }
}
# [11]

sub resizeEvent {
    my ($event) = @_;
    this->initImage();
    this->{polyLine} = [];
    this->polyLine->[0] = this->polyLine->[1] = this->polyLine->[2] = Qt::Point();
}

1;