File indexing completed on 2022-11-29 12:50:42

0001 /*
0002     SPDX-FileCopyrightText: 2005-2008 Carsten Niehaus <cniehaus@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "isotopetabledialog.h"
0008 
0009 #include "isotopeitem.h"
0010 #include "isotopescene.h"
0011 #include "kalziumschemetype.h"
0012 #include "legendwidget.h"
0013 
0014 #include <isotope.h>
0015 
0016 #include "kalzium_debug.h"
0017 #include <QDialogButtonBox>
0018 #include <QPushButton>
0019 #include <QVBoxLayout>
0020 #include <QtMath>
0021 
0022 #include <KConfigGroup>
0023 
0024 #include <prefs.h>
0025 
0026 IsotopeTableDialog::IsotopeTableDialog(QWidget *parent)
0027     : QDialog(parent)
0028 {
0029     setWindowTitle(i18nc("@title:window", "Isotope Table"));
0030     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Close);
0031     auto mainWidget = new QWidget(this);
0032     auto mainLayout = new QVBoxLayout(this);
0033     mainLayout->addWidget(mainWidget);
0034     connect(buttonBox, &QDialogButtonBox::rejected, this, &IsotopeTableDialog::reject);
0035     mainLayout->addWidget(buttonBox);
0036     buttonBox->button(QDialogButtonBox::Close)->setDefault(true);
0037     ui.setupUi(mainWidget);
0038     ui.guide->setGuidedView(ui.gv);
0039 
0040     connect(ui.gv->scene(), SIGNAL(itemSelected(IsotopeItem *)), this, SLOT(updateDockWidget(IsotopeItem *)));
0041     connect(ui.gv, &IsotopeView::zoomLevelChanged, this, &IsotopeTableDialog::slotZoomLevelChanged);
0042     connect(ui.Slider, &QAbstractSlider::valueChanged, this, &IsotopeTableDialog::zoom);
0043 
0044     // Here comes the legend part
0045     QList<QPair<QString, QColor>> items;
0046 
0047     items << qMakePair(i18nc("alpha ray emission", "alpha"), QColor(Qt::red));
0048     items << qMakePair(i18nc("Electron capture method", "EC"), QColor(Qt::blue));
0049     items << qMakePair(i18nc("Many ways", "Multiple"), QColor(Qt::green));
0050     items << qMakePair(i18nc("Beta plus ray emission", "Beta +"), QColor(Qt::yellow));
0051     items << qMakePair(i18nc("Beta minus ray emission", "Beta -"), QColor(Qt::white));
0052     items << qMakePair(i18nc("Stable isotope", "Stable"), QColor(Qt::magenta));
0053     items << qMakePair(i18nc("Unknown Decay", "unknown"), QColor(Qt::darkGray));
0054 
0055     foreach (const legendPair &pair, items) {
0056         auto item = new LegendItem(pair);
0057         ui.infoWidget->layout()->addWidget(item);
0058     }
0059     ui.infoWidget->setMinimumWidth(150);
0060 }
0061 
0062 void IsotopeTableDialog::zoom(int level)
0063 {
0064     double zoom = qPow(M_E, level / 10.0);
0065     (ui.gv)->setZoom(zoom);
0066 }
0067 
0068 void IsotopeTableDialog::updateDockWidget(IsotopeItem *item)
0069 {
0070     Isotope *s = item->isotope();
0071 
0072     const QString header = i18n("<h1>%1 (%2)</h1>", s->parentElementSymbol(), s->parentElementNumber());
0073     const QString mag = i18n("Magnetic moment: %1", s->magmoment().isEmpty() ? i18nc("Unknown magnetic moment", "Unknown") : s->magmoment());
0074 
0075     QString halflife;
0076     if (s->halflife() > 0.0) {
0077         halflife = i18n("Halflife: %1 %2", s->halflife(), s->halflifeUnit());
0078     } else {
0079         halflife = i18n("Halflife: Unknown");
0080     }
0081 
0082     const QString abundance = i18n("Abundance: %1 %", !s->abundance().isEmpty() ? s->abundance() : QStringLiteral("0"));
0083     const QString nucleons = i18n("Number of nucleons: %1", s->nucleons());
0084     const QString spin = i18n("Spin: %1", s->spin().isEmpty() ? i18nc("Unknown spin", "Unknown") : s->spin());
0085     const QString exactMass = i18n("Exact mass: %1 u", s->mass());
0086 
0087     const QString html = header + "<br />" + nucleons + "<br />" + mag + "<br />" + exactMass + "<br />" + spin + "<br />" + abundance + "<br />" + halflife;
0088 
0089     ui.label->setText(html);
0090 }
0091 
0092 void IsotopeTableDialog::slotZoomLevelChanged(double value)
0093 {
0094     const bool b = ui.Slider->blockSignals(true);
0095     ui.Slider->setValue(qLn(value) * 10.0);
0096     ui.Slider->blockSignals(b);
0097 }
0098 
0099 void IsotopeTableDialog::setMode(int mode)
0100 {
0101     ui.gv->setMode(mode);
0102     ui.guide->updateScene();
0103 }
0104 
0105 void IsotopeTableDialog::updateMode()
0106 {
0107     setMode(Prefs::isotopeTableMode());
0108 }