Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drag drop #262

Merged
merged 5 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ Begin...
End...
(Stats(profile).strip_dirs().sort_stats(SortKey.CUMULATIVE).print_stats()) #end cProfile

#### Debugging on a conventional install: linux
- 'sudo apt install python3-pudb' (not pip install)
- create small 'temp.py' into any folder, with this content
from pasta_eln.gui import startMain
startMain()
- start with 'pudb3 temp.py'


#### General notes
- Find qt-awesome icons: qta-browser
Expand Down
2 changes: 1 addition & 1 deletion pasta_eln/GUI/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def execute(self, command:list[Any]) -> None:
self.comm.changeTable.emit('x0','')
elif command[0] is Command.SCAN:
self.comm.backend.scanProject(self.comm.progressBar, self.projID, self.docProj['-branch'][0]['path'])
self.comm.changeSidebar.emit('redraw')
self.comm.changeProject.emit(self.projID,'')
showMessage(self, 'Information','Scanning finished')
elif command[0] is Command.SHOW_PROJ_DETAILS:
self.docProj['-gui'][0] = not self.docProj['-gui'][0]
Expand Down
70 changes: 63 additions & 7 deletions pasta_eln/GUI/projectTreeView.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from enum import Enum
from pathlib import Path
from typing import Any
from PySide6.QtWidgets import QWidget, QTreeView, QAbstractItemView, QMenu, QMessageBox # pylint: disable=no-name-in-module
from PySide6.QtGui import QStandardItemModel, QStandardItem # pylint: disable=no-name-in-module
from PySide6.QtWidgets import QWidget, QTreeView, QMenu, QMessageBox # pylint: disable=no-name-in-module
from PySide6.QtGui import QStandardItemModel, QStandardItem, QMouseEvent # pylint: disable=no-name-in-module
from PySide6.QtCore import QPoint, Qt # pylint: disable=no-name-in-module
from .projectLeafRenderer import ProjectLeafRenderer
from ..guiStyle import Action, showMessage
Expand All @@ -18,11 +18,14 @@ def __init__(self, parent:QWidget, comm:Communicate, model:QStandardItemModel):
self.setModel(model)
self.setHeaderHidden(True)
self.setStyleSheet('QTreeView::branch {border-image: none;}')
self.setExpandsOnDoubleClick(False)
self.setIndentation(40)
self.renderer = ProjectLeafRenderer(self.comm)
self.setItemDelegate(self.renderer)
self.setDragDropMode(QAbstractItemView.InternalMove)
self.setExpandsOnDoubleClick(False)
self.setAcceptDrops(True)
self.setDropIndicatorShown(True)
self.setDefaultDropAction(Qt.MoveAction)
self.setDragDropMode(QTreeView.InternalMove)
self.doubleClicked.connect(self.tree2Clicked)


Expand Down Expand Up @@ -131,9 +134,7 @@ def execute(self, command:list[Any]) -> None:
logging.debug('hide stack %s',str(hierStack))
self.comm.backend.db.hideShow(hierStack)
# self.comm.changeProject.emit('','') #refresh project
# after hide, not immediately hidden but on next refresh
# TODO Comment out for now to keep consistent with hide via context menu directly or form (which does
# not know it is a project )
# after hide, do not hide immediately but wait on next refresh
elif command[0] is Command.OPEN_EXTERNAL or command[0] is Command.OPEN_FILEBROWSER:
# depending if non-folder / folder; address different item in hierstack
docID = hierStack[-2] \
Expand Down Expand Up @@ -171,6 +172,61 @@ def tree2Clicked(self) -> None:
return


def dragEnterEvent(self, event:QMouseEvent) -> None:
"""
Override default: what happens if you drag an item

Args:
event (QMouseEvent): event
"""
event.acceptProposedAction()
return


def dropEvent(self, event:QMouseEvent) -> None:
"""
Override default: what happens at end of drag&drop

Args:
event (QMouseEvent): event
"""
if event.mimeData().hasUrls():
item = self.model().itemFromIndex(self.indexAt(event.pos()))
if item.data()['docType'][0][0]!='x':
showMessage(self, 'Error', 'You cannot drop files on non folders.')
return
# create a list of all files
files, folders = [], []
for url in event.mimeData().urls():
path = url.toLocalFile()
if Path(path).is_file():
files.append(path)
else:
files += list(Path(path).rglob("*"))
folders += [x[0] for x in os.walk(path)]
docID = item.data()['hierStack'].split('/')[-1]
doc = self.comm.backend.db.getDoc(docID)
targetFolder = Path(self.comm.backend.cwd/doc['-branch'][0]['path'])
commonBase = os.path.commonpath(folders+[str(i) for i in files])
# create folders and copy files
for folder in folders:
(targetFolder/(Path(folder).relative_to(commonBase))).mkdir(parents=True, exist_ok=True)
for fileStr in files:
file = Path(fileStr)
if file.is_file():
shutil.copy(file, targetFolder/(file.relative_to(commonBase)))
# scan
self.comm.backend.scanProject(self.comm.progressBar, docID, str(targetFolder) )
self.comm.changeProject.emit(item.data()['hierStack'].split('/')[0],'')
showMessage(self, 'Information','Drag & drop is finished')
event.ignore()
elif 'application/x-qstandarditemmodeldatalist' in event.mimeData().formats():
super().dropEvent(event)
else:
logging.error('Drop unknown data: %s', event.mimeData().formats())
return


class Command(Enum):
""" Commands used in this file """
ADD_CHILD = 1
Expand Down
Loading