Sayonara Player
SettingConverter.h
1 /* SettingConverter.h */
2 
3 /* Copyright (C) 2011-2017 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 #ifndef SETTINGCONVERTER_H
22 #define SETTINGCONVERTER_H
23 
24 #include <QPair>
25 #include <QStringList>
26 #include "Utils/typedefs.h"
27 
28 class QSize;
29 class QString;
30 class QPoint;
31 
32 // generic
33 template<typename T>
39 {
40 public:
41  static QString cvt_to_string(const T& val){
42  return val.toString();
43  }
44 
45  static bool cvt_from_string(const QString& val, T& ret){
46  ret = T::fromString(val);
47  return true;
48  }
49 };
50 
51 
52 // from bool
53 template<>
58 class SettingConverter<bool>{
59 public:
60  static QString cvt_to_string(const bool& val);
61  static bool cvt_from_string(const QString& val, bool& b);
62 };
63 
64 
65 // for int
66 
71 template<>
72 class SettingConverter<int>{
73 public:
74  static QString cvt_to_string(const int& val);
75  static bool cvt_from_string(const QString& val, int& i);
76 };
77 
78 template<>
79 class SettingConverter<float>{
80 public:
81  static QString cvt_to_string(const float& val);
82  static bool cvt_from_string(const QString& val, float& i);
83 };
84 
85 
86 // for QStringList
87 template<>
92 class SettingConverter<QStringList>{
93 public:
94  static QString cvt_to_string(const QStringList& val);
95  static bool cvt_from_string(const QString& val, QStringList& lst);
96 };
97 
98 
99 // for QString
100 template<>
105 class SettingConverter<QString>{
106 public:
107  static QString cvt_to_string(const QString& val);
108  static bool cvt_from_string(const QString& val, QString& b);
109 };
110 
111 
112 // for QSize
113 template<>
118 class SettingConverter<QSize>{
119 public:
120  static QString cvt_to_string(const QSize& val);
121  static bool cvt_from_string(const QString& val, QSize& sz);
122 };
123 
124 
125 // for QPoint
126 template<>
131 class SettingConverter<QPoint>{
132 public:
133  static QString cvt_to_string(const QPoint& val);
134  static bool cvt_from_string(const QString& val, QPoint& sz);
135 };
136 
137 
138 // for QByteArray
139 template<>
144 class SettingConverter<QByteArray>{
145 public:
146  static QString cvt_to_string(const QByteArray& arr);
147  static bool cvt_from_string(const QString& str, QByteArray& arr);
148 };
149 
150 
151 // generic for lists
152 template<typename T>
158 public:
159  static QString cvt_to_string(const QList<T>& val)
160  {
162  QStringList lst;
163 
164  for(const T& v : val){
165  lst << sc.cvt_to_string(v);
166  }
167 
168  return lst.join(",");
169  }
170 
171 
172  static bool cvt_from_string(const QString& val, QList<T>& ret)
173  {
175  ret.clear();
176  QStringList lst = val.split(",");
177 
178  for(const QString& l : lst){
179  T v;
180  sc.cvt_from_string(l, v);
181  ret << v;
182  }
183 
184  return true;
185  }
186 };
187 
188 // generic for lists
189 template<>
194 class SettingConverter< BoolList >{
195 public:
196  static QString cvt_to_string(const BoolList& val)
197  {
199  QStringList lst;
200 
201  for(const bool& v : val){
202  lst << sc.cvt_to_string(v);
203  }
204 
205  return lst.join(",");
206  }
207 
208 
209  static bool cvt_from_string(const QString& val, BoolList& ret)
210  {
212  ret.clear();
213  QStringList lst = val.split(",");
214 
215  for(const QString& l : lst){
216  bool v;
217  sc.cvt_from_string(l, v);
218  ret.push_back(v);
219  }
220 
221  return true;
222  }
223 };
224 
225 template<typename A, typename B>
230 class SettingConverter< QPair<A,B> >{
231 public:
232  static QString cvt_to_string(const QPair<A,B>& val){
233  A a = val.first;
234  B b = val.second;
235  SettingConverter<A> sc_a;
236  SettingConverter<B> sc_b;
237 
238  return sc_a.cvt_to_string(val.first) + "," + sc_b.cvt_to_string(b);
239  }
240 
241  static bool cvt_from_string(const QString& val, QPair<A,B>& ret){
242  SettingConverter<A> sc_a;
243  SettingConverter<B> sc_b;
244 
245  QStringList lst = val.split(",");
246  QString a, b;
247  bool success = true;
248  if(lst.size() > 0){
249  a = lst[0];
250  }
251 
252  if(lst.size() > 1){
253  b = lst[1];
254  }
255  else
256  {
257  success = false;
258  }
259 
260  sc_a.cvt_from_string (a, ret.first);
261  sc_b.cvt_from_string (b, ret.second);
262 
263  return success;
264  }
265 };
266 
267 #endif // SETTINGCONVERTER_H
Definition: typedefs.h:28
The SettingConverter<bool> class.
Definition: SettingConverter.h:58
The SettingConverter class.
Definition: SettingConverter.h:38
Definition: org_mpris_media_player2_adaptor.h:20