-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain_Window.py
306 lines (235 loc) · 11.9 KB
/
Main_Window.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Main_Window.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets, QtSql
from PyQt5.QtWidgets import QMainWindow, QFileDialog, QDialog, QInputDialog, QMessageBox, QLineEdit
from PyQt5.QtGui import QIcon
import sqlite3
from CopyPasteExcel import copy_paste
from Macros import Ui_Macro_Dialog as Macro_Dialog
import os
def create_connection(database):
db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName(database)
if not db.open():
print("Cannot open database")
print(
"Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information "
"how to build it.\n\n"
"Click Cancel to exit."
)
return False
query = QtSql.QSqlQuery()
if not query.exec_(
"""CREATE TABLE IF NOT EXISTS Macros (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"title" TEXT NOT NULL,
"description" TEXT)"""
):
print(query.lastError().text())
return False
return True
class FileEdit(QLineEdit):
""" Custom Subclass of QLineEdit tp allow users to drag & drop for file selection (instead of browsing) """
def __init__(self, parent):
super(FileEdit, self).__init__(parent)
self.setDragEnabled(True)
def dragEnterEvent(self, event):
data = event.mimeData()
urls = data.urls()
if urls and urls[0].scheme() == 'file':
event.acceptProposedAction()
def dragMoveEvent(self, event):
data = event.mimeData()
urls = data.urls()
if urls and urls[0].scheme() == 'file':
event.acceptProposedAction()
def dropEvent(self, event):
data = event.mimeData()
urls = data.urls()
if urls and urls[0].scheme() == 'file':
filepath = str(urls[0].path())[1:]
# any file type here
self.setText(filepath)
# BELOW IS CODE FROM: https://www.reddit.com/r/learnpython/comments/97z5dq/pyqt5_drag_and_drop_file_option/e4cv39x/
# WHEN YOU HAVE TIME, REFACTOR ABOVE CODE WITH REGEX TO ONLY OPEN EXCEL FILES (anything ending with .xl...)
# if filepath[-4:].upper() in [".txt", ".x"]:
# self.setText(filepath)
# else:
# dialog = QMessageBox()
# dialog.setWindowTitle("Error: Invalid File")
# dialog.setText("Only Excel files are accepted")
# dialog.setIcon(QMessageBox.Warning)
# dialog.exec_()
class Ui_MainWindow(QMainWindow):
def __init__(self, parent=None):
super(Ui_MainWindow, self).__init__(parent)
self.initUI(MainWindow)
def initUI(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(550, 500)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label_select_excel_files = QtWidgets.QLabel(self.centralwidget)
self.label_select_excel_files.setGeometry(QtCore.QRect(210, 70, 131, 21))
font = QtGui.QFont()
font.setPointSize(14)
self.label_select_excel_files.setFont(font)
self.label_select_excel_files.setAlignment(QtCore.Qt.AlignCenter)
self.label_select_excel_files.setObjectName("label_select_excel_files")
self.Frame_fileimport = QtWidgets.QFrame(self.centralwidget)
self.Frame_fileimport.setGeometry(QtCore.QRect(20, 100, 511, 100))
self.Frame_fileimport.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.Frame_fileimport.setFrameShadow(QtWidgets.QFrame.Raised)
self.Frame_fileimport.setObjectName("frame_fileimport")
self.Label_copyfrom = QtWidgets.QLabel(self.Frame_fileimport)
self.Label_copyfrom.setGeometry(QtCore.QRect(10, 10, 79, 35))
self.Label_copyfrom.setObjectName("Label_copyfrom")
self.Label_destination = QtWidgets.QLabel(self.Frame_fileimport)
self.Label_destination.setGeometry(QtCore.QRect(10, 50, 71, 31))
self.Label_destination.setObjectName("Label_destination")
self.textEdit_copyfrom = FileEdit(self.Frame_fileimport)
self.textEdit_copyfrom.setGeometry(QtCore.QRect(90, 20, 319, 21))
self.textEdit_copyfrom.setObjectName("textEdit_copyfrom")
self.textEdit_destination = FileEdit(self.Frame_fileimport)
self.textEdit_destination.setGeometry(QtCore.QRect(90, 60, 319, 21))
self.textEdit_destination.setObjectName("textEdit_destination")
self.Button_browse_copyfrom = QtWidgets.QPushButton(self.Frame_fileimport)
self.Button_browse_copyfrom.setGeometry(QtCore.QRect(410, 10, 91, 41))
self.Button_browse_copyfrom.setObjectName("Button_browse_copyfrom")
self.Button_browse_copyfrom.clicked.connect(lambda: self.open_excel_file(self.textEdit_copyfrom)) # Added by me (browse func for copy file)
self.Button_browse_destination = QtWidgets.QPushButton(self.Frame_fileimport)
self.Button_browse_destination.setGeometry(QtCore.QRect(410, 50, 91, 41))
self.Button_browse_destination.setObjectName("Button_browse_destination")
self.Button_browse_destination.clicked.connect(lambda: self.open_excel_file(self.textEdit_destination)) # Added by me (browse func for destination file)
self.line = QtWidgets.QFrame(self.centralwidget)
self.line.setGeometry(QtCore.QRect(0, 220, 550, 5))
self.line.setFrameShape(QtWidgets.QFrame.HLine)
self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line.setObjectName("line")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(380, 250, 151, 151))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.Label_macros = QtWidgets.QLabel(self.centralwidget)
self.Label_macros.setGeometry(QtCore.QRect(135, 230, 120, 16))
self.Label_macros.setAlignment(QtCore.Qt.AlignCenter)
self.Label_macros.setObjectName("Label_macros")
self._model = QtSql.QSqlTableModel(MainWindow) # Added SQL Table model
self.model.setTable("Macros")
self.model.select()
self.listView_macros = QtWidgets.QListView(self.centralwidget)
self.listView_macros.setGeometry(QtCore.QRect(20, 250, 350, 150))
self.listView_macros.setObjectName("tableWidget_macros")
self.listView_macros.setModel(self.model)
self.listView_macros.setModelColumn(self.model.record().indexOf("title"))
self.Button_new_macro = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.Button_new_macro.setObjectName("Button_new_macro")
self.verticalLayout.addWidget(self.Button_new_macro)
self.Button_new_macro.clicked.connect(self.new_macro) # Create new entry in macro list
self.Button_edit_macro = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.Button_edit_macro.setObjectName("Button_edit_macro")
self.verticalLayout.addWidget(self.Button_edit_macro)
self.Button_edit_macro.clicked.connect(self.edit_macro) # Edit selected entry in macro list
self.Button_remove_macro = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.Button_remove_macro.setObjectName("Button_remove_macro")
self.verticalLayout.addWidget(self.Button_remove_macro)
self.Button_remove_macro.clicked.connect(self.remove_macro) # Remove selected entry in macro list
self.Button_Run = QtWidgets.QPushButton(self.centralwidget)
self.Button_Run.setGeometry(QtCore.QRect(120, 410, 311, 51))
self.Button_Run.setObjectName("Button_Run")
# self.Button_Run.clicked.connect(self.refresh)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 550, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.Button_new_macro.setText(_translate("MainWindow", "New"))
self.Button_edit_macro.setText(_translate("MainWindow", "Edit"))
self.Button_remove_macro.setText(_translate("MainWindow", "Remove"))
# self.listView_macros.setSortingEnabled(False)
self.Label_macros.setText(_translate("MainWindow", "Macros"))
self.Button_Run.setText(_translate("MainWindow", "RUN"))
self.Label_copyfrom.setText(_translate("MainWindow", "Copy From:"))
self.Label_destination.setText(_translate("MainWindow", "Destination:"))
self.Button_browse_destination.setText(_translate("MainWindow", "Browse"))
self.Button_browse_copyfrom.setText(_translate("MainWindow", "Browse"))
self.label_select_excel_files.setText(_translate("MainWindow", "Select Excel Files"))
@property
def model(self):
return self._model
@QtCore.pyqtSlot()
def new_macro(self):
d = Macro_Dialog()
if d.exec_() == QtWidgets.QDialog.Accepted:
r = self.model.record()
r.setValue("title", d.title)
r.setValue("description", d.description)
if self.model.insertRecord(self.model.rowCount(), r):
self.model.select()
@QtCore.pyqtSlot()
def edit_macro(self):
ixs = self.listView_macros.selectionModel().selectedIndexes()
if ixs:
d = Macro_Dialog(self.model, ixs[0].row())
d.exec_()
@QtCore.pyqtSlot()
def remove_macro(self):
ixs = self.listView_macros.selectionModel().selectedIndexes()
if ixs:
reply = QMessageBox.warning(self, "Remove Macro?",
"Remove Macro?",
QMessageBox.Yes | QMessageBox.No)
if reply == QMessageBox.Yes:
self.model.removeRow(ixs[0].row())
self.model.select()
def _add_table(self, columns):
pass
# row_pos = self.tableWidget_macros.count()
# last_row = self.tableWidget_macros.
# self.tableWidget_macros.insertItem()
#
# for i, col in enumerate(columns):
# self.tableWidget_macros.setitem
def open_excel_file(self, textEdit):
""" open file browser and get path to designated copy or destination file """
fname = QFileDialog.getOpenFileName(self, "Open file")
if fname[0]:
file = open(fname[0], 'r')
with file:
text = file.name # << Saves file PATH to textEdit next to it
textEdit.setText(text)
def run(self):
""" """
copy_wb_path = self.textEdit_copyfrom.text()
destination_wb_path = self.textEdit_destination
rule = [["B2", "B2"], ["C2:C4", "D2:D4"]] # TEST rule
pass
if __name__ == "__main__":
import sys
database_name = "Macros_db" # ":memory:"
app = QtWidgets.QApplication(sys.argv)
if not create_connection(database_name):
sys.exit(app.exec_())
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.initUI(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
# ----------- TEST ----------
# open_excel_file()