LeechCraft  %{LEECHCRAFT_VERSION}
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules 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 
43 namespace LeechCraft
44 {
45  namespace Util
46  {
53  inline QString FromStdString (const std::string& str)
54  {
55  return QString::fromUtf8 (str.c_str ());
56  }
57 
58  template<typename T>
59  struct ValueFinder
60  {
61  typedef typename T::data_type data_type;
62  data_type Object_;
63 
64  ValueFinder (data_type data)
65  : Object_ (data)
66  {
67  }
68 
69  bool operator() (typename T::value_type i)
70  {
71  return i.second == Object_;
72  }
73  };
74 
84  UTIL_API QString GetAsBase64Src (const QImage& image);
85 
92  UTIL_API QString GetUserText (const Entity& entity);
93 
104  UTIL_API QString MakePrettySize (qint64 sourceSize);
105 
115  UTIL_API QString MakeTimeFromLong (ulong time);
116 
117  UTIL_API QTranslator* LoadTranslator (const QString& base,
118  const QString& locale,
119  const QString& prefix = "leechcraft",
120  const QString& appname = "leechcraft");
121 
140  UTIL_API QTranslator* InstallTranslator (const QString& base,
141  const QString& prefix = "leechcraft",
142  const QString& appname = "leechcraft");
143 
162  UTIL_API QString GetLocaleName ();
163 
164  UTIL_API QString GetInternetLocaleName (const QLocale&);
165 
175  UTIL_API QString GetLanguage ();
176 
177 
178  UTIL_API QModelIndexList GetSummarySelectedRows (QObject *sender);
179 
187  UTIL_API QAction* CreateSeparator (QObject *parent);
188 
189  UTIL_API QPixmap DrawOverlayText (QPixmap px, const QString& text, QFont font, const QPen& pen, const QBrush& brush);
190 
200  UTIL_API uintptr_t Handle2Num (Qt::HANDLE handle);
201 
234  template<typename TagGetter, typename TagSetter>
235  QDomElement GetElementForTags (const QStringList& tags,
236  QDomNode& node,
237  QDomDocument& document,
238  const QString& elementName,
239  TagGetter tagGetter,
240  TagSetter tagSetter)
241  {
242  if (!tags.size ())
243  {
244  qWarning () << Q_FUNC_INFO
245  << "no tags"
246  << elementName;
247  return node.toElement ();
248  }
249 
250  QDomNodeList elements = node.childNodes ();
251  for (int i = 0; i < elements.size (); ++i)
252  {
253  QDomElement elem = elements.at (i).toElement ();
254  if (tagGetter (elem) == tags.at (0))
255  {
256  if (tags.size () > 1)
257  {
258  QStringList childTags = tags;
259  childTags.removeAt (0);
260  return GetElementForTags (childTags, elem,
261  document, elementName,
262  tagGetter, tagSetter);
263  }
264  else
265  return elem;
266  }
267  }
268 
269  QDomElement result = document.createElement (elementName);
270  tagSetter (result, tags.at (0));
271  node.appendChild (result);
272  if (tags.size () > 1)
273  {
274  QStringList childTags = tags;
275  childTags.removeAt (0);
276  return GetElementForTags (childTags, result,
277  document, elementName,
278  tagGetter, tagSetter);
279  }
280  else
281  return result;
282  }
283 
284  template<typename K, typename V>
285  QMap<K, V> MakeMap (std::initializer_list<QPair<K, V>> l)
286  {
287  QMap<K, V> result;
288  for (const auto& pair : l)
289  result [pair.first] = pair.second;
290  return result;
291  }
292  }
293 }
UTIL_API QString MakePrettySize(qint64 sourceSize)
Makes a formatted size from number.
Definition: util.cpp:96
UTIL_API QTranslator * LoadTranslator(const QString &base, const QString &locale, const QString &prefix="leechcraft", const QString &appname="leechcraft")
Definition: util.cpp:154
bool operator()(typename T::value_type i)
Definition: util.h:69
UTIL_API uintptr_t Handle2Num(Qt::HANDLE handle)
Converts the handle to an integer.
Definition: util.cpp:341
#define UTIL_API
Definition: utilconfig.h:37
UTIL_API QString GetLocaleName()
Returns the current locale name, like en_US.
Definition: util.cpp:216
UTIL_API QString MakeTimeFromLong(ulong time)
Makes a formatted time from number.
Definition: util.cpp:143
T::data_type data_type
Definition: util.h:61
UTIL_API QString GetLanguage()
Returns the current language name.
Definition: util.cpp:255
UTIL_API QString GetInternetLocaleName(const QLocale &)
Definition: util.cpp:243
UTIL_API QTranslator * InstallTranslator(const QString &base, const QString &prefix="leechcraft", const QString &appname="leechcraft")
Loads and installs a translator.
Definition: util.cpp:196
UTIL_API QAction * CreateSeparator(QObject *parent)
Returns the action that is set to act as a separator.
Definition: util.cpp:278
UTIL_API QString GetUserText(const Entity &entity)
Return the user-readable representation of the entity.
Definition: util.cpp:60
UTIL_API QPixmap DrawOverlayText(QPixmap px, const QString &text, QFont font, const QPen &pen, const QBrush &brush)
Definition: util.cpp:285
ValueFinder(data_type data)
Definition: util.h:64
UTIL_API QString GetAsBase64Src(const QImage &image)
Returns the given image in a Base64-encoded form.
Definition: util.cpp:51
UTIL_API QModelIndexList GetSummarySelectedRows(QObject *sender)
Definition: util.cpp:260
Describes parameters of an entity.
Definition: structures.h:156
QMap< K, V > MakeMap(std::initializer_list< QPair< K, V >> l)
Definition: util.h:285
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:235
QString FromStdString(const std::string &str)
An utility function that creates a QString from UTF8-encoded std::string.
Definition: util.h:53