-
Notifications
You must be signed in to change notification settings - Fork 2
/
drag_and_drop.py
161 lines (129 loc) · 4.99 KB
/
drag_and_drop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from PyQt5.QtWidgets import (QPushButton, QWidget, QApplication, QProgressBar,
QTableWidget, QTableWidgetItem, QHBoxLayout,
QHeaderView, QLabel, QAbstractItemView)
from PyQt5.QtCore import Qt, QMimeData
from PyQt5.QtGui import QDrag
import sys
from converter import available_formats, process
import os
import ntpath
import subprocess, os
def open_with_application(path):
if sys.platform.startswith('darwin'):
subprocess.call(('open', path))
elif os.name == 'nt': # For Windows
os.startfile(path)
elif os.name == 'posix': # For Linux, Mac, etc.
subprocess.call(('xdg-open', path))
class TableWidgetItem(QLabel):
def __init__(self, parent, *args):
resp = super().__init__(*args)
self.parent = parent
self.rowIndex = None
return resp
def mouseDoubleClickEvent(self, e):
if self.rowIndex != None and self.parent.files[self.
rowIndex]['new_path']:
open_with_application(self.parent.files[self.rowIndex]['new_path'])
def setRowIndex(self, rowIndex):
self.rowIndex = rowIndex
class ProgressBar(QProgressBar):
def __init__(self, parent, *args):
resp = super().__init__(parent, *args)
self.parent = parent
self.rowIndex = None
return resp
def mouseDoubleClickEvent(self, e):
if self.rowIndex != None and self.parent.files[self.
rowIndex]['new_path']:
open_with_application(self.parent.files[self.rowIndex]['new_path'])
def setRowIndex(self, rowIndex):
self.rowIndex = rowIndex
class Table(QTableWidget):
def __init__(self, *args):
resp = super().__init__(*args)
self.setDragEnabled(True)
self.setAcceptDrops(True)
self.viewport().setAcceptDrops(True)
self.setDragDropOverwriteMode(False)
self.setDropIndicatorShown(True)
self.files = {}
return resp
def resizeEvent(self, e):
super().resizeEvent(e)
self.setColumnWidth(0, self.width() * 0.5)
# self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
# self.setColumnWidth(1, self.width() * 0.5 - 1)
self.horizontalHeader().setSectionResizeMode(1, QHeaderView.Stretch)
self.verticalHeader().setFixedWidth(0)
def dragEnterEvent(self, e):
valid = True
for url in e.mimeData().urls():
ext = os.path.splitext(url.fileName())[1]
if not ext or ext[1:] not in available_formats or url.path(
) in self.files:
valid = False
break
if valid:
e.accept()
else:
e.ignore()
def dropEvent(self, e):
e.accept()
urls = e.mimeData().urls()
for url in urls:
ext = os.path.splitext(url.fileName())[1]
path = url.path()
if os.name == 'nt' and path[0] == '/':
path = path[1:]
self.addFile(path)
if not ext or ext[1:] not in available_formats:
pass
def dragMoveEvent(self, e):
if (e.source() == self): e.ignore()
else: e.accept()
def dropMimeData(self, e):
e.accept()
def addFile(self, path):
filename = ntpath.basename(path)
rowsCount = self.rowCount()
self.setRowCount(rowsCount + 1)
self.files[rowsCount] = {'path': path, 'new_path': None}
self.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.setSelectionBehavior(QAbstractItemView.SelectRows)
table_item = TableWidgetItem(self, filename)
self.setCellWidget(rowsCount, 0, table_item)
pbar = ProgressBar(self)
pbar.setRowIndex(rowsCount)
table_item.setRowIndex(rowsCount)
self.setCellWidget(rowsCount, 1, pbar)
pbar.setValue(0)
process(path, handler=pbar, files_status_handler=self.files[rowsCount])
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# self.button = QPushButton('Ընտրել', self)
# self.button.move(100, 65)
# self.pbar = QProgressBar(self)
# self.pbar.setGeometry(5, -15, 290, 100)
# layout = QHBoxLayout(self)
self.table = Table(self)
self.table.setGeometry(0, 0, 300, 150)
self.table.setRowCount(0)
self.table.setColumnCount(2)
self.table.setHorizontalHeaderLabels(['Filename', 'Progress'])
# self.table.setCellWidget(0, 0, QPushButton('This is button'))
# layout.addWidget(self.table)
# self.setLayout(layout)
self.setWindowTitle('Armenian ANSI to Unicode')
self.setGeometry(400, 300, 450, 400)
def resizeEvent(self, e):
super().resizeEvent(e)
self.table.resize(self.width(), self.height())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
ex.show()
app.exec_()