libkdegames
kgamelcd.cpp
Go to the documentation of this file.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>
00024 #include <QLabel>
00025 #include <QTimer>
00026
00027 #include <kglobal.h>
00028
00029
00030
00031 KGameLCD::KGameLCD(uint nbDigits, QWidget *parent)
00032 : QLCDNumber(nbDigits, parent), _htime(800)
00033 {
00034 const QPalette &p = palette();
00035 _fgColor = p.color(QPalette::Active, QPalette::Foreground);
00036 _hlColor = p.color(QPalette::Active, QPalette::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(QPalette::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(QPalette::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(QPalette::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).rightJustified(n));
00098 }
00099
00100 void KGameLCD::highlight()
00101 {
00102 highlight(true);
00103 _timer->setSingleShot(true);
00104 _timer->start(_htime);
00105 }
00106
00107 void KGameLCD::highlight(bool light)
00108 {
00109 if (light) setColor(_hlColor);
00110 else resetColor();
00111 }
00112
00113
00114 KGameLCDClock::KGameLCDClock(QWidget *parent)
00115 : KGameLCD(5, parent)
00116 {
00117 _timerClock = new QTimer(this);
00118 connect(_timerClock, SIGNAL(timeout()), SLOT(timeoutClock()));
00119 }
00120
00121 KGameLCDClock::~KGameLCDClock()
00122 {}
00123
00124 void KGameLCDClock::timeoutClock()
00125 {
00126
00127 if ( _min==59 && _sec==59 ) return;
00128 _sec++;
00129 if (_sec==60) {
00130 _min++;
00131 _sec = 0;
00132 }
00133 showTime();
00134 }
00135
00136 QString KGameLCDClock::pretty() const
00137 {
00138 QString sec = QString::number(_sec).rightJustified(2, '0', true);
00139 QString min = QString::number(_min).rightJustified(2, '0', true);
00140 return min + ':' + sec;
00141 }
00142
00143 void KGameLCDClock::showTime()
00144 {
00145 display(pretty());
00146 }
00147
00148 void KGameLCDClock::reset()
00149 {
00150 _timerClock->stop();
00151 _sec = 0;
00152 _min = 0;
00153 showTime();
00154 }
00155
00156 void KGameLCDClock::start()
00157 {
00158 _timerClock->start(1000);
00159 }
00160
00161 void KGameLCDClock::stop()
00162 {
00163 _timerClock->stop();
00164 }
00165
00166 uint KGameLCDClock::seconds() const
00167 {
00168 return _min*60 + _sec;
00169 }
00170
00171 void KGameLCDClock::setTime(uint sec)
00172 {
00173 Q_ASSERT( sec<3600 );
00174 _sec = sec % 60;
00175 _min = sec / 60;
00176 showTime();
00177 }
00178
00179 void KGameLCDClock::setTime(const QString &s)
00180 {
00181 Q_ASSERT( s.length()==5 && s[2]==':' );
00182 uint min = qMin(s.section(':', 0, 0).toUInt(), uint(59));
00183 uint sec = qMin(s.section(':', 1, 1).toUInt(), uint(59));
00184 setTime(sec + min*60);
00185 }
00186
00187
00188
00189 class KGameLCDList::KGameLCDListPrivate
00190 {
00191 public:
00192 QVector<QLabel *> _leadings;
00193 };
00194
00195 KGameLCDList::KGameLCDList(const QString &title, QWidget *parent)
00196 : QWidget(parent)
00197 {
00198 init(title);
00199 }
00200
00201 KGameLCDList::KGameLCDList(QWidget *parent)
00202 : QWidget(parent)
00203 {
00204 init(QString());
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);
00217 top->setMargin(5);
00218 top->setColumnStretch(1, 1);
00219
00220 _title = new QLabel(title, this);
00221 _title->setAlignment(Qt::AlignCenter);
00222 top->addWidget(_title, 0, 0, 1, 2, Qt::AlignCenter);
00223 }
00224
00225 void KGameLCDList::append(QLCDNumber *lcd)
00226 {
00227 append(QString(), lcd);
00228 }
00229
00230 void KGameLCDList::append(const QString &leading, QLCDNumber *lcd)
00231 {
00232 uint i = size();
00233 QLabel *label = 0;
00234 if ( !leading.isEmpty() ) {
00235 label = new QLabel(leading, this);
00236 static_cast<QGridLayout *>(layout())->addWidget(label, i+1, 0);
00237 }
00238 d->_leadings.push_back(label);
00239 _lcds.push_back(lcd);
00240 static_cast<QGridLayout *>(layout())->addWidget(lcd, i+1, 1);
00241 }
00242
00243 void KGameLCDList::clear()
00244 {
00245 for (int i=0; i<_lcds.size(); i++) {
00246 delete d->_leadings[i];
00247 delete _lcds[i];
00248 }
00249 d->_leadings.clear();
00250 _lcds.clear();
00251 }