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

0001 /*
0002     SPDX-FileCopyrightText: 2009 Kashyap R Puranik <kashthealien@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "calculator.h"
0008 
0009 #include "kalzium_debug.h"
0010 #include <QDialogButtonBox>
0011 #include <QIcon>
0012 #include <QPushButton>
0013 #include <QVBoxLayout>
0014 
0015 #include <KHelpClient>
0016 #include <KLocalizedString>
0017 
0018 calculator::calculator(QWidget *parent)
0019     : QDialog(parent)
0020 {
0021     setWindowTitle(i18nc("@title:window", "Chemical Calculator"));
0022     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Help | QDialogButtonBox::Close, this);
0023     auto mainWidget = new QWidget(this);
0024     auto mainLayout = new QVBoxLayout(this);
0025     mainLayout->addWidget(mainWidget);
0026     connect(buttonBox, &QDialogButtonBox::accepted, this, &calculator::accept);
0027     connect(buttonBox, &QDialogButtonBox::rejected, this, &calculator::reject);
0028     mainLayout->addWidget(buttonBox);
0029     buttonBox->button(QDialogButtonBox::Close)->setDefault(true);
0030 
0031     ui.setupUi(mainWidget);
0032 
0033     int maxTextWidth = 0;
0034     QStyleOptionViewItem option;
0035     option.initFrom(ui.tree);
0036     for (int i = 0; i < ui.tree->topLevelItemCount(); ++i) {
0037         maxTextWidth = qMax(maxTextWidth, ui.tree->itemDelegate()->sizeHint(option, ui.tree->model()->index(i, 0)).width());
0038     }
0039     // 20 because we want some margins, not a too tight text
0040     ui.tree->setMaximumWidth(qMax(ui.tree->maximumWidth(), maxTextWidth + 20));
0041 
0042     // Add the nuclear calculator to the user interface
0043     m_nuclearCalculator = new nuclearCalculator(this);
0044     ui.stack->addWidget(m_nuclearCalculator);
0045     // Add the gas calculator to the user interface
0046     m_gasCalculator = new gasCalculator(this);
0047     ui.stack->addWidget(m_gasCalculator);
0048     // Add the concentration calculator to the user interface
0049     m_concCalculator = new concCalculator(this);
0050     ui.stack->addWidget(m_concCalculator);
0051     // Add the molecular mass Calculator widget to the user interface
0052     m_moleCalculator = new MolcalcWidget(this);
0053     ui.stack->addWidget(m_moleCalculator);
0054     // Add the molecular mass Calculator widget to the user interface
0055     m_titraCalculator = new titrationCalculator(this);
0056     ui.stack->addWidget(m_titraCalculator);
0057 
0058 #ifdef HAVE_FACILE
0059     // Add the equation balancer widget to the user interface
0060     QTreeWidgetItem *treeItem = new QTreeWidgetItem(ui.tree);
0061     treeItem->setText(0, i18n("Equation Balancer"));
0062 
0063     m_equationBalancer = new EQChemDialog(this);
0064     ui.stack->addWidget(m_equationBalancer);
0065 #endif
0066     // Add an image to the file
0067     ui.pic->setPixmap((QIcon::fromTheme(QStringLiteral("calculate"))).pixmap(128, 128));
0068 
0069     // Connect the tree item selection signal to the corresponding slot
0070     connect(ui.tree, &QTreeWidget::itemClicked, this, &calculator::slotItemSelection);
0071 
0072     ui.tree->setCurrentItem(ui.tree->topLevelItem(0), 0, QItemSelectionModel::ToggleCurrent);
0073 
0074     // help clicked
0075     connect(buttonBox->button(QDialogButtonBox::Help), &QPushButton::clicked, this, &calculator::slotHelp);
0076 }
0077 
0078 calculator ::~calculator() = default;
0079 
0080 void calculator::slotItemSelection(QTreeWidgetItem *item)
0081 {
0082     if (item == nullptr) {
0083         return;
0084     }
0085 
0086     // DEBUG
0087     qCDebug(KALZIUM_LOG) << "Item clicked: " << item->text(0);
0088 
0089     QString s = item->text(0);
0090 
0091     if (!(s.compare(i18n("Introduction")))) {
0092         ui.stack->setCurrentWidget(ui.intro);
0093     } else if (!(s.compare(i18n("Nuclear Calculator")))) {
0094         // if nuclear calculator is selected, show the widget in the user interface
0095         ui.stack->setCurrentWidget(m_nuclearCalculator);
0096     } else if (!(s.compare(i18n("Gas Calculator")))) {
0097         // if gas calculator is selected, show the widget in the user interface
0098         ui.stack->setCurrentWidget(m_gasCalculator);
0099     } else if (!(s.compare(i18n("Concentration Calculator")))) {
0100         // if the concentration calculator is selected, show the widget in the UI
0101         ui.stack->setCurrentWidget(m_concCalculator);
0102 
0103         // The equation balancer needs FACILE library, if it's present HAVE_FACILE = 1
0104 #ifdef HAVE_FACILE
0105     } else if (!(s.compare(i18n("Equation Balancer")))) {
0106         // If the equation balancer was selected, open it in the UI.
0107         ui.stack->setCurrentWidget(m_equationBalancer);
0108 #endif
0109 
0110     } else if (!(s.compare(i18n("Molecular mass Calculator")))) {
0111         ui.stack->setCurrentWidget(m_moleCalculator);
0112     } else if (!(s.compare(i18n("Titration Calculator")))) {
0113         ui.stack->setCurrentWidget(m_titraCalculator);
0114     }
0115 }
0116 
0117 void calculator::slotHelp()
0118 {
0119     KHelpClient::invokeHelp(QStringLiteral("tools.html#perf_calculation"), QStringLiteral("kalzium"));
0120 }