Refactor to use new protocol module and sipo.
[bertos.git] / wizard / qvariant_converter_old.py
1 #!/usr/bin/env python
2 # encoding: utf-8
3 #
4 # This file is part of BeRTOS.
5 #
6 # Bertos is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19 #
20 # As a special exception, you may use this file as part of a free software
21 # library without restriction.  Specifically, if other files instantiate
22 # templates or use macros or inline functions from this file, or you compile
23 # this file and link it with other files to produce an executable, this
24 # file does not by itself cause the resulting executable to be covered by
25 # the GNU General Public License.  This exception does not however
26 # invalidate any other reasons why the executable file might be covered by
27 # the GNU General Public License.
28 #
29 # Copyright 2008 Develer S.r.l. (http://www.develer.com/)
30 #
31 #
32 # Author: Lorenzo Berni <duplo@develer.com>
33 #
34
35 """
36 Awful module for the conversion from python types to qvariant, for make the wizard compatible with older version of pyqt (<= 4.4.3)
37 """
38
39 from PyQt4.QtCore import *
40 import pickle
41
42 def getString(qvariant):
43     if type(qvariant) == str or type(qvariant) == unicode:
44         string = qvariant
45     else:
46         string = unicode(qvariant.toString())
47     return string
48
49 def convertString(string):
50     return QVariant(string)
51
52 def getStringList(qvariant):
53     string_list = []
54     if type(qvariant) == list:
55         string_list = qvariant
56     else:
57         for element in qvariant.toStringList():
58             string_list.append(unicode(element))
59     return string_list
60
61 def convertStringList(string_list):
62     result = []
63     for element in string_list:
64         result.append(QString(element))
65     return QVariant(QStringList(result))
66
67 def getStringDict(qvariant):
68     a = str(qvariant.toByteArray())
69     if len(a) == 0:
70         dict_str_str = {}
71     else:
72         dict_str_str = pickle.loads(a)
73     return dict_str_str
74
75 def convertStringDict(dict_str_str):
76     a = pickle.dumps(dict_str_str)
77     return QVariant(QByteArray(a))
78
79 def getBool(qvariant):
80     return qvariant.toBool()
81
82 def convertBool(boolean):
83     return QVariant(boolean)
84
85 def getBoolDict(qvariant):
86     a = str(qvariant.toByteArray())
87     if len(a) == 0:
88         dict_str_bool = {}
89     else:
90         dict_str_bool = pickle.loads(a)
91     return dict_str_bool
92
93 def convertBoolDict(dict_str_bool):
94     a = pickle.dumps(dict_str_bool)
95     return QVariant(QByteArray(a))
96
97 def getDict(qvariant):
98     a = str(qvariant.toByteArray())
99     if len(a) == 0:
100         dict_str_bool = {}
101     else:
102         dict_str_bool = pickle.loads(a)
103     return dict_str_bool
104
105 def convertDict(dict_str_variant):
106     a = pickle.dumps(dict_str_variant)
107     return QVariant(QByteArray(a))