Commit fb056a15 authored by Eckhart Wörner's avatar Eckhart Wörner

Refactor EquationEditor and EquationEditorWidget

Let EquationEditorWidget take care of its own stuff

svn path=/trunk/KDE/kdeedu/kmplot/; revision=902614
parent 29d96a03
......@@ -147,6 +147,7 @@ class EquationEdit : public QWidget
static CharMap m_replaceMap;
friend class EquationEditor;
friend class EquationEditorWidget;
friend class EquationEditWidget;
friend class EquationHighlighter;
};
......
......@@ -24,13 +24,8 @@
#include "equationeditor.h"
#include <KAcceleratorManager>
#include "equationeditorwidget.h"
#include "equationeditwidget.h"
#include "maindlg.h"
#include "xparser.h"
EquationEditor::EquationEditor(QWidget* parent)
......@@ -46,53 +41,7 @@ EquationEditor::EquationEditor(QWidget* parent)
setButtons(Close);
showButtonSeparator(true);
QFont font;
double pointSize = font.pointSizeF() * 1.1;
font.setPointSizeF(pointSize);
m_widget->edit->m_equationEditWidget->document()->setDefaultFont(font);
m_widget->edit->m_equationEditWidget->recalculateGeometry();
QFont buttonFont;
buttonFont.setPointSizeF(font.pointSizeF() * 1.1);
QList<QToolButton*> buttons = m_widget->findChildren<QToolButton*>();
foreach (QToolButton * w, buttons) {
KAcceleratorManager::setNoAccel(w);
connect(w, SIGNAL(clicked()), this, SLOT(characterButtonClicked()));
// Also increase the font size, since the fractions, etc are probably not that visible
// at the default font size
w->setFont(buttonFont);
}
connect(m_widget->constantsButton, SIGNAL(clicked()), MainDlg::self(), SLOT(editConstants()));
connect(m_widget->functionList, SIGNAL(activated(const QString &)), this, SLOT(insertFunction(const QString &)));
connect(m_widget->constantList, SIGNAL(activated(int)), this, SLOT(insertConstant(int)));
m_widget->functionList->addItems(XParser::self()->predefinedFunctions(false));
connect(m_widget->edit, SIGNAL(returnPressed()), this, SLOT(accept()));
connect(XParser::self()->constants(), SIGNAL(constantsChanged()), this, SLOT(updateConstantList()));
updateConstantList();
}
void EquationEditor::updateConstantList() {
QStringList items;
// The first item text is "Insert constant..."
items << m_widget->constantList->itemText(0);
ConstantList constants = XParser::self()->constants()->list(Constant::All);
for (ConstantList::iterator it = constants.begin(); it != constants.end(); ++it) {
QString text = it.key() + " = " + it.value().value.expression();
items << text;
}
m_widget->constantList->clear();
m_widget->constantList->addItems(items);
}
......@@ -104,46 +53,3 @@ QString EquationEditor::text() const {
EquationEdit* EquationEditor::edit() const {
return m_widget->edit;
}
void EquationEditor::insertFunction(const QString& function) {
if (m_widget->functionList->currentIndex() == 0)
return;
m_widget->functionList->setCurrentIndex(0);
m_widget->edit->wrapSelected(function + '(', ")");
m_widget->edit->setFocus();
}
void EquationEditor::insertConstant(int index) {
if (index == 0)
return;
ConstantList constants = XParser::self()->constants()->list(Constant::All);
if (constants.size() < index)
return;
// Don't forget that index==0 corresponds to "Insert constant..."
ConstantList::iterator it = constants.begin();
int at = 0;
while (++at < index)
++it;
QString constant = it.key();
m_widget->constantList->setCurrentIndex(0);
m_widget->edit->insertText(constant);
m_widget->edit->setFocus();
}
void EquationEditor::characterButtonClicked() {
const QToolButton* tb = static_cast<const QToolButton*>(sender());
// Something (I can't work out what) is 'helpfully' inserting an ampersand (for keyboard acceleration).
// Get rid of it.
m_widget->edit->insertText(tb->text().remove('&'));
}
......@@ -50,15 +50,6 @@ class EquationEditor : public KDialog
EquationEdit* edit() const;
protected slots:
void insertFunction(const QString& text);
/**
* \p index is the position of the constant in the constants combo box.
*/
void insertConstant(int index);
void characterButtonClicked();
void updateConstantList();
protected:
EquationEditorWidget* m_widget;
};
......
......@@ -24,10 +24,104 @@
#include "equationeditorwidget.h"
#include <KAcceleratorManager>
#include "equationeditwidget.h"
#include "maindlg.h"
#include "xparser.h"
EquationEditorWidget::EquationEditorWidget(QWidget* parent)
: QWidget(parent)
{
setupUi(this);
QFont font;
double pointSize = font.pointSizeF() * 1.1;
font.setPointSizeF(pointSize);
edit->m_equationEditWidget->document()->setDefaultFont(font);
edit->m_equationEditWidget->recalculateGeometry();
QFont buttonFont;
buttonFont.setPointSizeF(font.pointSizeF() * 1.1);
QList<QToolButton*> buttons = findChildren<QToolButton*>();
foreach (QToolButton* w, buttons) {
KAcceleratorManager::setNoAccel(w);
connect(w, SIGNAL(clicked()), this, SLOT(characterButtonClicked()));
// Also increase the font size, since the fractions, etc are probably not that visible
// at the default font size
w->setFont(buttonFont);
}
connect(constantsButton, SIGNAL(clicked()), MainDlg::self(), SLOT(editConstants()));
connect(functionList, SIGNAL(activated(const QString&)), this, SLOT(insertFunction(const QString&)));
connect(constantList, SIGNAL(activated(int)), this, SLOT(insertConstant(int)));
functionList->addItems(XParser::self()->predefinedFunctions(false));
connect(XParser::self()->constants(), SIGNAL(constantsChanged()), this, SLOT(updateConstantList()));
updateConstantList();
}
void EquationEditorWidget::updateConstantList() {
QStringList items;
// The first item text is "Insert constant..."
items << constantList->itemText(0);
ConstantList constants = XParser::self()->constants()->list(Constant::All);
for (ConstantList::iterator it = constants.begin(); it != constants.end(); ++it) {
QString text = it.key() + " = " + it.value().value.expression();
items << text;
}
constantList->clear();
constantList->addItems(items);
}
void EquationEditorWidget::insertFunction(const QString& function) {
if (functionList->currentIndex() == 0)
return;
functionList->setCurrentIndex(0);
edit->wrapSelected(function + '(', ")");
edit->setFocus();
}
void EquationEditorWidget::insertConstant(int index) {
if (index == 0)
return;
ConstantList constants = XParser::self()->constants()->list(Constant::All);
if (constants.size() < index)
return;
// Don't forget that index==0 corresponds to "Insert constant..."
ConstantList::iterator it = constants.begin();
int at = 0;
while (++at < index)
++it;
QString constant = it.key();
constantList->setCurrentIndex(0);
edit->insertText(constant);
edit->setFocus();
}
#include "equationedit.moc"
void EquationEditorWidget::characterButtonClicked() {
const QToolButton* tb = static_cast<const QToolButton*>(sender());
// Something (I can't work out what) is 'helpfully' inserting an ampersand (for keyboard acceleration).
// Get rid of it.
edit->insertText(tb->text().remove('&'));
}
......@@ -30,8 +30,19 @@
class EquationEditorWidget : public QWidget, public Ui::EquationEditorWidget
{
Q_OBJECT
public:
EquationEditorWidget(QWidget* parent = 0);
protected slots:
void insertFunction(const QString& text);
/**
* \p index is the position of the constant in the constants combo box.
*/
void insertConstant(int index);
void characterButtonClicked();
void updateConstantList();
};
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment