SourceForge.net Logo
PythonQt
PythonQtObjectPtr.h
Go to the documentation of this file.
1 #ifndef _PYTHONQTOBJECTPTR_H
2 #define _PYTHONQTOBJECTPTR_H
3 
4 /*
5  *
6  * Copyright (C) 2010 MeVis Medical Solutions AG All Rights Reserved.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * Further, this software is distributed without any warranty that it is
19  * free of the rightful claim of any third person regarding infringement
20  * or the like. Any license provided herein, whether implied or
21  * otherwise, applies only to this software file. Patent licenses, if
22  * any, provided herein do not apply to combinations of this program with
23  * other software, or any other product whatsoever.
24  *
25  * You should have received a copy of the GNU Lesser General Public
26  * License along with this library; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28  *
29  * Contact information: MeVis Medical Solutions AG, Universitaetsallee 29,
30  * 28359 Bremen, Germany or:
31  *
32  * http://www.mevis.de
33  *
34  */
35 
36 //----------------------------------------------------------------------------------
43 //----------------------------------------------------------------------------------
44 
45 #include "PythonQtPythonInclude.h"
46 
47 #include "PythonQtSystem.h"
48 #include <QVariant>
49 #include <QVariantList>
50 #include <QVariantMap>
51 
54 {
55 public:
56  PythonQtObjectPtr():_object(NULL) {}
57 
59  :_object(NULL) {
60  setObject(p.object());
61  }
62 
64  PythonQtObjectPtr(const QVariant& variant):_object(NULL) {
65  fromVariant(variant);
66  }
67 
69 
71 
73  bool fromVariant(const QVariant& variant);
74 
76  setObject(p.object());
77  return *this;
78  }
79 
81  setObject(o);
82  return *this;
83  }
84 
85 
86  PythonQtObjectPtr &operator=(const QVariant& variant) {
87  fromVariant(variant);
88  return *this;
89  }
90 
91 
92  bool operator==( const PythonQtObjectPtr &p ) const {
93  return object() == p.object();
94  }
95 
96  bool operator!= ( const PythonQtObjectPtr& p ) const {
97  return !( *this == p );
98  }
99 
100  bool operator==( PyObject* p ) const {
101  return object() == p;
102  }
103 
104  bool operator!= ( PyObject* p ) const {
105  return object() != p;
106  }
107 
108  bool isNull() const { return !object(); }
109 
110  PyObject* operator->() const { return object(); }
111 
112  PyObject& operator*() const { return *( object() ); }
113 
114  operator PyObject*() const { return object(); }
115 
117  void setNewRef(PyObject* o);
118 
119  PyObject* object() const {
120  return _object;
121  }
122 
124  QVariant evalScript(const QString& script, int start = Py_file_input);
125 
128  QVariant evalCode(PyObject* pycode);
129 
131  void evalFile(const QString& filename);
132 
134  void addObject(const QString& name, QObject* object);
135 
137  void addVariable(const QString& name, const QVariant& v);
138 
140  void removeVariable(const QString& name);
141 
143  QVariant getVariable(const QString& name);
144 
146  QVariant call(const QString& callable, const QVariantList& args = QVariantList(), const QVariantMap& kwargs = QVariantMap());
147 
149  QVariant call(const QVariantList& args = QVariantList(), const QVariantMap& kwargs = QVariantMap());
150 
151 protected:
152 
153  void setObject(PyObject* o);
154 
155 private:
156  PyObject* _object;
157 };
158 
159 
160 // register it to the meta type system
161 Q_DECLARE_METATYPE(PythonQtObjectPtr)
162 
163 #endif
164 
bool operator==(PyObject *p) const
PyObject * operator->() const
PythonQtObjectPtr & operator=(const QVariant &variant)
PythonQtObjectPtr & operator=(const PythonQtObjectPtr &p)
PythonQtObjectPtr(const PythonQtObjectPtr &p)
PyObject * object() const
#define PYTHONQT_EXPORT
struct _object PyObject
a smart pointer that stores a PyObject pointer and that handles reference counting automatically ...
PythonQtObjectPtr(const QVariant &variant)
If the given variant holds a PythonQtObjectPtr, extract the value from it and hold onto the reference...
bool isNull() const
bool operator==(const PythonQtObjectPtr &p) const
PyObject & operator*() const
PythonQtObjectPtr & operator=(PyObject *o)