File indexing completed on 2022-09-27 12:24:09

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 #include <LineEditUrlDropEventFilter>
0014 
0015 
0016 #include <stdlib.h>
0017 
0018 #include <config-kalzium.h>
0019 
0020 #ifdef HAVE_FACILE
0021 extern "C" {
0022 char *solve_equation(const char *);
0023 }
0024 #else
0025 char *solve_equation(const char *)
0026 {
0027     return NULL;
0028 }
0029 #endif
0030 
0031 void EQChemDialog::compute()
0032 {
0033     QString equation(ui.lineEdit->text());
0034     equation.replace(QLatin1String("->"), QLatin1String(" -> "));
0035     equation.append(' ');
0036     equation.prepend(' ');
0037 
0038     char *result = solve_equation(equation.toLatin1().constData());
0039 
0040     QString answer = QString(result);
0041 
0042     qDebug() << "Answer: " << answer;
0043 
0044     ui.answer_label->setText(answer);
0045 
0046     // mem leak ?
0047     free(result);
0048 }
0049 
0050 EQChemDialog::EQChemDialog(QWidget *parent)
0051     : QWidget(parent)
0052 {
0053     ui.setupUi(this);
0054 
0055     LineEditUrlDropEventFilter *dropUrlEventFilter = new LineEditUrlDropEventFilter(parent);
0056     dropUrlEventFilter->installEventFilter(ui.lineEdit);
0057 
0058     ui.lblHelp->setText(getHelpText());
0059 
0060     connect(ui.calculateButton, &QAbstractButton::clicked, this, &EQChemDialog::compute);
0061     connect(ui.btnCopy, &QAbstractButton::clicked, this, &EQChemDialog::copyAnswer);
0062 }
0063 
0064 void EQChemDialog::copyAnswer()
0065 {
0066     qDebug() << "EQChemDialog::copyAnswer()";
0067     QClipboard *clipboard = QApplication::clipboard();
0068     clipboard->setText(ui.answer_label->text(), QClipboard::Clipboard);
0069 }
0070 
0071 const QString EQChemDialog::getHelpText()
0072 {
0073     return i18nc("Help text for the chemical equation solver",
0074                  "The equation solver allows you to balance a chemical equation.<br> "
0075                  "<br>"
0076                  "<b>Using Variables</b><br>"
0077                  "To express variable quantities of an element, put a single character in front "
0078                  "of the element's symbol, as shown in this example:<br>"
0079                  "<i>aH + bO -> 5H2O</i> (Result: <b>10</b> H + <b>5</b> O -&gt; <b>5</b> H<sub>2</sub>O)<br>"
0080                  "Solving this expression will give you the needed amount of Hydrogen and Oxygen.<br>"
0081                  "<br>"
0082                  "<b>Defining electric charges</b><br>"
0083                  "Use box brackets to specify the electric charge of an element, as shown in this example:<br>"
0084                  "<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>)");
0085 }
0086 
0087 QSize EQChemDialog::sizeHint() const
0088 {
0089     QSize size;
0090     size.setWidth(200);
0091     return size;
0092 }