]> git.sur5r.net Git - minitube/blobdiff - src/segmentedcontrol.cpp
New upstream version 3.1
[minitube] / src / segmentedcontrol.cpp
index 1af157e950d77cccd6b83a128a1ed9c477d5cf04..e763f12877da3ff73dc1699d1f33ebbd9401ee46 100644 (file)
@@ -19,30 +19,25 @@ along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
 $END_LICENSE */
 
 #include "segmentedcontrol.h"
-#include "mainwindow.h"
 #include "fontutils.h"
 #include "iconutils.h"
+#include "mainwindow.h"
 #include "painterutils.h"
 
-SegmentedControl::SegmentedControl (QWidget *parent) : QWidget(parent) {
+SegmentedControl::SegmentedControl(QWidget *parent) : QWidget(parent) {
     setAttribute(Qt::WA_OpaquePaintEvent);
 
     setMouseTracking(true);
 
-    hoveredAction = 0;
-    checkedAction = 0;
-    pressedAction = 0;
+    hoveredAction = nullptr;
+    checkedAction = nullptr;
+    pressedAction = nullptr;
 
-#ifdef APP_WIN
-    selectedColor = palette().color(QPalette::Base);
-#else
-    selectedColor = palette().color(QPalette::Window);
-#endif
-    int darkerFactor = 105;
-    backgroundColor = selectedColor.darker(darkerFactor);
-    borderColor = backgroundColor;
-    hoveredColor = backgroundColor.darker(darkerFactor);
-    pressedColor = hoveredColor.darker(darkerFactor);
+    setupColors();
+    connect(qApp, &QGuiApplication::paletteChanged, this, [this] {
+        setupColors();
+        update();
+    });
 }
 
 QAction *SegmentedControl::addAction(QAction *action) {
@@ -54,10 +49,10 @@ QAction *SegmentedControl::addAction(QAction *action) {
 
 bool SegmentedControl::setCheckedAction(int index) {
     if (index < 0) {
-        checkedAction = 0;
+        checkedAction = nullptr;
         return true;
     }
-    QActionnewCheckedAction = actionList.at(index);
+    QAction *newCheckedAction = actionList.at(index);
     return setCheckedAction(newCheckedAction);
 }
 
@@ -65,20 +60,19 @@ bool SegmentedControl::setCheckedAction(QAction *action) {
     if (checkedAction == action) {
         return false;
     }
-    if (checkedAction)
-        checkedAction->setChecked(false);
+    if (checkedAction) checkedAction->setChecked(false);
     checkedAction = action;
     checkedAction->setChecked(true);
     update();
     return true;
 }
 
-QSize SegmentedControl::minimumSizeHint (void) const {
+QSize SegmentedControl::minimumSizeHint() const {
     int itemsWidth = calculateButtonWidth() * actionList.size() * 1.2;
-    return(QSize(itemsWidth, QFontMetrics(font()).height() * 1.8));
+    return (QSize(itemsWidth, QFontMetrics(font()).height() * 1.8));
 }
 
-void SegmentedControl::paintEvent (QPaintEvent * /*event*/) {
+void SegmentedControl::paintEvent(QPaintEvent * /*event*/) {
     const int height = rect().height();
     const int width = rect().width();
 
@@ -87,8 +81,7 @@ void SegmentedControl::paintEvent (QPaintEvent * /*event*/) {
     // Calculate Buttons Size & Location
     const int buttonWidth = width / actionList.size();
 
-    const qreal pixelRatio = IconUtils::pixelRatio();
-
+    const qreal pixelRatio = devicePixelRatioF();
     QPen pen(borderColor);
     const qreal penWidth = 1. / pixelRatio;
     pen.setWidthF(penWidth);
@@ -101,22 +94,20 @@ void SegmentedControl::paintEvent (QPaintEvent * /*event*/) {
         QAction *action = actionList.at(i);
         if (i + 1 == actionCount) {
             // last button
-            rect.setWidth(width - buttonWidth * (actionCount-1));
+            rect.setWidth(width - buttonWidth * (actionCount - 1));
             paintButton(&p, rect, action);
         } else {
             paintButton(&p, rect, action);
             rect.moveLeft(rect.x() + rect.width());
         }
     }
-    const qreal y = height - penWidth;
-    p.drawLine(QPointF(0, y), QPointF(width, y));
 }
 
-void SegmentedControl::mouseMoveEvent (QMouseEvent *event) {
+void SegmentedControl::mouseMoveEvent(QMouseEvent *event) {
     QAction *action = findHoveredAction(event->pos());
 
     if (!action && hoveredAction) {
-        hoveredAction = 0;
+        hoveredAction = nullptr;
         update();
     } else if (action && action != hoveredAction) {
         hoveredAction = action;
@@ -138,7 +129,7 @@ void SegmentedControl::mousePressEvent(QMouseEvent *event) {
 
 void SegmentedControl::mouseReleaseEvent(QMouseEvent *event) {
     QWidget::mouseReleaseEvent(event);
-    pressedAction = 0;
+    pressedAction = nullptr;
     if (hoveredAction) {
         bool changed = setCheckedAction(hoveredAction);
         if (changed) hoveredAction->trigger();
@@ -149,22 +140,37 @@ void SegmentedControl::leaveEvent(QEvent *event) {
     QWidget::leaveEvent(event);
     // status tip
     MainWindow::instance()->statusBar()->clearMessage();
-    hoveredAction = 0;
-    pressedAction = 0;
+    hoveredAction = nullptr;
+    pressedAction = nullptr;
     update();
 }
 
-QAction *SegmentedControl::findHoveredAction(const QPoint& pos) const {
+void SegmentedControl::setupColors() {
+    selectedColor = palette().color(QPalette::Base);
+    if (selectedColor.value() > 128) {
+        int factor = 105;
+        backgroundColor = selectedColor.darker(factor);
+        borderColor = backgroundColor;
+        hoveredColor = backgroundColor.darker(factor);
+        pressedColor = hoveredColor.darker(factor);
+    } else {
+        int factor = 130;
+        backgroundColor = selectedColor.lighter(factor);
+        borderColor = backgroundColor;
+        hoveredColor = backgroundColor.lighter(factor);
+        pressedColor = hoveredColor.lighter(factor);
+    }
+}
+
+QAction *SegmentedControl::findHoveredAction(const QPoint &pos) const {
     const int w = width();
-    if (pos.y() <= 0 || pos.x() >= w || pos.y() >= height())
-        return 0;
+    if (pos.y() <= 0 || pos.x() >= w || pos.y() >= height()) return nullptr;
 
     int buttonWidth = w / actionList.size();
 
     int buttonIndex = pos.x() / buttonWidth;
 
-    if (buttonIndex >= actionList.size())
-        return 0;
+    if (buttonIndex >= actionList.size()) return nullptr;
     return actionList[buttonIndex];
 }
 
@@ -178,7 +184,7 @@ int SegmentedControl::calculateButtonWidth() const {
     return itemWidth;
 }
 
-void SegmentedControl::paintButton(QPainter *painter, const QRectrect, const QAction *action) {
+void SegmentedControl::paintButton(QPainter *painter, const QRect &rect, const QAction *action) {
     painter->save();
     painter->translate(rect.topLeft());
 
@@ -203,11 +209,9 @@ void SegmentedControl::paintButton(QPainter *painter, const QRect& rect, const Q
     painter->drawText(0, 0, width, height, Qt::AlignCenter, text);
 
     if (action->property("notifyCount").isValid()) {
-        QRect textBox = painter->boundingRect(rect,
-                                              Qt::AlignCenter,
-                                              text);
+        QRect textBox = painter->boundingRect(rect, Qt::AlignCenter, text);
         painter->translate((width + textBox.width()) / 2 + 10, (height - textBox.height()) / 2);
-        PainterUtils::paintBadge(painter, action->property("notifyCount").toString(), false, QColor(0,0,0,64));
+        PainterUtils::paintBadge(painter, action->property("notifyCount").toString(), false, c);
     }
 
     painter->restore();