LeechCraft  0.6.70-13729-g7046a9d2a7
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 LC
45 {
46  namespace Util
47  {
57  UTIL_API QString GetAsBase64Src (const QImage& image);
58 
65  UTIL_API QString GetUserText (const Entity& entity);
66 
77  UTIL_API QString MakePrettySize (qint64 sourceSize);
78 
93  UTIL_API QString MakePrettySizeShort (qint64 size);
94 
104  UTIL_API QString MakeTimeFromLong (ulong time);
105 
106  UTIL_API QTranslator* LoadTranslator (const QString& base,
107  const QString& locale,
108  const QString& prefix = "leechcraft",
109  const QString& appname = "leechcraft");
110 
129  UTIL_API QTranslator* InstallTranslator (const QString& base,
130  const QString& prefix = "leechcraft",
131  const QString& appname = "leechcraft");
132 
151  UTIL_API QString GetLocaleName ();
152 
153  UTIL_API QString GetInternetLocaleName (const QLocale&);
154 
164  UTIL_API QString GetLanguage ();
165 
166 
167  UTIL_API QModelIndexList GetSummarySelectedRows (QObject *sender);
168 
176  UTIL_API QAction* CreateSeparator (QObject *parent);
177 
178  UTIL_API QPixmap DrawOverlayText (QPixmap px, const QString& text, QFont font, const QPen& pen, const QBrush& brush);
179 
212  template<typename TagGetter, typename TagSetter>
213  QDomElement GetElementForTags (const QStringList& tags,
214  QDomNode& node,
215  QDomDocument& document,
216  const QString& elementName,
217  TagGetter tagGetter,
218  TagSetter tagSetter)
219  {
220  if (!tags.size ())
221  {
222  qWarning () << Q_FUNC_INFO
223  << "no tags"
224  << elementName;
225  return node.toElement ();
226  }
227 
228  QDomNodeList elements = node.childNodes ();
229  for (int i = 0; i < elements.size (); ++i)
230  {
231  QDomElement elem = elements.at (i).toElement ();
232  if (tagGetter (elem) == tags.at (0))
233  {
234  if (tags.size () > 1)
235  {
236  QStringList childTags = tags;
237  childTags.removeAt (0);
238  return GetElementForTags (childTags, elem,
239  document, elementName,
240  tagGetter, tagSetter);
241  }
242  else
243  return elem;
244  }
245  }
246 
247  QDomElement result = document.createElement (elementName);
248  tagSetter (result, tags.at (0));
249  node.appendChild (result);
250  if (tags.size () > 1)
251  {
252  QStringList childTags = tags;
253  childTags.removeAt (0);
254  return GetElementForTags (childTags, result,
255  document, elementName,
256  tagGetter, tagSetter);
257  }
258  else
259  return result;
260  }
261  }
262 }
UTIL_API QString GetInternetLocaleName(const QLocale &)
Definition: util.cpp:237
UTIL_API QAction * CreateSeparator(QObject *parent)
Returns the action that is set to act as a separator.
Definition: util.cpp:270
UTIL_API QString GetAsBase64Src(const QImage &image)
Returns the given image in a Base64-encoded form.
Definition: util.cpp:50
UTIL_API QString GetLanguage()
Returns the current language name.
Definition: util.cpp:247
UTIL_API QPixmap DrawOverlayText(QPixmap px, const QString &text, QFont font, const QPen &pen, const QBrush &brush)
Definition: util.cpp:277
UTIL_API QString GetLocaleName()
Returns the current locale name, like en_US.
Definition: util.cpp:206
UTIL_API QTranslator * InstallTranslator(const QString &base, const QString &prefix="leechcraft", const QString &appname="leechcraft")
Loads and installs a translator.
Definition: util.cpp:186
#define UTIL_API
Definition: utilconfig.h:37
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:213
UTIL_API QString MakePrettySizeShort(qint64 size)
Converts a bytes count to a string representation with appropriately chosen units.
Definition: util.cpp:122
UTIL_API QModelIndexList GetSummarySelectedRows(QObject *sender)
Definition: util.cpp:252
UTIL_API QString MakeTimeFromLong(ulong time)
Makes a formatted time from number.
Definition: util.cpp:135
UTIL_API QString MakePrettySize(qint64 sourceSize)
Makes a formatted size from number.
Definition: util.cpp:109
UTIL_API QString GetUserText(const Entity &entity)
Return the user-readable representation of the entity.
Definition: util.cpp:59
UTIL_API QTranslator * LoadTranslator(const QString &base, const QString &locale, const QString &prefix="leechcraft", const QString &appname="leechcraft")
Definition: util.cpp:146
Definition: constants.h:35