]> git.sur5r.net Git - minitube/blobdiff - src/painterutils.cpp
Switch watchfile to Github API
[minitube] / src / painterutils.cpp
index 9281b0d62ab5003eea0e2418c9557bf8b01d6ac1..7d145f79458b9e03b4e139973f1fd350bcff1541 100644 (file)
@@ -22,86 +22,45 @@ $END_LICENSE */
 #include "fontutils.h"
 #include "iconutils.h"
 
-PainterUtils::PainterUtils() { }
+PainterUtils::PainterUtils() {}
 
-void PainterUtils::centeredMessage(const QString &message, QWidgetwidget) {
+void PainterUtils::centeredMessage(const QString &message, QWidget *widget) {
     QPainter painter(widget);
     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) {
-    const QPixmap badge1 = IconUtils::pixmap(":/images/badge.png");
-    const QPixmap badge3 = IconUtils::pixmap(":/images/badge3.png");
-    const QPixmap badge4 = IconUtils::pixmap(":/images/badge4.png");
-
-    const int textSize = text.size();
-
-    QPixmap badge;
-    if (textSize < 3) badge = badge1;
-    else if (textSize == 3) badge = badge3;
-    else badge = badge4;
-
-    const int w = badge.width() / badge.devicePixelRatio();
-    const int h = badge.height() / badge.devicePixelRatio();
+void PainterUtils::paintBadge(QPainter *painter,
+                              const QString &text,
+                              bool center,
+                              const QColor &backgroundColor,
+                              bool literalColor) {
+    painter->save();
 
+    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, h);
-    painter->drawPixmap(rect, badge);
-
-    QFont f = painter->font();
-    f.setPixelSize(11);
-    f.setHintingPreference(QFont::PreferNoHinting);
-#ifdef APP_MAC
-    f.setFamily("Helvetica");
-#elif APP_WIN
-    rect.adjust(0, -2, 0, 0);
-#else
-    rect.adjust(0, -1, 0, 0);
-#endif
-    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 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);