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")
101 QString MakePrettySizeWith (qint64 sourceSize,
const QStringList& units)
104 long double size = sourceSize;
106 for (; strNum < 3 && size >= 1024; ++strNum, size /= 1024)
109 return QString::number (size,
'f', 1) + units.value (strNum);
115 static QStringList units
118 QObject::tr (
" KiB"),
119 QObject::tr (
" MiB"),
123 return MakePrettySizeWith (sourcesize, units);
128 static const QStringList units
130 QObject::tr (
"b",
"Short one-character unit for bytes."),
131 QObject::tr (
"K",
"Short one-character unit for kilobytes."),
132 QObject::tr (
"M",
"Short one-character unit for megabytes."),
133 QObject::tr (
"G",
"Short one-character unit for gigabytes.")
136 return MakePrettySizeWith (sourcesize, units);
141 int d = time / 86400;
145 result += QObject::tr (
"%n day(s), ",
"", d);
146 result += QTime (0, 0, 0).addSecs (time).toString ();
151 const QString& localeName,
152 const QString& prefix,
153 const QString& appName)
155 auto filename = prefix;
156 filename.append (
"_");
157 if (!baseName.isEmpty ())
158 filename.append (baseName).append (
"_");
159 filename.append (localeName);
161 auto transl =
new QTranslator;
163 if (transl->load (filename,
":/") ||
164 transl->load (filename,
165 QCoreApplication::applicationDirPath () +
"/translations"))
166 #elif defined (Q_OS_MAC) && !defined (USE_UNIX_LAYOUT)
167 if (transl->load (filename,
":/") ||
168 transl->load (filename,
169 QCoreApplication::applicationDirPath () +
"/../Resources/translations"))
170 #elif defined (INSTALL_PREFIX)
171 if (transl->load (filename,
":/") ||
172 transl->load (filename,
173 QString (INSTALL_PREFIX
"/share/%1/translations").arg (appName)))
175 if (transl->load (filename,
":/") ||
176 transl->load (filename,
177 QString (
"/usr/local/share/%1/translations").arg (appName)) ||
178 transl->load (filename,
179 QString (
"/usr/share/%1/translations").arg (appName)))
189 const QString& prefix,
190 const QString& appName)
193 if (
auto transl =
LoadTranslator (baseName, localeName, prefix, appName))
195 qApp->installTranslator (transl);
199 qWarning () << Q_FUNC_INFO
200 <<
"could not load translation file for locale" 210 QSettings settings (QCoreApplication::organizationName (),
211 QCoreApplication::applicationName ());
212 QString localeName = settings.value (
"Language",
"system").toString ();
214 if (localeName ==
"system")
216 localeName = QString (::getenv (
"LANG")).left (5);
218 if (localeName ==
"C" || localeName.isEmpty ())
219 localeName =
"en_US";
221 if (localeName.isEmpty () || localeName.size () != 5)
222 localeName = QLocale::system ().name ();
223 localeName = localeName.left (5);
226 if (localeName.size () == 2)
228 auto lang = QLocale (localeName).language ();
229 const auto& cs = QLocale::countriesForLanguage (lang);
233 localeName = QLocale (lang, cs.at (0)).name ();
241 if (locale.language () == QLocale::AnyLanguage)
244 auto locStr = locale.name ();
245 locStr.replace (
'_',
'-');
256 QAction *senderAct = qobject_cast<QAction*> (sender);
261 QDebug d (&debugString);
262 d <<
"sender is not a QAction*" 265 throw std::runtime_error (qPrintable (debugString));
274 QAction *result =
new QAction (parent);
275 result->setSeparator (
true);
280 const QString& text, QFont font,
const QPen& pen,
const QBrush& brush)
282 const auto& iconSize = px.size ();
284 const auto fontHeight = px.height () * 0.45;
285 font.setPixelSize (std::max (6., fontHeight));
287 const QFontMetrics fm (font);
288 const auto width = fm.width (text) + 2. * px.width () / 10.;
289 const auto height = fm.height () + 2. * px.height () / 10.;
290 const bool tooSmall = width > iconSize.width ();
292 const QRect textRect (iconSize.width () - width, iconSize.height () - height, width, height);
298 p.setRenderHint (QPainter::Antialiasing);
299 p.setRenderHint (QPainter::TextAntialiasing);
300 p.setRenderHint (QPainter::HighQualityAntialiasing);
301 p.drawRoundedRect (textRect, 4, 4);
302 p.drawText (textRect,
304 tooSmall ?
"#" : text);
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 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.
Describes parameters of an entity.