Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
104 lines (91 loc) · 3.44 KB
/
Copy pathmain.py
File metadata and controls
104 lines (91 loc) · 3.44 KB
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
import os
import sys
import time
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import (
QApplication, QWidget, QInputDialog, QLineEdit, QFileDialog, QPushButton,
QMessageBox
)
from huffman_compress import compressor
from huffman_decompress import decompressor
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'Mhs Huffman Coding'
self.left = 10
self.top = 10
self.width = 640
self.height = 480
self.setStyleSheet("background-color: rgb(136, 138, 133);")
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
compress_button = QPushButton('Compress', self)
# compress_button.setToolTip('This is an example button')
compress_button.resize(180, 260)
compress_button.move(50, 110)
compress_button.clicked.connect(self.compressButtonAction)
decompress_button = QPushButton('Decompress', self)
# decompress_button.setToolTip('This is an example button')
decompress_button.resize(180, 260)
decompress_button.move(420, 110)
decompress_button.clicked.connect(self.decompressButtonAction)
self.show()
@pyqtSlot()
def compressButtonAction(self):
files = self.openFileNamesDialog()
savedFile = self.saveFileDialog()
# print(files, savedFile)
start = time.clock()
compressor(files, savedFile[0])
end = time.clock()
run_time = "Complete in {:.2f} Seconds".format(end - start)
changed_size = "Files compressed {:.2f}%".format(100 - (os.path.getsize(savedFile[0] + ".mhs") * 100 / sum(os.path.getsize(x) for x in files)))
dialog_shown_text = "File compressed succesfully!\n{}\n{}".format(run_time, changed_size)
buttonReply = QMessageBox.question(self, 'Compress Message', dialog_shown_text, QMessageBox.Ok, QMessageBox.Ok)
@pyqtSlot()
def decompressButtonAction(self):
file = self.openFileNameDialog()
savedPath = self.savePathDialog()
# print(file, savedPath)
# Set ability to select file to uncompress
start = time.clock()
decompressor(file, savedPath)
end = time.clock()
run_time = "Complete in {:.2f} Seconds".format(end - start)
dialog_shown_text = "File decompressed succesfully!\n{}".format(run_time)
buttonReply = QMessageBox.question(self, 'Decompress Message', dialog_shown_text, QMessageBox.Ok, QMessageBox.Ok)
@pyqtSlot()
def openFileNamesDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
files, _ = QFileDialog.getOpenFileNames(self,"Select files for compress", "","All Files (*);;Python Files (*.py)", options=options)
if files:
return files
@pyqtSlot()
def openFileNameDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
files, _ = QFileDialog.getOpenFileName(self, "Select files for decompress", "","Mhs Compressed File (*.mhs)", options=options)
if files:
return files
@pyqtSlot()
def savePathDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName = QFileDialog.getExistingDirectory(self, "Select path for save your file", options=options)
if fileName:
return fileName
@pyqtSlot()
def saveFileDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName = QFileDialog.getSaveFileName(self, "Select path for save your file", options=options)
if fileName:
return fileName
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())