manuskript/manuskript/ui/editors/mainEditor.py

266 lines
8.7 KiB
Python
Raw Normal View History

2015-06-28 00:06:35 +12:00
#!/usr/bin/env python
2016-02-07 00:34:22 +13:00
# --!-- coding: utf8 --!--
2015-06-30 04:51:20 +12:00
import locale
2016-02-07 00:34:22 +13:00
from PyQt5.QtCore import QModelIndex, QRect, QPoint
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QPainter
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import qApp
from manuskript import settings
from manuskript.enums import Outline
from manuskript.functions import AUC, mainWindow, drawProgress
from manuskript.ui import style
2016-02-07 00:34:22 +13:00
from manuskript.ui.editors.editorWidget import editorWidget
from manuskript.ui.editors.fullScreenEditor import fullScreenEditor
from manuskript.ui.editors.mainEditor_ui import Ui_mainEditor
2015-06-30 04:51:20 +12:00
locale.setlocale(locale.LC_ALL, '')
2015-06-28 00:06:35 +12:00
class mainEditor(QWidget, Ui_mainEditor):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setupUi(self)
2015-06-30 00:21:57 +12:00
self._updating = False
2016-02-07 00:34:22 +13:00
2015-06-28 00:06:35 +12:00
self.mw = mainWindow()
2016-02-08 21:04:24 +13:00
2015-06-28 23:03:57 +12:00
# Connections --------------------------------------------------------
2016-02-07 00:34:22 +13:00
2015-06-28 00:06:35 +12:00
self.sldCorkSizeFactor.valueChanged.connect(
2016-02-07 00:34:22 +13:00
self.setCorkSizeFactor, AUC)
2015-06-28 00:06:35 +12:00
self.btnRedacFolderCork.toggled.connect(
2016-04-11 03:29:27 +12:00
self.sldCorkSizeFactor.setVisible, AUC
)
2015-06-28 00:06:35 +12:00
self.btnRedacFolderText.clicked.connect(
2016-02-07 00:34:22 +13:00
lambda v: self.setFolderView("text"), AUC)
2015-06-28 00:06:35 +12:00
self.btnRedacFolderCork.clicked.connect(
2016-02-07 00:34:22 +13:00
lambda v: self.setFolderView("cork"), AUC)
2015-06-28 00:06:35 +12:00
self.btnRedacFolderOutline.clicked.connect(
2016-02-07 00:34:22 +13:00
lambda v: self.setFolderView("outline"), AUC)
2015-06-28 00:06:35 +12:00
self.btnRedacFullscreen.clicked.connect(
2016-02-07 00:34:22 +13:00
self.showFullScreen, AUC)
2016-04-11 03:29:27 +12:00
# self.tab.setDocumentMode(False)
2016-04-09 01:23:20 +12:00
2016-02-07 00:34:22 +13:00
###############################################################################
# TABS
###############################################################################
2016-04-11 03:29:27 +12:00
def currentTabWidget(self):
"""Returns the tabSplitter that has focus."""
ts = self.tabSplitter
while ts:
if ts.focusTab == 1:
return ts.tab
else:
ts = ts.secondTab
2015-06-28 00:06:35 +12:00
def currentEditor(self):
2016-04-11 03:29:27 +12:00
return self.currentTabWidget().currentWidget()
# return self.tab.currentWidget()
2016-02-07 00:34:22 +13:00
2016-04-11 03:29:27 +12:00
def tabChanged(self, index=QModelIndex()):
2015-06-30 00:21:57 +12:00
if self.currentEditor():
index = self.currentEditor().currentIndex
view = self.currentEditor().folderView
self.updateFolderViewButtons(view)
else:
index = QModelIndex()
2016-02-07 00:34:22 +13:00
2015-06-30 00:21:57 +12:00
self._updating = True
self.mw.treeRedacOutline.setCurrentIndex(index)
self._updating = False
2016-02-07 00:34:22 +13:00
2015-06-30 00:33:30 +12:00
self.updateStats()
2015-07-02 09:15:35 +12:00
self.updateThingsVisible(index)
2016-02-07 00:34:22 +13:00
def closeAllTabs(self):
2016-04-11 03:29:27 +12:00
for ts in self.allTabSplitters():
while(ts.tab.count()):
ts.closeTab(0)
for ts in reversed(self.allTabSplitters()):
ts.closeSplit()
def allTabs(self, tabWidget=None):
"""Returns all the tabs from the given tabWidget. If tabWidget is None, from the current tabWidget."""
if tabWidget is None:
tabWidget = self.currentTabWidget()
return [tabWidget.widget(i) for i in range(tabWidget.count())]
def allAllTabs(self):
"""Returns a list of all tabs, from all tabWidgets."""
r = []
for ts in self.allTabSplitters():
r.extend(self.allTabs(ts.tab))
return r
def allTabSplitters(self):
r = []
ts = self.tabSplitter
while ts:
r.append(ts)
ts = ts.secondTab
return r
2016-02-07 00:34:22 +13:00
###############################################################################
# SELECTION AND UPDATES
###############################################################################
2015-06-28 00:06:35 +12:00
def selectionChanged(self):
2015-06-30 00:21:57 +12:00
if self._updating:
return
2016-02-07 00:34:22 +13:00
2015-06-28 00:06:35 +12:00
if len(self.mw.treeRedacOutline.selectionModel().
2016-02-07 00:34:22 +13:00
selection().indexes()) == 0:
idx = QModelIndex()
2015-06-28 00:06:35 +12:00
else:
idx = self.mw.treeRedacOutline.currentIndex()
2015-06-30 00:21:57 +12:00
self.setCurrentModelIndex(idx)
2016-02-07 00:34:22 +13:00
2015-07-02 09:15:35 +12:00
self.updateThingsVisible(idx)
2016-02-07 00:34:22 +13:00
2015-06-30 00:21:57 +12:00
def openIndexes(self, indexes, newTab=False):
for i in indexes:
self.setCurrentModelIndex(i, newTab)
2016-02-07 00:34:22 +13:00
2015-06-28 00:06:35 +12:00
def setCurrentModelIndex(self, index, newTab=False):
2016-02-07 00:34:22 +13:00
2015-06-28 00:06:35 +12:00
if not index.isValid():
2015-06-30 00:21:57 +12:00
title = self.tr("Root")
else:
title = index.internalPointer().title()
2016-02-07 00:34:22 +13:00
2015-06-30 00:21:57 +12:00
# Checking if tab is already openned
for w in self.allTabs():
if w.currentIndex == index:
2016-04-11 03:29:27 +12:00
self.currentTabWidget().setCurrentWidget(w)
2015-06-30 00:21:57 +12:00
return
2016-02-07 00:34:22 +13:00
2015-06-30 00:21:57 +12:00
if qApp.keyboardModifiers() & Qt.ControlModifier:
newTab = True
2016-02-07 00:34:22 +13:00
2016-04-11 03:29:27 +12:00
if newTab or not self.currentTabWidget().count():
2015-06-30 00:21:57 +12:00
editor = editorWidget(self)
editor.setCurrentModelIndex(index)
2016-04-11 03:29:27 +12:00
self.currentTabWidget().addTab(editor, title)
self.currentTabWidget().setCurrentIndex(self.currentTabWidget().count() - 1)
2015-06-30 00:21:57 +12:00
else:
self.currentEditor().setCurrentModelIndex(index)
2016-04-11 03:29:27 +12:00
self.currentTabWidget().setTabText(self.currentTabWidget().currentIndex(), title)
2016-02-07 00:34:22 +13:00
###############################################################################
# UI
###############################################################################
2015-07-02 09:15:35 +12:00
def updateThingsVisible(self, index):
if index.isValid():
visible = index.internalPointer().isFolder()
else:
visible = True
2016-02-07 00:34:22 +13:00
2015-07-02 09:15:35 +12:00
# Hides / show textFormat
self.textFormat.updateFromIndex(index)
2016-02-07 00:34:22 +13:00
2015-07-02 09:15:35 +12:00
self.btnRedacFolderText.setVisible(visible)
self.btnRedacFolderCork.setVisible(visible)
self.btnRedacFolderOutline.setVisible(visible)
2016-04-11 03:29:27 +12:00
self.sldCorkSizeFactor.setVisible(visible and self.btnRedacFolderCork.isChecked())
2015-07-02 09:15:35 +12:00
self.btnRedacFullscreen.setVisible(not visible)
2016-02-07 00:34:22 +13:00
2015-06-30 00:21:57 +12:00
def updateFolderViewButtons(self, view):
if view == "text":
self.btnRedacFolderText.setChecked(True)
elif view == "cork":
self.btnRedacFolderCork.setChecked(True)
elif view == "outline":
self.btnRedacFolderOutline.setChecked(True)
2016-02-07 00:34:22 +13:00
2015-06-30 00:33:30 +12:00
def updateStats(self):
2016-02-07 00:34:22 +13:00
2015-06-30 00:33:30 +12:00
if not self.currentEditor():
return
2016-02-07 00:34:22 +13:00
2015-06-30 00:33:30 +12:00
index = self.currentEditor().currentIndex
if index.isValid():
2015-06-28 00:06:35 +12:00
item = index.internalPointer()
else:
2015-06-30 00:21:57 +12:00
item = self.mw.mdlOutline.rootItem
2016-02-07 00:34:22 +13:00
2015-06-28 00:06:35 +12:00
if not item:
2015-06-30 00:21:57 +12:00
item = self.mw.mdlOutline.rootItem
2016-02-07 00:34:22 +13:00
2015-06-28 00:06:35 +12:00
wc = item.data(Outline.wordCount.value)
goal = item.data(Outline.goal.value)
progress = item.data(Outline.goalPercentage.value)
2016-02-07 00:34:22 +13:00
# mw = qApp.activeWindow()
2015-06-28 00:06:35 +12:00
if not wc:
wc = 0
if goal:
self.lblRedacProgress.show()
rect = self.lblRedacProgress.geometry()
rect = QRect(QPoint(0, 0), rect.size())
self.px = QPixmap(rect.size())
self.px.fill(Qt.transparent)
p = QPainter(self.px)
drawProgress(p, rect, progress, 2)
del p
self.lblRedacProgress.setPixmap(self.px)
2015-06-30 04:51:20 +12:00
self.lblRedacWC.setText(self.tr("{} words / {}").format(
2016-02-07 00:34:22 +13:00
locale.format("%d", wc, grouping=True),
locale.format("%d", goal, grouping=True)))
2015-06-28 00:06:35 +12:00
else:
self.lblRedacProgress.hide()
2015-06-30 04:51:20 +12:00
self.lblRedacWC.setText(self.tr("{} words").format(
2016-02-07 00:34:22 +13:00
locale.format("%d", wc, grouping=True)))
###############################################################################
# VIEWS
###############################################################################
2015-06-28 00:06:35 +12:00
def setFolderView(self, view):
if self.currentEditor():
self.currentEditor().setFolderView(view)
2016-02-07 00:34:22 +13:00
2015-06-28 00:06:35 +12:00
def setCorkSizeFactor(self, val):
2016-04-11 03:29:27 +12:00
for w in self.allAllTabs():
2015-06-30 00:33:30 +12:00
w.setCorkSizeFactor(val)
2015-06-30 09:41:13 +12:00
settings.corkSizeFactor = val
2016-02-07 00:34:22 +13:00
2015-06-28 00:06:35 +12:00
def updateCorkView(self):
2016-04-11 03:29:27 +12:00
for w in self.allAllTabs():
2015-06-30 00:33:30 +12:00
w.corkView.viewport().update()
2016-02-07 00:34:22 +13:00
2015-06-28 00:06:35 +12:00
def updateCorkBackground(self):
2016-04-11 03:29:27 +12:00
for w in self.allAllTabs():
2015-06-30 00:33:30 +12:00
w.corkView.updateBackground()
2016-02-07 00:34:22 +13:00
2015-06-28 00:06:35 +12:00
def updateTreeView(self):
2016-04-11 03:29:27 +12:00
for w in self.allAllTabs():
2016-02-05 05:48:35 +13:00
w.outlineView.viewport().update()
2016-02-07 00:34:22 +13:00
2015-06-28 00:06:35 +12:00
def showFullScreen(self):
2015-06-30 04:46:16 +12:00
if self.currentEditor():
self._fullScreen = fullScreenEditor(self.currentEditor().currentIndex)
2016-02-07 00:34:22 +13:00
###############################################################################
# DICT AND STUFF LIKE THAT
###############################################################################
2015-06-28 00:06:35 +12:00
def setDict(self, dict):
2015-06-30 00:21:57 +12:00
print(dict)
2016-04-11 03:29:27 +12:00
for w in self.allAllTabs():
2015-06-30 00:21:57 +12:00
w.setDict(dict)
2016-02-07 00:34:22 +13:00
2015-06-28 00:06:35 +12:00
def toggleSpellcheck(self, val):
2016-04-11 03:29:27 +12:00
for w in self.allAllTabs():
2015-06-30 00:21:57 +12:00
w.toggleSpellcheck(val)