[Digikam-devel] [kipi-plugins] common/libkipiplugins/widgets: Added Save and Load management to ImagesList and emitted:

Angelo Naselli anaselli at linux.it
Thu Jan 26 21:45:37 GMT 2012


Git commit 0a263ae6c29547a85ac248e8f0af4495994d884c by Angelo Naselli.
Committed on 26/01/2012 at 22:33.
Pushed by anaselli into branch 'master'.

Added Save and Load management to ImagesList and emitted:
signalXMLSaveItem, signalXMLLoadImageElement,
signalXMLCustomElements, signalXMLCustomElements
to save and load custom attributes and elements.
File format is XML:
<?xml version="1.0" encoding="UTF-8"?>
<Images>
    <Image url="file:///path/to/image" custom_attr1="value1">
        <CustomImageElem CustomImageElemAttr="attribute value"/>
    </Image>
    <CustomElem customElemAttr="" />
<Images/>

I suggest plugins to add a plugin prefix to allow loading imagelist
also to those plugins that don't have custom elements/attributes.

CCMAIL=digikam-devel at kde.org,kde-imaging at kde.org

M  +127  -7    common/libkipiplugins/widgets/imageslist.cpp
M  +12   -5    common/libkipiplugins/widgets/imageslist.h

http://commits.kde.org/kipi-plugins/0a263ae6c29547a85ac248e8f0af4495994d884c

diff --git a/common/libkipiplugins/widgets/imageslist.cpp b/common/libkipiplugins/widgets/imageslist.cpp
index 34c3ed7..003b9cb 100644
--- a/common/libkipiplugins/widgets/imageslist.cpp
+++ b/common/libkipiplugins/widgets/imageslist.cpp
@@ -36,6 +36,12 @@
 #include <QPushButton>
 #include <QUrl>
 #include <QTimer>
+#include <QFile>
+#include <QPointer>
+#include <QXmlStreamWriter>
+#include <QXmlStreamReader>
+#include <QXmlStreamAttributes>
+#include <QStringRef>
 
 // KDE includes
 
@@ -47,6 +53,8 @@
 #include <knuminput.h>
 #include <kio/previewjob.h>
 #include <kpixmapsequence.h>
+#include <KFileDialog>
+#include <KGlobalSettings>
 
 // LibKIPI includes
 
@@ -395,7 +403,7 @@ QModelIndex ImagesListView::indexFromItem ( ImagesListViewItem * item, int colum
 void ImagesListView::contextMenuEvent(QContextMenuEvent * e)
 {
     QTreeWidget::contextMenuEvent(e);
-    emit contextMenuRequested();
+    emit signalContextMenuRequested();
 }
 
 void ImagesListView::dragEnterEvent(QDragEnterEvent* e)
@@ -578,8 +586,8 @@ ImagesList::ImagesList(Interface* iface, QWidget* parent, int iconSize)
     connect(d->listView, SIGNAL(signalItemClicked(QTreeWidgetItem*)),
             this, SIGNAL(signalItemClicked(QTreeWidgetItem*)));
 
-    connect(d->listView, SIGNAL(contextMenuRequested()),
-            this, SIGNAL(contextMenuRequested()));
+    connect(d->listView, SIGNAL(signalContextMenuRequested()),
+            this, SIGNAL(signalContextMenuRequested()));
     
     // queue this connection because itemSelectionChanged is emitted
     // while items are deleted, and accessing selectedItems at that
@@ -912,9 +920,121 @@ void ImagesList::slotMoveDownItems()
 
 void ImagesList::slotClearItems()
 {
-  listView()->selectAll();
-  slotRemoveItems();
-  listView()->clear();
+    listView()->selectAll();
+    slotRemoveItems();
+    listView()->clear();
+}
+
+void ImagesList::slotLoadItems()
+{
+    KUrl loadLevelsFile;
+
+    loadLevelsFile = KFileDialog::getOpenUrl(KGlobalSettings::documentPath(),
+                                            QString( "*" ), this,
+                                            QString( i18n("Select the image file list to load")) );
+
+    if ( loadLevelsFile.isEmpty() )
+    {
+        return;
+    }
+    QFile file(loadLevelsFile.path());
+
+    kDebug() << "file path " <<loadLevelsFile.path();
+    file.open(QIODevice::ReadOnly);
+    QXmlStreamReader xmlReader;
+    xmlReader.setDevice(&file);
+
+    while (!xmlReader.atEnd())
+    {
+        if (xmlReader.isStartElement() && xmlReader.name() == "Image")
+        {
+          // get all attributes and its value of a tag in attrs variable.
+          QXmlStreamAttributes attrs = xmlReader.attributes();
+          // get value of each attribute from QXmlStreamAttributes
+          QStringRef url = attrs.value("url");
+          kDebug() << xmlReader.name() << " attributes test? " << url.toString();
+          
+          if (url.isEmpty())
+          {
+              xmlReader.readNext();
+              continue;
+          }
+          KUrl::List urls;
+          urls.append(url.toString());
+
+          if (!urls.isEmpty())
+          {
+              //allow plugins to append a new file
+              slotAddImages(urls);
+              // read plugin Image custom attributes and children element  
+              emit signalXMLLoadImageElement(xmlReader);
+          }
+        }
+        else if (xmlReader.isStartElement() && xmlReader.name() != "Images")
+        {
+          // unmanaged start element (it should be plugins one)
+          emit signalXMLCustomElements(xmlReader);
+        }
+        else if(xmlReader.isEndElement() && xmlReader.name() == "Images")
+        {
+          // if EndElement is Images return
+          return;
+        }
+        xmlReader.readNext();
+    }
+}
+
+void ImagesList::slotSaveItems()
+{
+    KUrl saveLevelsFile;
+    saveLevelsFile = KFileDialog::getSaveUrl(KGlobalSettings::documentPath(),
+                                             QString( "*" ), this,
+                                             QString( i18n("Select the image file list to save")) );
+    kDebug() << "file url " <<saveLevelsFile.prettyUrl().toAscii();
+
+    if ( saveLevelsFile.isEmpty() )
+    {
+       kDebug() << "empty url ";
+        return;
+    }
+
+    QFile file(saveLevelsFile.path() /*.prettyUrl().toAscii()*/);
+    file.open(QIODevice::WriteOnly);
+//     file.open(stdout, QIODevice::WriteOnly);
+
+    QXmlStreamWriter xmlWriter;
+    xmlWriter.setDevice(&file);
+
+    xmlWriter.setAutoFormatting(true);
+    xmlWriter.writeStartDocument();
+
+    xmlWriter.writeStartElement("Images");
+    
+    QTreeWidgetItemIterator it(listView());
+
+    while (*it)
+    {
+        ImagesListViewItem* lvItem = dynamic_cast<ImagesListViewItem*>(*it);
+
+        if (lvItem)
+        {
+            xmlWriter.writeStartElement("Image");
+
+            xmlWriter.writeAttribute("url", lvItem->url().prettyUrl().toAscii());
+            
+            //emit xmlWriter, item?
+            emit signalXMLSaveItem(xmlWriter, lvItem);
+            
+            xmlWriter.writeEndElement(); //Image        
+        }
+        ++it;
+    }
+
+    emit signalXMLCustomElements(xmlWriter);
+
+    xmlWriter.writeEndElement(); // Images
+
+    xmlWriter.writeEndDocument(); //end document
 }
 
 void ImagesList::removeItemByUrl(const KUrl& url)
@@ -1086,7 +1206,7 @@ void ImagesList::slotImageListChanged()
     // enabled, if the buttons are not explicitly disabled with enableControlButtons()
     d->addButton->setEnabled(d->controlButtonsEnabled);
 
-    // TODO: load and save are not yet implemented, when should they be enabled/disabled?
+    // TODO: should they be enabled by default now?
     d->loadButton->setEnabled(d->controlButtonsEnabled);
     d->saveButton->setEnabled(d->controlButtonsEnabled);
 }
diff --git a/common/libkipiplugins/widgets/imageslist.h b/common/libkipiplugins/widgets/imageslist.h
index 704a328..f893842 100644
--- a/common/libkipiplugins/widgets/imageslist.h
+++ b/common/libkipiplugins/widgets/imageslist.h
@@ -52,6 +52,9 @@ namespace KIPI
 class Interface;
 }
 
+class QXmlStreamWriter;
+class QXmlStreamReader;
+
 using namespace KIPI;
 
 namespace KIPIPlugins
@@ -155,7 +158,7 @@ Q_SIGNALS:
 
     void addedDropedItems(const KUrl::List& urls);
     void signalItemClicked(QTreeWidgetItem*);
-    void contextMenuRequested();
+    void signalContextMenuRequested();
 
 private Q_SLOTS:
 
@@ -262,8 +265,12 @@ Q_SIGNALS:
     void signalImageListChanged();
     void signalFoundRAWImages(bool);
     void signalItemClicked(QTreeWidgetItem*);
-    void contextMenuRequested();
-
+    void signalContextMenuRequested();
+    void signalXMLSaveItem(QXmlStreamWriter&, KIPIPlugins::ImagesListViewItem*);
+    void signalXMLLoadImageElement(QXmlStreamReader&);
+    void signalXMLCustomElements(QXmlStreamWriter&);
+    void signalXMLCustomElements(QXmlStreamReader&);
+    
 public Q_SLOTS:
 
     virtual void slotAddImages(const KUrl::List& list);
@@ -277,8 +284,8 @@ protected Q_SLOTS:
     virtual void slotMoveUpItems();
     virtual void slotMoveDownItems();
     virtual void slotClearItems();
-    virtual void slotLoadItems(){};
-    virtual void slotSaveItems(){};
+    virtual void slotLoadItems();
+    virtual void slotSaveItems();
     virtual void slotThumbnail(const KUrl& url, const QPixmap& pix);
     virtual void slotImageListChanged();
 




More information about the Digikam-devel mailing list