File indexing completed on 2024-05-12 15:28:15

0001 /***************************************************************************
0002     File                 : ConstantsWidget.cc
0003     Project              : LabPlot
0004     Description          : widget for selecting constants
0005     --------------------------------------------------------------------
0006     Copyright            : (C) 2014 Alexander Semke (alexander.semke@web.de)
0007 
0008  ***************************************************************************/
0009 
0010 /***************************************************************************
0011  *                                                                         *
0012  *  This program is free software; you can redistribute it and/or modify   *
0013  *  it under the terms of the GNU General Public License as published by   *
0014  *  the Free Software Foundation; either version 2 of the License, or      *
0015  *  (at your option) any later version.                                    *
0016  *                                                                         *
0017  *  This program is distributed in the hope that it will be useful,        *
0018  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0019  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0020  *  GNU General Public License for more details.                           *
0021  *                                                                         *
0022  *   You should have received a copy of the GNU General Public License     *
0023  *   along with this program; if not, write to the Free Software           *
0024  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0025  *   Boston, MA  02110-1301  USA                                           *
0026  *                                                                         *
0027  ***************************************************************************/
0028 #include "ConstantsWidget.h"
0029 #include "backend/gsl/ExpressionParser.h"
0030 #include <QTimer>
0031 
0032 /*!
0033     \class ConstantsWidget
0034     \brief Widget for selecting supported mathematical and physical constants
0035     that can be used in expressions in \c ExpressionTextEdit.
0036 
0037     \ingroup kdefrontend
0038  */
0039 ConstantsWidget::ConstantsWidget(QWidget* parent) : QWidget(parent) {
0040     ui.setupUi(this);
0041     ui.bInsert->setIcon(QIcon::fromTheme("edit-paste"));
0042     ui.bCancel->setIcon(QIcon::fromTheme("dialog-cancel"));
0043     m_expressionParser = ExpressionParser::getInstance();
0044     ui.cbGroup->addItems(m_expressionParser->constantsGroups());
0045 
0046     //SLOTS
0047     connect(ui.leFilter, &QLineEdit::textChanged, this, &ConstantsWidget::filterChanged);
0048     connect(ui.cbGroup, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &ConstantsWidget::groupChanged );
0049     connect(ui.lwConstants, &QListWidget::currentTextChanged, this, &ConstantsWidget::constantChanged);
0050     connect(ui.bInsert, &QPushButton::clicked, this, &ConstantsWidget::insertClicked);
0051     connect(ui.bCancel, &QPushButton::clicked, this, &ConstantsWidget::canceled);
0052     connect(ui.lwConstants, &QListWidget::itemDoubleClicked, this, &ConstantsWidget::insertClicked);
0053 
0054     //set the focus to the search field and select the first group after the widget is shown
0055     QTimer::singleShot(0, this, [=] () { ui.leFilter->setFocus(); this->groupChanged(0); });
0056 }
0057 
0058 /*!
0059  * shows all constants of the selected group and selects the first one in the QStringList
0060  */
0061 void ConstantsWidget::groupChanged(int index) {
0062     static const QStringList& constants = m_expressionParser->constants();
0063     static const QStringList& names = m_expressionParser->constantsNames();
0064     static const QVector<int>& indices = m_expressionParser->constantsGroupIndices();
0065 
0066     ui.lwConstants->clear();
0067     for (int i = 0; i < names.size(); ++i) {
0068         if (indices.at(i) == index)
0069             ui.lwConstants->addItem( names.at(i) + " (" + constants.at(i) + ')' );
0070     }
0071     ui.lwConstants->setCurrentRow(0);
0072 }
0073 
0074 void ConstantsWidget::filterChanged(const QString& filter) {
0075     if ( !filter.isEmpty() ) {
0076         ui.cbGroup->setEnabled(false);
0077 
0078         static const QStringList& names = m_expressionParser->constantsNames();
0079         static const QStringList& constants = m_expressionParser->constants();
0080         ui.lwConstants->clear();
0081         for (int i = 0; i < names.size(); ++i) {
0082             if (names.at(i).contains(filter, Qt::CaseInsensitive) || constants.at(i).contains(filter, Qt::CaseInsensitive))
0083                 ui.lwConstants->addItem( names.at(i) + " (" + constants.at(i) + ')' );
0084         }
0085 
0086         if (ui.lwConstants->count()) {
0087             ui.lwConstants->setCurrentRow(0);
0088             ui.bInsert->setEnabled(true);
0089         } else {
0090             ui.leValue->setText(QString());
0091             ui.lUnit->setText(QString());
0092             ui.bInsert->setEnabled(false);
0093         }
0094     } else {
0095         ui.cbGroup->setEnabled(true);
0096         groupChanged(ui.cbGroup->currentIndex());
0097     }
0098 }
0099 
0100 void ConstantsWidget::constantChanged(const QString& text) {
0101     static const QStringList& names = m_expressionParser->constantsNames();
0102     static const QStringList& values = m_expressionParser->constantsValues();
0103     static const QStringList& units = m_expressionParser->constantsUnits();
0104 
0105     QString name = text.left( text.indexOf(" (") );
0106     int index = names.indexOf(name);
0107     if (index != -1) {
0108         ui.leValue->setText(values.at(index));
0109         ui.leValue->setCursorPosition(0);
0110         ui.lUnit->setText(units.at(index));
0111     }
0112 }
0113 
0114 void ConstantsWidget::insertClicked() {
0115     static const QStringList& constants = m_expressionParser->constants();
0116     static const QStringList& names = m_expressionParser->constantsNames();
0117 
0118     //determine the currently selected constant
0119     const QString& text = ui.lwConstants->currentItem()->text();
0120     const QString& name = text.left( text.indexOf(" (") );
0121     int index = names.indexOf(name);
0122     const QString& constant = constants.at(index);
0123 
0124     emit constantSelected(constant);
0125 }