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

0001 //
0002 // C++ Implementation: cstatusvar
0003 //
0004 // Description: One variable entry in the status bar
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 "cstatusvar.h"
0024 
0025 #include "cactionmanager.h"
0026 #include "cstatusvarlist.h"
0027 #include "cvariablelist.h"
0028 
0029 struct cStatusVar::Private {
0030   QString variable, maxvariable;
0031   QString caption;
0032   bool percentage;
0033   cVariableList *vars;
0034 };
0035 
0036 cStatusVar::cStatusVar (cList *list) : cListObject (list)
0037 {
0038   d = new Private;
0039   d->percentage = false;
0040   d->vars = nullptr;
0041 }
0042 
0043 cStatusVar::~cStatusVar()
0044 {
0045   delete d;
0046 }
0047 
0048 void cStatusVar::objectMoved ()
0049 {
0050   // object position changed - must update the status bar
0051   ((cStatusVarList *) list())->scheduleUpdate();
0052 }
0053 
0054 void cStatusVar::objectEnabled ()
0055 {
0056   // object position changed - must update the status bar
0057   ((cStatusVarList *) list())->scheduleUpdate();
0058 }
0059 
0060 void cStatusVar::objectDisabled ()
0061 {
0062   // object position changed - must update the status bar
0063   ((cStatusVarList *) list())->scheduleUpdate();
0064 }
0065 
0066 void cStatusVar::attribChanged (const QString &name)
0067 {
0068   if (name == "variable") {
0069     d->variable = strVal ("variable");
0070     updateVisibleName ();
0071     // remove leading $
0072     // note that this will lead to this function being called again
0073     if ((!d->variable.isEmpty()) && (d->variable[0] == '$'))
0074       setStr ("variable", d->variable.mid (1));
0075     ((cStatusVarList *) list())->scheduleUpdate();
0076   }
0077   else if (name == "max-variable") {
0078     d->maxvariable = strVal ("max-variable");
0079     // remove leading $
0080     // note that this will lead to this function being called again
0081     if ((!d->maxvariable.isEmpty()) && (d->maxvariable[0] == '$'))
0082       setStr ("max-variable", d->maxvariable.mid (1));
0083     ((cStatusVarList *) list())->scheduleUpdate();
0084   }
0085   else if (name == "caption") {
0086     d->caption = strVal ("caption");
0087     updateVisibleName ();
0088     ((cStatusVarList *) list())->scheduleUpdate();
0089   }
0090   else if (name == "percentage") {
0091     d->percentage = boolVal ("percentage");\
0092     ((cStatusVarList *) list())->scheduleUpdate();
0093   }
0094 }
0095 
0096 void cStatusVar::updateVisibleName()
0097 {
0098   if (d->variable.isEmpty() && d->caption.isEmpty())
0099     cListObject::updateVisibleName();
0100   else
0101     setVisibleName (d->caption.isEmpty() ? d->variable : d->caption + " (" + d->variable + ")");
0102 }
0103 
0104 cList::TraverseAction cStatusVar::traverse (int traversalType)
0105 {
0106   if (traversalType == STATUSVAR_MATCH) {
0107     cStatusVarList *svl = (cStatusVarList *) list();
0108     QString variable = svl->variableName();
0109     if ((variable == d->variable) || (variable == d->maxvariable))
0110       // we'll need an update after this is done
0111       svl->scheduleUpdate ();
0112     return cList::Continue;
0113   }
0114   if (traversalType == STATUSVAR_UPDATE) {
0115     updateBar ();
0116     return cList::Continue;
0117   }
0118   return cList::Stop;
0119 }
0120 
0121 void cStatusVar::updateBar ()
0122 {
0123   QString text;
0124 
0125   if (!d->vars)
0126     d->vars = dynamic_cast<cVariableList *>(cActionManager::self()->object ("variables", list()->session()));
0127 
0128   text = d->caption.isEmpty() ? QString() : (d->caption + " ");
0129 
0130   if (d->percentage) {
0131     // if we want percentage, we'll need integer values
0132     int val = d->vars->getIntValue (d->variable);
0133     int maxVal = d->vars->exists (d->maxvariable) ? d->vars->getIntValue (d->maxvariable) : 100;
0134     
0135     int percent = (maxVal == 0) ? (val * 100 / maxVal) : 0;
0136     text += QString::number (percent) + "%";
0137   } else {
0138     // no percentage - text values
0139     text += d->vars->getValue (d->variable);
0140     if (d->vars->exists (d->maxvariable))
0141       text += "/" + d->vars->getValue (d->maxvariable);
0142   }
0143 
0144   ((cStatusVarList *) list())->addToStatusBar (text);
0145 }
0146