浏览代码

Added refesh function to ctkXnatTreeModel

Therefor also a "force" flag was added to xnatObject::fetch, in order to
allow a fetch even if the object was already fetch previously.

This is necessary if the data was changed on the server e.g. via the XNAT
webinterface and hence is not consistant any more.

In order to update the treemodel accordingly, a refresh function was added
there as well, which calls fetch recursively on all ctkXnatObject that are
already fetched.
Andreas Fetzer 9 年之前
父节点
当前提交
53b1903278

+ 2 - 0
Applications/ctkXnatTreeBrowser/ctkXnatTreeBrowserMainWindow.cpp

@@ -147,6 +147,8 @@ void ctkXnatTreeBrowserMainWindow::addResourceClicked()
 {
   const QModelIndex index = ui->treeView->selectionModel()->currentIndex();
   ctkXnatObject* parentObject = m_TreeModel->xnatObject(index);
+  parentObject->addResourceFolder("data");
+  m_TreeModel->refresh(index);
 }
 
 void ctkXnatTreeBrowserMainWindow::uploadFileClicked()

+ 2 - 2
Libs/XNAT/Core/ctkXnatObject.cpp

@@ -286,10 +286,10 @@ void ctkXnatObject::setSchemaType(const QString& schemaType)
 }
 
 //----------------------------------------------------------------------------
-void ctkXnatObject::fetch()
+void ctkXnatObject::fetch(bool forceFetch)
 {
   Q_D(ctkXnatObject);
-  if (!d->fetched)
+  if (!d->fetched || forceFetch)
   {
     this->fetchImpl();
     d->fetched = true;

+ 1 - 1
Libs/XNAT/Core/ctkXnatObject.h

@@ -117,7 +117,7 @@ public:
   virtual void reset();
 
   /// Fetches the children and the properties of the object.
-  void fetch();
+  void fetch(bool forceFetch = false);
 
   /// Checks if the object exists on the XNAT server.
   bool exists() const;

+ 53 - 0
Libs/XNAT/Core/ctkXnatTreeModel.cpp

@@ -226,6 +226,59 @@ void ctkXnatTreeModel::fetchMore(const QModelIndex& index)
 }
 
 //----------------------------------------------------------------------------
+void ctkXnatTreeModel::refresh(const QModelIndex& parent)
+{
+  if (!parent.isValid())
+  {
+    return;
+  }
+
+  Q_D(const ctkXnatTreeModel);
+
+  ctkXnatTreeItem* item = d->itemAt(parent);
+  int numChildren = rowCount(parent);
+
+  ctkXnatObject* xnatObject = item->xnatObject();
+
+  if (xnatObject->isFetched())
+  {
+    xnatObject->fetch(true);
+
+    QList<ctkXnatObject*> children = xnatObject->children();
+
+    bool exists (false);
+
+    foreach (ctkXnatObject* child, children)
+    {
+      for (int i = 0; i < numChildren; ++i)
+      {
+        ctkXnatObject* treeItem = item->child(i)->xnatObject();
+
+        if ((treeItem->id().length() != 0 && treeItem->id() == child->id()) ||
+            (treeItem->id().length() == 0 && treeItem->name() == child->name()))
+        {
+          exists = true;
+          break;
+        }
+      }
+      if (!exists)
+      {
+        beginInsertRows(parent, 0, numChildren - 1);
+        item->appendChild(new ctkXnatTreeItem(child, item));
+        endInsertRows();
+        ++numChildren;
+      }
+      exists = false;
+    }
+
+    for (int i=0; i<numChildren; i++)
+    {
+      refresh(index(i,0,parent));
+    }
+  }
+}
+
+//----------------------------------------------------------------------------
 ctkXnatObject* ctkXnatTreeModel::xnatObject(const QModelIndex& index) const
 {
   Q_D(const ctkXnatTreeModel);

+ 6 - 0
Libs/XNAT/Core/ctkXnatTreeModel.h

@@ -50,6 +50,12 @@ public:
   virtual bool canFetchMore(const QModelIndex& parent) const;
   virtual void fetchMore(const QModelIndex& parent);
 
+  /**
+   * @brief Convenience method for refreshing an entry of the tree model
+   * @param The parent item, whose children will be refreshed
+   */
+  virtual void refresh(const QModelIndex& parent = QModelIndex());
+
   ctkXnatObject* xnatObject(const QModelIndex& index) const;
 
   void addDataModel(ctkXnatDataModel* dataModel);