~ubuntu-branches/ubuntu/natty/kdebindings/natty

« back to all changes in this revision

Viewing changes to perl/qtgui/examples/tools/undoframework/MainWindow.pm

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-12-01 11:14:38 UTC
  • mfrom: (1.1.41 upstream)
  • Revision ID: james.westby@ubuntu.com-20101201111438-1q61cvs8zw118t3m
Tags: 4:4.5.80-0ubuntu1
* New upstream release
* Remove build-dep on libqimageblitz-dev, causes smoke to segfault

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package MainWindow;
 
2
 
 
3
use strict;
 
4
use warnings;
 
5
use QtCore4;
 
6
use QtGui4;
 
7
 
 
8
# [0]
 
9
use QtCore4::isa qw( Qt::MainWindow );
 
10
use QtCore4::slots
 
11
    itemMoved => ['QGraphicsPolygonItem *', 'const QPointF &'],
 
12
    deleteItem => [],
 
13
    addBox => [],
 
14
    addTriangle => [],
 
15
    about => [],
 
16
    itemMenuAboutToShow => [],
 
17
    itemMenuAboutToHide => [];
 
18
 
 
19
use MainWindow;
 
20
use DiagramScene;
 
21
use DiagramItem;
 
22
use Commands;
 
23
use MoveCommand;
 
24
use DeleteCommand;
 
25
use AddCommand;
 
26
 
 
27
sub deleteAction() {
 
28
    return this->{deleteAction};
 
29
}
 
30
 
 
31
sub addBoxAction() {
 
32
    return this->{addBoxAction};
 
33
}
 
34
 
 
35
sub addTriangleAction() {
 
36
    return this->{addTriangleAction};
 
37
}
 
38
 
 
39
sub undoAction() {
 
40
    return this->{undoAction};
 
41
}
 
42
 
 
43
sub redoAction() {
 
44
    return this->{redoAction};
 
45
}
 
46
 
 
47
sub exitAction() {
 
48
    return this->{exitAction};
 
49
}
 
50
 
 
51
sub aboutAction() {
 
52
    return this->{aboutAction};
 
53
}
 
54
 
 
55
sub fileMenu() {
 
56
    return this->{fileMenu};
 
57
}
 
58
 
 
59
sub editMenu() {
 
60
    return this->{editMenu};
 
61
}
 
62
 
 
63
sub itemMenu() {
 
64
    return this->{itemMenu};
 
65
}
 
66
 
 
67
sub helpMenu() {
 
68
    return this->{helpMenu};
 
69
}
 
70
 
 
71
sub diagramScene() {
 
72
    return this->{diagramScene};
 
73
}
 
74
 
 
75
sub undoStack() {
 
76
    return this->{undoStack};
 
77
}
 
78
 
 
79
sub undoView() {
 
80
    return this->{undoView};
 
81
}
 
82
 
 
83
sub rootUndoCommand{
 
84
    return this->{rootUndoCommand};
 
85
}
 
86
 
 
87
# [0]
 
88
sub NEW
 
89
{
 
90
    my ($class) = @_;
 
91
    $class->SUPER::NEW();
 
92
    this->{undoStack} = Qt::UndoStack();
 
93
    this->{rootUndoCommand} = Qt::UndoCommand();
 
94
 
 
95
    createActions();
 
96
    createMenus();
 
97
 
 
98
    createUndoView();
 
99
 
 
100
    this->{diagramScene} = DiagramScene();
 
101
    my $pixmapBrush = Qt::Brush(Qt::Pixmap(':/images/cross.png')->scaled(30, 30));
 
102
    diagramScene->setBackgroundBrush($pixmapBrush);
 
103
    diagramScene->setSceneRect(Qt::RectF(0, 0, 500, 500));
 
104
 
 
105
    this->connect(diagramScene, SIGNAL 'itemMoved(QGraphicsPolygonItem*,QPointF)',
 
106
            this, SLOT 'itemMoved(QGraphicsPolygonItem*,QPointF)');
 
107
 
 
108
    setWindowTitle('Undo Framework');
 
109
    my $view = Qt::GraphicsView(diagramScene);
 
110
    setCentralWidget($view);
 
111
    resize(700, 500);
 
112
}
 
113
# [0]
 
114
 
 
115
# [1]
 
116
sub createUndoView
 
117
{
 
118
    this->{undoView} = Qt::UndoView(undoStack);
 
119
    undoView->setWindowTitle(this->tr('Command List'));
 
120
    undoView->show();
 
121
    undoView->setAttribute(Qt::WA_QuitOnClose(), 0);
 
122
}
 
123
# [1]
 
124
 
 
125
# [2]
 
126
sub createActions
 
127
{
 
128
    this->{deleteAction} = Qt::Action(this->tr('&Delete Item'), this);
 
129
    deleteAction->setShortcut(Qt::KeySequence(this->tr('Del')));
 
130
    this->connect(deleteAction, SIGNAL 'triggered()', this, SLOT 'deleteItem()');
 
131
# [2] //! [3]
 
132
 
 
133
# [3] //! [4]
 
134
    this->{addBoxAction} = Qt::Action(this->tr('Add &Box'), this);
 
135
# [4]
 
136
    addBoxAction->setShortcut(Qt::KeySequence(this->tr('Ctrl+O')));
 
137
    this->connect(addBoxAction, SIGNAL 'triggered()', this, SLOT 'addBox()');
 
138
 
 
139
    this->{addTriangleAction} = Qt::Action(this->tr('Add &Triangle'), this);
 
140
    addTriangleAction->setShortcut(Qt::KeySequence(this->tr('Ctrl+T')));
 
141
    this->connect(addTriangleAction, SIGNAL 'triggered()', this, SLOT 'addTriangle()');
 
142
 
 
143
# [5]
 
144
    this->{undoAction} = undoStack->createUndoAction(this, this->tr('&Undo'));
 
145
    undoAction->setShortcut(Qt::KeySequence(Qt::KeySequence::Undo()));
 
146
 
 
147
    this->{redoAction} = undoStack->createRedoAction(this, this->tr('&Redo'));
 
148
    redoAction->setShortcut(Qt::KeySequence(Qt::KeySequence::Redo()));
 
149
# [5]
 
150
 
 
151
    this->{exitAction} = Qt::Action(this->tr('E&xit'), this);
 
152
    exitAction->setShortcut(Qt::KeySequence(Qt::KeySequence::Quit()));
 
153
    this->connect(exitAction, SIGNAL 'triggered()', this, SLOT 'close()');
 
154
 
 
155
    this->{aboutAction} = Qt::Action(this->tr('&About'), this);
 
156
    my @aboutShortcuts = (
 
157
        Qt::KeySequence( this->tr('Ctrl+A') ),
 
158
        Qt::KeySequence( this->tr('Ctrl+B') )
 
159
    );
 
160
    aboutAction->setShortcuts(\@aboutShortcuts);
 
161
    this->connect(aboutAction, SIGNAL 'triggered()', this, SLOT 'about()');
 
162
}
 
163
 
 
164
# [6]
 
165
sub createMenus
 
166
{
 
167
# [6]
 
168
    this->{fileMenu} = menuBar()->addMenu(this->tr('&File'));
 
169
    fileMenu->addAction(exitAction);
 
170
 
 
171
# [7]
 
172
    this->{editMenu} = menuBar()->addMenu(this->tr('&Edit'));
 
173
    editMenu->addAction(undoAction);
 
174
    editMenu->addAction(redoAction);
 
175
    editMenu->addSeparator();
 
176
    editMenu->addAction(deleteAction);
 
177
    this->connect(editMenu, SIGNAL 'aboutToShow()',
 
178
            this, SLOT 'itemMenuAboutToShow()');
 
179
    this->connect(editMenu, SIGNAL 'aboutToHide()',
 
180
            this, SLOT 'itemMenuAboutToHide()');
 
181
 
 
182
# [7]
 
183
    this->{itemMenu} = menuBar()->addMenu(this->tr('&Item'));
 
184
    itemMenu->addAction(addBoxAction);
 
185
    itemMenu->addAction(addTriangleAction);
 
186
 
 
187
    this->{helpMenu} = menuBar()->addMenu(this->tr('&About'));
 
188
    helpMenu->addAction(aboutAction);
 
189
# [8]
 
190
}
 
191
# [8]
 
192
 
 
193
# [9]
 
194
sub itemMoved
 
195
{
 
196
    my ($movedItem, $oldPosition) = @_;
 
197
    undoStack->push(MoveCommand($movedItem, $oldPosition, rootUndoCommand));
 
198
}
 
199
# [9]
 
200
 
 
201
# [10]
 
202
sub deleteItem
 
203
{
 
204
    if (!scalar @{diagramScene->selectedItems()}) {
 
205
        return;
 
206
    }
 
207
 
 
208
    my $deleteCommand = DeleteCommand(diagramScene, rootUndoCommand);
 
209
    undoStack->push($deleteCommand);
 
210
}
 
211
# [10]
 
212
 
 
213
# [11]
 
214
sub itemMenuAboutToHide
 
215
{
 
216
    deleteAction->setEnabled(1);
 
217
}
 
218
# [11]
 
219
 
 
220
# [12]
 
221
sub itemMenuAboutToShow
 
222
{
 
223
    deleteAction->setEnabled(scalar @{diagramScene->selectedItems()} > 0);
 
224
}
 
225
# [12]
 
226
 
 
227
# [13]
 
228
sub addBox
 
229
{
 
230
    my $addCommand = AddCommand(DiagramItem::Box, diagramScene, rootUndoCommand);
 
231
    undoStack->push($addCommand);
 
232
}
 
233
# [13]
 
234
 
 
235
# [14]
 
236
sub addTriangle
 
237
{
 
238
    my $addCommand = AddCommand(DiagramItem::Triangle, diagramScene, rootUndoCommand);
 
239
    undoStack->push($addCommand);
 
240
}
 
241
# [14]
 
242
 
 
243
# [15]
 
244
sub about
 
245
{
 
246
    Qt::MessageBox::about(this, this->tr('About Undo'),
 
247
                       this->tr('The <b>Undo</b> example demonstrates how to ' .
 
248
                          'use Qt\'s undo framework.'));
 
249
}
 
250
# [15]
 
251
 
 
252
1;