summaryrefslogtreecommitdiffstats
path: root/noatun/modules/winskin/guiSpectrumAnalyser.cpp
blob: d044739755af1c8e26877b495d4adddbcb3a177d (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
  winamp visualisation plugin.
  Copyright (C) 2001  Martin Vogt

  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.

  For more information look at the file COPYRIGHT in this package

 */


#include <klocale.h>
#include <tqcolor.h>
#include <tqpopupmenu.h>
#include <tqpainter.h>
#include <kconfig.h>

#include "waColor.h"
#include "waSkinModel.h"

#include "guiSpectrumAnalyser.h"
#include "vis/winskinvis.h"

#define __BANDS     75

GuiSpectrumAnalyser::GuiSpectrumAnalyser()
  : WaWidget(_WA_MAPPING_ANALYSER)
{
    connect(WaSkinModel::instance(), TQT_SIGNAL(skinChanged()), this, TQT_SLOT(pixmapChange()));

    contextMenu = new TQPopupMenu(this);
    visualizationMenu = new TQPopupMenu();
    analyserMenu = new TQPopupMenu();

    contextMenu->insertItem(i18n("Visualization Mode"), visualizationMenu);
    contextMenu->insertItem(i18n("Analyzer Mode"), analyserMenu);

    visualizationMenu->insertItem(i18n("Analyzer"), (int)MODE_ANALYSER);
    visualizationMenu->insertItem(i18n("Disabled"), (int)MODE_DISABLED);
    visualizationMenu->setCheckable(true);
    connect(visualizationMenu, TQT_SIGNAL(activated(int)), this, TQT_SLOT(setVisualizationMode(int)));

    analyserMenu->insertItem(i18n("Normal"), (int)MODE_NORMAL);
    analyserMenu->insertItem(i18n("Fire"), (int)MODE_FIRE);
    analyserMenu->insertItem(i18n("Vertical Lines"), (int)MODE_VERTICAL_LINES);
    analyserMenu->setCheckable(true);
    connect(analyserMenu, TQT_SIGNAL(activated(int)), this, TQT_SLOT(setAnalyserMode(int)));

    analyserCache = NULL;
    winSkinVis = NULL;

    TDEConfig *config = TDEGlobal::config();
    config->setGroup("Winskin");

    setVisualizationMode(config->readNumEntry("visualizationMode", MODE_ANALYSER));
    setAnalyserMode(config->readNumEntry("analyserMode", MODE_NORMAL));
}


GuiSpectrumAnalyser::~GuiSpectrumAnalyser()
{
    TDEConfig *config = TDEGlobal::config();
    config->setGroup("Winskin");

    config->writeEntry("visualizationMode", visualization_mode);
    config->writeEntry("analyserMode", analyser_mode);

    delete analyserCache;
}

void GuiSpectrumAnalyser::mousePressEvent ( TQMouseEvent *e )
{
    if (e->button() == Qt::LeftButton) {
        if (visualization_mode == MODE_DISABLED)
            setVisualizationMode(MODE_ANALYSER);
        else
            setVisualizationMode(MODE_DISABLED);
    }
    else if (e->button() == Qt::RightButton) {
        contextMenu->popup(mapToGlobal(TQPoint(e->x(), e->y())));
    }
}

void GuiSpectrumAnalyser::setAnalyserMode(int mode)
{
    analyser_mode = mode;

    analyserMenu->setItemChecked(MODE_NORMAL, (mode == MODE_NORMAL));
    analyserMenu->setItemChecked(MODE_FIRE, (mode == MODE_FIRE));
    analyserMenu->setItemChecked(MODE_VERTICAL_LINES, (mode == MODE_VERTICAL_LINES));

    delete analyserCache;
    analyserCache = NULL;
}

void GuiSpectrumAnalyser::pauseVisualization()
{
    hide();
}

void GuiSpectrumAnalyser::resumeVisualization()
{
    show();
}

void GuiSpectrumAnalyser::updatePeaks()
{
    if ((visualization_mode == MODE_DISABLED) || (!isVisible()))
        return;

    float* currentPeaks = winSkinVis->currentPeaks();

    if (!analyserCache)
        freshenAnalyserCache();

    for (int x = 0;x < __BANDS;x++) {
        int amp  = int(currentPeaks[x]);

        if (amp < 0)
            amp = 0;
        else if (amp > 16)
            amp = 16;

        bitBlt(this, x, 0, analyserCache, (amp * 2) + (x % 2), 0, 1, 16);
    }
}

void GuiSpectrumAnalyser::setVisualizationMode(int mode)
{
    visualization_mode = mode;

    visualizationMenu->setItemChecked(MODE_ANALYSER, (mode == MODE_ANALYSER));
    visualizationMenu->setItemChecked(MODE_DISABLED, (mode == MODE_DISABLED));

    if (mode == MODE_ANALYSER)
    {
        if (!winSkinVis)
        {
            winSkinVis=new WinSkinVis(TQT_TQOBJECT(this),"WinSkinVis");
            connect(winSkinVis,TQT_SIGNAL(doRepaint()),this,TQT_SLOT(updatePeaks()));
        }
    }
    else
    {
         delete winSkinVis;
         winSkinVis = NULL;
    }

    update();
}


void GuiSpectrumAnalyser::freshenAnalyserCache()
{
    // We need a color scheme
    if (!colorScheme)
         return;

    // The analyser cache is a 34x16 pixmap containing all the bits needed
    // to quickly draw the spectrum analyser
    analyserCache = new TQPixmap(34, 16);
    TQPainter p(analyserCache);

    for (unsigned int x = 0;x < 17;x++) {
        if (x != 16) {
            p.setPen(TQPen(colorScheme->skinColors[INDEX_BACKGROUND_COLOR]));
            p.drawLine(x * 2, 0, x * 2, 16 - x - 1);
        }

        for (unsigned int y = 0; y < (16 - x);y++) {
            if (y % 2)
                p.setPen(TQPen(colorScheme->skinColors[INDEX_GRID_COLOR]));
            else
                p.setPen(TQPen(colorScheme->skinColors[INDEX_BACKGROUND_COLOR]));

            p.drawPoint((x * 2) + 1, y);
        }

        if (!x)
            continue;

        switch (analyser_mode) {
        case MODE_FIRE:
            for (unsigned int y = (16 - x); y < 16; y++) {
                p.setPen(TQPen(colorScheme->skinColors[INDEX_SPEC_BASE + (y - (16 - x))]));
                p.drawPoint((x * 2), y);
                p.drawPoint((x * 2) + 1, y);
            }
            break;
        case MODE_VERTICAL_LINES:
            p.setPen(TQPen(colorScheme->skinColors[INDEX_SPEC_BASE + (16 - x)]));
            p.drawLine((x * 2), (15 - x), (x * 2), 15);
            p.drawLine((x * 2) + 1, (15 - x), (x * 2) + 1, 15);
            break;
        case MODE_NORMAL:
        // Fall through
        default:
            for (unsigned int y = (16 - x); y < 16; y++) {
                p.setPen(TQPen(colorScheme->skinColors[INDEX_SPEC_BASE + y]));
                p.drawPoint((x * 2), y);
                p.drawPoint((x * 2) + 1, y);
            }
            break;
        }
    }
}

void GuiSpectrumAnalyser::paintEvent (TQPaintEvent *)
{
    if (visualization_mode == MODE_DISABLED) 
        paintBackground();
}

void GuiSpectrumAnalyser::pixmapChange()
{
    delete analyserCache;
    analyserCache = NULL;
}


#include "guiSpectrumAnalyser.moc"