Merge pull request #13256 from jagannatharjun/pieces-style

Use palette colors in pieces bars
This commit is contained in:
Mike Tzou 2020-08-23 11:26:48 +08:00 committed by GitHub
commit 4539c679d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 42 deletions

View file

@ -120,7 +120,7 @@ bool DownloadedPiecesBar::updateImage(QImage &image)
}
if (m_pieces.isEmpty()) {
image2.fill(Qt::white);
image2.fill(backgroundColor());
image = image2;
return true;
}
@ -157,12 +157,6 @@ void DownloadedPiecesBar::setProgress(const QBitArray &pieces, const QBitArray &
requestImageUpdate();
}
void DownloadedPiecesBar::setColors(const QColor &background, const QColor &border, const QColor &complete, const QColor &incomplete)
{
m_dlPieceColor = incomplete;
base::setColors(background, border, complete);
}
void DownloadedPiecesBar::clear()
{
m_pieces.clear();
@ -172,7 +166,11 @@ void DownloadedPiecesBar::clear()
QString DownloadedPiecesBar::simpleToolTipText() const
{
return tr("White: Missing pieces") + '\n'
+ tr("Green: Partial pieces") + '\n'
+ tr("Blue: Completed pieces") + '\n';
const QString borderColor = colorBoxBorderColor().name();
const QString rowHTML = QString::fromLatin1("<tr><td width=20 bgcolor='%1' style='border: 1px solid \"%2\";'></td><td>%3</td></tr>");
return QLatin1String("<table cellspacing=4>")
+ rowHTML.arg(backgroundColor().name(), borderColor, tr("Missing pieces"))
+ rowHTML.arg(m_dlPieceColor.name(), borderColor, tr("Partial pieces"))
+ rowHTML.arg(pieceColor().name(), borderColor, tr("Completed pieces"))
+ QLatin1String("</table>");
}

View file

@ -47,8 +47,6 @@ public:
void setProgress(const QBitArray &pieces, const QBitArray &downloadedPieces);
void setColors(const QColor &background, const QColor &border, const QColor &complete, const QColor &incomplete);
// PiecesBar interface
void clear() override;

View file

@ -127,7 +127,7 @@ bool PieceAvailabilityBar::updateImage(QImage &image)
}
if (m_pieces.empty()) {
image2.fill(Qt::white);
image2.fill(backgroundColor());
image = image2;
return true;
}
@ -158,6 +158,10 @@ void PieceAvailabilityBar::clear()
QString PieceAvailabilityBar::simpleToolTipText() const
{
return tr("White: Unavailable pieces") + '\n'
+ tr("Blue: Available pieces") + '\n';
const QString borderColor = colorBoxBorderColor().name();
const QString rowHTML = QString::fromLatin1("<tr><td width=20 bgcolor='%1' style='border: 1px solid \"%2\";'></td><td>%3</td></tr>");
return QLatin1String("<table cellspacing=4>")
+ rowHTML.arg(backgroundColor().name(), borderColor, tr("Unavailable pieces"))
+ rowHTML.arg(pieceColor().name(), borderColor, tr("Available pieces"))
+ QLatin1String("</table>");
}

View file

@ -112,9 +112,6 @@ namespace
PiecesBar::PiecesBar(QWidget *parent)
: QWidget {parent}
, m_torrent {nullptr}
, m_borderColor {palette().color(QPalette::Dark)}
, m_bgColor {Qt::white}
, m_pieceColor {Qt::blue}
, m_hovered {false}
{
updatePieceColors();
@ -134,16 +131,6 @@ void PiecesBar::clear()
update();
}
void PiecesBar::setColors(const QColor &background, const QColor &border, const QColor &complete)
{
m_bgColor = background;
m_borderColor = border;
m_pieceColor = complete;
updatePieceColors();
requestImageUpdate();
}
bool PiecesBar::event(QEvent *e)
{
if (e->type() == QEvent::ToolTip) {
@ -181,7 +168,7 @@ void PiecesBar::paintEvent(QPaintEvent *)
QPainter painter(this);
QRect imageRect(borderWidth, borderWidth, width() - 2 * borderWidth, height() - 2 * borderWidth);
if (m_image.isNull()) {
painter.setBrush(Qt::white);
painter.setBrush(backgroundColor());
painter.drawRect(imageRect);
}
else {
@ -199,7 +186,7 @@ void PiecesBar::paintEvent(QPaintEvent *)
QPainterPath border;
border.addRect(0, 0, width(), height());
painter.setPen(m_borderColor);
painter.setPen(borderColor());
painter.drawPath(border);
}
@ -211,17 +198,22 @@ void PiecesBar::requestImageUpdate()
QColor PiecesBar::backgroundColor() const
{
return m_bgColor;
return palette().color(QPalette::Base);
}
QColor PiecesBar::borderColor() const
{
return m_borderColor;
return palette().color(QPalette::Dark);
}
QColor PiecesBar::pieceColor() const
{
return m_pieceColor;
return palette().color(QPalette::Highlight);
}
QColor PiecesBar::colorBoxBorderColor() const
{
return palette().color(QPalette::ToolTipText);
}
const QVector<QRgb> &PiecesBar::pieceColors() const
@ -326,6 +318,6 @@ void PiecesBar::updatePieceColors()
m_pieceColors = QVector<QRgb>(256);
for (int i = 0; i < 256; ++i) {
float ratio = (i / 255.0);
m_pieceColors[i] = mixTwoColors(backgroundColor().rgb(), m_pieceColor.rgb(), ratio);
m_pieceColors[i] = mixTwoColors(backgroundColor().rgb(), pieceColor().rgb(), ratio);
}
}

View file

@ -51,7 +51,6 @@ public:
explicit PiecesBar(QWidget *parent = nullptr);
void setTorrent(const BitTorrent::TorrentHandle *torrent);
void setColors(const QColor &background, const QColor &border, const QColor &complete);
virtual void clear();
@ -70,6 +69,7 @@ protected:
QColor backgroundColor() const;
QColor borderColor() const;
QColor pieceColor() const;
QColor colorBoxBorderColor() const;
const QVector<QRgb> &pieceColors() const;
// mix two colors by light model, ratio <0, 1>
@ -90,13 +90,6 @@ private:
const BitTorrent::TorrentHandle *m_torrent;
QImage m_image;
// I used values, because it should be possible to change colors at run time
// border color
QColor m_borderColor;
// background color
QColor m_bgColor;
// complete piece color
QColor m_pieceColor;
// buffered 256 levels gradient from bg_color to piece_color
QVector<QRgb> m_pieceColors;
bool m_hovered;