| Line | Source | Count |
| 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 | #include "qlcdnumber.h" | - |
| 41 | #ifndef QT_NO_LCDNUMBER | - |
| 42 | #include "qbitarray.h" | - |
| 43 | #include "qpainter.h" | - |
| 44 | #include "private/qframe_p.h" | - |
| 45 | | - |
| 46 | QT_BEGIN_NAMESPACE | - |
| 47 | | - |
| 48 | class QLCDNumberPrivate : public QFramePrivate | - |
| 49 | { | - |
| 50 | Q_DECLARE_PUBLIC(QLCDNumber) | - |
| 51 | public: | - |
| 52 | void init(); | - |
| 53 | void internalSetString(const QString& s); | - |
| 54 | void drawString(const QString& s, QPainter &, QBitArray * = 0, bool = true); | - |
| 55 | | - |
| 56 | void drawDigit(const QPoint &, QPainter &, int, char, char = ' '); | - |
| 57 | void drawSegment(const QPoint &, char, QPainter &, int, bool = false); | - |
| 58 | | - |
| 59 | int ndigits; | - |
| 60 | double val; | - |
| 61 | uint base : 2; | - |
| 62 | uint smallPoint : 1; | - |
| 63 | uint fill : 1; | - |
| 64 | uint shadow : 1; | - |
| 65 | QString digitStr; | - |
| 66 | QBitArray points; | - |
| 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 | static QString int2string(int num, int base, int ndigits, bool *oflow) | - |
| 158 | { | - |
| 159 | QString s; | - |
| 160 | bool negative; | - |
| 161 | if (num < 0) { | - |
| 162 | negative = true; | - |
| 163 | num = -num; | - |
| 164 | } else { | - |
| 165 | negative = false; | - |
| 166 | } | - |
| 167 | switch(base) { | - |
| 168 | case QLCDNumber::Hex: | - |
| 169 | s = QString::asprintf("%*x", ndigits, num); | - |
| 170 | break; | - |
| 171 | case QLCDNumber::Dec: | - |
| 172 | s = QString::asprintf("%*i", ndigits, num); | - |
| 173 | break; | - |
| 174 | case QLCDNumber::Oct: | - |
| 175 | s = QString::asprintf("%*o", ndigits, num); | - |
| 176 | break; | - |
| 177 | case QLCDNumber::Bin: | - |
| 178 | { | - |
| 179 | char buf[42]; | - |
| 180 | char *p = &buf[41]; | - |
| 181 | uint n = num; | - |
| 182 | int len = 0; | - |
| 183 | *p = '\0'; | - |
| 184 | do { | - |
| 185 | *--p = (char)((n&1)+'0'); | - |
| 186 | n >>= 1; | - |
| 187 | len++; | - |
| 188 | } while (n != 0); | - |
| 189 | len = ndigits - len; | - |
| 190 | if (len > 0) | - |
| 191 | s.fill(QLatin1Char(' '), len); | - |
| 192 | s += QString::fromLatin1(p); | - |
| 193 | } | - |
| 194 | break; | - |
| 195 | } | - |
| 196 | if (negative) { | - |
| 197 | for (int i=0; i<(int)s.length(); i++) { | - |
| 198 | if (s[i] != QLatin1Char(' ')) { | - |
| 199 | if (i != 0) { | - |
| 200 | s[i-1] = QLatin1Char('-'); | - |
| 201 | } else { | - |
| 202 | s.insert(0, QLatin1Char('-')); | - |
| 203 | } | - |
| 204 | break; | - |
| 205 | } | - |
| 206 | } | - |
| 207 | } | - |
| 208 | if (oflow) | - |
| 209 | *oflow = (int)s.length() > ndigits; | - |
| 210 | return s; | - |
| 211 | } | - |
| 212 | | - |
| 213 | | - |
| 214 | static QString double2string(double num, int base, int ndigits, bool *oflow) | - |
| 215 | { | - |
| 216 | QString s; | - |
| 217 | if (base != QLCDNumber::Dec) { | - |
| 218 | bool of = num >= 2147483648.0 || num < -2147483648.0; | - |
| 219 | if (of) { | - |
| 220 | if (oflow) | - |
| 221 | *oflow = true; | - |
| 222 | return s; | - |
| 223 | } | - |
| 224 | s = int2string((int)num, base, ndigits, 0); | - |
| 225 | } else { | - |
| 226 | int nd = ndigits; | - |
| 227 | do { | - |
| 228 | s = QString::asprintf("%*.*g", ndigits, nd, num); | - |
| 229 | int i = s.indexOf(QLatin1Char('e')); | - |
| 230 | if (i > 0 && s[i+1]==QLatin1Char('+')) { | - |
| 231 | s[i] = QLatin1Char(' '); | - |
| 232 | s[i+1] = QLatin1Char('e'); | - |
| 233 | } | - |
| 234 | } while (nd-- && (int)s.length() > ndigits); | - |
| 235 | } | - |
| 236 | if (oflow) | - |
| 237 | *oflow = (int)s.length() > ndigits; | - |
| 238 | return s; | - |
| 239 | } | - |
| 240 | | - |
| 241 | | - |
| 242 | static const char *getSegments(char ch) | - |
| 243 | { | - |
| 244 | static const char segments[30][8] = | - |
| 245 | { { 0, 1, 2, 4, 5, 6,99, 0}, | - |
| 246 | { 2, 5,99, 0, 0, 0, 0, 0}, | - |
| 247 | { 0, 2, 3, 4, 6,99, 0, 0}, | - |
| 248 | { 0, 2, 3, 5, 6,99, 0, 0}, | - |
| 249 | { 1, 2, 3, 5,99, 0, 0, 0}, | - |
| 250 | { 0, 1, 3, 5, 6,99, 0, 0}, | - |
| 251 | { 0, 1, 3, 4, 5, 6,99, 0}, | - |
| 252 | { 0, 2, 5,99, 0, 0, 0, 0}, | - |
| 253 | { 0, 1, 2, 3, 4, 5, 6,99}, | - |
| 254 | { 0, 1, 2, 3, 5, 6,99, 0}, | - |
| 255 | { 3,99, 0, 0, 0, 0, 0, 0}, | - |
| 256 | { 7,99, 0, 0, 0, 0, 0, 0}, | - |
| 257 | { 0, 1, 2, 3, 4, 5,99, 0}, | - |
| 258 | { 1, 3, 4, 5, 6,99, 0, 0}, | - |
| 259 | { 0, 1, 4, 6,99, 0, 0, 0}, | - |
| 260 | { 2, 3, 4, 5, 6,99, 0, 0}, | - |
| 261 | { 0, 1, 3, 4, 6,99, 0, 0}, | - |
| 262 | { 0, 1, 3, 4,99, 0, 0, 0}, | - |
| 263 | { 1, 3, 4, 5,99, 0, 0, 0}, | - |
| 264 | { 1, 2, 3, 4, 5,99, 0, 0}, | - |
| 265 | { 1, 4, 6,99, 0, 0, 0, 0}, | - |
| 266 | { 3, 4, 5, 6,99, 0, 0, 0}, | - |
| 267 | { 0, 1, 2, 3, 4,99, 0, 0}, | - |
| 268 | { 3, 4,99, 0, 0, 0, 0, 0}, | - |
| 269 | { 4, 5, 6,99, 0, 0, 0, 0}, | - |
| 270 | { 1, 2, 4, 5, 6,99, 0, 0}, | - |
| 271 | { 1, 2, 3, 5, 6,99, 0, 0}, | - |
| 272 | { 8, 9,99, 0, 0, 0, 0, 0}, | - |
| 273 | { 0, 1, 2, 3,99, 0, 0, 0}, | - |
| 274 | {99, 0, 0, 0, 0, 0, 0, 0} }; | - |
| 275 | | - |
| 276 | if (ch >= '0' && ch <= '9') | - |
| 277 | return segments[ch - '0']; | - |
| 278 | if (ch >= 'A' && ch <= 'F') | - |
| 279 | return segments[ch - 'A' + 12]; | - |
| 280 | if (ch >= 'a' && ch <= 'f') | - |
| 281 | return segments[ch - 'a' + 12]; | - |
| 282 | | - |
| 283 | int n; | - |
| 284 | switch (ch) { | - |
| 285 | case '-': | - |
| 286 | n = 10; break; | - |
| 287 | case 'O': | - |
| 288 | n = 0; break; | - |
| 289 | case 'g': | - |
| 290 | n = 9; break; | - |
| 291 | case '.': | - |
| 292 | n = 11; break; | - |
| 293 | case 'h': | - |
| 294 | n = 18; break; | - |
| 295 | case 'H': | - |
| 296 | n = 19; break; | - |
| 297 | case 'l': | - |
| 298 | case 'L': | - |
| 299 | n = 20; break; | - |
| 300 | case 'o': | - |
| 301 | n = 21; break; | - |
| 302 | case 'p': | - |
| 303 | case 'P': | - |
| 304 | n = 22; break; | - |
| 305 | case 'r': | - |
| 306 | case 'R': | - |
| 307 | n = 23; break; | - |
| 308 | case 's': | - |
| 309 | case 'S': | - |
| 310 | n = 5; break; | - |
| 311 | case 'u': | - |
| 312 | n = 24; break; | - |
| 313 | case 'U': | - |
| 314 | n = 25; break; | - |
| 315 | case 'y': | - |
| 316 | case 'Y': | - |
| 317 | n = 26; break; | - |
| 318 | case ':': | - |
| 319 | n = 27; break; | - |
| 320 | case '\'': | - |
| 321 | n = 28; break; | - |
| 322 | default: | - |
| 323 | n = 29; break; | - |
| 324 | } | - |
| 325 | return segments[n]; | - |
| 326 | } | - |
| 327 | | - |
| 328 | | - |
| 329 | | - |
| 330 | | - |
| 331 | | - |
| 332 | | - |
| 333 | | - |
| 334 | | - |
| 335 | | - |
| 336 | | - |
| 337 | | - |
| 338 | | - |
| 339 | | - |
| 340 | QLCDNumber::QLCDNumber(QWidget *parent) | - |
| 341 | : QFrame(*new QLCDNumberPrivate, parent) | - |
| 342 | { | - |
| 343 | Q_D(QLCDNumber); | - |
| 344 | d->ndigits = 5; | - |
| 345 | d->init(); | - |
| 346 | } | - |
| 347 | | - |
| 348 | | - |
| 349 | | - |
| 350 | | - |
| 351 | | - |
| 352 | | - |
| 353 | | - |
| 354 | | - |
| 355 | | - |
| 356 | | - |
| 357 | | - |
| 358 | | - |
| 359 | | - |
| 360 | QLCDNumber::QLCDNumber(uint numDigits, QWidget *parent) | - |
| 361 | : QFrame(*new QLCDNumberPrivate, parent) | - |
| 362 | { | - |
| 363 | Q_D(QLCDNumber); | - |
| 364 | d->ndigits = numDigits; | - |
| 365 | d->init(); | - |
| 366 | } | - |
| 367 | | - |
| 368 | void QLCDNumberPrivate::init() | - |
| 369 | { | - |
| 370 | Q_Q(QLCDNumber); | - |
| 371 | | - |
| 372 | q->setFrameStyle(QFrame::Box | QFrame::Raised); | - |
| 373 | val = 0; | - |
| 374 | base = QLCDNumber::Dec; | - |
| 375 | smallPoint = false; | - |
| 376 | q->setDigitCount(ndigits); | - |
| 377 | q->setSegmentStyle(QLCDNumber::Filled); | - |
| 378 | q->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum)); | - |
| 379 | } | - |
| 380 | | - |
| 381 | | - |
| 382 | | - |
| 383 | | - |
| 384 | | - |
| 385 | QLCDNumber::~QLCDNumber() | - |
| 386 | { | - |
| 387 | } | - |
| 388 | | - |
| 389 | | - |
| 390 | | - |
| 391 | | - |
| 392 | | - |
| 393 | | - |
| 394 | | - |
| 395 | | - |
| 396 | | - |
| 397 | | - |
| 398 | | - |
| 399 | | - |
| 400 | | - |
| 401 | | - |
| 402 | | - |
| 403 | | - |
| 404 | | - |
| 405 | | - |
| 406 | | - |
| 407 | | - |
| 408 | void QLCDNumber::setDigitCount(int numDigits) | - |
| 409 | { | - |
| 410 | Q_D(QLCDNumber); | - |
| 411 | if (Q_UNLIKELY(numDigits > 99))) {| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 412 | qWarning("QLCDNumber::setNumDigits: (%s) Max 99 digits allowed", | - |
| 413 | objectName().toLocal8Bit().constData()); | - |
| 414 | numDigits = 99; | - |
| 415 | } never executed: end of block | 0 |
| 416 | if (Q_UNLIKELY(numDigits < 0))) {| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 417 | qWarning("QLCDNumber::setNumDigits: (%s) Min 0 digits allowed", | - |
| 418 | objectName().toLocal8Bit().constData()); | - |
| 419 | numDigits = 0; | - |
| 420 | } never executed: end of block | 0 |
| 421 | if (d->digitStr.isNull()) { | TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 422 | d->ndigits = numDigits; | - |
| 423 | d->digitStr.fill(QLatin1Char(' '), d->ndigits); | - |
| 424 | d->points.fill(0, d->ndigits); | - |
| 425 | d->digitStr[d->ndigits - 1] = QLatin1Char('0'); | - |
| 426 | } else { never executed: end of block | 0 |
| 427 | bool doDisplay = d->ndigits == 0; | - |
| 428 | if (numDigits == d->ndigits) | TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 429 | return; never executed: return; | 0 |
| 430 | int i; | - |
| 431 | int dif; | - |
| 432 | if (numDigits > d->ndigits) { | TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 433 | dif = numDigits - d->ndigits; | - |
| 434 | QString buf; | - |
| 435 | buf.fill(QLatin1Char(' '), dif); | - |
| 436 | d->digitStr.insert(0, buf); | - |
| 437 | d->points.resize(numDigits); | - |
| 438 | for (i=numDigits-1; i>=dif; i--)| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 439 | d->points.setBit(i, d->points.testBit(i-dif)); never executed: d->points.setBit(i, d->points.testBit(i-dif)); | 0 |
| 440 | for (i=0; i<dif; i++)| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 441 | d->points.clearBit(i); never executed: d->points.clearBit(i); | 0 |
| 442 | } else { never executed: end of block | 0 |
| 443 | dif = d->ndigits - numDigits; | - |
| 444 | d->digitStr = d->digitStr.right(numDigits); | - |
| 445 | QBitArray tmpPoints = d->points; | - |
| 446 | d->points.resize(numDigits); | - |
| 447 | for (i=0; i<(int)numDigits; i++)| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 448 | d->points.setBit(i, tmpPoints.testBit(i+dif)); never executed: d->points.setBit(i, tmpPoints.testBit(i+dif)); | 0 |
| 449 | } never executed: end of block | 0 |
| 450 | d->ndigits = numDigits; | - |
| 451 | if (doDisplay)| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 452 | display(value()); never executed: display(value()); | 0 |
| 453 | update(); | - |
| 454 | } never executed: end of block | 0 |
| 455 | } | - |
| 456 | | - |
| 457 | | - |
| 458 | | - |
| 459 | | - |
| 460 | int QLCDNumber::digitCount() const | - |
| 461 | { | - |
| 462 | Q_D(const QLCDNumber); | - |
| 463 | return d->ndigits; | - |
| 464 | } | - |
| 465 | | - |
| 466 | | - |
| 467 | | - |
| 468 | | - |
| 469 | | - |
| 470 | | - |
| 471 | | - |
| 472 | | - |
| 473 | | - |
| 474 | | - |
| 475 | bool QLCDNumber::checkOverflow(int num) const | - |
| 476 | { | - |
| 477 | Q_D(const QLCDNumber); | - |
| 478 | bool of; | - |
| 479 | int2string(num, d->base, d->ndigits, &of); | - |
| 480 | return of; | - |
| 481 | } | - |
| 482 | | - |
| 483 | | - |
| 484 | | - |
| 485 | | - |
| 486 | | - |
| 487 | | - |
| 488 | | - |
| 489 | | - |
| 490 | | - |
| 491 | bool QLCDNumber::checkOverflow(double num) const | - |
| 492 | { | - |
| 493 | Q_D(const QLCDNumber); | - |
| 494 | bool of; | - |
| 495 | double2string(num, d->base, d->ndigits, &of); | - |
| 496 | return of; | - |
| 497 | } | - |
| 498 | | - |
| 499 | | - |
| 500 | | - |
| 501 | | - |
| 502 | | - |
| 503 | | - |
| 504 | | - |
| 505 | | - |
| 506 | | - |
| 507 | | - |
| 508 | | - |
| 509 | | - |
| 510 | | - |
| 511 | | - |
| 512 | QLCDNumber::Mode QLCDNumber::mode() const | - |
| 513 | { | - |
| 514 | Q_D(const QLCDNumber); | - |
| 515 | return (QLCDNumber::Mode) d->base; | - |
| 516 | } | - |
| 517 | | - |
| 518 | void QLCDNumber::setMode(Mode m) | - |
| 519 | { | - |
| 520 | Q_D(QLCDNumber); | - |
| 521 | d->base = m; | - |
| 522 | display(d->val); | - |
| 523 | } | - |
| 524 | | - |
| 525 | | - |
| 526 | | - |
| 527 | | - |
| 528 | | - |
| 529 | | - |
| 530 | | - |
| 531 | | - |
| 532 | | - |
| 533 | | - |
| 534 | | - |
| 535 | | - |
| 536 | | - |
| 537 | | - |
| 538 | | - |
| 539 | double QLCDNumber::value() const | - |
| 540 | { | - |
| 541 | Q_D(const QLCDNumber); | - |
| 542 | return d->val; | - |
| 543 | } | - |
| 544 | | - |
| 545 | | - |
| 546 | | - |
| 547 | | - |
| 548 | | - |
| 549 | | - |
| 550 | void QLCDNumber::display(double num) | - |
| 551 | { | - |
| 552 | Q_D(QLCDNumber); | - |
| 553 | d->val = num; | - |
| 554 | bool of; | - |
| 555 | QString s = double2string(d->val, d->base, d->ndigits, &of); | - |
| 556 | if (of) | - |
| 557 | emit overflow(); | - |
| 558 | else | - |
| 559 | d->internalSetString(s); | - |
| 560 | } | - |
| 561 | | - |
| 562 | | - |
| 563 | | - |
| 564 | | - |
| 565 | | - |
| 566 | | - |
| 567 | | - |
| 568 | | - |
| 569 | | - |
| 570 | | - |
| 571 | | - |
| 572 | | - |
| 573 | | - |
| 574 | | - |
| 575 | int QLCDNumber::intValue() const | - |
| 576 | { | - |
| 577 | Q_D(const QLCDNumber); | - |
| 578 | return qRound(d->val); | - |
| 579 | } | - |
| 580 | | - |
| 581 | | - |
| 582 | | - |
| 583 | | - |
| 584 | | - |
| 585 | | - |
| 586 | | - |
| 587 | void QLCDNumber::display(int num) | - |
| 588 | { | - |
| 589 | Q_D(QLCDNumber); | - |
| 590 | d->val = (double)num; | - |
| 591 | bool of; | - |
| 592 | QString s = int2string(num, d->base, d->ndigits, &of); | - |
| 593 | if (of) | - |
| 594 | emit overflow(); | - |
| 595 | else | - |
| 596 | d->internalSetString(s); | - |
| 597 | } | - |
| 598 | | - |
| 599 | | - |
| 600 | | - |
| 601 | | - |
| 602 | | - |
| 603 | | - |
| 604 | | - |
| 605 | | - |
| 606 | | - |
| 607 | | - |
| 608 | | - |
| 609 | | - |
| 610 | | - |
| 611 | | - |
| 612 | | - |
| 613 | void QLCDNumber::display(const QString &s) | - |
| 614 | { | - |
| 615 | Q_D(QLCDNumber); | - |
| 616 | d->val = 0; | - |
| 617 | bool ok = false; | - |
| 618 | double v = s.toDouble(&ok); | - |
| 619 | if (ok) | - |
| 620 | d->val = v; | - |
| 621 | d->internalSetString(s); | - |
| 622 | } | - |
| 623 | | - |
| 624 | | - |
| 625 | | - |
| 626 | | - |
| 627 | | - |
| 628 | | - |
| 629 | | - |
| 630 | | - |
| 631 | void QLCDNumber::setHexMode() | - |
| 632 | { | - |
| 633 | setMode(Hex); | - |
| 634 | } | - |
| 635 | | - |
| 636 | | - |
| 637 | | - |
| 638 | | - |
| 639 | | - |
| 640 | | - |
| 641 | | - |
| 642 | | - |
| 643 | | - |
| 644 | void QLCDNumber::setDecMode() | - |
| 645 | { | - |
| 646 | setMode(Dec); | - |
| 647 | } | - |
| 648 | | - |
| 649 | | - |
| 650 | | - |
| 651 | | - |
| 652 | | - |
| 653 | | - |
| 654 | | - |
| 655 | | - |
| 656 | | - |
| 657 | void QLCDNumber::setOctMode() | - |
| 658 | { | - |
| 659 | setMode(Oct); | - |
| 660 | } | - |
| 661 | | - |
| 662 | | - |
| 663 | | - |
| 664 | | - |
| 665 | | - |
| 666 | | - |
| 667 | | - |
| 668 | | - |
| 669 | | - |
| 670 | void QLCDNumber::setBinMode() | - |
| 671 | { | - |
| 672 | setMode(Bin); | - |
| 673 | } | - |
| 674 | | - |
| 675 | | - |
| 676 | | - |
| 677 | | - |
| 678 | | - |
| 679 | | - |
| 680 | | - |
| 681 | | - |
| 682 | | - |
| 683 | | - |
| 684 | | - |
| 685 | | - |
| 686 | | - |
| 687 | | - |
| 688 | | - |
| 689 | | - |
| 690 | void QLCDNumber::setSmallDecimalPoint(bool b) | - |
| 691 | { | - |
| 692 | Q_D(QLCDNumber); | - |
| 693 | d->smallPoint = b; | - |
| 694 | update(); | - |
| 695 | } | - |
| 696 | | - |
| 697 | bool QLCDNumber::smallDecimalPoint() const | - |
| 698 | { | - |
| 699 | Q_D(const QLCDNumber); | - |
| 700 | return d->smallPoint; | - |
| 701 | } | - |
| 702 | | - |
| 703 | | - |
| 704 | | - |
| 705 | | - |
| 706 | | - |
| 707 | | - |
| 708 | | - |
| 709 | void QLCDNumber::paintEvent(QPaintEvent *) | - |
| 710 | { | - |
| 711 | Q_D(QLCDNumber); | - |
| 712 | QPainter p(this); | - |
| 713 | drawFrame(&p); | - |
| 714 | p.setRenderHint(QPainter::Antialiasing); | - |
| 715 | if (d->shadow) | - |
| 716 | p.translate(0.5, 0.5); | - |
| 717 | | - |
| 718 | if (d->smallPoint) | - |
| 719 | d->drawString(d->digitStr, p, &d->points, false); | - |
| 720 | else | - |
| 721 | d->drawString(d->digitStr, p, 0, false); | - |
| 722 | } | - |
| 723 | | - |
| 724 | | - |
| 725 | void QLCDNumberPrivate::internalSetString(const QString& s) | - |
| 726 | { | - |
| 727 | Q_Q(QLCDNumber); | - |
| 728 | QString buffer; | - |
| 729 | int i; | - |
| 730 | int len = s.length(); | - |
| 731 | QBitArray newPoints(ndigits); | - |
| 732 | | - |
| 733 | if (!smallPoint) { | - |
| 734 | if (len == ndigits) | - |
| 735 | buffer = s; | - |
| 736 | else | - |
| 737 | buffer = s.right(ndigits).rightJustified(ndigits, QLatin1Char(' ')); | - |
| 738 | } else { | - |
| 739 | int index = -1; | - |
| 740 | bool lastWasPoint = true; | - |
| 741 | newPoints.clearBit(0); | - |
| 742 | for (i=0; i<len; i++) { | - |
| 743 | if (s[i] == QLatin1Char('.')) { | - |
| 744 | if (lastWasPoint) { | - |
| 745 | if (index == ndigits - 1) | - |
| 746 | break; | - |
| 747 | index++; | - |
| 748 | buffer[index] = QLatin1Char(' '); | - |
| 749 | } | - |
| 750 | newPoints.setBit(index); | - |
| 751 | lastWasPoint = true; | - |
| 752 | } else { | - |
| 753 | if (index == ndigits - 1) | - |
| 754 | break; | - |
| 755 | index++; | - |
| 756 | buffer[index] = s[i]; | - |
| 757 | newPoints.clearBit(index); | - |
| 758 | lastWasPoint = false; | - |
| 759 | } | - |
| 760 | } | - |
| 761 | if (index < ((int) ndigits) - 1) { | - |
| 762 | for(i=index; i>=0; i--) { | - |
| 763 | buffer[ndigits - 1 - index + i] = buffer[i]; | - |
| 764 | newPoints.setBit(ndigits - 1 - index + i, | - |
| 765 | newPoints.testBit(i)); | - |
| 766 | } | - |
| 767 | for(i=0; i<ndigits-index-1; i++) { | - |
| 768 | buffer[i] = QLatin1Char(' '); | - |
| 769 | newPoints.clearBit(i); | - |
| 770 | } | - |
| 771 | } | - |
| 772 | } | - |
| 773 | | - |
| 774 | if (buffer == digitStr) | - |
| 775 | return; | - |
| 776 | | - |
| 777 | digitStr = buffer; | - |
| 778 | if (smallPoint) | - |
| 779 | points = newPoints; | - |
| 780 | q->update(); | - |
| 781 | } | - |
| 782 | | - |
| 783 | | - |
| 784 | | - |
| 785 | | - |
| 786 | | - |
| 787 | void QLCDNumberPrivate::drawString(const QString &s, QPainter &p, | - |
| 788 | QBitArray *newPoints, bool newString) | - |
| 789 | { | - |
| 790 | Q_Q(QLCDNumber); | - |
| 791 | QPoint pos; | - |
| 792 | | - |
| 793 | int digitSpace = smallPoint ? 2 : 1; | - |
| 794 | int xSegLen = q->width()*5/(ndigits*(5 + digitSpace) + digitSpace); | - |
| 795 | int ySegLen = q->height()*5/12; | - |
| 796 | int segLen = ySegLen > xSegLen ? xSegLen : ySegLen; | - |
| 797 | int xAdvance = segLen*(5 + digitSpace)/5; | - |
| 798 | int xOffset = (q->width() - ndigits*xAdvance + segLen/5)/2; | - |
| 799 | int yOffset = (q->height() - segLen*2)/2; | - |
| 800 | | - |
| 801 | for (int i=0; i<ndigits; i++) { | - |
| 802 | pos = QPoint(xOffset + xAdvance*i, yOffset); | - |
| 803 | if (newString) | - |
| 804 | drawDigit(pos, p, segLen, s[i].toLatin1(), digitStr[i].toLatin1()); | - |
| 805 | else | - |
| 806 | drawDigit(pos, p, segLen, s[i].toLatin1()); | - |
| 807 | if (newPoints) { | - |
| 808 | char newPoint = newPoints->testBit(i) ? '.' : ' '; | - |
| 809 | if (newString) { | - |
| 810 | char oldPoint = points.testBit(i) ? '.' : ' '; | - |
| 811 | drawDigit(pos, p, segLen, newPoint, oldPoint); | - |
| 812 | } else { | - |
| 813 | drawDigit(pos, p, segLen, newPoint); | - |
| 814 | } | - |
| 815 | } | - |
| 816 | } | - |
| 817 | if (newString) { | - |
| 818 | digitStr = s; | - |
| 819 | digitStr.truncate(ndigits); | - |
| 820 | if (newPoints) | - |
| 821 | points = *newPoints; | - |
| 822 | } | - |
| 823 | } | - |
| 824 | | - |
| 825 | | - |
| 826 | | - |
| 827 | | - |
| 828 | | - |
| 829 | | - |
| 830 | void QLCDNumberPrivate::drawDigit(const QPoint &pos, QPainter &p, int segLen, | - |
| 831 | char newCh, char oldCh) | - |
| 832 | { | - |
| 833 | | - |
| 834 | | - |
| 835 | | - |
| 836 | char updates[18][2]; | - |
| 837 | | - |
| 838 | int nErases; | - |
| 839 | int nUpdates; | - |
| 840 | const char *segs; | - |
| 841 | int i,j; | - |
| 842 | | - |
| 843 | const char erase = 0; | - |
| 844 | const char draw = 1; | - |
| 845 | const char leaveAlone = 2; | - |
| 846 | | - |
| 847 | segs = getSegments(oldCh); | - |
| 848 | for (nErases=0; segs[nErases] != 99; nErases++) { | - |
| 849 | updates[nErases][0] = erase; | - |
| 850 | updates[nErases][1] = segs[nErases]; | - |
| 851 | } | - |
| 852 | nUpdates = nErases; | - |
| 853 | segs = getSegments(newCh); | - |
| 854 | for(i = 0 ; segs[i] != 99 ; i++) { | - |
| 855 | for (j=0; j<nErases; j++) | - |
| 856 | if (segs[i] == updates[j][1]) { | - |
| 857 | updates[j][0] = leaveAlone; | - |
| 858 | break; | - |
| 859 | } | - |
| 860 | if (j == nErases) { | - |
| 861 | updates[nUpdates][0] = draw; | - |
| 862 | updates[nUpdates][1] = segs[i]; | - |
| 863 | nUpdates++; | - |
| 864 | } | - |
| 865 | } | - |
| 866 | for (i=0; i<nUpdates; i++) { | - |
| 867 | if (updates[i][0] == draw) | - |
| 868 | drawSegment(pos, updates[i][1], p, segLen); | - |
| 869 | if (updates[i][0] == erase) | - |
| 870 | drawSegment(pos, updates[i][1], p, segLen, true); | - |
| 871 | } | - |
| 872 | } | - |
| 873 | | - |
| 874 | | - |
| 875 | static void addPoint(QPolygon &a, const QPoint &p) | - |
| 876 | { | - |
| 877 | uint n = a.size(); | - |
| 878 | a.resize(n + 1); | - |
| 879 | a.setPoint(n, p); | - |
| 880 | } | - |
| 881 | | - |
| 882 | | - |
| 883 | | - |
| 884 | | - |
| 885 | | - |
| 886 | void QLCDNumberPrivate::drawSegment(const QPoint &pos, char segmentNo, QPainter &p, | - |
| 887 | int segLen, bool erase) | - |
| 888 | { | - |
| 889 | Q_Q(QLCDNumber); | - |
| 890 | QPoint ppt; | - |
| 891 | QPoint pt = pos; | - |
| 892 | int width = segLen/5; | - |
| 893 | | - |
| 894 | const QPalette &pal = q->palette(); | - |
| 895 | QColor lightColor,darkColor,fgColor; | - |
| 896 | if (erase){ | - |
| 897 | lightColor = pal.color(q->backgroundRole()); | - |
| 898 | darkColor = lightColor; | - |
| 899 | fgColor = lightColor; | - |
| 900 | } else { | - |
| 901 | lightColor = pal.light().color(); | - |
| 902 | darkColor = pal.dark().color(); | - |
| 903 | fgColor = pal.color(q->foregroundRole()); | - |
| 904 | } | - |
| 905 | | - |
| 906 | | - |
| 907 | #define LINETO(X,Y) addPoint(a, QPoint(pt.x() + (X),pt.y() + (Y))) | - |
| 908 | #define LIGHT | - |
| 909 | #define DARK | - |
| 910 | | - |
| 911 | if (fill) { | - |
| 912 | QPolygon a(0); | - |
| 913 | | - |
| 914 | | - |
| 915 | switch (segmentNo) { | - |
| 916 | case 0 : | - |
| 917 | ppt = pt; | - |
| 918 | LIGHT; | - |
| 919 | LINETO(segLen - 1,0); | - |
| 920 | DARK; | - |
| 921 | LINETO(segLen - width - 1,width); | - |
| 922 | LINETO(width,width); | - |
| 923 | LINETO(0,0); | - |
| 924 | break; | - |
| 925 | case 1 : | - |
| 926 | pt += QPoint(0 , 1); | - |
| 927 | ppt = pt; | - |
| 928 | LIGHT; | - |
| 929 | LINETO(width,width); | - |
| 930 | DARK; | - |
| 931 | LINETO(width,segLen - width/2 - 2); | - |
| 932 | LINETO(0,segLen - 2); | - |
| 933 | LIGHT; | - |
| 934 | LINETO(0,0); | - |
| 935 | break; | - |
| 936 | case 2 : | - |
| 937 | pt += QPoint(segLen - 1 , 1); | - |
| 938 | ppt = pt; | - |
| 939 | DARK; | - |
| 940 | LINETO(0,segLen - 2); | - |
| 941 | LINETO(-width,segLen - width/2 - 2); | - |
| 942 | LIGHT; | - |
| 943 | LINETO(-width,width); | - |
| 944 | LINETO(0,0); | - |
| 945 | break; | - |
| 946 | case 3 : | - |
| 947 | pt += QPoint(0 , segLen); | - |
| 948 | ppt = pt; | - |
| 949 | LIGHT; | - |
| 950 | LINETO(width,-width/2); | - |
| 951 | LINETO(segLen - width - 1,-width/2); | - |
| 952 | LINETO(segLen - 1,0); | - |
| 953 | DARK; | - |
| 954 | if (width & 1) { | - |
| 955 | LINETO(segLen - width - 3,width/2 + 1); | - |
| 956 | LINETO(width + 2,width/2 + 1); | - |
| 957 | } else { | - |
| 958 | LINETO(segLen - width - 1,width/2); | - |
| 959 | LINETO(width,width/2); | - |
| 960 | } | - |
| 961 | LINETO(0,0); | - |
| 962 | break; | - |
| 963 | case 4 : | - |
| 964 | pt += QPoint(0 , segLen + 1); | - |
| 965 | ppt = pt; | - |
| 966 | LIGHT; | - |
| 967 | LINETO(width,width/2); | - |
| 968 | DARK; | - |
| 969 | LINETO(width,segLen - width - 2); | - |
| 970 | LINETO(0,segLen - 2); | - |
| 971 | LIGHT; | - |
| 972 | LINETO(0,0); | - |
| 973 | break; | - |
| 974 | case 5 : | - |
| 975 | pt += QPoint(segLen - 1 , segLen + 1); | - |
| 976 | ppt = pt; | - |
| 977 | DARK; | - |
| 978 | LINETO(0,segLen - 2); | - |
| 979 | LINETO(-width,segLen - width - 2); | - |
| 980 | LIGHT; | - |
| 981 | LINETO(-width,width/2); | - |
| 982 | LINETO(0,0); | - |
| 983 | break; | - |
| 984 | case 6 : | - |
| 985 | pt += QPoint(0 , segLen*2); | - |
| 986 | ppt = pt; | - |
| 987 | LIGHT; | - |
| 988 | LINETO(width,-width); | - |
| 989 | LINETO(segLen - width - 1,-width); | - |
| 990 | LINETO(segLen - 1,0); | - |
| 991 | DARK; | - |
| 992 | LINETO(0,0); | - |
| 993 | break; | - |
| 994 | case 7 : | - |
| 995 | if (smallPoint) | - |
| 996 | pt += QPoint(segLen + width/2 , segLen*2); | - |
| 997 | else | - |
| 998 | pt += QPoint(segLen/2 , segLen*2); | - |
| 999 | ppt = pt; | - |
| 1000 | DARK; | - |
| 1001 | LINETO(width,0); | - |
| 1002 | LINETO(width,-width); | - |
| 1003 | LIGHT; | - |
| 1004 | LINETO(0,-width); | - |
| 1005 | LINETO(0,0); | - |
| 1006 | break; | - |
| 1007 | case 8 : | - |
| 1008 | pt += QPoint(segLen/2 - width/2 + 1 , segLen/2 + width); | - |
| 1009 | ppt = pt; | - |
| 1010 | DARK; | - |
| 1011 | LINETO(width,0); | - |
| 1012 | LINETO(width,-width); | - |
| 1013 | LIGHT; | - |
| 1014 | LINETO(0,-width); | - |
| 1015 | LINETO(0,0); | - |
| 1016 | break; | - |
| 1017 | case 9 : | - |
| 1018 | pt += QPoint(segLen/2 - width/2 + 1 , 3*segLen/2 + width); | - |
| 1019 | ppt = pt; | - |
| 1020 | DARK; | - |
| 1021 | LINETO(width,0); | - |
| 1022 | LINETO(width,-width); | - |
| 1023 | LIGHT; | - |
| 1024 | LINETO(0,-width); | - |
| 1025 | LINETO(0,0); | - |
| 1026 | break; | - |
| 1027 | default : | - |
| 1028 | qWarning("QLCDNumber::drawSegment: (%s) Illegal segment id: %d\n", | - |
| 1029 | q->objectName().toLocal8Bit().constData(), segmentNo); | - |
| 1030 | } | - |
| 1031 | | - |
| 1032 | p.setPen(Qt::NoPen); | - |
| 1033 | p.setBrush(fgColor); | - |
| 1034 | p.drawPolygon(a); | - |
| 1035 | p.setBrush(Qt::NoBrush); | - |
| 1036 | | - |
| 1037 | pt = pos; | - |
| 1038 | } | - |
| 1039 | #undef LINETO | - |
| 1040 | #undef LIGHT | - |
| 1041 | #undef DARK | - |
| 1042 | | - |
| 1043 | #define LINETO(X,Y) p.drawLine(ppt.x(), ppt.y(), pt.x()+(X), pt.y()+(Y)); \ | - |
| 1044 | ppt = QPoint(pt.x()+(X), pt.y()+(Y)) | - |
| 1045 | #define LIGHT p.setPen(lightColor) | - |
| 1046 | #define DARK p.setPen(darkColor) | - |
| 1047 | if (shadow) | - |
| 1048 | switch (segmentNo) { | - |
| 1049 | case 0 : | - |
| 1050 | ppt = pt; | - |
| 1051 | LIGHT; | - |
| 1052 | LINETO(segLen - 1,0); | - |
| 1053 | DARK; | - |
| 1054 | LINETO(segLen - width - 1,width); | - |
| 1055 | LINETO(width,width); | - |
| 1056 | LINETO(0,0); | - |
| 1057 | break; | - |
| 1058 | case 1 : | - |
| 1059 | pt += QPoint(0,1); | - |
| 1060 | ppt = pt; | - |
| 1061 | LIGHT; | - |
| 1062 | LINETO(width,width); | - |
| 1063 | DARK; | - |
| 1064 | LINETO(width,segLen - width/2 - 2); | - |
| 1065 | LINETO(0,segLen - 2); | - |
| 1066 | LIGHT; | - |
| 1067 | LINETO(0,0); | - |
| 1068 | break; | - |
| 1069 | case 2 : | - |
| 1070 | pt += QPoint(segLen - 1 , 1); | - |
| 1071 | ppt = pt; | - |
| 1072 | DARK; | - |
| 1073 | LINETO(0,segLen - 2); | - |
| 1074 | LINETO(-width,segLen - width/2 - 2); | - |
| 1075 | LIGHT; | - |
| 1076 | LINETO(-width,width); | - |
| 1077 | LINETO(0,0); | - |
| 1078 | break; | - |
| 1079 | case 3 : | - |
| 1080 | pt += QPoint(0 , segLen); | - |
| 1081 | ppt = pt; | - |
| 1082 | LIGHT; | - |
| 1083 | LINETO(width,-width/2); | - |
| 1084 | LINETO(segLen - width - 1,-width/2); | - |
| 1085 | LINETO(segLen - 1,0); | - |
| 1086 | DARK; | - |
| 1087 | if (width & 1) { | - |
| 1088 | LINETO(segLen - width - 3,width/2 + 1); | - |
| 1089 | LINETO(width + 2,width/2 + 1); | - |
| 1090 | } else { | - |
| 1091 | LINETO(segLen - width - 1,width/2); | - |
| 1092 | LINETO(width,width/2); | - |
| 1093 | } | - |
| 1094 | LINETO(0,0); | - |
| 1095 | break; | - |
| 1096 | case 4 : | - |
| 1097 | pt += QPoint(0 , segLen + 1); | - |
| 1098 | ppt = pt; | - |
| 1099 | LIGHT; | - |
| 1100 | LINETO(width,width/2); | - |
| 1101 | DARK; | - |
| 1102 | LINETO(width,segLen - width - 2); | - |
| 1103 | LINETO(0,segLen - 2); | - |
| 1104 | LIGHT; | - |
| 1105 | LINETO(0,0); | - |
| 1106 | break; | - |
| 1107 | case 5 : | - |
| 1108 | pt += QPoint(segLen - 1 , segLen + 1); | - |
| 1109 | ppt = pt; | - |
| 1110 | DARK; | - |
| 1111 | LINETO(0,segLen - 2); | - |
| 1112 | LINETO(-width,segLen - width - 2); | - |
| 1113 | LIGHT; | - |
| 1114 | LINETO(-width,width/2); | - |
| 1115 | LINETO(0,0); | - |
| 1116 | break; | - |
| 1117 | case 6 : | - |
| 1118 | pt += QPoint(0 , segLen*2); | - |
| 1119 | ppt = pt; | - |
| 1120 | LIGHT; | - |
| 1121 | LINETO(width,-width); | - |
| 1122 | LINETO(segLen - width - 1,-width); | - |
| 1123 | LINETO(segLen - 1,0); | - |
| 1124 | DARK; | - |
| 1125 | LINETO(0,0); | - |
| 1126 | break; | - |
| 1127 | case 7 : | - |
| 1128 | if (smallPoint) | - |
| 1129 | pt += QPoint(segLen + width/2 , segLen*2); | - |
| 1130 | else | - |
| 1131 | pt += QPoint(segLen/2 , segLen*2); | - |
| 1132 | ppt = pt; | - |
| 1133 | DARK; | - |
| 1134 | LINETO(width,0); | - |
| 1135 | LINETO(width,-width); | - |
| 1136 | LIGHT; | - |
| 1137 | LINETO(0,-width); | - |
| 1138 | LINETO(0,0); | - |
| 1139 | break; | - |
| 1140 | case 8 : | - |
| 1141 | pt += QPoint(segLen/2 - width/2 + 1 , segLen/2 + width); | - |
| 1142 | ppt = pt; | - |
| 1143 | DARK; | - |
| 1144 | LINETO(width,0); | - |
| 1145 | LINETO(width,-width); | - |
| 1146 | LIGHT; | - |
| 1147 | LINETO(0,-width); | - |
| 1148 | LINETO(0,0); | - |
| 1149 | break; | - |
| 1150 | case 9 : | - |
| 1151 | pt += QPoint(segLen/2 - width/2 + 1 , 3*segLen/2 + width); | - |
| 1152 | ppt = pt; | - |
| 1153 | DARK; | - |
| 1154 | LINETO(width,0); | - |
| 1155 | LINETO(width,-width); | - |
| 1156 | LIGHT; | - |
| 1157 | LINETO(0,-width); | - |
| 1158 | LINETO(0,0); | - |
| 1159 | break; | - |
| 1160 | default : | - |
| 1161 | qWarning("QLCDNumber::drawSegment: (%s) Illegal segment id: %d\n", | - |
| 1162 | q->objectName().toLocal8Bit().constData(), segmentNo); | - |
| 1163 | } | - |
| 1164 | | - |
| 1165 | #undef LINETO | - |
| 1166 | #undef LIGHT | - |
| 1167 | #undef DARK | - |
| 1168 | } | - |
| 1169 | | - |
| 1170 | | - |
| 1171 | | - |
| 1172 | | - |
| 1173 | | - |
| 1174 | | - |
| 1175 | | - |
| 1176 | | - |
| 1177 | | - |
| 1178 | | - |
| 1179 | | - |
| 1180 | | - |
| 1181 | | - |
| 1182 | | - |
| 1183 | | - |
| 1184 | | - |
| 1185 | | - |
| 1186 | | - |
| 1187 | | - |
| 1188 | | - |
| 1189 | | - |
| 1190 | void QLCDNumber::setSegmentStyle(SegmentStyle s) | - |
| 1191 | { | - |
| 1192 | Q_D(QLCDNumber); | - |
| 1193 | d->fill = (s == Flat || s == Filled); | - |
| 1194 | d->shadow = (s == Outline || s == Filled); | - |
| 1195 | update(); | - |
| 1196 | } | - |
| 1197 | | - |
| 1198 | QLCDNumber::SegmentStyle QLCDNumber::segmentStyle() const | - |
| 1199 | { | - |
| 1200 | Q_D(const QLCDNumber); | - |
| 1201 | Q_ASSERT(d->fill || d->shadow); | - |
| 1202 | if (!d->fill && d->shadow) | - |
| 1203 | return Outline; | - |
| 1204 | if (d->fill && d->shadow) | - |
| 1205 | return Filled; | - |
| 1206 | return Flat; | - |
| 1207 | } | - |
| 1208 | | - |
| 1209 | | - |
| 1210 | | - |
| 1211 | | - |
| 1212 | QSize QLCDNumber::sizeHint() const | - |
| 1213 | { | - |
| 1214 | return QSize(10 + 9 * (digitCount() + (smallDecimalPoint() ? 0 : 1)), 23); | - |
| 1215 | } | - |
| 1216 | | - |
| 1217 | | - |
| 1218 | bool QLCDNumber::event(QEvent *e) | - |
| 1219 | { | - |
| 1220 | return QFrame::event(e); | - |
| 1221 | } | - |
| 1222 | | - |
| 1223 | QT_END_NAMESPACE | - |
| 1224 | | - |
| 1225 | #include "moc_qlcdnumber.cpp" | - |
| 1226 | | - |
| 1227 | #endif // QT_NO_LCDNUMBER | - |
| | |