Moved the additions folder into the gui folder as it only contains gui elements
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Qt Additions
|
||||
from nw.gui.additions.qconfiglayout import QConfigLayout
|
||||
from nw.gui.additions.qswitch import QSwitch
|
||||
|
||||
# Main Window Elements
|
||||
from nw.gui.icons import GuiIcons
|
||||
from nw.gui.mainmenu import GuiMainMenu
|
||||
@@ -33,6 +37,8 @@ from nw.gui.tools.optionstate import OptionState
|
||||
from nw.gui.tools.wordcounter import WordCounter
|
||||
|
||||
__all__ = [
|
||||
"QConfigLayout",
|
||||
"QSwitch",
|
||||
"GuiIcons",
|
||||
"GuiMainMenu",
|
||||
"GuiMainStatus",
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from nw.gui.additions.qconfiglayout import QConfigLayout
|
||||
from nw.gui.additions.qswitch import QSwitch
|
||||
|
||||
__all__ = [
|
||||
"QConfigLayout",
|
||||
"QSwitch",
|
||||
]
|
||||
@@ -0,0 +1,178 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""novelWriter Addition QConfigLayout
|
||||
|
||||
novelWriter – Addition QConfigLayout
|
||||
======================================
|
||||
A custom Qt grid layout for config forms similar to QFormLayout
|
||||
|
||||
File History:
|
||||
Created: 2020-05-03 [0.4.5]
|
||||
|
||||
This file is a part of novelWriter
|
||||
Copyright 2020, Veronica Berglyd Olsen
|
||||
|
||||
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 <https://www.gnu.org/licenses/>.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import nw
|
||||
|
||||
from PyQt5.QtCore import Qt
|
||||
from PyQt5.QtGui import QColor, QPalette
|
||||
from PyQt5.QtWidgets import (
|
||||
QGridLayout, QLabel, QWidget, QVBoxLayout, QHBoxLayout
|
||||
)
|
||||
|
||||
from nw.constants import nwUnicode
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class QConfigLayout(QGridLayout):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
self._nextRow = 0
|
||||
self._helpCol = QColor(0, 0, 0)
|
||||
self._fontScale = 0.9
|
||||
|
||||
self._itemMap = {}
|
||||
|
||||
self.setHorizontalSpacing(8)
|
||||
self.setVerticalSpacing(8)
|
||||
|
||||
return
|
||||
|
||||
##
|
||||
# Getters and Setters
|
||||
##
|
||||
|
||||
def setHelpTextStyle(self, helpCol, fontScale=0.9):
|
||||
"""Set the text color for the help text.
|
||||
"""
|
||||
if isinstance(helpCol, QColor):
|
||||
self._helpCol = helpCol
|
||||
else:
|
||||
self._helpCol = QColor(*helpCol)
|
||||
self._fontScale = fontScale
|
||||
return
|
||||
|
||||
def setHelpText(self, intRow, theText):
|
||||
if intRow in self._itemMap:
|
||||
self._itemMap[intRow]["help"].setText(theText)
|
||||
return
|
||||
|
||||
def setLabelText(self, intRow, theText):
|
||||
if intRow in self._itemMap:
|
||||
self._itemMap[intRow]["label"].setText(theText)
|
||||
return
|
||||
|
||||
##
|
||||
# Class Methods
|
||||
##
|
||||
|
||||
def addGroupLabel(self, theLabel):
|
||||
"""Adds a text label to separate groups of settings.
|
||||
"""
|
||||
|
||||
if isinstance(theLabel, QLabel):
|
||||
qLabel = theLabel
|
||||
elif isinstance(theLabel, str):
|
||||
qLabel = QLabel("<b>%s</b>" % theLabel)
|
||||
else:
|
||||
qLabel = None
|
||||
raise ValueError("theLabel must be a QLabel")
|
||||
|
||||
qLabel.setContentsMargins(0,4,0,4)
|
||||
self.addWidget(qLabel, self._nextRow, 0, 1, 2, Qt.AlignLeft)
|
||||
|
||||
self.setRowStretch(self._nextRow, 0)
|
||||
self.setRowStretch(self._nextRow+1, 1)
|
||||
|
||||
self._nextRow += 1
|
||||
|
||||
return
|
||||
|
||||
def addRow(self, theLabel, theWidget, helpText=None, theUnit=None):
|
||||
"""Add a label and a widget as a new row of the grid.
|
||||
"""
|
||||
|
||||
thisEntry = {
|
||||
"label" : None,
|
||||
"help" : None,
|
||||
"widget" : None,
|
||||
}
|
||||
|
||||
if isinstance(theLabel, QLabel):
|
||||
qLabel = theLabel
|
||||
elif isinstance(theLabel, str):
|
||||
qLabel = QLabel(theLabel)
|
||||
else:
|
||||
qLabel = None
|
||||
raise ValueError("theLabel must be a QLabel")
|
||||
|
||||
if isinstance(theWidget, QWidget):
|
||||
qWidget = theWidget
|
||||
else:
|
||||
qWidget = None
|
||||
raise ValueError("theWidget must be a QWidget")
|
||||
|
||||
qLabel.setIndent(8)
|
||||
if helpText is not None:
|
||||
qHelp = QLabel(str(helpText))
|
||||
qHelp.setIndent(8)
|
||||
|
||||
lblCol = qHelp.palette()
|
||||
lblCol.setColor(QPalette.WindowText, self._helpCol)
|
||||
qHelp.setPalette(lblCol)
|
||||
|
||||
lblFont = qHelp.font()
|
||||
lblFont.setPointSizeF(self._fontScale*lblFont.pointSizeF())
|
||||
qHelp.setFont(lblFont)
|
||||
|
||||
labelBox = QVBoxLayout()
|
||||
labelBox.addWidget(qLabel)
|
||||
labelBox.addWidget(qHelp)
|
||||
labelBox.setSpacing(0)
|
||||
|
||||
thisEntry["help"] = qHelp
|
||||
self.addLayout(labelBox, self._nextRow, 0, Qt.AlignLeft)
|
||||
|
||||
else:
|
||||
self.addWidget(qLabel, self._nextRow, 0, Qt.AlignLeft)
|
||||
|
||||
if theUnit is not None:
|
||||
controlBox = QHBoxLayout()
|
||||
controlBox.addWidget(qWidget, 0, Qt.AlignVCenter)
|
||||
controlBox.addWidget(QLabel(theUnit), 0, Qt.AlignVCenter)
|
||||
controlBox.setSpacing(8)
|
||||
self.addLayout(controlBox, self._nextRow, 1, Qt.AlignRight)
|
||||
else:
|
||||
self.addWidget(qWidget, self._nextRow, 1, Qt.AlignRight)
|
||||
|
||||
qLabel.setBuddy(qWidget)
|
||||
|
||||
self.setRowStretch(self._nextRow, 0)
|
||||
self.setRowStretch(self._nextRow+1, 1)
|
||||
|
||||
thisEntry["label"] = qLabel
|
||||
thisEntry["widget"] = qWidget
|
||||
|
||||
self._itemMap[self._nextRow] = thisEntry
|
||||
|
||||
self._nextRow += 1
|
||||
|
||||
return self._nextRow - 1
|
||||
|
||||
# END Class QConfigLayout
|
||||
@@ -0,0 +1,165 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""novelWriter Addition QSwitch
|
||||
|
||||
novelWriter – Addition QSwitch
|
||||
================================
|
||||
A custom Qt switch button
|
||||
|
||||
File History:
|
||||
Created: 2020-05-03 [0.4.5]
|
||||
|
||||
The core code of this class is based on Stack Overflow example by
|
||||
Stefan Scherfke: https://stackoverflow.com/a/51825815/5825851
|
||||
|
||||
This file is a part of novelWriter
|
||||
Copyright 2020, Veronica Berglyd Olsen
|
||||
|
||||
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 <https://www.gnu.org/licenses/>.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import nw
|
||||
|
||||
from PyQt5.QtCore import Qt, QSize, QRectF, QPropertyAnimation, pyqtProperty
|
||||
from PyQt5.QtWidgets import QAbstractButton, QSizePolicy
|
||||
from PyQt5.QtGui import QPainter
|
||||
|
||||
from nw.constants import nwUnicode
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class QSwitch(QAbstractButton):
|
||||
|
||||
def __init__(self, parent=None):
|
||||
super().__init__(parent=parent)
|
||||
|
||||
self.setCheckable(True)
|
||||
self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
|
||||
self.setFixedWidth(40)
|
||||
self.setFixedHeight(20)
|
||||
self._offset = 10
|
||||
|
||||
return
|
||||
|
||||
##
|
||||
# Properties
|
||||
##
|
||||
|
||||
@pyqtProperty(int)
|
||||
def offset(self):
|
||||
return self._offset
|
||||
|
||||
@offset.setter
|
||||
def offset(self, theOffset):
|
||||
self._offset = theOffset
|
||||
self.update()
|
||||
return
|
||||
|
||||
##
|
||||
# Getters and Setters
|
||||
##
|
||||
|
||||
def setChecked(self, isChecked):
|
||||
"""Overload setChecked to also alter the offset.
|
||||
"""
|
||||
super().setChecked(isChecked)
|
||||
if isChecked:
|
||||
self.offset = 30
|
||||
else:
|
||||
self.offset = 10
|
||||
return
|
||||
|
||||
##
|
||||
# Events
|
||||
##
|
||||
|
||||
def resizeEvent(self, theEvent):
|
||||
"""Overload resize to ensure correct offset.
|
||||
"""
|
||||
super().resizeEvent(theEvent)
|
||||
if self.isChecked():
|
||||
self.offset = 30
|
||||
else:
|
||||
self.offset = 10
|
||||
return
|
||||
|
||||
def paintEvent(self, event):
|
||||
"""Drawing the switch itself.
|
||||
"""
|
||||
|
||||
qPaint = QPainter(self)
|
||||
qPaint.setRenderHint(QPainter.Antialiasing, True)
|
||||
qPaint.setPen(Qt.NoPen)
|
||||
|
||||
qPalette = self.palette()
|
||||
if self.isChecked():
|
||||
trackBrush = qPalette.highlight()
|
||||
thumbBrush = qPalette.highlightedText()
|
||||
textColor = qPalette.highlight().color()
|
||||
thumbText = nwUnicode.U_CHECK
|
||||
else:
|
||||
trackBrush = qPalette.dark()
|
||||
thumbBrush = qPalette.light()
|
||||
textColor = qPalette.dark().color()
|
||||
thumbText = nwUnicode.U_MULT
|
||||
|
||||
if self.isEnabled():
|
||||
trackOpacity = 1.0
|
||||
else:
|
||||
trackOpacity = 0.6
|
||||
trackBrush = qPalette.shadow()
|
||||
thumbBrush = qPalette.mid()
|
||||
textColor = qPalette.shadow().color()
|
||||
|
||||
qPaint.setBrush(trackBrush)
|
||||
qPaint.setOpacity(trackOpacity)
|
||||
qPaint.drawRoundedRect(0, 0, 40, 20, 10, 10)
|
||||
|
||||
qPaint.setBrush(thumbBrush)
|
||||
qPaint.drawEllipse(self.offset - 8, 2, 16, 16)
|
||||
|
||||
theFont = qPaint.font()
|
||||
theFont.setPixelSize(12)
|
||||
qPaint.setPen(textColor)
|
||||
qPaint.setFont(theFont)
|
||||
qPaint.drawText(
|
||||
QRectF(self.offset - 8, 2, 16, 16),
|
||||
Qt.AlignCenter, thumbText
|
||||
)
|
||||
|
||||
return
|
||||
|
||||
def mouseReleaseEvent(self, event):
|
||||
"""Animate the switch on mouse release.
|
||||
"""
|
||||
super().mouseReleaseEvent(event)
|
||||
if event.button() == Qt.LeftButton:
|
||||
doAnim = QPropertyAnimation(self, b'offset', self)
|
||||
doAnim.setDuration(120)
|
||||
doAnim.setStartValue(self.offset)
|
||||
if self.isChecked():
|
||||
doAnim.setEndValue(30)
|
||||
else:
|
||||
doAnim.setEndValue(10)
|
||||
doAnim.start()
|
||||
return
|
||||
|
||||
def enterEvent(self, event):
|
||||
"""Change the cursor when hovering the button.
|
||||
"""
|
||||
self.setCursor(Qt.PointingHandCursor)
|
||||
super().enterEvent(event)
|
||||
return
|
||||
|
||||
# END Class QSwitch
|
||||
@@ -38,7 +38,7 @@ from PyQt5.QtWidgets import (
|
||||
QFileDialog
|
||||
)
|
||||
|
||||
from nw.additions import QSwitch, QConfigLayout
|
||||
from nw.gui.additions import QSwitch, QConfigLayout
|
||||
from nw.core import NWSpellCheck, NWSpellSimple, NWSpellEnchant
|
||||
from nw.constants import nwAlert, nwQuotes
|
||||
|
||||
|
||||
Reference in New Issue
Block a user