File indexing completed on 2024-04-14 03:40:24

0001 /*
0002     SPDX-FileCopyrightText: 2005, 2006 Carsten Niehaus <cniehaus@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef SPECTRUMWIDGET_H
0008 #define SPECTRUMWIDGET_H
0009 
0010 #include <QWidget>
0011 
0012 #include "prefs.h"
0013 #include "spectrum.h"
0014 
0015 /**
0016  * @author Carsten Niehaus
0017  */
0018 class SpectrumWidget : public QWidget
0019 {
0020     Q_OBJECT
0021 
0022 public:
0023     explicit SpectrumWidget(QWidget *parent);
0024 
0025     ~SpectrumWidget() override = default;
0026 
0027     void setSpectrum(Spectrum *spec);
0028 
0029     Spectrum *spectrum() const
0030     {
0031         return m_spectrum;
0032     }
0033 
0034     /**
0035      * This limits the width of the spectrum in terms of
0036      * wavelength. For example you can set it to only
0037      * show the area between 500 and 550 nm
0038      *
0039      * @param left the left border
0040      * @param right the right border
0041      */
0042     void setBorders(double left, double right);
0043 
0044     /**
0045      * there are several possible types.
0046      */
0047     enum SpectrumType { EmissionSpectrum = 0, AbsorptionSpectrum };
0048 
0049     /**
0050      * sets the type of the spectrum to @p t
0051      * @param t the type of the spectrum
0052      */
0053     void setType(int t)
0054     {
0055         m_type = t;
0056     }
0057 
0058     /**
0059      * @return the currently active type
0060      * of the spectrum
0061      */
0062     int spectrumType() const
0063     {
0064         return m_type;
0065     }
0066 
0067     /**
0068      * @return the adjusted value of the @p color. The
0069      * correction depends on @p factor which has been
0070      * figured out empirically
0071      */
0072     int Adjust(double color, double factor);
0073 
0074     /**
0075      * @return the position in the widget of a band
0076      * with the wavelength @p wavelength
0077      *
0078      * @param wavelength the wavelength for which the position is needed
0079      */
0080     inline int xPos(double wavelength)
0081     {
0082         return (int)((wavelength - m_startValue) * width() / (m_endValue - m_startValue));
0083     }
0084 
0085     /**
0086      * based on the current position of the mouse-cursor the nearest
0087      * peak is searched. If found, it will be emitted.
0088      *
0089      * @see peakSelected
0090      */
0091     void findPeakFromMouseposition(double wavelength);
0092 
0093     /**
0094      * @param xpos The ratio of the position relative to the width
0095      * of the widget.
0096      * @return the wavelength on position @p xpos
0097      */
0098     inline double Wavelength(double xpos)
0099     {
0100         return m_startValue + ((m_endValue - m_startValue) * xpos);
0101     }
0102 
0103     /**
0104      * This method changes the three values @p r, @p g and @p b to the
0105      * correct values
0106      * @param wavelength the wavelength for which the color is searched
0107      * @return the wavelenth color
0108      */
0109     QColor wavelengthToRGB(double wavelength);
0110 
0111     /**
0112      * set the maximum value to @p value
0113      */
0114     void setRightBorder(int value)
0115     {
0116         if (value != m_endValue) {
0117             m_endValue = value;
0118             if (m_endValue < m_startValue) {
0119                 m_startValue = m_endValue - 1;
0120             }
0121             update();
0122         }
0123     }
0124 
0125     /**
0126      * set the minimum value to @p value
0127      */
0128     void setLeftBorder(int value)
0129     {
0130         if (value != m_startValue) {
0131             m_startValue = value;
0132             if (m_startValue > m_endValue) {
0133                 m_endValue = m_startValue + 1;
0134             }
0135             update();
0136         }
0137     }
0138 
0139 private:
0140     QList<double> m_spectra;
0141 
0142     int m_type;
0143 
0144     Spectrum *m_spectrum;
0145 
0146     QPixmap m_pixmap;
0147 
0148     void paintBands(QPainter *p);
0149     void drawZoomLine(QPainter *p);
0150 
0151     /**
0152      * Draw the scale
0153      */
0154     void drawTickmarks(QPainter *p);
0155 
0156     double m_startValue;
0157     double m_endValue;
0158 
0159     double m_gamma;
0160     int m_intensityMax;
0161 
0162     int m_realHeight;
0163 
0164     /**
0165      * this QPoint stores the information where
0166      * the left mouse button has been pressed. This
0167      * is used for the mouse zooming
0168      */
0169     QPoint m_LMBPointPress;
0170 
0171     QPoint m_LMBPointCurrent;
0172 
0173 public Q_SLOTS:
0174     ///(re)create startconditions
0175     void resetSpectrum();
0176 
0177     /**
0178      * activates the spectrum of the type @p spectrumtype
0179      */
0180     void slotActivateSpectrum(int spectrumtype)
0181     {
0182         m_type = spectrumtype;
0183         Prefs::setSpectrumType(spectrumtype);
0184         Prefs::self()->save();
0185         update();
0186     }
0187 
0188 Q_SIGNALS:
0189     /**
0190      * the minimum and maximum displayed wavelength have
0191      * changed so emit the new minimum and maximum
0192      */
0193     void bordersChanged(int, int);
0194 
0195     /**
0196      * the user selected a peak
0197      */
0198     void peakSelected(Spectrum::peak *peak);
0199 
0200 private Q_SLOTS:
0201     void slotZoomIn();
0202     void slotZoomOut();
0203 
0204 protected:
0205     void paintEvent(QPaintEvent *e) override;
0206     void keyPressEvent(QKeyEvent *e) override;
0207     void mouseMoveEvent(QMouseEvent *e) override;
0208     void mousePressEvent(QMouseEvent *e) override;
0209     void mouseReleaseEvent(QMouseEvent *e) override;
0210 };
0211 
0212 #endif // SPECTRUMWIDGET_H