-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
146 lines (117 loc) · 4.6 KB
/
main.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
#!/usr/bin/env python3
# -*- coding=utf-8 -*-
import logging
import os
import sys
from PyQt5 import QtGui
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QMessageBox
from Ui_MainWindow import Ui_MainWindow
from bmp2hex import bmp2hex
from image2bmp import image_to_bmp
from text2image import ImageGenerate
logging.basicConfig(level=logging.DEBUG)
class MainWindow(QMainWindow):
RESOURCE_DIR = 'res'
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent=parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.create_folder()
self.message = ""
self.image_size = (48, 32)
self.font_size = None
self.font_family = None
self.imageGenerate = ImageGenerate()
self.register_listener()
self.set_up_ui()
def set_up_ui(self):
self.ui.menubar.setNativeMenuBar(False)
self.ui.tv_lcd.setWordWrapMode(QtGui.QTextOption.NoWrap)
self.ui.spinBox.setEnabled(False)
def register_listener(self):
self.ui.btn_transform.clicked.connect(self.text_to_image)
self.ui.btn_select_font.clicked.connect(self.select_font_file)
self.ui.action_select_image.triggered.connect(self.select_image_file)
self.ui.spinBox.valueChanged.connect(self.on_value_changed)
self.ui.btn_copy.clicked.connect(self.copy_to_clipboard)
self.ui.cb_font_size.stateChanged.connect(self.on_state_changed)
def copy_to_clipboard(self):
logging.debug('copy_to_clipboard')
self.ui.tv_lcd.selectAll()
self.ui.tv_lcd.copy()
def on_value_changed(self):
self.font_size = self.ui.spinBox.value()
def on_state_changed(self):
if self.ui.cb_font_size.isChecked():
self.ui.spinBox.setEnabled(True)
self.font_size = self.ui.spinBox.value()
else:
self.ui.spinBox.setEnabled(False)
self.font_size = None
def select_image_file(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
filename, _ = QFileDialog.getOpenFileName(
self, 'Select Image', '',
'Image files (*.jpg *.gif *.png *.bmp *.jpeg *.webp *.ico *.svg)',
options=options)
logging.debug(f'select_image_file {filename}')
if filename:
dist = image_to_bmp(filename, self.image_size)
self.ui.et_message.clear()
self.ui.et_font_path.clear()
self.ui.cb_font_size.setChecked(False)
self.display_dist_image(dist)
self.image_to_lcd(dist)
def select_font_file(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
filename, _ = QFileDialog.getOpenFileName(
self, 'Select Font', '', 'Font files (*.ttc *.ttf)', options=options)
logging.debug(f'select_font_file {filename}')
if filename:
self.ui.et_font_path.setText(filename)
self.ui.cb_font_size.setChecked(True)
self.font_family = filename
self.font_size = self.ui.spinBox.value()
def text_to_image(self):
self.message = self.ui.et_message.text()
logging.debug(
f'message {self.message} fontsize {self.font_size} fontFamily {self.font_family}')
# noinspection PyBroadException
try:
image_name = self.imageGenerate.generate(
self.message, self.font_family, self.font_size)
self.display_dist_image(image_name)
self.image_to_lcd(image_name)
except Exception as _:
QMessageBox(QMessageBox.NoIcon, 'Open File failed',
'Unable to open the file').exec()
pass
def image_to_lcd(self, image_name):
result = bmp2hex(image_name, 8, 0,
False, False, False, False, False)
self.ui.tv_lcd.setPlainText(result)
def display_dist_image(self, file):
pixmap = QPixmap(file)
self.ui.iv_image.setPixmap(pixmap)
def closeEvent(self, event):
self.clean_cache()
event.accept()
@staticmethod
def clean_cache():
for item in os.listdir(MainWindow.RESOURCE_DIR):
if item.endswith('bmp'):
os.remove(f'{MainWindow.RESOURCE_DIR}/{item}')
@staticmethod
def create_folder():
if not os.path.exists(MainWindow.RESOURCE_DIR):
os.mkdir(MainWindow.RESOURCE_DIR)
def main():
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()