File: DropSiteWindow.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 (148 lines) | stat: -rw-r--r-- 4,301 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
package DropSiteWindow;

use strict;
use warnings;
use QtCore4;
use QtGui4;
# [DropSiteWindow header]
use QtCore4::isa qw( Qt::Widget );
use QtCore4::slots
    updateFormatsTable => ['const QMimeData *'];
use DropArea;

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

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

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

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

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

sub buttonBox() {
    return this->{buttonBox};
}
# [DropSiteWindow header]

# [constructor part1]
sub NEW
{
    my ($class) = @_;
    $class->SUPER::NEW();
    this->{abstractLabel} = Qt::Label(this->tr('This example accepts drags from other ' .
                                  'applications and displays the MIME types ' .
                                  'provided by the drag object.'));
    this->abstractLabel->setWordWrap(1);
    this->abstractLabel->adjustSize();
# [constructor part1]

# [constructor part2]
    this->{dropArea} = DropArea();
    this->connect(this->dropArea, SIGNAL 'changed(const QMimeData *)',
            this, SLOT 'updateFormatsTable(const QMimeData *)');
# [constructor part2]

# [constructor part3]
    my @labels = (this->tr('Format'), this->tr('Content'));

    this->{formatsTable} = Qt::TableWidget();
    this->formatsTable->setColumnCount(2);
    this->formatsTable->setEditTriggers(Qt::AbstractItemView::NoEditTriggers());
    this->formatsTable->setHorizontalHeaderLabels(\@labels);
    this->formatsTable->horizontalHeader()->setStretchLastSection(1);
# [constructor part3]

# [constructor part4]
    this->{clearButton} = Qt::PushButton(this->tr('Clear'));
    this->{quitButton} = Qt::PushButton(this->tr('Quit'));

    this->{buttonBox} = Qt::DialogButtonBox();
    this->buttonBox->addButton(this->clearButton, Qt::DialogButtonBox::ActionRole());
    this->buttonBox->addButton(this->quitButton, Qt::DialogButtonBox::RejectRole());

    this->connect(this->quitButton, SIGNAL 'pressed()', this, SLOT 'close()');
    this->connect(this->clearButton, SIGNAL 'pressed()', this->dropArea, SLOT 'clear()');
# [constructor part4]

# [constructor part5]
    my $mainLayout = Qt::VBoxLayout();
    $mainLayout->addWidget(this->abstractLabel);
    $mainLayout->addWidget(this->dropArea);
    $mainLayout->addWidget(this->formatsTable);
    $mainLayout->addWidget(this->buttonBox);
    this->setLayout($mainLayout);

    this->setWindowTitle(this->tr('Drop Site'));
    this->setMinimumSize(350, 500);
}
# [constructor part5]

sub simplified
{
    my ($text) = @_;
    $text =~ s/[\s]+/ /g;
    $text =~ s/^ //g;
    $text =~ s/ $//g;
    return $text;
}

# [updateFormatsTable() part1]
sub updateFormatsTable
{
    my ($mimeData) = @_;
    this->formatsTable->setRowCount(0);
    if (!$mimeData) {
        return;
    }
# [updateFormatsTable() part1]

# [updateFormatsTable() part2]        
    foreach my $format ( @{$mimeData->formats()} ) {
        my $formatItem = Qt::TableWidgetItem($format);
        $formatItem->setFlags(Qt::ItemIsEnabled());
        $formatItem->setTextAlignment(Qt::AlignTop() | Qt::AlignLeft());
# [updateFormatsTable() part2]

# [updateFormatsTable() part3]
        my $text;
        if ($format eq 'text/plain') {
            $text = this->simplified($mimeData->text());
        } elsif ($format eq 'text/html') {
            $text = this->simplified($mimeData->html());
        } elsif ($format eq 'text/uri-list') {
            my $urlList = $mimeData->urls();
            foreach my $url ( @{$urlList} ) {
                $text .= $url . ' ';
            }
        } else {
            my $data = $mimeData->data($format);
            for (my $i = 0; $i < $data->size() && $i < 32; ++$i) {
                my $hex = uc sprintf '%02x', $data->at($i);
                $text .= $hex . ' ';
            }
        }
# [updateFormatsTable() part3]   

# [updateFormatsTable() part4]
        my $row = this->formatsTable->rowCount();
        this->formatsTable->insertRow($row);
        this->formatsTable->setItem($row, 0, Qt::TableWidgetItem($format));
        this->formatsTable->setItem($row, 1, Qt::TableWidgetItem(Qt::String($text)));
    }
    
    this->formatsTable->resizeColumnToContents(0);
}
# [updateFormatsTable() part4] 

1;