Changeset View
Changeset View
Standalone View
Standalone View
src/context/applets/analyzer/plugin/BlockRenderer.h
Show All 20 Lines | |||||
21 | * | 21 | * | ||
22 | */ | 22 | */ | ||
23 | 23 | | |||
24 | #ifndef BLOCKRENDERER_H | 24 | #ifndef BLOCKRENDERER_H | ||
25 | #define BLOCKRENDERER_H | 25 | #define BLOCKRENDERER_H | ||
26 | 26 | | |||
27 | #include "BlockAnalyzer.h" | 27 | #include "BlockAnalyzer.h" | ||
28 | #include "BlockWorker.h" | 28 | #include "BlockWorker.h" | ||
29 | #include "core/support/Debug.h" | | |||
30 | 29 | | |||
31 | #include <QOpenGLFramebufferObject> | 30 | #include <QOpenGLFramebufferObject> | ||
32 | #include <QOpenGLPaintDevice> | 31 | #include <QOpenGLPaintDevice> | ||
33 | #include <QPainter> | 32 | #include <QPainter> | ||
33 | #include <QPointer> | ||||
34 | #include <QQuickFramebufferObject> | 34 | #include <QQuickFramebufferObject> | ||
35 | #include <QSGTexture> | 35 | | ||
36 | #include <QSharedPointer> | | |||
37 | 36 | | |||
38 | class BlockRenderer : public QQuickFramebufferObject::Renderer | 37 | class BlockRenderer : public QQuickFramebufferObject::Renderer | ||
39 | { | 38 | { | ||
40 | public: | 39 | public: | ||
41 | static const int BLOCK_HEIGHT = BlockAnalyzer::BLOCK_HEIGHT; | 40 | static const int BLOCK_HEIGHT = BlockAnalyzer::BLOCK_HEIGHT; | ||
42 | 41 | | |||
43 | BlockRenderer() {} | 42 | BlockRenderer() : m_worker( nullptr ) {} | ||
44 | 43 | | |||
45 | protected: | 44 | protected: | ||
46 | QOpenGLFramebufferObject* createFramebufferObject(const QSize &size) override | 45 | QOpenGLFramebufferObject* createFramebufferObject(const QSize &size) override | ||
47 | { | 46 | { | ||
48 | QOpenGLFramebufferObject* fo = new QOpenGLFramebufferObject(size); | 47 | QOpenGLFramebufferObject* fo = new QOpenGLFramebufferObject(size); | ||
49 | fo->setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil); | 48 | fo->setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil); | ||
50 | return fo; | 49 | return fo; | ||
51 | } | 50 | } | ||
52 | 51 | | |||
53 | void render() override | 52 | void render() override | ||
54 | { | 53 | { | ||
54 | // Synchronize worker data | ||||
55 | if (!m_worker) | ||||
56 | return; | ||||
57 | | ||||
58 | m_worker->m_mutex.lock(); | ||||
59 | const QVector<double> store = m_worker->m_store; | ||||
60 | const QVector<QList<BlockWorker::Fadebar> > fadebars = m_worker->m_fadebars; | ||||
61 | m_worker->m_mutex.unlock(); | ||||
62 | | ||||
55 | QOpenGLPaintDevice d; | 63 | QOpenGLPaintDevice d; | ||
56 | d.setSize(framebufferObject()->size()); | 64 | d.setSize(framebufferObject()->size()); | ||
57 | QPainter p(&d); | 65 | QPainter p(&d); | ||
58 | 66 | | |||
59 | // Draw the background | 67 | // Draw the background | ||
60 | p.drawPixmap(QRect(QPoint(0, 0), framebufferObject()->size()), m_backgroundPixmap); | 68 | p.drawPixmap(QRect(QPoint(0, 0), framebufferObject()->size()), m_backgroundPixmap); | ||
61 | 69 | | |||
62 | for( uint x = 0; x < (uint)m_store.size(); ++x ) | 70 | for(uint x = 0; x < (uint)store.size(); ++x) | ||
63 | { | 71 | { | ||
64 | // Draw fade bars | 72 | // Draw fade bars | ||
65 | for( const auto &fadebar : m_fadebars.at(x) ) | 73 | for(const auto &fadebar : qAsConst(fadebars.at(x))) | ||
66 | { | 74 | { | ||
67 | if( fadebar.intensity > 0 ) | 75 | if(fadebar.intensity > 0) | ||
68 | { | 76 | { | ||
69 | const uint offset = fadebar.intensity; | 77 | const uint offset = fadebar.intensity; | ||
70 | const int fadeHeight = fadebar.y * (BLOCK_HEIGHT + 1); | 78 | const int fadeHeight = fadebar.y * (BLOCK_HEIGHT + 1); | ||
71 | if( fadeHeight > 0 ) | 79 | if(fadeHeight > 0) | ||
72 | p.drawPixmap(x * ( m_columnWidth + 1 ), 0, m_fadeBarsPixmaps.value(offset), 0, 0, m_columnWidth, fadeHeight); | 80 | p.drawPixmap(x * (m_columnWidth + 1), 0, m_fadeBarsPixmaps.value(offset), 0, 0, m_columnWidth, fadeHeight); | ||
73 | } | 81 | } | ||
74 | } | 82 | } | ||
75 | 83 | | |||
76 | // Draw bars | 84 | // Draw bars | ||
77 | const int height = m_store.at(x) * (BLOCK_HEIGHT + 1); | 85 | const int height = store.at(x) * (BLOCK_HEIGHT + 1); | ||
78 | if (height > 0) | 86 | if (height > 0) | ||
79 | p.drawPixmap(x * (m_columnWidth + 1), 0, m_barPixmap, 0, 0, m_columnWidth, height); | 87 | p.drawPixmap(x * (m_columnWidth + 1), 0, m_barPixmap, 0, 0, m_columnWidth, height); | ||
80 | 88 | | |||
81 | // Draw top bar | 89 | // Draw top bar | ||
82 | p.drawPixmap(x * (m_columnWidth + 1), height + BLOCK_HEIGHT - 1, m_topBarPixmap); | 90 | p.drawPixmap(x * (m_columnWidth + 1), height + BLOCK_HEIGHT - 1, m_topBarPixmap); | ||
83 | } | 91 | } | ||
84 | } | 92 | } | ||
85 | 93 | | |||
86 | void synchronize(QQuickFramebufferObject *item) override | 94 | void synchronize(QQuickFramebufferObject *item) override | ||
87 | { | 95 | { | ||
88 | auto analyzer = qobject_cast<BlockAnalyzer*>(item); | 96 | auto analyzer = qobject_cast<BlockAnalyzer*>(item); | ||
89 | if (!analyzer) | 97 | if (!analyzer) | ||
90 | return; | 98 | return; | ||
91 | 99 | | |||
92 | m_rows = analyzer->m_rows; | 100 | m_rows = analyzer->m_rows; | ||
93 | m_columnWidth = analyzer->m_columnWidth; | 101 | m_columnWidth = analyzer->m_columnWidth; | ||
94 | 102 | | |||
95 | auto worker = qobject_cast<const BlockWorker*>(analyzer->worker()); | 103 | if (!m_worker) | ||
96 | if (worker) | 104 | m_worker = qobject_cast<const BlockWorker*>(analyzer->worker()); | ||
97 | { | | |||
98 | worker->m_mutex.lock(); | | |||
99 | m_store = worker->m_store; | | |||
100 | m_fadebars = worker->m_fadebars; | | |||
101 | worker->m_mutex.unlock(); | | |||
102 | } | | |||
103 | 105 | | |||
104 | if (analyzer->m_pixmapsChanged) | 106 | if (analyzer->m_pixmapsChanged) | ||
105 | { | 107 | { | ||
106 | m_barPixmap = analyzer->m_barPixmap; | 108 | m_barPixmap = analyzer->m_barPixmap; | ||
107 | m_topBarPixmap = analyzer->m_topBarPixmap; | 109 | m_topBarPixmap = analyzer->m_topBarPixmap; | ||
108 | m_backgroundPixmap = analyzer->m_backgroundPixmap; | 110 | m_backgroundPixmap = analyzer->m_backgroundPixmap; | ||
109 | m_fadeBarsPixmaps = analyzer->m_fadeBarsPixmaps; | 111 | m_fadeBarsPixmaps = analyzer->m_fadeBarsPixmaps; | ||
110 | 112 | | |||
111 | analyzer->m_pixmapsChanged = false; | 113 | analyzer->m_pixmapsChanged = false; | ||
112 | } | 114 | } | ||
113 | } | 115 | } | ||
114 | 116 | | |||
115 | private: | 117 | private: | ||
116 | QVector<double> m_store; | 118 | QPointer<const BlockWorker> m_worker; | ||
117 | QVector<QList<BlockWorker::Fadebar> > m_fadebars; | 119 | | ||
118 | int m_rows; | 120 | int m_rows; | ||
119 | int m_columnWidth; | 121 | int m_columnWidth; | ||
120 | 122 | | |||
121 | QPixmap m_barPixmap; | 123 | QPixmap m_barPixmap; | ||
122 | QPixmap m_topBarPixmap; | 124 | QPixmap m_topBarPixmap; | ||
123 | QPixmap m_backgroundPixmap; | 125 | QPixmap m_backgroundPixmap; | ||
124 | QVector<QPixmap> m_fadeBarsPixmaps; | 126 | QVector<QPixmap> m_fadeBarsPixmaps; | ||
125 | }; | 127 | }; | ||
126 | 128 | | |||
127 | #endif //BLOCKRENDERER_H | 129 | #endif //BLOCKRENDERER_H |