[KLF Backend][KLF Tools][KLF Home]
KLatexFormula Project
src/klftools/klfunitinput.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *   file klfunitinput.cpp
00003  *   This file is part of the KLatexFormula Project.
00004  *   Copyright (C) 2011 by Philippe Faist
00005  *   philippe.faist at bluewin.ch
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************/
00022 /* $Id$ */
00023 
00024 #include <math.h>
00025 
00026 #include <QDebug>
00027 #include <QVariant>
00028 #include <QStringList>
00029 #include <QComboBox>
00030 #include <QDoubleSpinBox>
00031 #include <QEvent>
00032 
00033 #include "klfunitinput.h"
00034 
00035 
00036 KLFUnitChooser::KLFUnitChooser(QWidget *parent)
00037   : QComboBox(parent)
00038 {
00039   connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(internalCurrentIndexChanged(int)));
00040 }
00041 KLFUnitChooser::~KLFUnitChooser()
00042 {
00043 }
00044 
00045 void KLFUnitChooser::setUnits(const QString& unitstr)
00046 {
00047   QStringList unitstrlist = unitstr.split(';');
00048   QList<Unit> units;
00049   int k;
00050   for (k = 0; k < unitstrlist.size(); ++k) {
00051     QStringList parts = unitstrlist[k].split('=');
00052     if (parts.size() != 3) {
00053       qWarning()<<KLF_FUNC_NAME<<": Invalid unit specification: "<<unitstrlist[k];
00054       continue;
00055     }
00056     Unit u;
00057     u.name = parts[0];
00058     u.abbrev = parts[1];
00059     u.factor = parts[2].toDouble();
00060     units << u;
00061   }
00062   setUnits(units);
00063 }
00064 
00065 void KLFUnitChooser::setUnits(const QList<Unit>& unitlist)
00066 {
00067   clear(); // clear combo box
00068   pUnits = unitlist;
00069   int k;
00070   for (k = 0; k < pUnits.size(); ++k) {
00071     Unit u = pUnits[k];
00072     // add this cbx item
00073     addItem(u.name, QVariant::fromValue<Unit>(u));
00074   }
00075 }
00076 
00077 QString KLFUnitChooser::unitStringDescription() const
00078 {
00079   QStringList l;
00080   int k;
00081   for (k = 0; k < pUnits.size(); ++k)
00082     l << QString("%2=%3=%1").arg(pUnits[k].factor, 0, 'g').arg(pUnits[k].name, pUnits[k].abbrev);
00083   return l.join(";");
00084 }
00085 
00086 void KLFUnitChooser::setCurrentUnit(const QString& unitname)
00087 {
00088   int k;
00089   for (k = 0; k < count(); ++k) {
00090     if (itemData(k).value<Unit>().name == unitname) {
00091       setCurrentUnitIndex(k);
00092       return;
00093     }
00094   }
00095   qWarning()<<KLF_FUNC_NAME<<": unit "<<unitname<<" not found.";
00096 }
00097 void KLFUnitChooser::setCurrentUnitAbbrev(const QString& unitAbbrev)
00098 {
00099   int k;
00100   for (k = 0; k < count(); ++k) {
00101     if (itemData(k).value<Unit>().abbrev == unitAbbrev) {
00102       setCurrentUnitIndex(k);
00103       return;
00104     }
00105   }
00106   qWarning()<<KLF_FUNC_NAME<<": unit abbrev. "<<unitAbbrev<<" not found.";
00107 }
00108 
00109 // private
00110 void KLFUnitChooser::setCurrentUnitIndex(int k)
00111 {
00112   if (isEnabled()) {
00113     setCurrentIndex(k);
00114   } else {
00115     pDelayedUnitSet = pUnits[k].name;
00116     emit unitChanged(pUnits[k].name);
00117     emit unitChanged(pUnits[k].factor);
00118     emit unitChanged(pUnits[k].factor, pUnits[k].abbrev);
00119   }
00120 }
00121 
00122 void KLFUnitChooser::changeEvent(QEvent *event)
00123 {
00124   if (event->type() == QEvent::EnabledChange) {
00125     if (isEnabled() && !pDelayedUnitSet.isEmpty()) {
00126       setCurrentUnit(pDelayedUnitSet);
00127       pDelayedUnitSet = QString();
00128     }
00129   }
00130   QComboBox::changeEvent(event);
00131 }
00132 
00133 
00134 void KLFUnitChooser::internalCurrentIndexChanged(int index)
00135 {
00136   if (index < 0 || index >= count())
00137     return;
00138 
00139   Unit u = itemData(index).value<Unit>();
00140   klfDbg("New unit selected : #"<<index<<" = "<<u.name) ;
00141   emit unitChanged(u.name);
00142   emit unitChanged(u.factor);
00143   emit unitChanged(u.factor, u.abbrev);
00144 }
00145 
00146 
00147 // -------------------------
00148 
00149 
00150 KLFUnitSpinBox::KLFUnitSpinBox(QWidget *parent)
00151   : QDoubleSpinBox(parent)
00152 {
00153   pUnitFactor = 1.0f;
00154   pShowUnitSuffix = true;
00155   connect(this, SIGNAL(valueChanged(double)), this, SLOT(internalValueChanged(double)));
00156 }
00157 KLFUnitSpinBox::~KLFUnitSpinBox()
00158 {
00159 }
00160 
00161 void KLFUnitSpinBox::setUnit(double unitfactor)
00162 {
00163   double curValue = value();
00164   double curMinimum = minimum();
00165   double curMaximum = maximum();
00166   double curUnitFactor = pUnitFactor;
00167   int curPrecision = decimals();
00168 
00169   klfDbg("unitfactor="<<unitfactor<<" cur: val="<<curValue<<",min="<<curMinimum<<",max="<<curMaximum
00170          <<",prec="<<curPrecision) ;
00171 
00172   pUnitFactor = unitfactor;
00173 
00174   // calculate the right number of decimal places.
00175   // due to round-off errors, we need to first determine how many decimals we started
00176   // off with, then re-calculate the number of decimals.
00177   int unitRefDecimals = curPrecision - (int)( log((double)curUnitFactor)/log((double)10.0)  + 0.5 );
00178 
00179   setDecimals(unitRefDecimals + (int)( log((double)pUnitFactor)/log((double)10.0) + 0.5 ) );
00180 
00181   // set the appropriate range
00182   setMinimum(curMinimum * curUnitFactor / pUnitFactor);
00183   setMaximum(curMaximum * curUnitFactor / pUnitFactor);
00184 
00185   // and set the value
00186   setValue(curValue * curUnitFactor / pUnitFactor);
00187 }
00188 
00189 void KLFUnitSpinBox::setUnitWithSuffix(double unitfactor, const QString& suffix)
00190 {
00191   setUnit(unitfactor);
00192   if (pShowUnitSuffix)
00193     setSuffix("  "+suffix);
00194 }
00195 
00196 void KLFUnitSpinBox::setValueInRefUnit(double value)
00197 {
00198   setValue(value / pUnitFactor);
00199 }
00200 
00201 void KLFUnitSpinBox::setShowUnitSuffix(bool show)
00202 {
00203   pShowUnitSuffix = show;
00204 }
00205 
00206 
00207 void KLFUnitSpinBox::internalValueChanged(double valueInExtUnits)
00208 {
00209   klfDbg("val in ext. units="<<valueInExtUnits<<"; our unitfactor="<<pUnitFactor) ;
00210   emit valueInRefUnitChanged(valueInExtUnits / pUnitFactor);
00211 }
00212 
00213 
00214 
00215 

Generated by doxygen 1.7.6.1. The KLatexFormula website is hosted on sourceforge.net