displaynumber.cpp 1.98 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/***************************************************************************
                          displaynumber.cpp  -  description
                             -------------------
    begin                : Sun Feb 23 2003
    copyright            : (C) 2003 by Michael v.Ostheim
    email                : ostheimm@users.berlios.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

David Edmundson's avatar
David Edmundson committed
18
19
#include <QString>
#include <QFontMetrics>
20
#include <QLabel>
Laurent Montel's avatar
Laurent Montel committed
21
#include <QFrame>
David Edmundson's avatar
David Edmundson committed
22
#include <QDebug>
23
24
25

#include "displaynumber.h"

Laurent Montel's avatar
Laurent Montel committed
26
27
DisplayNumber::DisplayNumber(QWidget *parent, int digits, int prec) 
  : QLabel(parent) {
28
29

  setPrecision(prec);
Stephan Kulow's avatar
Stephan Kulow committed
30
  setWidth(digits);
31

Laurent Montel's avatar
Laurent Montel committed
32
  setFrameStyle(QFrame::Panel | QFrame::Sunken);
Stephan Kulow's avatar
Stephan Kulow committed
33
  setBackgroundRole(QPalette::Base);
34
  setAlignment(Qt::AlignCenter);
35
  setFocusPolicy(Qt::NoFocus);
36
37
38
39
40
41
42
43
44
45
46
47
}

DisplayNumber::~DisplayNumber(){
}

void DisplayNumber::setFont( const QFont & f ) {
  QLabel::setFont(f);
  setWidth(dg);
}

void DisplayNumber::setWidth(int digits) {
  QFontMetrics fm(font());
48
  QString s(QStringLiteral("0123456789.+-"));
49
50
  int width = 0, charWidth=0;

51
  for (int i = 0; i < 11; i++, width = fm.boundingRect(s[i]).width())
52
53
54
55
56
57
58
59
60
61
    charWidth = (width > charWidth) ? width : charWidth;

  dg = digits;
  setMinimumWidth( dg * charWidth + charWidth/2 );
}

void DisplayNumber::setNum(double num) {
  QString text;
  setText(text.setNum(num, 'f', precision));
}