File: MdiChild.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 (148 lines) | stat: -rw-r--r-- 3,582 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
package MdiChild;

use strict;
use warnings;
use blib;

use QtCore4;
use QtGui4;
use QtCore4::isa qw( Qt::TextEdit );
use QtCore4::slots
    documentWasModified => [''];

my $sequenceNumber = 1;

sub NEW {
    shift->SUPER::NEW(@_);
    this->setAttribute(Qt::WA_DeleteOnClose());
    this->{isUntitled} = 1;
}

sub newFile {
    this->{isUntitled} = 1;
    this->{curFile} = sprintf "document%d.txt", $sequenceNumber++;
    this->setWindowTitle(this->{curFile} . "[*]");

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

sub loadFile {
    my ( $fileName ) = @_;
    if(!(open( FH, "< $fileName"))) {
        Qt::MessageBox::warning(this, "MDI",
                                 sprintf("Cannot read file %s:\n%s.",
                                 $fileName,
                                 $!));
        return 0;
    }

    Qt::Application::setOverrideCursor(Qt::Cursor(Qt::WaitCursor()));
    this->setPlainText( join '', <FH> );
    Qt::Application::restoreOverrideCursor();
    close FH;

    setCurrentFile($fileName);

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

    return 1;
}

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

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

    return saveFile($fileName);
}

sub saveFile {
    my ( $fileName ) = @_;
    if(!(open( FH, "> $fileName"))) {
        Qt::MessageBox::warning(this, "MDI",
                                 sprintf("Cannot write file %s:\n%s.",
                                 $fileName,
                                 $!));
        return 0;
    }

    Qt::Application::setOverrideCursor(Qt::Cursor(Qt::WaitCursor()));
    print FH this->toPlainText();
    Qt::Application::restoreOverrideCursor();
    close FH;

    setCurrentFile($fileName);
    return 1;
}

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

sub closeEvent {
    my ( $event ) = @_;
    if (maybeSave()) {
        $event->accept();
    } else {
        $event->ignore();
    }
}

sub documentWasModified {
    this->setWindowModified(this->document()->isModified());
}

sub maybeSave {
    if (this->document()->isModified()) {
        my $ret = Qt::MessageBox::warning(
                     this,
                     "MDI",
                     sprintf( "'%s' has been modified.\n" .
                         "Do you want to save your changes?",
                         userFriendlyCurrentFile() ),
                     CAST Qt::MessageBox::Save() | Qt::MessageBox::Discard() | Qt::MessageBox::Cancel(), 'QMessageBox::StandardButtons');
        if ($ret == Qt::MessageBox::Save()) {
            return save();
        }
        elsif ($ret == Qt::MessageBox::Cancel()) {
            return 0;
        }
    }
    return 1;
}

sub setCurrentFile {
    my ( $fileName ) = @_;
    this->{curFile} = Qt::FileInfo($fileName)->canonicalFilePath();
    this->{isUntitled} = 0;
    this->document()->setModified(0);
    this->setWindowModified(0);
    this->setWindowTitle(userFriendlyCurrentFile() . "[*]");
}

sub currentFile {
    if( defined this->{curFile} ) {
        return this->{curFile};
    }
    return '';
}

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

1;