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;
160 if (transl->load (filename,
":/") ||
161 transl->load (filename,
162 QCoreApplication::applicationDirPath () +
"/translations"))
163 #elif defined (Q_OS_MAC) && !defined (USE_UNIX_LAYOUT)
165 if (transl->load (filename,
":/") ||
166 transl->load (filename,
167 QCoreApplication::applicationDirPath () +
"/../Resources/translations"))
168 #elif defined (INSTALL_PREFIX)
169 if (transl->load (filename,
":/") ||
170 transl->load (filename,
171 QString (INSTALL_PREFIX
"/share/%1/translations").arg (appName)))
173 if (transl->load (filename,
":/") ||
174 transl->load (filename,
175 QString (
"/usr/local/share/%1/translations").arg (appName)) ||
176 transl->load (filename,
177 QString (
"/usr/share/%1/translations").arg (appName)))
187 const QString& prefix,
188 const QString& appName)
191 if (
auto transl =
LoadTranslator (baseName, localeName, prefix, appName))
193 qApp->installTranslator (transl);
197 qWarning () << Q_FUNC_INFO
198 <<
"could not load translation file for locale" 208 QSettings settings (QCoreApplication::organizationName (),
209 QCoreApplication::applicationName ());
210 QString localeName = settings.value (
"Language",
"system").toString ();
212 if (localeName ==
"system")
214 localeName = QString (::getenv (
"LANG")).left (5);
216 if (localeName ==
"C" || localeName.isEmpty ())
217 localeName =
"en_US";
219 if (localeName.isEmpty () || localeName.size () != 5)
220 localeName = QLocale::system ().name ();
221 localeName = localeName.left (5);
224 if (localeName.size () == 2)
226 auto lang = QLocale (localeName).language ();
227 const auto& cs = QLocale::countriesForLanguage (lang);
231 localeName = QLocale (lang, cs.at (0)).name ();
239 if (locale.language () == QLocale::AnyLanguage)
242 auto locStr = locale.name ();
243 locStr.replace (
'_',
'-');
254 QAction *senderAct = qobject_cast<QAction*> (sender);
259 QDebug d (&debugString);
260 d <<
"sender is not a QAction*" 263 throw std::runtime_error (qPrintable (debugString));
272 QAction *result =
new QAction (parent);
273 result->setSeparator (
true);
278 const QString& text, QFont font,
const QPen& pen,
const QBrush& brush)
280 const auto& iconSize = px.size () / px.devicePixelRatio ();
282 const auto fontHeight = iconSize.height () * 0.45;
283 font.setPixelSize (
std::max (6., fontHeight));
285 const QFontMetrics fm (font);
286 const auto width = fm.horizontalAdvance (text) + 2. * iconSize.width () / 10.;
287 const auto height = fm.height () + 2. * iconSize.height () / 10.;
288 const bool tooSmall = width > iconSize.width ();
290 const QRect textRect (iconSize.width () - width, iconSize.height () - height, width, height);
296 p.setRenderHint (QPainter::Antialiasing);
297 p.setRenderHint (QPainter::TextAntialiasing);
298 p.drawRoundedRect (textRect, 4, 4);
299 p.drawText (textRect,
301 tooSmall ?
"#" : text);
UTIL_API QString GetInternetLocaleName(const QLocale &)
UTIL_API QAction * CreateSeparator(QObject *parent)
Returns the action that is set to act as a separator.
UTIL_API QString GetAsBase64Src(const QImage &image)
Returns the given image in a Base64-encoded form.
constexpr detail::AggregateType< detail::AggregateFunction::Max, Ptr > max
UTIL_API QString GetLanguage()
Returns the current language name.
UTIL_API QPixmap DrawOverlayText(QPixmap px, const QString &text, QFont font, const QPen &pen, const QBrush &brush)
UTIL_API QString GetLocaleName()
Returns the current locale name, like en_US.
QString Mime_
MIME type of the entity.
UTIL_API QTranslator * InstallTranslator(const QString &base, const QString &prefix="leechcraft", const QString &appname="leechcraft")
Loads and installs a translator.
QVariant Entity_
The entity that this object represents.
UTIL_API QString MakePrettySizeShort(qint64 size)
Converts a bytes count to a string representation with appropriately chosen units.
UTIL_API QModelIndexList GetSummarySelectedRows(QObject *sender)
UTIL_API QString MakeTimeFromLong(ulong time)
Makes a formatted time from number.
UTIL_API QString MakePrettySize(qint64 sourceSize)
Makes a formatted size from number.
QMap< QString, QVariant > Additional_
Additional parameters.
UTIL_API QString GetUserText(const Entity &entity)
Return the user-readable representation of the entity.
A message used for inter-plugin communication.
UTIL_API QTranslator * LoadTranslator(const QString &base, const QString &locale, const QString &prefix="leechcraft", const QString &appname="leechcraft")