~ubuntu-branches/ubuntu/precise/kalzium/precise

« back to all changes in this revision

Viewing changes to src/spectrumwidget.h

  • Committer: Bazaar Package Importer
  • Author(s): Philip Muškovac
  • Date: 2011-07-03 12:28:58 UTC
  • Revision ID: james.westby@ubuntu.com-20110703122858-q1yyxncs89e4w0hs
Tags: upstream-4.6.90+repack
Import upstream version 4.6.90+repack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2005, 2006 by Carsten Niehaus                                 *
 
3
 *   cniehaus@kde.org                                                      *
 
4
 *
 
5
 *                                                                         *
 
6
 *   This program is free software; you can redistribute it and/or modify  *
 
7
 *   it under the terms of the GNU General Public License as published by  *
 
8
 *   the Free Software Foundation; either version 2 of the License, or     *
 
9
 *   (at your option) any later version.                                   *
 
10
 *                                                                         *
 
11
 *   This program is distributed in the hope that it will be useful,       *
 
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
14
 *   GNU General Public License for more details.                          *
 
15
 *                                                                         *
 
16
 *   You should have received a copy of the GNU General Public License     *
 
17
 *   along with this program; if not, write to the                         *
 
18
 *   Free Software Foundation, Inc.,                                       *
 
19
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.             *
 
20
 ***************************************************************************/
 
21
#ifndef SPECTRUMWIDGET_H
 
22
#define SPECTRUMWIDGET_H
 
23
 
 
24
#include <QWidget>
 
25
 
 
26
#include "spectrum.h"
 
27
#include "prefs.h"
 
28
 
 
29
/**
 
30
 * @author Carsten Niehaus
 
31
 */
 
32
class SpectrumWidget : public QWidget
 
33
{
 
34
    Q_OBJECT
 
35
 
 
36
public:
 
37
    SpectrumWidget( QWidget *parent );
 
38
 
 
39
    ~SpectrumWidget() {}
 
40
 
 
41
    void setSpectrum( Spectrum* spec );
 
42
 
 
43
    Spectrum* spectrum()const {
 
44
        return m_spectrum;
 
45
    }
 
46
 
 
47
    /**
 
48
     * This limits the width of the spectrum in terms of
 
49
     * wavelength. For example you can set it to only
 
50
     * show the area between 500 and 550 nm
 
51
     *
 
52
     * @param left the left border
 
53
     * @param right the right border
 
54
     */
 
55
    void setBorders( double left, double right );
 
56
 
 
57
    /**
 
58
     * there are several possible types.
 
59
     */
 
60
    enum SpectrumType
 
61
    {
 
62
        EmissionSpectrum = 0,
 
63
        AbsorptionSpectrum
 
64
    };
 
65
 
 
66
    /**
 
67
     * sets the type of the spectrum to @p t
 
68
     * @param t the type of the spectrum
 
69
     */
 
70
    void setType( int t ) {
 
71
        m_type = t;
 
72
    }
 
73
 
 
74
    /**
 
75
     * @return the currently active type
 
76
     * of the spectrum
 
77
     */
 
78
    int spectrumType() const {
 
79
        return m_type;
 
80
    }
 
81
 
 
82
    /**
 
83
     * @return the adjusted value of the @p color. The
 
84
     * correction depends on @p factor which has been
 
85
     * figured out emperically
 
86
     */
 
87
    int Adjust( double color, double factor );
 
88
 
 
89
    /**
 
90
     * @return the postion in the widget of a band
 
91
     * with the wavelength @p wavelength
 
92
     *
 
93
     * @param wavelength the wavelength for which the position is needed
 
94
     */
 
95
    inline int xPos( double wavelength ) {
 
96
        return ( int ) ( ( wavelength-m_startValue ) * width() / ( m_endValue - m_startValue ) );
 
97
    }
 
98
 
 
99
    /**
 
100
     * based on the current position of the mouse-cursor the nearest
 
101
     * peak is searched. If found, it will be emitted.
 
102
     *
 
103
     * @see peakSelected
 
104
     */
 
105
    void findPeakFromMouseposition( double wavelength );
 
106
 
 
107
    /**
 
108
     * @param xpos The ratio of the position relative to the width
 
109
     * of the widget.
 
110
     * @return the wavelength on position @p xpos
 
111
     */
 
112
    inline double Wavelength( double xpos ) {
 
113
        return m_startValue + (  (  m_endValue-m_startValue ) *  xpos );
 
114
    }
 
115
 
 
116
    /**
 
117
     * This method changes the three values @p r, @p g and @p b to the
 
118
     * correct values
 
119
     * @param wavelength the wavelength for which the color is searched
 
120
     * @return the wavelenth color
 
121
     */
 
122
    QColor wavelengthToRGB( double wavelength );
 
123
 
 
124
private:
 
125
    QList<double> m_spectra;
 
126
 
 
127
    int m_type;
 
128
 
 
129
    Spectrum *m_spectrum;
 
130
 
 
131
    QPixmap m_pixmap;
 
132
 
 
133
    void paintBands( QPainter* p );
 
134
    void drawZoomLine( QPainter* p );
 
135
 
 
136
    /**
 
137
     * Draw the scale
 
138
     */
 
139
    void drawTickmarks( QPainter *p );
 
140
 
 
141
    double m_startValue;
 
142
    double m_endValue;
 
143
 
 
144
    double m_gamma;
 
145
    int m_intensityMax;
 
146
 
 
147
    int m_realHeight;
 
148
 
 
149
    /**
 
150
     * this QPoint stores the information where
 
151
     * the left mouse button has been pressed. This
 
152
     * is used for the mouse zooming
 
153
     */
 
154
    QPoint m_LMBPointPress;
 
155
 
 
156
    QPoint m_LMBPointCurrent;
 
157
 
 
158
public slots:
 
159
    ///(re)create startconditions
 
160
    void resetSpectrum();
 
161
 
 
162
    /**
 
163
     * set the maximum value to @p value
 
164
     */
 
165
    void setRightBorder( int value ) {
 
166
        if (value != m_endValue) {
 
167
            m_endValue = value;
 
168
            if ( m_endValue < m_startValue ) {
 
169
                m_startValue = m_endValue-1;
 
170
            }
 
171
            update();
 
172
        }
 
173
    }
 
174
 
 
175
    /**
 
176
     * set the minimum value to @p value
 
177
     */
 
178
    void setLeftBorder( int value ) {
 
179
        if (value != m_startValue) {
 
180
            m_startValue = value;
 
181
            if ( m_startValue > m_endValue ) {
 
182
                m_endValue = m_startValue+1;
 
183
            }
 
184
            update();
 
185
        }
 
186
    }
 
187
 
 
188
    /**
 
189
     * activates the spectrum of the type @p spectrumtype
 
190
     */
 
191
    void slotActivateSpectrum( int spectrumtype ) {
 
192
        m_type = spectrumtype;
 
193
        Prefs::setSpectrumType( spectrumtype );
 
194
        Prefs::self()->writeConfig();
 
195
        update();
 
196
    }
 
197
 
 
198
signals:
 
199
    /**
 
200
     * the minimum and maximum displayed wavelength have
 
201
     * changed so emit the new minimum and maximum
 
202
     */
 
203
    void bordersChanged( int, int );
 
204
 
 
205
    /**
 
206
     * the user selected a peak
 
207
     */
 
208
    void peakSelected( Spectrum::peak * peak );
 
209
 
 
210
private slots:
 
211
    void slotZoomIn();
 
212
    void slotZoomOut();
 
213
 
 
214
protected:
 
215
    virtual void paintEvent( QPaintEvent *e );
 
216
    virtual void keyPressEvent(QKeyEvent *e);
 
217
    virtual void mouseMoveEvent( QMouseEvent *e );
 
218
    virtual void mousePressEvent( QMouseEvent *e );
 
219
    virtual void mouseReleaseEvent( QMouseEvent *e );
 
220
};
 
221
 
 
222
#endif // SPECTRUMWIDGET_H