diff --git a/keduvocdocument/CMakeLists.txt b/keduvocdocument/CMakeLists.txt index 4c7aa8e..48aed16 100644 --- a/keduvocdocument/CMakeLists.txt +++ b/keduvocdocument/CMakeLists.txt @@ -1,218 +1,220 @@ if (BUILD_TESTING) add_subdirectory(autotests) add_subdirectory(tests) endif() ########### next target ############### include(ECMSetupVersion) ecm_setup_version(5.0.0 VARIABLE_PREFIX KDEEDU VERSION_HEADER "${CMAKE_BINARY_DIR}/keduvocdocument/keduvocdocument_version.h" PACKAGE_VERSION_FILE "${CMAKE_BINARY_DIR}/KEduVocDocumentVersion.cmake") include_directories( ${CMAKE_CURRENT_BINARY_DIR} ) set(keduvocdocument_LIB_SRCS keduvocdocument.cpp keduvocidentifier.cpp keduvocexpression.cpp keduvoctranslation.cpp keduvoccontainer.cpp keduvoclesson.cpp keduvocleitnerbox.cpp keduvoctext.cpp keduvocarticle.cpp keduvocconjugation.cpp keduvocpersonalpronoun.cpp keduvocdeclension.cpp keduvocwordtype.cpp keduvockvtmlcompability.cpp keduvockvtml2writer.cpp keduvoccsvwriter.cpp keduvoccontainermodel.cpp keduvoclessonmodel.cpp keduvocreadonlycontainermodel.cpp keduvocvocabularymodel.cpp keduvocwordclassmodel.cpp keduvoccontainermimedata.cpp keduvocvocabularymimedata.cpp keduvocvocabularyfilter.cpp keduvoctranslator.cpp keduvocvocabularydelegate.cpp keduvocvocabularyheaderview.cpp keduvocvocabularycolumnsdialog.cpp keduvoccontainerview.cpp keduvocvocabularyview.cpp keduvocwordtypeview.cpp keduvoclessonview.cpp keduvocaudiowidget.cpp keduvocmultiplechoicewidget.cpp keduvoccomparisonwidget.cpp keduvocdeclensionwidget.cpp keduvocconjugationwidget.cpp keduvocinflectionwidget.cpp keduvocimagewidget.cpp keduvocimagechooserwidget.cpp keduvocsynonymwidget.cpp keduvocsummarywordwidget.cpp + keduvoclatexrenderer.cpp readerwriters/dummyreader.cpp readerwriters/failedreader.cpp readerwriters/keduvockvtmlreader.cpp readerwriters/keduvockvtml2reader.cpp readerwriters/keduvoccsvreader.cpp readerwriters/keduvocpaukerreader.cpp readerwriters/keduvocvokabelnreader.cpp readerwriters/keduvocwqlreader.cpp readerwriters/keduvocxdxfreader.cpp readerwriters/readermanager.cpp sharedkvtmlfiles.cpp ) ki18n_wrap_ui(keduvocdocument_LIB_SRCS keduvocaudiowidget.ui keduvocmultiplechoicewidget.ui keduvoccomparisonwidget.ui keduvocdeclensionwidget.ui keduvocconjugationwidget.ui keduvocimagechooserwidget.ui keduvocsynonymwidget.ui keduvocsummarywordwidget.ui ) include(ECMGenerateHeaders) ecm_generate_headers( KdeEdu_HEADERS HEADER_NAMES KEduVocDocument KEduVocIdentifier KEduVocExpression KEduVocText KEduVocArticle KEduVocConjugation KEduVocLesson KEduVocLeitnerBox KEduVocContainer KEduVocWordFlags KEduVocMultipleChoice KEduVocTranslation KEduVocWordtype KEduVocPersonalPronoun SharedKVTMLFiles KEduVocDeclension KEduVocKVTML2Writer KEduVocContainerModel KEduVocLessonModel KEduVocReadOnlyContainerModel KEduVocVocabularyModel KEduVocWordClassModel KEduVocContainerMimeData KEduVocVocabularyMimeData KEduVocVocabularyFilter KEduVocTranslator KEduVocVocabularyDelegate KEduVocVocabularyHeaderView KEduVocVocabularyColumnsDialog KEduVocContainerView KEduVocVocabularyView KEduVocWordTypeView KEduVocLessonView KEduVocAudioWidget KEduVocMultipleChoiceWidget KEduVocComparisonWidget KEduVocDeclensionWidget KEduVocConjugationWidget KEduVocInflectionWidget KEduVocImageWidget KEduVocImageChooserWidget KEduVocSynonymWidget KEduVocSummaryWordWidget + KEduVocLatexRenderer REQUIRED_HEADERS KdeEdu_HEADERS ) add_library(KEduVocDocument SHARED ${keduvocdocument_LIB_SRCS}) generate_export_header(KEduVocDocument BASE_NAME KEduVocDocument) # vHanda: Add library alias? target_link_libraries(KEduVocDocument PUBLIC Qt5::Xml Qt5::Gui Qt5::Core Qt5::Widgets Qt5::DBus Qt5::Multimedia PRIVATE KF5::I18n KF5::KIOCore KF5::KIOFileWidgets KF5::KIOWidgets KF5::KIONTLM KF5::Archive KF5::Completion KF5::WidgetsAddons KF5::SonnetCore KF5::SonnetUi KF5::Notifications KF5::XmlGui ) # vHanda: Add Export Name? set_target_properties(KEduVocDocument PROPERTIES VERSION ${KDEEDU_VERSION_STRING} SOVERSION ${KDEEDU_SOVERSION} ) target_include_directories(KEduVocDocument INTERFACE "$") #Library for non-api unittests add_library(keduvocdocument_static STATIC ${keduvocdocument_LIB_SRCS}) set_target_properties(keduvocdocument_static PROPERTIES COMPILE_FLAGS -DKEDUVOCDOCUMENT_STATIC_DEFINE) target_link_libraries(keduvocdocument_static PUBLIC KF5::KIOCore Qt5::Xml Qt5::Core Qt5::Gui Qt5::Widgets Qt5::DBus Qt5::Multimedia PRIVATE KF5::I18n KF5::KIOCore KF5::KIOFileWidgets KF5::KIOWidgets KF5::KIONTLM KF5::Archive KF5::Completion KF5::WidgetsAddons KF5::SonnetCore KF5::SonnetUi KF5::Notifications KF5::XmlGui ) # if we want to set our own version instead of following kde generic #set(LIB_KEDUVOCDOCUMENT_VERSION "5.0.0") #set(LIB_KEDUVOCDOCUMENT_SOVERSION "5") #set_target_properties(keduvocdocument # PROPERTIES VERSION ${LIB_KEDUVOCDOCUMENT_VERSION} # SOVERSION ${LIB_KEDUVOCDOCUMENT_SOVERSION} #) install(TARGETS KEduVocDocument EXPORT LibKEduVocDocumentTargets ${INSTALL_TARGETS_DEFAULT_ARGS}) ########### install files ############### install(FILES ${CMAKE_CURRENT_BINARY_DIR}/keduvocdocument_export.h ${KdeEdu_HEADERS} DESTINATION ${INCLUDE_INSTALL_DIR}/libkeduvocdocument COMPONENT Devel ) diff --git a/keduvocdocument/keduvoclatexrenderer.cpp b/keduvocdocument/keduvoclatexrenderer.cpp new file mode 100644 index 0000000..eaa2b69 --- /dev/null +++ b/keduvocdocument/keduvoclatexrenderer.cpp @@ -0,0 +1,180 @@ +/*************************************************************************** + Copyright 2010 Daniel Laidig + Copyright 2009 Alexander Rieder + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#include "keduvoclatexrenderer.h" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace Practice; + +const char* texTemplate = "\\documentclass[12pt,fleqn]{article} \n "\ + "\\usepackage{latexsym,amsfonts,amssymb,ulem} \n "\ + "\\usepackage[dvips]{graphicx} \n "\ + "\\setlength\\textwidth{5in} \n "\ + "\\setlength{\\parindent}{0pt} \n "\ + "\\usepackage{amsmath} \n "\ + "\\usepackage{color} \n "\ + "\\pagestyle{empty} \n "\ + "\\begin{document} \n "\ + "{\\definecolor{mycolor}{rgb}{%1} \n "\ + "{\\color{mycolor} \n "\ + "%2 } \n "\ + "\\end{document}\n"; + + +class KEduVocLatexRenderer::Private +{ +public: + Private(); + + QLabel *m_label; + QString m_latexFilename; +}; + +KEduVocLatexRenderer::Private::Private() +{ + m_label = 0; +} + +KEduVocLatexRenderer::KEduVocLatexRenderer( QObject* parent ) + : QObject(parent) + , d( new Private ) +{ +} + +KEduVocLatexRenderer::~KEduVocLatexRenderer() +{ + delete d; +} + +void KEduVocLatexRenderer::setResultLabel( QLabel *label ) { + d->m_label = label; +} + +void KEduVocLatexRenderer::renderLatex( QString tex ) +{ + if( d->m_label ) { + d->m_label->setText( i18n( "Rendering..." ) ); + d->m_label->setToolTip( tex ); + } + + if( tex.startsWith( QLatin1String( "$$" ) ) ) { + tex.replace( 0, 2, "\\begin{eqnarray*}").remove( -2, 2 ).append( "\\end{eqnarray*}" ); + } else { + tex.remove( 0, 2 ).remove( -2, 2 ); + } + qDebug() << "rendering as latex"; + + //Check if the parley subdir exists, if not, create it + QString dir( QDir::tempPath() + QLatin1Char('/') + QCoreApplication::applicationName() ); + QTemporaryFile *texFile = new QTemporaryFile( dir + QLatin1Char('/') + QLatin1String( "XXXXXX" ) + ".tex" ); + if( !texFile->open() ) { + return; + } + + QColor color = d->m_label->palette().color( QPalette::WindowText ); + QString colorString = QString::number( color.redF() ) + ',' + + QString::number( color.greenF() ) + ',' + + QString::number( color.blueF() ); + QString expressionTex = QString( texTemplate ).arg( colorString, tex.trimmed() ); + + texFile->write( expressionTex.toUtf8() ); + texFile->flush(); + + QString fileName = texFile->fileName(); + qDebug() << "fileName: " << fileName; + d->m_latexFilename = fileName; + d->m_latexFilename.replace( ".tex", ".eps" ); + KProcess *p = new KProcess( this ); + p->setWorkingDirectory( dir ); + + (*p) << "latex" << "-interaction=batchmode" << "-halt-on-error" << fileName; + + connect( p, SIGNAL( finished( int, QProcess::ExitStatus ) ), this, SLOT( convertToPs() ) ); + connect( p, SIGNAL( error( QProcess::ProcessError ) ), this, SLOT( latexRendered() ) ); + p->start(); +} + +bool KEduVocLatexRenderer::isLatex( const QString& tex ) +{ + return tex.length() > 4 && tex.mid( 2, tex.length() - 4 ).simplified().length() > 0 && + ( ( tex.startsWith( QLatin1String( "$$" ) ) && tex.endsWith( QLatin1String( "$$" ) ) ) || + ( tex.startsWith( QString::fromUtf8( "§§" ) ) && tex.endsWith( QString::fromUtf8( "§§" ) ) ) ); +} + +void KEduVocLatexRenderer::convertToPs() +{ + qDebug() << "converting to ps"; + QString dviFile = d->m_latexFilename; + dviFile.replace( ".eps", ".dvi" ); + KProcess *p = new KProcess( this ); + qDebug() << "running: " << "dvips" << "-E" << "-o" << d->m_latexFilename << dviFile; + (*p) << "dvips" << "-E" << "-o" << d->m_latexFilename << dviFile; + + connect( p, SIGNAL( finished( int, QProcess::ExitStatus ) ), this, SLOT( convertToImage() ) ); + connect( p, SIGNAL( error( QProcess::ProcessError ) ), this, SLOT( latexRendered() ) ); + p->start(); +} + +void KEduVocLatexRenderer::convertToImage() +{ + qDebug() << "converting to ps"; + QString pngFile = d->m_latexFilename; + pngFile.replace( ".eps", ".png" ); + KProcess *p = new KProcess( this ); + qDebug() << "running:" << "convert" << d->m_latexFilename << pngFile; + (*p) << "convert" << "-density" << "85" << d->m_latexFilename << pngFile; + + connect( p, SIGNAL( finished( int, QProcess::ExitStatus ) ), this, SLOT( latexRendered() ) ); + connect( p, SIGNAL( error( QProcess::ProcessError ) ), this, SLOT( latexRendered() ) ); + p->start(); +} + +void KEduVocLatexRenderer::latexRendered() +{ + qDebug() << "rendered file " << d->m_latexFilename; + + QString pngFile = d->m_latexFilename; + pngFile.replace( ".eps", ".png" ); + if( QFileInfo( pngFile ).exists() ) { + QPixmap pixmap( pngFile ); + d->m_label->setPixmap( pixmap ); + d->m_label->setMinimumSize( pixmap.size().boundedTo( QSize( 600, 300 ) ) ); + } else { + d->m_label->setText( i18n( "LaTeX error." ) ); //TODO: better error handling and error messages + } + + //cleanup the temp directory a bit... + QString dir( QDir::tempPath() + QLatin1Char('/') + QCoreApplication::applicationName() ); + + QStringList extensions; + extensions << ".log" << ".aux" << ".tex" << ".dvi" << ".eps" << ".png"; + foreach( const QString & ext, extensions ) { + QString s = d->m_latexFilename; + s.replace( ".eps", ext ); + QFile f( s ); + f.remove(); + } +} diff --git a/keduvocdocument/keduvoclatexrenderer.h b/keduvocdocument/keduvoclatexrenderer.h new file mode 100644 index 0000000..578149b --- /dev/null +++ b/keduvocdocument/keduvoclatexrenderer.h @@ -0,0 +1,54 @@ +/*************************************************************************** + Copyright 2010 Daniel Laidig + Copyright 2009 Alexander Rieder + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +// based on Cantor's LaTeX rendering code + +#ifndef KEDUVOCLATEXRENDERER_H +#define KEDUVOCLATEXRENDERER_H + +#include + +#include + +class QLabel; + +namespace Practice +{ + +class KEDUVOCDOCUMENT_EXPORT KEduVocLatexRenderer : public QObject +{ + Q_OBJECT + +public: + KEduVocLatexRenderer( QObject* parent = 0 ); + ~KEduVocLatexRenderer(); + + void setResultLabel( QLabel *label ); + void renderLatex(QString tex); + + static bool isLatex( const QString& tex ); + +private slots: + void convertToPs(); + void convertToImage(); + void latexRendered(); + +private: + class Private; + Private * const d; +}; + +} + +#endif // PRACTICE_LATEXRENDERER_H