LeechCraft  0.6.70-10870-g558588d6ec
Modular cross-platform feature rich live environment.
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 
104  UTIL_API QString MakePrettySizeShort (qint64 size);
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 
223  template<typename TagGetter, typename TagSetter>
224  QDomElement GetElementForTags (const QStringList& tags,
225  QDomNode& node,
226  QDomDocument& document,
227  const QString& elementName,
228  TagGetter tagGetter,
229  TagSetter tagSetter)
230  {
231  if (!tags.size ())
232  {
233  qWarning () << Q_FUNC_INFO
234  << "no tags"
235  << elementName;
236  return node.toElement ();
237  }
238 
239  QDomNodeList elements = node.childNodes ();
240  for (int i = 0; i < elements.size (); ++i)
241  {
242  QDomElement elem = elements.at (i).toElement ();
243  if (tagGetter (elem) == tags.at (0))
244  {
245  if (tags.size () > 1)
246  {
247  QStringList childTags = tags;
248  childTags.removeAt (0);
249  return GetElementForTags (childTags, elem,
250  document, elementName,
251  tagGetter, tagSetter);
252  }
253  else
254  return elem;
255  }
256  }
257 
258  QDomElement result = document.createElement (elementName);
259  tagSetter (result, tags.at (0));
260  node.appendChild (result);
261  if (tags.size () > 1)
262  {
263  QStringList childTags = tags;
264  childTags.removeAt (0);
265  return GetElementForTags (childTags, result,
266  document, elementName,
267  tagGetter, tagSetter);
268  }
269  else
270  return result;
271  }
272  }
273 }
UTIL_API QString MakePrettySize(qint64 sourceSize)
Makes a formatted size from number.
Definition: util.cpp:109
UTIL_API QTranslator * LoadTranslator(const QString &base, const QString &locale, const QString &prefix="leechcraft", const QString &appname="leechcraft")
Definition: util.cpp:146
UTIL_API QString MakePrettySizeShort(qint64 size)
Converts a bytes count to a string representation with appropriately chosen units.
Definition: util.cpp:122
#define UTIL_API
Definition: utilconfig.h:37
UTIL_API QString GetLocaleName()
Returns the current locale name, like en_US.
Definition: util.cpp:204
UTIL_API QString MakeTimeFromLong(ulong time)
Makes a formatted time from number.
Definition: util.cpp:135
UTIL_API QString GetLanguage()
Returns the current language name.
Definition: util.cpp:245
UTIL_API QString GetInternetLocaleName(const QLocale &)
Definition: util.cpp:235
UTIL_API QTranslator * InstallTranslator(const QString &base, const QString &prefix="leechcraft", const QString &appname="leechcraft")
Loads and installs a translator.
Definition: util.cpp:184
UTIL_API QAction * CreateSeparator(QObject *parent)
Returns the action that is set to act as a separator.
Definition: util.cpp:268
UTIL_API QString GetUserText(const Entity &entity)
Return the user-readable representation of the entity.
Definition: util.cpp:59
UTIL_API QPixmap DrawOverlayText(QPixmap px, const QString &text, QFont font, const QPen &pen, const QBrush &brush)
Definition: util.cpp:275
UTIL_API QString GetAsBase64Src(const QImage &image)
Returns the given image in a Base64-encoded form.
Definition: util.cpp:50
UTIL_API QModelIndexList GetSummarySelectedRows(QObject *sender)
Definition: util.cpp:250
A message used for inter-plugin communication.
Definition: structures.h:119
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:224
QString FromStdString(const std::string &str)
An utility function that creates a QString from UTF8-encoded std::string.
Definition: util.h:54