Sayonara Player
SettingConverter.h
1 /* SettingConverter.h */
2 
3 /* Copyright (C) 2011-2016 Lucio Carreras
4  *
5  * This file is part of sayonara player
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11 
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16 
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 
22 
23 #ifndef SETTINGCONVERTER_H
24 #define SETTINGCONVERTER_H
25 
26 #include <QString>
27 #include <QSize>
28 #include <QPoint>
29 #include <QList>
30 
31 // generic
32 template<typename T>
38 public:
39  static QString cvt_to_string(const T& val){
40  return val.toString();
41  }
42 
43  static bool cvt_from_string(QString val, T& ret){
44  ret = T::fromString(val);
45  return true;
46  }
47 };
48 
49 
50 // from bool
51 template<>
56 class SettingConverter<bool>{
57 public:
58  static QString cvt_to_string(const bool& val){
59  if(val) {
60  return QString("true");
61  }
62 
63  else {
64  return QString("false");
65  }
66  }
67 
68  static bool cvt_from_string(QString val, bool& b){
69  if( val.compare("true", Qt::CaseInsensitive) == 0 ||
70  val.toInt() > 0)
71  {
72  b = true;
73  }
74 
75  else
76  {
77  b = false;
78  }
79 
80  return true;
81  }
82 };
83 
84 
85 // for int
86 template<>
91 class SettingConverter<int>{
92 public:
93  static QString cvt_to_string(const int& val){
94  return QString::number(val);
95  }
96 
97  static bool cvt_from_string(QString val, int& i){
98  bool ok;
99  i = val.toInt(&ok);
100 
101  return ok;
102  }
103 };
104 
105 // for QStringList
106 template<>
111 class SettingConverter<QStringList>{
112 public:
113  static QString cvt_to_string(const QStringList& val){
114  return val.join(",");
115  }
116 
117  static bool cvt_from_string(QString val, QStringList& lst){
118  lst = val.split(",");
119  return true;
120  }
121 };
122 
123 // for QString
124 template<>
129 class SettingConverter<QString>{
130 public:
131  static QString cvt_to_string(const QString& val){
132  return val;
133  }
134 
135  static bool cvt_from_string(QString val, QString& b){
136  b = val;
137  return true;
138  }
139 };
140 
141 // for QSize
142 template<>
147 class SettingConverter<QSize>{
148 public:
149  static QString cvt_to_string(const QSize& val){
150  return QString::number(val.width()) + "," + QString::number(val.height());
151  }
152 
153  static bool cvt_from_string(QString val, QSize& sz){
154 
155  bool ok;
156  int width, height;
157 
158  QStringList lst = val.split(",");
159 
160  if(lst.size() < 2) return false;
161 
162  width = lst[0].toInt(&ok);
163 
164  if(!ok) return false;
165  height = lst[1].toInt(&ok);
166  if(!ok) return false;
167 
168  sz.setWidth(width);
169  sz.setHeight(height);
170 
171  return true;
172  }
173 };
174 
175 // for QPoint
176 template<>
181 class SettingConverter<QPoint>{
182 public:
183  static QString cvt_to_string(const QPoint& val){
184  return QString::number(val.x()) + "," + QString::number(val.y());
185  }
186 
187  static bool cvt_from_string(QString val, QPoint& sz){
188 
189  bool ok;
190  int x, y;
191 
192  QStringList lst = val.split(",");
193 
194  if(lst.size() < 2) return false;
195 
196  x = lst[0].toInt(&ok);
197 
198  if(!ok) return false;
199  y = lst[1].toInt(&ok);
200  if(!ok) return false;
201 
202  sz.setX(x);
203  sz.setY(y);
204 
205  return true;
206  }
207 };
208 
209 // for QPoint
210 template<>
215 class SettingConverter<QByteArray>{
216 public:
217  static QString cvt_to_string(const QByteArray& arr){
218  QStringList numbers;
219  for(quint8 item : arr){
220  numbers << QString::number(item);
221  }
222 
223  return numbers.join(",");
224  }
225 
226  static bool cvt_from_string(QString str, QByteArray& arr){
227  QStringList numbers = str.split(",");
228 
229  for(const QString& num_str : numbers){
230  quint8 num = num_str.toInt();
231  arr.append((char) num);
232  }
233 
234  return (numbers.size() > 0);
235  }
236 };
237 
238 // generic for lists
239 template<typename T>
245 
246 public:
247  static QString cvt_to_string(const QList<T>& val){
248 
250  QStringList lst;
251 
252  for(const T& v : val){
253  lst << sc.cvt_to_string(v);
254  }
255 
256  return lst.join(",");
257  }
258 
259 
260  static bool cvt_from_string(const QString& val, QList<T>& ret){
261 
263  ret.clear();
264  QStringList lst = val.split(",");
265 
266  for(const QString& l : lst){
267 
268  T v;
269  sc.cvt_from_string(l, v);
270  ret << v;
271  }
272 
273  return true;
274  }
275 };
276 
277 template<typename A, typename B>
282 class SettingConverter< QPair<A,B> >{
283 public:
284  static QString cvt_to_string(const QPair<A,B>& val){
285  A a = val.first;
286  B b = val.second;
287  SettingConverter<A> sc_a;
288  SettingConverter<B> sc_b;
289  return sc_a.cvt_to_string(val.first) + "," + sc_b.cvt_to_string(b);
290  }
291 
292  static bool cvt_from_string(const QString& val, QPair<A,B>& ret){
293 
294  SettingConverter<A> sc_a;
295  SettingConverter<B> sc_b;
296 
297  QStringList lst = val.split(",");
298  QString a, b;
299  bool success = true;
300  if(lst.size() > 0){
301  a = lst[0];
302  }
303 
304  if(lst.size() > 1){
305  b = lst[1];
306  }
307  else {
308  success = false;
309  }
310 
311  sc_a.cvt_from_string (a, ret.first);
312  sc_b.cvt_from_string (b, ret.second);
313 
314  return success;
315  }
316 };
317 
318 #endif // SETTINGCONVERTER_H
The SettingConverter class.
Definition: SettingConverter.h:37
Definition: org_mpris_media_player2_adaptor.h:20