diff --git a/src/gui/properties/downloadedpiecesbar.cpp b/src/gui/properties/downloadedpiecesbar.cpp index fd76d6c63..0f1ec8cea 100644 --- a/src/gui/properties/downloadedpiecesbar.cpp +++ b/src/gui/properties/downloadedpiecesbar.cpp @@ -1,5 +1,6 @@ /* * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2024 Vladimir Golovnev * Copyright (C) 2006 Christophe Dumez * * This program is free software; you can redistribute it and/or @@ -46,9 +47,9 @@ namespace } DownloadedPiecesBar::DownloadedPiecesBar(QWidget *parent) - : base {parent} - , m_dlPieceColor {dlPieceColor(pieceColor())} + : base(parent) { + updateColorsImpl(); } QList DownloadedPiecesBar::bitfieldToFloatVector(const QBitArray &vecin, int reqSize) @@ -128,25 +129,24 @@ QList DownloadedPiecesBar::bitfieldToFloatVector(const QBitArray &vecin, return result; } -bool DownloadedPiecesBar::updateImage(QImage &image) +QImage DownloadedPiecesBar::renderImage() { // qDebug() << "updateImage"; - QImage image2(width() - 2 * borderWidth, 1, QImage::Format_RGB888); - if (image2.isNull()) + QImage image {width() - 2 * borderWidth, 1, QImage::Format_RGB888}; + if (image.isNull()) { - qDebug() << "QImage image2() allocation failed, width():" << width(); - return false; + qDebug() << "QImage allocation failed, width():" << width(); + return image; } if (m_pieces.isEmpty()) { - image2.fill(backgroundColor()); - image = image2; - return true; + image.fill(backgroundColor()); + return image; } - QList scaledPieces = bitfieldToFloatVector(m_pieces, image2.width()); - QList scaledPiecesDl = bitfieldToFloatVector(m_downloadedPieces, image2.width()); + QList scaledPieces = bitfieldToFloatVector(m_pieces, image.width()); + QList scaledPiecesDl = bitfieldToFloatVector(m_downloadedPieces, image.width()); // filling image for (int x = 0; x < scaledPieces.size(); ++x) @@ -161,15 +161,15 @@ bool DownloadedPiecesBar::updateImage(QImage &image) QRgb mixedColor = mixTwoColors(pieceColor().rgb(), m_dlPieceColor.rgb(), ratio); mixedColor = mixTwoColors(backgroundColor().rgb(), mixedColor, fillRatio); - image2.setPixel(x, 0, mixedColor); + image.setPixel(x, 0, mixedColor); } else { - image2.setPixel(x, 0, pieceColors()[piecesToValue * 255]); + image.setPixel(x, 0, pieceColors()[piecesToValue * 255]); } } - image = image2; - return true; + + return image; } void DownloadedPiecesBar::setProgress(const QBitArray &pieces, const QBitArray &downloadedPieces) @@ -177,7 +177,7 @@ void DownloadedPiecesBar::setProgress(const QBitArray &pieces, const QBitArray & m_pieces = pieces; m_downloadedPieces = downloadedPieces; - requestImageUpdate(); + redraw(); } void DownloadedPiecesBar::clear() @@ -198,3 +198,14 @@ QString DownloadedPiecesBar::simpleToolTipText() const + u""; } + +void DownloadedPiecesBar::updateColors() +{ + PiecesBar::updateColors(); + updateColorsImpl(); +} + +void DownloadedPiecesBar::updateColorsImpl() +{ + m_dlPieceColor = dlPieceColor(pieceColor()); +} diff --git a/src/gui/properties/downloadedpiecesbar.h b/src/gui/properties/downloadedpiecesbar.h index c7357316f..85e9e0c8d 100644 --- a/src/gui/properties/downloadedpiecesbar.h +++ b/src/gui/properties/downloadedpiecesbar.h @@ -1,5 +1,6 @@ /* * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2024 Vladimir Golovnev * Copyright (C) 2006 Christophe Dumez * * This program is free software; you can redistribute it and/or @@ -52,11 +53,13 @@ public: private: // scale bitfield vector to float vector QList bitfieldToFloatVector(const QBitArray &vecin, int reqSize); - bool updateImage(QImage &image) override; + QImage renderImage() override; QString simpleToolTipText() const override; + void updateColors() override; + void updateColorsImpl(); // incomplete piece color - const QColor m_dlPieceColor; + QColor m_dlPieceColor; // last used bitfields, uses to better resize redraw // TODO: make a diff pieces to new pieces and update only changed pixels, speedup when update > 20x faster QBitArray m_pieces; diff --git a/src/gui/properties/pieceavailabilitybar.cpp b/src/gui/properties/pieceavailabilitybar.cpp index 9315f0c0b..5a9e61845 100644 --- a/src/gui/properties/pieceavailabilitybar.cpp +++ b/src/gui/properties/pieceavailabilitybar.cpp @@ -1,5 +1,6 @@ /* * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2024 Vladimir Golovnev * Copyright (C) 2006 Christophe Dumez * * This program is free software; you can redistribute it and/or @@ -126,39 +127,38 @@ QList PieceAvailabilityBar::intToFloatVector(const QList &vecin, int return result; } -bool PieceAvailabilityBar::updateImage(QImage &image) +QImage PieceAvailabilityBar::renderImage() { - QImage image2(width() - 2 * borderWidth, 1, QImage::Format_RGB888); - if (image2.isNull()) + QImage image {width() - 2 * borderWidth, 1, QImage::Format_RGB888}; + if (image.isNull()) { - qDebug() << "QImage image2() allocation failed, width():" << width(); - return false; + qDebug() << "QImage allocation failed, width():" << width(); + return image; } if (m_pieces.empty()) { - image2.fill(backgroundColor()); - image = image2; - return true; + image.fill(backgroundColor()); + return image; } - QList scaledPieces = intToFloatVector(m_pieces, image2.width()); + QList scaledPieces = intToFloatVector(m_pieces, image.width()); // filling image for (int x = 0; x < scaledPieces.size(); ++x) { float piecesToValue = scaledPieces.at(x); - image2.setPixel(x, 0, pieceColors()[piecesToValue * 255]); + image.setPixel(x, 0, pieceColors()[piecesToValue * 255]); } - image = image2; - return true; + + return image; } void PieceAvailabilityBar::setAvailability(const QList &avail) { m_pieces = avail; - requestImageUpdate(); + redraw(); } void PieceAvailabilityBar::clear() diff --git a/src/gui/properties/pieceavailabilitybar.h b/src/gui/properties/pieceavailabilitybar.h index f96ad9e33..3fb6be98e 100644 --- a/src/gui/properties/pieceavailabilitybar.h +++ b/src/gui/properties/pieceavailabilitybar.h @@ -1,5 +1,6 @@ /* * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2024 Vladimir Golovnev * Copyright (C) 2006 Christophe Dumez * * This program is free software; you can redistribute it and/or @@ -46,7 +47,7 @@ public: void clear() override; private: - bool updateImage(QImage &image) override; + QImage renderImage() override; QString simpleToolTipText() const override; // last used int vector, uses to better resize redraw diff --git a/src/gui/properties/piecesbar.cpp b/src/gui/properties/piecesbar.cpp index 7e948019d..e922e2440 100644 --- a/src/gui/properties/piecesbar.cpp +++ b/src/gui/properties/piecesbar.cpp @@ -1,5 +1,6 @@ /* * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2024 Vladimir Golovnev * Copyright (C) 2016 Eugene Shalygin * Copyright (C) 2006 Christophe Dumez * @@ -41,6 +42,7 @@ #include "base/indexrange.h" #include "base/path.h" #include "base/utils/misc.h" +#include "gui/uithememanager.h" namespace { @@ -114,10 +116,16 @@ namespace } PiecesBar::PiecesBar(QWidget *parent) - : QWidget {parent} + : QWidget(parent) { - updatePieceColors(); setMouseTracking(true); + + updateColorsImpl(); + connect(UIThemeManager::instance(), &UIThemeManager::themeChanged, this, [this] + { + updateColors(); + redraw(); + }); } void PiecesBar::setTorrent(const BitTorrent::Torrent *torrent) @@ -154,7 +162,7 @@ void PiecesBar::leaveEvent(QEvent *e) { m_hovered = false; m_highlightedRegion = {}; - requestImageUpdate(); + redraw(); base::leaveEvent(e); } @@ -178,7 +186,10 @@ void PiecesBar::paintEvent(QPaintEvent *) else { if (m_image.width() != imageRect.width()) - updateImage(m_image); + { + if (const QImage image = renderImage(); !image.isNull()) + m_image = image; + } painter.drawImage(imageRect, m_image); } @@ -196,30 +207,33 @@ void PiecesBar::paintEvent(QPaintEvent *) painter.drawPath(border); } -void PiecesBar::requestImageUpdate() +void PiecesBar::redraw() { - if (updateImage(m_image)) + if (const QImage image = renderImage(); !image.isNull()) + { + m_image = image; update(); + } } QColor PiecesBar::backgroundColor() const { - return palette().color(QPalette::Base); + return palette().color(QPalette::Active, QPalette::Base); } QColor PiecesBar::borderColor() const { - return palette().color(QPalette::Dark); + return palette().color(QPalette::Active, QPalette::Dark); } QColor PiecesBar::pieceColor() const { - return palette().color(QPalette::Highlight); + return palette().color(QPalette::Active, QPalette::Highlight); } QColor PiecesBar::colorBoxBorderColor() const { - return palette().color(QPalette::ToolTipText); + return palette().color(QPalette::Active, QPalette::ToolTipText); } const QList &PiecesBar::pieceColors() const @@ -325,12 +339,17 @@ void PiecesBar::highlightFile(int imagePos) } } -void PiecesBar::updatePieceColors() +void PiecesBar::updateColors() +{ + updateColorsImpl(); +} + +void PiecesBar::updateColorsImpl() { m_pieceColors = QList(256); for (int i = 0; i < 256; ++i) { - float ratio = (i / 255.0); + const float ratio = (i / 255.0); m_pieceColors[i] = mixTwoColors(backgroundColor().rgb(), pieceColor().rgb(), ratio); } } diff --git a/src/gui/properties/piecesbar.h b/src/gui/properties/piecesbar.h index 42a614091..b761db2cd 100644 --- a/src/gui/properties/piecesbar.h +++ b/src/gui/properties/piecesbar.h @@ -1,5 +1,6 @@ /* * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2024 Vladimir Golovnev * Copyright (C) 2016 Eugene Shalygin * Copyright (C) 2006 Christophe Dumez * @@ -54,17 +55,15 @@ public: virtual void clear(); - // QObject interface - bool event(QEvent *e) override; - protected: - // QWidget interface + bool event(QEvent *e) override; void enterEvent(QEnterEvent *e) override; void leaveEvent(QEvent *e) override; void mouseMoveEvent(QMouseEvent *e) override; - void paintEvent(QPaintEvent *e) override; - void requestImageUpdate(); + + virtual void updateColors(); + void redraw(); QColor backgroundColor() const; QColor borderColor() const; @@ -82,11 +81,9 @@ private: void highlightFile(int imagePos); virtual QString simpleToolTipText() const = 0; + virtual QImage renderImage() = 0; - // draw new image to replace the actual image - // returns true if image was successfully updated - virtual bool updateImage(QImage &image) = 0; - void updatePieceColors(); + void updateColorsImpl(); const BitTorrent::Torrent *m_torrent = nullptr; QImage m_image;