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

List:       kde-commits
Subject:    playground/base/nepomuk-kde/backupsync/gui
From:       Vishesh Handa <handa.vish () gmail ! com>
Date:       2010-06-27 17:55:16
Message-ID: 20100627175516.A669AAC8DF () svn ! kde ! org
[Download RAW message or body]

SVN commit 1143417 by vhanda:

Identifier Model changes -
* Fixed buggy adition of TreeItem
* Optimized identified slot by using a sparql query to determine the uri
* Added extra checks
* Cleanup + Documentation


 M  +73 -50    identifiermodel.cpp  
 M  +67 -8     identifiermodel.h  


--- trunk/playground/base/nepomuk-kde/backupsync/gui/identifiermodel.cpp #1143416:1143417
@@ -22,7 +22,11 @@
 #include "nfo.h"
 
 #include <Soprano/Vocabulary/RDF>
+#include <Soprano/QueryResultIterator>
+
 #include <KUrl>
+#include <Nepomuk/ResourceManager>
+#include <Soprano/Model>
 
 Nepomuk::IdentifierModel::IdentifierModel(QObject* parent): QAbstractItemModel(parent)
 {
@@ -82,7 +86,7 @@
     else
         parentItem = static_cast<TreeItem*>(parent.internalPointer());
 
-    return parentItem->rows();
+    return parentItem->children();
 }
 
 
@@ -120,16 +124,27 @@
          return QModelIndex();
 }
 
-
+//TODO: Handle multiple models
 void Nepomuk::IdentifierModel::identified(int id, const QString& oldUri, const QString& newUri)
 {
     emit layoutAboutToBeChanged();
 
-    TreeItem * t = m_root->getByUri( newUri );
+    // Get nie:url
+    QString query = QString::fromLatin1(" select ?url where { %1 %2 ?url. } ")
+                    .arg( Soprano::Node::resourceToN3(newUri) )
+                    .arg( Soprano::Node::resourceToN3( Nepomuk::Vocabulary::NIE::url() ) );
+
+    Soprano::Model * model = Nepomuk::ResourceManager::instance()->mainModel();
+    Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageSparql );
+    if( it.next() ) {
+        const QString nieUrl = it[0].toString();
+        TreeItem * t = m_root->find( nieUrl );
     if( t )
         t->del();
 }
+}
 
+//TODO: Ditto!
 void Nepomuk::IdentifierModel::notIdentified(int id, const QList< Soprano::Statement >& sts)
 {
     TreeItem * t = new TreeItem( sts );
@@ -187,12 +202,10 @@
 
 Nepomuk::TreeItem * Nepomuk::TreeItem::child(int row)
 {
-    if( row < m_children.size() - 1)
-        return *( m_children.begin() + row );
-    return 0;
+    return m_children.at( row );
 }
 
-int Nepomuk::TreeItem::rows() const
+int Nepomuk::TreeItem::children() const
 {
     return m_children.size();
 }
@@ -206,13 +219,8 @@
 int Nepomuk::TreeItem::row()
 {
     if( m_parent ) {
-        int pos = 0;
-        foreach( TreeItem * t, m_parent->m_children ) {
-            if( t == this )
-                return pos;
-            pos++;
+        return m_parent->m_children.indexOf( this );
         }
-    }
     return 0;
 }
 
@@ -222,7 +230,7 @@
     return m_nieUrl;
 }
 
-QUrl Nepomuk::TreeItem::resUrl() const
+QUrl Nepomuk::TreeItem::resUri() const
 {
     return m_resUri;
 }
@@ -246,8 +254,18 @@
 void Nepomuk::TreeItem::add(Nepomuk::TreeItem* item)
 {
     //
-    // Check if it should be in the current position
+    // Check if it already exists
     //
+    if( m_nieUrl == item->m_nieUrl )
+        return;
+
+    foreach( TreeItem * t, m_children )
+        if( t->m_nieUrl == item->m_nieUrl )
+            return;
+
+    //
+    // Check if it should be inserted in the current node
+    //
     bool shouldBeHere = true;
     foreach( TreeItem * t, m_children ) {
         if( t->len() < item->len() ) {
@@ -256,31 +274,30 @@
         }
     }
 
+    // If this is the correct position
     if( shouldBeHere ) {
-        // Check if root
+        // Check if root node
         if( m_parent == 0 ) {
             // item should become the root
             m_nieUrl = item->nieUrl();
-            m_resUri = item->resUrl();
+            m_resUri = item->resUri();
             m_statements = item->statements();
             return;
         }
 
         // Otherwise find all the TreeItem in m_children who should be item's children
-        QMutableMapIterator<QString, TreeItem *> it( m_children );
+        // and relocate them
+        QMutableListIterator<TreeItem *> it( m_children );
         while( it.hasNext() ) {
-            it.next();
+            TreeItem * c = it.next();
 
-            TreeItem * c = it.value();
             if( c->contains( item ) ) {
                 // Relocate c
                 it.remove();
-                c->m_parent = item;
-                item->m_children.insert( c->nieUrl(), c );
+                insert( item, c );
             }
         }
-        m_parent->m_children.insert( item->nieUrl(), item );
-        item->m_parent = this;
+        insert( item );
         return;
     }
 
@@ -292,29 +309,53 @@
         }
     }
 
-    // Looks like you just need to add it
-    m_parent->m_children.insert( item->nieUrl(), item );
-    item->m_parent = this;
+    // Nothing needs to be done, just add it
+    insert( item );
 }
 
 
+void Nepomuk::TreeItem::insert(Nepomuk::TreeItem* child)
+{
+    insert( this, child );
+}
+
+
+void Nepomuk::TreeItem::insert(Nepomuk::TreeItem* parent, Nepomuk::TreeItem* child)
+{
+    parent->m_children.append( child );
+
+    //TODO: Insert it in the correct position -> InsertionSort
+    child->m_parent = parent;
+}
+
+
 void Nepomuk::TreeItem::del()
 {
+    // If you are NOT the root node
+    if( m_parent ) {
     // Give your children to your parent
-    m_parent->m_children.unite( m_children );
+        m_parent->m_children << m_children;
 
     // Get disowned
-    m_parent->m_children.remove( m_nieUrl );
-    m_parent = NULL;
+        m_parent->m_children.removeAll( this );
+        m_parent = 0;
     m_children.clear();
 }
 
+    // Clear the interal data
+    m_statements.clear();
+    m_nieUrl.clear();
+    m_resUri.clear();
+}
 
+
 Nepomuk::TreeItem* Nepomuk::TreeItem::find(const QString & nieUrl)
 {
     // If it's your one of your children
-    if( m_children.contains(nieUrl) )
-        return m_children[ nieUrl ];
+    foreach( TreeItem * t, m_children ) {
+        if( t->m_nieUrl == nieUrl )
+            return t;
+    }
 
     // Who's child is it?
     foreach( TreeItem * t, m_children ) {
@@ -323,23 +364,5 @@
     }
 
     // Not found
-    return NULL;
-}
-
-
-Nepomuk::TreeItem* Nepomuk::TreeItem::getByUri(const QUrl& uri)
-{
-    // Do a horrible dfs
-    foreach( TreeItem * t, m_children ) {
-        TreeItem* res = t->getByUri( uri );
-        if( res )
-            return res;
-    }
-
-    if( m_resUri == uri )
-        return this;
-
     return 0;
 }
-
-
--- trunk/playground/base/nepomuk-kde/backupsync/gui/identifiermodel.h #1143416:1143417
@@ -60,28 +60,74 @@
 
     public:
         TreeItem();
+        /**
+         * Gets the resource uri, and the nie:url from the list of statements
+         */
         TreeItem( const QList<Soprano::Statement> st );
+
+        /**
+         * Normal destructor. Deletes all the children as well.
+         */
         ~TreeItem();
 
+        /**
+         * Inserts \p child into the children of this, and sets
+         * the child's parent to this
+         */
+        void insert( Nepomuk::TreeItem* child );
+
+        /**
+         * Based on the nieurl it determines the correct position
+         * and inserts item.
+         * May cause major restructuring of the tree.
+         * Should be called from the root node.
+         */
         void add( TreeItem * item );
+
+        /**
+         * Return the TreeItem which has it's nie:url equal to \p nieUrl
+         * It searches the entire tree
+         */
         TreeItem* find( const QString& nieUrl );
+
+        /**
+         * Remove this from it's parents children list, and resets it's parent
+         * Clears all other data as well
+         */
         void del();
 
+        /**
+         * Returns the row number of the current TreeItem in it's parents children list.
+         * Returns 0 if it doesn't have a parent
+         */
         int row();
-        int rows() const;
 
+        /**
+         * Returns the number of children.
+         */
+        int children() const;
+
+        /**
+         * Returns the child at row number \p row
+         */
+        TreeItem* child(int row);
+
+        /**
+         * Returns the nie:url
+         */
         QString nieUrl() const;
-        QUrl resUrl() const;
+
+        /**
+         * Returns the resource uri
+         */
+        QUrl resUri() const;
+
         QList<Soprano::Statement> statements() const;
 
         TreeItem * parent() const;
-
-        TreeItem* child(int row);
-
-        TreeItem* getByUri( const QUrl & uri );
-
     private:
-        QMap<QString, TreeItem *> m_children;
+        // Tree Handling
+        QList<TreeItem *> m_children;
         TreeItem * m_parent;
 
         // Internal data
@@ -89,8 +135,21 @@
         QUrl m_resUri;
         QList<Soprano::Statement> m_statements;
 
+        /**
+         * Return true if this should be \p t 's parent based on filesystem ordering
+         * Based on the TreeItem's nie:url
+         */
         bool contains( TreeItem * t );
+
+        /**
+         * Returns the depth of the nieUrl. Used to arrange the tree structure
+         */
         int len() const;
+
+        /**
+         * Inserts \p child into \p parent, and sets \p child 's parent = \p parent
+         */
+        static void insert( Nepomuk::TreeItem* parent, Nepomuk::TreeItem* child );
     };
 
 }
[prev in list] [next in list] [prev in thread] [next in thread] 

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