manuskript/manuskript/ui/editors/mainEditor.py

251 lines
8.2 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()
self.tab.tabCloseRequested.connect(self.closeTab)
self.tab.currentChanged.connect(self.tabChanged)
2016-02-07 00:34:22 +13:00
2016-02-08 21:04:24 +13:00
# UI
try:
self.tab.setTabBarAutoHide(True)
except AttributeError:
print("Info: install Qt 5.4 or higher to use tabbar auto-hide in editor.")
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-02-07 00:34:22 +13: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-09 06:09:41 +12:00
self.tab.setDocumentMode(False)
self.tab.setStyleSheet(style.mainEditorTabSS())
2016-04-09 01:23:20 +12:00
2016-02-07 00:34:22 +13:00
###############################################################################
# TABS
###############################################################################
2015-06-28 00:06:35 +12:00
def currentEditor(self):
return self.tab.currentWidget()
2016-02-07 00:34:22 +13:00
2015-06-28 00:06:35 +12:00
def tabChanged(self, index):
2015-06-30 00:21:57 +12:00
if self.currentEditor():
index = self.currentEditor().currentIndex
view = self.currentEditor().folderView
self.updateFolderViewButtons(view)
2015-06-30 04:46:16 +12:00
if index.isValid():
hidden = not index.internalPointer().isFolder()
else:
hidden = False
2015-06-30 00:21:57 +12:00
else:
index = QModelIndex()
2015-06-30 04:46:16 +12:00
hidden = False
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
2015-06-28 00:06:35 +12:00
def closeTab(self, index):
2015-07-03 23:59:41 +12:00
w = self.tab.widget(index)
2015-06-28 00:06:35 +12:00
self.tab.removeTab(index)
w.setCurrentModelIndex(QModelIndex())
2015-07-03 23:59:41 +12:00
w.deleteLater()
2016-02-07 00:34:22 +13:00
def closeAllTabs(self):
while(self.tab.count()):
self.closeTab(0)
2015-06-30 00:21:57 +12:00
def allTabs(self):
return [self.tab.widget(i) for i in range(self.tab.count())]
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:
self.tab.setCurrentWidget(w)
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
2015-06-30 00:21:57 +12:00
if newTab or not self.tab.count():
editor = editorWidget(self)
editor.setCurrentModelIndex(index)
self.tab.addTab(editor, title)
self.tab.setCurrentIndex(self.tab.count() - 1)
else:
self.currentEditor().setCurrentModelIndex(index)
self.tab.setTabText(self.tab.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)
self.sldCorkSizeFactor.setVisible(visible)
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):
2015-06-30 00:33:30 +12:00
for w in self.allTabs():
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):
2015-06-30 00:33:30 +12:00
for w in self.allTabs():
w.corkView.viewport().update()
2016-02-07 00:34:22 +13:00
2015-06-28 00:06:35 +12:00
def updateCorkBackground(self):
2015-06-30 00:33:30 +12:00
for w in self.allTabs():
w.corkView.updateBackground()
2016-02-07 00:34:22 +13:00
2015-06-28 00:06:35 +12:00
def updateTreeView(self):
2015-06-30 00:33:30 +12:00
for w in self.allTabs():
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)
for w in self.allTabs():
w.setDict(dict)
2016-02-07 00:34:22 +13:00
2015-06-28 00:06:35 +12:00
def toggleSpellcheck(self, val):
2015-06-30 00:21:57 +12:00
for w in self.allTabs():
w.toggleSpellcheck(val)