Qt 4.8
qlcdnumber.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qlcdnumber.h"
43 #ifndef QT_NO_LCDNUMBER
44 #include "qbitarray.h"
45 #include "qpainter.h"
46 #include "private/qframe_p.h"
47 
49 
51 {
53 public:
54  void init();
55  void internalSetString(const QString& s);
56  void drawString(const QString& s, QPainter &, QBitArray * = 0, bool = true);
57  //void drawString(const QString &, QPainter &, QBitArray * = 0) const;
58  void drawDigit(const QPoint &, QPainter &, int, char, char = ' ');
59  void drawSegment(const QPoint &, char, QPainter &, int, bool = false);
60 
61  int ndigits;
62  double val;
63  uint base : 2;
65  uint fill : 1;
66  uint shadow : 1;
69 };
70 
178 static QString int2string(int num, int base, int ndigits, bool *oflow)
179 {
180  QString s;
181  bool negative;
182  if (num < 0) {
183  negative = true;
184  num = -num;
185  } else {
186  negative = false;
187  }
188  switch(base) {
189  case QLCDNumber::Hex:
190  s.sprintf("%*x", ndigits, num);
191  break;
192  case QLCDNumber::Dec:
193  s.sprintf("%*i", ndigits, num);
194  break;
195  case QLCDNumber::Oct:
196  s.sprintf("%*o", ndigits, num);
197  break;
198  case QLCDNumber::Bin:
199  {
200  char buf[42];
201  char *p = &buf[41];
202  uint n = num;
203  int len = 0;
204  *p = '\0';
205  do {
206  *--p = (char)((n&1)+'0');
207  n >>= 1;
208  len++;
209  } while (n != 0);
210  len = ndigits - len;
211  if (len > 0)
212  s.fill(QLatin1Char(' '), len);
213  s += QString::fromLatin1(p);
214  }
215  break;
216  }
217  if (negative) {
218  for (int i=0; i<(int)s.length(); i++) {
219  if (s[i] != QLatin1Char(' ')) {
220  if (i != 0) {
221  s[i-1] = QLatin1Char('-');
222  } else {
223  s.insert(0, QLatin1Char('-'));
224  }
225  break;
226  }
227  }
228  }
229  if (oflow)
230  *oflow = (int)s.length() > ndigits;
231  return s;
232 }
233 
234 
235 static QString double2string(double num, int base, int ndigits, bool *oflow)
236 {
237  QString s;
238  if (base != QLCDNumber::Dec) {
239  bool of = num >= 2147483648.0 || num < -2147483648.0;
240  if (of) { // oops, integer overflow
241  if (oflow)
242  *oflow = true;
243  return s;
244  }
245  s = int2string((int)num, base, ndigits, 0);
246  } else { // decimal base
247  int nd = ndigits;
248  do {
249  s.sprintf("%*.*g", ndigits, nd, num);
250  int i = s.indexOf(QLatin1Char('e'));
251  if (i > 0 && s[i+1]==QLatin1Char('+')) {
252  s[i] = QLatin1Char(' ');
253  s[i+1] = QLatin1Char('e');
254  }
255  } while (nd-- && (int)s.length() > ndigits);
256  }
257  if (oflow)
258  *oflow = (int)s.length() > ndigits;
259  return s;
260 }
261 
262 
263 static const char *getSegments(char ch) // gets list of segments for ch
264 {
265  static const char segments[30][8] =
266  { { 0, 1, 2, 4, 5, 6,99, 0}, // 0 0 / O
267  { 2, 5,99, 0, 0, 0, 0, 0}, // 1 1
268  { 0, 2, 3, 4, 6,99, 0, 0}, // 2 2
269  { 0, 2, 3, 5, 6,99, 0, 0}, // 3 3
270  { 1, 2, 3, 5,99, 0, 0, 0}, // 4 4
271  { 0, 1, 3, 5, 6,99, 0, 0}, // 5 5 / S
272  { 0, 1, 3, 4, 5, 6,99, 0}, // 6 6
273  { 0, 2, 5,99, 0, 0, 0, 0}, // 7 7
274  { 0, 1, 2, 3, 4, 5, 6,99}, // 8 8
275  { 0, 1, 2, 3, 5, 6,99, 0}, // 9 9 / g
276  { 3,99, 0, 0, 0, 0, 0, 0}, // 10 -
277  { 7,99, 0, 0, 0, 0, 0, 0}, // 11 .
278  { 0, 1, 2, 3, 4, 5,99, 0}, // 12 A
279  { 1, 3, 4, 5, 6,99, 0, 0}, // 13 B
280  { 0, 1, 4, 6,99, 0, 0, 0}, // 14 C
281  { 2, 3, 4, 5, 6,99, 0, 0}, // 15 D
282  { 0, 1, 3, 4, 6,99, 0, 0}, // 16 E
283  { 0, 1, 3, 4,99, 0, 0, 0}, // 17 F
284  { 1, 3, 4, 5,99, 0, 0, 0}, // 18 h
285  { 1, 2, 3, 4, 5,99, 0, 0}, // 19 H
286  { 1, 4, 6,99, 0, 0, 0, 0}, // 20 L
287  { 3, 4, 5, 6,99, 0, 0, 0}, // 21 o
288  { 0, 1, 2, 3, 4,99, 0, 0}, // 22 P
289  { 3, 4,99, 0, 0, 0, 0, 0}, // 23 r
290  { 4, 5, 6,99, 0, 0, 0, 0}, // 24 u
291  { 1, 2, 4, 5, 6,99, 0, 0}, // 25 U
292  { 1, 2, 3, 5, 6,99, 0, 0}, // 26 Y
293  { 8, 9,99, 0, 0, 0, 0, 0}, // 27 :
294  { 0, 1, 2, 3,99, 0, 0, 0}, // 28 '
295  {99, 0, 0, 0, 0, 0, 0, 0} }; // 29 empty
296 
297  if (ch >= '0' && ch <= '9')
298  return segments[ch - '0'];
299  if (ch >= 'A' && ch <= 'F')
300  return segments[ch - 'A' + 12];
301  if (ch >= 'a' && ch <= 'f')
302  return segments[ch - 'a' + 12];
303 
304  int n;
305  switch (ch) {
306  case '-':
307  n = 10; break;
308  case 'O':
309  n = 0; break;
310  case 'g':
311  n = 9; break;
312  case '.':
313  n = 11; break;
314  case 'h':
315  n = 18; break;
316  case 'H':
317  n = 19; break;
318  case 'l':
319  case 'L':
320  n = 20; break;
321  case 'o':
322  n = 21; break;
323  case 'p':
324  case 'P':
325  n = 22; break;
326  case 'r':
327  case 'R':
328  n = 23; break;
329  case 's':
330  case 'S':
331  n = 5; break;
332  case 'u':
333  n = 24; break;
334  case 'U':
335  n = 25; break;
336  case 'y':
337  case 'Y':
338  n = 26; break;
339  case ':':
340  n = 27; break;
341  case '\'':
342  n = 28; break;
343  default:
344  n = 29; break;
345  }
346  return segments[n];
347 }
348 
349 
350 #ifdef QT3_SUPPORT
351 
366  : QFrame(*new QLCDNumberPrivate, parent)
367 {
368  setObjectName(QString::fromAscii(name));
369  Q_D(QLCDNumber);
370  d->ndigits = 5;
371  d->init();
372 }
373 
374 
390 QLCDNumber::QLCDNumber(uint numDigits, QWidget *parent, const char *name)
391  : QFrame(*new QLCDNumberPrivate, parent)
392 {
393  setObjectName(QString::fromAscii(name));
394  Q_D(QLCDNumber);
395  d->ndigits = numDigits;
396  d->init();
397 }
398 #endif //QT3_SUPPORT
399 
411  : QFrame(*new QLCDNumberPrivate, parent)
412 {
413  Q_D(QLCDNumber);
414  d->ndigits = 5;
415  d->init();
416 }
417 
418 
431  : QFrame(*new QLCDNumberPrivate, parent)
432 {
433  Q_D(QLCDNumber);
434  d->ndigits = numDigits;
435  d->init();
436 }
437 
439 {
440  Q_Q(QLCDNumber);
441 
442  q->setFrameStyle(QFrame::Box | QFrame::Raised);
443  val = 0;
445  smallPoint = false;
446  q->setDigitCount(ndigits);
447  q->setSegmentStyle(QLCDNumber::Filled);
449 }
450 
456 {
457 }
458 
459 
471 {
472  setDigitCount(numDigits);
473 }
474 
496 {
497  Q_D(QLCDNumber);
498  if (numDigits > 99) {
499  qWarning("QLCDNumber::setNumDigits: (%s) Max 99 digits allowed",
500  objectName().toLocal8Bit().constData());
501  numDigits = 99;
502  }
503  if (numDigits < 0) {
504  qWarning("QLCDNumber::setNumDigits: (%s) Min 0 digits allowed",
505  objectName().toLocal8Bit().constData());
506  numDigits = 0;
507  }
508  if (d->digitStr.isNull()) { // from constructor
509  d->ndigits = numDigits;
510  d->digitStr.fill(QLatin1Char(' '), d->ndigits);
511  d->points.fill(0, d->ndigits);
512  d->digitStr[d->ndigits - 1] = QLatin1Char('0'); // "0" is the default number
513  } else {
514  bool doDisplay = d->ndigits == 0;
515  if (numDigits == d->ndigits) // no change
516  return;
517  register int i;
518  int dif;
519  if (numDigits > d->ndigits) { // expand
520  dif = numDigits - d->ndigits;
521  QString buf;
522  buf.fill(QLatin1Char(' '), dif);
523  d->digitStr.insert(0, buf);
524  d->points.resize(numDigits);
525  for (i=numDigits-1; i>=dif; i--)
526  d->points.setBit(i, d->points.testBit(i-dif));
527  for (i=0; i<dif; i++)
528  d->points.clearBit(i);
529  } else { // shrink
530  dif = d->ndigits - numDigits;
531  d->digitStr = d->digitStr.right(numDigits);
532  QBitArray tmpPoints = d->points;
533  d->points.resize(numDigits);
534  for (i=0; i<(int)numDigits; i++)
535  d->points.setBit(i, tmpPoints.testBit(i+dif));
536  }
537  d->ndigits = numDigits;
538  if (doDisplay)
539  display(value());
540  update();
541  }
542 }
543 
544 int QLCDNumber::numDigits() const
545 {
546  Q_D(const QLCDNumber);
547  return d->ndigits;
548 }
549 
553 int QLCDNumber::digitCount() const
554 {
555  Q_D(const QLCDNumber);
556  return d->ndigits;
557 }
558 
571 bool QLCDNumber::checkOverflow(int num) const
572 {
573  Q_D(const QLCDNumber);
574  bool of;
575  int2string(num, d->base, d->ndigits, &of);
576  return of;
577 }
578 
579 
587 bool QLCDNumber::checkOverflow(double num) const
588 {
589  Q_D(const QLCDNumber);
590  bool of;
591  double2string(num, d->base, d->ndigits, &of);
592  return of;
593 }
594 
595 
612 {
613  Q_D(const QLCDNumber);
614  return (QLCDNumber::Mode) d->base;
615 }
616 
618 {
619  Q_D(QLCDNumber);
620  d->base = m;
621  display(d->val);
622 }
623 
624 
641 double QLCDNumber::value() const
642 {
643  Q_D(const QLCDNumber);
644  return d->val;
645 }
646 
655 void QLCDNumber::display(double num)
656 {
657  Q_D(QLCDNumber);
658  d->val = num;
659  bool of;
660  QString s = double2string(d->val, d->base, d->ndigits, &of);
661  if (of)
662  emit overflow();
663  else
664  d->internalSetString(s);
665 }
666 
683 int QLCDNumber::intValue() const
684 {
685  Q_D(const QLCDNumber);
686  return qRound(d->val);
687 }
688 
689 
698 void QLCDNumber::display(int num)
699 {
700  Q_D(QLCDNumber);
701  d->val = (double)num;
702  bool of;
703  QString s = int2string(num, d->base, d->ndigits, &of);
704  if (of)
705  emit overflow();
706  else
707  d->internalSetString(s);
708 }
709 
710 
725 {
726  Q_D(QLCDNumber);
727  d->val = 0;
728  bool ok = false;
729  double v = s.toDouble(&ok);
730  if (ok)
731  d->val = v;
732  d->internalSetString(s);
733 }
734 
743 {
744  setMode(Hex);
745 }
746 
747 
756 {
757  setMode(Dec);
758 }
759 
760 
769 {
770  setMode(Oct);
771 }
772 
773 
782 {
783  setMode(Bin);
784 }
785 
786 
805 {
806  Q_D(QLCDNumber);
807  d->smallPoint = b;
808  update();
809 }
810 
812 {
813  Q_D(const QLCDNumber);
814  return d->smallPoint;
815 }
816 
817 
818 
824 {
825  Q_D(QLCDNumber);
826  QPainter p(this);
827  drawFrame(&p);
829  if (d->shadow)
830  p.translate(0.5, 0.5);
831 
832  if (d->smallPoint)
833  d->drawString(d->digitStr, p, &d->points, false);
834  else
835  d->drawString(d->digitStr, p, 0, false);
836 }
837 
838 
840 {
841  Q_Q(QLCDNumber);
842  QString buffer;
843  int i;
844  int len = s.length();
845  QBitArray newPoints(ndigits);
846 
847  if (!smallPoint) {
848  if (len == ndigits)
849  buffer = s;
850  else
851  buffer = s.right(ndigits).rightJustified(ndigits, QLatin1Char(' '));
852  } else {
853  int index = -1;
854  bool lastWasPoint = true;
855  newPoints.clearBit(0);
856  for (i=0; i<len; i++) {
857  if (s[i] == QLatin1Char('.')) {
858  if (lastWasPoint) { // point already set for digit?
859  if (index == ndigits - 1) // no more digits
860  break;
861  index++;
862  buffer[index] = QLatin1Char(' '); // 2 points in a row, add space
863  }
864  newPoints.setBit(index); // set decimal point
865  lastWasPoint = true;
866  } else {
867  if (index == ndigits - 1)
868  break;
869  index++;
870  buffer[index] = s[i];
871  newPoints.clearBit(index); // decimal point default off
872  lastWasPoint = false;
873  }
874  }
875  if (index < ((int) ndigits) - 1) {
876  for(i=index; i>=0; i--) {
877  buffer[ndigits - 1 - index + i] = buffer[i];
878  newPoints.setBit(ndigits - 1 - index + i,
879  newPoints.testBit(i));
880  }
881  for(i=0; i<ndigits-index-1; i++) {
882  buffer[i] = QLatin1Char(' ');
883  newPoints.clearBit(i);
884  }
885  }
886  }
887 
888  if (buffer == digitStr)
889  return;
890 
891  digitStr = buffer;
892  if (smallPoint)
893  points = newPoints;
894  q->update();
895 }
896 
902  QBitArray *newPoints, bool newString)
903 {
904  Q_Q(QLCDNumber);
905  QPoint pos;
906 
907  int digitSpace = smallPoint ? 2 : 1;
908  int xSegLen = q->width()*5/(ndigits*(5 + digitSpace) + digitSpace);
909  int ySegLen = q->height()*5/12;
910  int segLen = ySegLen > xSegLen ? xSegLen : ySegLen;
911  int xAdvance = segLen*(5 + digitSpace)/5;
912  int xOffset = (q->width() - ndigits*xAdvance + segLen/5)/2;
913  int yOffset = (q->height() - segLen*2)/2;
914 
915  for (int i=0; i<ndigits; i++) {
916  pos = QPoint(xOffset + xAdvance*i, yOffset);
917  if (newString)
918  drawDigit(pos, p, segLen, s[i].toLatin1(), digitStr[i].toLatin1());
919  else
920  drawDigit(pos, p, segLen, s[i].toLatin1());
921  if (newPoints) {
922  char newPoint = newPoints->testBit(i) ? '.' : ' ';
923  if (newString) {
924  char oldPoint = points.testBit(i) ? '.' : ' ';
925  drawDigit(pos, p, segLen, newPoint, oldPoint);
926  } else {
927  drawDigit(pos, p, segLen, newPoint);
928  }
929  }
930  }
931  if (newString) {
932  digitStr = s;
933  digitStr.truncate(ndigits);
934  if (newPoints)
935  points = *newPoints;
936  }
937 }
938 
939 
944 void QLCDNumberPrivate::drawDigit(const QPoint &pos, QPainter &p, int segLen,
945  char newCh, char oldCh)
946 {
947 // Draws and/or erases segments to change display of a single digit
948 // from oldCh to newCh
949 
950  char updates[18][2]; // can hold 2 times number of segments, only
951  // first 9 used if segment table is correct
952  int nErases;
953  int nUpdates;
954  const char *segs;
955  int i,j;
956 
957  const char erase = 0;
958  const char draw = 1;
959  const char leaveAlone = 2;
960 
961  segs = getSegments(oldCh);
962  for (nErases=0; segs[nErases] != 99; nErases++) {
963  updates[nErases][0] = erase; // get segments to erase to
964  updates[nErases][1] = segs[nErases]; // remove old char
965  }
966  nUpdates = nErases;
967  segs = getSegments(newCh);
968  for(i = 0 ; segs[i] != 99 ; i++) {
969  for (j=0; j<nErases; j++)
970  if (segs[i] == updates[j][1]) { // same segment ?
971  updates[j][0] = leaveAlone; // yes, already on screen
972  break;
973  }
974  if (j == nErases) { // if not already on screen
975  updates[nUpdates][0] = draw;
976  updates[nUpdates][1] = segs[i];
977  nUpdates++;
978  }
979  }
980  for (i=0; i<nUpdates; i++) {
981  if (updates[i][0] == draw)
982  drawSegment(pos, updates[i][1], p, segLen);
983  if (updates[i][0] == erase)
984  drawSegment(pos, updates[i][1], p, segLen, true);
985  }
986 }
987 
988 
989 static void addPoint(QPolygon &a, const QPoint &p)
990 {
991  uint n = a.size();
992  a.resize(n + 1);
993  a.setPoint(n, p);
994 }
995 
1000 void QLCDNumberPrivate::drawSegment(const QPoint &pos, char segmentNo, QPainter &p,
1001  int segLen, bool erase)
1002 {
1003  Q_Q(QLCDNumber);
1004  QPoint ppt;
1005  QPoint pt = pos;
1006  int width = segLen/5;
1007 
1008  const QPalette &pal = q->palette();
1009  QColor lightColor,darkColor,fgColor;
1010  if (erase){
1011  lightColor = pal.color(q->backgroundRole());
1012  darkColor = lightColor;
1013  fgColor = lightColor;
1014  } else {
1015  lightColor = pal.light().color();
1016  darkColor = pal.dark().color();
1017  fgColor = pal.color(q->foregroundRole());
1018  }
1019 
1020 
1021 #define LINETO(X,Y) addPoint(a, QPoint(pt.x() + (X),pt.y() + (Y)))
1022 #define LIGHT
1023 #define DARK
1024 
1025  if (fill) {
1026  QPolygon a(0);
1027  //The following is an exact copy of the switch below.
1028  //don't make any changes here
1029  switch (segmentNo) {
1030  case 0 :
1031  ppt = pt;
1032  LIGHT;
1033  LINETO(segLen - 1,0);
1034  DARK;
1035  LINETO(segLen - width - 1,width);
1036  LINETO(width,width);
1037  LINETO(0,0);
1038  break;
1039  case 1 :
1040  pt += QPoint(0 , 1);
1041  ppt = pt;
1042  LIGHT;
1043  LINETO(width,width);
1044  DARK;
1045  LINETO(width,segLen - width/2 - 2);
1046  LINETO(0,segLen - 2);
1047  LIGHT;
1048  LINETO(0,0);
1049  break;
1050  case 2 :
1051  pt += QPoint(segLen - 1 , 1);
1052  ppt = pt;
1053  DARK;
1054  LINETO(0,segLen - 2);
1055  LINETO(-width,segLen - width/2 - 2);
1056  LIGHT;
1057  LINETO(-width,width);
1058  LINETO(0,0);
1059  break;
1060  case 3 :
1061  pt += QPoint(0 , segLen);
1062  ppt = pt;
1063  LIGHT;
1064  LINETO(width,-width/2);
1065  LINETO(segLen - width - 1,-width/2);
1066  LINETO(segLen - 1,0);
1067  DARK;
1068  if (width & 1) { // adjust for integer division error
1069  LINETO(segLen - width - 3,width/2 + 1);
1070  LINETO(width + 2,width/2 + 1);
1071  } else {
1072  LINETO(segLen - width - 1,width/2);
1073  LINETO(width,width/2);
1074  }
1075  LINETO(0,0);
1076  break;
1077  case 4 :
1078  pt += QPoint(0 , segLen + 1);
1079  ppt = pt;
1080  LIGHT;
1081  LINETO(width,width/2);
1082  DARK;
1083  LINETO(width,segLen - width - 2);
1084  LINETO(0,segLen - 2);
1085  LIGHT;
1086  LINETO(0,0);
1087  break;
1088  case 5 :
1089  pt += QPoint(segLen - 1 , segLen + 1);
1090  ppt = pt;
1091  DARK;
1092  LINETO(0,segLen - 2);
1093  LINETO(-width,segLen - width - 2);
1094  LIGHT;
1095  LINETO(-width,width/2);
1096  LINETO(0,0);
1097  break;
1098  case 6 :
1099  pt += QPoint(0 , segLen*2);
1100  ppt = pt;
1101  LIGHT;
1102  LINETO(width,-width);
1103  LINETO(segLen - width - 1,-width);
1104  LINETO(segLen - 1,0);
1105  DARK;
1106  LINETO(0,0);
1107  break;
1108  case 7 :
1109  if (smallPoint) // if smallpoint place'.' between other digits
1110  pt += QPoint(segLen + width/2 , segLen*2);
1111  else
1112  pt += QPoint(segLen/2 , segLen*2);
1113  ppt = pt;
1114  DARK;
1115  LINETO(width,0);
1116  LINETO(width,-width);
1117  LIGHT;
1118  LINETO(0,-width);
1119  LINETO(0,0);
1120  break;
1121  case 8 :
1122  pt += QPoint(segLen/2 - width/2 + 1 , segLen/2 + width);
1123  ppt = pt;
1124  DARK;
1125  LINETO(width,0);
1126  LINETO(width,-width);
1127  LIGHT;
1128  LINETO(0,-width);
1129  LINETO(0,0);
1130  break;
1131  case 9 :
1132  pt += QPoint(segLen/2 - width/2 + 1 , 3*segLen/2 + width);
1133  ppt = pt;
1134  DARK;
1135  LINETO(width,0);
1136  LINETO(width,-width);
1137  LIGHT;
1138  LINETO(0,-width);
1139  LINETO(0,0);
1140  break;
1141  default :
1142  qWarning("QLCDNumber::drawSegment: (%s) Illegal segment id: %d\n",
1143  q->objectName().toLocal8Bit().constData(), segmentNo);
1144  }
1145  // End exact copy
1146  p.setPen(Qt::NoPen);
1147  p.setBrush(fgColor);
1148  p.drawPolygon(a);
1149  p.setBrush(Qt::NoBrush);
1150 
1151  pt = pos;
1152  }
1153 #undef LINETO
1154 #undef LIGHT
1155 #undef DARK
1156 
1157 #define LINETO(X,Y) p.drawLine(ppt.x(), ppt.y(), pt.x()+(X), pt.y()+(Y)); \
1158  ppt = QPoint(pt.x()+(X), pt.y()+(Y))
1159 #define LIGHT p.setPen(lightColor)
1160 #define DARK p.setPen(darkColor)
1161  if (shadow)
1162  switch (segmentNo) {
1163  case 0 :
1164  ppt = pt;
1165  LIGHT;
1166  LINETO(segLen - 1,0);
1167  DARK;
1168  LINETO(segLen - width - 1,width);
1169  LINETO(width,width);
1170  LINETO(0,0);
1171  break;
1172  case 1 :
1173  pt += QPoint(0,1);
1174  ppt = pt;
1175  LIGHT;
1176  LINETO(width,width);
1177  DARK;
1178  LINETO(width,segLen - width/2 - 2);
1179  LINETO(0,segLen - 2);
1180  LIGHT;
1181  LINETO(0,0);
1182  break;
1183  case 2 :
1184  pt += QPoint(segLen - 1 , 1);
1185  ppt = pt;
1186  DARK;
1187  LINETO(0,segLen - 2);
1188  LINETO(-width,segLen - width/2 - 2);
1189  LIGHT;
1190  LINETO(-width,width);
1191  LINETO(0,0);
1192  break;
1193  case 3 :
1194  pt += QPoint(0 , segLen);
1195  ppt = pt;
1196  LIGHT;
1197  LINETO(width,-width/2);
1198  LINETO(segLen - width - 1,-width/2);
1199  LINETO(segLen - 1,0);
1200  DARK;
1201  if (width & 1) { // adjust for integer division error
1202  LINETO(segLen - width - 3,width/2 + 1);
1203  LINETO(width + 2,width/2 + 1);
1204  } else {
1205  LINETO(segLen - width - 1,width/2);
1206  LINETO(width,width/2);
1207  }
1208  LINETO(0,0);
1209  break;
1210  case 4 :
1211  pt += QPoint(0 , segLen + 1);
1212  ppt = pt;
1213  LIGHT;
1214  LINETO(width,width/2);
1215  DARK;
1216  LINETO(width,segLen - width - 2);
1217  LINETO(0,segLen - 2);
1218  LIGHT;
1219  LINETO(0,0);
1220  break;
1221  case 5 :
1222  pt += QPoint(segLen - 1 , segLen + 1);
1223  ppt = pt;
1224  DARK;
1225  LINETO(0,segLen - 2);
1226  LINETO(-width,segLen - width - 2);
1227  LIGHT;
1228  LINETO(-width,width/2);
1229  LINETO(0,0);
1230  break;
1231  case 6 :
1232  pt += QPoint(0 , segLen*2);
1233  ppt = pt;
1234  LIGHT;
1235  LINETO(width,-width);
1236  LINETO(segLen - width - 1,-width);
1237  LINETO(segLen - 1,0);
1238  DARK;
1239  LINETO(0,0);
1240  break;
1241  case 7 :
1242  if (smallPoint) // if smallpoint place'.' between other digits
1243  pt += QPoint(segLen + width/2 , segLen*2);
1244  else
1245  pt += QPoint(segLen/2 , segLen*2);
1246  ppt = pt;
1247  DARK;
1248  LINETO(width,0);
1249  LINETO(width,-width);
1250  LIGHT;
1251  LINETO(0,-width);
1252  LINETO(0,0);
1253  break;
1254  case 8 :
1255  pt += QPoint(segLen/2 - width/2 + 1 , segLen/2 + width);
1256  ppt = pt;
1257  DARK;
1258  LINETO(width,0);
1259  LINETO(width,-width);
1260  LIGHT;
1261  LINETO(0,-width);
1262  LINETO(0,0);
1263  break;
1264  case 9 :
1265  pt += QPoint(segLen/2 - width/2 + 1 , 3*segLen/2 + width);
1266  ppt = pt;
1267  DARK;
1268  LINETO(width,0);
1269  LINETO(width,-width);
1270  LIGHT;
1271  LINETO(0,-width);
1272  LINETO(0,0);
1273  break;
1274  default :
1275  qWarning("QLCDNumber::drawSegment: (%s) Illegal segment id: %d\n",
1276  q->objectName().toLocal8Bit().constData(), segmentNo);
1277  }
1278 
1279 #undef LINETO
1280 #undef LIGHT
1281 #undef DARK
1282 }
1283 
1284 
1285 
1308 {
1309  Q_D(QLCDNumber);
1310  d->fill = (s == Flat || s == Filled);
1311  d->shadow = (s == Outline || s == Filled);
1312  update();
1313 }
1314 
1316 {
1317  Q_D(const QLCDNumber);
1318  Q_ASSERT(d->fill || d->shadow);
1319  if (!d->fill && d->shadow)
1320  return Outline;
1321  if (d->fill && d->shadow)
1322  return Filled;
1323  return Flat;
1324 }
1325 
1326 
1330 {
1331  return QSize(10 + 9 * (digitCount() + (smallDecimalPoint() ? 0 : 1)), 23);
1332 }
1333 
1336 {
1337  return QFrame::event(e);
1338 }
1339 
1363 
1364 #endif // QT_NO_LCDNUMBER
QPoint pos() const
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:86
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
double d
Definition: qnumeric_p.h:62
void drawString(const QString &s, QPainter &, QBitArray *=0, bool=true)
Definition: qlcdnumber.cpp:901
QString & sprintf(const char *format,...)
Safely builds a formatted string from the format string cformat and an arbitrary list of arguments...
Definition: qstring.cpp:5567
QT_DEPRECATED void setNumDigits(int nDigits)
Definition: qlcdnumber.cpp:470
QT_DEPRECATED int numDigits() const
#define LINETO(X, Y)
void setBit(int i)
Sets the bit at index position i to 1.
Definition: qbitarray.h:128
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
const QColor & color() const
Returns the brush color.
Definition: qbrush.h:183
static QString double2string(double num, int base, int ndigits, bool *oflow)
Definition: qlcdnumber.cpp:235
static QString fromAscii(const char *, int size=-1)
Returns a QString initialized with the first size characters from the string str. ...
Definition: qstring.cpp:4276
const QBrush & dark() const
Returns the dark brush of the current color group.
Definition: qpalette.h:127
void setMode(Mode)
Definition: qlcdnumber.cpp:617
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
void internalSetString(const QString &s)
Definition: qlcdnumber.cpp:839
static void addPoint(QPolygon &a, const QPoint &p)
Definition: qlcdnumber.cpp:989
bool event(QEvent *e)
Reimplemented Function
Mode
This type determines how numbers are shown.
Definition: qlcdnumber.h:74
long ASN1_INTEGER_get ASN1_INTEGER * a
The QPolygon class provides a vector of points using integer precision.
Definition: qpolygon.h:60
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool testBit(int i) const
Returns true if the bit at index position i is 1; otherwise returns false.
Definition: qbitarray.h:124
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
#define Q_D(Class)
Definition: qglobal.h:2482
const QColor & color(ColorGroup cg, ColorRole cr) const
Returns the color in the specified color group, used for the given color role.
Definition: qpalette.h:107
static const uint base
Definition: qurl.cpp:268
void overflow()
This signal is emitted whenever the QLCDNumber is asked to display a too-large number or a too-long s...
~QLCDNumber()
Destroys the LCD number.
Definition: qlcdnumber.cpp:455
void resize(int size)
Sets the size of the vector to size.
Definition: qvector.h:342
#define Q_Q(Class)
Definition: qglobal.h:2483
void update()
Updates the widget unless updates are disabled or the widget is hidden.
Definition: qwidget.cpp:10883
QString rightJustified(int width, QChar fill=QLatin1Char(' '), bool trunc=false) const Q_REQUIRED_RESULT
Returns a string of size() width that contains the fill character followed by the string...
Definition: qstring.cpp:5357
void setRenderHint(RenderHint hint, bool on=true)
Sets the given render hint on the painter if on is true; otherwise clears the render hint...
Definition: qpainter.cpp:7620
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void setSmallDecimalPoint(bool)
Definition: qlcdnumber.cpp:804
void truncate(int pos)
Truncates the string at the given position index.
Definition: qstring.cpp:4603
const QBrush & light() const
Returns the light brush of the current color group.
Definition: qpalette.h:126
int width() const
static int numDigits(qlonglong n)
Definition: qvalidator.cpp:385
const char * name
#define emit
Definition: qobjectdefs.h:76
void setBinMode()
Calls setMode(Bin).
Definition: qlcdnumber.cpp:781
Q_CORE_EXPORT void qWarning(const char *,...)
unsigned int uint
Definition: qglobal.h:996
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
SegmentStyle segmentStyle() const
void drawPolygon(const QPointF *points, int pointCount, Qt::FillRule fillRule=Qt::OddEvenFill)
Draws the polygon defined by the first pointCount points in the array points using the current pen an...
Definition: qpainter.cpp:5205
void drawFrame(QPainter *)
em>Reimplemented Function
Definition: qframe.cpp:538
QString right(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n rightmost characters of the string.
Definition: qstring.cpp:3682
Mode mode() const
double value() const
QSize sizeHint() const
em>Reimplemented Function
void setOctMode()
Calls setMode(Oct).
Definition: qlcdnumber.cpp:768
void setHexMode()
Calls setMode(Hex).
Definition: qlcdnumber.cpp:742
bool smallDecimalPoint() const
The QBitArray class provides an array of bits.
Definition: qbitarray.h:54
bool checkOverflow(double num) const
Returns true if num is too big to be displayed in its entirety; otherwise returns false...
Definition: qlcdnumber.cpp:587
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
void drawDigit(const QPoint &, QPainter &, int, char, char=' ')
Definition: qlcdnumber.cpp:944
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
static const char * getSegments(char ch)
Definition: qlcdnumber.cpp:263
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
bool event(QEvent *e)
Reimplemented Function
Definition: qframe.cpp:587
void setBrush(const QBrush &brush)
Sets the painter&#39;s brush to the given brush.
Definition: qpainter.cpp:4171
double toDouble(bool *ok=0) const
Returns the string converted to a double value.
Definition: qstring.cpp:6227
void setPen(const QColor &color)
Sets the painter&#39;s pen to have style Qt::SolidLine, width 0 and the specified color.
Definition: qpainter.cpp:4047
QString & fill(QChar c, int size=-1)
Sets every character in the string to character ch.
Definition: qstring.cpp:4641
QString objectName() const
void setDigitCount(int nDigits)
Sets the current number of digits to numDigits.
Definition: qlcdnumber.cpp:495
void setPoint(int index, int x, int y)
Sets the point at the given index to the point specified by ({x}, {y}).
Definition: qpolygon.h:120
QBitArray points
Definition: qlcdnumber.cpp:68
The QLCDNumber widget displays a number with LCD-like digits.
Definition: qlcdnumber.h:57
SegmentStyle
This type determines the visual appearance of the QLCDNumber widget.
Definition: qlcdnumber.h:80
#define LIGHT
quint16 index
QObject * parent
Definition: qobject.h:92
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
int intValue() const
QLCDNumber(QWidget *parent=0)
Constructs an LCD number, sets the number of digits to 5, the base to decimal, the decimal point mode...
Definition: qlcdnumber.cpp:410
#define DARK
void setSegmentStyle(SegmentStyle)
void drawSegment(const QPoint &, char, QPainter &, int, bool=false)
void paintEvent(QPaintEvent *)
This event handler can be reimplemented in a subclass to receive paint events passed in event...
Definition: qlcdnumber.cpp:823
int digitCount() const
The QPaintEvent class contains event parameters for paint events.
Definition: qevent.h:298
void setDecMode()
Calls setMode(Dec).
Definition: qlcdnumber.cpp:755
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
The QFrame class is the base class of widgets that can have a frame.
Definition: qframe.h:55
QString & insert(int i, QChar c)
Definition: qstring.cpp:1671
void clearBit(int i)
Sets the bit at index position i to 0.
Definition: qbitarray.h:132
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137
void display(const QString &str)
Displays the number represented by the string s.
Definition: qlcdnumber.cpp:724
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
static QString int2string(int num, int base, int ndigits, bool *oflow)
Definition: qlcdnumber.cpp:178
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
void translate(const QPointF &offset)
Translates the coordinate system by the given offset; i.e.
Definition: qpainter.cpp:3311
The QPalette class contains color groups for each widget state.
Definition: qpalette.h:61