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 (189 lines) | stat: -rw-r--r-- 6,010 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
package MainWindow;

use strict;
use warnings;
use QtCore4;
use QtGui4;
use QtCore4::isa qw( Qt::MainWindow );
use Ui_MainWindow;
use MainWindow;
use TreeModel;

use QtCore4::slots
    updateActions => [],
    insertChild => [],
    insertColumn => ['const QModelIndex &'],
    insertColumn => [],
    insertRow => [],
    removeColumn => ['const QModelIndex &'],
    removeColumn => [],
    removeRow => [];

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

    this->{ui} = Ui_MainWindow->setupUi(this);

    my @headers = ( this->tr('Title'), this->tr('Description') );

    my $file = Qt::File('default.txt');
    $file->open(Qt::IODevice::ReadOnly());
    my $model = TreeModel(\@headers, $file->readAll(), this);
    $file->close();

    my $view = this->{ui}->view();
    $view->setModel($model);
    for (my $column = 0; $column < $model->columnCount(); ++$column) {
        $view->resizeColumnToContents($column);
    }

    my $exitAction = this->{ui}->exitAction();
    this->connect($exitAction, SIGNAL 'triggered()', qApp, SLOT 'quit()');

    this->connect($view->selectionModel(),
            SIGNAL 'selectionChanged(const QItemSelection &,' .
                                    'const QItemSelection &)',
            this, SLOT 'updateActions()');

    my $actionsMenu = this->{ui}->actionsMenu();
    my $insertRowAction = this->{ui}->insertRowAction();
    my $insertColumnAction = this->{ui}->insertColumnAction();
    my $removeRowAction = this->{ui}->removeRowAction();
    my $removeColumnAction = this->{ui}->removeColumnAction();
    my $insertChildAction = this->{ui}->insertChildAction();
    this->connect($actionsMenu, SIGNAL 'aboutToShow()', this, SLOT 'updateActions()');
    this->connect($insertRowAction, SIGNAL 'triggered()', this, SLOT 'insertRow()');
    this->connect($insertColumnAction, SIGNAL 'triggered()', this, SLOT 'insertColumn()');
    this->connect($removeRowAction, SIGNAL 'triggered()', this, SLOT 'removeRow()');
    this->connect($removeColumnAction, SIGNAL 'triggered()', this, SLOT 'removeColumn()');
    this->connect($insertChildAction, SIGNAL 'triggered()', this, SLOT 'insertChild()');

    this->updateActions();
}

sub insertChild
{
    $DB::single=1;
    my $view = this->{ui}->view();
    my $index = $view->selectionModel()->currentIndex();
    my $model = $view->model();

    if ($model->columnCount($index) == 0) {
        if (!$model->insertColumn(0, $index)) {
            return;
        }
    }

    if (!$model->insertRow(0, $index)) {
        return;
    }

    for (my $column = 0; $column < $model->columnCount($index); ++$column) {
        my $child = $model->index(0, $column, $index);
        $model->setData($child, Qt::Variant('[No data]'), Qt::EditRole());
        if (!$model->headerData($column, Qt::Horizontal())->isValid()) {
            $model->setHeaderData($column, Qt::Horizontal(), Qt::Variant('[No header]'),
                                 Qt::EditRole());
        }
    }

    $view->selectionModel()->setCurrentIndex($model->index(0, 0, $index),
                                            Qt::ItemSelectionModel::ClearAndSelect());
    this->updateActions();
}

sub insertColumn
{
    my ($parent) = @_;
    $parent = $parent ? $parent : Qt::ModelIndex();
    my $view = this->{ui}->view();
    my $model = $view->model();
    my $column = $view->selectionModel()->currentIndex()->column();

    # Insert a column in the parent item.
    my $changed = $model->insertColumn($column + 1, $parent);
    if ($changed) {
        $model->setHeaderData($column + 1, Qt::Horizontal(), Qt::Variant('[No header]'),
                             Qt::EditRole());
    }

    this->updateActions();

    return $changed;
}

sub insertRow
{
    my $view = this->{ui}->view();
    my $index = $view->selectionModel()->currentIndex();
    my $model = $view->model();

    if (!$model->insertRow($index->row()+1, $index->parent())) {
        return;
    }

    this->updateActions();

    for (my $column = 0; $column < $model->columnCount($index->parent()); ++$column) {
        my $child = $model->index($index->row()+1, $column, $index->parent());
        $model->setData($child, Qt::Variant('[No data]'), Qt::EditRole());
    }
}

sub removeColumn
{
    my ($parent) = @_;
    $parent = $parent ? $parent : Qt::ModelIndex();
    my $view = this->{ui}->view();
    my $model = $view->model();
    my $column = $view->selectionModel()->currentIndex()->column();

    # Insert columns in each child of the parent item.
    my $changed = $model->removeColumn($column, $parent);

    if (!$parent->isValid() && $changed) {
        this->updateActions();
    }

    return $changed;
}

sub removeRow
{
    my $view = this->{ui}->view();
    my $index = $view->selectionModel()->currentIndex();
    my $model = $view->model();
    if ($model->removeRow($index->row(), $index->parent())) {
        this->updateActions();
    }
}

sub updateActions
{
    my $view = this->{ui}->view();
    my $selection = $view->selectionModel()->selection()->indexes();
    my $hasSelection = $selection ? scalar @{$selection} : 0;
    this->{ui}->removeRowAction->setEnabled($hasSelection);
    this->{ui}->removeColumnAction->setEnabled($hasSelection);

    my $hasCurrent = $view->selectionModel()->currentIndex()->isValid();
    this->{ui}->insertRowAction->setEnabled($hasCurrent);
    this->{ui}->insertColumnAction->setEnabled($hasCurrent);

    if ($hasCurrent) {
        $view->closePersistentEditor($view->selectionModel()->currentIndex());

        my $row = $view->selectionModel()->currentIndex()->row();
        my $column = $view->selectionModel()->currentIndex()->column();
        if ($view->selectionModel()->currentIndex()->parent()->isValid()) {
            this->statusBar()->showMessage(sprintf this->tr('Position: (%d,%d)'), $row, $column);
        }
        else {
            this->statusBar()->showMessage(sprintf this->tr('Position: (%d,%d) in top level'), $row, $column);
        }
    }
}

1;