work save: drag & drop in project view

This commit is contained in:
royqh1979 2021-10-24 00:17:08 +08:00
parent 9937ef50c3
commit 06f035926a
3 changed files with 35 additions and 1 deletions

View File

@ -131,6 +131,9 @@
<property name="selectionBehavior"> <property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum> <enum>QAbstractItemView::SelectRows</enum>
</property> </property>
<property name="autoExpandDelay">
<number>500</number>
</property>
<attribute name="headerVisible"> <attribute name="headerVisible">
<bool>false</bool> <bool>false</bool>
</attribute> </attribute>

View File

@ -602,6 +602,8 @@ PFolderNode Project::pointerToNode(FolderNode *p, PFolderNode parent)
if (!parent) { if (!parent) {
parent = mNode; parent = mNode;
} }
if (p==mNode.get())
return mNode;
foreach (const PFolderNode& node , parent->children) { foreach (const PFolderNode& node , parent->children) {
if (node.get()==p) if (node.get()==p)
return node; return node;
@ -1765,6 +1767,8 @@ QModelIndex ProjectModel::index(int row, int column, const QModelIndex &parent)
if (!parentNode) { if (!parentNode) {
return QModelIndex(); return QModelIndex();
} }
if (row<0 || row>=parentNode->children.count())
return QModelIndex();
return createIndex(row,column,parentNode->children[row].get()); return createIndex(row,column,parentNode->children[row].get());
} }
@ -1942,12 +1946,36 @@ bool ProjectModel::canDropMimeData(const QMimeData *data, Qt::DropAction action,
{ {
if (action != Qt::MoveAction) if (action != Qt::MoveAction)
return false; return false;
QModelIndex idx = index(row,column,parent); QModelIndex idx;
if (row >= rowCount(parent) || row < 0) {
qDebug()<<"above/below";
idx = parent;
} else {
idx= index(row,column,parent);
}
if (!idx.isValid()) if (!idx.isValid())
return false; return false;
FolderNode* p = static_cast<FolderNode*>(idx.internalPointer()); FolderNode* p = static_cast<FolderNode*>(idx.internalPointer());
PFolderNode node = mProject->pointerToNode(p); PFolderNode node = mProject->pointerToNode(p);
qDebug()<<node->text;
if (node->unitIndex>=0) if (node->unitIndex>=0)
return false; return false;
return true; return true;
} }
QStringList ProjectModel::mimeTypes() const
{
QStringList t=QAbstractItemModel::mimeTypes();
return t;
}
QMimeData *ProjectModel::mimeData(const QModelIndexList &indexes) const
{
QMimeData * d= QAbstractItemModel::mimeData(indexes);
return d;
}
Qt::DropActions ProjectModel::supportedDropActions() const
{
return Qt::MoveAction;
}

View File

@ -101,6 +101,9 @@ public:
// QAbstractItemModel interface // QAbstractItemModel interface
public: public:
bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const override; bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const override;
QStringList mimeTypes() const override;
QMimeData *mimeData(const QModelIndexList &indexes) const override;
Qt::DropActions supportedDropActions() const override;
}; };
class ProjectTemplate; class ProjectTemplate;