[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [cantor/labplot-integration] src: add an interface for applications that embed the cantor part to in
From:       Alexander Rieder <alexanderrieder () gmail ! com>
Date:       2015-07-18 21:08:34
Message-ID: E1ZGZLW-0007BK-6C () scm ! kde ! org
[Download RAW message or body]

Git commit 789630ecc9adab0fcbb8e0aa49c930aac4dba8ef by Alexander Rieder.
Committed on 18/07/2015 at 14:34.
Pushed by arieder into branch 'labplot-integration'.

add an interface for applications that embed the cantor part to interact
more closely with the worksheet

the worksheet api is not public, but labplot integrate the cantor kpart and
needs more direct access to some functionality. The new WorksheetAccessInterface
offers a (stable) api for most common actions without exposing implementation
details of the worksheet

M  +21   -1    src/cantor.cpp
M  +5    -0    src/cantor.h
M  +55   -1    src/cantor_part.cpp
M  +2    -0    src/cantor_part.h
M  +3    -2    src/lib/CMakeLists.txt
M  +2    -0    src/lib/extension.h
A  +39   -0    src/lib/worksheetaccess.cpp     [License: GPL (v2+)]
A  +59   -0    src/lib/worksheetaccess.h     [License: GPL (v2+)]
M  +40   -3    src/worksheet.cpp
M  +4    -0    src/worksheet.h

http://commits.kde.org/cantor/789630ecc9adab0fcbb8e0aa49c930aac4dba8ef

diff --git a/src/cantor.cpp b/src/cantor.cpp
index 8035bdf..79e1844 100644
--- a/src/cantor.cpp
+++ b/src/cantor.cpp
@@ -52,7 +52,7 @@
 #include "lib/backend.h"
 #include "lib/panelpluginhandler.h"
 #include "lib/panelplugin.h"
-
+#include "lib/worksheetaccess.h"
 
 #include "settings.h"
 #include "ui_settings.h"
@@ -585,3 +585,23 @@ void CantorShell::updatePanel()
     }
     plugActionList(QLatin1String("new_worksheet_with_backend_list"), \
newBackendActions);  }
+
+Cantor::WorksheetAccessInterface* CantorShell::currentWorksheetAccessInterface()
+{
+    Cantor::WorksheetAccessInterface* \
wa=m_part->findChild<Cantor::WorksheetAccessInterface*>(Cantor::WorksheetAccessInterface::Name);
 +
+    if (!wa)
+        qDebug()<<"failed to access worksheet access interface for current part";
+
+    return wa;
+}
+
+
+
+
+
+
+
+
+
+
diff --git a/src/cantor.h b/src/cantor.h
index 918b00f..95ad83b 100644
--- a/src/cantor.h
+++ b/src/cantor.h
@@ -30,6 +30,10 @@
 class QTabWidget;
 class KTextEdit;
 
+namespace Cantor{
+class WorksheetAccessInterface;
+};
+
 /**
  * This is the application "Shell".  It has a menubar, toolbar, and
  * statusbar but relies on the "Part" to do all the real work.
@@ -58,6 +62,7 @@ public:
      */
     bool hasAvailableBackend();
 
+    Cantor::WorksheetAccessInterface* currentWorksheetAccessInterface();
 protected:
     /**
      * This method is called when it is time for the app to save its
diff --git a/src/cantor_part.cpp b/src/cantor_part.cpp
index 2e3d36d..2b0b1be 100644
--- a/src/cantor_part.cpp
+++ b/src/cantor_part.cpp
@@ -58,6 +58,7 @@
 #include "lib/assistant.h"
 #include "lib/panelpluginhandler.h"
 #include "lib/panelplugin.h"
+#include "lib/worksheetaccess.h"
 
 #include "settings.h"
 
@@ -66,16 +67,64 @@ K_EXPORT_PLUGIN(CantorPartFactory("cantor"))
 
 #include "cantor_part.moc"
 
+
+//A concrete implementation of the WorksheetAccesssInterface
+class WorksheetAccessInterfaceImpl : public Cantor::WorksheetAccessInterface
+{
+    //Q_OBJECT
+  public:
+    WorksheetAccessInterfaceImpl(QObject* parent, Worksheet* worksheet) :   \
WorksheetAccessInterface(parent),  m_worksheet(worksheet) +    {
+        qDebug()<<"new worksheetaccess interface";
+        connect(worksheet, SIGNAL(sessionChanged()), this, \
SIGNAL(sessionChanged())); +
+    }
+
+    ~WorksheetAccessInterfaceImpl()
+    {
+
+    }
+
+    virtual QByteArray saveWorksheetToByteArray()
+    {
+        return m_worksheet->saveToByteArray();
+    }
+
+    virtual void loadWorksheetFromByteArray(QByteArray* data)
+    {
+        m_worksheet->load(data);
+    }
+
+    virtual Cantor::Session* session()
+    {
+        return m_worksheet->session();
+    }
+//  public Q_SLOTS:
+  void evaluate()
+    {
+        m_worksheet->evaluate();
+    }
+
+    void interrupt()
+    {
+        m_worksheet->interrupt();
+    }
+
+  private:
+    Worksheet* m_worksheet;
+};
+
 CantorPart::CantorPart( QWidget *parentWidget, QObject *parent, const QVariantList & \
args ): KParts::ReadWritePart(parent)  {
     m_showBackendHelp=0;
     m_initProgressDlg=0;
     m_statusBarBlocked=false;
 
+    qDebug()<<"Created a CantorPart";
+
     m_panelHandler=new Cantor::PanelPluginHandler(this);
     connect(m_panelHandler, SIGNAL(pluginsChanged()), this, SLOT(pluginsChanged()));
 
-    qDebug()<<"Created a CantorPart";
     QString backendName;
     if(args.isEmpty())
         backendName=QLatin1String("null");
@@ -110,6 +159,11 @@ CantorPart::CantorPart( QWidget *parentWidget, QObject *parent, \
const QVariantLi  // notify the part that this is our internal widget
     setWidget(widget);
 
+    Cantor::WorksheetAccessInterface* iface=new WorksheetAccessInterfaceImpl(this, \
m_worksheet); +
+    qDebug()<<"there really should be a worksheet interface by now";
+
+
     // create our actions
     m_worksheet->createActions(actionCollection());
 
diff --git a/src/cantor_part.h b/src/cantor_part.h
index 821fb86..c687ad9 100644
--- a/src/cantor_part.h
+++ b/src/cantor_part.h
@@ -173,4 +173,6 @@ private:
     bool m_statusBarBlocked;
 };
 
+
+
 #endif // CANTORPART_H
diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt
index 9e33924..039a81b 100644
--- a/src/lib/CMakeLists.txt
+++ b/src/lib/CMakeLists.txt
@@ -21,6 +21,7 @@ set( cantor_LIB_SRCS
   defaultvariablemodel.cpp
   panelplugin.cpp
   panelpluginhandler.cpp
+  worksheetaccess.cpp
   directives/plotdirectives.cpp
 )
 
@@ -44,8 +45,8 @@ Set( cantor_LIB_HDRS
   textresult.h
   #helper classes
   defaulthighlighter.h
-  defaultvariablemodel.h
-
+  defaultvariablemodel.h  
+  worksheetaccess.h
 )
 
 ki18n_wrap_ui(cantor_LIB_SRCS directives/axisrange.ui directives/plottitle.ui)
diff --git a/src/lib/extension.h b/src/lib/extension.h
index 41564fc..6f4fd95 100644
--- a/src/lib/extension.h
+++ b/src/lib/extension.h
@@ -49,6 +49,8 @@ class CANTOR_EXPORT Extension : public QObject
 
 };
 
+
+
 //Some basic interfaces for extensions
 
 /**
diff --git a/src/lib/worksheetaccess.cpp b/src/lib/worksheetaccess.cpp
new file mode 100644
index 0000000..9cc512f
--- /dev/null
+++ b/src/lib/worksheetaccess.cpp
@@ -0,0 +1,39 @@
+/*
+    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.
+
+    This program 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 General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor,
+    Boston, MA  02110-1301, USA.
+
+    ---
+    Copyright (C) 2009 Alexander Rieder <alexanderrieder@gmail.com>
+ */
+
+#include "worksheetaccess.h"
+
+
+using namespace Cantor;
+
+QLatin1String WorksheetAccessInterface::Name =  \
QLatin1String("WorksheetAccessInterface"); +
+WorksheetAccessInterface::WorksheetAccessInterface(QObject* parent) : \
QObject(parent) +{
+    setObjectName(Name);
+}
+
+WorksheetAccessInterface::~WorksheetAccessInterface()
+{
+
+}
+
+
+
diff --git a/src/lib/worksheetaccess.h b/src/lib/worksheetaccess.h
new file mode 100644
index 0000000..9c0bd75
--- /dev/null
+++ b/src/lib/worksheetaccess.h
@@ -0,0 +1,59 @@
+/*
+    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.
+
+    This program 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 General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor,
+    Boston, MA  02110-1301, USA.
+
+    ---
+    Copyright (C) 2015 Alexander Rieder <alexanderrieder@gmail.com>
+ */
+
+#ifndef _WORKSHEET_ACCESS_INTERFACE_H
+#define _WORKSHEET_ACCESS_INTERFACE_H
+
+#include "cantor_export.h"
+
+#include<QObject>
+
+
+namespace Cantor
+{
+    class Session;
+    
+class CANTOR_EXPORT WorksheetAccessInterface : public QObject
+{
+  Q_OBJECT
+  public:  
+    static QLatin1String Name;
+    WorksheetAccessInterface(QObject* parent);
+    virtual ~WorksheetAccessInterface();
+  public:
+    virtual QByteArray saveWorksheetToByteArray() = 0;
+    virtual void loadWorksheetFromByteArray(QByteArray* data) = 0;
+
+    virtual Session* session() = 0;
+  public Q_SLOTS:
+      virtual void evaluate() = 0;
+      virtual void interrupt() = 0;
+
+  Q_SIGNALS:
+      void sessionChanged();
+};
+
+}
+
+#endif
+
+
+
+
diff --git a/src/worksheet.cpp b/src/worksheet.cpp
index 8d54032..64a412b 100644
--- a/src/worksheet.cpp
+++ b/src/worksheet.cpp
@@ -830,14 +830,36 @@ QDomDocument Worksheet::toXML(KZip* archive)
 
 void Worksheet::save( const QString& filename )
 {
+    QFile file(filename);
+    if ( !file.open(QIODevice::WriteOnly) )
+    {
+        KMessageBox::error( worksheetView(),
+                            i18n( "Cannot write file %1." , filename ),
+                            i18n( "Error - Cantor" ));
+        return;
+    }
+
+    save(&file);
+}
+
+QByteArray Worksheet::saveToByteArray()
+{
+    QBuffer buffer;
+    save(&buffer);
+
+    return buffer.buffer();
+}
+
+void Worksheet::save( QIODevice* device)
+{
     qDebug()<<"saving to filename";
-    KZip zipFile( filename );
+    KZip zipFile( device );
 
 
     if ( !zipFile.open(QIODevice::WriteOnly) )
     {
         KMessageBox::error( worksheetView(),
-                            i18n( "Cannot write file %1." , filename ),
+                            i18n( "Cannot write file." ),
                             i18n( "Error - Cantor" ));
         return;
     }
@@ -916,8 +938,23 @@ void Worksheet::saveLatex(const QString& filename)
 
 void Worksheet::load(const QString& filename )
 {
+    QFile file(filename);
+    if (!file.open(QIODevice::ReadOnly))
+        return ;
+    load(&file);
+
+}
+
+void Worksheet::load(QByteArray* data)
+{
+    QBuffer buf(data);
+    load(&buf);
+}
+
+void Worksheet::load(QIODevice* device)
+{
     // m_file is always local so we can use QFile on it
-    KZip file(filename);
+    KZip file(device);
     if (!file.open(QIODevice::ReadOnly))
         return ;
 
diff --git a/src/worksheet.h b/src/worksheet.h
index 9086e40..847b22f 100644
--- a/src/worksheet.h
+++ b/src/worksheet.h
@@ -155,8 +155,12 @@ class Worksheet : public QGraphicsScene
     QDomDocument toXML(KZip* archive=0);
 
     void save(const QString& filename);
+    void save(QIODevice* device);
+    QByteArray saveToByteArray();
     void savePlain(const QString& filename);
     void saveLatex(const QString& filename);
+    void load(QIODevice* device);
+    void load(QByteArray* data);
     void load(const QString& filename);
 
     void gotResult(Cantor::Expression* expr=0);


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic