Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#include "resourcebrowserview.hpp"
#include <qjsonarray.h>
#include <QMimeData>
ResourceBrowserView::ResourceBrowserView(QWidget *parent) : QColumnView(parent)
{
setModel(new ResourceBrowserModel("studies",this));
connect(this,SIGNAL(updatePreviewWidget(QModelIndex)),SLOT(onUpdatePreview(QModelIndex)));
setSelectionMode(QAbstractItemView::ExtendedSelection);
setDragEnabled(true);
}
void ResourceBrowserView::onUpdatePreview(const QModelIndex &index)
{
ResourceBrowserModel *res_model=dynamic_cast<ResourceBrowserModel*>(model());
if(res_model){
QGraphicsView *view = qobject_cast<QGraphicsView*>(previewWidget());
if(!view){
view=new QGraphicsView(new QGraphicsScene,this);
setPreviewWidget(view);
}
res_model->updatePreview(index,view);
}
}
OrthancItem *ResourceBrowserModel::parentFromIndex(const QModelIndex &idx)const
{
OrthancItem *ret;
if(idx.isValid()){
ret = static_cast<OrthancItem *>(idx.internalPointer());
} else {
ret=const_cast<OrthancRoot*>(&root);
}
return ret;
}
ResourceBrowserModel::ResourceBrowserModel(QString request, QObject *parent):QAbstractItemModel(parent),root(request){
connect(&root,SIGNAL(childReady(QModelIndex,OrthancItem*,int)),SLOT(onChildReady(QModelIndex,OrthancItem*,int)));
}
void ResourceBrowserModel::updatePreview(const QModelIndex &index, QGraphicsView *preview)
{
static_cast<OrthancItem*>(index.internalPointer())->updatePreview(preview);
}
QModelIndex ResourceBrowserModel::index(int row, int column, const QModelIndex &parent) const
{
if(!hasIndex(row,column,parent))
return QModelIndex();
OrthancItem *parentItem=parentFromIndex(parent);
OrthancItem *childItem=parentItem->child(row);
const QModelIndex idx=childItem ? createIndex(row,column,const_cast<OrthancItem*>(childItem)) : QModelIndex();
childItem->setIndex(idx);
return idx;
}
QModelIndex ResourceBrowserModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
const OrthancItem *childItem = static_cast<OrthancItem*>(index.internalPointer());
const OrthancItem *parentItem = static_cast<OrthancItem*>(childItem->parent());
if (!parentItem || parentItem == &root)
return QModelIndex();
Q_ASSERT(parentItem->index().isValid());
return parentItem->index();
}
int ResourceBrowserModel::rowCount(const QModelIndex &parent) const
{
if (parent.column() > 0)
return 0;
const OrthancItem *parentItem=parentFromIndex(parent);
return parentItem->childCount();
}
int ResourceBrowserModel::columnCount(const QModelIndex &/*parent*/) const{return 1;}
QVariant ResourceBrowserModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role != Qt::DisplayRole)
return QVariant();
OrthancItem *item = static_cast<OrthancItem*>(index.internalPointer());
return item->data(index.column());
}
bool ResourceBrowserModel::hasChildren(const QModelIndex &parent) const
{
return parentFromIndex(parent)->hasChildren();
}
void ResourceBrowserModel::onChildReady(QModelIndex parent, OrthancItem *child, int at)
{
OrthancDynamicNode *p_node=qobject_cast<OrthancDynamicNode*>(child);
if(p_node)
connect(p_node,SIGNAL(childReady(QModelIndex,OrthancItem*,int)),SLOT(onChildReady(QModelIndex,OrthancItem*,int)),Qt::QueuedConnection);
beginInsertRows(parent,at,at);
endInsertRows();
}
bool ResourceBrowserModel::canFetchMore(const QModelIndex &parent) const
{
//use canFetchMore to signal item to populate
// if items tells us that there is nothing there, forward that to the view
OrthancItem *p=parentFromIndex(parent);
ResourceBrowserModel* me=const_cast<ResourceBrowserModel*>(this);
if(me->m_populating.loadAcquire()!=p){
me->m_populating.store(p);
bool ret=p->populate();
me->m_populating.storeRelease(NULL);
return ret;
} else {
qDebug() << "Already populating" << p;
return true;
}
}
QMimeData *ResourceBrowserModel::mimeData(const QModelIndexList &indexes) const
{
QMimeData *ret = new QMimeData;
QList<QUrl> urls;
foreach(QModelIndex index,indexes){
const QUrl url = static_cast<OrthancItem*>(index.internalPointer())->downloadUrl();
urls.append(url);
}
ret->setUrls(urls);
return ret;
}
Qt::DropActions ResourceBrowserModel::supportedDragActions() const
{
return Qt::CopyAction;
}
Qt::ItemFlags ResourceBrowserModel::flags(const QModelIndex &index) const
{
return index.isValid() ?
Qt::ItemIsDragEnabled | QAbstractItemModel::flags(index):
QAbstractItemModel::flags(index);
}