diff --git a/cmake/modules/FindCauchy.cmake b/cmake/modules/FindCauchy.cmake new file mode 100644 index 00000000000..02099aecf91 --- /dev/null +++ b/cmake/modules/FindCauchy.cmake @@ -0,0 +1,31 @@ +# - Try to find Cauchy lib +# Once done this will define +# +# CAUCHY_FOUND - tell whether cauchy is installed +# M2MML_FOUND - tell if the M2MML library is installed +# CAUCHY_INCLUDE_DIR - the cauchy include directory +# M2MML_LIBRARY - the M2MML library + +# Copyright (c) 2011, Cyrille Berger +# Redistribution and use is allowed according to the terms of the BSD license. + +FIND_PATH(CAUCHY_INCLUDE_DIR m2mml.h +/usr/local/include +/usr/include +) + +FIND_LIBRARY(M2MML_LIBRARY + NAMES m2mml + PATHS /usr/lib /usr/local/lib ) + +IF (CAUCHY_INCLUDE_DIR) + SET(CAUCHY_FOUND "YES") +ELSE () + SET(CAUCHY_FOUND "NO") +ENDIF () + +IF(CAUCHY_FOUND AND M2MML_LIBRARY) + SET(M2MML_FOUND "YES") +ELSE () + SET(M2MML_FOUND "NO") +ENDIF () diff --git a/plugins/formulashape/CMakeLists.txt b/plugins/formulashape/CMakeLists.txt index 8e11cf575e9..6edfcc6b418 100644 --- a/plugins/formulashape/CMakeLists.txt +++ b/plugins/formulashape/CMakeLists.txt @@ -1,93 +1,106 @@ 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/cyloncore/cauchy" FALSE "" "Required for the matlab/octave formul tool") + +if(M2MML_FOUND) + set(M2MML_TOOL_SRCS KoM2MMLForumulaTool.cpp) + 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 + ${M2MML_TOOL_SRCS} ) kde4_add_ui_files( formulashape_PART_SRCS FormulaToolWidget.ui ) kde4_add_plugin(formulashape ${formulashape_PART_SRCS}) -target_link_libraries(formulashape kformulalib) +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 f1998ade933..93b714dbcb5 100644 --- a/plugins/formulashape/KoFormulaShapePlugin.cpp +++ b/plugins/formulashape/KoFormulaShapePlugin.cpp @@ -1,41 +1,48 @@ /* 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 new file mode 100644 index 00000000000..404a7f407c4 --- /dev/null +++ b/plugins/formulashape/KoM2MMLForumulaTool.cpp @@ -0,0 +1,142 @@ +/* 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" + +#include "KoFormulaShape.h" +#include +#include +#include + +#include +#include +#include "FormulaCommand.h" +#include +#include +#include "FormulaCommandUpdate.h" + +KoM2MMLFormulaTool::KoM2MMLFormulaTool(KoCanvasBase* canvas): KoToolBase(canvas) +{ + +} + +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; + } +} + +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; + QHBoxLayout* layout = new QHBoxLayout; + widget->setLayout(layout); + m_lineEdit = new QLineEdit(widget); + layout->addWidget(m_lineEdit); + m_errorLabel = new QLabel(widget); + layout->addWidget(m_errorLabel); + m_errorLabel->setText(""); + + layout->addSpacerItem(); + + connect(m_lineEdit, SIGNAL(editingFinished()), SLOT(textEdited())); + connect(m_lineEdit, SIGNAL(returnPressed()), SLOT(textEdited())); + + 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; + + std::string source = QStringtoStdString(m_lineEdit->text()); + std::string mathml; + std::string errmsg; + + if(m2mml(source, mathml, &errmsg)) + { + KoXmlDocument tmpDocument; + tmpDocument.setContent( QStringfromStdString(mathml), false, 0, 0, 0 ); + FormulaElement* formulaElement = new FormulaElement(); // create a new root element + formulaElement->readMathML( tmpDocument.documentElement() ); // and load the new formula + + canvas()->addCommand(new FormulaCommandUpdate(m_formulaShape, new FormulaCommandLoad(m_formulaShape->formulaData(), formulaElement))); + m_errorLabel->setText(""); + } else { + m_errorLabel->setText(QStringfromStdString(errmsg)); + } + +} + +KoM2MMLFormulaToolFactory::KoM2MMLFormulaToolFactory() + : KoToolFactoryBase("KoM2MMLFormulaToolFactoryId") +{ + setToolTip( i18n( "Edit formula with matlab syntax" ) ); + setToolType( dynamicToolType() ); + setIcon( "edittext" ); + setPriority( 1 ); + setActivationShapeId( KoFormulaShapeId ); +} + +KoM2MMLFormulaToolFactory::~KoM2MMLFormulaToolFactory() +{} + +KoToolBase* KoM2MMLFormulaToolFactory::createTool( KoCanvasBase* canvas ) +{ + return new KoM2MMLFormulaTool( canvas ); +} + +#include "KoM2MMLForumulaTool.moc" diff --git a/plugins/formulashape/KoM2MMLForumulaTool.h b/plugins/formulashape/KoM2MMLForumulaTool.h new file mode 100644 index 00000000000..f140439c983 --- /dev/null +++ b/plugins/formulashape/KoM2MMLForumulaTool.h @@ -0,0 +1,63 @@ +/* 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. + */ + +#ifndef KOM2MMLFORMULATOOL_H_ +#define KOM2MMLFORMULATOOL_H_ + +#include +#include + +class QLabel; +class KoFormulaShape; +class QLineEdit; + +class KoM2MMLFormulaTool : public KoToolBase +{ + Q_OBJECT +public: + KoM2MMLFormulaTool(KoCanvasBase *canvas); + + virtual void activate(ToolActivation toolActivation, const QSet< KoShape* >& shapes); + + virtual void mouseReleaseEvent(KoPointerEvent* event); + virtual void mousePressEvent(KoPointerEvent* event); + virtual void mouseMoveEvent(KoPointerEvent* event); + virtual void paint(QPainter& painter, const KoViewConverter& converter); + virtual QWidget* createOptionWidget(); +public slots: + void textEdited(); +private: + QLineEdit* m_lineEdit; + QLabel* m_errorLabel; + KoFormulaShape* m_formulaShape; +}; + +class KoM2MMLFormulaToolFactory : public KoToolFactoryBase { +public: + /// The constructor - reimplemented from KoToolFactoryBase + explicit KoM2MMLFormulaToolFactory(); + + /// The destructor - reimplemented from KoToolFactoryBase + ~KoM2MMLFormulaToolFactory(); + + /// @return an instance of KoFormulaTool + KoToolBase* createTool( KoCanvasBase* canvas ); +}; + +#endif