diff --git a/plugins/formulashape/CMakeLists.txt b/plugins/formulashape/CMakeLists.txt index 4c0b7a43098..854ce45ad6d 100644 --- a/plugins/formulashape/CMakeLists.txt +++ b/plugins/formulashape/CMakeLists.txt @@ -1,107 +1,109 @@ include_directories( ${CMAKE_SOURCE_DIR}/plugins/formulashape ${CMAKE_SOURCE_DIR}/plugins/formulashape/elements ${QT_INCLUDES} ${KOMAIN_INCLUDES} ) ########### KFormula Library ############### set( kformulalib_PART_SRCS AttributeManager.cpp FormulaRenderer.cpp FormulaData.cpp FormulaCursor.cpp FormulaEditor.cpp FormulaCommand.cpp ElementFactory.cpp Dictionary.cpp elements/BasicElement.cpp elements/FixedElement.cpp elements/RowElement.cpp elements/FormulaElement.cpp elements/TextElement.cpp elements/FractionElement.cpp elements/SpaceElement.cpp elements/GlyphElement.cpp elements/IdentifierElement.cpp elements/OperatorElement.cpp elements/MultiscriptElement.cpp elements/SubSupElement.cpp elements/UnderOverElement.cpp elements/FencedElement.cpp elements/TableElement.cpp elements/TableRowElement.cpp elements/TableDataElement.cpp elements/RootElement.cpp elements/EncloseElement.cpp elements/ActionElement.cpp elements/PaddedElement.cpp elements/ErrorElement.cpp elements/StyleElement.cpp elements/TokenElement.cpp elements/NumberElement.cpp elements/StringElement.cpp elements/PhantomElement.cpp elements/SquareRootElement.cpp elements/AnnotationElement.cpp elements/UnknownElement.cpp ) kde4_add_library(kformulalib SHARED ${kformulalib_PART_SRCS}) target_link_libraries(kformulalib komain) target_link_libraries(kformulalib LINK_INTERFACE_LIBRARIES komain) set_target_properties(kformulalib PROPERTIES VERSION ${GENERIC_CALLIGRA_LIB_VERSION} SOVERSION ${GENERIC_CALLIGRA_LIB_SOVERSION} ) install(TARGETS kformulalib ${INSTALL_TARGETS_DEFAULT_ARGS}) ########### FormulaShape Plugin ############### find_package(Cauchy) macro_optional_find_package(Cauchy) macro_log_feature(M2MML_FOUND "Cauchy's M2MML" "Matlab/Octave to MathML compiler" "https://bitbucket.org/cyrille/cauchy" FALSE "" "Required for the matlab/octave formula tool") if(M2MML_FOUND) - set(M2MML_TOOL_SRCS KoM2MMLForumulaTool.cpp itexToMML/lex.yy.c itexToMML/y.tab.c) set(M2MML_TOOL_LIBRARIES ${M2MML_LIBRARY}) include_directories(${CAUCHY_INCLUDE_DIR}) add_definitions(-DHAVE_M2MML) endif() set( formulashape_PART_SRCS KoFormulaShapePlugin.cpp KoFormulaShape.cpp FormulaCommandUpdate.cpp KoFormulaShapeFactory.cpp KoFormulaTool.cpp FormulaCommand.cpp KoFormulaToolFactory.cpp FormulaToolWidget.cpp FormulaDocument.cpp FormulaPart.cpp - ${M2MML_TOOL_SRCS} + KoM2MMLForumulaTool.cpp + # itexToMML files + itexToMML/lex.yy.c + itexToMML/y.tab.c ) kde4_add_ui_files( formulashape_PART_SRCS FormulaToolWidget.ui ) kde4_add_plugin(formulashape ${formulashape_PART_SRCS}) target_link_libraries(formulashape kformulalib ${M2MML_TOOL_LIBRARIES}) #set_target_properties(kformulalib PROPERTIES VERSION ${GENERIC_CALLIGRA_LIB_VERSION} SOVERSION ${GENERIC_CALLIGRA_LIB_SOVERSION} ) install(TARGETS formulashape DESTINATION ${PLUGIN_INSTALL_DIR}) install(FILES formulashape.desktop DESTINATION ${SERVICES_INSTALL_DIR}) add_subdirectory( pics ) add_subdirectory( fonts ) add_subdirectory( tests ) add_subdirectory( templates ) install( FILES kformulapart.desktop DESTINATION ${SERVICES_INSTALL_DIR}) diff --git a/plugins/formulashape/KoFormulaShapePlugin.cpp b/plugins/formulashape/KoFormulaShapePlugin.cpp index 93b714dbcb5..4d58c3e3bd1 100644 --- a/plugins/formulashape/KoFormulaShapePlugin.cpp +++ b/plugins/formulashape/KoFormulaShapePlugin.cpp @@ -1,48 +1,44 @@ /* This file is part of the KDE project * Copyright (C) 2006 Martin Pfeiffer * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "KoFormulaShapePlugin.h" #include "KoFormulaToolFactory.h" #include "KoFormulaShapeFactory.h" #include #include #include -#ifdef HAVE_M2MML #include "KoM2MMLForumulaTool.h" -#endif K_PLUGIN_FACTORY(KoFormulaShapePluginFactory, registerPlugin();) K_EXPORT_PLUGIN(KoFormulaShapePluginFactory("FormulaShape")) KoFormulaShapePlugin::KoFormulaShapePlugin( QObject* parent, const QVariantList& ) : QObject( parent ) { KoToolRegistry::instance()->add( new KoFormulaToolFactory() ); -#ifdef HAVE_M2MML KoToolRegistry::instance()->add( new KoM2MMLFormulaToolFactory()); -#endif KoShapeRegistry::instance()->add( new KoFormulaShapeFactory() ); } KoFormulaShapePlugin::~KoFormulaShapePlugin() {} #include "KoFormulaShapePlugin.moc" diff --git a/plugins/formulashape/KoM2MMLForumulaTool.cpp b/plugins/formulashape/KoM2MMLForumulaTool.cpp index 5b02a0c1913..b0a9d2f8cd8 100644 --- a/plugins/formulashape/KoM2MMLForumulaTool.cpp +++ b/plugins/formulashape/KoM2MMLForumulaTool.cpp @@ -1,216 +1,230 @@ /* This file is part of the KDE project Copyright (C) 2011 Cyrille Berger This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "KoM2MMLForumulaTool.h" +#ifdef HAVE_M2MML #include +#endif + +#include #include #include #include #include #include #include #include #include #include #include #include #include "KoFormulaShape.h" #include "FormulaCommand.h" #include "FormulaCommandUpdate.h" #include "itexToMML/itex2MML.h" KoM2MMLFormulaTool::KoM2MMLFormulaTool(KoCanvasBase* canvas): KoToolBase(canvas), m_lineEdit(0), m_errorLabel(0), m_formulaShape(0), m_comboBox(0) { } void KoM2MMLFormulaTool::activate(KoToolBase::ToolActivation toolActivation, const QSet< KoShape* >& shapes) { foreach (KoShape *shape, shapes) { m_formulaShape = dynamic_cast( shape ); if( m_formulaShape ) break; } if( m_formulaShape == 0 ) // none found { emit done(); return; } FormulaElement* element = m_formulaShape->formulaData()->formulaElement(); foreach(BasicElement* elt, element->childElements()) { if(elt->elementType() == Annotation) { AnnotationElement* annot = static_cast(elt); m_text = annot->content(); m_mode = annot->attribute("mode"); } } if(m_lineEdit) { m_lineEdit->setText(m_text); } } void KoM2MMLFormulaTool::mouseMoveEvent(KoPointerEvent* event) { } void KoM2MMLFormulaTool::mousePressEvent(KoPointerEvent* event) { } void KoM2MMLFormulaTool::mouseReleaseEvent(KoPointerEvent* event) { } void KoM2MMLFormulaTool::paint(QPainter& painter, const KoViewConverter& converter) { } QWidget* KoM2MMLFormulaTool::createOptionWidget() { QWidget* widget = new QWidget; QVBoxLayout* layout = new QVBoxLayout; // Combobox to select between latex or matlab QLabel* modeLabel = new QLabel(i18n("Mode: ")); m_comboBox = new QComboBox; m_comboBox->addItem(i18n("LaTeX")); +#ifdef HAVE_M2MML m_comboBox->addItem(i18n("Matlab")); if(m_mode == "Matlab") { m_comboBox->setCurrentIndex(1); } +#endif QHBoxLayout* hlayout = new QHBoxLayout(); hlayout->addWidget(modeLabel); hlayout->addWidget(m_comboBox); layout->addLayout(hlayout); // Edit line widget->setLayout(layout); m_lineEdit = new QLineEdit(widget); layout->addWidget(m_lineEdit); // Error label m_errorLabel = new QLabel(widget); layout->addWidget(m_errorLabel); m_errorLabel->setText(""); layout->addSpacerItem(new QSpacerItem(0,0)); connect(m_lineEdit, SIGNAL(editingFinished()), SLOT(textEdited())); connect(m_lineEdit, SIGNAL(returnPressed()), SLOT(textEdited())); m_lineEdit->setText(m_text); return widget; } // Not sure why but the toStdString/fromStdString in QString are not accessible inline std::string QStringtoStdString(const QString& str) { const QByteArray asc = str.toAscii(); return std::string(asc.constData(), asc.length()); } inline QString QStringfromStdString(const std::string &s) { return QString::fromAscii(s.data(), int(s.size())); } void KoM2MMLFormulaTool::textEdited() { if(!m_formulaShape) return; if(!m_lineEdit) return; +#ifdef HAVE_M2MML if(m_comboBox->currentIndex() == 1) { std::string source = QStringtoStdString(m_lineEdit->text()); std::string mathml; std::string errmsg; if(m2mml(source, mathml, &errmsg)) { setMathML(QStringfromStdString(mathml), "Matlab"); } else { m_errorLabel->setText(QStringfromStdString(errmsg)); } } else { +#endif std::string source = QStringtoStdString(m_lineEdit->text()); source = "$" + source + "$"; char * mathml = itex2MML_parse (source.c_str(), source.size()); if(mathml) { setMathML(mathml, "LaTeX"); itex2MML_free_string(mathml); mathml = 0; } else { m_errorLabel->setText(i18n("Parse error.")); } +#ifdef HAVE_M2MML } +#endif } void KoM2MMLFormulaTool::setMathML(const QString& mathml, const QString& mode) { KoXmlDocument tmpDocument; tmpDocument.setContent( QString(mathml), false, 0, 0, 0 ); FormulaElement* formulaElement = new FormulaElement(); // create a new root element formulaElement->readMathML( tmpDocument.documentElement() ); // and load the new formula AnnotationElement* annot = new AnnotationElement; annot->setContent(m_lineEdit->text()); annot->setAttribute("mode", mode); formulaElement->insertChild(0, annot); kDebug() << mathml; canvas()->addCommand(new FormulaCommandUpdate(m_formulaShape, new FormulaCommandLoad(m_formulaShape->formulaData(), formulaElement))); m_errorLabel->setText(""); } KoM2MMLFormulaToolFactory::KoM2MMLFormulaToolFactory() : KoToolFactoryBase("KoM2MMLFormulaToolFactoryId") { +#ifdef HAVE_M2MML setToolTip( i18n( "Edit formula with LaTeX/Matlab syntax" ) ); +#else + setToolTip( i18n( "Edit formula with LaTeX syntax" ) ); +#endif setToolType( dynamicToolType() ); setIconName(koIconNameCStr("edittext")); setPriority( 1 ); setActivationShapeId( KoFormulaShapeId ); } KoM2MMLFormulaToolFactory::~KoM2MMLFormulaToolFactory() {} KoToolBase* KoM2MMLFormulaToolFactory::createTool( KoCanvasBase* canvas ) { return new KoM2MMLFormulaTool( canvas ); } #include "KoM2MMLForumulaTool.moc"