00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "kgamelcd.h"
00021 #include "kgamelcd.moc"
00022
00023 #include <qlayout.h>
00024 #include <qlabel.h>
00025 #include <qtimer.h>
00026
00027 #include <kglobal.h>
00028
00029
00030
00031 KGameLCD::KGameLCD(uint nbDigits, QWidget *parent, const char *name)
00032 : QLCDNumber(nbDigits, parent, name), _htime(800)
00033 {
00034 const QPalette &p = palette();
00035 _fgColor = p.color(QPalette::Active, QColorGroup::Foreground);
00036 _hlColor = p.color(QPalette::Active, QColorGroup::HighlightedText);
00037
00038 _timer = new QTimer(this);
00039 connect(_timer, SIGNAL(timeout()), SLOT(timeout()));
00040
00041 setFrameStyle(Panel | Plain);
00042 setSegmentStyle(Flat);
00043
00044 displayInt(0);
00045 }
00046
00047 KGameLCD::~KGameLCD()
00048 {}
00049
00050 void KGameLCD::setDefaultBackgroundColor(const QColor &color)
00051 {
00052 QPalette p = palette();
00053 p.setColor(QColorGroup::Background, color);
00054 setPalette(p);
00055 }
00056
00057 void KGameLCD::setDefaultColor(const QColor &color)
00058 {
00059 _fgColor = color;
00060 QPalette p = palette();
00061 p.setColor(QColorGroup::Foreground, color);
00062 setPalette(p);
00063 }
00064
00065 void KGameLCD::setHighlightColor(const QColor &color)
00066 {
00067 _hlColor = color;
00068 }
00069
00070 void KGameLCD::setLeadingString(const QString &s)
00071 {
00072 _lead = s;
00073 displayInt(0);
00074 }
00075
00076 void KGameLCD::setHighlightTime(uint time)
00077 {
00078 _htime = time;
00079 }
00080
00081 void KGameLCD::resetColor()
00082 {
00083 setColor(QColor());
00084 }
00085
00086 void KGameLCD::setColor(const QColor &color)
00087 {
00088 const QColor &c = (color.isValid() ? color : _fgColor);
00089 QPalette p = palette();
00090 p.setColor(QColorGroup::Foreground, c);
00091 setPalette(p);
00092 }
00093
00094 void KGameLCD::displayInt(int v)
00095 {
00096 int n = numDigits() - _lead.length();
00097 display(_lead + QString::number(v).rightJustify(n));
00098 }
00099
00100 void KGameLCD::highlight()
00101 {
00102 highlight(true);
00103 _timer->start(_htime, true);
00104 }
00105
00106 void KGameLCD::highlight(bool light)
00107 {
00108 if (light) setColor(_hlColor);
00109 else resetColor();
00110 }
00111
00112
00113 KGameLCDClock::KGameLCDClock(QWidget *parent, const char *name)
00114 : KGameLCD(5, parent, name)
00115 {
00116 _timerClock = new QTimer(this);
00117 connect(_timerClock, SIGNAL(timeout()), SLOT(timeoutClock()));
00118 }
00119
00120 KGameLCDClock::~KGameLCDClock()
00121 {}
00122
00123 void KGameLCDClock::timeoutClock()
00124 {
00125
00126 if ( _min==59 && _sec==59 ) return;
00127 _sec++;
00128 if (_sec==60) {
00129 _min++;
00130 _sec = 0;
00131 }
00132 showTime();
00133 }
00134
00135 QString KGameLCDClock::pretty() const
00136 {
00137 QString sec = QString::number(_sec).rightJustify(2, '0', true);
00138 QString min = QString::number(_min).rightJustify(2, '0', true);
00139 return min + ':' + sec;
00140 }
00141
00142 void KGameLCDClock::showTime()
00143 {
00144 display(pretty());
00145 }
00146
00147 void KGameLCDClock::reset()
00148 {
00149 _timerClock->stop();
00150 _sec = 0;
00151 _min = 0;
00152 showTime();
00153 }
00154
00155 void KGameLCDClock::start()
00156 {
00157 _timerClock->start(1000);
00158 }
00159
00160 void KGameLCDClock::stop()
00161 {
00162 _timerClock->stop();
00163 }
00164
00165 uint KGameLCDClock::seconds() const
00166 {
00167 return _min*60 + _sec;
00168 }
00169
00170 void KGameLCDClock::setTime(uint sec)
00171 {
00172 Q_ASSERT( sec<3600 );
00173 _sec = sec % 60;
00174 _min = sec / 60;
00175 showTime();
00176 }
00177
00178 void KGameLCDClock::setTime(const QString &s)
00179 {
00180 Q_ASSERT( s.length()==5 && s[2]==':' );
00181 uint min = kMin(s.section(':', 0, 0).toUInt(), uint(59));
00182 uint sec = kMin(s.section(':', 1, 1).toUInt(), uint(59));
00183 setTime(sec + min*60);
00184 }
00185
00186
00187
00188 class KGameLCDList::KGameLCDListPrivate
00189 {
00190 public:
00191 QValueVector<QLabel *> _leadings;
00192 };
00193
00194 KGameLCDList::KGameLCDList(const QString &title, QWidget *parent,
00195 const char *name)
00196 : QWidget(parent, name)
00197 {
00198 init(title);
00199 }
00200
00201 KGameLCDList::KGameLCDList(QWidget *parent, const char *name)
00202 : QWidget(parent, name)
00203 {
00204 init(QString::null);
00205 }
00206
00207 KGameLCDList::~KGameLCDList()
00208 {
00209 delete d;
00210 }
00211
00212 void KGameLCDList::init(const QString &title)
00213 {
00214 d = new KGameLCDListPrivate;
00215
00216 QGridLayout *top = new QGridLayout(this, 1, 2, 5);
00217 top->setColStretch(1, 1);
00218
00219 _title = new QLabel(title, this);
00220 _title->setAlignment(AlignCenter);
00221 top->addMultiCellWidget(_title, 0, 0, 0, 1, AlignCenter);
00222 }
00223
00224 void KGameLCDList::append(QLCDNumber *lcd)
00225 {
00226 append(QString::null, lcd);
00227 }
00228
00229 void KGameLCDList::append(const QString &leading, QLCDNumber *lcd)
00230 {
00231 uint i = size();
00232 QLabel *label = 0;
00233 if ( !leading.isEmpty() ) {
00234 label = new QLabel(leading, this);
00235 static_cast<QGridLayout *>(layout())->addWidget(label, i+1, 0);
00236 }
00237 d->_leadings.push_back(label);
00238 _lcds.push_back(lcd);
00239 static_cast<QGridLayout *>(layout())->addWidget(lcd, i+1, 1);
00240 }
00241
00242 void KGameLCDList::clear()
00243 {
00244 for (uint i=0; i<_lcds.size(); i++) {
00245 delete d->_leadings[i];
00246 delete _lcds[i];
00247 }
00248 d->_leadings.clear();
00249 _lcds.clear();
00250 }