summaryrefslogtreecommitdiffstats
path: root/libkdegames/kgamelcd.cpp
blob: cf079a15b388139a5e15ae5e6e2a57d735228b59 (plain)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
    This file is part of the KDE games library
    Copyright (C) 2001,2002,2003 Nicolas Hadacek ([email protected])

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License version 2 as published by the Free Software Foundation.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "kgamelcd.h"
#include "kgamelcd.moc"

#include <tqlayout.h>
#include <tqlabel.h>
#include <tqtimer.h>

#include <kglobal.h>


//-----------------------------------------------------------------------------
KGameLCD::KGameLCD(uint nbDigits, TQWidget *parent, const char *name)
    : TQLCDNumber(nbDigits, parent, name), _htime(800)
{
    const TQPalette &p = palette();
    _fgColor = p.color(TQPalette::Active, TQColorGroup::Foreground);
    _hlColor = p.color(TQPalette::Active, TQColorGroup::HighlightedText);

    _timer = new TQTimer(this);
    connect(_timer, TQT_SIGNAL(timeout()), TQT_SLOT(timeout()));

    setFrameStyle(Panel | Plain);
	setSegmentStyle(Flat);

    displayInt(0);
}

KGameLCD::~KGameLCD()
{}

void KGameLCD::setDefaultBackgroundColor(const TQColor &color)
{
    TQPalette p = palette();
    p.setColor(TQColorGroup::Background, color);
    setPalette(p);
}

void KGameLCD::setDefaultColor(const TQColor &color)
{
    _fgColor = color;
    TQPalette p = palette();
    p.setColor(TQColorGroup::Foreground, color);
    setPalette(p);
}

void KGameLCD::setHighlightColor(const TQColor &color)
{
    _hlColor = color;
}

void KGameLCD::setLeadingString(const TQString &s)
{
    _lead = s;
    displayInt(0);
}

void KGameLCD::setHighlightTime(uint time)
{
    _htime = time;
}

void KGameLCD::resetColor()
{
    setColor(TQColor());
}

void KGameLCD::setColor(const TQColor &color)
{
    const TQColor &c = (color.isValid() ? color : _fgColor);
    TQPalette p = palette();
    p.setColor(TQColorGroup::Foreground, c);
    setPalette(p);
}

void KGameLCD::displayInt(int v)
{
    int n = numDigits() - _lead.length();
    display(_lead + TQString::number(v).rightJustify(n));
}

void KGameLCD::highlight()
{
    highlight(true);
    _timer->start(_htime, true);
}

void KGameLCD::highlight(bool light)
{
    if (light) setColor(_hlColor);
    else resetColor();
}

//-----------------------------------------------------------------------------
KGameLCDClock::KGameLCDClock(TQWidget *parent, const char *name)
: KGameLCD(5, parent, name)
{
    _timerClock = new TQTimer(this);
    connect(_timerClock, TQT_SIGNAL(timeout()), TQT_SLOT(timeoutClock()));
}

KGameLCDClock::~KGameLCDClock()
{}

void KGameLCDClock::timeoutClock()
{
    // waiting an hour does not restart timer
    if ( _min==59 && _sec==59 ) return;
    _sec++;
    if (_sec==60) {
        _min++;
        _sec = 0;
    }
    showTime();
}

TQString KGameLCDClock::pretty() const
{
    TQString sec = TQString::number(_sec).rightJustify(2, '0', true);
    TQString min = TQString::number(_min).rightJustify(2, '0', true);
    return min + ':' + sec;
}

void KGameLCDClock::showTime()
{
    display(pretty());
}

void KGameLCDClock::reset()
{
    _timerClock->stop();
	_sec = 0;
    _min = 0;
	showTime();
}

void KGameLCDClock::start()
{
    _timerClock->start(1000); // 1 second
}

void KGameLCDClock::stop()
{
    _timerClock->stop();
}

uint KGameLCDClock::seconds() const
{
    return _min*60 + _sec;
}

void KGameLCDClock::setTime(uint sec)
{
    Q_ASSERT( sec<3600 );
    _sec = sec % 60;
    _min = sec / 60;
    showTime();
}

void KGameLCDClock::setTime(const TQString &s)
{
    Q_ASSERT( s.length()==5 && s[2]==':' );
    uint min = kMin(s.section(':', 0, 0).toUInt(), uint(59));
    uint sec = kMin(s.section(':', 1, 1).toUInt(), uint(59));
    setTime(sec + min*60);
}


//-----------------------------------------------------------------------------
class KGameLCDList::KGameLCDListPrivate
{
public:
  TQValueVector<TQLabel *> _leadings;
};

KGameLCDList::KGameLCDList(const TQString &title, TQWidget *parent,
                           const char *name)
    : TQWidget(parent, name)
{
    init(title);
}

KGameLCDList::KGameLCDList(TQWidget *parent, const char *name)
    : TQWidget(parent, name)
{
    init(TQString());
}

KGameLCDList::~KGameLCDList()
{
  delete d;
}

void KGameLCDList::init(const TQString &title)
{
    d = new KGameLCDListPrivate;

    TQGridLayout *top = new TQGridLayout(this, 1, 2, 5);
    top->setColStretch(1, 1);

    _title = new TQLabel(title, this);
    _title->tqsetAlignment(AlignCenter);
    top->addMultiCellWidget(_title, 0, 0, 0, 1, AlignCenter);
}

void KGameLCDList::append(TQLCDNumber *lcd)
{
    append(TQString(), lcd);
}

void KGameLCDList::append(const TQString &leading, TQLCDNumber *lcd)
{
    uint i = size();
    TQLabel *label = 0;
    if ( !leading.isEmpty() ) {
      label = new TQLabel(leading, this);
      static_cast<TQGridLayout *>(tqlayout())->addWidget(label, i+1, 0);
    }
    d->_leadings.push_back(label);
    _lcds.push_back(lcd);
    static_cast<TQGridLayout *>(tqlayout())->addWidget(lcd, i+1, 1);
}

void KGameLCDList::clear()
{
    for (uint i=0; i<_lcds.size(); i++) {
        delete d->_leadings[i];
        delete _lcds[i];
    }
    d->_leadings.clear();
    _lcds.clear();
}