33 #include <type_traits>
35 #include <QApplication>
36 #include <QTranslator>
54 buf.open (QIODevice::ReadWrite);
55 pix.save (&buf,
"PNG", 100);
56 return QString (
"data:image/png;base64,%1")
57 .arg (QString (buf.buffer ().toBase64 ()));
62 QString
string = QObject::tr (
"Too long to show");
64 p.
Additional_ [
"UserVisibleName"].canConvert<QString> ())
65 string = p.
Additional_ [
"UserVisibleName"].toString ();
66 else if (p.
Entity_.canConvert<QByteArray> ())
68 QByteArray entity = p.
Entity_.toByteArray ();
69 if (entity.size () < 100)
70 string = QTextCodec::codecForName (
"UTF-8")->toUnicode (entity);
72 else if (p.
Entity_.canConvert<QUrl> ())
74 string = p.
Entity_.toUrl ().toString ();
75 if (
string.size () > 100)
76 string =
string.left (97) +
"...";
79 string = QObject::tr (
"Binary entity");
81 if (!p.
Mime_.isEmpty ())
82 string += QObject::tr (
"<br /><br />of type <code>%1</code>").arg (p.
Mime_);
84 if (!p.
Additional_ [
"SourceURL"].toUrl ().isEmpty ())
86 QString urlStr = p.
Additional_ [
"SourceURL"].toUrl ().toString ();
87 if (urlStr.size () > 63)
88 urlStr = urlStr.left (60) +
"...";
89 string += QObject::tr (
"<br />from %1")
99 long double size = sourcesize;
120 static const QString unit = QObject::tr (
" b");
121 return QString::number (size,
'f', 1) + unit;
125 static const QString unit = QObject::tr (
" KiB");
126 return QString::number (size,
'f', 1) + unit;
130 static const QString unit = QObject::tr (
" MiB");
131 return QString::number (size,
'f', 1) + unit;
135 static const QString unit = QObject::tr (
" GiB");
136 return QString::number (size,
'f', 1) + unit;
145 int d = time / 86400;
149 result += QObject::tr (
"%n day(s), ",
"", d);
150 result += QTime (0, 0, 0).addSecs (time).toString ();
155 const QString& localeName,
156 const QString& prefix,
157 const QString& appName)
159 auto filename = prefix;
160 filename.append (
"_");
161 if (!baseName.isEmpty ())
162 filename.append (baseName).append (
"_");
163 filename.append (localeName);
165 auto transl =
new QTranslator;
167 if (transl->load (filename,
":/") ||
168 transl->load (filename,
169 QCoreApplication::applicationDirPath () +
"/translations"))
170 #elif defined (Q_OS_MAC)
171 const auto tryLocal = QApplication::arguments ().contains (
"-nobundle");
172 if (transl->load (filename,
":/") ||
174 transl->load (filename,
175 QString (
"/usr/local/share/%1/translations").arg (appName))) ||
176 transl->load (filename,
177 QCoreApplication::applicationDirPath () +
"/../Resources/translations"))
178 #elif defined (INSTALL_PREFIX)
179 if (transl->load (filename,
":/") ||
180 transl->load (filename,
181 QString (INSTALL_PREFIX
"/share/%1/translations").arg (appName)))
183 if (transl->load (filename,
":/") ||
184 transl->load (filename,
185 QString (
"/usr/local/share/%1/translations").arg (appName)) ||
186 transl->load (filename,
187 QString (
"/usr/share/%1/translations").arg (appName)))
197 const QString& prefix,
198 const QString& appName)
201 if (
auto transl =
LoadTranslator (baseName, localeName, prefix, appName))
203 qApp->installTranslator (transl);
207 qWarning () << Q_FUNC_INFO
208 <<
"could not load translation file for locale"
218 QSettings settings (QCoreApplication::organizationName (),
219 QCoreApplication::applicationName ());
220 QString localeName = settings.value (
"Language",
"system").toString ();
222 if (localeName ==
"system")
224 localeName = QString (::getenv (
"LANG")).left (5);
225 if (localeName.isEmpty () || localeName.size () != 5)
226 localeName = QLocale::system ().name ();
227 localeName = localeName.left (5);
230 if (localeName.size () == 2)
232 QLocale::Language lang = QLocale (localeName).language ();
237 localeName = QLocale (lang, cs.at (0)).name ();
245 #if QT_VERSION >= 0x040800
246 if (locale.language () == QLocale::AnyLanguage)
250 QString locStr = locale.name ();
251 locStr.replace (
'_',
'-');
262 QAction *senderAct = qobject_cast<QAction*> (sender);
267 QDebug d (&debugString);
268 d <<
"sender is not a QAction*"
271 throw std::runtime_error (qPrintable (debugString));
280 QAction *result =
new QAction (parent);
281 result->setSeparator (
true);
286 const QString& text, QFont font,
const QPen& pen,
const QBrush& brush)
288 const auto& iconSize = px.size ();
290 const auto fontHeight = px.height () * 0.45;
291 font.setPixelSize (std::max (6., fontHeight));
293 const QFontMetrics fm (font);
294 const auto width = fm.width (text) + 2. * px.width () / 10.;
295 const auto height = fm.height () + 2. * px.height () / 10.;
296 const bool tooSmall = width > iconSize.width ();
298 const QRect textRect (iconSize.width () - width, iconSize.height () - height, width, height);
304 p.setRenderHint (QPainter::Antialiasing);
305 p.setRenderHint (QPainter::TextAntialiasing);
306 p.setRenderHint (QPainter::HighQualityAntialiasing);
307 p.drawRoundedRect (textRect, 4, 4);
308 p.drawText (textRect,
310 tooSmall ?
"#" : text);
318 template<
typename To,
typename From>
319 typename std::enable_if<std::is_same<From, To>::value, To>::type DumbCast (From from)
324 template<
typename To,
typename From>
325 typename std::enable_if<!std::is_same<From, To>::value &&
326 std::is_integral<From>::value &&
327 std::is_integral<To>::value, To>::type DumbCast (From from)
329 return static_cast<To
> (from);
332 template<
typename To,
typename From>
333 typename std::enable_if<!std::is_same<From, To>::value &&
334 !(std::is_integral<From>::value &&
335 std::is_integral<To>::value), To>::type DumbCast (From from)
337 return reinterpret_cast<To
> (from);
343 return DumbCast<uintptr_t> (handle);
Q_DECLARE_METATYPE(QList< QModelIndex >)
UTIL_API QString MakePrettySize(qint64 sourceSize)
Makes a formatted size from number.
UTIL_API QTranslator * LoadTranslator(const QString &base, const QString &locale, const QString &prefix="leechcraft", const QString &appname="leechcraft")
UTIL_API uintptr_t Handle2Num(Qt::HANDLE handle)
Converts the handle to an integer.
UTIL_API QString GetLocaleName()
Returns the current locale name, like en_US.
UTIL_API QString MakeTimeFromLong(ulong time)
Makes a formatted time from number.
UTIL_API QString GetLanguage()
Returns the current language name.
UTIL_API QString GetInternetLocaleName(const QLocale &)
UTIL_API QTranslator * InstallTranslator(const QString &base, const QString &prefix="leechcraft", const QString &appname="leechcraft")
Loads and installs a translator.
UTIL_API QAction * CreateSeparator(QObject *parent)
Returns the action that is set to act as a separator.
UTIL_API QString GetUserText(const Entity &entity)
Return the user-readable representation of the entity.
UTIL_API QPixmap DrawOverlayText(QPixmap px, const QString &text, QFont font, const QPen &pen, const QBrush &brush)
UTIL_API QString GetAsBase64Src(const QImage &image)
Returns the given image in a Base64-encoded form.
UTIL_API QModelIndexList GetSummarySelectedRows(QObject *sender)
QMap< QString, QVariant > Additional_
Additional parameters.
QString Mime_
MIME type of the entity.
QVariant Entity_
The entity that this object represents.
Describes parameters of an entity.