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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
/*
* numberwidget.cpp
*
* (c) 2003-2004,2008-2010 by Jeremy Bowman <jmbowman@alum.mit.edu>
*
* 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.
*/
/** @file numberwidget.cpp
* Source file for NumberWidget
*/
#include <QAbstractButton>
#include <QIcon>
#include <QLayout>
#include <QLineEdit>
#include <QLocale>
#include "calculator.h"
#include "datatypes.h"
#include "factory.h"
#include "formatting.h"
#include "numberwidget.h"
/**
* Constructor.
*
* @param type The type of value to edit (FLOAT or INT)
* @param parent This widget's parent widget
*/
NumberWidget::NumberWidget(int type, QWidget *parent)
: QWidget(parent), dataType(type)
{
QHBoxLayout *layout = Factory::hBoxLayout(this, true);
entryField = new QLineEdit(this);
layout->addWidget(entryField, 1);
QAbstractButton *button = Factory::button(this);
layout->addWidget(button);
button->setIcon(QIcon(":/icons/calculator.png"));
button->setToolTip(tr("Show calculator"));
connect(button, SIGNAL(clicked()), this, SLOT(launchCalculator()));
}
/**
* Get the currently selected value.
*
* @return The C-locale text representation of the currently selected value
*/
QString NumberWidget::getValue()
{
if (dataType == INTEGER) {
int value = QLocale::system().toInt(entryField->text());
return QString::number(value);
}
else {
return Formatting::fromLocalDouble(entryField->text());
}
}
/**
* Set this widget's new value.
*
* @param value The new value to be displayed in the widget (formatted for
* the C locale)
*/
void NumberWidget::setValue(const QString &value)
{
if (dataType == INTEGER) {
double result = Formatting::parseDouble(value);
entryField->setText(QLocale::system().toString((int)result));
}
else {
entryField->setText(Formatting::toLocalDouble(value));
}
}
/**
* Launch the calculator dialog. Called automatically when the right-hand
* button is clicked.
*/
void NumberWidget::launchCalculator()
{
Calculator calc(this);
calc.setInitialValue(getValue());
if (calc.exec()) {
setValue(calc.result());
}
}
|