File: validators.pl

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 (130 lines) | stat: -rwxr-xr-x 3,725 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
#!/usr/bin/perl

package ValidatorWidget;

use strict;
use warnings;

use QtCore4;
use QtGui4;
use Ui_ValidatorsForm;
use QtCore4::isa qw( Qt::Widget Ui_ValidatorsForm );
use QtCore4::slots
    updateValidator => [],
    updateDoubleValidator => [],
    _setLocale => ['const QLocale &'];

sub _setLocale {
    my( $l ) = @_;
    this->setLocale($l);
    this->updateValidator();
    this->updateDoubleValidator();
}

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

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

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

    my $localeSelector = $ui->{localeSelector};
    this->connect($localeSelector, SIGNAL 'localeSelected(QLocale)', this, SLOT '_setLocale(QLocale)');

    my $minVal = $ui->{minVal};
    my $maxVal = $ui->{maxVal};
    my $editor = $ui->{editor};
    my $ledWidget = $ui->{ledWidget};
    this->connect($minVal, SIGNAL 'editingFinished()', this, SLOT 'updateValidator()');
    this->connect($maxVal, SIGNAL 'editingFinished()', this, SLOT 'updateValidator()');
    this->connect($editor, SIGNAL 'editingFinished()', $ledWidget, SLOT 'flash()');

    my $doubleMaxVal = $ui->{doubleMaxVal};
    my $doubleMinVal = $ui->{doubleMinVal};
    my $doubleDecimals = $ui->{doubleDecimals};
    my $doubleFormat = $ui->{doubleFormat};
    my $doubleEditor = $ui->{doubleEditor};
    my $doubleLedWidget = $ui->{doubleLedWidget};
    this->connect($doubleMaxVal, SIGNAL 'editingFinished()', this, SLOT 'updateDoubleValidator()');
    this->connect($doubleMinVal, SIGNAL 'editingFinished()', this, SLOT 'updateDoubleValidator()');
    this->connect($doubleDecimals, SIGNAL 'valueChanged(int)', this, SLOT 'updateDoubleValidator()');
    this->connect($doubleFormat, SIGNAL 'activated(int)', this, SLOT 'updateDoubleValidator()');
    this->connect($doubleEditor, SIGNAL 'editingFinished()', $doubleLedWidget, SLOT 'flash()');

    this->{validator} = 0;
    this->{doubleValidator} = 0;
    this->updateValidator();
    this->updateDoubleValidator();
}

sub updateValidator {
    my $ui = this->{ui};
    my $minVal = $ui->{minVal};
    my $maxVal = $ui->{maxVal};
    my $editor = $ui->{editor};
    my $v = Qt::IntValidator($minVal->value(), $maxVal->value(), this);
    $v->setLocale(this->locale());
    $editor->setValidator($v);
    this->{validator} = $v;
    my $validator = this->{validator};

    my $s = $editor->text();
    my $i = 0;
    if ($validator->validate($s, $i) == Qt::Validator::Invalid()) {
        $editor->clear();
    } else {
        $editor->setText($s);
    }
}

sub updateDoubleValidator {
    my $ui = this->{ui};
    my $doubleMinVal = $ui->{doubleMinVal};
    my $doubleMaxVal = $ui->{doubleMaxVal};
    my $doubleDecimals = $ui->{doubleDecimals};
    my $doubleFormat = $ui->{doubleFormat};
    my $v = Qt::DoubleValidator($doubleMinVal->value(), $doubleMaxVal->value(),
                                $doubleDecimals->value(), this);
    $v->setNotation($doubleFormat->currentIndex());
    $v->setLocale(this->locale());
    my $doubleEditor = $ui->{doubleEditor};
    $doubleEditor->setValidator($v);
    this->{doubleValidator} = $v;
    my $doubleValidator = this->{doubleValidator};

    my $s = $doubleEditor->text();
    my $i = 0;
    if ($doubleValidator->validate($s, $i) == Qt::Validator::Invalid()) {
        $doubleEditor->clear();
    } else {
        $doubleEditor->setText($s);
    }
}

1;

package main;

use strict;
use warnings;

use QtCore4;
use QtGui4;
use ValidatorWidget;

sub main {
    my $app = Qt::Application( \@ARGV );

    my $w = ValidatorWidget();
    $w->show();

    return $app->exec();
}

exit main();