Petri Damstén - 2003-05-30

This patch gives following functions to handle bar meter:
createBar
deleteBar
getThemeBar
getBarSize
resizeBar
getBarPos
moveBar
hideBar
showBar
setBarSensor
getBarSensor
setBarImage
getBarImage
setBarVertical
getBarVertical
setBarValue
getBarValue
setBarMinMax
getBarMinMax

Patch is also found here with example theme:
http://koti.mbnet.fi/damu/bar_functions.tar.gz

-Petri Damstn-

diff -Naur superkaramba-0.26/src/bar.cpp superkaramba-new/src/bar.cpp
--- superkaramba-0.26/src/bar.cpp    Mon Apr 21 16:17:03 2003
+++ superkaramba-new/src/bar.cpp    Fri May 30 13:58:43 2003
@@ -13,14 +13,17 @@
     value = 0;
     minValue = 0;
     maxValue = 100;
+    barValue = 0;
+    vertical = FALSE;
}

Bar::~Bar()
{}

-void Bar::setImage( QString fileName )
+bool Bar::setImage( QString fileName )
{
   QFileInfo fileInfo( fileName );
+  bool res;
   QString path;

   if( fileInfo.isRelative() )
@@ -32,7 +35,7 @@
      path = fileName;
   }
     //qDebug( "bar: fileName=" + path );
-    pixmap.load( path );
+    res = pixmap.load( path );
     pixmapWidth = pixmap.width();
     pixmapHeight = pixmap.height();

@@ -41,6 +44,9 @@
         width = pixmapWidth;
     height = pixmapHeight;
     }
+  if (res)
+    imagePath = path;
+  return res;
}

void Bar::setValue( int v )
@@ -57,6 +63,8 @@
     v = minValue;
     }

+    barValue = v;
+
     int diff = maxValue-minValue;
     if ( diff != 0)
     {
@@ -98,6 +106,9 @@

void Bar::mUpdate( QPainter *p )
{
+  //only draw image if not hidden
+  if (hidden == 0)
+  {
     if( vertical )
     {
         //  int v = int( (value-minValue)*height / (maxValue-minValue) + 0.5 );
@@ -108,4 +119,5 @@
         //int v = int( (value-minValue)*width / (maxValue-minValue) + 0.5 );
         p->drawTiledPixmap( x, y, value, height, pixmap);
     }
+  }
}
diff -Naur superkaramba-0.26/src/bar.h superkaramba-new/src/bar.h
--- superkaramba-0.26/src/bar.h    Mon Apr 21 16:17:03 2003
+++ superkaramba-new/src/bar.h    Fri May 30 13:57:53 2003
@@ -24,18 +24,24 @@

public slots:
-    void setImage( QString imagePath );
+    bool setImage( QString imagePath );
+    QString getImage() { return imagePath; };

     void setValue( int );
+    int  getBarValue() { return barValue; };
     void setValue( QString );

     void setMax( int );
     void setMin( int );
+    int  getMax() { return maxValue; };
+    int  getMin() { return minValue; };

     void setVertical( bool );
+    int  getVertical() { return vertical; };

private:
+    int barValue;
     int value;
     int maxValue;
     int minValue;
@@ -45,6 +51,7 @@

     bool vertical; // vertical bar?

+    QString imagePath;
     QPixmap pixmap;

}
diff -Naur superkaramba-0.26/src/karamba.cpp superkaramba-new/src/karamba.cpp
--- superkaramba-0.26/src/karamba.cpp    Wed May 21 08:43:53 2003
+++ superkaramba-new/src/karamba.cpp    Fri May 30 13:52:30 2003
@@ -11,6 +11,8 @@
#define EDITSCRIPT 1
#define THEMECONF  2

+#include "pyapi.cpp"
+
long createMenu(long widget){
   karamba* currTheme = (karamba*)widget;

@@ -1385,6 +1387,16 @@
}

static PyMethodDef karamba_methods[] = {
+  PY_API_DECLARE_BASIC(Bar)
+  {"setBarImage", py_setBarImage, METH_VARARGS, "Set bar image"},
+  {"getBarImage", py_getBarImage, METH_VARARGS, "Get bar image"},
+  {"setBarVertical", py_setBarVertical, METH_VARARGS, "Set bar orientation"},
+  {"getBarVertical", py_getBarVertical, METH_VARARGS, "Get bar orientation"},
+  {"setBarValue", py_setBarValue, METH_VARARGS, "Set bar value"},
+  {"getBarValue", py_getBarValue, METH_VARARGS, "Get bar value"},
+  {"setBarMinMax", py_setBarMinMax, METH_VARARGS, "Set bar min & max"},
+  {"getBarMinMax", py_getBarMinMax, METH_VARARGS, "Get bar min & max"},
+
   {"addMenuConfigOption", py_add_menu_config_option, METH_VARARGS, "Add a configuration entry to the menu"},
   {"setMenuConfigOption", py_set_menu_config_option, METH_VARARGS, "Set a configuration entry in the menu"},
   {"readMenuConfigOption", py_read_menu_config_option, METH_VARARGS, "Read a configuration entry in the menu"},
@@ -2160,6 +2172,9 @@
                 if( minFound )
                     tmp->setMin( min );
                 tmp->setValue( getInt( "VALUE", line ) );
+                QString name = getString("NAME",line);
+                if (name != "")
+                    tmp->setName(name.ascii());
                 setSensor( line, (Meter*)tmp );
                 meterList->append ( tmp );
             }
@@ -2469,6 +2484,47 @@
     return  ( getString( w, line, found ).lower() == "true")?true:false;
}

+QString karamba::findSensorFromMap(Sensor* sensor)
+{
+  QMap<QString,Sensor*>::Iterator it;
+
+  for ( it = sensorMap.begin(); it != sensorMap.end(); ++it )
+    {
+        if (it.data() == sensor)
+          return it.key();
+    }
+  return "";
+}
+
+Sensor* karamba::findSensorFromList(Meter* meter)
+{
+  QObjectListIt it( *sensorList ); // iterate over meters
+
+  while ( it != 0 )
+    {
+        if (((Sensor*) *it)->hasMeter(meter))
+            return ((Sensor*)*it);
+        ++it;
+    }
+  return NULL;
+}
+
+void karamba::deleteMeterFromSensors(Meter* meter)
+{
+  Sensor* sensor = findSensorFromList(meter);
+
+  if (sensor)
+    {
+        sensor->deleteMeter(meter);
+        if (sensor->isEmpty())
+          {
+            QString s = findSensorFromMap(sensor);
+            sensorMap.erase(s);
+            sensorList->removeRef(sensor);
+          }
+    }
+}
+
void karamba::setSensor(QString &line, Meter* meter)
{
     Sensor* sensor = 0;
diff -Naur superkaramba-0.26/src/karamba.h superkaramba-new/src/karamba.h
--- superkaramba-0.26/src/karamba.h    Wed May 21 07:34:27 2003
+++ superkaramba-new/src/karamba.h    Fri May 30 13:56:24 2003
@@ -98,6 +98,9 @@
     //    QObjectList *clickList;
     list<Clickable*> *clickList;
     void setSensor( QString &line , Meter* meter);
+    QString findSensorFromMap(Sensor* sensor);
+    void deleteMeterFromSensors(Meter* meter);
+    Sensor* findSensorFromList(Meter* meter);
     KPopupMenu* keditpop;
     KPopupMenu *kpop;
     QBitmap* widgetMask;
diff -Naur superkaramba-0.26/src/meter.cpp superkaramba-new/src/meter.cpp
--- superkaramba-0.26/src/meter.cpp    Sun May 11 09:11:20 2003
+++ superkaramba-new/src/meter.cpp    Fri May 30 14:12:57 2003
@@ -15,6 +15,7 @@
   y = iy;
   width = iw;
   height = ih;
+  hidden  = 0;
}
Meter::Meter()
{
diff -Naur superkaramba-0.26/src/meter.h superkaramba-new/src/meter.h
--- superkaramba-0.26/src/meter.h    Sun May 11 09:10:32 2003
+++ superkaramba-new/src/meter.h    Fri May 30 13:55:28 2003
@@ -44,12 +44,15 @@
   virtual void setValue(int){}
   virtual void setValue(QString){}

+  void show() { hidden = 0; };
+  void hide() { hidden = 1; };

protected: // Protected attributes
   int x;
   int y;
   int width;
   int height;
+  int hidden;
   QString themePath;

};
diff -Naur superkaramba-0.26/src/pyapi.cpp superkaramba-new/src/pyapi.cpp
--- superkaramba-0.26/src/pyapi.cpp    Thu Jan  1 02:00:00 1970
+++ superkaramba-new/src/pyapi.cpp    Fri May 30 11:01:47 2003
@@ -0,0 +1,86 @@
+#include "pyapi_templates.h"
+
+long getMeter(long widget, char* name) {
+  if (!widget)
+    return 0;
+  if (!((QObject*)widget)->isA("karamba"))
+    return 0;
+
+ karamba* theme = (karamba*)widget;
+ QObjectListIt it( *theme->meterList ); // iterate over meters
+
+ while ( it != 0 )
+ {
+   if (strcmp(((Meter*) *it)->name(), name) == 0)
+     return (long)*it;
+   ++it;
+ }
+ return 0;
+}
+
+PyObject* toPyObject(QPoint& p)
+{
+  return Py_BuildValue("(i,i)", p.x(), p.y());
+}
+
+PyObject* toPyObject(int i)
+{
+  return Py_BuildValue("i", i);
+}
+
+PyObject* toPyObject(QString s)
+{
+  return Py_BuildValue("s", s.ascii());
+}
+
+PY_API_BASIC(Bar, Bar)
+
+PY_API_METER_CREATE(createBar, Bar)
+  Bar *tmp = new Bar(x,y,w,h);
+  tmp->setThemePath(theme->themePath);
+  if (!s.isEmpty())
+    tmp->setImage(s);
+  theme->meterList->append(tmp);
+PY_API_RETURN((long)tmp)
+
+PY_API_METER_0(deleteBar, Bar)
+  theme->deleteMeterFromSensors(meter);
+  long res = theme->meterList->removeRef(meter);
+  //meterList is autoDelete(true) so we don't have to delete the bar here
+PY_API_RETURN(res)
+
+PY_API_METER_0(getBarImage, Bar)
+  QString path = meter->getImage();
+PY_API_RETURN(path)
+
+PY_API_METER_S(setBarImage, Bar)
+  long res = (long) meter->setImage(s);
+PY_API_RETURN(res)
+
+PY_API_METER_0(getBarVertical, Bar)
+  long ori = (long)meter->getVertical();
+PY_API_RETURN(ori)
+
+PY_API_METER_L(setBarVertical, Bar)
+  meter->setVertical(l);
+PY_API_RETURN(1)
+
+PY_API_METER_0(getBarValue, Bar)
+  long val = (long)meter->getBarValue();
+PY_API_RETURN(val)
+
+PY_API_METER_L(setBarValue, Bar)
+  meter->setValue(l);
+PY_API_RETURN(1)
+
+PY_API_METER_0(getBarMinMax, Bar)
+  QPoint minmax(meter->getMin(), meter->getMax());
+PY_API_RETURN(minmax)
+
+PY_API_METER_P(setBarMinMax, Bar)
+  meter->setMin(point.x());
+  meter->setMax(point.y());
+PY_API_RETURN(1)
+
+//PY_API_BASIC(Image, ImageLabel)
+//PY_API_BASIC(Text, TextLabel)
diff -Naur superkaramba-0.26/src/pyapi_templates.h superkaramba-new/src/pyapi_templates.h
--- superkaramba-0.26/src/pyapi_templates.h    Thu Jan  1 02:00:00 1970
+++ superkaramba-new/src/pyapi_templates.h    Fri May 30 12:51:14 2003
@@ -0,0 +1,153 @@
+#define PY_API_RETURN(__value) \
+  return (toPyObject(__value));\
+}
+
+#define PY_API_GET_METER(__name, __type) \
+static PyObject* py_getTheme ## __name(PyObject *self, PyObject *args)\
+{\
+  long widget, meter;\
+  char* name;\
+  if (!PyArg_ParseTuple(args, "ls", &widget, &name))\
+    return NULL;\
+  meter = getMeter(widget, name);\
+  if (meter)\
+    if (!((QObject*)meter)->isA(#__type))\
+      return NULL;\
+  return (toPyObject(meter));\
+}
+
+#define PY_API_METER_CREATE(__func, __type) \
+PyObject* __func(karamba* theme, int x, int y, int w, int h, QString s);\
+static PyObject* py_ ## __func(PyObject *self, PyObject *args)\
+{\
+  long widget, x, y, w, h;\
+  char *text;\
+  if (!PyArg_ParseTuple(args, "lllll|s", &widget, &x, &y, &w, &h, &text))\
+    return NULL;\
+  if (!widget)\
+    return NULL;\
+  if (!((QObject*)widget)->isA("karamba"))\
+    return NULL;\
+  return __func((karamba*)widget, x, y, w, h, QString(text));\
+}\
+PyObject* __func(karamba* theme, int x, int y, int w, int h, QString s)\
+{
+
+
+#define PY_API_METER_0(__func, __type) \
+PyObject* __func(karamba* theme, __type* meter);\
+static PyObject* py_ ## __func(PyObject *self, PyObject *args)\
+{\
+  long widget, meter;\
+  if (!PyArg_ParseTuple(args, "ll", &widget, &meter))\
+    return NULL;\
+  if (!widget || !meter)\
+    return NULL;\
+  if (!((QObject*)widget)->isA("karamba") || !((QObject*)meter)->isA(#__type))\
+    return NULL;\
+  return __func((karamba*)widget, (__type*)meter);\
+}\
+PyObject* __func(karamba* theme, __type* meter)\
+{
+
+#define PY_API_METER_P(__func, __type) \
+PyObject* __func(karamba* theme, __type* meter, QPoint point);\
+static PyObject* py_ ## __func(PyObject *self, PyObject *args)\
+{\
+  long widget, meter, x, y;\
+  if (!PyArg_ParseTuple(args, "llll", &widget, &meter, &x, &y))\
+    return NULL;\
+  if (!widget || !meter)\
+    return NULL;\
+  if (!((QObject*)widget)->isA("karamba") || !((QObject*)meter)->isA(#__type))\
+    return NULL;\
+  return __func((karamba*)widget, (__type*)meter, QPoint(x,y));\
+}\
+PyObject* __func(karamba* theme, __type* meter, QPoint point)\
+{
+
+#define PY_API_METER_S(__func, __type) \
+PyObject* __func(karamba* theme, __type* meter, QString s);\
+static PyObject* py_ ## __func(PyObject *self, PyObject *args)\
+{\
+  long widget, meter;\
+  char* s;\
+  if (!PyArg_ParseTuple(args, "lls", &widget, &meter, &s))\
+    return NULL;\
+  if (!widget || !meter)\
+    return NULL;\
+  if (!((QObject*)widget)->isA("karamba") || !((QObject*)meter)->isA(#__type))\
+    return NULL;\
+  return __func((karamba*)widget, (__type*)meter, QString(s));\
+}\
+PyObject* __func(karamba* theme, __type* meter, QString s)\
+{
+
+#define PY_API_METER_L(__func, __type) \
+PyObject* __func(karamba* theme, __type* meter, long l);\
+static PyObject* py_ ## __func(PyObject *self, PyObject *args)\
+{\
+  long widget, meter, l;\
+  char* s;\
+  if (!PyArg_ParseTuple(args, "lll", &widget, &meter, &l))\
+    return NULL;\
+  if (!widget || !meter)\
+    return NULL;\
+  if (!((QObject*)widget)->isA("karamba") || !((QObject*)meter)->isA(#__type))\
+    return NULL;\
+  return __func((karamba*)widget, (__type*)meter, l);\
+}\
+PyObject* __func(karamba* theme, __type* meter, long l)\
+{
+
+#define PY_API_BASIC(__name, __type) \
+PY_API_GET_METER(__name, __type) \
+PY_API_METER_0(get ## __name ## Size, __type)\
+  QPoint size(meter->getWidth(), meter->getHeight());\
+PY_API_RETURN(size)\
+\
+PY_API_METER_P(resize ## __name, __type)\
+  meter->setSize(meter->getX(), meter->getY(), point.x(), point.y());\
+PY_API_RETURN(1)\
+\
+PY_API_METER_0(get ## __name ## Pos, __type)\
+  QPoint pos(meter->getX(), meter->getY());\
+PY_API_RETURN(pos)\
+\
+PY_API_METER_P(move ## __name, __type)\
+  meter->setX(point.x());\
+  meter->setY(point.y());\
+PY_API_RETURN(1)\
+\
+PY_API_METER_0(hide ## __name, __type)\
+  meter->hide();\
+PY_API_RETURN(1)\
+\
+PY_API_METER_0(show ## __name, __type)\
+  meter->show();\
+PY_API_RETURN(1)\
+\
+PY_API_METER_0(get ## __name ## Sensor, __type)\
+  QString s;\
+  Sensor* sensor = theme->findSensorFromList(meter);\
+  if (sensor)\
+    s = theme->findSensorFromMap(sensor);\
+PY_API_RETURN(s)\
+\
+PY_API_METER_S(set ## __name ## Sensor, __type)\
+  theme->deleteMeterFromSensors(meter);\
+  theme->setSensor(s, meter);\
+PY_API_RETURN(1)
+
+#define PY_API_DECLARE_BASIC(__name)\
+  {"create" #__name, py_create ## __name, METH_VARARGS, "Create new ## __name ##."},\
+  {"delete" #__name, py_delete ## __name, METH_VARARGS, "Delete ## __name ##."},\
+  {"getTheme" #__name, py_getTheme ## __name, METH_VARARGS, "Get ## __name ## from .theme using it's name."},\
+  {"get" # __name "Size", py_get ## __name ## Size, METH_VARARGS, "Get ## __name ## size."},\
+  {"resize" # __name, py_resize ## __name, METH_VARARGS, "Resize ## __name ##."},\
+  {"get" # __name "Pos", py_get ## __name ## Pos, METH_VARARGS, "Get ## __name ## position."},\
+  {"move" # __name, py_move ## __name, METH_VARARGS, "Move ## __name ##."},\
+  {"hide" # __name, py_hide ## __name, METH_VARARGS, "Hide ## __name ##."},\
+  {"show" # __name, py_show ## __name, METH_VARARGS, "Show ## __name ##."},\
+  {"get" # __name "Sensor", py_get ## __name ## Sensor, METH_VARARGS, "Get ## __name ## sensor."},\
+  {"set" # __name "Sensor", py_set ## __name ## Sensor, METH_VARARGS, "Set ## __name ## sensor."},
diff -Naur superkaramba-0.26/src/sensor.cpp superkaramba-new/src/sensor.cpp
--- superkaramba-0.26/src/sensor.cpp    Mon Apr 21 16:17:03 2003
+++ superkaramba-new/src/sensor.cpp    Fri May 30 14:01:18 2003
@@ -32,8 +32,29 @@

void Sensor::addMeter( SensorParams *sp )
{
-objList->append(sp);
+  objList->append(sp);
+}
+
+SensorParams* Sensor::hasMeter( Meter *meter )
+{
+  QObjectListIt it( *objList );
+  while ( it != 0 )
+    {
+      if (((SensorParams*) *it)->getMeter() == meter)
+        {
+          return (SensorParams*) *it;
+        }
+      ++it;
+    }
+  return NULL;
+}
+
+void Sensor::deleteMeter( Meter *meter )
+{
+  SensorParams* sp = hasMeter(meter);

+  if (sp)
+    objList->removeRef(sp);
}

void Sensor::setMaxValue( SensorParams* )
diff -Naur superkaramba-0.26/src/sensor.h superkaramba-new/src/sensor.h
--- superkaramba-0.26/src/sensor.h    Mon Apr 21 16:17:03 2003
+++ superkaramba-new/src/sensor.h    Fri May 30 14:01:45 2003
@@ -27,6 +27,9 @@
     void start();
     virtual ~Sensor();
     void addMeter( SensorParams *s );
+    SensorParams* hasMeter( Meter *meter );
+    void deleteMeter( Meter *meter );
+    int isEmpty() { return objList->isEmpty(); };
     virtual void setMaxValue( SensorParams *s );

private: