novelWriter 2.1.1__py3-none-any.whl → 2.2rc1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {novelWriter-2.1.1.dist-info → novelWriter-2.2rc1.dist-info}/METADATA +3 -3
- {novelWriter-2.1.1.dist-info → novelWriter-2.2rc1.dist-info}/RECORD +105 -76
- novelwriter/__init__.py +6 -24
- novelwriter/assets/i18n/project_de_DE.json +10 -0
- novelwriter/assets/i18n/project_en_GB.json +11 -0
- novelwriter/assets/i18n/project_en_US.json +10 -0
- novelwriter/assets/i18n/project_ja_JP.json +11 -1
- novelwriter/assets/i18n/project_nb_NO.json +10 -0
- novelwriter/assets/i18n/project_nn_NO.json +10 -0
- novelwriter/assets/icons/novelwriter.ico +0 -0
- novelwriter/assets/icons/novelwriter.svg +8 -183
- novelwriter/assets/icons/typicons_dark/icons.conf +17 -2
- novelwriter/assets/icons/typicons_dark/nw_deco-h2-narrow.svg +4 -0
- novelwriter/assets/icons/typicons_dark/nw_deco-h3-narrow.svg +4 -0
- novelwriter/assets/icons/typicons_dark/nw_deco-h4-narrow.svg +4 -0
- novelwriter/assets/icons/typicons_dark/nw_deco-note.svg +4 -0
- novelwriter/assets/icons/typicons_dark/nw_panel.svg +4 -0
- novelwriter/assets/icons/typicons_dark/nw_tb-bold.svg +4 -0
- novelwriter/assets/icons/typicons_dark/nw_tb-italic.svg +4 -0
- novelwriter/assets/icons/typicons_dark/nw_tb-markdown.svg +8 -0
- novelwriter/assets/icons/typicons_dark/nw_tb-shortcode.svg +8 -0
- novelwriter/assets/icons/typicons_dark/nw_tb-strike.svg +4 -0
- novelwriter/assets/icons/typicons_dark/nw_tb-subscript.svg +5 -0
- novelwriter/assets/icons/typicons_dark/nw_tb-superscript.svg +5 -0
- novelwriter/assets/icons/typicons_dark/nw_tb-underline.svg +5 -0
- novelwriter/assets/icons/typicons_dark/typ_eye.svg +4 -0
- novelwriter/assets/icons/typicons_dark/typ_th-dot-menu.svg +4 -0
- novelwriter/assets/icons/typicons_light/icons.conf +17 -2
- novelwriter/assets/icons/typicons_light/nw_deco-h2-narrow.svg +4 -0
- novelwriter/assets/icons/typicons_light/nw_deco-h3-narrow.svg +4 -0
- novelwriter/assets/icons/typicons_light/nw_deco-h4-narrow.svg +4 -0
- novelwriter/assets/icons/typicons_light/nw_deco-note.svg +4 -0
- novelwriter/assets/icons/typicons_light/nw_panel.svg +4 -0
- novelwriter/assets/icons/typicons_light/nw_tb-bold.svg +4 -0
- novelwriter/assets/icons/typicons_light/nw_tb-italic.svg +4 -0
- novelwriter/assets/icons/typicons_light/nw_tb-markdown.svg +8 -0
- novelwriter/assets/icons/typicons_light/nw_tb-shortcode.svg +8 -0
- novelwriter/assets/icons/typicons_light/nw_tb-strike.svg +4 -0
- novelwriter/assets/icons/typicons_light/nw_tb-subscript.svg +5 -0
- novelwriter/assets/icons/typicons_light/nw_tb-superscript.svg +5 -0
- novelwriter/assets/icons/typicons_light/nw_tb-underline.svg +5 -0
- novelwriter/assets/icons/typicons_light/typ_eye.svg +4 -0
- novelwriter/assets/icons/typicons_light/typ_th-dot-menu.svg +4 -0
- novelwriter/assets/icons/x-novelwriter-project.ico +0 -0
- novelwriter/assets/icons/x-novelwriter-project.svg +7 -206
- novelwriter/assets/manual.pdf +0 -0
- novelwriter/assets/sample.zip +0 -0
- novelwriter/assets/syntax/default_dark.conf +1 -0
- novelwriter/assets/syntax/default_light.conf +1 -0
- novelwriter/assets/syntax/grey_dark.conf +1 -0
- novelwriter/assets/syntax/grey_light.conf +1 -0
- novelwriter/assets/syntax/light_owl.conf +1 -0
- novelwriter/assets/syntax/night_owl.conf +1 -0
- novelwriter/assets/syntax/solarized_dark.conf +1 -0
- novelwriter/assets/syntax/solarized_light.conf +1 -0
- novelwriter/assets/syntax/tomorrow.conf +1 -0
- novelwriter/assets/syntax/tomorrow_night.conf +1 -0
- novelwriter/assets/syntax/tomorrow_night_blue.conf +1 -0
- novelwriter/assets/syntax/tomorrow_night_bright.conf +1 -0
- novelwriter/assets/syntax/tomorrow_night_eighties.conf +1 -0
- novelwriter/assets/text/credits_en.htm +7 -0
- novelwriter/assets/text/release_notes.htm +7 -37
- novelwriter/common.py +22 -1
- novelwriter/config.py +27 -42
- novelwriter/constants.py +45 -7
- novelwriter/core/buildsettings.py +40 -24
- novelwriter/core/coretools.py +8 -1
- novelwriter/core/docbuild.py +2 -6
- novelwriter/core/index.py +264 -175
- novelwriter/core/options.py +8 -3
- novelwriter/core/project.py +2 -2
- novelwriter/core/projectdata.py +3 -3
- novelwriter/core/tohtml.py +60 -59
- novelwriter/core/tokenizer.py +110 -70
- novelwriter/core/tomd.py +51 -38
- novelwriter/core/toodt.py +184 -147
- novelwriter/dialogs/preferences.py +75 -106
- novelwriter/dialogs/projsettings.py +101 -110
- novelwriter/dialogs/updates.py +25 -14
- novelwriter/enum.py +28 -3
- novelwriter/extensions/novelselector.py +1 -1
- novelwriter/gui/doceditor.py +1345 -1235
- novelwriter/gui/dochighlight.py +98 -62
- novelwriter/gui/docviewer.py +151 -340
- novelwriter/gui/docviewerpanel.py +457 -0
- novelwriter/gui/editordocument.py +126 -0
- novelwriter/gui/mainmenu.py +350 -300
- novelwriter/gui/noveltree.py +101 -125
- novelwriter/gui/outline.py +154 -171
- novelwriter/gui/projtree.py +480 -380
- novelwriter/gui/sidebar.py +106 -75
- novelwriter/gui/statusbar.py +1 -1
- novelwriter/gui/theme.py +114 -75
- novelwriter/guimain.py +353 -254
- novelwriter/shared.py +36 -3
- novelwriter/tools/dictionaries.py +268 -0
- novelwriter/tools/manusbuild.py +17 -6
- novelwriter/tools/manuscript.py +11 -3
- novelwriter/tools/manussettings.py +0 -14
- novelwriter/tools/projwizard.py +16 -2
- novelwriter/tools/writingstats.py +1 -1
- novelwriter/assets/icons/typicons_dark/typ_at.svg +0 -4
- novelwriter/assets/icons/typicons_dark/typ_th-menu.svg +0 -4
- novelwriter/assets/icons/typicons_light/typ_at.svg +0 -4
- novelwriter/assets/icons/typicons_light/typ_th-menu.svg +0 -4
- {novelWriter-2.1.1.dist-info → novelWriter-2.2rc1.dist-info}/LICENSE.md +0 -0
- {novelWriter-2.1.1.dist-info → novelWriter-2.2rc1.dist-info}/WHEEL +0 -0
- {novelWriter-2.1.1.dist-info → novelWriter-2.2rc1.dist-info}/entry_points.txt +0 -0
- {novelWriter-2.1.1.dist-info → novelWriter-2.2rc1.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,457 @@
|
|
1
|
+
"""
|
2
|
+
novelWriter – GUI Document Viewer Panel
|
3
|
+
=======================================
|
4
|
+
|
5
|
+
File History:
|
6
|
+
Created: 2023-11-14 [2.2rc1] GuiDocViewerPanel
|
7
|
+
|
8
|
+
This file is a part of novelWriter
|
9
|
+
Copyright 2018–2023, Veronica Berglyd Olsen
|
10
|
+
|
11
|
+
This program is free software: you can redistribute it and/or modify
|
12
|
+
it under the terms of the GNU General Public License as published by
|
13
|
+
the Free Software Foundation, either version 3 of the License, or
|
14
|
+
(at your option) any later version.
|
15
|
+
|
16
|
+
This program is distributed in the hope that it will be useful, but
|
17
|
+
WITHOUT ANY WARRANTY; without even the implied warranty of
|
18
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
19
|
+
General Public License for more details.
|
20
|
+
|
21
|
+
You should have received a copy of the GNU General Public License
|
22
|
+
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
23
|
+
"""
|
24
|
+
from __future__ import annotations
|
25
|
+
|
26
|
+
import logging
|
27
|
+
|
28
|
+
from enum import Enum
|
29
|
+
|
30
|
+
from PyQt5.QtCore import QModelIndex, QSize, Qt, pyqtSignal, pyqtSlot
|
31
|
+
from PyQt5.QtWidgets import (
|
32
|
+
QAbstractItemView, QFrame, QHeaderView, QTabWidget, QTreeWidget,
|
33
|
+
QTreeWidgetItem, QVBoxLayout, QWidget
|
34
|
+
)
|
35
|
+
|
36
|
+
from novelwriter import CONFIG, SHARED
|
37
|
+
from novelwriter.enum import nwDocMode, nwItemClass
|
38
|
+
from novelwriter.common import checkInt
|
39
|
+
from novelwriter.constants import nwHeaders, nwLabels, nwLists, trConst
|
40
|
+
from novelwriter.core.index import IndexHeading, IndexItem
|
41
|
+
|
42
|
+
logger = logging.getLogger(__name__)
|
43
|
+
|
44
|
+
|
45
|
+
class GuiDocViewerPanel(QWidget):
|
46
|
+
|
47
|
+
openDocumentRequest = pyqtSignal(str, Enum, str, bool)
|
48
|
+
loadDocumentTagRequest = pyqtSignal(str, Enum)
|
49
|
+
|
50
|
+
def __init__(self, parent: QWidget) -> None:
|
51
|
+
super().__init__(parent=parent)
|
52
|
+
|
53
|
+
logger.debug("Create: GuiDocViewerPanel")
|
54
|
+
|
55
|
+
self._lastHandle = None
|
56
|
+
|
57
|
+
self.tabBackRefs = _ViewPanelBackRefs(self)
|
58
|
+
|
59
|
+
self.mainTabs = QTabWidget(self)
|
60
|
+
self.mainTabs.addTab(self.tabBackRefs, self.tr("Backreferences"))
|
61
|
+
|
62
|
+
self.kwTabs: dict[str, _ViewPanelKeyWords] = {}
|
63
|
+
self.idTabs: dict[str, int] = {}
|
64
|
+
for itemClass in nwLists.USER_CLASSES:
|
65
|
+
cTab = _ViewPanelKeyWords(self, itemClass)
|
66
|
+
tabId = self.mainTabs.addTab(cTab, trConst(nwLabels.CLASS_NAME[itemClass]))
|
67
|
+
self.kwTabs[itemClass.name] = cTab
|
68
|
+
self.idTabs[itemClass.name] = tabId
|
69
|
+
|
70
|
+
# Assemble
|
71
|
+
self.outerBox = QVBoxLayout()
|
72
|
+
self.outerBox.addWidget(self.mainTabs)
|
73
|
+
self.outerBox.setContentsMargins(0, 0, 0, 0)
|
74
|
+
|
75
|
+
self.setLayout(self.outerBox)
|
76
|
+
self.updateTheme()
|
77
|
+
|
78
|
+
logger.debug("Ready: GuiDocViewerPanel")
|
79
|
+
|
80
|
+
return
|
81
|
+
|
82
|
+
##
|
83
|
+
# Methods
|
84
|
+
##
|
85
|
+
|
86
|
+
def updateTheme(self) -> None:
|
87
|
+
"""Update theme elements."""
|
88
|
+
vPx = CONFIG.pxInt(4)
|
89
|
+
lPx = CONFIG.pxInt(2)
|
90
|
+
rPx = CONFIG.pxInt(14)
|
91
|
+
hCol = self.palette().highlight().color()
|
92
|
+
|
93
|
+
styleSheet = (
|
94
|
+
"QTabWidget::pane {border: 0;} "
|
95
|
+
"QTabWidget QTabBar::tab {"
|
96
|
+
f"border: 0; padding: {vPx}px {rPx}px {vPx}px {lPx}px;"
|
97
|
+
"} "
|
98
|
+
"QTabWidget QTabBar::tab:selected {"
|
99
|
+
f"color: rgb({hCol.red()}, {hCol.green()}, {hCol.blue()});"
|
100
|
+
"} "
|
101
|
+
)
|
102
|
+
self.mainTabs.setStyleSheet(styleSheet)
|
103
|
+
self.updateHandle(self._lastHandle)
|
104
|
+
|
105
|
+
return
|
106
|
+
|
107
|
+
def openProjectTasks(self) -> None:
|
108
|
+
"""Run open project tasks."""
|
109
|
+
widths = SHARED.project.options.getValue("GuiDocViewerPanel", "colWidths", {})
|
110
|
+
if isinstance(widths, dict):
|
111
|
+
for key, value in widths.items():
|
112
|
+
if key in self.kwTabs and isinstance(value, list):
|
113
|
+
self.kwTabs[key].setColumnWidths(value)
|
114
|
+
return
|
115
|
+
|
116
|
+
def closeProjectTasks(self) -> None:
|
117
|
+
"""Run close project tasks."""
|
118
|
+
widths = {}
|
119
|
+
for key, tab in self.kwTabs.items():
|
120
|
+
widths[key] = tab.getColumnWidths()
|
121
|
+
SHARED.project.options.setValue("GuiDocViewerPanel", "colWidths", widths)
|
122
|
+
return
|
123
|
+
|
124
|
+
##
|
125
|
+
# Public Slots
|
126
|
+
##
|
127
|
+
|
128
|
+
@pyqtSlot()
|
129
|
+
def indexWasCleared(self) -> None:
|
130
|
+
"""Handle event when the index has been cleared of content."""
|
131
|
+
self.tabBackRefs.clearContent()
|
132
|
+
for cTab in self.kwTabs.values():
|
133
|
+
cTab.clearContent()
|
134
|
+
return
|
135
|
+
|
136
|
+
@pyqtSlot()
|
137
|
+
def indexHasAppeared(self) -> None:
|
138
|
+
"""Handle event when the index has appeared."""
|
139
|
+
for key, name, tClass, iItem, hItem in SHARED.project.index.getTagsData():
|
140
|
+
if tClass in self.kwTabs and iItem and hItem:
|
141
|
+
self.kwTabs[tClass].addUpdateEntry(key, name, iItem, hItem)
|
142
|
+
self._updateTabVisibility()
|
143
|
+
self.updateHandle(self._lastHandle)
|
144
|
+
return
|
145
|
+
|
146
|
+
@pyqtSlot(str)
|
147
|
+
def projectItemChanged(self, tHandle: str) -> None:
|
148
|
+
"""Update meta data for project item."""
|
149
|
+
self.tabBackRefs.refreshDocument(tHandle)
|
150
|
+
for key in SHARED.project.index.getDocumentTags(tHandle):
|
151
|
+
name, tClass, iItem, hItem = SHARED.project.index.getSingleTag(key)
|
152
|
+
if tClass in self.kwTabs and iItem and hItem:
|
153
|
+
self.kwTabs[tClass].addUpdateEntry(key, name, iItem, hItem)
|
154
|
+
return
|
155
|
+
|
156
|
+
@pyqtSlot(str)
|
157
|
+
def updateHandle(self, tHandle: str | None) -> None:
|
158
|
+
"""Update the document handle."""
|
159
|
+
self._lastHandle = tHandle
|
160
|
+
self.tabBackRefs.refreshContent(tHandle or None)
|
161
|
+
return
|
162
|
+
|
163
|
+
@pyqtSlot(list, list)
|
164
|
+
def updateChangedTags(self, updated: list[str], deleted: list[str]) -> None:
|
165
|
+
"""Forward tags changes to the lists."""
|
166
|
+
for key in updated:
|
167
|
+
name, tClass, iItem, hItem = SHARED.project.index.getSingleTag(key)
|
168
|
+
if tClass in self.kwTabs and iItem and hItem:
|
169
|
+
self.kwTabs[tClass].addUpdateEntry(key, name, iItem, hItem)
|
170
|
+
for key in deleted:
|
171
|
+
for cTab in self.kwTabs.values():
|
172
|
+
if cTab.removeEntry(key):
|
173
|
+
break
|
174
|
+
else:
|
175
|
+
logger.warning("Could not remove tag '%s' from view panel", key)
|
176
|
+
self._updateTabVisibility()
|
177
|
+
return
|
178
|
+
|
179
|
+
##
|
180
|
+
# Internal Functions
|
181
|
+
##
|
182
|
+
|
183
|
+
def _updateTabVisibility(self) -> None:
|
184
|
+
"""Hide class tabs with no content."""
|
185
|
+
if CONFIG.verQtValue >= 0x050f00:
|
186
|
+
for tClass, cTab in self.kwTabs.items():
|
187
|
+
self.mainTabs.setTabVisible(self.idTabs[tClass], cTab.countEntries() > 0)
|
188
|
+
return
|
189
|
+
|
190
|
+
# END Class GuiDocViewerPanel
|
191
|
+
|
192
|
+
|
193
|
+
class _ViewPanelBackRefs(QTreeWidget):
|
194
|
+
|
195
|
+
C_DATA = 0
|
196
|
+
C_DOC = 0
|
197
|
+
C_EDIT = 1
|
198
|
+
C_VIEW = 2
|
199
|
+
C_TITLE = 3
|
200
|
+
|
201
|
+
D_HANDLE = Qt.ItemDataRole.UserRole
|
202
|
+
|
203
|
+
def __init__(self, parent: GuiDocViewerPanel) -> None:
|
204
|
+
super().__init__(parent=parent)
|
205
|
+
|
206
|
+
self._parent = parent
|
207
|
+
self._treeMap: dict[str, QTreeWidgetItem] = {}
|
208
|
+
|
209
|
+
iPx = SHARED.theme.baseIconSize
|
210
|
+
cMg = CONFIG.pxInt(6)
|
211
|
+
|
212
|
+
self.setHeaderLabels([self.tr("Document"), "", "", self.tr("First Heading")])
|
213
|
+
self.setIndentation(0)
|
214
|
+
self.setSelectionMode(QAbstractItemView.SelectionMode.NoSelection)
|
215
|
+
self.setIconSize(QSize(iPx, iPx))
|
216
|
+
self.setFrameStyle(QFrame.Shape.NoFrame)
|
217
|
+
|
218
|
+
# Set Header Sizes
|
219
|
+
treeHeader = self.header()
|
220
|
+
treeHeader.setStretchLastSection(True)
|
221
|
+
treeHeader.setSectionResizeMode(self.C_DOC, QHeaderView.ResizeMode.ResizeToContents)
|
222
|
+
treeHeader.setSectionResizeMode(self.C_EDIT, QHeaderView.ResizeMode.Fixed)
|
223
|
+
treeHeader.setSectionResizeMode(self.C_VIEW, QHeaderView.ResizeMode.Fixed)
|
224
|
+
treeHeader.setSectionResizeMode(self.C_TITLE, QHeaderView.ResizeMode.ResizeToContents)
|
225
|
+
treeHeader.resizeSection(self.C_EDIT, iPx + cMg)
|
226
|
+
treeHeader.resizeSection(self.C_VIEW, iPx + cMg)
|
227
|
+
|
228
|
+
# Cache Icons Locally
|
229
|
+
self._editIcon = SHARED.theme.getIcon("edit")
|
230
|
+
self._viewIcon = SHARED.theme.getIcon("view")
|
231
|
+
|
232
|
+
# Signals
|
233
|
+
self.clicked.connect(self._treeItemClicked)
|
234
|
+
self.doubleClicked.connect(self._treeItemDoubleClicked)
|
235
|
+
|
236
|
+
return
|
237
|
+
|
238
|
+
def clearContent(self) -> None:
|
239
|
+
"""Clear the widget."""
|
240
|
+
self.clear()
|
241
|
+
self._treeMap = {}
|
242
|
+
return
|
243
|
+
|
244
|
+
def refreshContent(self, dHandle: str | None) -> None:
|
245
|
+
"""Update the content."""
|
246
|
+
self.clearContent()
|
247
|
+
if dHandle:
|
248
|
+
refs = SHARED.project.index.getBackReferenceList(dHandle)
|
249
|
+
for tHandle, (sTitle, hItem) in refs.items():
|
250
|
+
self._setTreeItemValues(tHandle, sTitle, hItem)
|
251
|
+
return
|
252
|
+
|
253
|
+
def refreshDocument(self, tHandle: str) -> None:
|
254
|
+
"""Refresh document meta data."""
|
255
|
+
if iItem := SHARED.project.index.getItemData(tHandle):
|
256
|
+
for sTitle, hItem in iItem.items():
|
257
|
+
if f"{tHandle}:{sTitle}" in self._treeMap:
|
258
|
+
self._setTreeItemValues(tHandle, sTitle, hItem)
|
259
|
+
return
|
260
|
+
|
261
|
+
##
|
262
|
+
# Private Slots
|
263
|
+
##
|
264
|
+
|
265
|
+
@pyqtSlot("QModelIndex")
|
266
|
+
def _treeItemClicked(self, index: QModelIndex) -> None:
|
267
|
+
"""Emit document open signal on user click."""
|
268
|
+
tHandle = index.siblingAtColumn(self.C_DATA).data(self.D_HANDLE)
|
269
|
+
if index.column() == self.C_EDIT:
|
270
|
+
self._parent.openDocumentRequest.emit(tHandle, nwDocMode.EDIT, "", True)
|
271
|
+
elif index.column() == self.C_VIEW:
|
272
|
+
self._parent.openDocumentRequest.emit(tHandle, nwDocMode.VIEW, "", True)
|
273
|
+
return
|
274
|
+
|
275
|
+
@pyqtSlot("QModelIndex")
|
276
|
+
def _treeItemDoubleClicked(self, index: QModelIndex) -> None:
|
277
|
+
"""Emit follow tag signal on user double click."""
|
278
|
+
tHandle = index.siblingAtColumn(self.C_DATA).data(self.D_HANDLE)
|
279
|
+
if index.column() == self.C_DOC:
|
280
|
+
self._parent.openDocumentRequest.emit(tHandle, nwDocMode.VIEW, "", True)
|
281
|
+
return
|
282
|
+
|
283
|
+
##
|
284
|
+
# Internal Functions
|
285
|
+
##
|
286
|
+
|
287
|
+
def _setTreeItemValues(self, tHandle: str, sTitle: str, hItem: IndexHeading) -> None:
|
288
|
+
"""Add or update a tree item."""
|
289
|
+
if nwItem := SHARED.project.tree[tHandle]:
|
290
|
+
docIcon = SHARED.theme.getItemIcon(
|
291
|
+
nwItem.itemType, nwItem.itemClass,
|
292
|
+
nwItem.itemLayout, nwItem.mainHeading
|
293
|
+
)
|
294
|
+
iLevel = nwHeaders.H_LEVEL.get(hItem.level, 0) if nwItem.isDocumentLayout() else 5
|
295
|
+
hDec = SHARED.theme.getHeaderDecorationNarrow(iLevel)
|
296
|
+
|
297
|
+
tKey = f"{tHandle}:{sTitle}"
|
298
|
+
trItem = self._treeMap[tKey] if tKey in self._treeMap else QTreeWidgetItem()
|
299
|
+
|
300
|
+
trItem.setIcon(self.C_DOC, docIcon)
|
301
|
+
trItem.setText(self.C_DOC, nwItem.itemName)
|
302
|
+
trItem.setIcon(self.C_EDIT, self._editIcon)
|
303
|
+
trItem.setIcon(self.C_VIEW, self._viewIcon)
|
304
|
+
trItem.setText(self.C_TITLE, hItem.title)
|
305
|
+
trItem.setData(self.C_TITLE, Qt.ItemDataRole.DecorationRole, hDec)
|
306
|
+
trItem.setData(self.C_DATA, self.D_HANDLE, tHandle)
|
307
|
+
|
308
|
+
if tKey not in self._treeMap:
|
309
|
+
self.addTopLevelItem(trItem)
|
310
|
+
self._treeMap[tKey] = trItem
|
311
|
+
|
312
|
+
return
|
313
|
+
|
314
|
+
# END Class _ViewPanelBackRefs
|
315
|
+
|
316
|
+
|
317
|
+
class _ViewPanelKeyWords(QTreeWidget):
|
318
|
+
|
319
|
+
C_DATA = 0
|
320
|
+
C_NAME = 0
|
321
|
+
C_EDIT = 1
|
322
|
+
C_VIEW = 2
|
323
|
+
C_DOC = 3
|
324
|
+
C_TITLE = 4
|
325
|
+
C_SHORT = 5
|
326
|
+
|
327
|
+
D_TAG = Qt.ItemDataRole.UserRole
|
328
|
+
|
329
|
+
def __init__(self, parent: GuiDocViewerPanel, itemClass: nwItemClass) -> None:
|
330
|
+
super().__init__(parent=parent)
|
331
|
+
|
332
|
+
self._parent = parent
|
333
|
+
self._treeMap: dict[str, QTreeWidgetItem] = {}
|
334
|
+
|
335
|
+
iPx = SHARED.theme.baseIconSize
|
336
|
+
cMg = CONFIG.pxInt(6)
|
337
|
+
|
338
|
+
self.setHeaderLabels([
|
339
|
+
self.tr("Tag"), "", "", self.tr("Document"),
|
340
|
+
self.tr("Heading"), self.tr("Short Description")
|
341
|
+
])
|
342
|
+
self.setIndentation(0)
|
343
|
+
self.setIconSize(QSize(iPx, iPx))
|
344
|
+
self.setFrameStyle(QFrame.Shape.NoFrame)
|
345
|
+
self.setSelectionMode(QAbstractItemView.SelectionMode.NoSelection)
|
346
|
+
self.setExpandsOnDoubleClick(False)
|
347
|
+
self.setDragEnabled(False)
|
348
|
+
self.setSortingEnabled(True)
|
349
|
+
self.sortByColumn(self.C_NAME, Qt.SortOrder.AscendingOrder)
|
350
|
+
|
351
|
+
# Set Header Sizes
|
352
|
+
treeHeader = self.header()
|
353
|
+
treeHeader.setStretchLastSection(True)
|
354
|
+
treeHeader.setSectionResizeMode(self.C_NAME, QHeaderView.ResizeMode.ResizeToContents)
|
355
|
+
treeHeader.setSectionResizeMode(self.C_EDIT, QHeaderView.ResizeMode.Fixed)
|
356
|
+
treeHeader.setSectionResizeMode(self.C_VIEW, QHeaderView.ResizeMode.Fixed)
|
357
|
+
treeHeader.resizeSection(self.C_EDIT, iPx + cMg)
|
358
|
+
treeHeader.resizeSection(self.C_VIEW, iPx + cMg)
|
359
|
+
treeHeader.setSectionsMovable(False)
|
360
|
+
|
361
|
+
# Cache Icons Locally
|
362
|
+
self._classIcon = SHARED.theme.getIcon(nwLabels.CLASS_ICON[itemClass])
|
363
|
+
self._editIcon = SHARED.theme.getIcon("edit")
|
364
|
+
self._viewIcon = SHARED.theme.getIcon("view")
|
365
|
+
|
366
|
+
# Signals
|
367
|
+
self.clicked.connect(self._treeItemClicked)
|
368
|
+
self.doubleClicked.connect(self._treeItemDoubleClicked)
|
369
|
+
|
370
|
+
return
|
371
|
+
|
372
|
+
def countEntries(self) -> int:
|
373
|
+
"""Return the number of items in the list."""
|
374
|
+
return self.topLevelItemCount()
|
375
|
+
|
376
|
+
def clearContent(self) -> None:
|
377
|
+
"""Clear the list."""
|
378
|
+
self._treeMap = {}
|
379
|
+
self.clear()
|
380
|
+
return
|
381
|
+
|
382
|
+
def addUpdateEntry(self, tag: str, name: str, iItem: IndexItem, hItem: IndexHeading) -> None:
|
383
|
+
"""Add a new entry, or update an existing one."""
|
384
|
+
nwItem = iItem.item
|
385
|
+
docIcon = SHARED.theme.getItemIcon(
|
386
|
+
nwItem.itemType, nwItem.itemClass,
|
387
|
+
nwItem.itemLayout, nwItem.mainHeading
|
388
|
+
)
|
389
|
+
iLevel = nwHeaders.H_LEVEL.get(hItem.level, 0) if nwItem.isDocumentLayout() else 5
|
390
|
+
hDec = SHARED.theme.getHeaderDecorationNarrow(iLevel)
|
391
|
+
|
392
|
+
# This can not use a get call to the dictionary as that creates
|
393
|
+
# some weird issue with Qt, so we need to do this with an if
|
394
|
+
trItem = self._treeMap[tag] if tag in self._treeMap else QTreeWidgetItem()
|
395
|
+
|
396
|
+
trItem.setText(self.C_NAME, name)
|
397
|
+
trItem.setIcon(self.C_NAME, self._classIcon)
|
398
|
+
trItem.setIcon(self.C_EDIT, self._editIcon)
|
399
|
+
trItem.setIcon(self.C_VIEW, self._viewIcon)
|
400
|
+
trItem.setIcon(self.C_DOC, docIcon)
|
401
|
+
trItem.setText(self.C_DOC, nwItem.itemName)
|
402
|
+
trItem.setText(self.C_TITLE, hItem.title)
|
403
|
+
trItem.setData(self.C_TITLE, Qt.ItemDataRole.DecorationRole, hDec)
|
404
|
+
trItem.setText(self.C_SHORT, hItem.synopsis)
|
405
|
+
trItem.setData(self.C_DATA, self.D_TAG, tag)
|
406
|
+
|
407
|
+
if tag not in self._treeMap:
|
408
|
+
self.addTopLevelItem(trItem)
|
409
|
+
self._treeMap[tag] = trItem
|
410
|
+
|
411
|
+
return
|
412
|
+
|
413
|
+
def removeEntry(self, tag: str) -> bool:
|
414
|
+
"""Remove a tag from the list."""
|
415
|
+
if tag in self._treeMap:
|
416
|
+
self.takeTopLevelItem(self.indexOfTopLevelItem(self._treeMap[tag]))
|
417
|
+
self._treeMap.pop(tag, None)
|
418
|
+
return True
|
419
|
+
return False
|
420
|
+
|
421
|
+
def setColumnWidths(self, widths: list[int]) -> None:
|
422
|
+
"""Set the column widths."""
|
423
|
+
if isinstance(widths, list) and len(widths) >= 2:
|
424
|
+
self.setColumnWidth(self.C_DOC, CONFIG.pxInt(checkInt(widths[0], 100)))
|
425
|
+
self.setColumnWidth(self.C_TITLE, CONFIG.pxInt(checkInt(widths[1], 100)))
|
426
|
+
return
|
427
|
+
|
428
|
+
def getColumnWidths(self) -> list[int]:
|
429
|
+
"""Get the widths of the user-adjustable columns."""
|
430
|
+
return [
|
431
|
+
CONFIG.rpxInt(self.columnWidth(self.C_DOC)),
|
432
|
+
CONFIG.rpxInt(self.columnWidth(self.C_TITLE)),
|
433
|
+
]
|
434
|
+
|
435
|
+
##
|
436
|
+
# Private Slots
|
437
|
+
##
|
438
|
+
|
439
|
+
@pyqtSlot("QModelIndex")
|
440
|
+
def _treeItemClicked(self, index: QModelIndex) -> None:
|
441
|
+
"""Emit follow tag signal on user click."""
|
442
|
+
tag = index.siblingAtColumn(self.C_DATA).data(self.D_TAG)
|
443
|
+
if index.column() == self.C_EDIT:
|
444
|
+
self._parent.loadDocumentTagRequest.emit(tag, nwDocMode.EDIT)
|
445
|
+
elif index.column() == self.C_VIEW:
|
446
|
+
self._parent.loadDocumentTagRequest.emit(tag, nwDocMode.VIEW)
|
447
|
+
return
|
448
|
+
|
449
|
+
@pyqtSlot("QModelIndex")
|
450
|
+
def _treeItemDoubleClicked(self, index: QModelIndex) -> None:
|
451
|
+
"""Emit follow tag signal on user double click."""
|
452
|
+
tag = index.siblingAtColumn(self.C_DATA).data(self.D_TAG)
|
453
|
+
if index.column() == self.C_NAME:
|
454
|
+
self._parent.loadDocumentTagRequest.emit(tag, nwDocMode.VIEW)
|
455
|
+
return
|
456
|
+
|
457
|
+
# END Class _ViewPanelKeyWords
|
@@ -0,0 +1,126 @@
|
|
1
|
+
"""
|
2
|
+
novelWriter – GUI Text Document
|
3
|
+
===============================
|
4
|
+
|
5
|
+
File History:
|
6
|
+
Created: 2023-09-07 [2.2b1]
|
7
|
+
|
8
|
+
This file is a part of novelWriter
|
9
|
+
Copyright 2018–2023, Veronica Berglyd Olsen
|
10
|
+
|
11
|
+
This program is free software: you can redistribute it and/or modify
|
12
|
+
it under the terms of the GNU General Public License as published by
|
13
|
+
the Free Software Foundation, either version 3 of the License, or
|
14
|
+
(at your option) any later version.
|
15
|
+
|
16
|
+
This program is distributed in the hope that it will be useful, but
|
17
|
+
WITHOUT ANY WARRANTY; without even the implied warranty of
|
18
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
19
|
+
General Public License for more details.
|
20
|
+
|
21
|
+
You should have received a copy of the GNU General Public License
|
22
|
+
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
23
|
+
"""
|
24
|
+
from __future__ import annotations
|
25
|
+
|
26
|
+
import logging
|
27
|
+
|
28
|
+
from time import time
|
29
|
+
|
30
|
+
from PyQt5.QtGui import QTextCursor, QTextDocument
|
31
|
+
from PyQt5.QtCore import QObject, pyqtSlot
|
32
|
+
from PyQt5.QtWidgets import QPlainTextDocumentLayout, qApp
|
33
|
+
from novelwriter import SHARED
|
34
|
+
|
35
|
+
from novelwriter.gui.dochighlight import GuiDocHighlighter, TextBlockData
|
36
|
+
|
37
|
+
logger = logging.getLogger(__name__)
|
38
|
+
|
39
|
+
|
40
|
+
class GuiTextDocument(QTextDocument):
|
41
|
+
|
42
|
+
def __init__(self, parent: QObject) -> None:
|
43
|
+
super().__init__(parent=parent)
|
44
|
+
|
45
|
+
self._handle = None
|
46
|
+
self._syntax = GuiDocHighlighter(self)
|
47
|
+
self.setDocumentLayout(QPlainTextDocumentLayout(self))
|
48
|
+
|
49
|
+
logger.debug("Ready: GuiTextDocument")
|
50
|
+
|
51
|
+
return
|
52
|
+
|
53
|
+
def __del__(self): # pragma: no cover
|
54
|
+
logger.debug("Delete: GuiTextDocument")
|
55
|
+
return
|
56
|
+
|
57
|
+
##
|
58
|
+
# Properties
|
59
|
+
##
|
60
|
+
|
61
|
+
@property
|
62
|
+
def syntaxHighlighter(self) -> GuiDocHighlighter:
|
63
|
+
"""Return the document's syntax highlighter object."""
|
64
|
+
return self._syntax
|
65
|
+
|
66
|
+
##
|
67
|
+
# Methods
|
68
|
+
##
|
69
|
+
|
70
|
+
def setTextContent(self, text: str, tHandle: str) -> None:
|
71
|
+
"""Set the text content of the document."""
|
72
|
+
self._syntax.setHandle(tHandle)
|
73
|
+
|
74
|
+
self.blockSignals(True)
|
75
|
+
self.setUndoRedoEnabled(False)
|
76
|
+
self.clear()
|
77
|
+
|
78
|
+
tStart = time()
|
79
|
+
|
80
|
+
self.setPlainText(text)
|
81
|
+
count = self.lineCount()
|
82
|
+
|
83
|
+
tMid = time()
|
84
|
+
|
85
|
+
self.setUndoRedoEnabled(True)
|
86
|
+
self.blockSignals(False)
|
87
|
+
self._syntax.rehighlight()
|
88
|
+
qApp.processEvents()
|
89
|
+
|
90
|
+
tEnd = time()
|
91
|
+
|
92
|
+
logger.debug("Loaded %d text blocks in %.3f ms", count, 1000*(tMid - tStart))
|
93
|
+
logger.debug("Highlighted document in %.3f ms", 1000*(tEnd - tMid))
|
94
|
+
|
95
|
+
return
|
96
|
+
|
97
|
+
def spellErrorAtPos(self, pos: int) -> tuple[str, int, int, list[str]]:
|
98
|
+
"""Check if there is a misspelled word at a given position in
|
99
|
+
the document, and if so, return it.
|
100
|
+
"""
|
101
|
+
cursor = QTextCursor(self)
|
102
|
+
cursor.setPosition(pos)
|
103
|
+
block = cursor.block()
|
104
|
+
data = block.userData()
|
105
|
+
if block.isValid() and isinstance(data, TextBlockData):
|
106
|
+
text = block.text()
|
107
|
+
check = pos - block.position()
|
108
|
+
if check >= 0:
|
109
|
+
for cPos, cLen in data.spellErrors:
|
110
|
+
cEnd = cPos + cLen
|
111
|
+
if cPos <= check <= cEnd:
|
112
|
+
word = text[cPos:cEnd]
|
113
|
+
return word, cPos, cLen, SHARED.spelling.suggestWords(word)
|
114
|
+
return "", -1, -1, []
|
115
|
+
|
116
|
+
##
|
117
|
+
# Public Slots
|
118
|
+
##
|
119
|
+
|
120
|
+
@pyqtSlot(bool)
|
121
|
+
def setSpellCheckState(self, state: bool) -> None:
|
122
|
+
"""Set the spell check state of the syntax highlighter."""
|
123
|
+
self._syntax.setSpellCheck(state)
|
124
|
+
return
|
125
|
+
|
126
|
+
# END Class GuiTextDocument
|