beeref/beeref/gui.py
2021-07-06 18:10:12 +02:00

138 lines
4.9 KiB
Python

# This file is part of BeeRef.
#
# BeeRef 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.
#
# BeeRef 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 BeeRef. If not, see <https://www.gnu.org/licenses/>.
import logging
import os.path
from PyQt6 import QtCore, QtWidgets
from PyQt6.QtCore import Qt
from beeref import constants
from beeref.config import logfile_name
logger = logging.getLogger(__name__)
class WelcomeOverlay(QtWidgets.QWidget):
"""Some basic info to be displayed when the scene is empty."""
txt = """<p>Paste or drop images here.</p>
<p>Right-click for more options.</p>"""
def __init__(self, parent=None):
super().__init__(parent)
self.setAttribute(Qt.WidgetAttribute.WA_NoSystemBackground)
self.setAttribute(Qt.WidgetAttribute.WA_TransparentForMouseEvents)
label = QtWidgets.QLabel(self)
label.setStyleSheet("QLabel { color: #cccccc; }")
label.setText(self.txt)
label.setAlignment(Qt.AlignmentFlag.AlignVCenter
| Qt.AlignmentFlag.AlignCenter)
layout = QtWidgets.QVBoxLayout()
layout.addWidget(label)
self.setLayout(layout)
class BeeProgressDialog(QtWidgets.QProgressDialog):
def __init__(self, label, worker, maximum=0, parent=None):
super().__init__(label, 'Cancel', 0, maximum, parent=parent)
logger.debug('Initialised progress bar')
self.setMinimumDuration(0)
self.setWindowModality(Qt.WindowModality.WindowModal)
self.setAutoReset(False)
self.setAutoClose(False)
worker.begin_processing.connect(self.on_begin_processing)
worker.progress.connect(self.on_progress)
worker.finished.connect(self.on_finished)
self.canceled.connect(worker.on_canceled)
def on_progress(self, value):
logger.debug(f'Progress dialog: {value}')
self.setValue(value)
def on_begin_processing(self, value):
logger.debug(f'Beginn progress dialog: {value}')
self.setMaximum(value)
def on_finished(self, filename, errors):
logger.debug('Finished progress dialog')
self.setValue(self.maximum())
self.reset()
self.hide()
QtCore.QTimer.singleShot(100, self.deleteLater)
class HelpDialog(QtWidgets.QDialog):
def __init__(self, parent):
super().__init__(parent)
self.setWindowTitle(f'{constants.APPNAME} Help')
docdir = os.path.join(os.path.dirname(__file__),
'documentation')
tabs = QtWidgets.QTabWidget()
# Controls
with open(os.path.join(docdir, 'controls.html')) as f:
controls_txt = f.read()
controls = QtWidgets.QLabel(controls_txt)
controls.setTextInteractionFlags(
Qt.TextInteractionFlag.TextSelectableByMouse)
scroll = QtWidgets.QScrollArea(self)
scroll.setWidgetResizable(True)
scroll.setWidget(controls)
tabs.addTab(scroll, '&Controls')
layout = QtWidgets.QVBoxLayout()
self.setLayout(layout)
layout.addWidget(tabs)
self.show()
class DebugLogDialog(QtWidgets.QDialog):
def __init__(self, parent):
super().__init__(parent)
self.setWindowTitle(f'{constants.APPNAME} Debug Log')
with open(logfile_name()) as f:
self.log_txt = f.read()
self.log = QtWidgets.QLabel(self.log_txt)
self.log.setTextInteractionFlags(
Qt.TextInteractionFlag.TextSelectableByMouse)
scroll = QtWidgets.QScrollArea(self)
scroll.setWidgetResizable(True)
scroll.setWidget(self.log)
buttons = QtWidgets.QDialogButtonBox(
QtWidgets.QDialogButtonBox.StandardButton.Close)
buttons.rejected.connect(self.reject)
self.copy_button = QtWidgets.QPushButton('Co&py To Clipboard')
self.copy_button.released.connect(self.copy_to_clipboard)
buttons.addButton(
self.copy_button, QtWidgets.QDialogButtonBox.ButtonRole.ActionRole)
layout = QtWidgets.QVBoxLayout()
self.setLayout(layout)
name_widget = QtWidgets.QLabel(logfile_name())
name_widget.setTextInteractionFlags(
Qt.TextInteractionFlag.TextSelectableByMouse)
layout.addWidget(name_widget)
layout.addWidget(scroll)
layout.addWidget(buttons)
self.show()
def copy_to_clipboard(self):
clipboard = QtWidgets.QApplication.clipboard()
clipboard.setText(self.log_txt)