summaryrefslogtreecommitdiffstats
path: root/src/common/gui/number_gui.cpp
blob: d8554075e4d63ac7fdff9b95379011e79b6ff580 (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
/***************************************************************************
 *   Copyright (C) 2006 Nicolas Hadacek <hadacek@kde.org>                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/
#include "number_gui.h"

#include <qfontmetrics.h>

//-----------------------------------------------------------------------------
uint maxCharWidth(const QString &s, const QFont &font)
{
  QFontMetrics fm(font);
  uint w = 0;
  for (uint i=0; i<uint(s.length()); i++)
    w = QMAX(w, uint(fm.width(s[i])));
  return w;
}

uint maxCharWidth(NumberBase base, const QFont &font)
{
  QString s;
  for (uint i=0; i<base.data().base; i++) s += toChar(base, i);
  return maxCharWidth(s, font);
}

uint maxLabelWidth(NumberBase base, uint nbChars, const QFont &font)
{
  uint w = maxStringWidth(base, nbChars, font);
  QFontMetrics fm(font);
  if ( base==NumberBase::String ) return w + 2 * fm.width("\"");
  return w + fm.width(base.data().prefix);
}

//-----------------------------------------------------------------------------
NumberLineEdit::NumberLineEdit(QWidget *parent, const char *name)
  : KLineEdit(parent, name)
{
  connect(this, SIGNAL(textChanged(const QString &)), SLOT(textChangedSlot()));
}

NumberLineEdit::NumberLineEdit(const QString &text, QWidget *parent, const char *name)
  : KLineEdit(text, parent, name)
{
  connect(this, SIGNAL(textChanged(const QString &)), SLOT(textChangedSlot()));
}

QValidator::State validateNumber(const QString &input)
{
  if ( input.isEmpty() ) return QValidator::Intermediate;
  bool ok;
  (void)fromAnyLabel(input, &ok);
  if (ok) return QValidator::Acceptable;
  FOR_EACH(NumberBase, base)
    if ( input==base.data().prefix ) return QValidator::Intermediate;
  if ( input[0]=='\"' ) return QValidator::Intermediate;
  if ( input[0]=='\'' ) return QValidator::Intermediate;
  return QValidator::Invalid;
}

void NumberLineEdit::textChangedSlot()
{
  QValidator::State state = validateNumber(text());
  switch (state) {
    case QValidator::Acceptable: unsetColor(); break;
    case QValidator::Intermediate: setColor(QColor("#FF9900")); break;
    case QValidator::Invalid: setColor(red); break;
  }
}