File indexing completed on 2024-04-14 14:32:06

0001 //
0002 // C++ Implementation: cgauge
0003 //
0004 // Description: One gauge (no GUI)
0005 //
0006 /*
0007 Copyright 2004-2011 Tomas Mecir <kmuddy@kmuddy.com>
0008 
0009 This program is free software; you can redistribute it and/or
0010 modify it under the terms of the GNU General Public License as
0011 published by the Free Software Foundation; either version 2 of 
0012 the License, or (at your option) any later version.
0013 
0014 This program is distributed in the hope that it will be useful,
0015 but WITHOUT ANY WARRANTY; without even the implied warranty of
0016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017 GNU General Public License for more details.
0018 
0019 You should have received a copy of the GNU General Public License
0020 along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 
0023 #include "cgauge.h"
0024 
0025 #include "cactionmanager.h"
0026 #include "cgaugelist.h"
0027 #include "cgaugebar.h"
0028 #include "cgaugebaritem.h"
0029 #include "cvariablelist.h"
0030 
0031 struct cGauge::Private {
0032   QString variable, maxvariable;
0033   QString caption;
0034   QColor color;
0035   cGaugeBarItem *gaugeitem;
0036   cVariableList *vars;
0037 };
0038 
0039 cGauge::cGauge (cList *list) : cListObject (list)
0040 {
0041   d = new Private;
0042   
0043   d->color = Qt::white;
0044   d->vars = nullptr;
0045   
0046   QWidget *gaugeBar = dynamic_cast<QWidget *>(cActionManager::self()->object ("gaugebar", list->session()));
0047   d->gaugeitem = new cGaugeBarItem (gaugeBar);
0048 
0049   cGaugeList *gl = (cGaugeList *) list;
0050   gl->addGauge (this);
0051 }
0052 
0053 cGauge::~cGauge()
0054 {
0055   delete d->gaugeitem;
0056   d->gaugeitem = nullptr;
0057   delete d;
0058 }
0059 
0060 void cGauge::attribChanged (const QString &name)
0061 {
0062   if (name == "variable") {
0063     d->variable = strVal ("variable");
0064     updateVisibleName ();
0065     // remove leading $
0066     // note that this will lead to this function being called again
0067     if ((!d->variable.isEmpty()) && (d->variable[0] == '$'))
0068       setStr ("variable", d->variable.mid (1));
0069     updateGauge ();
0070   }
0071   else if (name == "max-variable") {
0072     d->maxvariable = strVal ("max-variable");
0073     // remove leading $
0074     // note that this will lead to this function being called again
0075     if ((!d->maxvariable.isEmpty()) && (d->maxvariable[0] == '$'))
0076       setStr ("max-variable", d->maxvariable.mid (1));
0077     updateGauge ();
0078   }
0079   else if (name == "caption") {
0080     d->caption = strVal ("caption");
0081     updateVisibleName ();
0082     updateGauge ();
0083   }
0084   else if (name == "color") {
0085     int color = intVal ("color");
0086     d->color.setBlue (color % 256);
0087     color /= 256;
0088     d->color.setGreen (color % 256);
0089     color /= 256;
0090     d->color.setRed (color % 256);
0091     updateGauge ();
0092   }
0093 }
0094 
0095 void cGauge::updateVisibleName()
0096 {
0097   if (d->variable.isEmpty() && d->caption.isEmpty())
0098     cListObject::updateVisibleName();
0099   else
0100     setVisibleName (d->caption.isEmpty() ? d->variable : d->caption + " (" + d->variable + ")");
0101 }
0102 
0103 cList::TraverseAction cGauge::traverse (int traversalType)
0104 {
0105   if (traversalType == GAUGE_MATCH) {
0106     cGaugeList *gl = (cGaugeList *) list();
0107     QString variable = gl->variableName();
0108     if ((variable == d->variable) || (variable == d->maxvariable))
0109       updateGauge ();
0110     return cList::Continue;
0111   }
0112   if (traversalType == GAUGE_UPDATE) {
0113     // the gauge isn't on the bar currently, so add it there
0114     cGaugeBar *bar = dynamic_cast<cGaugeBar *>(cActionManager::self()->object ("gaugebar", list()->session()));
0115     bar->addGauge (this);
0116     updateGauge ();
0117     return cList::Continue;
0118   }
0119   return cList::Stop;
0120 }
0121 
0122 void cGauge::updateGauge ()
0123 {
0124   if (!d->vars)
0125     d->vars = dynamic_cast<cVariableList *>(cActionManager::self()->object ("variables", list()->session()));
0126   int val = d->vars->getIntValue (d->variable);
0127   int maxVal = d->vars->exists (d->maxvariable) ? d->vars->getIntValue (d->maxvariable) : 100;
0128   int value = (maxVal != 0) ? (val * 100 / maxVal) : 0;
0129   if (value < 0) value = 0;
0130   if (value > 100) value = 100;
0131 
0132   d->gaugeitem->setValue (value);
0133   d->gaugeitem->setText (d->caption);
0134   d->gaugeitem->setColor (d->color);
0135 }
0136 
0137 cGaugeBarItem *cGauge::gaugeItem ()
0138 {
0139   return d->gaugeitem;
0140 }
0141 
0142 void cGauge::objectMoved ()
0143 {
0144   ((cGaugeList *) list())->updateGauges();
0145 }
0146 
0147 void cGauge::objectEnabled ()
0148 {
0149   ((cGaugeList *) list())->updateGauges();
0150 }
0151 
0152 void cGauge::objectDisabled ()
0153 {
0154   ((cGaugeList *) list())->updateGauges();
0155 }
0156