소스 검색

Changed TreeModel functionality of CanFetchMore() and FetchMore(),
to add children which have been fetched already.
Previously these children were not added to the model,
they were fetched somewhere else.
Added a ctkXnatListModel which is based on
ctkXnatProjectListModel but it has different headerData.
It can be used for all types of ctkXnatObjects.

Daniel Knorr 11 년 전
부모
커밋
905a9c6ff1

+ 2 - 0
Libs/XNAT/Widgets/CMakeLists.txt

@@ -12,6 +12,7 @@ set(KIT_SRCS
   ctkXnatProjectListModel.cpp
   ctkXnatTreeModel.cpp
   ctkXnatSettings.cpp
+  ctkXnatListModel.cpp
 )
 
 # Files which should be processed by Qts moc
@@ -19,6 +20,7 @@ set(KIT_MOC_SRCS
   ctkXnatLoginDialog.h
   ctkXnatProjectListModel.h
   ctkXnatTreeModel.h
+  ctkXnatListModel.h
 )
 
 

+ 125 - 0
Libs/XNAT/Widgets/ctkXnatListModel.cpp

@@ -0,0 +1,125 @@
+/*=============================================================================
+
+  Library: CTK
+
+  Copyright (c) German Cancer Research Center,
+    Division of Medical and Biological Informatics
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=============================================================================*/
+
+#include "ctkXnatDataModel.h"
+#include "ctkXnatListModel.h"
+#include "ctkXnatProject.h"
+#include "ctkXnatSubject.h"
+#include "ctkXnatExperiment.h"
+#include "ctkXnatScanFolder.h"
+#include "ctkXnatScan.h"
+#include "ctkXnatScanResource.h"
+
+#include <iostream>
+#include <typeinfo>
+
+#include <QDebug>
+
+ctkXnatListModel::ctkXnatListModel()
+  : rootObject(0)
+{
+}
+
+void ctkXnatListModel::setRootObject(ctkXnatObject* root)
+{
+  rootObject = root;
+}
+
+ctkXnatObject* ctkXnatListModel::getRootObject()
+{
+  return rootObject;
+}
+
+int ctkXnatListModel::rowCount(const QModelIndex& /*parent*/) const
+{
+  if (!rootObject) return 0;
+  return rootObject->children().size();
+}
+
+QVariant ctkXnatListModel::data(const QModelIndex& index, int role) const
+{
+  if (!rootObject) return QVariant();
+
+  if (role == Qt::DisplayRole)
+  {
+    ctkXnatObject* child = rootObject->children().at(index.row());
+    if (!child)
+    {
+      qWarning() << "child at index" << index << "is NULL!";
+    }
+    else
+    {
+      QString displayData = child->name();
+      if (displayData.isEmpty())
+      {
+        displayData = child->id();
+      }
+      return displayData;
+    }
+  }
+  else if (role == Qt::UserRole)
+  {
+    return QVariant::fromValue(rootObject->children().at(index.row()));
+  }
+  return QVariant();
+}
+
+QVariant ctkXnatListModel::headerData(int /*section*/, Qt::Orientation /*orientation*/, int role) const
+{
+  if (role == Qt::DisplayRole)
+  {
+    if (!rootObject) return QString("Unavailable");
+
+    if( dynamic_cast<ctkXnatDataModel*>(rootObject) != NULL )
+    {
+      return QString("Projects");
+    }
+    else if( dynamic_cast<ctkXnatProject*>(rootObject) != NULL )
+    {
+      return QString("Subjects");
+    }
+    else if( dynamic_cast<ctkXnatSubject*>(rootObject) != NULL )
+    {
+      return QString("Experiments");
+    }
+    else if( dynamic_cast<ctkXnatExperiment*>(rootObject) != NULL )
+    {
+      return QString("Kinds of data");
+    }
+    else if( dynamic_cast<ctkXnatScanFolder*>(rootObject) != NULL )
+    {
+      return QString("Image Sessions");
+    }
+    else if( dynamic_cast<ctkXnatScan*>(rootObject) != NULL )
+    {
+      return QString("Resource Folders");
+    }
+    else if( dynamic_cast<ctkXnatScanResource*>(rootObject) != NULL )
+    {
+      return QString("Files");
+    }
+    else
+    {
+      return QString("ERROR");
+    }
+  }
+  return QVariant();
+}

+ 48 - 0
Libs/XNAT/Widgets/ctkXnatListModel.h

@@ -0,0 +1,48 @@
+/*=============================================================================
+
+  Library: CTK
+
+  Copyright (c) German Cancer Research Center,
+    Division of Medical and Biological Informatics
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=============================================================================*/
+
+#ifndef CTKXNATPROJECTLISTMODEL_H
+#define CTKXNATPROJECTLISTMODEL_H
+
+#include "QAbstractListModel"
+
+#include "ctkXNATWidgetsExport.h"
+
+class ctkXnatObject;
+
+class CTK_XNAT_WIDGETS_EXPORT ctkXnatListModel : public QAbstractListModel
+{
+  Q_OBJECT
+
+public:
+  ctkXnatListModel();
+  void setRootObject(ctkXnatObject* root);
+  ctkXnatObject* getRootObject();
+
+  int rowCount(const QModelIndex &parent) const;
+  QVariant data(const QModelIndex &index, int role) const;
+  QVariant headerData(int section, Qt::Orientation orientation, int role) const;
+
+private:
+  ctkXnatObject* rootObject;
+};
+
+#endif // CTKXNATPROJECTLISTMODEL_H

+ 22 - 5
Libs/XNAT/Widgets/ctkXnatTreeModel.cpp

@@ -20,10 +20,11 @@
 =============================================================================*/
 
 #include "ctkXnatTreeModel.h"
-
+#include "ctkXnatObjectPrivate.h"
 #include "ctkXnatObject.h"
 #include "ctkXnatSubject.h"
 
+#include <QSharedPointer>
 #include <QList>
 
 ctkXnatTreeModel::ctkXnatTreeModel()
@@ -64,6 +65,10 @@ QVariant ctkXnatTreeModel::data(const QModelIndex& index, int role) const
   {
     return this->xnatObject(index)->description();
   }
+  else if (role == Qt::EditRole)
+  {
+    return QVariant::fromValue<ctkXnatObject*>(this->xnatObject(index));
+  }
 
   return QVariant();
 }
@@ -148,7 +153,7 @@ bool ctkXnatTreeModel::hasChildren(const QModelIndex& index) const
   }
 
   ctkXnatTreeItem* item = this->itemAt(index);
-  return !item->xnatObject()->isFetched() || (item->childCount() > 0);
+  return !item->xnatObject()->isFetched() || (item->childCount() > 0) || m_RootItem->child(0)->xnatObject()->children().count() > 0;
 }
 
 bool ctkXnatTreeModel::canFetchMore(const QModelIndex& index) const
@@ -157,8 +162,8 @@ bool ctkXnatTreeModel::canFetchMore(const QModelIndex& index) const
   {
     return false;
   }
-
-  return !this->xnatObject(index)->isFetched();
+  ctkXnatTreeItem* item = this->itemAt(index);
+  return !(item->childCount() > 0);
 }
 
 void ctkXnatTreeModel::fetchMore(const QModelIndex& index)
@@ -172,7 +177,10 @@ void ctkXnatTreeModel::fetchMore(const QModelIndex& index)
 
   ctkXnatObject* xnatObject = item->xnatObject();
 
-  xnatObject->fetch();
+  if ( !xnatObject->isFetched() )
+  {
+    xnatObject->fetch();
+  }
 
   QList<ctkXnatObject*> children = xnatObject->children();
   if (!children.isEmpty())
@@ -263,3 +271,12 @@ void ctkXnatTreeModel::uploadFile(const QModelIndex& index, const QString& zipFi
 
   child->upload(zipFileName);
 }
+
+QVariant ctkXnatTreeModel::headerData(int /*section*/, Qt::Orientation /*orientation*/, int role) const
+{
+  if (role == Qt::DisplayRole)
+  {
+    if (!m_RootItem) return QString("Unavailable");
+  }
+  return QVariant();
+}

+ 2 - 0
Libs/XNAT/Widgets/ctkXnatTreeModel.h

@@ -38,6 +38,7 @@ public:
   explicit ctkXnatTreeModel();
   virtual ~ctkXnatTreeModel();
 
+  QVariant headerData(int /*section*/, Qt::Orientation /*orientation*/, int role) const;
   virtual QVariant data(const QModelIndex& index, int role) const;
   virtual QModelIndex parent(const QModelIndex& child) const;
   virtual QModelIndex index(int row, int column, const QModelIndex& parent) const;
@@ -61,6 +62,7 @@ private:
   ctkXnatTreeItem* itemAt(const QModelIndex& index) const;
 
   ctkXnatTreeItem* m_RootItem;
+
 };
 
 #endif