File: spectrumwidget.cpp

package info (click to toggle)
kalzium 4%3A17.08.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 56,876 kB
  • sloc: xml: 20,671; cpp: 16,953; ml: 799; makefile: 65; ansic: 32; sh: 21
file content (395 lines) | stat: -rw-r--r-- 11,324 bytes parent folder | download
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/***************************************************************************
 *   Copyright (C) 2005, 2006 by Carsten Niehaus                           *
 *   cniehaus@kde.org                                                      *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
 ***************************************************************************/

#include "spectrumwidget.h"

#include "spectrum.h"
#include "kalziumutils.h"

#include <config-kalzium.h>

#include <element.h>

#include <klocale.h>
#include <kunitconversion/converter.h>
#include <math.h>

#include <QKeyEvent>
#include <QSizePolicy>
#include <QPainter>
#include <QPixmap>
#include <QDebug>

#include <qglobal.h>
#if defined(HAVE_IEEEFP_H)
#include <ieeefp.h>
#endif

SpectrumWidget::SpectrumWidget(QWidget *parent) : QWidget(parent)
{
    m_startValue = 0;
    m_endValue = 0;

    m_LMBPointCurrent.setX(-1);
    m_LMBPointPress.setX(-1);

    m_realHeight = 200;

    m_gamma = 0.8;
    m_intensityMax = 255;

    setType(Prefs::spectrumType());

    setMinimumSize(400, 230);
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    setAttribute(Qt::WA_OpaquePaintEvent, true);
    setContextMenuPolicy(Qt::PreventContextMenu);
}

void SpectrumWidget::paintEvent(QPaintEvent * /*e*/)
{
    if (!m_spectrum) {
        return;
    }

    m_pixmap = QPixmap(width(), height());
    m_pixmap.fill(this, width(), height());

    QPainter p;
    p.begin(&m_pixmap);
    p.fillRect(0, 0, width(), m_realHeight, Qt::black);

    paintBands(&p);

    drawTickmarks(&p);

    if (m_LMBPointPress.x() != -1 && m_LMBPointCurrent.x() != -1) {
        drawZoomLine(&p);
    }

    p.end();

    QPainter p2(this);
    p2.drawPixmap(0, 0, m_pixmap);
}

void SpectrumWidget::drawZoomLine(QPainter* p)
{
    p->setPen(Qt::white);
    p->drawLine(m_LMBPointPress.x(), m_LMBPointPress.y(), m_LMBPointCurrent.x(), m_LMBPointPress.y());
    p->drawLine(m_LMBPointCurrent.x(), m_LMBPointPress.y() + 10, m_LMBPointCurrent.x(),
                m_LMBPointPress.y() - 10);
    p->drawLine(m_LMBPointPress.x(), m_LMBPointPress.y() + 10, m_LMBPointPress.x(),
                m_LMBPointPress.y() - 10);
}

void SpectrumWidget::paintBands(QPainter* p)
{
    if (m_type == AbsorptionSpectrum) {
        for (double va = m_startValue; va <= m_endValue; va += 0.1) {
            int x = xPos(va);
            p->setPen(wavelengthToRGB(va));
            p->drawLine(x, 0, x, m_realHeight);
        }

        p->setPen(Qt::black);
    }

    int i = 0;
    int x = 0;
    double currentWavelength;

    foreach (Spectrum::peak *peak, m_spectrum->peaklist()) {
        currentWavelength = peak->wavelengthToUnit(Prefs::spectrumWavelengthUnit());

        if (currentWavelength < m_startValue || currentWavelength > m_endValue) {
            continue;
        }

        x = xPos(currentWavelength);

        switch (m_type) {
        case EmissionSpectrum:
            p->setPen(wavelengthToRGB(currentWavelength));
            p->drawLine(x, 0, x, m_realHeight - 1);

            p->setPen(Qt::black);
            p->drawLine(x, m_realHeight, x, m_realHeight);
            break;

        case AbsorptionSpectrum:
            p->setPen(Qt::black);
            p->drawLine(x, 0, x, m_realHeight - 1);
            break;
        }

        ++i;
    }
}

QColor SpectrumWidget::wavelengthToRGB(double wavelength)
{
    double blue = 0.0, green = 0.0, red = 0.0, factor = 0.0;

    // wavelengthTo RGB function works with nanometers.
    wavelength = KUnitConversion::Value(wavelength,KUnitConversion::UnitId(Prefs::spectrumWavelengthUnit()))
                 .convertTo(KUnitConversion::Nanometer).number();

    int wavelength_ = (int)floor(wavelength);

    if (wavelength_ < 380 || wavelength_ > 780) {
        return QColor(Qt::white);
    } else if (wavelength_ >= 380 && wavelength_ < 440) {
        red = -(wavelength - 440) / (440 - 380);
        green = 0.0;
        blue = 1.0;
    } else if (wavelength_ >= 440 && wavelength_ < 490) {
        red = 0.0;
        green = (wavelength - 440) / (490 - 440);
        blue = 1.0;
    } else if (wavelength_ >= 490 && wavelength_ < 510) {
        red = 0.0;
        green = 1.0;
        blue = -(wavelength - 510) / (510 - 490);
    } else if (wavelength_ >= 510 && wavelength_ < 580) {
        red = (wavelength - 510) / (580 - 510);
        green = 1.0;
        blue = 0.0;
    } else if (wavelength_ >= 580 && wavelength_ < 645) {
        red = 1.0;
        green = -(wavelength - 645) / (645 - 580);
        blue = 0.0;
    } else if (wavelength_ >= 645 && wavelength_ < 780) {
        red = 1.0;
        green = 0.0;
        blue = 0.0;
    }

    if (wavelength_ > 380 && wavelength_ <= 420) {
        factor = 0.3 + 0.7 * (wavelength - 380) / (420 - 380);
    } else if (wavelength_ > 420 && wavelength_ <= 701) {
        factor = 1.0;
    } else if (wavelength_ > 701 && wavelength_ <= 780) {
        factor = 0.3 + 0.7 * (780 - wavelength) / (780 - 700);
    } else {
        factor = 0.0;
    }

    return QColor(Adjust(red, factor),
                  Adjust(green, factor),
                  Adjust(blue, factor));
}

int SpectrumWidget::Adjust(double color, double /*factor*/)
{
    if (color == 0.0) {
        return 0;
    } else {
//         return qRound(m_intensityMax * pow(color*factor, m_gamma)); FIXME
        return m_intensityMax * color;
    }
}

void SpectrumWidget::drawTickmarks(QPainter* p)
{
    //the size of the text on the tickmarks
    const int space = 20;

    //the distance between the tickmarks in pixel
    const int d = 10;

    //the total number of tickmarks to draw (small and long)
    const int numberOfTickmarks = (int)floor((double)(width() / d));

    double pos = 0.0;

    for (int i = 0; i < numberOfTickmarks; ++i) {
        if (i % 5 == 0) {
            //long tickmarks plus text
            p->drawLine(i * d, m_realHeight, i * d, m_realHeight + 10);
            if (i % 10 == 0 &&
                i * d > space &&
                i * d < width() - space) {
                pos = (double)(i * d) / width();
                p->fillRect(i * d - space, m_realHeight + 12, 2 * space, 15, Qt::white);
                p->drawText(i * d - space, m_realHeight + 12, 2 * space, 15, Qt::AlignCenter,
                            QString::number(KalziumUtils::strippedValue(Wavelength(pos))));
            }
        } else { //small tickmarks
            p->drawLine(i * d, m_realHeight, i * d, m_realHeight + 5);
        }
    }
}

void SpectrumWidget::keyPressEvent(QKeyEvent *e)
{
    switch (e->key()) {
    case Qt::Key_Plus:
        slotZoomIn();
        break;
    case Qt::Key_Minus:
        slotZoomOut();
        break;
    }
}

void SpectrumWidget::slotZoomOut()
{
    qDebug() << "SpectrumWidget::slotZoomOut() " << m_startValue << ":: " << m_endValue;

    double diff = m_endValue - m_startValue;

    double offset = diff * 0.05;

    m_endValue = m_endValue + offset;
    m_startValue = m_startValue - offset;

    //check for invalid values
    if (m_startValue < 0.0) {
        m_startValue = 0.0;
    }

    if (m_endValue > 10000.0) {   // FIXME: Magic numbers...
        m_endValue = 40000.0;
    }

    setBorders(m_startValue, m_endValue);
}

void SpectrumWidget::setBorders(double left, double right)
{
    qDebug() << "setBorders    " << left << ".." << right;

    m_startValue = left;
    m_endValue = right;

    //round the startValue down and the endValue up
    emit bordersChanged(int(m_startValue + 0.5), int(m_endValue + 0.5));

    update();
}

void SpectrumWidget::slotZoomIn()
{
    qDebug() << "SpectrumWidget::slotZoomIn() " << m_startValue << ":: " << m_endValue;

    double diff = m_endValue - m_startValue;

    double offset = diff * 0.05;

    m_endValue = m_endValue - offset;
    m_startValue = m_startValue + offset;

    setBorders(m_startValue, m_endValue);
}

void SpectrumWidget::mouseMoveEvent(QMouseEvent *e)
{
    m_LMBPointCurrent = e->pos();
    update();
}

void SpectrumWidget::mousePressEvent(QMouseEvent *e)
{
    if (e->button() == Qt::LeftButton) {
        m_LMBPointPress = e->pos();
    }
    if (e->button() == Qt::RightButton) {
        resetSpectrum();
    }

    findPeakFromMouseposition(Wavelength((double)e->pos().x() / width()));
}

void SpectrumWidget::findPeakFromMouseposition(double wavelength)
{
    qDebug() << "SpectrumWidget::findPeakFromMouseposition()";
    Spectrum::peak *peak = NULL;

    //find the difference in percent (1.0 is 100%, 0.1 is 10%)
    double dif = 0.0;

    bool foundWavelength = false;

    //find the highest intensity
    foreach (Spectrum::peak *currentPeak, m_spectrum->peaklist()) {
        double currentWavelength = currentPeak->wavelengthToUnit(Prefs::spectrumWavelengthUnit());

        double thisdif = currentWavelength / wavelength;

        if (thisdif < 0.9 || thisdif > 1.1) {
            continue;
        }

        if (thisdif > 1.0) {  //convert for example 1.3 to 0.7
            thisdif = thisdif - 1;
            thisdif = 1 - thisdif;
        }

        if (thisdif > dif) {
            dif = thisdif;
            peak = currentPeak;
            foundWavelength = true;
        }
    }

    if (foundWavelength) {
        emit peakSelected(peak);
    }
}

void SpectrumWidget::mouseReleaseEvent(QMouseEvent *e)
{
    if (e->button() == Qt::LeftButton) {
        int left = (int)Wavelength((double)m_LMBPointPress.x() / width());
        int right = (int)Wavelength((double)e->pos().x() / width());

        if (left == right) {
            return;
        }

        if (left > right) {
            setBorders(right, left);
        } else {
            setBorders(left, right);
        }
    }

    m_LMBPointPress.setX(-1);
    m_LMBPointCurrent.setX(-1);
}

void SpectrumWidget::setSpectrum(Spectrum* spec)
{
    m_spectrum = spec;
    resetSpectrum();
}

void SpectrumWidget::resetSpectrum()
{
    //set the minimum and maximum peak to the min/max wavelength
    //plus/minus ten. This makes then always visible
    double minimumPeak = m_spectrum->minPeak(Prefs::spectrumWavelengthUnit()) - 20.0;
    double maximumPeak = m_spectrum->maxPeak(Prefs::spectrumWavelengthUnit()) + 20.0;

    setBorders(minimumPeak, maximumPeak);
}