Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sidebar cleanup and minor fixes #1520

Merged
merged 5 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions novelwriter/gui/doceditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,9 +460,8 @@ def loadText(self, tHandle, tLine=None) -> bool:

return True

def updateTagHighLighting(self):
"""Rerun the syntax highlighter on all meta data lines.
"""
def updateTagHighLighting(self) -> None:
"""Rerun the syntax highlighter on all meta data lines."""
self.highLight.rehighlightByType(GuiDocHighlighter.BLOCK_META)
return

Expand Down Expand Up @@ -672,7 +671,7 @@ def saveCursorPosition(self) -> None:
self._nwItem.setCursorPos(cursPos)
return

def setCursorLine(self, line: int) -> bool:
def setCursorLine(self, line: int | None) -> bool:
"""Move the cursor to a given line in the document."""
if not isinstance(line, int):
return False
Expand Down
6 changes: 3 additions & 3 deletions novelwriter/gui/dochighlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def highlightBlock(self, text: str) -> None:
self.setFormat(0, 4, self._hStyles["header4h"])
self.setFormat(4, len(text), self._hStyles["header4"])

if text.startswith("#! "): # Title
elif text.startswith("#! "): # Title
self.setFormat(0, 2, self._hStyles["header1h"])
self.setFormat(2, len(text), self._hStyles["header1"])

Expand Down Expand Up @@ -367,7 +367,7 @@ def highlightBlock(self, text: str) -> None:
self.setFormat(tLen-1, tLen, self._hStyles["keyword"])
return

# Regular text
# Regular Text
self.setCurrentBlockState(self.BLOCK_TEXT)
for rX, xFmt in self.rxRules:
rxItt = rX.globalMatch(text, 0)
Expand All @@ -389,7 +389,7 @@ def highlightBlock(self, text: str) -> None:
while rxSpell.hasNext():
rxMatch = rxSpell.next()
if not SHARED.spelling.checkWord(rxMatch.captured(0)):
if rxMatch.captured(0).isupper() or rxMatch.captured(0).isnumeric():
if not rxMatch.captured(0).isalpha() or rxMatch.captured(0).isupper():
continue
xPos = rxMatch.capturedStart(0)
xLen = rxMatch.capturedLength(0)
Expand Down
180 changes: 106 additions & 74 deletions novelwriter/gui/sidebar.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,119 +25,151 @@

import logging

from PyQt5.QtCore import Qt, QSize, pyqtSignal
from PyQt5.QtWidgets import (
QToolBar, QWidget, QSizePolicy, QAction, QMenu, QToolButton
)
from typing import TYPE_CHECKING

from PyQt5.QtCore import QEvent, QPoint, Qt, QSize, pyqtSignal
from PyQt5.QtGui import QPalette
from PyQt5.QtWidgets import QMenu, QToolButton, QVBoxLayout, QWidget

from novelwriter import CONFIG, SHARED
from novelwriter.enum import nwView

if TYPE_CHECKING: # pragma: no cover
from novelwriter.guimain import GuiMain

logger = logging.getLogger(__name__)


class GuiSideBar(QToolBar):
class GuiSideBar(QWidget):

viewChangeRequested = pyqtSignal(nwView)

def __init__(self, mainGui):
def __init__(self, mainGui: GuiMain) -> None:
super().__init__(parent=mainGui)

logger.debug("Create: GuiSideBar")

self.mainGui = mainGui

# Style
iPx = CONFIG.pxInt(22)
mPx = CONFIG.pxInt(60)

lblFont = SHARED.theme.guiFont
lblFont.setPointSizeF(0.65*SHARED.theme.fontPointSize)

self.setMovable(False)
self.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
self.setIconSize(QSize(iPx, iPx))
self.setMaximumWidth(mPx)
iPx = CONFIG.pxInt(24)
iconSize = QSize(iPx, iPx)
self.setContentsMargins(0, 0, 0, 0)

stretch = QWidget(self)
stretch.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)

# Actions
self.aProject = QAction(self.tr("Project"), self)
self.aProject.setFont(lblFont)
self.aProject.setToolTip(self.tr("Project Tree View"))
self.aProject.triggered.connect(lambda: self.viewChangeRequested.emit(nwView.PROJECT))

self.aNovel = QAction(self.tr("Novel"), self)
self.aNovel.setFont(lblFont)
self.aNovel.setToolTip(self.tr("Novel Tree View"))
self.aNovel.triggered.connect(lambda: self.viewChangeRequested.emit(nwView.NOVEL))

self.aOutline = QAction(self.tr("Outline"), self)
self.aOutline.setFont(lblFont)
self.aOutline.setToolTip(self.tr("Novel Outline View"))
self.aOutline.triggered.connect(lambda: self.viewChangeRequested.emit(nwView.OUTLINE))

self.aBuild = QAction(self.tr("Build"), self)
self.aBuild.setFont(lblFont)
self.aBuild.setToolTip(self.tr("Build Manuscript"))
self.aBuild.triggered.connect(lambda: self.mainGui.showBuildManuscriptDialog())

self.aDetails = QAction(self.tr("Details"), self)
self.aDetails.setFont(lblFont)
self.aDetails.setToolTip(self.tr("Project Details"))
self.aDetails.triggered.connect(lambda: self.mainGui.showProjectDetailsDialog())

self.aStats = QAction(self.tr("Stats"), self)
self.aStats.setFont(lblFont)
self.aStats.setToolTip(self.tr("Writing Statistics"))
self.aStats.triggered.connect(lambda: self.mainGui.showWritingStatsDialog())
# Buttons
self.tbProject = QToolButton(self)
self.tbProject.setToolTip(self.tr("Project Tree View"))
self.tbProject.setIconSize(iconSize)
self.tbProject.clicked.connect(lambda: self.viewChangeRequested.emit(nwView.PROJECT))

self.tbNovel = QToolButton(self)
self.tbNovel.setToolTip(self.tr("Novel Tree View"))
self.tbNovel.setIconSize(iconSize)
self.tbNovel.clicked.connect(lambda: self.viewChangeRequested.emit(nwView.NOVEL))

self.tbOutline = QToolButton(self)
self.tbOutline.setToolTip(self.tr("Novel Outline View"))
self.tbOutline.setIconSize(iconSize)
self.tbOutline.clicked.connect(lambda: self.viewChangeRequested.emit(nwView.OUTLINE))

self.tbBuild = QToolButton(self)
self.tbBuild.setToolTip(self.tr("Build Manuscript"))
self.tbBuild.setIconSize(iconSize)
self.tbBuild.clicked.connect(self.mainGui.showBuildManuscriptDialog)

self.tbDetails = QToolButton(self)
self.tbDetails.setToolTip(self.tr("Project Details"))
self.tbDetails.setIconSize(iconSize)
self.tbDetails.clicked.connect(self.mainGui.showProjectDetailsDialog)

self.tbStats = QToolButton(self)
self.tbStats.setToolTip(self.tr("Writing Statistics"))
self.tbStats.setIconSize(iconSize)
self.tbStats.clicked.connect(self.mainGui.showWritingStatsDialog)

# Settings Menu
self.mSettings = QMenu()
self.tbSettings = QToolButton(self)
self.tbSettings.setToolTip(self.tr("Settings"))
self.tbSettings.setIconSize(iconSize)
self.tbSettings.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonIconOnly)

self.mSettings = _PopRightMenu(self.tbSettings)
self.mSettings.addAction(self.mainGui.mainMenu.aEditWordList)
self.mSettings.addAction(self.mainGui.mainMenu.aProjectSettings)
self.mSettings.addSeparator()
self.mSettings.addAction(self.mainGui.mainMenu.aPreferences)

self.tbSettings = QToolButton(self)
self.tbSettings.setFont(lblFont)
self.tbSettings.setText(self.tr("Settings"))
self.tbSettings.setMenu(self.mSettings)
self.tbSettings.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
self.tbSettings.setPopupMode(QToolButton.InstantPopup)

# Assemble
self.addAction(self.aProject)
self.addAction(self.aNovel)
self.addAction(self.aOutline)
self.addAction(self.aBuild)
self.addWidget(stretch)
self.addAction(self.aDetails)
self.addAction(self.aStats)
self.addWidget(self.tbSettings)
self.outerBox = QVBoxLayout()
self.outerBox.addWidget(self.tbProject)
self.outerBox.addWidget(self.tbNovel)
self.outerBox.addWidget(self.tbOutline)
self.outerBox.addWidget(self.tbBuild)
self.outerBox.addStretch(1)
self.outerBox.addWidget(self.tbDetails)
self.outerBox.addWidget(self.tbStats)
self.outerBox.addWidget(self.tbSettings)
self.outerBox.setContentsMargins(0, 0, CONFIG.pxInt(2), 0)
self.outerBox.setSpacing(CONFIG.pxInt(4))

self.setLayout(self.outerBox)

self.updateTheme()

logger.debug("Ready: GuiSideBar")

return

def updateTheme(self):
"""Initialise GUI elements that depend on specific settings.
"""
self.setStyleSheet("QToolBar {border: 0px;}")

self.aProject.setIcon(SHARED.theme.getIcon("view_editor"))
self.aNovel.setIcon(SHARED.theme.getIcon("view_novel"))
self.aOutline.setIcon(SHARED.theme.getIcon("view_outline"))
self.aBuild.setIcon(SHARED.theme.getIcon("view_build"))
self.aDetails.setIcon(SHARED.theme.getIcon("proj_details"))
self.aStats.setIcon(SHARED.theme.getIcon("proj_stats"))
def updateTheme(self) -> None:
"""Initialise GUI elements that depend on specific settings."""
qPalette = self.palette()
qPalette.setBrush(QPalette.Window, qPalette.base())
self.setPalette(qPalette)

fadeCol = qPalette.text().color()
buttonStyle = (
"QToolButton {{padding: {0}px; border: none; background: transparent;}} "
"QToolButton:hover {{border: none; background: rgba({1},{2},{3},0.2);}}"
).format(CONFIG.pxInt(4), fadeCol.red(), fadeCol.green(), fadeCol.blue())
buttonStyleMenu = f"{buttonStyle} QToolButton::menu-indicator {{image: none;}}"

self.tbProject.setIcon(SHARED.theme.getIcon("view_editor"))
self.tbProject.setStyleSheet(buttonStyle)

self.tbNovel.setIcon(SHARED.theme.getIcon("view_novel"))
self.tbNovel.setStyleSheet(buttonStyle)

self.tbOutline.setIcon(SHARED.theme.getIcon("view_outline"))
self.tbOutline.setStyleSheet(buttonStyle)

self.tbBuild.setIcon(SHARED.theme.getIcon("view_build"))
self.tbBuild.setStyleSheet(buttonStyle)

self.tbDetails.setIcon(SHARED.theme.getIcon("proj_details"))
self.tbDetails.setStyleSheet(buttonStyle)

self.tbStats.setIcon(SHARED.theme.getIcon("proj_stats"))
self.tbStats.setStyleSheet(buttonStyle)

self.tbSettings.setIcon(SHARED.theme.getIcon("settings"))
self.tbSettings.setStyleSheet(buttonStyleMenu)

return

# END Class GuiSideBar


class _PopRightMenu(QMenu):

def event(self, event: QEvent):
"""Overload the show event and move the menu popup location."""
if event.type() == QEvent.Show:
parent = self.parent()
if isinstance(parent, QWidget):
offset = QPoint(parent.width(), parent.height() - self.height())
self.move(parent.mapToGlobal(offset))
return super(_PopRightMenu, self).event(event)

# END Class _PopRightMenu
Loading