Also draw the arrow dark when in dark theme with highlight

This commit is contained in:
Kovid Goyal 2023-02-03 19:46:15 +05:30
parent 118ba06700
commit d6068e0316
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 87 additions and 0 deletions

View File

@ -7,6 +7,7 @@
#include "QProgressIndicator.h"
#include <QPainter>
#include <QPixmapCache>
#include <QtWidgets/QStyle>
#include <QtWidgets/QApplication>
#include <QDebug>
@ -61,6 +62,82 @@ static inline QByteArray detectDesktopEnvironment()
return QByteArrayLiteral("UNKNOWN");
}
#ifdef Q_OS_DARWIN
static const qreal qstyleBaseDpi = 72;
#else
static const qreal qstyleBaseDpi = 96;
#endif
qreal dpiScaled(qreal value, qreal dpi)
{
return value * dpi / qstyleBaseDpi;
}
static QPixmap styleCachePixmap(const QSize &size)
{
const qreal pixelRatio = qApp->devicePixelRatio();
QPixmap cachePixmap = QPixmap(size * pixelRatio);
cachePixmap.setDevicePixelRatio(pixelRatio);
return cachePixmap;
}
static void draw_arrow(Qt::ArrowType type, QPainter *painter, const QStyleOption *option, const QRect &rect, const QColor &color)
{
if (rect.isEmpty())
return;
const qreal dpi = option->fontMetrics.fontDpi();
const int arrowWidth = int(dpiScaled(14, dpi));
const int arrowHeight = int(dpiScaled(8, dpi));
const int arrowMax = qMin(arrowHeight, arrowWidth);
const int rectMax = qMin(rect.height(), rect.width());
const int size = qMin(arrowMax, rectMax);
QPixmap cachePixmap;
QString cacheKey = QString("calibre-tree-view-arrow-%1-%2-%3").arg((unsigned long)color.rgba()).arg((unsigned long)type).arg(size);
if (!QPixmapCache::find(cacheKey, &cachePixmap)) {
cachePixmap = styleCachePixmap(rect.size());
cachePixmap.fill(Qt::transparent);
QPainter cachePainter(&cachePixmap);
QRectF arrowRect;
arrowRect.setWidth(size);
arrowRect.setHeight(arrowHeight * size / arrowWidth);
if (type == Qt::LeftArrow || type == Qt::RightArrow)
arrowRect = arrowRect.transposed();
arrowRect.moveTo((rect.width() - arrowRect.width()) / 2.0,
(rect.height() - arrowRect.height()) / 2.0);
QPolygonF triangle;
triangle.reserve(3);
switch (type) {
case Qt::DownArrow:
triangle << arrowRect.topLeft() << arrowRect.topRight() << QPointF(arrowRect.center().x(), arrowRect.bottom());
break;
case Qt::RightArrow:
triangle << arrowRect.topLeft() << arrowRect.bottomLeft() << QPointF(arrowRect.right(), arrowRect.center().y());
break;
case Qt::LeftArrow:
triangle << arrowRect.topRight() << arrowRect.bottomRight() << QPointF(arrowRect.left(), arrowRect.center().y());
break;
default:
triangle << arrowRect.bottomLeft() << arrowRect.bottomRight() << QPointF(arrowRect.center().x(), arrowRect.top());
break;
}
cachePainter.setPen(Qt::NoPen);
cachePainter.setBrush(color);
cachePainter.setRenderHint(QPainter::Antialiasing);
cachePainter.drawPolygon(triangle);
QPixmapCache::insert(cacheKey, cachePixmap);
}
painter->drawPixmap(rect, cachePixmap);
}
CalibreStyle::CalibreStyle(int transient_scroller) : QProxyStyle(QString::fromUtf8("Fusion")), transient_scroller(transient_scroller) {
setObjectName(QString("calibre"));
@ -212,6 +289,14 @@ void CalibreStyle::drawPrimitive(PrimitiveElement element, const QStyleOption *
}
break; // }}}
case PE_IndicatorBranch: // {{{
if (option->state & State_MouseOver && option->state & State_Children && widget && widget->property("hovered_item_is_highlighted").toBool() && is_color_dark(option->palette.color(QPalette::Window))) {;
if (option->rect.width() <= 1 || option->rect.height() <= 1) return;
Qt::ArrowType arrow = Qt::ArrowType::DownArrow;
if (!(option->state & State_Open)) arrow = Qt::ArrowType::RightArrow;
draw_arrow(arrow, painter, option, option->rect, QColor(Qt::black));
return;
} break; // }}}
case PE_IndicatorToolBarSeparator: // {{{
// Make toolbar separators stand out a bit more in dark themes
{

View File

@ -241,6 +241,7 @@ class TagsView(QTreeView): # {{{
'''.replace('PAD', str(gprefs['tag_browser_item_padding'])) + (
'' if gprefs['tag_browser_old_look'] else stylish_tb) + QApplication.instance().palette_manager.tree_view_hover_style()
)
self.setProperty('hovered_item_is_highlighted', True)
def set_look_and_feel(self, first=False):
self.set_style_sheet()

View File

@ -89,6 +89,7 @@ class TOCView(QTreeView):
}
''' + QApplication.instance().palette_manager.tree_view_hover_style())
self.setProperty('hovered_item_is_highlighted', True)
def mouseMoveEvent(self, ev):
if self.indexAt(ev.pos()).isValid():