136 lines
4.8 KiB
Python
136 lines
4.8 KiB
Python
import sys
|
||
from PyQt5 import uic
|
||
from PyQt5.QtWidgets import QApplication, QMainWindow, QDesktopWidget, QMessageBox, QFileDialog
|
||
from PyQt5.QtCore import Qt
|
||
from PyQt5.QtGui import QIcon
|
||
from config import *
|
||
from base64 import b64encode
|
||
from os.path import exists
|
||
|
||
|
||
class MainForm(QMainWindow):
|
||
def __init__(self, cfg):
|
||
super().__init__()
|
||
uic.loadUi('ui/MainForm.ui', self)
|
||
self.setWindowIcon(QIcon('ui/sign.ico'))
|
||
|
||
self.cfg = cfg
|
||
self.fname_txt.setText(cfg['General']['firstname'])
|
||
self.lname_txt.setText(cfg['General']['lastname'])
|
||
self.jobtitle_txt.setText(cfg['General']['jobtitle'])
|
||
self.mgts_txt.setText(cfg['General']['gtsphone'])
|
||
self.mgtsadd_txt.setText(cfg['General']['gtsphoneadd'])
|
||
self.mobile_txt.setText(cfg['General']['mobilephone'])
|
||
self.template_txt.setText(cfg['General']['template'])
|
||
self.logo_txt.setText(cfg['General']['logo'])
|
||
|
||
self.selecttemplate_btn.clicked.connect(self.selecttemplate_btn_clicked)
|
||
self.selectlogo_btn.clicked.connect(self.selectlogo_btn_clicked)
|
||
self.generate_btn.clicked.connect(self.generate_btn_clicked)
|
||
self.copy_btn.clicked.connect(self.copy_btn_clicked)
|
||
|
||
|
||
def selecttemplate_btn_clicked(self):
|
||
tmpl = QFileDialog.getOpenFileName(self, 'Выбрать файл шаблона', '', filter='Шаблон HTML (*.html)')[0]
|
||
if tmpl:
|
||
self.template_txt.setText(tmpl)
|
||
self.template_txt.setFocus()
|
||
|
||
|
||
def selectlogo_btn_clicked(self):
|
||
logo = QFileDialog.getOpenFileName(self, 'Выбрать файл логотипа', '', filter='Логотип PNG (*.png)')[0]
|
||
if logo:
|
||
self.logo_txt.setText(logo)
|
||
self.logo_txt.setFocus()
|
||
|
||
|
||
def generate_btn_clicked(self):
|
||
fname = self.fname_txt.text()
|
||
lname = self.lname_txt.text()
|
||
jobtitle = self.jobtitle_txt.text()
|
||
if self.mgts_txt.text() == '+7 () --':
|
||
mgts = ''
|
||
else:
|
||
mgts = self.mgts_txt.text()
|
||
mgtsadd = self.mgtsadd_txt.text()
|
||
if self.mobile_txt.text() == '+7 () --':
|
||
mobile = ''
|
||
else:
|
||
mobile = self.mobile_txt.text()
|
||
template = self.template_txt.text()
|
||
logo = self.logo_txt.text()
|
||
|
||
username = fname + ' ' + lname
|
||
|
||
if not exists(template):
|
||
alrt = QMessageBox(self)
|
||
alrt.setWindowIcon(self.windowIcon())
|
||
alrt.setWindowTitle('Ошибка')
|
||
alrt.setText('Не найден шаблон!')
|
||
alrt.setStandardButtons(QMessageBox.Ok)
|
||
alrt.setIcon(QMessageBox.Warning)
|
||
alrt.exec_()
|
||
return
|
||
|
||
with open(template, 'r', encoding='utf-8') as f:
|
||
text = f.read()
|
||
|
||
if not exists(logo):
|
||
alrt = QMessageBox(self)
|
||
alrt.setWindowIcon(self.windowIcon())
|
||
alrt.setWindowTitle('Ошибка')
|
||
alrt.setText('Не найден логотип!')
|
||
alrt.setStandardButtons(QMessageBox.Ok)
|
||
alrt.setIcon(QMessageBox.Warning)
|
||
alrt.exec_()
|
||
return
|
||
|
||
with open(logo, 'rb') as f:
|
||
logo_b64 = 'data:image/png;base64, ' + str(b64encode(f.read()))[2:-1]
|
||
|
||
workphone = mgts
|
||
if mgtsadd:
|
||
workphone += ' доб. ' + mgtsadd
|
||
|
||
text = text.format(username=username, logo=logo_b64, jobtitle=jobtitle, mgts=workphone, mobile=mobile)
|
||
self.view_txt.setHtml(text)
|
||
self.html_txt.setPlainText(text)
|
||
|
||
cfg['General']['firstname'] = fname
|
||
cfg['General']['lastname'] = lname
|
||
cfg['General']['jobtitle'] = jobtitle
|
||
cfg['General']['gtsphone'] = mgts
|
||
cfg['General']['gtsphoneadd'] = mgtsadd
|
||
cfg['General']['mobilephone'] = mobile
|
||
cfg['General']['template'] = template
|
||
cfg['General']['logo'] = logo
|
||
|
||
save_config(cfg)
|
||
|
||
def copy_btn_clicked(self):
|
||
self.html_txt.selectAll()
|
||
self.html_txt.copy()
|
||
alrt = QMessageBox(self)
|
||
alrt.setWindowIcon(self.windowIcon())
|
||
alrt.setWindowTitle('Сообщение')
|
||
alrt.setText('Текст скопирован!')
|
||
alrt.setStandardButtons(QMessageBox.Ok)
|
||
alrt.setIcon(QMessageBox.Information)
|
||
alrt.exec_()
|
||
|
||
|
||
def center(self):
|
||
qr = self.frameGeometry()
|
||
cp = QDesktopWidget().availableGeometry().center()
|
||
qr.moveCenter(cp)
|
||
self.move(qr.topLeft())
|
||
|
||
|
||
if __name__ == '__main__':
|
||
app = QApplication(sys.argv)
|
||
ex = MainForm(cfg)
|
||
ex.setWindowFlags(Qt.WindowCloseButtonHint | Qt.WindowMinimizeButtonHint)
|
||
ex.setFixedSize(680, 760)
|
||
ex.center()
|
||
ex.show()
|
||
sys.exit(app.exec_()) |