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

List:       kde-commits
Subject:    koffice/kspread/plugins/scripting
From:       Sebastian Sauer <mail () dipe ! org>
Date:       2007-06-22 14:28:34
Message-ID: 1182522514.281751.4536.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 678902 by sebsauer:

* added ScriptingWriter to provide a high-level API for scripts to manipulate \
                content.
* added initial implementation of the csvimport.py sample that uses the new writer.



 M  +1 -1      CMakeLists.txt  
 M  +6 -0      ScriptingModule.cpp  
 M  +6 -0      ScriptingModule.h  
 M  +71 -19    ScriptingReader.h  
 A             ScriptingWriter.h   [License: LGPL (v2+)]
 M  +1 -1      scripts/CMakeLists.txt  
 M  +2 -2      scripts/csvexport.py  
 D             scripts/csvexportoptions.ui  
 AM            scripts/csvimport.py  
 A             scripts/csvoptions.ui   scripts/csvexportoptions.ui#678161
 M  +16 -6     scripts/scripts.rc  


--- trunk/koffice/kspread/plugins/scripting/CMakeLists.txt #678901:678902
@@ -13,7 +13,7 @@
 # the KSpread Scripting KParts::Plugin + Kross module
 set(krossmodulekspread_PART_SRCS ScriptingModule.cpp ScriptingFunction.cpp \
ScriptingWidgets.cpp ScriptingPart.cpp)  
-qt4_wrap_cpp(krossmodulekspread_PART_SRCS ScriptingReader.h)
+qt4_wrap_cpp(krossmodulekspread_PART_SRCS ScriptingReader.h ScriptingWriter.h)
 kde4_automoc(${krossmodulekspread_PART_SRCS})
 kde4_add_plugin(krossmodulekspread ${krossmodulekspread_PART_SRCS})
 
--- trunk/koffice/kspread/plugins/scripting/ScriptingModule.cpp #678901:678902
@@ -24,6 +24,7 @@
 #include "ScriptingFunction.h"
 #include "ScriptingWidgets.h"
 #include "ScriptingReader.h"
+#include "ScriptingWriter.h"
 
 #include <QPointer>
 #include <QApplication>
@@ -179,6 +180,11 @@
     return new ScriptingReader(this);
 }
 
+QObject* ScriptingModule::writer()
+{
+    return new ScriptingWriter(this);
+}
+
 QWidget* ScriptingModule::createSheetsListView(QWidget* parent)
 {
 	ScriptingSheetsListView* listview = new ScriptingSheetsListView(this, parent);
--- trunk/koffice/kspread/plugins/scripting/ScriptingModule.h #678901:678902
@@ -153,6 +153,12 @@
         QObject* reader();
 
         /**
+        * Create and return a new \a ScriptingWriter object that provides us an
+        * abstract high-level API to write content to KSpread sheets.
+        */
+        QObject* writer();
+
+        /**
         * Create and return a new \a ScriptingSheetsListView widget instance which
         * could be used to select 0..n sheets from a list of all available sheets.
         */
--- trunk/koffice/kspread/plugins/scripting/ScriptingReader.h #678901:678902
@@ -37,6 +37,39 @@
 /**
 * The ScriptingReader class provides abstract high-level functionality to read
 * content from KSpread sheets.
+*
+* The following python sample demonstrates how to use the ScriptingReader to
+* read content from KSpread.
+* \code
+* # Import the KSpread module
+* import KSpread
+*
+* # Create a ScriptingReader object
+* reader = KSpread.reader()
+* # We like to read all of Sheet1
+* reader.setSheet("Sheet1")
+* # and from Sheet2 only the range A5:F12
+* reader.setSheet("Sheet2","A5:F12")
+*
+* # This function got called in our case two times.
+* # One time if we switch to Sheet1 and the other
+* # time if switched to Sheet2.
+* def changedSheet(sheetname):
+*     print "sheetname=%s" % sheetname
+*
+* # This function got called for each row that got
+* # readed.
+* def changedRow(row):
+*     values = reader.currentValues()
+*     print "row=%i values=%s" % (row,values)
+*
+* # Now let's connect our both functions to matching
+* # signals the reader provides.
+* reader.connect("changedSheet(QString)",changedSheet)
+* reader.connect("changedRow(int)",changedRow)
+* # and finally start the reader.
+* reader.start()
+* \endcode
 */
 class ScriptingReader : public QObject
 {
@@ -89,21 +122,23 @@
         * reader is stopped and can be started again by using the \a start() method \
                false
         * got returned.
         */
-        bool isRunning() {
+        bool isRunning() const {
             return m_state != Stopped;
         }
 
         /**
-        * \return the names of the sheets that should be readed.
+        * \return the names of the sheets that should be readed. If the list is \
empty and the +        * reader got started, then the reader will walk through all \
                sheets the document has.
         */
-        QStringList sheetNames() {
+        QStringList sheetNames() const {
             return m_sheetnames;
         }
 
         /**
-        * \return the defined ranges for the \p sheetname .
+        * \return the defined ranges for the \p sheetname . If there was no range \
defined, +        * then we walk over all the data the sheet has.
         */
-        QVariantList range(const QString& sheetname) {
+        QVariantList range(const QString& sheetname) const {
             return m_ranges.contains(sheetname) ? m_ranges[sheetname] : \
QVariantList();  }
 
@@ -125,9 +160,9 @@
         /**
         * Set the sheets and there ranges.
         *
-        * For example in python following structure is a valid definition for 3
-        * 3 sheets where Sheet1 and Sheet2 are selected. Sheet1 also does
-        * define the range A1:B2.
+        * For example in python following structure is a valid definition
+        * for 3 sheets where Sheet1 and Sheet2 are selected. Sheet1 also
+        * does define the range A1:B2.
         * [['Sheet1', 1, [1, 1, 2, 2]], ['Sheet2', 1], ['Sheet3', 0]]
         *
         * Each sheet contains a tuple of
@@ -135,7 +170,7 @@
         * \li 1=enabled or 0=disabled
         * \li optional range tuple [from column, from row, to column, to row]
         */
-        void setSheets(const QVariantList& sheets) {
+        void setSheets(const QVariantList& sheets = QVariantList()) {
             m_sheetnames.clear();
             m_ranges.clear();
             foreach(QVariant item, sheets) {
@@ -144,10 +179,12 @@
                 if( ! args[1].toBool() ) continue;
                 const QString sheetname = args[0].toString();
                 if( sheetname.isEmpty() ) continue;
-                args.removeFirst();
-                args.removeFirst();
                 m_sheetnames.append(sheetname);
-                if( args.count() > 0 ) m_ranges[sheetname] = args;
+                if( args.count() > 2 ) {
+                    args.removeFirst();
+                    args.removeFirst();
+                    m_ranges[sheetname] = args;
+                }
             }
         }
 
@@ -173,7 +210,8 @@
             if( ! region.isValid() ) return;
             for(KSpread::Region::ConstIterator it = region.constBegin(); it != \
region.constEnd(); ++it) {  const QRect rect = (*it)->rect();
-                if( ! rect.isNull() ) ranges.append(rect);
+                if( rect.isNull() ) continue;
+                ranges.append(rect);
             }
             m_ranges[sheetname] = ranges;
         }
@@ -182,7 +220,7 @@
         * \return the sheetname the reader currently is on. An empty/null string \
                will be
         * returned if there is no current sheet (e.g. if the reader just doesn't run \
                currently).
         */
-        QString currentSheetName() {
+        QString currentSheetName() const {
             return m_currentSheet ? m_currentSheet->sheetName() : QString();
         }
 
@@ -190,15 +228,29 @@
         * \return the current row number the reader is on. This will be -1 if the \
                reader
         * isn't running.
         */
-        int currentRow() {
+        int currentRow() const {
             return m_currentRow;
         }
 
         /**
+        * \return the most left column the current row has or -1 if there is no \
current row. +        */
+        int currentFirstColumn() const {
+            return m_currentLeft;
+        }
+
+        /**
+        * \return the most right column the current row has or -1 if there is no \
current row. +        */
+        int currentLastColumn() const {
+            return m_currentRight;
+        }
+
+        /**
         * \return a list of values for the current row. This will be an empty list \
                of the
         * reader isn't running.
         */
-        QVariantList currentValues() {
+        QVariantList currentValues() const {
             QVariantList values;
             if( m_currentSheet && m_currentRow >= 0 ) {
                 for(int col = m_currentLeft; col <= m_currentRight; ++col) {
@@ -228,8 +280,8 @@
 
         /**
         * This signal is emitted once the reading started with the \a start() method
-        * changed to the sheet with name \p sheetname cause reading the probably
-        * previous sheet was done.
+        * changed to the sheet with name \p sheetname cause e.g. reading the \
previous +        * sheet was done.
         */
         void changedSheet(const QString& sheetname);
 
@@ -254,7 +306,7 @@
         void clear() {
             m_state = Stopped;
             m_currentSheet = 0;
-            m_currentRow = -1;
+            m_currentRow = m_currentLeft = m_currentRight = -1;
         }
 
         void readSheet(const QString& sheetname) {
--- trunk/koffice/kspread/plugins/scripting/scripts/CMakeLists.txt #678901:678902
@@ -3,7 +3,7 @@
     DESTINATION ${DATA_INSTALL_DIR}/kspread/scripts)
 
 install(FILES
-    csvexport.py csvexportoptions.ui
+    csvimport.py csvexport.py csvoptions.ui
     htmlexport.py htmlexportinfos.ui
     odfpyexport.py
     DESTINATION ${DATA_INSTALL_DIR}/kspread/scripts/extensions)
--- trunk/koffice/kspread/plugins/scripting/scripts/csvexport.py #678901:678902
@@ -27,13 +27,13 @@
         savepage = self.dialog.addPage("Save","Export to CSV File","document-save")
         self.savewidget = self.forms.createFileWidget(savepage, \
"kfiledialog:///kspreadcsvexportsave")  self.savewidget.setMode("Saving")
-        self.savewidget.setFilter("*.csv|CSV Files\n*.txt|Text Files\n*|All Files")
+        self.savewidget.setFilter("*.csv *.txt|Comma-Separated-Value Files\n*|All \
Files")  
         datapage = self.dialog.addPage("Sheets","Export Sheets","spreadsheet")
         self.sheetslistview = KSpread.createSheetsListView(datapage)
 
         optionspage = self.dialog.addPage("Options","Comma Separated Value \
                Options","configure")
-        self.optionswidget = self.forms.createWidgetFromUIFile(optionspage, \
os.path.join(self.currentpath, "csvexportoptions.ui")) +        self.optionswidget = \
self.forms.createWidgetFromUIFile(optionspage, os.path.join(self.currentpath, \
"csvoptions.ui"))  
         if self.dialog.exec_loop():
             try:
** trunk/koffice/kspread/plugins/scripting/scripts/csvimport.py #property \
svn:executable  + *
--- trunk/koffice/kspread/plugins/scripting/scripts/scripts.rc #678901:678902
@@ -1,20 +1,30 @@
 <KrossScripting>
-    <script
+    <collection name="import" text="Import" comment="Import content">
+        <script
+            name="pycsvimport"
+            text="CSV Import"
+            comment="Import from a Comma-Separated-Value File"
+            interpreter="python"
+            file="extensions/csvimport.py" />
+    </collection>
+    <collection name="export" text="Export" comment="Export content">
+        <script
             name="pycsvexport"
             text="CSV Export"
-            comment="Export an OpenDocument Spreadsheet File to a CSV File"
+            comment="Export to a Comma-Separated-Value File"
             interpreter="python"
             file="extensions/csvexport.py" />
-    <script
+        <script
             name="pyhtmlexport"
             text="HTML Export"
-            comment="Export an OpenDocument Spreadsheet File to a HTML File"
+            comment="Export to a HTML File"
             interpreter="python"
             file="extensions/htmlexport.py" />
-    <script
+        <script
             name="odfpyexport"
             text="OdfPy Export"
-            comment="Export an OpenDocument Spreadsheet File to an OpenDocument \
File" +            comment="Export to an OpenDocument File"
             interpreter="python"
             file="extensions/odfpyexport.py" />
+    </collection>
 </KrossScripting>


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

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