LeechCraft  0.6.70-3565-g2d86529
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
util.h
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #pragma once
31 
32 #include "utilconfig.h"
33 #include <QString>
34 #include <QDir>
35 #include <QModelIndex>
36 #include <QtXml/QDomElement>
37 #include <QtDebug>
38 #include <interfaces/structures.h>
39 
40 class QTranslator;
41 class QLocale;
42 class QAction;
43 
44 namespace LeechCraft
45 {
46  namespace Util
47  {
54  inline QString FromStdString (const std::string& str)
55  {
56  return QString::fromUtf8 (str.c_str ());
57  }
58 
68  UTIL_API QString GetAsBase64Src (const QImage& image);
69 
76  UTIL_API QString GetUserText (const Entity& entity);
77 
88  UTIL_API QString MakePrettySize (qint64 sourceSize);
89 
99  UTIL_API QString MakeTimeFromLong (ulong time);
100 
101  UTIL_API QTranslator* LoadTranslator (const QString& base,
102  const QString& locale,
103  const QString& prefix = "leechcraft",
104  const QString& appname = "leechcraft");
105 
124  UTIL_API QTranslator* InstallTranslator (const QString& base,
125  const QString& prefix = "leechcraft",
126  const QString& appname = "leechcraft");
127 
146  UTIL_API QString GetLocaleName ();
147 
148  UTIL_API QString GetInternetLocaleName (const QLocale&);
149 
159  UTIL_API QString GetLanguage ();
160 
161 
162  UTIL_API QModelIndexList GetSummarySelectedRows (QObject *sender);
163 
171  UTIL_API QAction* CreateSeparator (QObject *parent);
172 
173  UTIL_API QPixmap DrawOverlayText (QPixmap px, const QString& text, QFont font, const QPen& pen, const QBrush& brush);
174 
184  UTIL_API uintptr_t Handle2Num (Qt::HANDLE handle);
185 
218  template<typename TagGetter, typename TagSetter>
219  QDomElement GetElementForTags (const QStringList& tags,
220  QDomNode& node,
221  QDomDocument& document,
222  const QString& elementName,
223  TagGetter tagGetter,
224  TagSetter tagSetter)
225  {
226  if (!tags.size ())
227  {
228  qWarning () << Q_FUNC_INFO
229  << "no tags"
230  << elementName;
231  return node.toElement ();
232  }
233 
234  QDomNodeList elements = node.childNodes ();
235  for (int i = 0; i < elements.size (); ++i)
236  {
237  QDomElement elem = elements.at (i).toElement ();
238  if (tagGetter (elem) == tags.at (0))
239  {
240  if (tags.size () > 1)
241  {
242  QStringList childTags = tags;
243  childTags.removeAt (0);
244  return GetElementForTags (childTags, elem,
245  document, elementName,
246  tagGetter, tagSetter);
247  }
248  else
249  return elem;
250  }
251  }
252 
253  QDomElement result = document.createElement (elementName);
254  tagSetter (result, tags.at (0));
255  node.appendChild (result);
256  if (tags.size () > 1)
257  {
258  QStringList childTags = tags;
259  childTags.removeAt (0);
260  return GetElementForTags (childTags, result,
261  document, elementName,
262  tagGetter, tagSetter);
263  }
264  else
265  return result;
266  }
267 
268  template<typename K, typename V>
269  QMap<K, V> MakeMap (std::initializer_list<std::pair<K, V>> l)
270  {
271 #if QT_VERSION >= 0x050000
272  return QMap<K, V> { std::move (l) };
273 #else
274  QMap<K, V> result;
275  for (const auto& pair : l)
276  result [pair.first] = pair.second;
277  return result;
278 #endif
279  }
280  }
281 }
UTIL_API QString MakePrettySize(qint64 sourceSize)
Makes a formatted size from number.
Definition: util.cpp:99
UTIL_API QTranslator * LoadTranslator(const QString &base, const QString &locale, const QString &prefix="leechcraft", const QString &appname="leechcraft")
Definition: util.cpp:129
UTIL_API uintptr_t Handle2Num(Qt::HANDLE handle)
Converts the handle to an integer.
Definition: util.cpp:318
#define UTIL_API
Definition: utilconfig.h:37
UTIL_API QString GetLocaleName()
Returns the current locale name, like en_US.
Definition: util.cpp:191
UTIL_API QString MakeTimeFromLong(ulong time)
Makes a formatted time from number.
Definition: util.cpp:118
UTIL_API QString GetLanguage()
Returns the current language name.
Definition: util.cpp:232
UTIL_API QString GetInternetLocaleName(const QLocale &)
Definition: util.cpp:222
QMap< K, V > MakeMap(std::initializer_list< std::pair< K, V >> l)
Definition: util.h:269
UTIL_API QTranslator * InstallTranslator(const QString &base, const QString &prefix="leechcraft", const QString &appname="leechcraft")
Loads and installs a translator.
Definition: util.cpp:171
UTIL_API QAction * CreateSeparator(QObject *parent)
Returns the action that is set to act as a separator.
Definition: util.cpp:255
UTIL_API QString GetUserText(const Entity &entity)
Return the user-readable representation of the entity.
Definition: util.cpp:63
UTIL_API QPixmap DrawOverlayText(QPixmap px, const QString &text, QFont font, const QPen &pen, const QBrush &brush)
Definition: util.cpp:262
UTIL_API QString GetAsBase64Src(const QImage &image)
Returns the given image in a Base64-encoded form.
Definition: util.cpp:54
UTIL_API QModelIndexList GetSummarySelectedRows(QObject *sender)
Definition: util.cpp:237
Definition: anutil.h:38
Describes parameters of an entity.
Definition: structures.h:154
QDomElement GetElementForTags(const QStringList &tags, QDomNode &node, QDomDocument &document, const QString &elementName, TagGetter tagGetter, TagSetter tagSetter)
Returns an element for a given tags list.
Definition: util.h:219
QString FromStdString(const std::string &str)
An utility function that creates a QString from UTF8-encoded std::string.
Definition: util.h:54