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