Skip to content
Snippets Groups Projects
Commit 50d0358c authored by Van Herck, Walter's avatar Van Herck, Walter
Browse files

Smarter drag and drop in treeview (GUI indicates permitted drop

operations)
parent cb137c3e
No related branches found
No related tags found
No related merge requests found
...@@ -27,7 +27,6 @@ enum Column { ...@@ -27,7 +27,6 @@ enum Column {
ModelType, ModelType,
MaxColumns MaxColumns
}; };
const QString MimeType = "application/org.bornagainproject.xml.item.z";
} }
SessionModel::SessionModel(QObject *parent) SessionModel::SessionModel(QObject *parent)
...@@ -46,8 +45,14 @@ Qt::ItemFlags SessionModel::flags(const QModelIndex &index) const ...@@ -46,8 +45,14 @@ Qt::ItemFlags SessionModel::flags(const QModelIndex &index) const
Qt::ItemFlags result_flags = QAbstractItemModel::flags(index); Qt::ItemFlags result_flags = QAbstractItemModel::flags(index);
if (index.isValid()) { if (index.isValid()) {
result_flags |= Qt::ItemIsSelectable|Qt::ItemIsEnabled result_flags |= Qt::ItemIsSelectable|Qt::ItemIsEnabled
|Qt::ItemIsEditable|Qt::ItemIsDragEnabled |Qt::ItemIsEditable|Qt::ItemIsDragEnabled;
|Qt::ItemIsDropEnabled; QList<QString> acceptable_child_items = getAcceptableChildItems(index);
if (acceptable_child_items.contains(m_dragged_item_type)) {
result_flags |= Qt::ItemIsDropEnabled;
}
}
else {
result_flags |= Qt::ItemIsDropEnabled;
} }
return result_flags; return result_flags;
} }
...@@ -154,7 +159,7 @@ bool SessionModel::removeRows(int row, int count, const QModelIndex &parent) ...@@ -154,7 +159,7 @@ bool SessionModel::removeRows(int row, int count, const QModelIndex &parent)
QStringList SessionModel::mimeTypes() const QStringList SessionModel::mimeTypes() const
{ {
return QStringList() << MimeType; return QStringList() << SessionXML::MimeType;
} }
QMimeData *SessionModel::mimeData(const QModelIndexList &indices) const QMimeData *SessionModel::mimeData(const QModelIndexList &indices) const
...@@ -165,7 +170,8 @@ QMimeData *SessionModel::mimeData(const QModelIndexList &indices) const ...@@ -165,7 +170,8 @@ QMimeData *SessionModel::mimeData(const QModelIndexList &indices) const
QByteArray xml_data; QByteArray xml_data;
QXmlStreamWriter writer(&xml_data); QXmlStreamWriter writer(&xml_data);
writeItemAndChildItems(&writer, item); writeItemAndChildItems(&writer, item);
mime_data->setData(MimeType, qCompress(xml_data, MaxCompression)); mime_data->setData(SessionXML::MimeType,
qCompress(xml_data, MaxCompression));
return mime_data; return mime_data;
} }
return 0; return 0;
...@@ -178,10 +184,10 @@ bool SessionModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, ...@@ -178,10 +184,10 @@ bool SessionModel::canDropMimeData(const QMimeData *data, Qt::DropAction action,
(void)row; (void)row;
if (action == Qt::IgnoreAction) return true; if (action == Qt::IgnoreAction) return true;
if (action != Qt::MoveAction || column > 0 || !data if (action != Qt::MoveAction || column > 0 || !data
|| !data->hasFormat(MimeType)) return false; || !data->hasFormat(SessionXML::MimeType)) return false;
if (!parent.isValid()) return true; if (!parent.isValid()) return true;
QList<QString> acceptable_child_items = getAcceptableChildItems(parent); QList<QString> acceptable_child_items = getAcceptableChildItems(parent);
QByteArray xml_data = qUncompress(data->data(MimeType)); QByteArray xml_data = qUncompress(data->data(SessionXML::MimeType));
QXmlStreamReader reader(xml_data); QXmlStreamReader reader(xml_data);
while (!reader.atEnd()) { while (!reader.atEnd()) {
reader.readNext(); reader.readNext();
...@@ -201,10 +207,10 @@ bool SessionModel::dropMimeData(const QMimeData *data, Qt::DropAction action, ...@@ -201,10 +207,10 @@ bool SessionModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
{ {
if (action == Qt::IgnoreAction) return true; if (action == Qt::IgnoreAction) return true;
if (action != Qt::MoveAction || column > 0 || !data if (action != Qt::MoveAction || column > 0 || !data
|| !data->hasFormat(MimeType)) return false; || !data->hasFormat(SessionXML::MimeType)) return false;
if (!canDropMimeData(data, action, row, column, parent)) return false; if (!canDropMimeData(data, action, row, column, parent)) return false;
if (ParameterizedItem *item = itemForIndex(parent)) { if (ParameterizedItem *item = itemForIndex(parent)) {
QByteArray xml_data = qUncompress(data->data(MimeType)); QByteArray xml_data = qUncompress(data->data(SessionXML::MimeType));
QXmlStreamReader reader(xml_data); QXmlStreamReader reader(xml_data);
if (row == -1) row = item->childItemCount(); if (row == -1) row = item->childItemCount();
beginInsertRows(parent, row, row); beginInsertRows(parent, row, row);
......
...@@ -23,6 +23,8 @@ ...@@ -23,6 +23,8 @@
#include "ParameterizedItem.h" #include "ParameterizedItem.h"
namespace SessionXML { namespace SessionXML {
const QString MimeType = "application/org.bornagainproject.xml.item.z";
const QString ItemTag("Item"); const QString ItemTag("Item");
const QString ModelTypeAttribute("ModelType"); const QString ModelTypeAttribute("ModelType");
const QString ItemNameAttribute("ItemName"); const QString ItemNameAttribute("ItemName");
...@@ -82,6 +84,11 @@ public: ...@@ -82,6 +84,11 @@ public:
void load(const QString &filename=QString()); void load(const QString &filename=QString());
void save(const QString &filename=QString()); void save(const QString &filename=QString());
// Sets mimedata pointer of item being dragged
void setDraggedItemType(const QString& type) {
m_dragged_item_type = type;
}
private: private:
ParameterizedItem *insertNewItem(QString model_type, ParameterizedItem *insertNewItem(QString model_type,
ParameterizedItem *parent, ParameterizedItem *parent,
...@@ -96,6 +103,7 @@ private: ...@@ -96,6 +103,7 @@ private:
const char *property_name) const; const char *property_name) const;
QString m_filename; QString m_filename;
ParameterizedItem *m_root_item; ParameterizedItem *m_root_item;
QString m_dragged_item_type;
}; };
#endif // SESSIONMODEL_H #endif // SESSIONMODEL_H
// ************************************************************************** //
//
// BornAgain: simulate and fit scattering at grazing incidence
//
//! @file Views/SampleDesigner/ItemTreeView.cpp
//! @brief Implements class ItemTreeView.
//!
//! @homepage http://apps.jcns.fz-juelich.de/BornAgain
//! @license GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2013
//! @authors Scientific Computing Group at MLZ Garching
//! @authors C. Durniak, G. Pospelov, W. Van Herck, J. Wuttke
//
// ************************************************************************** //
#include "ItemTreeView.h"
#include "SessionModel.h"
#include "GUIHelpers.h"
#include <QDragMoveEvent>
#include <QMimeData>
ItemTreeView::ItemTreeView(QWidget *parent)
: QTreeView(parent)
{
}
ItemTreeView::~ItemTreeView()
{
}
void ItemTreeView::dragMoveEvent(QDragMoveEvent *event)
{
QTreeView::dragMoveEvent(event);
SessionModel *model = static_cast<SessionModel *>(this->model());
model->setDraggedItemType(QString());
QByteArray xml_data = qUncompress(
event->mimeData()->data(SessionXML::MimeType));
QXmlStreamReader reader(xml_data);
while (!reader.atEnd()) {
reader.readNext();
if (reader.isStartElement()) {
if (reader.name() == SessionXML::ItemTag) {
const QString model_type = reader.attributes()
.value(SessionXML::ModelTypeAttribute).toString();
model->setDraggedItemType(model_type);
break;
}
}
}
}
#ifndef ITEMTREEVIEW_H
#define ITEMTREEVIEW_H
#include <QTreeView>
class ItemTreeView : public QTreeView
{
Q_OBJECT
public:
explicit ItemTreeView(QWidget *parent=0);
~ItemTreeView();
protected:
#ifndef QT_NO_DRAGANDDROP
void dragMoveEvent(QDragMoveEvent *event);
#endif
};
#endif // ITEMTREEVIEW_H
...@@ -41,10 +41,10 @@ SamplePropertyEditor *SampleViewComponents::createPropertyEditor( ...@@ -41,10 +41,10 @@ SamplePropertyEditor *SampleViewComponents::createPropertyEditor(
return new SamplePropertyEditor(selection_model, parent); return new SamplePropertyEditor(selection_model, parent);
} }
QTreeView *SampleViewComponents::createTreeView( ItemTreeView *SampleViewComponents::createTreeView(
SessionModel *session_model, QWidget *parent) SessionModel *session_model, QWidget *parent)
{ {
QTreeView *tree_view = new QTreeView(parent); ItemTreeView *tree_view = new ItemTreeView(parent);
tree_view->setModel(session_model); tree_view->setModel(session_model);
tree_view->setAllColumnsShowFocus(true); tree_view->setAllColumnsShowFocus(true);
tree_view->setWindowTitle(QString("Object Tree View")); tree_view->setWindowTitle(QString("Object Tree View"));
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
#define SAMPLEVIEWCOMPONENTS_H #define SAMPLEVIEWCOMPONENTS_H
#include <QWidget> #include <QWidget>
#include <QTreeView> #include "ItemTreeView.h"
#include "widgetbox.h" #include "widgetbox.h"
#include "SamplePropertyEditor.h" #include "SamplePropertyEditor.h"
...@@ -45,7 +45,7 @@ public: ...@@ -45,7 +45,7 @@ public:
SampleDesignerInterface *core, QWidget *parent); SampleDesignerInterface *core, QWidget *parent);
static SamplePropertyEditor *createPropertyEditor( static SamplePropertyEditor *createPropertyEditor(
QItemSelectionModel *selection_model, QWidget *parent); QItemSelectionModel *selection_model, QWidget *parent);
static QTreeView *createTreeView( static ItemTreeView *createTreeView(
SessionModel *session_model, QWidget *parent); SessionModel *session_model, QWidget *parent);
static SampleInfoStreamInterface *createInfoStream(QWidget *parent); static SampleInfoStreamInterface *createInfoStream(QWidget *parent);
}; };
......
...@@ -46,7 +46,7 @@ bool question(QWidget *parent, const QString &title, ...@@ -46,7 +46,7 @@ bool question(QWidget *parent, const QString &title,
const QString &noText=QObject::tr("&No")); const QString &noText=QObject::tr("&No"));
bool okToDelete(QWidget *parent, const QString &title, bool okToDelete(QWidget *parent, const QString &title,
const QString &text, const QString &detailedText=QString()); const QString &text, const QString &detailedText=QString());
}; }
#endif // GUIHELPERS_H #endif // GUIHELPERS_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment