hydrogen 1.2.6
PixmapWidget.cpp
Go to the documentation of this file.
1/*
2 * Hydrogen
3 * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net]
4 * Copyright(c) 2008-2025 The hydrogen development team [hydrogen-devel@lists.sourceforge.net]
5 *
6 * http://www.hydrogen-music.org
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY, without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see https://www.gnu.org/licenses
20 *
21 */
22
23#include "PixmapWidget.h"
24#include "../Skin.h"
25#include <QPainter>
26
27#include <core/Object.h>
28
29PixmapWidget::PixmapWidget( QWidget *pParent )
30 : QWidget( pParent )
31 , m_sPixmapPath( "" )
32 , __expand_horiz(false)
33{
34 // draw the background: slower but useful with transparent images!
35 //setAttribute(Qt::WA_OpaquePaintEvent);
36 __color = QColor(200, 0, 0);
37}
38
39
40
44
45
46
47
48void PixmapWidget::setColor(const QColor& color)
49{
50 if (__color == color) {
51 return;
52 }
53 __color = color;
54 update();
55}
56
57
58
59void PixmapWidget::setPixmap( QString sPixmapPath, bool expand_horiz )
60{
61 if ( m_sPixmapPath == sPixmapPath ) {
62 return;
63 }
64 m_sPixmapPath = sPixmapPath;
65 __expand_horiz = expand_horiz;
66
67 bool ok = m_pixmap.load( Skin::getImagePath() + sPixmapPath );
68 if ( !ok ) {
69 _INFOLOG( QString( "Error loading: %1%2").arg( Skin::getImagePath() ).arg( sPixmapPath ) );
70 }
71
72 resize( m_pixmap.width(), m_pixmap.height() );
73 update();
74}
75
76
77
78void PixmapWidget::paintEvent( QPaintEvent* ev)
79{
80 QWidget::paintEvent(ev);
81
82 QPainter painter(this);
83 if ( m_pixmap.isNull() ) {
84 painter.fillRect( ev->rect(), __color );
85 }
86 else {
87 if (__expand_horiz) {
88 static int w = 10;
89 static int h = m_pixmap.height();
90
91 // central section, scaled
92 painter.drawPixmap( QRect(w, 0, width() - w * 2, h), m_pixmap, QRect(10, 0, w, h) );
93
94 // left side
95 painter.drawPixmap( QRect(0, 0, w, h), m_pixmap, QRect(0, 0, w, h) );
96
97 // right side
98 painter.drawPixmap( QRect(width() - w, 0, w, h), m_pixmap, QRect(m_pixmap.width() - w, 0, w, h) );
99 }
100 else {
101 painter.drawPixmap( ev->rect(), m_pixmap, ev->rect() );
102 }
103 }
104}
105
106
#define _INFOLOG(x)
Definition Object.h:246
QPixmap m_pixmap
QString m_sPixmapPath
bool __expand_horiz
void setColor(const QColor &color)
virtual void paintEvent(QPaintEvent *ev) override
void setPixmap(QString sPixmapPath, bool expand_horiz=false)
PixmapWidget(QWidget *pParent)
static QString getImagePath()
Definition Skin.h:36