2023-10-03 14:47:35 +03:00
|
|
|
#
|
2024-01-08 16:09:12 +03:00
|
|
|
# Copyright (c) 2024 Maxim Slipenko, Eugene Lazurenko.
|
2023-10-03 14:47:35 +03:00
|
|
|
#
|
|
|
|
# This file is part of Statapp
|
|
|
|
# (see https://github.com/shizand/statapp).
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
2023-09-26 21:17:23 +03:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2023-10-04 14:38:42 +03:00
|
|
|
from PySide2.QtCore import QSize
|
|
|
|
from PySide2.QtGui import QIcon
|
2023-11-25 14:21:05 +03:00
|
|
|
from PySide2.QtWidgets import QMessageBox, QDoubleSpinBox, QStyledItemDelegate
|
|
|
|
|
|
|
|
from statapp.constants import NUMBERS_PRECISION
|
2023-10-01 21:53:06 +03:00
|
|
|
|
2023-09-26 21:17:23 +03:00
|
|
|
|
2023-10-04 14:38:42 +03:00
|
|
|
def resourcePath(relative):
|
2023-09-26 21:17:23 +03:00
|
|
|
if getattr(sys, 'frozen', False):
|
2023-10-04 14:38:42 +03:00
|
|
|
# pylint: disable=protected-access
|
|
|
|
bundleDir = sys._MEIPASS
|
2023-09-26 21:17:23 +03:00
|
|
|
else:
|
|
|
|
# we are running in a normal Python environment
|
2023-10-04 14:38:42 +03:00
|
|
|
bundleDir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
return os.path.join(bundleDir, relative)
|
2023-10-01 21:53:06 +03:00
|
|
|
|
2023-10-05 21:28:11 +03:00
|
|
|
|
2023-10-04 14:38:42 +03:00
|
|
|
def addIcon(windowOrDialog):
|
|
|
|
icon = QIcon()
|
|
|
|
icon.addFile(resourcePath("ui/images/logo.ico"), QSize(), QIcon.Normal, QIcon.Off)
|
|
|
|
windowOrDialog.setWindowIcon(icon)
|
2023-10-01 21:53:06 +03:00
|
|
|
|
2023-10-05 21:28:11 +03:00
|
|
|
|
2023-10-04 14:38:42 +03:00
|
|
|
def safeListGet(lst, idx, default):
|
2023-10-01 21:53:06 +03:00
|
|
|
try:
|
2023-10-04 14:38:42 +03:00
|
|
|
return lst[idx]
|
2023-10-01 21:53:06 +03:00
|
|
|
except IndexError:
|
|
|
|
return default
|
|
|
|
|
|
|
|
|
|
|
|
def buildMessageBox(title, text, icon, buttons, defaultButton):
|
|
|
|
msgBox = QMessageBox()
|
|
|
|
|
|
|
|
msgBox.setIcon(icon)
|
|
|
|
msgBox.setWindowTitle(title)
|
|
|
|
msgBox.setText(text)
|
|
|
|
msgBox.setStandardButtons(buttons)
|
|
|
|
msgBox.setDefaultButton(defaultButton)
|
|
|
|
|
2024-01-29 11:11:53 +03:00
|
|
|
addIcon(msgBox)
|
|
|
|
|
2023-10-01 21:53:06 +03:00
|
|
|
return msgBox
|
2023-11-25 14:21:05 +03:00
|
|
|
|
2024-01-29 11:11:53 +03:00
|
|
|
def onError(errorName: Exception):
|
|
|
|
msgBox = buildMessageBox \
|
|
|
|
('Ошибка',
|
|
|
|
"Упс.. Произошла ошибка:\n" + str(errorName),
|
|
|
|
QMessageBox.Critical,
|
|
|
|
QMessageBox.Ok,
|
|
|
|
QMessageBox.Ok)
|
|
|
|
|
|
|
|
msgBox.exec_()
|
2023-11-25 14:21:05 +03:00
|
|
|
|
|
|
|
class FloatDelegate(QStyledItemDelegate):
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
QStyledItemDelegate.__init__(self, parent=parent)
|
|
|
|
|
|
|
|
def createEditor(self, parent, option, index):
|
|
|
|
editor = QDoubleSpinBox(parent)
|
|
|
|
editor.setDecimals(NUMBERS_PRECISION)
|
|
|
|
editor.setMaximum(10**8)
|
|
|
|
editor.setMinimum(-10**8)
|
|
|
|
return editor
|
|
|
|
|
|
|
|
def displayText(self, value, locale):
|
|
|
|
# Грязный хак, скорее всего нужно было использовать locale
|
|
|
|
return f'{value:.{NUMBERS_PRECISION}f}'
|