File indexing completed on 2024-04-21 03:41:42

0001 /*
0002     SPDX-FileCopyrightText: 2004, 2005, 2006 Thomas Nagy <tnagy2^8@yahoo.fr>
0003     SPDX-FileCopyrightText: 2006 Carsten Niehaus <cniehaus@kde.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "eqchemview.h"
0009 
0010 #include <QClipboard>
0011 #include <QDebug>
0012 
0013 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0014 #include <LineEditUrlDropEventFilter>
0015 #else
0016 #include <KLineEditUrlDropEventFilter>
0017 #endif
0018 
0019 
0020 #include <stdlib.h>
0021 
0022 #include <config-kalzium.h>
0023 
0024 #ifdef HAVE_FACILE
0025 extern "C" {
0026 char *solve_equation(const char *);
0027 }
0028 #else
0029 char *solve_equation(const char *)
0030 {
0031     return NULL;
0032 }
0033 #endif
0034 
0035 void EQChemDialog::compute()
0036 {
0037     QString equation(ui.lineEdit->text());
0038     equation.replace(QLatin1String("->"), QLatin1String(" -> "));
0039     equation.append(' ');
0040     equation.prepend(' ');
0041 
0042     char *result = solve_equation(equation.toLatin1().constData());
0043 
0044     QString answer = QString(result);
0045 
0046     qDebug() << "Answer: " << answer;
0047 
0048     ui.answer_label->setText(answer);
0049 
0050     // mem leak ?
0051     free(result);
0052 }
0053 
0054 EQChemDialog::EQChemDialog(QWidget *parent)
0055     : QWidget(parent)
0056 {
0057     ui.setupUi(this);
0058 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0059     LineEditUrlDropEventFilter *dropUrlEventFilter = new LineEditUrlDropEventFilter(parent);
0060     dropUrlEventFilter->installEventFilter(ui.lineEdit);
0061 #else
0062     KLineEditUrlDropEventFilter *dropUrlEventFilter = new KLineEditUrlDropEventFilter(parent);
0063     dropUrlEventFilter->installEventFilter(ui.lineEdit);
0064 #endif
0065     ui.lblHelp->setText(getHelpText());
0066 
0067     connect(ui.calculateButton, &QAbstractButton::clicked, this, &EQChemDialog::compute);
0068     connect(ui.btnCopy, &QAbstractButton::clicked, this, &EQChemDialog::copyAnswer);
0069 }
0070 
0071 void EQChemDialog::copyAnswer()
0072 {
0073     qDebug() << "EQChemDialog::copyAnswer()";
0074     QClipboard *clipboard = QApplication::clipboard();
0075     clipboard->setText(ui.answer_label->text(), QClipboard::Clipboard);
0076 }
0077 
0078 const QString EQChemDialog::getHelpText()
0079 {
0080     return i18nc("Help text for the chemical equation solver",
0081                  "The equation solver allows you to balance a chemical equation.<br> "
0082                  "<br>"
0083                  "<b>Using Variables</b><br>"
0084                  "To express variable quantities of an element, put a single character in front "
0085                  "of the element's symbol, as shown in this example:<br>"
0086                  "<i>aH + bO -> 5H2O</i> (Result: <b>10</b> H + <b>5</b> O -&gt; <b>5</b> H<sub>2</sub>O)<br>"
0087                  "Solving this expression will give you the needed amount of Hydrogen and Oxygen.<br>"
0088                  "<br>"
0089                  "<b>Defining electric charges</b><br>"
0090                  "Use box brackets to specify the electric charge of an element, as shown in this example:<br>"
0091                  "<i>4H[+] + 2O -> cH2O[2+]</i> (Result: <b>4</b> H<b><sup>+</sup></b> + <b>2</b> O -&gt; <b>2</b> H<b><sub>2</sub></b>O<b><sup>2+</sub></b>)");
0092 }
0093 
0094 QSize EQChemDialog::sizeHint() const
0095 {
0096     QSize size;
0097     size.setWidth(200);
0098     return size;
0099 }
0100 
0101 #include "moc_eqchemview.cpp"