summaryrefslogtreecommitdiffstats
path: root/src/widget/wdisplay.cpp
blob: 63cc194060f1ef0bc193f86827b67742f8f3b7b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/***************************************************************************
                          wdisplay.cpp  -  description
                             -------------------
    begin                : Fri Jun 21 2002
    copyright            : (C) 2002 by Tue & Ken Haste Andersen
    email                : haste@diku.dk
***************************************************************************/

/***************************************************************************
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
***************************************************************************/

#include "wdisplay.h"
#include "wpixmapstore.h"

#include <QPainter>
#include <QPaintEvent>
#include <QtDebug>
#include <QPixmap>

WDisplay::WDisplay(QWidget * parent) : WWidget(parent) {
    m_pPixmaps = 0;
    setPositions(0);
}

WDisplay::~WDisplay() {
    resetPositions();
}

void WDisplay::setup(QDomNode node) {
    // Number of states
    setPositions(selectNodeInt(node, "NumberStates"));

    // Load knob  pixmaps
    QString path = selectNodeQString(node, "Path");
    for (int i=0; i<m_iNoPos; ++i) {
        setPixmap(i, getPath(path.arg(i)));
    }
}

void WDisplay::setPositions(int iNoPos) {
    m_iNoPos = iNoPos;
    m_iPos = 0;

    resetPositions();

    if (m_iNoPos>0) {
        m_pPixmaps = new QPixmap*[m_iNoPos];
        for (int i=0; i<m_iNoPos; i++)
            m_pPixmaps[i] = 0;
    }
}

void WDisplay::resetPositions() {
    if (m_pPixmaps) {
        for (int i=0; i<m_iNoPos; i++)
            if (m_pPixmaps[i])
                WPixmapStore::deletePixmap(m_pPixmaps[i]);

        //WPixmapStore::deletePixmap(m_pPixmaps);
        m_pPixmaps = 0;
    }
}

void WDisplay::setPixmap(int iPos, const QString &filename) {
    m_pPixmaps[iPos] = WPixmapStore::getPixmap(filename);
    if (!m_pPixmaps[iPos])
        qDebug() << "WDisplay: Error loading pixmap" << filename;
    else
        setFixedSize(m_pPixmaps[iPos]->size());
}

void WDisplay::paintEvent(QPaintEvent *) {
    if (m_pPixmaps) {
        int idx = (int)(m_fValue*(float)(m_iNoPos)/128.);
        // Range check
        if (idx>(m_iNoPos-1))
            idx = m_iNoPos-1;
        else if (idx<0)
            idx = 0;
        if (m_pPixmaps[idx]) {
            QPainter p(this);
            p.drawPixmap(0, 0, *m_pPixmaps[idx]);
        }
    }
}