/
DlgUnitsCalculatorImp.cpp
138 lines (115 loc) · 5 KB
/
DlgUnitsCalculatorImp.cpp
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
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
/***************************************************************************
* Copyright (c) 2013 Jürgen Riegel <juergen.riegel@web.de> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <QApplication>
# include <QClipboard>
# include <QLocale>
#endif
#include "DlgUnitsCalculatorImp.h"
#include <Base/UnitsApi.h>
using namespace Gui::Dialog;
/* TRANSLATOR Gui::Dialog::DlgUnitsCalculator */
/**
* Constructs a DlgUnitsCalculator which is a child of 'parent', with the
* name 'name' and widget flags set to 'f'
*
* The dialog will by default be modeless, unless you set 'modal' to
* TRUE to construct a modal dialog.
*/
DlgUnitsCalculator::DlgUnitsCalculator( QWidget* parent, Qt::WFlags fl )
: QDialog( parent, fl )
{
// create widgets
setupUi(this);
this->setAttribute(Qt::WA_DeleteOnClose);
connect(this->ValueInput, SIGNAL(valueChanged(Base::Quantity)), this, SLOT(valueChanged(Base::Quantity)));
connect(this->ValueInput, SIGNAL(returnPressed () ), this, SLOT(returnPressed()));
connect(this->UnitInput, SIGNAL(valueChanged(Base::Quantity)), this, SLOT(unitValueChanged(Base::Quantity)));
connect(this->UnitInput, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
connect(this->pushButton_Help, SIGNAL(clicked()), this, SLOT(help()));
connect(this->pushButton_Close, SIGNAL(clicked()), this, SLOT(accept()));
connect(this->pushButton_Copy, SIGNAL(clicked()), this, SLOT(copy()));
connect(this->ValueInput, SIGNAL(parseError(QString)), this, SLOT(parseError(QString)));
connect(this->UnitInput, SIGNAL(parseError(QString)), this, SLOT(parseError(QString)));
this->ValueInput->setParamGrpPath(QByteArray("User parameter:BaseApp/History/UnitsCalculator"));
actUnit.setInvalid();
}
/** Destroys the object and frees any allocated resources */
DlgUnitsCalculator::~DlgUnitsCalculator()
{
}
void DlgUnitsCalculator::accept()
{
QDialog::accept();
}
void DlgUnitsCalculator::reject()
{
QDialog::reject();
}
void DlgUnitsCalculator::unitValueChanged(const Base::Quantity& unit)
{
actUnit = unit;
valueChanged(actValue);
}
void DlgUnitsCalculator::valueChanged(const Base::Quantity& quant)
{
if (actUnit.isValid()) {
if (actUnit.getUnit() != quant.getUnit()) {
this->ValueOutput->setText(tr("Unit mismatch"));
this->pushButton_Copy->setEnabled(false);
} else {
double value = quant.getValue()/actUnit.getValue();
QString val = QLocale::system().toString(value, 'f', Base::UnitsApi::getDecimals());
QString out = QString::fromAscii("%1 %2").arg(val).arg(this->UnitInput->text());
this->ValueOutput->setText(out);
this->pushButton_Copy->setEnabled(true);
}
} else {
//this->ValueOutput->setValue(quant);
this->ValueOutput->setText(quant.getUserString());
this->pushButton_Copy->setEnabled(true);
}
actValue = quant;
}
void DlgUnitsCalculator::parseError(const QString& errorText)
{
this->pushButton_Copy->setEnabled(false);
this->ValueOutput->setText(errorText);
}
void DlgUnitsCalculator::copy(void)
{
QClipboard *cb = QApplication::clipboard();
cb->setText(ValueOutput->text());
}
void DlgUnitsCalculator::help(void)
{
//TODO: call help page Std_UnitsCalculator
}
void DlgUnitsCalculator::returnPressed(void)
{
if (this->pushButton_Copy->isEnabled()) {
this->textEdit->append(this->ValueInput->text() + QString::fromAscii(" = ") + this->ValueOutput->text());
this->ValueInput->pushToHistory();
}
}
#include "moc_DlgUnitsCalculatorImp.cpp"