Skip to content

Commit

Permalink
Support Drag&Drop in uploads #85
Browse files Browse the repository at this point in the history
  • Loading branch information
lakewik authored Jun 18, 2017
1 parent b67d3ac commit 93a4927
Showing 1 changed file with 111 additions and 6 deletions.
117 changes: 111 additions & 6 deletions UI/file_upload.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-

from functools import partial
from sys import platform
import os

Expand Down Expand Up @@ -42,6 +42,7 @@ def __init__(self, parent=None, bucketid=None, fileid=None, dashboard_instance=N
QtGui.QWidget.__init__(self, parent)
self.ui_single_file_upload = Ui_SingleFileUpload()
self.ui_single_file_upload.setupUi(self)
self.setAcceptDrops(True)
# open bucket manager
QtCore.QObject.connect(
self.ui_single_file_upload.start_upload_bt,
Expand All @@ -64,6 +65,13 @@ def __init__(self, parent=None, bucketid=None, fileid=None, dashboard_instance=N
QtCore.SIGNAL('clicked()'),
self.handle_cancel_action)

self.ui_single_file_upload.files_queue_table_widget.setContextMenuPolicy(
QtCore.Qt.CustomContextMenu)

self.ui_single_file_upload.files_queue_table_widget. \
customContextMenuRequested.connect(
partial(self.display_files_table_context_menu))

self.already_used_farmers_nodes = []

self.tools = Tools()
Expand Down Expand Up @@ -116,6 +124,7 @@ def __init__(self, parent=None, bucketid=None, fileid=None, dashboard_instance=N
self.connect(self, QtCore.SIGNAL('setCurrentActiveConnections'), self.set_current_active_connections)
self.connect(self, QtCore.SIGNAL('setShardSize'), self.set_shard_size)
self.connect(self, QtCore.SIGNAL('createShardUploadThread'), self.createNewShardUploadThread)
self.connect(self, QtCore.SIGNAL('droppedFileToTable'), self.append_dropped_files_to_table)
# self.connect(self, QtCore.SIGNAL('handleCancelAction'), self.ha)

# resolve buckets and put to buckets combobox
Expand Down Expand Up @@ -146,6 +155,41 @@ def __init__(self, parent=None, bucketid=None, fileid=None, dashboard_instance=N

self.current_row = 0

def dragEnterEvent(self, event):
if event.mimeData().hasUrls:
event.accept()
else:
event.ignore()

def dragMoveEvent(self, event):
if event.mimeData().hasUrls:
event.setDropAction(QtCore.Qt.CopyAction)
event.accept()
else:
event.ignore()

def dropEvent(self, event):
if event.mimeData().hasUrls:
event.setDropAction(QtCore.Qt.CopyAction)
event.accept()
paths = []
for path in event.mimeData().urls():
paths.append(str(path.toLocalFile()))
self.emit(QtCore.SIGNAL("droppedFileToTable"), paths)
else:
event.ignore()

def resizeEvent(self, event):
current_window_width = self.frameGeometry().width()
if current_window_width < 980 and self.is_files_queue_table_visible:
#self.is_files_queue_table_visible = False
#self.ui_single_file_upload.files_list_view_bt.setPixmap(QtGui.QPixmap(":/resources/rarrow.png"))
print "Closed"
elif current_window_width > 980 and not self.is_files_queue_table_visible:
#self.is_files_queue_table_visible = True
#self.ui_single_file_upload.files_list_view_bt.setPixmap(QtGui.QPixmap(":/resources/larrow.jpg"))
print "Opened"

def keyPressEvent(self, e):
# copy upload queue table content to clipboard #
if (e.modifiers() & QtCore.Qt.ControlModifier):
Expand All @@ -163,6 +207,54 @@ def keyPressEvent(self, e):
s = s[:-1] + "\n" # eliminate last '\t'
self.clip.setText(s)

def append_dropped_files_to_table(self, paths):
row_data = {}
for url in paths:
if os.path.exists(url):
row_data["file_path"] = str(url)
self.add_row_files_queue_table(row_data)
print url
return True


def display_files_table_context_menu(self, position):
tablemodel = self.ui_single_file_upload.files_queue_table_widget.model()
rows = sorted(set(index.row() for index in
self.ui_single_file_upload.files_queue_table_widget.
selectedIndexes()))
i = 0
selected_row = 0
any_row_selected = False
for row in rows:
any_row_selected = True
filename_index = tablemodel.index(row, 0) # get shard Index
# We suppose data are strings
self.current_selected_file_name = str(tablemodel.data(
filename_index).toString())
selected_row = row
i += 1

if any_row_selected:
menu = QtGui.QMenu()
fileDeleteFromTableAction = menu.addAction('Delete file from table')
action = menu.exec_(self.ui_single_file_upload.files_queue_table_widget.mapToGlobal(position))

if action == fileDeleteFromTableAction:
# ask user and delete if sure
msgBox = QtGui.QMessageBox(
QtGui.QMessageBox.Question,
'Question',
'Are you sure that you want to remove file '
'"%s" from upload queue?' %
str(self.current_selected_file_name),
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)

result = msgBox.exec_()

if result == QtGui.QMessageBox.Yes:
self.ui_single_file_upload.files_queue_table_widget.removeRow(int(selected_row))
print "Delete action"

def shardUploadInitThread(self, shard, chapters, frame, file_name):
shard_upload_init_thread = threading.Thread(
target=self.createNewShardUploadThread(
Expand Down Expand Up @@ -190,11 +282,6 @@ def display_files_queue_change(self, x):
self.animation.start()


def hide_files_queue(self):

return True


def prepare_files_queue_table(self):
self.files_queue_table_header = ['File name', 'Path', 'Size', 'Progress']
self.ui_single_file_upload.files_queue_table_widget.setColumnCount(4)
Expand All @@ -219,6 +306,24 @@ def handle_cancel_action(self):
else:
self.close()


def add_row_files_queue_table(self, row_data):

self.files_queue_table_row_count = self.ui_single_file_upload.files_queue_table_widget.rowCount()

self.ui_single_file_upload.files_queue_table_widget.setRowCount(
self.files_queue_table_row_count + 1)

self.ui_single_file_upload.files_queue_table_widget.setItem(
self.files_queue_table_row_count, 0, QtGui.QTableWidgetItem(os.path.split(str(row_data['file_path']))[1]))
self.ui_single_file_upload.files_queue_table_widget.setItem(
self.files_queue_table_row_count, 1, QtGui.QTableWidgetItem(row_data['file_path']))
# self.ui_single_file_upload.files_queue_table_widget.setItem(
# self.upload_queue_table_row_count, 2, QtGui.QTableWidgetItem(
# '%s:%d' % (row_data['farmer_address'], row_data['farmer_port'])))



def show_upload_finished_message(self):
self.is_upload_active = False
self.ui_single_file_upload.connections_onetime.setEnabled(True)
Expand Down

0 comments on commit 93a4927

Please sign in to comment.