| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392 | 
							- /*=============================================================================
 
-   Library: XNAT/Core
 
-   Copyright (c) University College London,
 
-     Centre for Medical Image Computing
 
-   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 "ctkXnatTreeModel.h"
 
- #include "ctkXnatDataModel.h"
 
- #include "ctkXnatObject.h"
 
- #include "ctkXnatTreeItem_p.h"
 
- #include <QList>
 
- class ctkXnatTreeModelPrivate
 
- {
 
- public:
 
-   ctkXnatTreeModelPrivate()
 
-     : m_RootItem(new ctkXnatTreeItem())
 
-   {
 
-   }
 
-   ctkXnatTreeItem* itemAt(const QModelIndex& index) const
 
-   {
 
-     return static_cast<ctkXnatTreeItem*>(index.internalPointer());
 
-   }
 
-   QScopedPointer<ctkXnatTreeItem> m_RootItem;
 
- };
 
- //----------------------------------------------------------------------------
 
- ctkXnatTreeModel::ctkXnatTreeModel()
 
- : d_ptr(new ctkXnatTreeModelPrivate())
 
- {
 
- }
 
- //----------------------------------------------------------------------------
 
- ctkXnatTreeModel::~ctkXnatTreeModel()
 
- {
 
- }
 
- //----------------------------------------------------------------------------
 
- // returns name (project, subject, etc.) for row and column of
 
- //   parent in index if role is Qt::DisplayRole
 
- QVariant ctkXnatTreeModel::data(const QModelIndex& index, int role) const
 
- {
 
-   if (!index.isValid())
 
-   {
 
-     return QVariant();
 
-   }
 
-   if (role == Qt::TextAlignmentRole)
 
-   {
 
-     return QVariant(int(Qt::AlignTop | Qt::AlignLeft));
 
-   }
 
-   else if (role == Qt::DisplayRole)
 
-   {
 
-     ctkXnatObject* xnatObject = this->xnatObject(index);
 
-     QString displayData = xnatObject->name();
 
-     if (displayData.isEmpty())
 
-     {
 
-       displayData = xnatObject->property(ctkXnatObject::LABEL);
 
-     }
 
-     return displayData;
 
-   }
 
-   else if (role == Qt::ToolTipRole)
 
-   {
 
-     return this->xnatObject(index)->description();
 
-   }
 
-   else if (role == Qt::UserRole)
 
-   {
 
-     return QVariant::fromValue<ctkXnatObject*>(this->xnatObject(index));
 
-   }
 
-   return QVariant();
 
- }
 
- //----------------------------------------------------------------------------
 
- QModelIndex ctkXnatTreeModel::index(int row, int column, const QModelIndex& index) const
 
- {
 
-   if (!this->hasIndex(row, column, index))
 
-   {
 
-     return QModelIndex();
 
-   }
 
-   Q_D(const ctkXnatTreeModel);
 
-   ctkXnatTreeItem* item;
 
-   if (!index.isValid())
 
-   {
 
-     item = d->m_RootItem.data();
 
-   }
 
-   else
 
-   {
 
-     item = d->itemAt(index);
 
-   }
 
-   ctkXnatTreeItem* childItem = item->child(row);
 
-   if (childItem)
 
-   {
 
-     return this->createIndex(row, column, childItem);
 
-   }
 
-   return QModelIndex();
 
- }
 
- //----------------------------------------------------------------------------
 
- QModelIndex ctkXnatTreeModel::parent(const QModelIndex& index) const
 
- {
 
-   if (!index.isValid())
 
-   {
 
-     return QModelIndex();
 
-   }
 
-   Q_D(const ctkXnatTreeModel);
 
-   ctkXnatTreeItem* item = d->itemAt(index);
 
-   ctkXnatTreeItem* parentItem = item->parent();
 
-   if (parentItem == d->m_RootItem.data())
 
-   {
 
-     return QModelIndex();
 
-   }
 
-   return this->createIndex(parentItem->row(), 0, parentItem);
 
- }
 
- //----------------------------------------------------------------------------
 
- int ctkXnatTreeModel::rowCount(const QModelIndex& index) const
 
- {
 
-   if (index.column() > 0)
 
-   {
 
-     return 0;
 
-   }
 
-   Q_D(const ctkXnatTreeModel);
 
-   ctkXnatTreeItem* item;
 
-   if (!index.isValid())
 
-   {
 
-     item = d->m_RootItem.data();
 
-   }
 
-   else
 
-   {
 
-     item = d->itemAt(index);
 
-   }
 
-   return item->childCount();
 
- }
 
- //----------------------------------------------------------------------------
 
- int ctkXnatTreeModel::columnCount(const QModelIndex& index) const
 
- {
 
-   Q_UNUSED(index);
 
-   return 1;
 
- }
 
- //----------------------------------------------------------------------------
 
- // defer request for children until actually needed by QTreeView object
 
- bool ctkXnatTreeModel::hasChildren(const QModelIndex& index) const
 
- {
 
-   Q_D(const ctkXnatTreeModel);
 
-   if (!index.isValid())
 
-   {
 
-     return d->m_RootItem->childCount() > 0;
 
-   }
 
-   ctkXnatTreeItem* item = d->itemAt(index);
 
-   return item->xnatObject() || !item->xnatObject()->isFetched() || !item->xnatObject()->children().isEmpty();
 
- }
 
- //----------------------------------------------------------------------------
 
- bool ctkXnatTreeModel::canFetchMore(const QModelIndex& index) const
 
- {
 
-   if (!index.isValid())
 
-   {
 
-     return false;
 
-   }
 
-   Q_D(const ctkXnatTreeModel);
 
-   ctkXnatTreeItem* item = d->itemAt(index);
 
-   return !(item->childCount() > 0);
 
- }
 
- //----------------------------------------------------------------------------
 
- void ctkXnatTreeModel::fetchMore(const QModelIndex& index)
 
- {
 
-   if (!index.isValid())
 
-   {
 
-     return;
 
-   }
 
-   Q_D(const ctkXnatTreeModel);
 
-   ctkXnatTreeItem* item = d->itemAt(index);
 
-   ctkXnatObject* xnatObject = item->xnatObject();
 
-   xnatObject->fetch();
 
-   QList<ctkXnatObject*> children = xnatObject->children();
 
-   if (!children.isEmpty())
 
-   {
 
-     beginInsertRows(index, 0, children.size() - 1);
 
-     foreach (ctkXnatObject* child, children)
 
-     {
 
-       item->appendChild(new ctkXnatTreeItem(child, item));
 
-     }
 
-     endInsertRows();
 
-   }
 
- }
 
- //----------------------------------------------------------------------------
 
- void ctkXnatTreeModel::refresh(const QModelIndex& parent)
 
- {
 
-   if (!parent.isValid())
 
-   {
 
-     return;
 
-   }
 
-   Q_D(const ctkXnatTreeModel);
 
-   ctkXnatTreeItem* item = d->itemAt(parent);
 
-   ctkXnatObject* xnatObject = item->xnatObject();
 
-   // Do this just for xnatObjects that are already fetched.
 
-   // Otherwise we would retrieve all data from XNAT
 
-   if (xnatObject->isFetched())
 
-   {
 
-     // Force a fetch for current object (it might has changed on the server)
 
-     xnatObject->fetch(true);
 
-     int numChildren = rowCount(parent);
 
-     bool addToTreeView (true);
 
-     // For all children, check if they are already in the treeview,
 
-     // if not -> add them
 
-     // For all items of the treeview, check if they are still on the server
 
-     // if not -> remove them
 
-     QList<ctkXnatObject*> children = xnatObject->children();
 
-     QMutableListIterator<ctkXnatObject*> iter (children);
 
-     while (iter.hasNext())
 
-     {
 
-       ctkXnatObject* child = iter.next();
 
-       for (int i = 0; i < numChildren; ++i)
 
-       {
 
-         ctkXnatObject* childItemObject = item->child(i)->xnatObject();
 
-         // If the item was deleted from the server in the meantime
 
-         // -> remove it from the treeview
 
-         if (!childItemObject->exists())
 
-         {
 
-           beginRemoveRows(parent, item->child(i)->row(), item->child(i)->row());
 
-           item->remove(childItemObject);
 
-           xnatObject->remove(child);
 
-           iter.remove();
 
-           endRemoveRows();
 
-           --numChildren;
 
-           --i;
 
-           addToTreeView = false;
 
-           break;
 
-         }
 
-         if ((childItemObject->id().length() != 0 && childItemObject->id() == child->id()) ||
 
-             (childItemObject->id().length() == 0 && childItemObject->name() == child->name()))
 
-         {
 
-           addToTreeView = false;
 
-           break;
 
-         }
 
-       }
 
-       // If the current xnatObject was created on the server in the meantime
 
-       // -> add it to the treeview
 
-       if (addToTreeView)
 
-       {
 
-         beginInsertRows(parent, 0, numChildren - 1);
 
-         item->appendChild(new ctkXnatTreeItem(child, item));
 
-         endInsertRows();
 
-         ++numChildren;
 
-       }
 
-       addToTreeView = true;
 
-     }
 
-     numChildren = rowCount(parent);
 
-     for (int i=0; i<numChildren; i++)
 
-     {
 
-       refresh(index(i,0,parent));
 
-     }
 
-   }
 
- }
 
- //----------------------------------------------------------------------------
 
- ctkXnatObject* ctkXnatTreeModel::xnatObject(const QModelIndex& index) const
 
- {
 
-   Q_D(const ctkXnatTreeModel);
 
-   return d->itemAt(index)->xnatObject();
 
- }
 
- //----------------------------------------------------------------------------
 
- void ctkXnatTreeModel::addDataModel(ctkXnatDataModel* dataModel)
 
- {
 
-   Q_D(ctkXnatTreeModel);
 
-   d->m_RootItem->appendChild(new ctkXnatTreeItem(dataModel, d->m_RootItem.data()));
 
- }
 
- //----------------------------------------------------------------------------
 
- void ctkXnatTreeModel::removeDataModel(ctkXnatDataModel* dataModel)
 
- {
 
-   Q_D(ctkXnatTreeModel);
 
-   d->m_RootItem->remove(dataModel);
 
- }
 
- //----------------------------------------------------------------------------
 
- bool ctkXnatTreeModel::removeAllRows(const QModelIndex& parent)
 
- {
 
-   // do nothing for the root
 
-   if ( !parent.isValid() )
 
-   {
 
-     return false;
 
-   }
 
-   ctkXnatObject* xnatObject = this->xnatObject(parent);
 
-   // nt: not sure why the parent.row() is used here instead of the first item in list
 
-   // that is xnatObject->children()[0];
 
-   ctkXnatObject* child = xnatObject->children()[parent.row()];
 
-   if ( child == NULL )
 
-   {
 
-     return false;
 
-   }
 
-   int numberofchildren = child->children().size();
 
-   if (numberofchildren > 0)
 
-   {
 
-     beginRemoveRows(parent, 0, numberofchildren - 1);
 
-     // xnatObject->removeChild(parent.row());
 
-     // nt: not sure if this is the right implementation here, should iterate ?
 
-     xnatObject->remove(child);
 
-     endRemoveRows();
 
-   }
 
-   else
 
-   {
 
-     // xnatObject->removeChild(parent.row());
 
-     // nt: not sure if this is the right implementation here, should iterate ?
 
-     xnatObject->remove(child);
 
-   }
 
-   return true;
 
- }
 
- //----------------------------------------------------------------------------
 
- void ctkXnatTreeModel::downloadFile(const QModelIndex& index, const QString& zipFileName)
 
- {
 
-   if (!index.isValid())
 
-   {
 
-     return;
 
-   }
 
-   this->xnatObject(index)->download(zipFileName);
 
-   return;
 
- }
 
- //----------------------------------------------------------------------------
 
- void ctkXnatTreeModel::addChildNode(const QModelIndex &index, ctkXnatObject* child)
 
- {
 
-   Q_D(ctkXnatTreeModel);
 
-   ctkXnatTreeItem* item = d->itemAt(index);
 
-   beginInsertRows(index, 0, 1);
 
-   item->appendChild(new ctkXnatTreeItem(child, item));
 
-   endInsertRows();
 
- }
 
 
  |