• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeutils API Reference
  • KDE Home
  • Contact Us
 

superkaramba

  • sources
  • kde-4.12
  • kdeutils
  • superkaramba
  • src
  • python
python/meter.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2003-2004 Adam Geitgey <adam@rootnode.org> *
3  * Copyright (C) 2003 Hans Karlsson <karlsson.h@home.se> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  ***************************************************************************/
10 
11 #if defined(_XOPEN_SOURCE) && !defined(__SUNPRO_CC)
12 #undef _XOPEN_SOURCE
13 #endif
14 
15 #include "python/meter.h"
16 
17 #include <Python.h>
18 #include <qobject.h>
19 #include "../karamba.h"
20 #include "../karambaapp.h"
21 #include "meters/meter.h"
22 #include "lineparser.h"
23 
24 #include <QList>
25 
26 bool checkKaramba(long widget)
27 {
28  /*
29  if (!karambaApp)
30  {
31  PyErr_SetString(PyExc_ValueError, "app pointer was 0.");
32  return false;
33  }
34  */
35  if (!widget) {
36  PyErr_SetString(PyExc_ValueError, "widget pointer was 0.");
37  return false;
38  }
39 /*
40  if (!karambaApp->hasKaramba((Karamba*)widget)) {
41  QString tmp;
42 
43  tmp.sprintf("no %x widget found.", (unsigned int)widget);
44  PyErr_SetString(PyExc_ValueError, tmp.toAscii().constData());
45  return false;
46  }
47 */
48  return true;
49 }
50 
51 bool checkMeter(long widget, long meter, const char* type)
52 {
53  if (!meter) {
54  PyErr_SetString(PyExc_ValueError, "meter pointer was 0.");
55  return false;
56  }
57  if (!((Karamba*)widget)->hasMeter((Meter*)meter)) {
58  QString tmp;
59 
60  tmp.sprintf("widget does not have meter %x.", (unsigned int)meter);
61  PyErr_SetString(PyExc_ValueError, tmp.toAscii().constData());
62  return false;
63  }
64  if (!((QObject*)meter)->inherits(type)) {
65  QString tmp;
66 
67  tmp.sprintf("meter is not type of %s.", type);
68  PyErr_SetString(PyExc_TypeError, tmp.toAscii().constData());
69  return false;
70  }
71  return true;
72 }
73 
74 bool checkKarambaAndMeter(long widget, long meter, const char* type)
75 {
76  return checkKaramba(widget) && checkMeter(widget, meter, type);
77 }
78 
79 // This just throws awya extra bytes.
80 // I guess there is a better way to do this...
81 QString fromUcs4(quint32* ucs4)
82 {
83  QString result = "";
84  while (*ucs4 != 0) {
85  result += QChar((quint16) * ucs4);
86  ++ucs4;
87  }
88  return result;
89 }
90 
91 // Converts a Python String to a QString with Unicode support
92 QString PyString2QString(PyObject* text)
93 {
94  QString qtext;
95  if (PyString_CheckExact(text)) {
96  char* t = PyString_AsString(text);
97  qtext = QString::fromAscii(t);
98  } else if (PyUnicode_CheckExact(text)) {
99  Py_UNICODE* t = PyUnicode_AsUnicode(text);
100  if (sizeof(Py_UNICODE) == 4)
101  qtext = fromUcs4((quint32*)t);
102  else
103  qtext.setUtf16((quint16*)t, sizeof(t) / 4);
104  } else {
105  // Error raise execption ...
106  }
107  return qtext;
108 }
109 
110 // Converts a QString to a Python String with Unicode support
111 PyObject* QString2PyString(QString string)
112 {
113  PyObject *pyString;
114 
115  const unsigned short* tmp = string.utf16();
116  bool dofree = false;
117 
118  if (tmp) {
119 #if Py_UNICODE_SIZE == 4
120 
121  Py_UNICODE* buf = new Py_UNICODE[string.length()];
122 
123  for (int i = 0; i < string.length(); i++) {
124  buf[i] = tmp[i];
125  }
126  dofree = true;
127 
128 #else
129 
130  Py_UNICODE* buf = (Py_UNICODE*) tmp;
131 
132 #endif
133 
134  pyString = PyUnicode_FromUnicode(buf, string.length());
135 
136  if (dofree) {
137  delete [] buf;
138  }
139  }
140  else
141  pyString = PyString_FromString("");
142 
143  return pyString;
144 }
145 
146 
147 long getMeter(long widget, char* name)
148 {
149  Karamba* theme = (Karamba*)widget;
150 
151  QGraphicsItem *meter;
152  QList<QGraphicsItem*> list = ((QGraphicsItemGroup*)theme)->children();
153  foreach(meter, list) {
154  if (((Meter*) meter)->objectName() == name)
155  return (long)(Meter*)meter;
156  }
157 
158  return 0;
159 }
160 
161 PyObject* py_getThemeMeter(PyObject *, PyObject *args, QString type)
162 {
163  long widget, meter;
164  char* name;
165  if (!PyArg_ParseTuple(args, (char*)"ls", &widget, &name))
166  return NULL;
167  if (!checkKaramba(widget))
168  return NULL;
169  if (!name)
170  return NULL;
171  meter = getMeter(widget, name);
172  if (!checkMeter(widget, meter, type.toAscii().constData()))
173  return NULL;
174  return (Py_BuildValue((char*)"l", meter));
175 }
176 
177 PyObject* py_getSize(PyObject *, PyObject *args, QString type)
178 {
179  long widget;
180  long meter;
181  if (!PyArg_ParseTuple(args, (char*)"ll", &widget, &meter))
182  return NULL;
183  if (!checkKarambaAndMeter(widget, meter, type.toAscii().constData()))
184  return NULL;
185  return Py_BuildValue((char*)"(i,i)", ((Meter*)meter)->getWidth(),
186  ((Meter*)meter)->getHeight());
187 }
188 
189 PyObject* py_resize(PyObject *, PyObject *args, QString type)
190 {
191  long widget, meter, x, y;
192  if (!PyArg_ParseTuple(args, (char*)"llll", &widget, &meter, &x, &y))
193  return NULL;
194  if (!checkKarambaAndMeter(widget, meter, type.toAscii().constData()))
195  return NULL;
196  ((Meter*)meter)->setSize(((Meter*)meter)->getX(), ((Meter*)meter)->getY(),
197  x, y);
198  return Py_BuildValue((char*)"l", 1);
199 }
200 
201 PyObject* py_getPos(PyObject *, PyObject *args, QString type)
202 {
203  long widget, meter;
204  if (!PyArg_ParseTuple(args, (char*)"ll", &widget, &meter))
205  return NULL;
206  if (!checkKarambaAndMeter(widget, meter, type.toAscii().constData()))
207  return NULL;
208  return Py_BuildValue((char*)"(i,i)", ((Meter*)meter)->getX(),
209  ((Meter*)meter)->getY());
210 }
211 
212 PyObject* py_move(PyObject *, PyObject *args, QString type)
213 {
214  long widget, meter, x, y;
215  if (!PyArg_ParseTuple(args, (char*)"llll", &widget, &meter, &x, &y))
216  return NULL;
217  if (!checkKarambaAndMeter(widget, meter, type.toAscii().constData()))
218  return NULL;
219 
220  ((Karamba*)widget)->moveMeter((Meter*)meter, (int)x, (int)y);
221 
222  return Py_BuildValue((char*)"l", 1);
223 }
224 
225 PyObject* py_hide(PyObject *, PyObject *args, QString type)
226 {
227  long widget, meter;
228  if (!PyArg_ParseTuple(args, (char*)"ll", &widget, &meter))
229  return NULL;
230  if (!checkKarambaAndMeter(widget, meter, type.toAscii().constData()))
231  return NULL;
232  ((Meter*)meter)->hide();
233  return Py_BuildValue((char*)"l", 1);
234 }
235 
236 PyObject* py_show(PyObject *, PyObject *args, QString type)
237 {
238  long widget, meter;
239  if (!PyArg_ParseTuple(args, (char*)"ll", &widget, &meter))
240  return NULL;
241  if (!checkKarambaAndMeter(widget, meter, type.toAscii().constData()))
242  return NULL;
243  ((Meter*)meter)->show();
244  return Py_BuildValue((char*)"l", 1);
245 }
246 
247 PyObject* py_getValue(PyObject *, PyObject *args, QString type)
248 {
249  long widget, meter;
250  if (!PyArg_ParseTuple(args, (char*)"ll", &widget, &meter))
251  return NULL;
252  if (!checkKarambaAndMeter(widget, meter, type.toAscii().constData()))
253  return NULL;
254  return Py_BuildValue((char*)"l", ((Meter*)meter)->getValue());
255 }
256 
257 PyObject* py_setValue(PyObject *, PyObject *args, QString type)
258 {
259  long widget, meter, l;
260  if (!PyArg_ParseTuple(args, (char*)"lll", &widget, &meter, &l))
261  return NULL;
262  if (!checkKarambaAndMeter(widget, meter, type.toAscii().constData()))
263  return NULL;
264  ((Meter*)meter)->setValue(l);
265  return Py_BuildValue((char*)"l", ((long)meter));
266 }
267 
268 PyObject* py_getStringValue(PyObject *, PyObject *args, QString type)
269 {
270  long widget, meter;
271  if (!PyArg_ParseTuple(args, (char*)"ll", &widget, &meter))
272  return NULL;
273  if (!checkKarambaAndMeter(widget, meter, type.toAscii().constData()))
274  return NULL;
275  return Py_BuildValue((char*)"O",
276  QString2PyString(((Meter*)meter)->getStringValue()));
277 }
278 
279 PyObject* py_setStringValue(PyObject *, PyObject *args, QString type)
280 {
281  long widget, meter;
282  PyObject* s;
283  if (!PyArg_ParseTuple(args, (char*)"llO", &widget, &meter, &s))
284  return NULL;
285  if (!s)
286  return NULL;
287  if (!checkKarambaAndMeter(widget, meter, type.toAscii().constData()))
288  return NULL;
289  ((Meter*)meter)->setValue(PyString2QString(s));
290  return Py_BuildValue((char*)"l", ((long)meter));
291 }
292 
293 PyObject* py_getMinMax(PyObject *, PyObject *args, QString type)
294 {
295  long widget, meter;
296  if (!PyArg_ParseTuple(args, (char*)"ll", &widget, &meter))
297  return NULL;
298  if (!checkKarambaAndMeter(widget, meter, type.toAscii().constData()))
299  return NULL;
300  return Py_BuildValue((char*)"(i,i)", ((Meter*)meter)->getMax(),
301  ((Meter*)meter)->getMin());
302 }
303 
304 PyObject* py_setMinMax(PyObject *, PyObject *args, QString type)
305 {
306  long widget, meter, x, y;
307  if (!PyArg_ParseTuple(args, (char*)"llll", &widget, &meter, &x, &y))
308  return NULL;
309  if (!checkKarambaAndMeter(widget, meter, type.toAscii().constData()))
310  return NULL;
311  ((Meter*)meter)->setMin(x);
312  ((Meter*)meter)->setMax(y);
313  return Py_BuildValue((char*)"l", 1);
314 }
315 
316 PyObject* py_getSensor(PyObject *, PyObject *args, QString type)
317 {
318  long widget, meter;
319  QString s;
320  if (!PyArg_ParseTuple(args, (char*)"ll", &widget, &meter))
321  return NULL;
322  if (!checkKarambaAndMeter(widget, meter, type.toAscii().constData()))
323  return NULL;
324  return Py_BuildValue((char*)"s",
325  ((Karamba*)widget)->getSensor((Meter*)meter).toAscii().constData());
326 }
327 
328 PyObject* py_setSensor(PyObject *, PyObject *args, QString type)
329 {
330  long widget, meter;
331  char* s;
332 
333  if (!PyArg_ParseTuple(args, (char*)"lls", &widget, &meter, &s))
334  return NULL;
335  if (!checkKarambaAndMeter(widget, meter, type.toAscii().constData()))
336  return NULL;
337  ((Karamba*)widget)->setSensor(LineParser(s), (Meter*)meter);
338  return Py_BuildValue((char*)"l", 1);
339 }
340 
341 PyObject* py_setColor(PyObject *, PyObject *args, QString type)
342 {
343  long widget, meter;
344  long r, g, b;
345  if (!PyArg_ParseTuple(args, (char*)"lllll", &widget, &meter, &r, &g, &b))
346  return NULL;
347  if (!checkKarambaAndMeter(widget, meter, type.toAscii().constData()))
348  return NULL;
349  ((Meter*)meter)->setColor(QColor(r, g, b));
350  return Py_BuildValue((char*)"l", 1);
351 }
352 
353 PyObject* py_getColor(PyObject *, PyObject *args, QString type)
354 {
355  long widget, meter;
356  if (!PyArg_ParseTuple(args, (char*)"ll", &widget, &meter))
357  return NULL;
358  if (!checkKarambaAndMeter(widget, meter, type.toAscii().constData()))
359  return NULL;
360  QColor color = ((Meter*)meter)->getColor();
361  return Py_BuildValue((char*)"(i,i,i)", color.red(), color.green(), color.blue());
362 }
363 
py_getThemeMeter
PyObject * py_getThemeMeter(PyObject *, PyObject *args, QString type)
Definition: python/meter.cpp:161
py_getMinMax
PyObject * py_getMinMax(PyObject *, PyObject *args, QString type)
Definition: python/meter.cpp:293
py_move
PyObject * py_move(PyObject *, PyObject *args, QString type)
Definition: python/meter.cpp:212
PyObject
struct _object PyObject
Definition: python/karamba.h:35
PyString2QString
QString PyString2QString(PyObject *text)
Definition: python/meter.cpp:92
meter.h
meter.h
py_hide
PyObject * py_hide(PyObject *, PyObject *args, QString type)
Definition: python/meter.cpp:225
QGraphicsItem
py_getSensor
PyObject * py_getSensor(PyObject *, PyObject *args, QString type)
Definition: python/meter.cpp:316
QObject
py_getSize
PyObject * py_getSize(PyObject *, PyObject *args, QString type)
Definition: python/meter.cpp:177
py_getValue
PyObject * py_getValue(PyObject *, PyObject *args, QString type)
Definition: python/meter.cpp:247
QString2PyString
PyObject * QString2PyString(QString string)
Definition: python/meter.cpp:111
Karamba
Definition: karamba.h:52
py_resize
PyObject * py_resize(PyObject *, PyObject *args, QString type)
Definition: python/meter.cpp:189
py_setValue
PyObject * py_setValue(PyObject *, PyObject *args, QString type)
Definition: python/meter.cpp:257
py_getColor
PyObject * py_getColor(PyObject *, PyObject *args, QString type)
Definition: python/meter.cpp:353
lineparser.h
LineParser
Definition: lineparser.h:6
getMeter
long getMeter(long widget, char *name)
Definition: python/meter.cpp:147
checkKaramba
bool checkKaramba(long widget)
Definition: python/meter.cpp:26
py_setMinMax
PyObject * py_setMinMax(PyObject *, PyObject *args, QString type)
Definition: python/meter.cpp:304
checkKarambaAndMeter
bool checkKarambaAndMeter(long widget, long meter, const char *type)
Definition: python/meter.cpp:74
py_show
PyObject * py_show(PyObject *, PyObject *args, QString type)
Definition: python/meter.cpp:236
py_getPos
PyObject * py_getPos(PyObject *, PyObject *args, QString type)
Definition: python/meter.cpp:201
py_setSensor
PyObject * py_setSensor(PyObject *, PyObject *args, QString type)
Definition: python/meter.cpp:328
Meter
Definition: meters/meter.h:23
py_setStringValue
PyObject * py_setStringValue(PyObject *, PyObject *args, QString type)
Definition: python/meter.cpp:279
QGraphicsItemGroup
fromUcs4
QString fromUcs4(quint32 *ucs4)
Definition: python/meter.cpp:81
py_getStringValue
PyObject * py_getStringValue(PyObject *, PyObject *args, QString type)
Definition: python/meter.cpp:268
checkMeter
bool checkMeter(long widget, long meter, const char *type)
Definition: python/meter.cpp:51
py_setColor
PyObject * py_setColor(PyObject *, PyObject *args, QString type)
Definition: python/meter.cpp:341
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:07:20 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

superkaramba

Skip menu "superkaramba"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • kremotecontrol
  • ktimer
  • kwallet
  • superkaramba
  • sweeper

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal