Commit 17112328 authored by Dirk Wilden's avatar Dirk Wilden
Browse files

fixed the contextMenu drag&click bug

added basic drag & drop for the treemodel

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@4098 383ad7c9-94d9-4d36-a494-682f7c89f535
parent 64ba2bf3
......@@ -84,6 +84,7 @@ void DataControlPlugin::pluginsInitialized() {
contextMenu->addAction(sourceAction_);
emit addContextMenu(contextMenu , DATA_ALL , CONTEXTTOPLEVELMENU);
}
bool DataControlPlugin::initializeToolbox(QWidget*& _widget)
......@@ -108,6 +109,10 @@ bool DataControlPlugin::initializeToolbox(QWidget*& _widget)
view_->setContextMenuPolicy(Qt::CustomContextMenu);
view_->setDragEnabled(true);
view_->setAcceptDrops(true);
view_->setDropIndicatorShown(true);
view_->setSelectionBehavior(QAbstractItemView::SelectRows);
view_->setSelectionMode(QAbstractItemView::ExtendedSelection);
......@@ -120,6 +125,7 @@ bool DataControlPlugin::initializeToolbox(QWidget*& _widget)
connect( view_,SIGNAL(customContextMenuRequested ( const QPoint & ) ),
this,SLOT(slotCustomContextMenuRequested ( const QPoint & ) ));
MeshDialogLayout_->addWidget( view_ , 0,0 );
viewHeader_ = view_->header();
......
#include "Menu.hh"
Menu::Menu(QWidget* _parent) : QMenu(_parent)
{
pressed_ = false;
}
void Menu::mouseReleaseEvent(QMouseEvent * _event)
{
if (pressed_)
QMenu::mouseReleaseEvent(_event);
pressed_ = false;
}
void Menu::mousePressEvent(QMouseEvent * _event)
{
pressed_ = true;
QMenu::mousePressEvent(_event);
}
#include <QMenu>
#include <QMouseEvent>
class Menu : public QMenu
{
bool pressed_;
public:
Menu(QWidget* _parent = 0);
protected:
void mousePressEvent(QMouseEvent* _event);
void mouseReleaseEvent(QMouseEvent* _event);
};
\ No newline at end of file
......@@ -41,6 +41,7 @@
#include <QInputDialog>
#include <QMessageBox>
#include "Menu.hh"
/// Slot for Remove action in ContextMenu
void DataControlPlugin::slotPopupRemove ( ) {
......@@ -178,15 +179,19 @@ void DataControlPlugin::slotGroup ( ) {
void DataControlPlugin::slotCustomContextMenuRequested ( const QPoint & _pos ) {
popupIndex_ = view_->indexAt(_pos);
if (!popupIndex_.isValid())
return;
BaseObject* item = model_->getItem(popupIndex_);
QItemSelectionModel* selection = view_->selectionModel();
// Get all selected rows
QModelIndexList indexList = selection->selectedRows ( 0 );
int selectedRows = indexList.size();
QMenu menu(0);
Menu menu(0);
QAction* action;
QIcon icon;
if ( selectedRows > 1 ) {
......
......@@ -187,17 +187,27 @@ Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return 0;
Qt::ItemFlags flags = 0;
// Show/Source/Target
if ( ( index.column() == 1 ) ||
( index.column() == 2 ) ||
( index.column() == 3 ) )
return Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
flags = Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
else
if ( index.column() == 0 )
return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
return Qt::ItemIsEnabled;
flags = Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
else
flags = Qt::ItemIsEnabled;
// Get the corresponding tree item
BaseObject *item = static_cast<BaseObject*>(index.internalPointer());
if ( item->isGroup() )
return flags | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
else
return flags | Qt::ItemIsDragEnabled;
}
/// Returns the data in the header
......@@ -611,4 +621,91 @@ bool TreeModel::isRoot(BaseObject * _item) {
return ( _item == rootItem_ );
}
//-------------------------------------------------------------
// Drag Drop - Stuff
//-------------------------------------------------------------
Qt::DropActions TreeModel::supportedDropActions() const
{
return /*Qt::CopyAction |*/ Qt::MoveAction;
}
QStringList TreeModel::mimeTypes() const
{
QStringList types;
types << "DataControl/dragDrop";
return types;
}
QMimeData* TreeModel::mimeData(const QModelIndexList& indexes) const
{
QMimeData *mimeData = new QMimeData();
QByteArray encodedData;
QDataStream stream(&encodedData, QIODevice::WriteOnly);
QVector< int > rows;
foreach (QModelIndex index, indexes) {
if (index.isValid()) {
if (!rows.contains( index.row() ) ){
BaseObject *item = getItem(index);
stream << item->id();
rows.push_back( index.row() );
}
}
}
mimeData->setData("DataControl/dragDrop", encodedData);
return mimeData;
}
bool TreeModel::dropMimeData(const QMimeData *data,
Qt::DropAction action, int /*row*/, int /*column*/, const QModelIndex &parent)
{
if (action == Qt::IgnoreAction)
return true;
if (!data->hasFormat("DataControl/dragDrop"))
return false;
QByteArray encodedData = data->data("DataControl/dragDrop");
QDataStream stream(&encodedData, QIODevice::ReadOnly);
QVector< int > ids;
while (!stream.atEnd()) {
int id;
stream >> id;
ids.push_back( id );
}
if (ids.count() != 1)
return false;
//get new parent
BaseObject *newParent = getItem(parent);
if ( newParent == 0 || !newParent->isGroup() )
return false;
BaseObject* item = 0;
if (PluginFunctions::get_object(ids[0], item)){
item->parent()->removeChild(item);
item->setParent( newParent );
newParent->appendChild( item );
// emit dataChanged(QModelIndex(),QModelIndex());
//TODO do something better than reset
reset();
return true;
}
return false;
}
......@@ -83,6 +83,16 @@ public:
bool isRoot(BaseObject * _item);
bool getObjectName(BaseObject* _object , QString& _name);
//drag & Drop
Qt::DropActions supportedDropActions() const;
QStringList mimeTypes() const;
QMimeData* mimeData(const QModelIndexList& indexes) const;
bool dropMimeData(const QMimeData *data,
Qt::DropAction action, int row, int column, const QModelIndex &parent);
private:
/// Rootitem of the tree
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment