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")
98 QString MakePrettySizeWith (qint64 sourceSize,
const QStringList& units)
101 long double size = sourceSize;
103 for (; strNum < 3 && size >= 1024; ++strNum, size /= 1024)
106 return QString::number (size,
'f', 1) + units.value (strNum);
112 static QStringList units
115 QObject::tr (
" KiB"),
116 QObject::tr (
" MiB"),
120 return MakePrettySizeWith (sourcesize, units);
125 static const QStringList units
127 QObject::tr (
"b",
"Short one-character unit for bytes."),
128 QObject::tr (
"K",
"Short one-character unit for kilobytes."),
129 QObject::tr (
"M",
"Short one-character unit for megabytes."),
130 QObject::tr (
"G",
"Short one-character unit for gigabytes.")
133 return MakePrettySizeWith (sourcesize, units);
138 int d = time / 86400;
142 result += QObject::tr (
"%n day(s), ",
"", d);
143 result += QTime (0, 0, 0).addSecs (time).toString ();
148 const QString& localeName,
149 const QString& prefix,
150 const QString& appName)
152 auto filename = prefix;
153 filename.append (
"_");
154 if (!baseName.isEmpty ())
155 filename.append (baseName).append (
"_");
156 filename.append (localeName);
158 auto transl =
new QTranslator;
161 if (transl->load (filename,
":/") ||
162 transl->load (filename,
163 QCoreApplication::applicationDirPath () +
"/translations"))
164 #elif defined (Q_OS_MAC) && !defined (USE_UNIX_LAYOUT)
166 if (transl->load (filename,
":/") ||
167 transl->load (filename,
168 QCoreApplication::applicationDirPath () +
"/../Resources/translations"))
169 #elif defined (INSTALL_PREFIX)
170 if (transl->load (filename,
":/") ||
171 transl->load (filename,
172 QString (INSTALL_PREFIX
"/share/%1/translations").arg (appName)))
174 if (transl->load (filename,
":/") ||
175 transl->load (filename,
176 QString (
"/usr/local/share/%1/translations").arg (appName)) ||
177 transl->load (filename,
178 QString (
"/usr/share/%1/translations").arg (appName)))
188 const QString& prefix,
189 const QString& appName)
192 if (
auto transl =
LoadTranslator (baseName, localeName, prefix, appName))
194 qApp->installTranslator (transl);
198 qWarning () << Q_FUNC_INFO
199 <<
"could not load translation file for locale"
209 QSettings settings (QCoreApplication::organizationName (),
210 QCoreApplication::applicationName ());
211 QString localeName = settings.value (
"Language",
"system").toString ();
213 if (localeName ==
"system")
215 localeName = QString (::getenv (
"LANG")).left (5);
217 if (localeName ==
"C" || localeName.isEmpty ())
218 localeName =
"en_US";
220 if (localeName.isEmpty () || localeName.size () != 5)
221 localeName = QLocale::system ().name ();
222 localeName = localeName.left (5);
225 if (localeName.size () == 2)
227 auto lang = QLocale (localeName).language ();
228 const auto& cs = QLocale::countriesForLanguage (lang);
232 localeName = QLocale (lang, cs.at (0)).name ();
240 if (locale.language () == QLocale::AnyLanguage)
243 auto locStr = locale.name ();
244 locStr.replace (
'_',
'-');
255 QAction *senderAct = qobject_cast<QAction*> (sender);
260 QDebug d (&debugString);
261 d <<
"sender is not a QAction*"
264 throw std::runtime_error (qPrintable (debugString));
273 QAction *result =
new QAction (parent);
274 result->setSeparator (
true);
279 const QString& text, QFont font,
const QPen& pen,
const QBrush& brush)
281 const auto& iconSize = px.size () / px.devicePixelRatio ();
283 const auto fontHeight = iconSize.height () * 0.45;
284 font.setPixelSize (
std::max (6., fontHeight));
286 const QFontMetrics fm (font);
287 const auto width =
Compat::Width (fm, text) + 2. * iconSize.width () / 10.;
288 const auto height = fm.height () + 2. * iconSize.height () / 10.;
289 const bool tooSmall = width > iconSize.width ();
291 const QRect textRect (iconSize.width () - width, iconSize.height () - height, width, height);
297 p.setRenderHint (QPainter::Antialiasing);
298 p.setRenderHint (QPainter::TextAntialiasing);
299 p.drawRoundedRect (textRect, 4, 4);
300 p.drawText (textRect,
302 tooSmall ?
"#" : text);