diff --git a/CMakeLists.txt b/CMakeLists.txt index 764917d..291a69f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,44 +1,44 @@ project(LibKEduVocDocument) cmake_minimum_required(VERSION 3.5) find_package(ECM 1.3.0 REQUIRED NO_MODULE) set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH}) set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH}) find_package(Qt5 5.2 REQUIRED NO_MODULE COMPONENTS Xml Gui Widgets DBus Core Multimedia) -find_package(KF5 5.3.0 REQUIRED COMPONENTS I18n Archive KIO Completion Sonnet Notifications XmlGui) +find_package(KF5 5.3.0 REQUIRED COMPONENTS I18n Archive KIO Completion Sonnet Notifications XmlGui Kross ) #add_definitions( -Wall -fprofile-arcs -ftest-coverage ) include_directories(${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR} ) remove_definitions(-DQT_NO_CAST_FROM_ASCII) remove_definitions(-DQT_NO_CAST_FROM_BYTEARRAY) remove_definitions(-DQT_NO_KEYWORDS) add_definitions(-DQT_NO_CAST_TO_ASCII) add_definitions(-DQT_NO_URL_CAST_FROM_STRING) add_definitions(-DQT_USE_FAST_CONCATENATION -DQT_USE_FAST_OPERATOR_PLUS) add_definitions(-DTRANSLATION_DOMAIN=\"libkeduvocdocument\") add_definitions(-DKF_DISABLE_DEPRECATED_BEFORE_AND_AT=0x060000) include(KDEInstallDirs) include(KDECompilerSettings NO_POLICY_SCOPE) include(KDECMakeSettings) include(GenerateExportHeader) add_subdirectory(keduvocdocument) configure_file( "${LibKEduVocDocument_SOURCE_DIR}/LibKEduVocDocumentConfig.cmake.in" "${LibKEduVocDocument_BINARY_DIR}/LibKEduVocDocumentConfig.cmake" @ONLY ) set(_LibKEduVocDocument_CONFIG_DEST "${LIB_INSTALL_DIR}/cmake/libkeduvocdocument") install( FILES "${LibKEduVocDocument_BINARY_DIR}/LibKEduVocDocumentConfig.cmake" DESTINATION "${_LibKEduVocDocument_CONFIG_DEST}" ) install( EXPORT LibKEduVocDocumentTargets DESTINATION "${_LibKEduVocDocument_CONFIG_DEST}" NAMESPACE LibKEduVocDocumentImport__ FILE LibKEduVocDocumentTargets.cmake ) feature_summary(WHAT ALL INCLUDE_QUIET_PACKAGES FATAL_ON_MISSING_REQUIRED_PACKAGES) diff --git a/keduvocdocument/CMakeLists.txt b/keduvocdocument/CMakeLists.txt index 6e92243..b15ecb3 100644 --- a/keduvocdocument/CMakeLists.txt +++ b/keduvocdocument/CMakeLists.txt @@ -1,225 +1,231 @@ 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 keduvoclatexwidget.cpp + keduvocscript.cpp autosave/kautosavefile.cpp autosave/kautosavefileprivate.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 keduvoclatexwidget.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 KEduVocLatexWidget + KEduVocScript 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 + KF5::KrossCore + KF5::KrossUi ) # 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 + KF5::KrossCore + KF5::KrossUi ) # 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/keduvocscript.cpp b/keduvocdocument/keduvocscript.cpp new file mode 100644 index 0000000..912d08c --- /dev/null +++ b/keduvocdocument/keduvocscript.cpp @@ -0,0 +1,137 @@ +/*************************************************************************** + + Copyright 2008 Avgoustinos Kadis + + ***************************************************************************/ + +/*************************************************************************** + * * + * 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 "keduvocscript.h" + +#include +#include + +#include + + +class KEduVocScript::Private +{ +public: + Private( QString file ); + + bool m_activated; + QString m_file; + QPointer m_object; + QMap m_scriptObjects; + QString m_errorMessage; +}; + +KEduVocScript::Private::Private( QString file ) +{ + m_file = file; + m_activated = false; +} + +KEduVocScript::KEduVocScript( QString file ) + : d( new Private( file ) ) +{ +} + +KEduVocScript::~KEduVocScript() +{ + delete d; +} + + +bool KEduVocScript::isActivated() +{ + return d->m_activated; +} + + +void KEduVocScript::activate() +{ + qDebug() << "Activating Script" << fileName(); + if( isActivated() ) { + qDebug() << "Script already activated"; + return; + } + if( !exists() ) { + qDebug() << "Script file given does not exist"; + d->m_errorMessage = i18n( "The script file does not exist." ); + return; + } + + // Create the script container. m_object is the parent QObject, + // so that our action instance will be destroyed once the m_object + // is destroyed. + d->m_object = new QObject(); + Kross::Action* action = new Kross::Action( d->m_object, d->m_file); + // Publish our myobject instance and connect signals with + // scripting functions. + QMapIterator i( d->m_scriptObjects ); + while( i.hasNext() ) { + i.next(); + qDebug() << i.key(); + action->addObject( i.value() , i.key(), Kross::ChildrenInterface::AutoConnectSignals ); + } + + // Set the file to be execute + action->setFile( d->m_file ); + + // Execute the script. + action->trigger(); + + d->m_activated = !action->isFinalized(); + if( !d->m_activated ) { + qDebug() << "Script not activated"; + QString msg = action->errorMessage(); + QString trace = action->errorTrace(); + msg.replace( '<', "<" ).replace( '\n', "
" ); + trace.replace( '<', "<" ).replace( '\n', "
" ); + d->m_errorMessage = "

" + i18n( "Error in file %1 at line %2:", fileName(), + action->errorLineNo()) + "
" + msg + "
" + + i18nc( "debug information in error message", "Backtrace:" ) + + "
" + trace + "

"; + } +} + +void KEduVocScript::deactivate() +{ + delete d->m_object; + d->m_activated = false; +} + +bool KEduVocScript::exists() +{ + QFileInfo fileInfo( d->m_file ); + return fileInfo.exists(); +} + + +QString KEduVocScript::fileName() +{ + return d->m_file; +} + + +void KEduVocScript::addObject( QString name, QObject * object ) +{ + d->m_scriptObjects.insert( name, object ); +} + +void KEduVocScript::addObjects( QMap objects ) +{ + d->m_scriptObjects.unite( objects ); +} + +QString KEduVocScript::errorMessage() +{ + return d->m_errorMessage; +} diff --git a/keduvocdocument/keduvocscript.h b/keduvocdocument/keduvocscript.h new file mode 100644 index 0000000..2cdd6e4 --- /dev/null +++ b/keduvocdocument/keduvocscript.h @@ -0,0 +1,87 @@ +/*************************************************************************** + + Copyright 2008 Avgoustinos Kadis + + ***************************************************************************/ + +/*************************************************************************** + * * + * 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. * + * * + ***************************************************************************/ +#ifndef KEDUVOCSCRIPT_H +#define KEDUVOCSCRIPT_H + +#include +#include + +#include +#include +#include +#include + +/** + * This class represents the activated script and is used by the ScriptManager + * to activate/deactivate scripts. + * + * @author Avgoustinos Kadis + */ +class KEDUVOCDOCUMENT_EXPORT KEduVocScript: public QObject +{ + Q_OBJECT +public: + /** + * Script class constructor + * + * @param file The path to the script file to be activated (executed) + */ + KEduVocScript( QString file ); + ~KEduVocScript(); + /** + * Returns true if the script was successfully activated; false otherwise + */ + bool isActivated(); + /** + * Activates the script + */ + void activate(); + /** + * Deactivate the script + */ + void deactivate(); + /** + * Checks if the script file assigned to the Script object exists as a file on the + * given path. + * + * @return True if the script file exists + * False if it does not exist + */ + bool exists(); + /** + * Returns the file that was given as parameter to the constructor + */ + QString fileName(); + /** + * Add an object to be accessible by the script + * @param name Name to appear in the script + * @param object Object to be accessible by the script + */ + void addObject( QString name, QObject * object ); + /** + * Adds more than one scripting Objects to the script + * @param objects Map of the objects to add + */ + void addObjects( QMap objects ); + /** + * Returns an html error message if there have been errors in the script. + */ + QString errorMessage(); +private: + class Private; + Private * const d; +}; + +#endif diff --git a/keduvocdocument/keduvoctranslator.cpp b/keduvocdocument/keduvoctranslator.cpp index 6de3f5f..033b2e1 100644 --- a/keduvocdocument/keduvoctranslator.cpp +++ b/keduvocdocument/keduvoctranslator.cpp @@ -1,274 +1,273 @@ /*************************************************************************** Copyright 2008 Avgoustinos Kadis ***************************************************************************/ /*************************************************************************** * * * 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 "keduvoctranslator.h" #include #include #include #include #include #include #include namespace Scripting { - KEduVocDocument::KEduVocDocument( ::KEduVocDocument * doc, KEduVocVocabularyModel * vocabularyModel, QString * separator ) + KEduVocDocument::KEduVocDocument( ::KEduVocDocument * doc, KEduVocVocabularyModel * vocabularyModel, QString separator ) : QObject(), m_translator( new KEduVocTranslator( this ) ), m_doc( new ::KEduVocDocument( doc ) ), - m_vocabularyModel( vocabularyModel ), - m_separator(separator) + m_vocabularyModel( vocabularyModel ) { + m_separator = separator; } KEduVocDocument::~KEduVocDocument() { delete m_translator; delete m_doc; delete m_vocabularyModel; - delete m_separator; } void KEduVocDocument::callTranslateWord(const QString & word, const QString& fromLanguage, const QString& toLanguage) { emit translationStarted(word, fromLanguage, toLanguage); emit translateWord(word, fromLanguage, toLanguage); emit translationFinished(word, fromLanguage, toLanguage); } void KEduVocDocument::addTranslation(QString word, QString fromLanguage, QString toLanguage, QString translation) { if (m_translator) m_translator->addTranslation(word, fromLanguage, toLanguage, translation); } QStringList KEduVocDocument::locales() { /// @todo Change it into a QMap property (Parley.languageCodes) return QLocale().uiLanguages(); } QString KEduVocDocument::localeName(QString locale) { return QLocale( locale ).nativeLanguageName( ); } bool KEduVocDocument::open(QString filename) { QUrl url( QUrl::fromLocalFile(filename) ); qDebug() << url; if (url.path().isEmpty()) { return false; } close(); if (m_doc) { m_doc->deleteLater(); } m_doc = new ::KEduVocDocument(this); emit documentChanged(m_doc); - m_doc->setCsvDelimiter(*m_separator); + m_doc->setCsvDelimiter(m_separator); bool isSuccess = false, isError = false; ::KEduVocDocument::ErrorCode ret = m_doc->open(url, ::KEduVocDocument::FileDefaultHandling); switch ( ret ) { case ::KEduVocDocument::NoError : isSuccess = true; break; case ::KEduVocDocument::FileLocked : { QMessageBox warningBox; warningBox.setIcon( QMessageBox::Warning ); warningBox.setText( "Take Over Lock" ); warningBox.setInformativeText( "The vocabulary collection is locked by another process. You can open the file if you take over the lock, but you will lose any changes from the other process.\n\nDo you want to take over the lock?\n" ); warningBox.setStandardButtons( QMessageBox::Yes | QMessageBox::No ); int exit = warningBox.exec(); if ( exit == QMessageBox::Yes ) { //attempt to steal lock ret = m_doc->open(url, ::KEduVocDocument::FileIgnoreLock); if ( ret == ::KEduVocDocument::NoError ) { qDebug() << "Lock stolen"; isSuccess = true; } else { isError = true; } } else { //Don't Steal continue work without saving !!! } break; } default: isError = true; } if ( isSuccess ) { qDebug() << "Open success."; //m_parleyApp->editor()->updateDocument(); } else { if ( isError ) { QMessageBox errorBox; errorBox.setIcon( QMessageBox::Critical ); errorBox.setText( "Open Collection" ); errorBox.setInformativeText( "Opening collection " + m_doc->url().url() + " resulted in an error: " + m_doc->errorDescription( ret ) ); errorBox.setStandardButtons( QMessageBox::Ok ); errorBox.exec(); } m_doc->deleteLater(); emit documentChanged(0); m_doc = 0; } return isSuccess; } void KEduVocDocument::close() { if (m_doc) { emit documentChanged(0); m_doc->deleteLater(); m_doc = 0; } } //FIXME Fix errors related to conversion of KEduVocLesson* to QObject* /*KEduVocLesson* KEduVocDocument::activeLesson() { return m_vocabularyModel->lesson(); }*/ QVariantList KEduVocDocument::selectedEntries( QModelIndexList indexes ) { QVariantList entries; //get selected indexes and active lesson //get the unique selected entries QSet kentries; foreach(const QModelIndex & index, indexes) { // qDebug() << index.row() << index.data(Qt::DisplayRole); KEduVocExpression * expr = qvariant_cast (index.data(KEduVocVocabularyModel::EntryRole)); kentries << expr; } //convert them to Expression objects and add them to the QVariantList foreach(KEduVocExpression * expr, kentries) { // Expression entry(expr); // qDebug() << entry.translationTexts(); KEduVocExpression * obj = new KEduVocExpression( *expr ); entries << qVariantFromValue( obj ); } return entries; } QVariantList KEduVocDocument::selectedTranslations( QModelIndexList indexes ) { QVariantList translations; //get selected indexes and active lesson //get the unique selected entries QSet ktranslations; // const QModelIndex &index; foreach(const QModelIndex & index, indexes) { if (KEduVocVocabularyModel::columnType(index.column()) == KEduVocVocabularyModel::Translation) { KEduVocExpression * expr = qvariant_cast (index.data(KEduVocVocabularyModel::EntryRole)); ktranslations << expr->translation(KEduVocVocabularyModel::translation(index.column())); } // qDebug() << index.row() << index.data(Qt::DisplayRole); } //convert them to Expression objects and add them to the QVariantList foreach(KEduVocTranslation * tr, ktranslations) { // KEduVocTranslation transltion(tr); // qDebug() << entry.translationTexts(); KEduVocTranslation * obj = new KEduVocTranslation( *tr ); translations << qVariantFromValue(obj); } return translations; } } class KEduVocTranslator::Private { public: Private( QObject * parent ); QMap*> m_translations; QObject * m_parent; }; KEduVocTranslator::Private::Private( QObject * parent = 0 ) : m_parent( parent ) { } KEduVocTranslator::KEduVocTranslator() : d( new Private ) { } KEduVocTranslator::KEduVocTranslator(QObject * parent) : d( new Private( parent ) ) { } KEduVocTranslator::~KEduVocTranslator() { delete d; } void KEduVocTranslator::addTranslation(QString word, QString fromLanguage, QString toLanguage, QString translation) { if (word.trimmed() == "") return; QString t = word + fromLanguage + toLanguage; qDebug() << "Translation for " << word << "in cache: " << d->m_translations.contains(t); if (!d->m_translations.contains(t)) { d->m_translations[t] = new QSet(); } d->m_translations[t]->insert(translation.simplified()); } QSet* KEduVocTranslator::getTranslation(QString word, QString fromLanguage, QString toLanguage) { if (word.isEmpty() || fromLanguage.isEmpty() || toLanguage.isEmpty()) return 0; QString t = word + fromLanguage + toLanguage; qDebug() << "Fetch translation " << word << "(" << fromLanguage << "to" << toLanguage << ")" << "already in cache:" << d->m_translations.contains(t); /*if (!d->m_translations.contains(t)) { //FIXME Remove Parley dependancy and reimplement the commented code below Scripting::KEduVocDocument * p = dynamic_cast(d->m_parent); if (p) { p->callTranslateWord(word, fromLanguage, toLanguage); } }*/ if (d->m_translations.contains(t)) return d->m_translations.value(t); else return 0; } diff --git a/keduvocdocument/keduvoctranslator.h b/keduvocdocument/keduvoctranslator.h index 2829fbc..c7e03fe 100644 --- a/keduvocdocument/keduvoctranslator.h +++ b/keduvocdocument/keduvoctranslator.h @@ -1,372 +1,372 @@ /*************************************************************************** Copyright 2008 Avgoustinos Kadis ***************************************************************************/ /*************************************************************************** * * * 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. * * * ***************************************************************************/ #ifndef KEDUVOCTRANSLATOR_H #define KEDUVOCTRANSLATOR_H #include #include #include #include #include #include #include /** Keeps the translated words @author Avgoustinos Kadis */ class KEDUVOCDOCUMENT_EXPORT KEduVocTranslator { public: KEduVocTranslator(); KEduVocTranslator(QObject * parent); ~KEduVocTranslator(); /** * Stores the translation of @p word from language @p fromLanguage, to language @p toLanguage. * @param word word that was translated * @param fromLanguage language of @p word * @param toLanguage language of @p translation * @param translation translation of @p word */ void addTranslation(QString word, QString fromLanguage, QString toLanguage, QString translation); /** * Returns a QStringList with all the translations of @p word from @p fromLanguage to @p toLanguage. * This function will call the translateWord function of the translation scripts if this word * wasn't translated before. * @param word * @param fromLanguage * @param toLanguage * @return QStringList with the translations (or an empty QStringList if no translations found) */ QSet* getTranslation(QString word, QString fromLanguage, QString toLanguage); private: class Private; Private * const d; }; /*namespace Editor { class EditorWindow; }*/ namespace Scripting { /** * @class KEduVocDocument * @brief KEduVocDocument scripting class * * KEduVocDocument class is the main entry point of KEduVocDocument scripting classes. Through it you can access the Document class (KEduVocDocument.doc or KEduVocDocument.document) which provides functionality for viewing/modifying a %KEduVocDocument document , that means access lessons, entries, document languages etc. * * Here follows a list of possible usages of KEduVocDocument class: * * - Add a new Action to the script menu (see KEduVocDocument::newAction() function) * - Add a new translation script (see KEduVocDocument::translateWord() signal) * - Have direct access to the active lesson (see KEduVocDocument::activeLesson property) * - Have access to various enumerations (see KEduVocDocument::Number, KEduVocDocument::Case, KEduVocDocument::Person, KEduVocDocument::Gender and KeduVocDocument::Definiteness enumerations) * - Create a new %KEduVocDocument %Document (see KEduVocDocument::newDocument() function) * * Signals and Slots: To connect a script function (slot) to a signal you just define a function with the same name as the signal or use the KEduVocDocument.connect function: * @code * #how to connect a function to a signal (example with KEduVocDocument::translateWord() signal) * def translateFromInternet(word,fromLang,toLang): * print "Translating from Internet!!.." * * KEduVocDocument.connect("translateWord(const QString &,const QString &,const QString &)",translateFromInternet) * @endcode * * @author Avgoustinos Kadis */ - class KEduVocDocument : public QObject + class KEDUVOCDOCUMENT_EXPORT KEduVocDocument : public QObject { Q_OBJECT /// Read-only property of the active document Q_PROPERTY(QObject * document READ getDocument) /// Abreviation of document property (same as KEduVocDocument.document) Q_PROPERTY(QObject * doc READ getDocument) /// Currently active lesson //Q_PROPERTY(QObject * activeLesson READ activeLesson) // Q_ENUMS ( Number Case Person Gender Definiteness ) Q_ENUMS(Flags) public: enum Flags { // This is used for both empty flags and to denote no flags of the correct type were set. NoInformation = 0x0, // Gender Masculine = 0x1, Feminine = 0x2, Neuter = 0x4, // Plurality Singular = 0x10, Dual = 0x20, Plural = 0x40, // Part of Speech Verb = 0x100, Noun = 0x200, Pronoun = 0x400, Adjective = 0x800, Adverb = 0x1000, Article = 0x2000, Conjunction = 0x4000, // Person First = 0x10000, Second = 0x20000, Third = 0x40000, // Declension Case Nominative = 0x80000, Genitive = 0x100000, Dative = 0x200000, Accusative = 0x400000, Ablative = 0x800000, Locative = 0x1000000, Vocative = 0x2000000, // Other assorted flags Definite = 0x4000000, // The article is definite Indefinite = 0x8000000, // The article is indefinite Regular = 0x10000000, Irregular = 0x20000000 }; /** * @param doc Represents a KEduVococument object * For example, when using this function in Parley, pass the doc value like this: * m_editor->m_mainWindow->parleyDocument()->document() * where m_editor is a EditorWindow object * @param vocabularyModel Represents the VocabularyModel object for the KEduVocDocument * For example, when using this function in Parley, pass the vocabularyModel value like this: * m_editor->m_vocabularyModel * where m_editor is a EditorWindow object * @param separator Represents Prefs::separator() */ - KEduVocDocument( ::KEduVocDocument * doc, KEduVocVocabularyModel * vocabularyModel, QString * separator ); + KEduVocDocument( ::KEduVocDocument * doc, KEduVocVocabularyModel * vocabularyModel, QString separator ); ~KEduVocDocument(); /* * Emits translateWord signal so the script function connected to it translate the @p word * @param word Word to translate * @param fromLanguage The language of @p word to translate from * @param toLanguage The language you want to translate to */ void callTranslateWord(const QString & word, const QString& fromLanguage, const QString& toLanguage); //sets the translator object // void setTranslator ( Translator* translator ) { m_translator = translator; } KEduVocTranslator * translator() { return m_translator; } //Returns the active document QObject* getDocument() { return m_doc; } //Returns the active lesson //KEduVocLesson* activeLesson(); - QString * separator() + QString separator() { return m_separator; } - void setSeparator( QString* v ) + void setSeparator( QString v ) { m_separator = v; } public Q_SLOTS: QStringList dataDirs() { return QStandardPaths::standardLocations(QStandardPaths::DataLocation); } QStringList pluginDirs() { QStringList basedirs(QStandardPaths::standardLocations(QStandardPaths::DataLocation)); QStringList ret; foreach ( const QString dir , basedirs){ ret << (dir + "/plugins"); } return ret; } /** close the document*/ void close(); /** Returns a list of Expression objects (the selected entries of the active lesson) Usage example: While using in Parley, pass the 'indexes' value like this indexes = m_editor->m_vocabularyView->getSelectedIndexes(); where m_editor is a EditorWindow object TODO Add a KEduVocVocabularyView object to this class (after is is moved to keduvoc from parley) and access getSelectedIndexes() from there */ QVariantList selectedEntries( QModelIndexList indexes ); /** Returns a list of Translation objects (the selected translations of the active lesson) Usage example: While using in Parley, pass the 'indexes' value like this indexes = m_editor->m_vocabularyView->getSelectedIndexes(); where m_editor is a EditorWindow object TODO Add a KEduVocVocabularyView object to this class (after is is moved to keduvoc from parley) and access getSelectedIndexes() from there */ QVariantList selectedTranslations( QModelIndexList indexes ); /** * Adds the found @p translation of the @p word from language @p fromLanguage to language @p toLanguage to %KEduVocDocument translations to be used for translating lesson entries (or anything else). This function is ment to be used by scripts to add a translation of a word by parsing either online or offline dictionaries. * * @code * #example usage of addTranslation function * import KEduVocDocument * #function called by KEduVocDocument whenever a translation of a word is needed * def translateWord(word,fromLang,toLang): * <> * KEduVocDocument.addTranslation(word,fromLang,toLang,foundWord) * <> * @endcode * * @param word Translated word * @param fromLanguage From language * @param toLanguage To language * @param translation %Translation of word */ void addTranslation(QString word, QString fromLanguage, QString toLanguage, QString translation); /** * Returns a list of all available locales (to be used by the scripts) * @return */ QStringList locales(); /** * Gives the language name of the given @p locale * @param locale Language locale * @return Language name */ QString localeName(QString locale); /// @todo Make this function working (not very important function) /* * Open the KEduVocDocument Document @p filename * Usage: * @code * import KEduVocDocument * KEduVocDocument.open("Vocab/MyVocab.kvtml") * @endcode * @param file KEduVocDocument Document file path (ex. /home/kde-user/MyVocab.kvtml) */ bool open(QString filename); /** * Creates a new document and returns a reference to it * * @code * #how to create a new document, add lessons, add entries and save it to a kvtml file * import KEduVocDocument * * #create new document * doc = KEduVocDocument.newDocument() * doc.title = "New document" * * #set identifiers * doc.appendNewIdentifier("English","en_US") * doc.appendNewIdentifier("French","fr") * * #lessons * l1 = doc.newLesson("Lesson1") * doc.rootLesson.appendChildLesson(l1) * * #add a new entry (first way) * e = l1.newEntry() * e.setTranslation(0,"dog") * e.setTranslation(1,"chien") * l1.appendEntry(e) * * #add a new entry (second way) * ee = l1.newEntry(["glass","verre"]) * l1.appendEntry(ee) * #third way * ee = l1.appendNewEntry(["book","livre"]) * * #add a new lesson (fast way) * l2 = doc.appendNewLesson("Lesson 2") * * #add a new child lesson under "Lesson 2" * l3 = doc.appendNewLesson("Lesson 3",l2) * * #add a new entry (third way) * l2.appendNewEntry(["I","je"]); * l3.appendNewEntry(["good morning","bonjour"]) * * #save document * doc.saveAs("/home/kde-devel/test_new_document.kvtml") * @endcode * * @return A Document object, the newly created lesson */ QObject * newDocument() { return new ::KEduVocDocument(); } Q_SIGNALS: /** * Slots (script functions) connected to this signal are called when a translation of @p word is requested. Note that a script function with the same name as a signal will be automatically connected to that signal when the script is activated. * * @code * #example usage of translateWord signal * import KEduVocDocument * #function called by KEduVocDocument whenever a translation of a word is needed * def translateWord(word,fromLang,toLang): * <> * KEduVocDocument.addTranslation(word,fromLang,toLang,foundWord) * <> * @endcode * * @param word Word to translate * @param fromLanguage The language of @p word to translate from * @param toLanguage The language you want to translate to */ void translateWord(const QString & word, const QString& fromLanguage, const QString& toLanguage); /* Emits when the translation of a word is finished (from all the scripts) [not to be used by scripts] */ void translationFinished(const QString & word, const QString& fromLanguage, const QString& toLanguage); /* Emits when the translation of a word starts [not to be used by scripts] */ void translationStarted(const QString & word, const QString& fromLanguage, const QString& toLanguage); /** Emitted when the document pointer is changed. */ void documentChanged(::KEduVocDocument *newDocument); private: KEduVocTranslator* m_translator; ::KEduVocDocument* m_doc; KEduVocVocabularyModel * m_vocabularyModel; - QString * m_separator; + QString m_separator; }; } Q_DECLARE_METATYPE( KEduVocTranslation* ) #endif