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

0001 /*
0002     KmPlot - a math. function plotter for the KDE-Desktop
0003 
0004     SPDX-FileCopyrightText: 2006 David Saxton <david@bluehaze.org>
0005 
0006     This file is part of the KDE Project.
0007     KmPlot is part of the KDE-EDU Project.
0008 
0009     SPDX-License-Identifier: GPL-2.0-or-later
0010 
0011 */
0012 
0013 #include "equationeditorwidget.h"
0014 
0015 #include <KAcceleratorManager>
0016 
0017 #include "equationeditwidget.h"
0018 #include "maindlg.h"
0019 #include "xparser.h"
0020 
0021 EquationEditorWidget::EquationEditorWidget(QWidget *parent)
0022     : QWidget(parent)
0023 {
0024     setupUi(this);
0025 
0026     QFont font;
0027     double pointSize = font.pointSizeF() * 1.1;
0028     font.setPointSizeF(pointSize);
0029     edit->m_equationEditWidget->document()->setDefaultFont(font);
0030     edit->m_equationEditWidget->recalculateGeometry();
0031 
0032     QFont buttonFont;
0033     buttonFont.setPointSizeF(font.pointSizeF() * 1.1);
0034 
0035     const QList<QToolButton *> buttons = findChildren<QToolButton *>();
0036     for (QToolButton *w : buttons) {
0037         KAcceleratorManager::setNoAccel(w);
0038 
0039         connect(w, &QToolButton::clicked, this, &EquationEditorWidget::characterButtonClicked);
0040 
0041         // Also increase the font size, since the fractions, etc are probably not that visible
0042         // at the default font size
0043         w->setFont(buttonFont);
0044     }
0045 
0046     connect(constantsButton, &QPushButton::clicked, this, &EquationEditorWidget::editConstants);
0047     connect(functionList, &QComboBox::textActivated, this, &EquationEditorWidget::insertFunction);
0048     connect(constantList, QOverload<int>::of(&QComboBox::activated), this, &EquationEditorWidget::insertConstant);
0049 
0050     QStringList functions = XParser::self()->predefinedFunctions(false);
0051     functions.sort();
0052     functionList->addItems(functions);
0053 
0054     connect(XParser::self()->constants(), &Constants::constantsChanged, this, &EquationEditorWidget::updateConstantList);
0055     updateConstantList();
0056 }
0057 
0058 void EquationEditorWidget::updateConstantList()
0059 {
0060     QStringList items;
0061 
0062     // The first item text is "Insert constant..."
0063     items << constantList->itemText(0);
0064 
0065     ConstantList constants = XParser::self()->constants()->list(Constant::All);
0066     for (ConstantList::iterator it = constants.begin(); it != constants.end(); ++it) {
0067         QString text = it.key() + " = " + it.value().value.expression();
0068         items << text;
0069     }
0070 
0071     constantList->clear();
0072     constantList->addItems(items);
0073 }
0074 
0075 void EquationEditorWidget::insertFunction(const QString &function)
0076 {
0077     if (functionList->currentIndex() == 0)
0078         return;
0079 
0080     functionList->setCurrentIndex(0);
0081     edit->wrapSelected(function + '(', ")");
0082     edit->setFocus();
0083 }
0084 
0085 void EquationEditorWidget::editConstants()
0086 {
0087     MainDlg::self()->editConstantsModal(this);
0088 }
0089 
0090 void EquationEditorWidget::insertConstant(int index)
0091 {
0092     if (index == 0)
0093         return;
0094 
0095     ConstantList constants = XParser::self()->constants()->list(Constant::All);
0096 
0097     if (constants.size() < index)
0098         return;
0099 
0100     // Don't forget that index==0 corresponds to "Insert constant..."
0101 
0102     ConstantList::iterator it = constants.begin();
0103     int at = 0;
0104     while (++at < index)
0105         ++it;
0106 
0107     QString constant = it.key();
0108 
0109     constantList->setCurrentIndex(0);
0110     edit->insertText(constant);
0111     edit->setFocus();
0112 }
0113 
0114 void EquationEditorWidget::characterButtonClicked()
0115 {
0116     const QToolButton *tb = static_cast<const QToolButton *>(sender());
0117 
0118     // Something (I can't work out what) is 'helpfully' inserting an ampersand (for keyboard acceleration).
0119     // Get rid of it.
0120     edit->insertText(tb->text().remove('&'));
0121 }
0122 
0123 #include "moc_equationeditorwidget.cpp"