File: KALedMeter.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 (167 lines) | stat: -rw-r--r-- 3,155 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package KALedMeter;

#/*
 #* KDE::Asteroids - Copyright (c) Martin R. Jones 1997
 #*
 #* Part of the KDE::DE project
 #*/


use strict;
use warnings;
use QtCore4;
use QtGui4;
use Qt3Support4;
use QtCore4::isa qw( Qt3::Frame );
use QtCore4::slots
    setValue => ['int'];

#struct ColorRange
#{
    #int mPc;
    #int mValue;
    #Qt::Color mColor;
#};

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

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

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

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

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

sub NEW
{
    my ($class, $parent) = @_;
    $class->SUPER::NEW($parent);
    this->{mRange} = 100;
    this->{mCount} = 20;
    this->{mCurrentCount} = 0;
    this->{mValue} = 0;
    this->{mCRanges} = [];
    setMinimumWidth( mCount * 2 + frameWidth() );
}

sub setRange
{
    my ( $r ) = @_;
    this->{mRange} = $r;
    if ( mRange < 1 ) {
        this->{mRange} = 1;
    }
    setValue( mValue );
    update();
}

sub setCount
{
    my ( $c ) = @_;
    this->{mCount} = $c;
    if ( mCount < 1 ) {
        this->{mCount} = 1;
    }
    setMinimumWidth( mCount * 2 + frameWidth() );
    calcColorRanges();
    setValue( mValue );
    update();
}

sub setValue
{
    my ( $v ) = @_;
    this->{mValue} = $v;
    if ( mValue > mRange ) {
        this->{mValue} = mRange;
    }
    elsif ( mValue < 0 ) {
        this->{mValue} = 0;
    }
    my $c = ( mValue + mRange / mCount - 1 ) * mCount / mRange;
    if ( $c != mCurrentCount )
    {
        this->{mCurrentCount} = $c;
        update();
    }
}

sub addColorRange
{
    my ( $pc, $c ) = @_;
    my $cr = {
        mPc => $pc,
        mValue => undef,
        mColor => $c,
    };
    push @{mCRanges()}, $cr;
    calcColorRanges();
}

sub resizeEvent
{
    my ( $e ) = @_;
    this->SUPER::resizeEvent( $e );
    my $w = ( width() - frameWidth() - 2 ) / mCount * mCount;
    $w += frameWidth() + 2;
    setFrameRect( Qt::Rect( 0, 0, $w, height() ) );
}

sub drawContents
{
    my ( $p ) = @_;
    my $b = contentsRect();

    my $cidx = 0;
    my $ncol = mCount;
    my $col = colorGroup()->foreground();

    if ( scalar @{mCRanges()} )
    {
        $col = mCRanges()->[$cidx]->{mColor};
        $ncol = mCRanges()->[$cidx]->{mValue};
    }
    $p->setBrush( Qt::Brush(Qt::Color($col)) );
    $p->setPen( Qt::Pen(Qt::Color($col)) );

    my $lw = $b->width() / mCount;
    my $lx = $b->left() + 1;
    for ( my $i = 0; $i < mCurrentCount; $i++, $lx += $lw )
    {
        if ( $i > $ncol )
        {
            if ( ++$cidx < scalar @{mCRanges()} )
            {
                $col = mCRanges()->[$cidx]->{mColor};
                $ncol = mCRanges()->[$cidx]->{mValue};
                $p->setBrush( Qt::Brush(Qt::Color($col)) );
                $p->setPen( Qt::Pen(Qt::Color($col)) );
            }
        }

        $p->drawRect( int($lx), int($b->top() + 1), int($lw - 1), int($b->height() - 2) );
    }
}

sub calcColorRanges
{
    my $prev = 0;
    foreach my $cr ( @{mCRanges()} )
    {
        $cr->{mValue} = $prev + $cr->{mPc} * mCount / 100;
        $prev = $cr->{mValue};
    }
}

1;