summaryrefslogtreecommitdiffstats
path: root/src/common/gui/number_gui.cpp
blob: 21bc5fe26f6df153bcda0c6f1c6038b580af50e9 (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
73
74
/***************************************************************************
 *   Copyright (C) 2006 Nicolas Hadacek <[email protected]>                  *
 *                                                                         *
 *   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 <tqfontmetrics.h>

//-----------------------------------------------------------------------------
uint maxCharWidth(const TQString &s, const TQFont &font)
{
  TQFontMetrics fm(font);
  uint w = 0;
  for (uint i=0; i<uint(s.length()); i++)
    w = TQMAX(w, uint(fm.width(s[i])));
  return w;
}

uint maxCharWidth(NumberBase base, const TQFont &font)
{
  TQString 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 TQFont &font)
{
  uint w = maxStringWidth(base, nbChars, font);
  TQFontMetrics fm(font);
  if ( base==NumberBase::String ) return w + 2 * fm.width("\"");
  return w + fm.width(base.data().prefix);
}

//-----------------------------------------------------------------------------
NumberLineEdit::NumberLineEdit(TQWidget *parent, const char *name)
  : KLineEdit(parent, name)
{
  connect(this, TQ_SIGNAL(textChanged(const TQString &)), TQ_SLOT(textChangedSlot()));
}

NumberLineEdit::NumberLineEdit(const TQString &text, TQWidget *parent, const char *name)
  : KLineEdit(text, parent, name)
{
  connect(this, TQ_SIGNAL(textChanged(const TQString &)), TQ_SLOT(textChangedSlot()));
}

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

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

#include "number_gui.moc"