File indexing completed on 2022-10-04 13:46:51

0001 /*
0002     SPDX-FileCopyrightText: 2005, 2006 Carsten Niehaus <cniehaus@kde.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "spectrumviewimpl.h"
0007 
0008 #include "kalzium_debug.h"
0009 #include <QTableWidget>
0010 #include <QTreeWidget>
0011 
0012 #include <KLocalizedString>
0013 #include <KUnitConversion/Converter>
0014 
0015 #include "kalziumdataobject.h"
0016 #include "prefs.h"
0017 
0018 SpectrumViewImpl::SpectrumViewImpl(QWidget *parent)
0019     : QWidget(parent)
0020 {
0021     setupUi(this);
0022 
0023     QStringList headers = QStringList() << i18n("Wavelength") << i18n("Intensity");
0024     peakListTable->setHeaderLabels(headers);
0025     peakListTable->setRootIsDecorated(false);
0026 
0027     QList<int> length;
0028     length << KUnitConversion::Nanometer << KUnitConversion::Angstrom;
0029     m_lengthUnit->setUnitList(length);
0030 
0031     m_lengthUnit->setIndexWithUnitId(Prefs::spectrumWavelengthUnit());
0032 
0033     m_spectrumType->setCurrentIndex(Prefs::spectrumType());
0034 
0035     connect(minimumValue, SIGNAL(valueChanged(int)), this, SLOT(updateMin(int)));
0036     connect(maximumValue, SIGNAL(valueChanged(int)), this, SLOT(updateMax(int)));
0037     connect(m_spectrumWidget, &SpectrumWidget::bordersChanged, this, &SpectrumViewImpl::updateUI);
0038     connect(m_spectrumWidget, &SpectrumWidget::peakSelected, this, &SpectrumViewImpl::updatePeakInformation);
0039 
0040     connect(m_spectrumType, SIGNAL(currentIndexChanged(int)), m_spectrumWidget, SLOT(slotActivateSpectrum(int)));
0041 
0042     connect(btn_resetZoom, &QAbstractButton::pressed, m_spectrumWidget, &SpectrumWidget::resetSpectrum);
0043     connect(this, &SpectrumViewImpl::settingsChanged, m_spectrumWidget, &SpectrumWidget::resetSpectrum);
0044 
0045     connect(m_lengthUnit, SIGNAL(currentIndexChanged(int)), this, SLOT(setUnit()));
0046 
0047     resize(minimumSizeHint());
0048 }
0049 
0050 void SpectrumViewImpl::fillPeakList()
0051 {
0052     peakListTable->clear();
0053 
0054     QList<QTreeWidgetItem *> items;
0055 
0056     for (int i = 0; i < m_spectrumWidget->spectrum()->peaklist().count(); ++i) {
0057         Spectrum::peak *peak = m_spectrumWidget->spectrum()->peaklist().at(i);
0058 
0059         double peakWavelength = peak->wavelengthToUnit(Prefs::spectrumWavelengthUnit());
0060 
0061         const QStringList row = QStringList() << QString::number(peakWavelength) << QString::number(peak->intensity);
0062 
0063         items.append(new QTreeWidgetItem((QTreeWidget *)nullptr, row));
0064     }
0065     peakListTable->insertTopLevelItems(0, items);
0066 }
0067 
0068 void SpectrumViewImpl::updateUI(int l, int r)
0069 {
0070     minimumValue->setValue(l);
0071     maximumValue->setValue(r);
0072     minimumValue->setSuffix(KalziumDataObject::instance()->unitAsString(Prefs::spectrumWavelengthUnit()));
0073     maximumValue->setSuffix(KalziumDataObject::instance()->unitAsString(Prefs::spectrumWavelengthUnit()));
0074 }
0075 
0076 void SpectrumViewImpl::updatePeakInformation(Spectrum::peak *peak)
0077 {
0078     double peakWavelength = peak->wavelengthToUnit(Prefs::spectrumWavelengthUnit());
0079 
0080     const QList<QTreeWidgetItem *> foundItems = peakListTable->findItems(QString::number(peakWavelength), Qt::MatchExactly);
0081 
0082     if (foundItems.isEmpty()) {
0083         return;
0084     }
0085 
0086     foreach (QTreeWidgetItem *item, peakListTable->selectedItems())
0087         item->setSelected(false);
0088 
0089     foundItems.first()->setSelected(true);
0090     peakListTable->scrollToItem(foundItems.first());
0091 }
0092 
0093 void SpectrumViewImpl::setUnit()
0094 {
0095     Prefs::setSpectrumWavelengthUnit(m_lengthUnit->getCurrentUnitId());
0096     Prefs::self()->save();
0097     qCDebug(KALZIUM_LOG) << "Unit changed: " << m_lengthUnit->getCurrentUnitId();
0098 
0099     Q_EMIT settingsChanged();
0100     fillPeakList();
0101 }
0102 
0103 void SpectrumViewImpl::updateMin(int left)
0104 {
0105     m_spectrumWidget->setRightBorder(maximumValue->value());
0106     m_spectrumWidget->setLeftBorder(left);
0107 }
0108 
0109 void SpectrumViewImpl::updateMax(int right)
0110 {
0111     m_spectrumWidget->setLeftBorder(minimumValue->value());
0112     m_spectrumWidget->setRightBorder(right);
0113 }