33 #include <type_traits>
35 #include <QApplication>
36 #include <QTranslator>
50 #if QT_VERSION < 0x050500
57 buf.open (QIODevice::ReadWrite);
58 pix.save (&buf,
"PNG", 100);
59 return QString (
"data:image/png;base64,%1")
60 .arg (QString (buf.buffer ().toBase64 ()));
65 QString
string = QObject::tr (
"Too long to show");
67 p.
Additional_ [
"UserVisibleName"].canConvert<QString> ())
68 string = p.
Additional_ [
"UserVisibleName"].toString ();
69 else if (p.
Entity_.canConvert<QByteArray> ())
71 QByteArray entity = p.
Entity_.toByteArray ();
72 if (entity.size () < 100)
73 string = QTextCodec::codecForName (
"UTF-8")->toUnicode (entity);
75 else if (p.
Entity_.canConvert<QUrl> ())
77 string = p.
Entity_.toUrl ().toString ();
78 if (
string.size () > 100)
79 string =
string.left (97) +
"...";
82 string = QObject::tr (
"Binary entity");
84 if (!p.
Mime_.isEmpty ())
85 string += QObject::tr (
"<br /><br />of type <code>%1</code>").arg (p.
Mime_);
87 if (!p.
Additional_ [
"SourceURL"].toUrl ().isEmpty ())
89 QString urlStr = p.
Additional_ [
"SourceURL"].toUrl ().toString ();
90 if (urlStr.size () > 63)
91 urlStr = urlStr.left (60) +
"...";
92 string += QObject::tr (
"<br />from %1")
102 long double size = sourcesize;
104 for (; strNum < 3 && size >= 1024; ++strNum, size /= 1024)
107 static QStringList units
110 QObject::tr (
" KiB"),
111 QObject::tr (
" MiB"),
115 return QString::number (size,
'f', 1) + units.value (strNum);
120 int d = time / 86400;
124 result += QObject::tr (
"%n day(s), ",
"", d);
125 result += QTime (0, 0, 0).addSecs (time).toString ();
130 const QString& localeName,
131 const QString& prefix,
132 const QString& appName)
134 auto filename = prefix;
135 filename.append (
"_");
136 if (!baseName.isEmpty ())
137 filename.append (baseName).append (
"_");
138 filename.append (localeName);
140 auto transl =
new QTranslator;
142 if (transl->load (filename,
":/") ||
143 transl->load (filename,
144 QCoreApplication::applicationDirPath () +
"/translations"))
145 #elif defined (Q_OS_MAC)
146 const auto tryLocal = QApplication::arguments ().contains (
"-nobundle");
147 if (transl->load (filename,
":/") ||
149 transl->load (filename,
150 QString (
"/usr/local/share/%1/translations").arg (appName))) ||
151 transl->load (filename,
152 QCoreApplication::applicationDirPath () +
"/../Resources/translations"))
153 #elif defined (INSTALL_PREFIX)
154 if (transl->load (filename,
":/") ||
155 transl->load (filename,
156 QString (INSTALL_PREFIX
"/share/%1/translations").arg (appName)))
158 if (transl->load (filename,
":/") ||
159 transl->load (filename,
160 QString (
"/usr/local/share/%1/translations").arg (appName)) ||
161 transl->load (filename,
162 QString (
"/usr/share/%1/translations").arg (appName)))
172 const QString& prefix,
173 const QString& appName)
176 if (
auto transl =
LoadTranslator (baseName, localeName, prefix, appName))
178 qApp->installTranslator (transl);
182 qWarning () << Q_FUNC_INFO
183 <<
"could not load translation file for locale"
193 QSettings settings (QCoreApplication::organizationName (),
194 QCoreApplication::applicationName ());
195 QString localeName = settings.value (
"Language",
"system").toString ();
197 if (localeName ==
"system")
199 localeName = QString (::getenv (
"LANG")).left (5);
201 if (localeName ==
"C" || localeName.isEmpty ())
202 localeName =
"en_US";
204 if (localeName.isEmpty () || localeName.size () != 5)
205 localeName = QLocale::system ().name ();
206 localeName = localeName.left (5);
209 if (localeName.size () == 2)
211 auto lang = QLocale (localeName).language ();
212 const auto& cs = QLocale::countriesForLanguage (lang);
216 localeName = QLocale (lang, cs.at (0)).name ();
224 if (locale.language () == QLocale::AnyLanguage)
227 auto locStr = locale.name ();
228 locStr.replace (
'_',
'-');
239 QAction *senderAct = qobject_cast<QAction*> (sender);
244 QDebug d (&debugString);
245 d <<
"sender is not a QAction*"
248 throw std::runtime_error (qPrintable (debugString));
257 QAction *result =
new QAction (parent);
258 result->setSeparator (
true);
263 const QString& text, QFont font,
const QPen& pen,
const QBrush& brush)
265 const auto& iconSize = px.size ();
267 const auto fontHeight = px.height () * 0.45;
268 font.setPixelSize (std::max (6., fontHeight));
270 const QFontMetrics fm (font);
271 const auto width = fm.width (text) + 2. * px.width () / 10.;
272 const auto height = fm.height () + 2. * px.height () / 10.;
273 const bool tooSmall = width > iconSize.width ();
275 const QRect textRect (iconSize.width () - width, iconSize.height () - height, width, height);
281 p.setRenderHint (QPainter::Antialiasing);
282 p.setRenderHint (QPainter::TextAntialiasing);
283 p.setRenderHint (QPainter::HighQualityAntialiasing);
284 p.drawRoundedRect (textRect, 4, 4);
285 p.drawText (textRect,
287 tooSmall ?
"#" : text);
295 template<
typename To,
typename From>
296 typename std::enable_if<std::is_same<From, To>::value, To>::type DumbCast (From from)
301 template<
typename To,
typename From>
302 typename std::enable_if<!std::is_same<From, To>::value &&
303 std::is_integral<From>::value &&
304 std::is_integral<To>::value, To>::type DumbCast (From from)
306 return static_cast<To
> (from);
309 template<
typename To,
typename From>
310 typename std::enable_if<!std::is_same<From, To>::value &&
311 !(std::is_integral<From>::value &&
312 std::is_integral<To>::value), To>::type DumbCast (From from)
314 return reinterpret_cast<To
> (from);
320 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.