VisionScore/win_venv/main.py
2021-01-04 12:58:04 +01:00

174 lines
5.4 KiB
Python

#import tkinter as tk
import sys
import os
import shutil
from datetime import datetime
import time
from PyQt5 import QtCore
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtCore import *
scriptPath = os.path.dirname(os.path.realpath(__file__))
inputFilePath = scriptPath + '\\files\\input'
outputFilePath = scriptPath + '\\files\\output'
LIB_RAW = 0
LIB_VIS = 1
def files(path):
for file in os.listdir(path):
if os.path.isfile(os.path.join(path, file)):
yield file
class deletePopup(QMessageBox):
def __init__(self, parent=None):
super(deletePopup,self).__init__(parent)
self.setText("Are you sure you want to delete the file?")
self.setIcon(self.Warning)
self.setWindowTitle("Confirm deletion")
self.setStandardButtons(self.Yes | self.No)
def save_input(oldpath):
# make timestampt the filename, so it wouln't overwrite
timestamp = str(int(time.time()))
filename = timestamp + '.' + oldpath.split('.')[-1]
newpath = inputFilePath + '\\' + filename
shutil.copy(oldpath, newpath)
return newpath
class LibraryTableButtons(QWidget):
def __init__(self, file, parent=None):
super(LibraryTableButtons,self).__init__(parent)
def viewFile():
os.startfile(file)
def deleteFile():
self.exPopup = deletePopup()
# self.exPopup.setGeometry(100, 200, 100, 100)
ret = self.exPopup.exec()
if ret == self.exPopup.Yes:
os.remove(file)
layout = QHBoxLayout()
layout.setContentsMargins(0,0,0,0)
layout.setSpacing(0)
view_btn = QPushButton('View')
view_btn.clicked.connect(viewFile)
layout.addWidget(view_btn)
delete_btn = QPushButton('Delete')
delete_btn.clicked.connect(deleteFile)
layout.addWidget(delete_btn)
self.setLayout(layout)
class LibraryTable(QTableWidget):
def __init__(self, type, parent=None):
QTableWidget.__init__(self)
self.setColumnCount(3)
if type == LIB_RAW:
self.setHorizontalHeaderLabels(['Upload date', 'Filename', 'Options'])
filePath = inputFilePath
else:
self.setHorizontalHeaderLabels(['Creation date', 'Filename', 'Options'])
filePath = outputFilePath
self.horizontalHeader().setSectionResizeMode(0, QHeaderView.Stretch)
self.horizontalHeader().setSectionResizeMode(1, QHeaderView.Stretch)
self.horizontalHeader().setSectionResizeMode(2, QHeaderView.ResizeToContents)
dates = []
names = []
for index, file in enumerate(files(filePath)):
dates.append(QTableWidgetItem(str(datetime.fromtimestamp(os.path.getmtime(filePath + '/' + file)))))
names.append(QTableWidgetItem(str(file)))
self.setRowCount(len(dates))
for index, date in enumerate(dates):
self.setItem(index,0,date)
self.setItem(index,1,names[index])
self.setCellWidget(index,2,LibraryTableButtons(filePath + '\\' + names[index].text()))
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
# Show raw uploaded files
def showInputLibrary(self):
libTable = LibraryTable(LIB_RAW)
self.setCentralWidget(libTable)
# Show visualisations
def showVisualisationsLibrary(self):
libTable = LibraryTable(LIB_VIS)
self.setCentralWidget(libTable)
def showUploadFile(self):
dialog = QFileDialog(self)
dialog.setFileMode(QFileDialog.AnyFile)
dialog.setFilter(QDir.Files)
if dialog.exec_():
file_path = dialog.selectedFiles()[0] # ['C:/Users/agatha/Desktop/SYI/VisionScore/win_venv/requirements.txt']
save_input(file_path)
def initUI(self):
self.setGeometry(0, 0, 600, 400)
self.setWindowTitle('VisionScore')
scriptDir = os.path.dirname(os.path.realpath(__file__))
self.setWindowIcon(QIcon(scriptDir + os.path.sep + 'static/v_logo.jpg'))
# Toolbar
menuBar = self.menuBar()
homeMenu = QMenu("&Home", self)
menuBar.addMenu(homeMenu)
# Upload file
uploadAct = QAction('&Upload new file', self)
uploadAct.triggered.connect(self.showUploadFile)
homeMenu.addAction(uploadAct)
#Library menu
libraryMenu = QMenu("&Library", self)
menuBar.addMenu(libraryMenu)
#Input files
inputAct = QAction('&Input files', self)
inputAct.triggered.connect(self.showInputLibrary)
libraryMenu.addAction(inputAct)
#Visualisations
visAct = QAction('&Visualisations', self)
visAct.triggered.connect(self.showVisualisationsLibrary)
libraryMenu.addAction(visAct)
# Exit app
exitAct = QAction('&Exit', self)
exitAct.setShortcut('Ctrl+Q')
exitAct.setStatusTip('Exit')
exitAct.triggered.connect(qApp.quit)
homeMenu.addAction(exitAct)
helpMenu = menuBar.addMenu("&Help")
self.show()
def main():
app = QApplication(sys.argv)
w = MainWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()