File: MainWindow.pm

package info (click to toggle)
qt4-perl 4.8.4-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 8,464 kB
  • sloc: perl: 42,937; cpp: 28,039; makefile: 153; xml: 98; sh: 4
file content (386 lines) | stat: -rw-r--r-- 12,686 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
package MainWindow;

use strict;
use warnings;

use QtCore4;
use QtGui4;
use QtCore4::isa qw( Qt::MainWindow );
use QtCore4::slots
    newFile => [''],
    openSlot => [''],
    save => [''],
    saveAs => [''],
    cut => [''],
    copy => [''],
    paste => [''],
    about => [''],
    updateMenus => [''],
    updateWindowMenu => [''],
    createMdiChild => [''],
    switchLayoutDirection => [''],
    setActiveSubWindow => ['QWidget*'];

use MdiChild;

sub NEW {
    shift->SUPER::NEW(@_);
    my $mdiArea = Qt::MdiArea();
    this->{mdiArea} = $mdiArea;
    this->setCentralWidget($mdiArea);
    this->connect($mdiArea, SIGNAL 'subWindowActivated(QMdiSubWindow *)',
                  this, SLOT 'updateMenus()');
    my $windowMapper = Qt::SignalMapper(this);
    this->{windowMapper} = $windowMapper;
    this->connect($windowMapper, SIGNAL 'mapped(QWidget*)',
                  this, SLOT 'setActiveSubWindow(QWidget*)');

    createActions();
    createMenus();
    createToolBars();
    createStatusBar();
    updateMenus();

    readSettings();

    this->setWindowTitle("MDI");
}

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

    this->{mdiArea}->closeAllSubWindows();
    if (activeMdiChild()) {
        $event->ignore();
    } else {
        writeSettings();
        $event->accept();
    }
}

sub newFile {
    my $child = createMdiChild();
    $child->newFile();
    $child->show();
}

sub openSlot {
    my $fileName = Qt::FileDialog::getOpenFileName(this);
    if ($fileName) {
        my $existing = findMdiChild($fileName);
        if ($existing) {
            this->{mdiArea}->setActiveSubWindow($existing);
            return;
        }

        my $child = createMdiChild();
        if ($child->loadFile($fileName)) {
            this->statusBar()->showMessage("File loaded", 2000);
            $child->show();
        } else {
            $child->close();
        }
        push @{this->{children}}, $child;
    }
}

sub save {
    if (activeMdiChild() && activeMdiChild()->save()) {
        this->statusBar()->showMessage("File saved", 2000);
    }
}

sub saveAs {
    if (activeMdiChild() && activeMdiChild()->saveAs()) {
        this->statusBar()->showMessage("File saved", 2000);
    }
}

sub cut {
    if (activeMdiChild()) {
        activeMdiChild()->cut();
    }
}

sub copy {
    if (activeMdiChild()) {
        activeMdiChild()->copy();
    }
}

sub paste {
    if (activeMdiChild()) {
        activeMdiChild()->paste();
    }
}

sub about {
   Qt::MessageBox::about(this, "About MDI",
            "The <b>MDI</b> example demonstrates how to write multiple " .
            "document interface applications using Qt.");
}

sub updateMenus {
    my $hasMdiChild = (activeMdiChild() != 0);
    this->{saveAct}->setEnabled($hasMdiChild);
    this->{saveAsAct}->setEnabled($hasMdiChild);
    this->{pasteAct}->setEnabled($hasMdiChild);
    this->{closeAct}->setEnabled($hasMdiChild);
    this->{closeAllAct}->setEnabled($hasMdiChild);
    this->{tileAct}->setEnabled($hasMdiChild);
    this->{cascadeAct}->setEnabled($hasMdiChild);
    this->{nextAct}->setEnabled($hasMdiChild);
    this->{previousAct}->setEnabled($hasMdiChild);
    this->{separatorAct}->setVisible($hasMdiChild);

    my $hasSelection = (this->activeMdiChild() &&
                        this->activeMdiChild()->textCursor()->hasSelection());
    this->{cutAct}->setEnabled($hasSelection);
    this->{copyAct}->setEnabled($hasSelection);
}

sub updateWindowMenu {
    this->{windowMenu}->clear();
    this->{windowMenu}->addAction(this->{closeAct});
    this->{windowMenu}->addAction(this->{closeAllAct});
    this->{windowMenu}->addSeparator();
    this->{windowMenu}->addAction(this->{tileAct});
    this->{windowMenu}->addAction(this->{cascadeAct});
    this->{windowMenu}->addSeparator();
    this->{windowMenu}->addAction(this->{nextAct});
    this->{windowMenu}->addAction(this->{previousAct});
    this->{windowMenu}->addAction(this->{separatorAct});

    my @windows = @{this->{mdiArea}->subWindowList()};
    this->{separatorAct}->setVisible(scalar @windows);

    foreach my $i ( 0..$#windows ) {
        my $child = $windows[$i]->widget();

        my $text;
        if ($i < 9) {
            $text = sprintf "&%d %s", $i + 1,
                               $child->userFriendlyCurrentFile();
        } else {
            $text = sprintf "%s %s", $i + 1,
                              $child->userFriendlyCurrentFile();
        }
        my $action  = this->{windowMenu}->addAction($text);
        $action->setCheckable(1);
        $action->setChecked($child == activeMdiChild());
        this->connect($action, SIGNAL 'triggered()', this->{windowMapper}, SLOT 'map()');
        this->{windowMapper}->setMapping($action, $windows[$i]);
    }
}

sub createMdiChild {
    my $child = MdiChild();
    this->{mdiArea}->addSubWindow($child);

    this->connect($child, SIGNAL 'copyAvailable(bool)',
                  this->{cutAct}, SLOT 'setEnabled(bool)');
    this->connect($child, SIGNAL 'copyAvailable(bool)',
                  this->{copyAct}, SLOT 'setEnabled(bool)');

    return $child;
}

sub createActions {
    my $newAct = Qt::Action(Qt::Icon("images/new.png"), "&New", this);
    this->{newAct} = $newAct;
    $newAct->setShortcut(Qt::KeySequence("Ctrl+N"));
    $newAct->setStatusTip("Create a new file");
    this->connect($newAct, SIGNAL 'triggered()', this, SLOT 'newFile()');

    my $openAct = Qt::Action(Qt::Icon("images/open.png"), "&Open...", this);
    this->{openAct} = $openAct;
    $openAct->setShortcut(Qt::KeySequence("Ctrl+O"));
    $openAct->setStatusTip("Open an existing file");
    this->connect($openAct, SIGNAL 'triggered()', this, SLOT 'openSlot()');

    my $saveAct = Qt::Action(Qt::Icon("images/save.png"), "&Save", this);
    this->{saveAct} = $saveAct;
    $saveAct->setShortcut(Qt::KeySequence("Ctrl+S"));
    $saveAct->setStatusTip("Save the document to disk");
    this->connect($saveAct, SIGNAL 'triggered()', this, SLOT 'save()');

    my $saveAsAct = Qt::Action("Save &As...", this);
    this->{saveAsAct} = $saveAsAct;
    $saveAsAct->setStatusTip("Save the document under a new name");
    this->connect($saveAsAct, SIGNAL 'triggered()', this, SLOT 'saveAs()');

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

    my $cutAct = Qt::Action(Qt::Icon("images/cut.png"), "Cu&t", this);
    this->{cutAct} = $cutAct;
    $cutAct->setShortcut(Qt::KeySequence("Ctrl+X"));
    $cutAct->setStatusTip("Cut the current selection's contents to the " .
                            "clipboard");
    this->connect($cutAct, SIGNAL 'triggered()', this, SLOT 'cut()');

    my $copyAct = Qt::Action(Qt::Icon("images/copy.png"), "&Copy", this);
    this->{copyAct} = $copyAct;
    $copyAct->setShortcut(Qt::KeySequence("Ctrl+C"));
    $copyAct->setStatusTip("Copy the current selection's contents to the " .
                             "clipboard");
    this->connect($copyAct, SIGNAL 'triggered()', this, SLOT 'copy()');

    my $pasteAct = Qt::Action(Qt::Icon("images/paste.png"), "&Paste", this);
    this->{pasteAct} = $pasteAct;
    $pasteAct->setShortcut(Qt::KeySequence("Ctrl+V"));
    $pasteAct->setStatusTip("Paste the clipboard's contents into the current " .
                              "selection");
    this->connect($pasteAct, SIGNAL 'triggered()', this, SLOT 'paste()');

    my $closeAct = Qt::Action("Cl&ose", this);
    this->{closeAct} = $closeAct;
    $closeAct->setShortcut(Qt::KeySequence("Ctrl+F4"));
    $closeAct->setStatusTip("Close the active window");
    this->connect($closeAct, SIGNAL 'triggered()',
            this->{mdiArea}, SLOT 'closeActiveSubWindow()');

    my $closeAllAct = Qt::Action("Close &All", this);
    this->{closeAllAct} = $closeAllAct;
    $closeAllAct->setStatusTip("Close all the windows");
    this->connect($closeAllAct, SIGNAL 'triggered()',
            this->{mdiArea}, SLOT 'closeAllSubWindows()');

    my $tileAct = Qt::Action("&Tile", this);
    this->{tileAct} = $tileAct;
    $tileAct->setStatusTip("Tile the windows");
    this->connect($tileAct, SIGNAL 'triggered()', this->{mdiArea}, SLOT 'tileSubWindows()');

    my $cascadeAct = Qt::Action("&Cascade", this);
    this->{cascadeAct} = $cascadeAct;
    $cascadeAct->setStatusTip("Cascade the windows");
    this->connect($cascadeAct, SIGNAL 'triggered()', this->{mdiArea}, SLOT 'cascadeSubWindows()');

    my $nextAct = Qt::Action("Ne&xt", this);
    this->{nextAct} = $nextAct;
    $nextAct->setStatusTip("Move the focus to the next window");
    this->connect($nextAct, SIGNAL 'triggered()',
            this->{mdiArea}, SLOT 'activateNextSubWindow()');

    my $previousAct = Qt::Action("Pre&vious", this);
    this->{previousAct} = $previousAct;
    $previousAct->setStatusTip("Move the focus to the previous " .
                                 "window");
    this->connect($previousAct, SIGNAL 'triggered()',
            this->{mdiArea}, SLOT 'activatePreviousSubWindow()');

    my $separatorAct = Qt::Action(this);
    this->{separatorAct} = $separatorAct;
    $separatorAct->setSeparator(1);

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

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

sub createMenus {
    my $fileMenu = this->menuBar()->addMenu("&File");
    $fileMenu->addAction(this->{newAct});
    $fileMenu->addAction(this->{openAct});
    $fileMenu->addAction(this->{saveAct});
    $fileMenu->addAction(this->{saveAsAct});
    $fileMenu->addSeparator();
    my $action = $fileMenu->addAction("Switch layout direction");
    this->connect($action, SIGNAL 'triggered()', this, SLOT 'switchLayoutDirection()');
    $fileMenu->addAction(this->{exitAct});

    my $editMenu = this->menuBar()->addMenu("&Edit");
    $editMenu->addAction(this->{cutAct});
    $editMenu->addAction(this->{copyAct});
    $editMenu->addAction(this->{pasteAct});

    my $windowMenu = this->menuBar()->addMenu("&Window");
    this->{windowMenu} = $windowMenu;
    this->updateWindowMenu();
    this->connect($windowMenu, SIGNAL 'aboutToShow()', this, SLOT 'updateWindowMenu()');

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

    my $helpMenu = this->menuBar()->addMenu("&Help");
    $helpMenu->addAction(this->{aboutAct});
    $helpMenu->addAction(this->{aboutQtAct});

}

sub createToolBars {
    my $fileToolBar = this->addToolBar("File");
    $fileToolBar->addAction(this->{newAct});
    $fileToolBar->addAction(this->{openAct});
    $fileToolBar->addAction(this->{saveAct});

    my $editToolBar = this->addToolBar("Edit");
    $editToolBar->addAction(this->{cutAct});
    $editToolBar->addAction(this->{copyAct});
    $editToolBar->addAction(this->{pasteAct});
}

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

sub readSettings {
    my $settings = Qt::Settings("Trolltech", "MDI 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->resize($size);
    this->move($pos);
}

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

sub activeMdiChild {
    if (my $activeSubWindow = this->{mdiArea}->activeSubWindow()) {
        return CAST $activeSubWindow->widget(), 'MdiChild';
    }
    return 0;
}

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

    foreach my $window ( @{this->{mdiArea}->subWindowList()} ) {
        my $mdiChild = CAST $window->widget(), 'MdiChild';
        if ($mdiChild->currentFile() eq $canonicalFilePath) {
            return $window;
        }
    }
    return 0;
}

sub switchLayoutDirection {
    if (this->layoutDirection() == Qt::LeftToRight()) {
        Qt::qApp()->setLayoutDirection(Qt::RightToLeft());
    }
    else {
        Qt::qApp()->setLayoutDirection(Qt::LeftToRight());
    }
}

sub setActiveSubWindow {
    my ( $window ) = @_;
    if (!$window){
        return;
    }
    this->{mdiArea}->setActiveSubWindow($window);
}

1;