33 #include <type_traits> 35 #include <QApplication> 36 #include <QTranslator> 53 buf.open (QIODevice::ReadWrite);
54 pix.save (&buf,
"PNG", 100);
55 return QString (
"data:image/png;base64,%1")
56 .arg (QString (buf.buffer ().toBase64 ()));
61 QString
string = QObject::tr (
"Too long to show");
63 p.
Additional_ [
"UserVisibleName"].canConvert<QString> ())
64 string = p.
Additional_ [
"UserVisibleName"].toString ();
65 else if (p.
Entity_.canConvert<QByteArray> ())
67 QByteArray entity = p.
Entity_.toByteArray ();
68 if (entity.size () < 100)
69 string = QTextCodec::codecForName (
"UTF-8")->toUnicode (entity);
71 else if (p.
Entity_.canConvert<QUrl> ())
73 string = p.
Entity_.toUrl ().toString ();
74 if (
string.size () > 100)
75 string =
string.left (97) +
"...";
78 string = QObject::tr (
"Binary entity");
80 if (!p.
Mime_.isEmpty ())
81 string += QObject::tr (
"<br /><br />of type <code>%1</code>").arg (p.
Mime_);
83 if (!p.
Additional_ [
"SourceURL"].toUrl ().isEmpty ())
85 QString urlStr = p.
Additional_ [
"SourceURL"].toUrl ().toString ();
86 if (urlStr.size () > 63)
87 urlStr = urlStr.left (60) +
"...";
88 string += QObject::tr (
"<br />from %1")
97 QString MakePrettySizeWith (qint64 sourceSize,
const QStringList& units)
100 long double size = sourceSize;
102 for (; strNum < 3 && size >= 1024; ++strNum, size /= 1024)
105 return QString::number (size,
'f', 1) + units.value (strNum);
111 static QStringList units
114 QObject::tr (
" KiB"),
115 QObject::tr (
" MiB"),
119 return MakePrettySizeWith (sourcesize, units);
124 static const QStringList units
126 QObject::tr (
"b",
"Short one-character unit for bytes."),
127 QObject::tr (
"K",
"Short one-character unit for kilobytes."),
128 QObject::tr (
"M",
"Short one-character unit for megabytes."),
129 QObject::tr (
"G",
"Short one-character unit for gigabytes.")
132 return MakePrettySizeWith (sourcesize, units);
137 int d = time / 86400;
141 result += QObject::tr (
"%n day(s), ",
"", d);
142 result += QTime (0, 0, 0).addSecs (time).toString ();
147 const QString& localeName,
148 const QString& prefix,
149 const QString& appName)
151 auto filename = prefix;
152 filename.append (
"_");
153 if (!baseName.isEmpty ())
154 filename.append (baseName).append (
"_");
155 filename.append (localeName);
157 auto transl =
new QTranslator;
159 if (transl->load (filename,
":/") ||
160 transl->load (filename,
161 QCoreApplication::applicationDirPath () +
"/translations"))
162 #elif defined (Q_OS_MAC) && !defined (USE_UNIX_LAYOUT)
163 if (transl->load (filename,
":/") ||
164 transl->load (filename,
165 QCoreApplication::applicationDirPath () +
"/../Resources/translations"))
166 #elif defined (INSTALL_PREFIX)
167 if (transl->load (filename,
":/") ||
168 transl->load (filename,
169 QString (INSTALL_PREFIX
"/share/%1/translations").arg (appName)))
171 if (transl->load (filename,
":/") ||
172 transl->load (filename,
173 QString (
"/usr/local/share/%1/translations").arg (appName)) ||
174 transl->load (filename,
175 QString (
"/usr/share/%1/translations").arg (appName)))
185 const QString& prefix,
186 const QString& appName)
189 if (
auto transl =
LoadTranslator (baseName, localeName, prefix, appName))
191 qApp->installTranslator (transl);
195 qWarning () << Q_FUNC_INFO
196 <<
"could not load translation file for locale" 206 QSettings settings (QCoreApplication::organizationName (),
207 QCoreApplication::applicationName ());
208 QString localeName = settings.value (
"Language",
"system").toString ();
210 if (localeName ==
"system")
212 localeName = QString (::getenv (
"LANG")).left (5);
214 if (localeName ==
"C" || localeName.isEmpty ())
215 localeName =
"en_US";
217 if (localeName.isEmpty () || localeName.size () != 5)
218 localeName = QLocale::system ().name ();
219 localeName = localeName.left (5);
222 if (localeName.size () == 2)
224 auto lang = QLocale (localeName).language ();
225 const auto& cs = QLocale::countriesForLanguage (lang);
229 localeName = QLocale (lang, cs.at (0)).name ();
237 if (locale.language () == QLocale::AnyLanguage)
240 auto locStr = locale.name ();
241 locStr.replace (
'_',
'-');
252 QAction *senderAct = qobject_cast<QAction*> (sender);
257 QDebug d (&debugString);
258 d <<
"sender is not a QAction*" 261 throw std::runtime_error (qPrintable (debugString));
270 QAction *result =
new QAction (parent);
271 result->setSeparator (
true);
276 const QString& text, QFont font,
const QPen& pen,
const QBrush& brush)
278 const auto& iconSize = px.size ();
280 const auto fontHeight = px.height () * 0.45;
281 font.setPixelSize (std::max (6., fontHeight));
283 const QFontMetrics fm (font);
284 const auto width = fm.width (text) + 2. * px.width () / 10.;
285 const auto height = fm.height () + 2. * px.height () / 10.;
286 const bool tooSmall = width > iconSize.width ();
288 const QRect textRect (iconSize.width () - width, iconSize.height () - height, width, height);
294 p.setRenderHint (QPainter::Antialiasing);
295 p.setRenderHint (QPainter::TextAntialiasing);
296 p.setRenderHint (QPainter::HighQualityAntialiasing);
297 p.drawRoundedRect (textRect, 4, 4);
298 p.drawText (textRect,
300 tooSmall ?
"#" : text);
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 QString MakePrettySizeShort(qint64 size)
Converts a bytes count to a string representation with appropriately chosen units.
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.
A message used for inter-plugin communication.