| 1 |
/* |
| 2 |
* Copyright (c) 2003-2005 Max Howell <[email protected]> |
| 3 |
* Copyright (c) 2005-2013 Mark Kretschmann <[email protected]> |
| 4 |
* Copyright 2017 Malte Veerman <[email protected]> |
| 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 as |
| 8 |
* published by the Free Software Foundation; either version 2 of |
| 9 |
* the License or (at your option) version 3 or any later version |
| 10 |
* accepted by the membership of KDE e.V. (or its successor approved |
| 11 |
* by the membership of KDE e.V.), which shall act as a proxy |
| 12 |
* defined in Section 14 of version 3 of the license. |
| 13 |
* |
| 14 |
* This program is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
* GNU General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU General Public License |
| 20 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 |
* |
| 22 |
*/ |
| 23 |
|
| 24 |
#ifndef BLOCKRENDERER_H |
| 25 |
#define BLOCKRENDERER_H |
| 26 |
|
| 27 |
#include "BlockAnalyzer.h" |
| 28 |
#include "BlockWorker.h" |
| 29 |
|
| 30 |
#include <QGuiApplication> |
| 31 |
#include <QOpenGLFramebufferObject> |
| 32 |
#include <QOpenGLPaintDevice> |
| 33 |
#include <QPainter> |
| 34 |
#include <QPointer> |
| 35 |
#include <QQuickFramebufferObject> |
| 36 |
#include <QScreen> |
| 37 |
|
| 38 |
|
| 39 |
class BlockRenderer : public QQuickFramebufferObject::Renderer |
| 40 |
{ |
| 41 |
public: |
| 42 |
static const int BLOCK_HEIGHT = BlockAnalyzer::BLOCK_HEIGHT; |
| 43 |
|
| 44 |
BlockRenderer() : m_worker( nullptr ) {} |
| 45 |
|
| 46 |
protected: |
| 47 |
QOpenGLFramebufferObject* createFramebufferObject(const QSize &size) override |
| 48 |
{ |
| 49 |
QOpenGLFramebufferObject* fo = new QOpenGLFramebufferObject(size); |
| 50 |
fo->setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil); |
| 51 |
return fo; |
| 52 |
} |
| 53 |
|
| 54 |
void render() override |
| 55 |
{ |
| 56 |
// Synchronize worker data |
| 57 |
if (!m_worker) |
| 58 |
return; |
| 59 |
|
| 60 |
m_worker->m_mutex.lock(); |
| 61 |
const QVector<double> store = m_worker->m_store; |
| 62 |
const QVector<QList<BlockWorker::Fadebar> > fadebars = m_worker->m_fadebars; |
| 63 |
m_worker->m_mutex.unlock(); |
| 64 |
|
| 65 |
QOpenGLPaintDevice d; |
| 66 |
d.setSize(framebufferObject()->size()); |
| 67 |
QPainter p(&d); |
| 68 |
p.scale( QGuiApplication::primaryScreen()->devicePixelRatio(), QGuiApplication::primaryScreen()->devicePixelRatio() ); |
| 69 |
|
| 70 |
// Draw the background |
| 71 |
p.drawPixmap(QRect(QPoint(0, 0), framebufferObject()->size()), m_backgroundPixmap); |
| 72 |
|
| 73 |
for(uint x = 0; x < (uint)store.size(); ++x) |
| 74 |
{ |
| 75 |
// Draw fade bars |
| 76 |
for(const auto &fadebar : std::as_const(fadebars.at(x))) |
| 77 |
{ |
| 78 |
if(fadebar.intensity > 0) |
| 79 |
{ |
| 80 |
const uint offset = fadebar.intensity; |
| 81 |
const int fadeHeight = fadebar.y * (BLOCK_HEIGHT + 1); |
| 82 |
if(fadeHeight > 0) |
| 83 |
p.drawPixmap(x * (m_columnWidth + 1), 0, m_fadeBarsPixmaps.value(offset), 0, 0, m_columnWidth, fadeHeight); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
// Draw bars |
| 88 |
const int height = store.at(x) * (BLOCK_HEIGHT + 1); |
| 89 |
if (height > 0) |
| 90 |
p.drawPixmap(x * (m_columnWidth + 1), 0, m_barPixmap, 0, 0, m_columnWidth, height); |
| 91 |
|
| 92 |
// Draw top bar |
| 93 |
p.drawPixmap(x * (m_columnWidth + 1), height + BLOCK_HEIGHT - 1, m_topBarPixmap); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
void synchronize(QQuickFramebufferObject *item) override |
| 98 |
{ |
| 99 |
auto analyzer = qobject_cast<BlockAnalyzer*>(item); |
| 100 |
if (!analyzer) |
| 101 |
return; |
| 102 |
|
| 103 |
m_rows = analyzer->m_rows; |
| 104 |
m_columnWidth = analyzer->m_columnWidth; |
| 105 |
|
| 106 |
if (!m_worker) |
| 107 |
m_worker = qobject_cast<const BlockWorker*>(analyzer->worker()); |
| 108 |
|
| 109 |
if (analyzer->m_pixmapsChanged) |
| 110 |
{ |
| 111 |
m_barPixmap = analyzer->m_barPixmap; |
| 112 |
m_topBarPixmap = analyzer->m_topBarPixmap; |
| 113 |
m_backgroundPixmap = analyzer->m_backgroundPixmap; |
| 114 |
m_fadeBarsPixmaps = analyzer->m_fadeBarsPixmaps; |
| 115 |
|
| 116 |
analyzer->m_pixmapsChanged = false; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
private: |
| 121 |
QPointer<const BlockWorker> m_worker; |
| 122 |
|
| 123 |
int m_rows; |
| 124 |
int m_columnWidth; |
| 125 |
|
| 126 |
QPixmap m_barPixmap; |
| 127 |
QPixmap m_topBarPixmap; |
| 128 |
QPixmap m_backgroundPixmap; |
| 129 |
QVector<QPixmap> m_fadeBarsPixmaps; |
| 130 |
}; |
| 131 |
|
| 132 |
#endif //BLOCKRENDERER_H |