#include "painterutils.h"
#include "fontutils.h"
+#include "iconutils.h"
-PainterUtils::PainterUtils() { }
+PainterUtils::PainterUtils() {}
-void PainterUtils::centeredMessage(QString message, QWidget* widget) {
+void PainterUtils::centeredMessage(const QString &message, QWidget *widget) {
QPainter painter(widget);
- painter.setFont(FontUtils::bigBold());
+ painter.setFont(FontUtils::big());
QSize textSize(QFontMetrics(painter.font()).size(Qt::TextSingleLine, message));
- QPoint topLeft(
- (widget->width()-textSize.width())/2,
- ((widget->height()-textSize.height())/2)
- );
+ QPoint topLeft((widget->width() - textSize.width()) / 2,
+ ((widget->height() - textSize.height()) / 2));
QRect rect(topLeft, textSize);
-
- /*
- rect.adjust(0, -1, 0, 0);
- painter.setPen(QColor(0, 0, 0, 128));
- painter.drawText(rect, Qt::AlignCenter, message);
- rect.adjust(0, 1, 0, 0);
- */
-
- QPen textPen;
- textPen.setBrush(widget->palette().mid());
- painter.setPen(textPen);
+ painter.setOpacity(.5);
painter.drawText(rect, Qt::AlignCenter, message);
}
-void PainterUtils::topShadow(QWidget *widget) {
- static QLinearGradient shadow;
- static const int shadowHeight = 10;
- if (shadow.stops().count() == 2) {
- shadow.setFinalStop(0, shadowHeight);
- const qreal initialOpacity = 96;
- for (qreal i = 0; i <= 1; i += 1.0/shadowHeight) {
- qreal opacity = qPow(initialOpacity, (1.0 - i)) - 1;
- shadow.setColorAt(i, QColor(0x00, 0x00, 0x00, opacity));
- }
- }
- QRect rect = widget->rect();
- QPainter p(widget);
- p.fillRect(rect.x(), rect.y(), rect.width(), shadowHeight, QBrush(shadow));
-}
-
-void PainterUtils::paintBadge(QPainter *painter, const QString &text, bool center) {
- static const QPixmap badge1 = QPixmap(":/images/badge.png");
- static const QPixmap badge3 = QPixmap(":/images/badge3.png");
- static const QPixmap badge4 = QPixmap(":/images/badge4.png");
- static const int size = badge1.height();
-
- const int textSize = text.size();
-
- QPixmap badge;
- if (textSize < 3) badge = badge1;
- else if (textSize == 3) badge = badge3;
- else badge = badge4;
-
- int x = 0;
- if (center) x -= badge.width() / 2;
-
- QRect rect(x, 0, badge.width(), size);
- painter->drawPixmap(rect, badge);
-
- QFont f = painter->font();
- f.setPixelSize(11);
- f.setHintingPreference(QFont::PreferNoHinting);
-#ifdef APP_MAC
- f.setFamily("Helvetica");
-#endif
-#ifdef APP_WIN
- rect.adjust(0, -2, 0, 0);
-#endif
-#ifdef Q_WS_X11
- rect.adjust(0, -1, 0, 0);
-#endif
+void PainterUtils::paintBadge(QPainter *painter,
+ const QString &text,
+ bool center,
+ const QColor &backgroundColor,
+ bool literalColor) {
painter->save();
- painter->setFont(f);
- rect.adjust(0, 1, 0, 0);
- painter->setPen(QColor(0, 0, 0, 64));
- painter->drawText(rect, Qt::AlignCenter, text);
-
- rect.adjust(0, -1, 0, 0);
+ QRect textBox = painter->boundingRect(QRect(), Qt::AlignCenter, text);
+ int w = textBox.width() + painter->fontMetrics().width('m');
+ int x = 0;
+ if (center) x -= w / 2;
+ QRect rect(x, 0, w, textBox.height());
+ if (rect.width() < rect.height() || text.length() == 1) rect.setWidth(rect.height());
+
+ painter->setPen(Qt::NoPen);
+ QColor bg;
+ if (literalColor)
+ bg = backgroundColor;
+ else
+ bg = backgroundColor.value() > 128 ? QColor(0, 0, 0, 64) : QColor(255, 255, 255, 64);
+ painter->setBrush(bg);
+ painter->setRenderHint(QPainter::Antialiasing);
+ qreal borderRadius = rect.height() / 2.;
+ painter->drawRoundedRect(rect, borderRadius, borderRadius);
+
+ painter->setFont(FontUtils::small());
painter->setPen(Qt::white);
painter->drawText(rect, Qt::AlignCenter, text);