blockview.h raw
1 // Copyright (c) 2024 Luke Dashjr
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5 #ifndef LIMENKA_QT_BLOCKVIEW_H
6 #define LIMENKA_QT_BLOCKVIEW_H
7
8 #include <consensus/amount.h>
9 #include <primitives/transaction.h>
10 #include <sync.h>
11 #include <threadsafety.h>
12 #include <util/transaction_identifier.h>
13
14 #include <atomic>
15 #include <map>
16 #include <memory>
17 #include <vector>
18
19 #include <QColor>
20 #include <QDialog>
21 #include <QGraphicsView>
22 #include <QPointF>
23 #include <QTimer>
24
25 QT_BEGIN_NAMESPACE
26 class QComboBox;
27 class QEvent;
28 class QGraphicsItem;
29 class QGraphicsScene;
30 class QLabel;
31 class QWidget;
32 QT_END_NAMESPACE
33
34 class CBlock;
35 namespace node { struct CBlockTemplate; }
36 class ChainstateManager;
37 class ClientModel;
38 class CValidationInterface;
39 class GuiBlockView;
40 class NetworkStyle;
41 class PlatformStyle;
42
43 class ScalingGraphicsView : public QGraphicsView
44 {
45 Q_OBJECT
46
47 public:
48 using QGraphicsView::QGraphicsView;
49
50 void mouseMoveEvent(QMouseEvent *event) override;
51 void resizeEvent(QResizeEvent *event) override;
52 bool viewportEvent(QEvent *event) override;
53
54 GuiBlockView *m_bv;
55 };
56
57 class BlockViewValidationInterface;
58
59 class GuiBlockView : public QDialog
60 {
61 Q_OBJECT
62
63 public:
64 RecursiveMutex m_mutex;
65
66 private:
67 struct SceneElement {
68 QGraphicsItem* gi;
69 QPointF target_loc;
70 };
71 struct Bubble {
72 CTransactionRef tx;
73 QPointF pos;
74 double radius;
75 SceneElement *el;
76 };
77 struct BubbleGraph {
78 std::vector<Bubble> bubbles;
79 qreal min_x{0};
80 qreal max_x{0};
81 qreal min_y{0};
82 bool instant;
83 size_t txs_count;
84 size_t txs_size{0};
85 };
86 std::map<Wtxid, SceneElement> m_elements GUARDED_BY(m_mutex);
87 std::unique_ptr<BubbleGraph> m_bubblegraph GUARDED_BY(m_mutex);
88 QGraphicsScene *m_scene;
89 QTimer m_timer;
90 unsigned int m_frame_div;
91
92 QComboBox *m_block_chooser;
93 QLabel *m_lbl_tx_count;
94 QLabel *m_lbl_tx_fees;
95 QColor m_bubble_color;
96
97 BlockViewValidationInterface *m_validation_interface;
98
99 static bool any_overlap(const Bubble& proposed, const std::vector<Bubble>& others);
100 void updateThemeColors();
101
102 protected:
103 void changeEvent(QEvent* e) override;
104 void updateElements(bool instant) EXCLUSIVE_LOCKS_REQUIRED(m_mutex);
105 void updateBlockFees(CAmount block_fees);
106
107 protected Q_SLOTS:
108 void updateDisplayUnit();
109 void updateSceneInit();
110 void updateScene();
111
112 public:
113 ClientModel *m_client_model{nullptr};
114
115 bool m_block_changed GUARDED_BY(m_mutex);
116 CAmount m_block_fees GUARDED_BY(m_mutex) {-1};
117 std::shared_ptr<const node::CBlockTemplate> m_block_template GUARDED_BY(m_mutex);
118 std::shared_ptr<const CBlock> m_block GUARDED_BY(m_mutex);
119 std::atomic<bool> m_follow_tip;
120
121 GuiBlockView(const PlatformStyle *, const NetworkStyle *, QWidget * parent = nullptr);
122 ~GuiBlockView();
123
124 void setClientModel(ClientModel *model);
125 ChainstateManager* getChainstateManager() const;
126
127 void clear();
128 void setBlock(std::shared_ptr<const CBlock> block, CAmount block_subsidy);
129 void setBlock(std::shared_ptr<const node::CBlockTemplate> blocktemplate);
130
131 public Q_SLOTS:
132 void updateBestBlock(int height);
133 };
134
135 #endif // LIMENKA_QT_BLOCKVIEW_H
136