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 
91 template<>
92 class SettingConverter<int>{
93 public:
94  static QString cvt_to_string(const int& val){
95  return QString::number(val);
96  }
97 
98  static bool cvt_from_string(QString val, int& i){
99  bool ok;
100  i = val.toInt(&ok);
101 
102  return ok;
103  }
104 };
105 
106 template<>
107 class SettingConverter<float>{
108 public:
109  static QString cvt_to_string(const float& val){
110  return QString::number(val);
111  }
112 
113  static bool cvt_from_string(QString val, float& i){
114  bool ok;
115  i = val.toFloat(&ok);
116 
117  return ok;
118  }
119 };
120 
121 
122 // for QStringList
123 template<>
128 class SettingConverter<QStringList>{
129 public:
130  static QString cvt_to_string(const QStringList& val){
131  return val.join(",");
132  }
133 
134  static bool cvt_from_string(QString val, QStringList& lst){
135  lst = val.split(",");
136  return true;
137  }
138 };
139 
140 // for QString
141 template<>
146 class SettingConverter<QString>{
147 public:
148  static QString cvt_to_string(const QString& val){
149  return val;
150  }
151 
152  static bool cvt_from_string(QString val, QString& b){
153  b = val;
154  return true;
155  }
156 };
157 
158 // for QSize
159 template<>
164 class SettingConverter<QSize>{
165 public:
166  static QString cvt_to_string(const QSize& val){
167  return QString::number(val.width()) + "," + QString::number(val.height());
168  }
169 
170  static bool cvt_from_string(QString val, QSize& sz){
171 
172  bool ok;
173  int width, height;
174 
175  QStringList lst = val.split(",");
176 
177  if(lst.size() < 2) return false;
178 
179  width = lst[0].toInt(&ok);
180 
181  if(!ok) return false;
182  height = lst[1].toInt(&ok);
183  if(!ok) return false;
184 
185  sz.setWidth(width);
186  sz.setHeight(height);
187 
188  return true;
189  }
190 };
191 
192 // for QPoint
193 template<>
198 class SettingConverter<QPoint>{
199 public:
200  static QString cvt_to_string(const QPoint& val){
201  return QString::number(val.x()) + "," + QString::number(val.y());
202  }
203 
204  static bool cvt_from_string(QString val, QPoint& sz){
205 
206  bool ok;
207  int x, y;
208 
209  QStringList lst = val.split(",");
210 
211  if(lst.size() < 2) return false;
212 
213  x = lst[0].toInt(&ok);
214 
215  if(!ok) return false;
216  y = lst[1].toInt(&ok);
217  if(!ok) return false;
218 
219  sz.setX(x);
220  sz.setY(y);
221 
222  return true;
223  }
224 };
225 
226 // for QPoint
227 template<>
232 class SettingConverter<QByteArray>{
233 public:
234  static QString cvt_to_string(const QByteArray& arr){
235  QStringList numbers;
236  for(quint8 item : arr){
237  numbers << QString::number(item);
238  }
239 
240  return numbers.join(",");
241  }
242 
243  static bool cvt_from_string(QString str, QByteArray& arr){
244  QStringList numbers = str.split(",");
245 
246  for(const QString& num_str : numbers){
247  quint8 num = num_str.toInt();
248  arr.append((char) num);
249  }
250 
251  return (numbers.size() > 0);
252  }
253 };
254 
255 // generic for lists
256 template<typename T>
262 
263 public:
264  static QString cvt_to_string(const QList<T>& val){
265 
267  QStringList lst;
268 
269  for(const T& v : val){
270  lst << sc.cvt_to_string(v);
271  }
272 
273  return lst.join(",");
274  }
275 
276 
277  static bool cvt_from_string(const QString& val, QList<T>& ret){
278 
280  ret.clear();
281  QStringList lst = val.split(",");
282 
283  for(const QString& l : lst){
284 
285  T v;
286  sc.cvt_from_string(l, v);
287  ret << v;
288  }
289 
290  return true;
291  }
292 };
293 
294 template<typename A, typename B>
299 class SettingConverter< QPair<A,B> >{
300 public:
301  static QString cvt_to_string(const QPair<A,B>& val){
302  A a = val.first;
303  B b = val.second;
304  SettingConverter<A> sc_a;
305  SettingConverter<B> sc_b;
306  return sc_a.cvt_to_string(val.first) + "," + sc_b.cvt_to_string(b);
307  }
308 
309  static bool cvt_from_string(const QString& val, QPair<A,B>& ret){
310 
311  SettingConverter<A> sc_a;
312  SettingConverter<B> sc_b;
313 
314  QStringList lst = val.split(",");
315  QString a, b;
316  bool success = true;
317  if(lst.size() > 0){
318  a = lst[0];
319  }
320 
321  if(lst.size() > 1){
322  b = lst[1];
323  }
324  else {
325  success = false;
326  }
327 
328  sc_a.cvt_from_string (a, ret.first);
329  sc_b.cvt_from_string (b, ret.second);
330 
331  return success;
332  }
333 };
334 
335 #endif // SETTINGCONVERTER_H
The SettingConverter class.
Definition: SettingConverter.h:37
Definition: org_mpris_media_player2_adaptor.h:20