qBittorrent
piecesbar.h
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2016 Eugene Shalygin
4  * Copyright (C) 2006 Christophe Dumez
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * In addition, as a special exception, the copyright holders give permission to
21  * link this program with the OpenSSL project's "OpenSSL" library (or with
22  * modified versions of it that use the same license as the "OpenSSL" library),
23  * and distribute the linked executables. You must obey the GNU General Public
24  * License in all respects for all of the code used other than "OpenSSL". If you
25  * modify file(s), you may extend this exception to your version of the file(s),
26  * but you are not obligated to do so. If you do not wish to do so, delete this
27  * exception statement from your version.
28  */
29 
30 #pragma once
31 
32 #include <QColor>
33 #include <QImage>
34 #include <QWidget>
35 
36 class QHelpEvent;
37 
38 namespace BitTorrent
39 {
40  class Torrent;
41 }
42 
43 class PiecesBar : public QWidget
44 {
45  using base = QWidget;
46  Q_OBJECT
47  Q_DISABLE_COPY_MOVE(PiecesBar)
48 
49 public:
50  explicit PiecesBar(QWidget *parent = nullptr);
51 
52  void setTorrent(const BitTorrent::Torrent *torrent);
53 
54  virtual void clear();
55 
56  // QObject interface
57  virtual bool event(QEvent *e) override;
58 
59 protected:
60  // QWidget interface
61 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
62  void enterEvent(QEnterEvent *e) override;
63 #else
64  void enterEvent(QEvent *e) override;
65 #endif
66  void leaveEvent(QEvent *e) override;
67  void mouseMoveEvent(QMouseEvent *e) override;
68 
69  void paintEvent(QPaintEvent *e) override;
70  void requestImageUpdate();
71 
72  QColor backgroundColor() const;
73  QColor borderColor() const;
74  QColor pieceColor() const;
75  QColor colorBoxBorderColor() const;
76  const QVector<QRgb> &pieceColors() const;
77 
78  // mix two colors by light model, ratio <0, 1>
79  static QRgb mixTwoColors(QRgb rgb1, QRgb rgb2, float ratio);
80 
81  static constexpr int borderWidth = 1;
82 
83 private:
84  void showToolTip(const QHelpEvent*);
85  void highlightFile(int imagePos);
86 
87  virtual QString simpleToolTipText() const = 0;
88 
89  // draw new image to replace the actual image
90  // returns true if image was successfully updated
91  virtual bool updateImage(QImage &image) = 0;
92  void updatePieceColors();
93 
94  const BitTorrent::Torrent *m_torrent = nullptr;
95  QImage m_image;
96  // buffered 256 levels gradient from bg_color to piece_color
97  QVector<QRgb> m_pieceColors;
98  bool m_hovered = false;
99  QRect m_highlightedRegion; // part of the bar can be highlighted; this rectangle is in the same frame as m_image
100 };
static QRgb mixTwoColors(QRgb rgb1, QRgb rgb2, float ratio)
Definition: piecesbar.cpp:231
virtual bool event(QEvent *e) override
Definition: piecesbar.cpp:133
QColor colorBoxBorderColor() const
Definition: piecesbar.cpp:221
QColor pieceColor() const
Definition: piecesbar.cpp:216
QColor backgroundColor() const
Definition: piecesbar.cpp:206
QWidget base
Definition: piecesbar.h:45
void requestImageUpdate()
Definition: piecesbar.cpp:200
void mouseMoveEvent(QMouseEvent *e) override
Definition: piecesbar.cpp:162
const BitTorrent::Torrent * m_torrent
Definition: piecesbar.h:94
virtual bool updateImage(QImage &image)=0
QImage m_image
Definition: piecesbar.h:95
void updatePieceColors()
Definition: piecesbar.cpp:334
PiecesBar(QWidget *parent=nullptr)
Definition: piecesbar.cpp:113
void enterEvent(QEnterEvent *e) override
Definition: piecesbar.cpp:145
void highlightFile(int imagePos)
Definition: piecesbar.cpp:305
QVector< QRgb > m_pieceColors
Definition: piecesbar.h:97
static constexpr int borderWidth
Definition: piecesbar.h:81
void showToolTip(const QHelpEvent *)
Definition: piecesbar.cpp:249
void paintEvent(QPaintEvent *e) override
Definition: piecesbar.cpp:170
void setTorrent(const BitTorrent::Torrent *torrent)
Definition: piecesbar.cpp:120
void leaveEvent(QEvent *e) override
Definition: piecesbar.cpp:154
virtual void clear()
Definition: piecesbar.cpp:127
virtual QString simpleToolTipText() const =0
QRect m_highlightedRegion
Definition: piecesbar.h:99
const QVector< QRgb > & pieceColors() const
Definition: piecesbar.cpp:226
QColor borderColor() const
Definition: piecesbar.cpp:211
bool m_hovered
Definition: piecesbar.h:98