File: MainWindow.pm

package info (click to toggle)
qt4-perl 4.5~~svn1145508-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 5,144 kB
  • ctags: 5,947
  • sloc: perl: 29,224; cpp: 18,849; xml: 98; makefile: 91; sh: 4
file content (280 lines) | stat: -rw-r--r-- 7,740 bytes parent folder | download
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
package MainWindow;

use strict;
use warnings;
use blib;

use QtCore4;
use QtGui4;
# [0]
use QtCore4::isa qw( Qt::MainWindow );
use QtCore4::slots
    open => [],
    save => [],
    penColor => [],
    penWidth => [],
    about => [];
use ScribbleArea;

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

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

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

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

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

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

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

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

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

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

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

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

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

sub aboutQtAct() {
    return this->{aboutQtAct};
}
# [0]

# [0]
sub NEW {
    my ( $class, $parent ) = @_;
    $class->SUPER::NEW( $parent );

    this->{scribbleArea} = ScribbleArea();
    this->setCentralWidget(this->scribbleArea);

    this->createActions();
    this->createMenus();

    this->setWindowTitle(this->tr('Scribble'));
    this->resize(500, 500);
}
# [0]

# [1]
sub closeEvent {
# [1] //! [2]
    my ($event) = @_;
    if (this->maybeSave()) {
        $event->accept();
    } else {
        $event->ignore();
    }
}
# [2]

# [3]
sub open {
# [3] //! [4]
    if (maybeSave()) {
        my $fileName = Qt::FileDialog::getOpenFileName(this,
                                   this->tr('Open File'), Qt::Dir::currentPath());
        if ($fileName) {
            this->scribbleArea->openImage($fileName);
        }
    }
}
# [4]

# [5]
sub save {
# [5] //! [6]
    my $action = this->sender();
    my $fileFormat = $action->data()->toString();
    this->saveFile($fileFormat);
}
# [6]

# [7]
sub penColor {
# [7] //! [8]
    my $newColor = Qt::ColorDialog::getColor(this->scribbleArea->penColor());
    if ($newColor->isValid()) {
        this->scribbleArea->setPenColor($newColor);
    }
}
# [8]

# [9]
sub penWidth {
# [9] //! [10]
    my $ok = 0;
    my $newWidth = Qt::InputDialog::getInteger(this, this->tr('Scribble'),
                                            this->tr('Select pen width:'),
                                            this->scribbleArea->penWidth(),
                                            1, 50, 1, $ok);
    if ($ok) {
        this->scribbleArea->setPenWidth($newWidth);
    }
}
# [10]

# [11]
sub about {
# [11] //! [12]
    Qt::MessageBox::about(this, this->tr('About Scribble'),
            this->tr('<p>The <b>Scribble</b> example shows how to use Qt::MainWindow as the ' .
               'base widget for an application, and how to reimplement some of ' .
               'Qt::Widget\'s event handlers to receive the events generated for ' .
               'the application\'s widgets:</p><p> We reimplement the mouse event ' .
               'handlers to facilitate drawing, the paint event handler to ' .
               'update the application and the resize event handler to optimize ' .
               'the application\'s appearance. In addition we reimplement the ' .
               'close event handler to intercept the close events before ' .
               'terminating the application.</p><p> The example also demonstrates ' .
               'how to use Qt::Painter to draw an image in real time, as well as ' .
               'to repaint widgets.</p>'));
}
# [12]

# [13]
sub createActions {
# [13] //! [14]
    this->{openAct} = Qt::Action(this->tr('&Open...'), this);
    this->openAct->setShortcut(Qt::KeySequence(this->tr('Ctrl+O')));
    this->connect(this->openAct, SIGNAL 'triggered()', this, SLOT 'open()');

    foreach my $format ( @{Qt::ImageWriter::supportedImageFormats()} ) {
        my $text = sprintf this->tr('%s...'), uc $format;

        my $action = Qt::Action($text, this);
        $action->setData(Qt::Variant(Qt::String($format)));
        this->connect($action, SIGNAL 'triggered()', this, SLOT 'save()');
        push @{this->{saveAsActs}}, $action;
    }

    this->{printAct} = Qt::Action(this->tr('&Print...'), this);
    this->connect(this->printAct, SIGNAL 'triggered()', this->scribbleArea, SLOT 'print()');

    this->{exitAct} = Qt::Action(this->tr('E&xit'), this);
    this->exitAct->setShortcut(Qt::KeySequence(this->tr('Ctrl+Q')));
    this->connect(this->exitAct, SIGNAL 'triggered()', this, SLOT 'close()');

    this->{penColorAct} = Qt::Action(this->tr('&Pen Color...'), this);
    this->connect(this->penColorAct, SIGNAL 'triggered()', this, SLOT 'penColor()');

    this->{penWidthAct} = Qt::Action(this->tr('Pen &Width...'), this);
    this->connect(this->penWidthAct, SIGNAL 'triggered()', this, SLOT 'penWidth()');

    this->{clearScreenAct} = Qt::Action(this->tr('&Clear Screen'), this);
    this->clearScreenAct->setShortcut(Qt::KeySequence(this->tr('Ctrl+L')));
    this->connect(this->clearScreenAct, SIGNAL 'triggered()',
            this->scribbleArea, SLOT 'clearImage()');

    this->{aboutAct} = Qt::Action(this->tr('&About'), this);
    this->connect(this->aboutAct, SIGNAL 'triggered()', this, SLOT 'about()');

    this->{aboutQtAct} = Qt::Action(this->tr('About &Qt'), this);
    this->connect(this->aboutQtAct, SIGNAL 'triggered()', qApp, SLOT 'aboutQt()');
}
# [14]

# [15]
sub createMenus {
# [15] //! [16]
    this->{saveAsMenu} = Qt::Menu(this->tr('&Save As'), this);
    foreach my $action ( @{this->{saveAsActs}} ) {
        this->saveAsMenu->addAction($action);
    }

    this->{fileMenu} = Qt::Menu(this->tr('&File'), this);
    this->fileMenu->addAction(this->openAct);
    this->fileMenu->addMenu(this->saveAsMenu);
    this->fileMenu->addAction(this->printAct);
    this->fileMenu->addSeparator();
    this->fileMenu->addAction(this->exitAct);

    this->{optionMenu} = Qt::Menu(this->tr('&Options'), this);
    this->optionMenu->addAction(this->penColorAct);
    this->optionMenu->addAction(this->penWidthAct);
    this->optionMenu->addSeparator();
    this->optionMenu->addAction(this->clearScreenAct);

    this->{helpMenu} = Qt::Menu(this->tr('&Help'), this);
    this->helpMenu->addAction(this->aboutAct);
    this->helpMenu->addAction(this->aboutQtAct);

    this->menuBar()->addMenu(this->fileMenu);
    this->menuBar()->addMenu(this->optionMenu);
    this->menuBar()->addMenu(this->helpMenu);
}
# [16]

# [17]
sub maybeSave {
# [17] //! [18]
    if (this->scribbleArea->isModified()) {
        my $ret = Qt::MessageBox::warning(this, this->tr('Scribble'),
                          this->tr("The image has been modified.\n" .
                             'Do you want to save your changes?'),
                          CAST Qt::MessageBox::Save() | Qt::MessageBox::Discard()
			  | Qt::MessageBox::Cancel(), 'QMessageBox::StandardButtons');
        if ($ret == Qt::MessageBox::Save()) {
            return this->saveFile('png');
        } elsif ($ret == Qt::MessageBox::Cancel()) {
            return 0;
        }
    }
    return 1;
}
# [18]

# [19]
sub saveFile {
# [19] //! [20]
    my ($fileFormat) = @_;
    my $initialPath = Qt::Dir::currentPath() . '/untitled.' . $fileFormat;

    my $fileName = Qt::FileDialog::getSaveFileName(this, this->tr('Save As'),
                               $initialPath,
                               sprintf( this->tr('%s Files (*.%s);;All Files (*)'),
                                   uc $fileFormat,
                                   $fileFormat )
                               );
    if (!$fileName) {
        return 0;
    } else {
        return this->scribbleArea->saveImage($fileName, $fileFormat);
    }
}
# [20]

1;