File: MainWindow.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 (447 lines) | stat: -rw-r--r-- 12,358 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
package MainWindow;

use strict;
use warnings;
use QtCore4;
use QtGui4;
use QtCore4::isa qw( Qt::MainWindow );
use QtCore4::slots
    newFile => [],
    open => [],
    save => [],
    saveAs => [],
    about => [],
    documentWasModified => [];
use MainWindow;

my %WINDOWS;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

sub NEW
{
    my ( $class, $fileName ) = @_;
    $class->SUPER::NEW();
    this->init();
    if ( $fileName ) {
        this->loadFile( $fileName );
    }
    else {
        this->setCurrentFile(undef);
    }
}

sub closeEvent
{
    my ($event) = @_;

    delete $WINDOWS{this->Qt::base::getPointer()};

    if (this->maybeSave()) {
        this->writeSettings();
        $event->accept();
    } else {
        $event->ignore();
    }
}

sub newFile
{
    my $other = MainWindow();
    $other->move(this->x() + 40, this->y() + 40);
    $other->show();

    $WINDOWS{$other->Qt::base::getPointer()} = $other;
}

sub open
{
    my $fileName = Qt::FileDialog::getOpenFileName(this);
    if ($fileName) {
        my $existing = findMainWindow($fileName);
        if ($existing) {
            $existing->show();
            $existing->raise();
            $existing->activateWindow();
            return;
        }

        if (this->isUntitled && this->textEdit->document()->isEmpty()
                && !this->isWindowModified()) {
            this->loadFile($fileName);
        } else {
            my $other = MainWindow($fileName);
            if ($other->isUntitled) {
                $other->setParent( undef );
                $other->DESTROY();
                return;
            }
            $other->move(this->x() + 40, this->y() + 40);
            $other->show();
        }
    }
}

sub save
{
    if (this->isUntitled) {
        return this->saveAs();
    } else {
        return this->saveFile(this->curFile);
    }
}

sub saveAs
{
    my $fileName = Qt::FileDialog::getSaveFileName(this, this->tr('Save As'),
                                                    this->curFile);
    if (!$fileName) {
        return 0;
    }

    return this->saveFile($fileName);
}

sub about
{
   Qt::MessageBox::about(this, this->tr('About SDI'),
            this->tr('The <b>SDI</b> example demonstrates how to write single ' .
               'document interface applications using Qt.'));
}

sub documentWasModified
{
    this->setWindowModified(1);
}

sub init
{
    #this->setAttribute(Qt::WA_DeleteOnClose());

    this->{isUntitled} = 1;

    this->{textEdit} = Qt::TextEdit();
    this->setCentralWidget(this->textEdit);

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

    this->readSettings();

    this->connect(this->textEdit->document(), SIGNAL 'contentsChanged()',
            this, SLOT 'documentWasModified()');

    this->setUnifiedTitleAndToolBarOnMac(1);
}

sub createActions
{
    this->{newAct} = Qt::Action(Qt::Icon('images/new.png'), this->tr('&New'), this);
    this->newAct->setShortcuts(Qt::KeySequence::New());
    this->newAct->setStatusTip(this->tr('Create a file'));
    this->connect(this->newAct, SIGNAL 'triggered()', this, SLOT 'newFile()');

    this->{openAct} = Qt::Action(Qt::Icon('images/open.png'), this->tr('&Open...'), this);
    this->openAct->setShortcuts(Qt::KeySequence::Open());
    this->openAct->setStatusTip(this->tr('Open an existing file'));
    this->connect(this->openAct, SIGNAL 'triggered()', this, SLOT 'open()');

    this->{saveAct} = Qt::Action(Qt::Icon('images/save.png'), this->tr('&Save'), this);
    this->saveAct->setShortcuts(Qt::KeySequence::Save());
    this->saveAct->setStatusTip(this->tr('Save the document to disk'));
    this->connect(this->saveAct, SIGNAL 'triggered()', this, SLOT 'save()');

    this->{saveAsAct} = Qt::Action(this->tr('Save &As...'), this);
    this->saveAsAct->setShortcut(Qt::KeySequence('Ctrl+A'));
    this->saveAsAct->setStatusTip(this->tr('Save the document under a name'));
    this->connect(this->saveAsAct, SIGNAL 'triggered()', this, SLOT 'saveAs()');

    this->{closeAct} = Qt::Action(this->tr('&Close'), this);
    this->closeAct->setShortcut(Qt::KeySequence(this->tr('Ctrl+W')));
    this->closeAct->setStatusTip(this->tr('Close this window'));
    this->connect(this->closeAct, SIGNAL 'triggered()', this, SLOT 'close()');

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

    this->{cutAct} = Qt::Action(Qt::Icon('images/cut.png'), this->tr('Cu&t'), this);
    this->cutAct->setShortcuts(Qt::KeySequence::Cut());
    this->cutAct->setStatusTip(this->tr('Cut the current selection\'s contents to the ' .
                            'clipboard'));
    this->connect(this->cutAct, SIGNAL 'triggered()', textEdit, SLOT 'cut()');

    this->{copyAct} = Qt::Action(Qt::Icon('images/copy.png'), this->tr('&Copy'), this);
    this->copyAct->setShortcuts(Qt::KeySequence::Copy());
    this->copyAct->setStatusTip(this->tr('Copy the current selection\'s contents to the ' .
                             'clipboard'));
    this->connect(this->copyAct, SIGNAL 'triggered()', textEdit, SLOT 'copy()');

    this->{pasteAct} = Qt::Action(Qt::Icon('images/paste.png'), this->tr('&Paste'), this);
    this->pasteAct->setShortcuts(Qt::KeySequence::Paste());
    this->pasteAct->setStatusTip(this->tr('Paste the clipboard\'s contents into the current ' .
                              'selection'));
    this->connect(this->pasteAct, SIGNAL 'triggered()', textEdit, SLOT 'paste()');

    this->{aboutAct} = Qt::Action(this->tr('&About'), this);
    this->aboutAct->setStatusTip(this->tr('Show the application\'s About box'));
    this->connect(this->aboutAct, SIGNAL 'triggered()', this, SLOT 'about()');

    this->{aboutQtAct} = Qt::Action(this->tr('About &Qt'), this);
    this->aboutQtAct->setStatusTip(this->tr('Show the Qt4 library\'s About box'));
    this->connect(this->aboutQtAct, SIGNAL 'triggered()', qApp, SLOT 'aboutQt()');


    this->cutAct->setEnabled(0);
    this->copyAct->setEnabled(0);
    this->connect(textEdit, SIGNAL 'copyAvailable(bool)',
            this->cutAct, SLOT 'setEnabled(bool)');
    this->connect(textEdit, SIGNAL 'copyAvailable(bool)',
            this->copyAct, SLOT 'setEnabled(bool)');
}

# [implicit tr context]
sub createMenus
{
    this->{fileMenu} = this->menuBar()->addMenu(this->tr('&File'));
# [implicit tr context]
    this->fileMenu->addAction(this->newAct);
    this->fileMenu->addAction(this->openAct);
    this->fileMenu->addAction(this->saveAct);
    this->fileMenu->addAction(this->saveAsAct);
    this->fileMenu->addSeparator();
    this->fileMenu->addAction(this->closeAct);
    this->fileMenu->addAction(this->exitAct);

    this->{editMenu} = this->menuBar()->addMenu(this->tr('&Edit'));
    this->editMenu->addAction(this->cutAct);
    this->editMenu->addAction(this->copyAct);
    this->editMenu->addAction(this->pasteAct);

    this->menuBar()->addSeparator();

    this->{helpMenu} = this->menuBar()->addMenu(this->tr('&Help'));
    this->helpMenu->addAction(this->aboutAct);
    this->helpMenu->addAction(this->aboutQtAct);
}

sub createToolBars
{
# [0]
    this->{fileToolBar} = this->addToolBar(this->tr('File'));
    this->fileToolBar->addAction(this->newAct);
    this->fileToolBar->addAction(this->openAct);
# [0]
    this->fileToolBar->addAction(this->saveAct);

    this->{editToolBar} = this->addToolBar(this->tr('Edit'));
    this->editToolBar->addAction(this->cutAct);
    this->editToolBar->addAction(this->copyAct);
    this->editToolBar->addAction(this->pasteAct);
}

sub createStatusBar
{
    this->statusBar()->showMessage(this->tr('Ready'));
}

sub readSettings
{
    my $settings = Qt::Settings('Trolltech', 'SDI Example');
    my $pos = $settings->value('pos', Qt::Variant(Qt::Point(200, 200)))->toPoint();
    my $size = $settings->value('size', Qt::Variant(Qt::Size(400, 400)))->toSize();
    this->move($pos);
    this->resize($size);
}

sub writeSettings
{
    my $settings = Qt::Settings('Trolltech', 'SDI Example');
    $settings->setValue('pos', Qt::Variant(this->pos()));
    $settings->setValue('size', Qt::Variant(this->size()));
}

sub maybeSave
{
    if (this->textEdit->document()->isModified()) {
        my $ret = Qt::MessageBox::warning(this, this->tr('SDI'),
                     this->tr("The document has been modified.\n" .
                        'Do you want to save your changes?'),
                     Qt::MessageBox::Save() | Qt::MessageBox::Discard()
		     | Qt::MessageBox::Cancel());
        if ($ret == Qt::MessageBox::Save()) {
            return this->save();
        }
        elsif ($ret == Qt::MessageBox::Cancel()) {
            return 0;
        }
    }
    return 1;
}

sub loadFile
{
    my ($fileName) = @_;

    my $file = Qt::File($fileName);
    if (!$file->open(Qt::File::ReadOnly() | Qt::File::Text())) {
        Qt::MessageBox::warning(this, this->tr('SDI'),
                     sprintf this->tr('Cannot read file %s:\n%s.'),
                             $fileName,
                             $file->errorString());
        return;
    }

    my $in = Qt::TextStream($file);
    Qt::Application::setOverrideCursor(Qt::Cursor(Qt::WaitCursor()));
    this->textEdit->setPlainText($in->readAll());
    Qt::Application::restoreOverrideCursor();
    $file->close();

    this->setCurrentFile($fileName);
    this->statusBar()->showMessage(this->tr('File loaded'), 2000);
}

sub saveFile
{
    my ($fileName) = @_;
    my $file = Qt::File($fileName);
    if (!$file->open(Qt::File::WriteOnly() | Qt::File::Text())) {
        Qt::MessageBox::warning(this, this->tr('SDI'),
                     sprintf this->tr('Cannot write file %s:\n%s.'),
                             $fileName,
                             $file->errorString());
        return 0;
    }

    my $out = Qt::TextStream($file);
    Qt::Application::setOverrideCursor(Qt::Cursor(Qt::WaitCursor()));
    no warnings qw(void); # For bitshift warning
    $out << Qt::String(this->textEdit->toPlainText());
    use warnings;
    Qt::Application::restoreOverrideCursor();
    $file->close();

    this->setCurrentFile($fileName);
    this->statusBar()->showMessage(this->tr('File saved'), 2000);
    return 1;
}

my $sequenceNumber = 1;
sub setCurrentFile
{
    my ($fileName) = @_;

    this->{isUntitled} = $fileName ? 0 : 1;;
    if (this->isUntitled) {
        this->{curFile} = sprintf this->tr('document%d.txt'), $sequenceNumber++;
    } else {
        this->{curFile} = Qt::FileInfo($fileName)->canonicalFilePath();
    }

    this->textEdit->document()->setModified(0);
    this->setWindowModified(0);

    this->setWindowTitle(sprintf this->tr('%s[*] - %s'), this->strippedName(this->curFile),
                                       this->tr('SDI'));
}

sub strippedName
{
    my ($fullFileName) = @_;
    return Qt::FileInfo($fullFileName)->fileName();
}

sub findMainWindow
{
    my ($fileName) = @_;
    my $canonicalFilePath = Qt::FileInfo($fileName)->canonicalFilePath();

    foreach my $widget ( @{qApp->topLevelWidgets()} ) {
        next unless ref $widget;
        my $mainWin = $widget->qobject_cast( 'Qt::MainWindow' );
        if ($mainWin && $mainWin->curFile eq $canonicalFilePath) {
            return $mainWin;
        }
    }
    return 0;
}

1;