Debian Codegraph @

src/context/applets/analyzer/plugin/BlockAnalyzer.cpp

raw
1 /****************************************************************************************
2 * Copyright (c) 2003-2005 Max Howell <[email protected]> *
3 * Copyright (c) 2005-2013 Mark Kretschmann <[email protected]> *
4 * *
5 * This program is free software; you can redistribute it and/or modify it under *
6 * the terms of the GNU General Public License as published by the Free Software *
7 * Foundation; either version 2 of the License, or (at your option) any later *
8 * version. *
9 * *
10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY *
11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
12 * PARTICULAR PURPOSE. See the GNU General Pulic License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License along with *
15 * this program. If not, see <http://www.gnu.org/licenses/>. *
16 ****************************************************************************************/
17
18 #include "BlockAnalyzer.h"
19
20 #include "AnalyzerWorker.h"
21 #include "BlockRenderer.h"
22 #include "BlockWorker.h"
23 #include "PaletteHandler.h"
24 #include "core/support/Debug.h"
25
26 #include <cmath>
27
28 #include <QPainter>
29 #include <QQuickWindow>
30 #include <QScreen>
31
32
33 BlockAnalyzer::BlockAnalyzer( QQuickItem *parent )
34 : Analyzer::Base( parent )
35 , m_columns( 0 ) //int
36 , m_rows( 0 ) //int
37 , m_fadeBarsPixmaps( FADE_SIZE ) //vector<QPixmap>
38 {
39 setTextureFollowsItemSize( true );
40 setObjectName( QStringLiteral("Blocky") );
41
42 m_columnWidth = config().readEntry( "columnWidth", 4 );
43 m_fallSpeed = (FallSpeed) config().readEntry( "fallSpeed", (int) Medium );
44 m_showFadebars = config().readEntry( "showFadebars", true );
45
46 paletteChange( The::paletteHandler()->palette() );
47 connect( The::paletteHandler(), &PaletteHandler::newPalette, this, &BlockAnalyzer::paletteChange );
48 connect( this, &QQuickItem::windowChanged, this, &BlockAnalyzer::newWindow );
49 }
50
51 QQuickFramebufferObject::Renderer*
52 BlockAnalyzer::createRenderer() const
53 {
54 return new BlockRenderer;
55 }
56
57 Analyzer::Worker * BlockAnalyzer::createWorker() const
58 {
59 auto worker = new BlockWorker( m_rows, m_columns, m_step, m_showFadebars );
60 if( window() )
61 worker->setRefreshRate( window()->screen()->refreshRate() );
62 connect( worker, &BlockWorker::finished, this, &QQuickFramebufferObject::update, Qt::QueuedConnection );
63 connect( this, &BlockAnalyzer::stepChanged, worker, &BlockWorker::setStep, Qt::QueuedConnection );
64 connect( this, &BlockAnalyzer::rowsChanged, worker, &BlockWorker::setRows, Qt::QueuedConnection );
65 connect( this, &BlockAnalyzer::columnsChanged, worker, &BlockWorker::setColumns, Qt::QueuedConnection );
66 connect( this, &BlockAnalyzer::refreshRateChanged, worker, &BlockWorker::setRefreshRate, Qt::QueuedConnection );
67 connect( this, &BlockAnalyzer::showFadebarsChanged, worker, &BlockWorker::setShowFadebars, Qt::QueuedConnection );
68 return worker;
69 }
70
71
72 void
73 BlockAnalyzer::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
74 {
75 QQuickFramebufferObject::geometryChange( newGeometry, oldGeometry );
76 if( ( oldGeometry.height() == 0 || newGeometry.height() == 0 ) && oldGeometry.height() != newGeometry.height() )
77 {
78 drawNeedChanged( newGeometry.height() != 0 ); // Stop data processing if applet is minimized
79 }
80
81 if( !newGeometry.isValid() )
82 return;
83
84 const int oldRows = m_rows;
85
86 // Rounded up so that the last column/line is covered if partially visible
87 m_columns = std::lround( std::ceil( (double)newGeometry.width() / ( m_columnWidth + 1 ) ) );
88 Q_EMIT columnsChanged( m_columns );
89 m_rows = std::ceil( (double)newGeometry.height() / ( BLOCK_HEIGHT + 1 ) );
90 Q_EMIT rowsChanged( m_rows );
91
92 setScopeSize( m_columns );
93
94 if( m_rows != oldRows )
95 {
96 m_barPixmap = QPixmap( m_columnWidth, m_rows * ( BLOCK_HEIGHT + 1 ) );
97
98 determineStep();
99 paletteChange( The::paletteHandler()->palette() );
100 }
101 else
102 drawBackground( The::paletteHandler()->palette() );
103 }
104
105 void
106 BlockAnalyzer::determineStep()
107 {
108 // falltime is dependent on rowcount due to our digital resolution (ie we have boxes/blocks of pixels)
109
110 const qreal fallTime = 1.0 / pow( 1.5, m_fallSpeed ); // time to fall from top to bottom
111 m_step = qreal( m_rows ) / fallTime; // the amount of rows to fall per second
112 Q_EMIT stepChanged( m_step );
113 }
114
115 void
116 BlockAnalyzer::paletteChange( const QPalette& palette ) //virtual
117 {
118 const QColor bg = palette.color( QPalette::Active, QPalette::Base );
119 const QColor abg = palette.color( QPalette::Active, QPalette::AlternateBase );
120 const QColor highlight = palette.color( QPalette::Active, QPalette::Highlight );
121
122 m_topBarPixmap = QPixmap( m_columnWidth, BLOCK_HEIGHT );
123 m_topBarPixmap.fill( highlight );
124
125 m_barPixmap.fill( QColor( ( highlight.red() + bg.red() ) / 2, ( highlight.green() + bg.green() ) / 2, ( highlight.blue() + bg.blue() ) / 2 ) );
126
127 int h, s, v;
128 palette.color( QPalette::Active, QPalette::Dark ).getHsv( &h, &s, &v );
129 const QColor fade = QColor::fromHsv( h + 30, s, v );
130
131 const double dr = fade.red() - abg.red();
132 const double dg = fade.green() - abg.green();
133 const double db = fade.blue() - abg.blue();
134 const int r = abg.red(), g = abg.green(), b = abg.blue();
135
136 if( m_rows == 0 )
137 return; // Nothing to draw yet, avoid some QPainter warnings from empty surfaces
138
139 // Precalculate all fade-bar pixmaps
140 for( int y = 0; y < FADE_SIZE; ++y )
141 {
142 m_fadeBarsPixmaps[y] = QPixmap( m_columnWidth, m_rows * ( BLOCK_HEIGHT + 1 ) );
143
144 m_fadeBarsPixmaps[y].fill( palette.color( QPalette::Active, QPalette::Base ) );
145 const double Y = 1.0 - ( log10( ( FADE_SIZE ) - y ) / log10( ( FADE_SIZE ) ) );
146 QPainter f( &m_fadeBarsPixmaps[y] );
147 for( int z = 0; z < m_rows; ++z )
148 f.fillRect( 0,
149 z * ( BLOCK_HEIGHT + 1 ),
150 m_columnWidth, BLOCK_HEIGHT,
151 QColor( r + int( dr * Y ), g + int( dg * Y ), b + int( db * Y ) ) );
152 }
153
154 m_pixmapsChanged = true;
155 drawBackground( palette );
156 }
157
158 void
159 BlockAnalyzer::drawBackground( const QPalette &palette )
160 {
161 const QColor bg = palette.color( QPalette::Active, QPalette::Base );
162 const QColor abg = palette.color( QPalette::Active, QPalette::AlternateBase );
163
164 // background gets stretched if it is too big
165 m_backgroundPixmap = QPixmap( width(), height() );
166 m_backgroundPixmap.fill( bg );
167
168 QPainter p( &m_backgroundPixmap );
169 p.scale( 1/QGuiApplication::primaryScreen()->devicePixelRatio(), 1/QGuiApplication::primaryScreen()->devicePixelRatio() );
170 for( int x = 0; x < m_columns; ++x )
171 for( int y = 0; y < m_rows; ++y )
172 p.fillRect( x * ( m_columnWidth + 1 ), y * ( BLOCK_HEIGHT + 1 ), m_columnWidth, BLOCK_HEIGHT, abg );
173
174 m_pixmapsChanged = true;
175
176 update();
177 }
178
179 void
180 BlockAnalyzer::setFallSpeed( FallSpeed fallSpeed )
181 {
182 DEBUG_BLOCK
183
184 debug() << "Fall speed set to:" << fallSpeed;
185
186 if( m_fallSpeed == fallSpeed )
187 return;
188
189 m_fallSpeed = fallSpeed;
190 config().writeEntry( "fallSpeed", (int) m_fallSpeed );
191 Q_EMIT fallSpeedChanged();
192
193 determineStep();
194 }
195
196 void
197 BlockAnalyzer::setColumnWidth( int columnWidth )
198 {
199 DEBUG_BLOCK
200
201 debug() << "Column width set to:" << columnWidth;
202
203 if( columnWidth < 1 )
204 {
205 warning() << "Column width can not be smaller than one!";
206 columnWidth = 1;
207 }
208
209 if( m_columnWidth == columnWidth )
210 return;
211
212 m_columnWidth = columnWidth;
213 config().writeEntry( "columnWidth", m_columnWidth );
214 Q_EMIT columnWidthChanged();
215
216 m_columns = std::lround( std::ceil( (double)width() / ( m_columnWidth + 1 ) ) );
217 Q_EMIT columnsChanged( m_columns );
218 setScopeSize( m_columns );
219 m_barPixmap = QPixmap( m_columnWidth, m_rows * ( BLOCK_HEIGHT + 1 ) );
220 paletteChange( The::paletteHandler()->palette() );
221 }
222
223 void
224 BlockAnalyzer::setShowFadebars( bool showFadebars )
225 {
226 DEBUG_BLOCK
227
228 debug() << "Show fadebars:" << showFadebars;
229
230 if( m_showFadebars == showFadebars )
231 return;
232
233 m_showFadebars = showFadebars;
234 Q_EMIT showFadebarsChanged( m_showFadebars );
235 }
236
237 void
238 BlockAnalyzer::newWindow( QQuickWindow* window )
239 {
240 if( window )
241 Q_EMIT refreshRateChanged( window->screen()->refreshRate() );
242 }