manuskript/manuskript/ui/editors/mainEditor.py

233 lines
7.7 KiB
Python
Raw Normal View History

2015-06-28 00:06:35 +12:00
#!/usr/bin/env python
#--!-- coding: utf8 --!--
from qt import *
from enums import *
from ui.editors.mainEditor_ui import *
from ui.editors.editorWidget import *
from functions import *
2015-06-30 04:51:20 +12:00
import locale
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
2015-06-28 00:06:35 +12:00
self.mw = mainWindow()
self.tab.tabCloseRequested.connect(self.closeTab)
self.tab.currentChanged.connect(self.tabChanged)
2015-06-28 23:03:57 +12:00
# Connections --------------------------------------------------------
2015-06-28 00:06:35 +12:00
self.sldCorkSizeFactor.valueChanged.connect(
self.setCorkSizeFactor, AUC)
self.btnRedacFolderCork.toggled.connect(
self.sldCorkSizeFactor.setVisible, AUC)
self.btnRedacFolderText.clicked.connect(
lambda v: self.setFolderView("text"), AUC)
self.btnRedacFolderCork.clicked.connect(
lambda v: self.setFolderView("cork"), AUC)
self.btnRedacFolderOutline.clicked.connect(
lambda v: self.setFolderView("outline"), AUC)
self.btnRedacFullscreen.clicked.connect(
self.showFullScreen, AUC)
###############################################################################
# TABS
###############################################################################
def currentEditor(self):
return self.tab.currentWidget()
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
2015-06-30 00:21:57 +12:00
self._updating = True
self.mw.treeRedacOutline.setCurrentIndex(index)
self._updating = False
2015-06-30 00:33:30 +12:00
self.updateStats()
2015-07-02 09:15:35 +12:00
self.updateThingsVisible(index)
2015-06-28 00:06:35 +12:00
def closeTab(self, index):
#FIXME: submit data if textedit?
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)
2015-07-03 23:59:41 +12:00
w.deleteLater()
2015-06-30 00:21:57 +12:00
def allTabs(self):
return [self.tab.widget(i) for i in range(self.tab.count())]
2015-06-28 00:06:35 +12:00
###############################################################################
# SELECTION AND UPDATES
###############################################################################
def selectionChanged(self):
2015-06-30 00:21:57 +12:00
if self._updating:
return
2015-06-28 00:06:35 +12:00
if len(self.mw.treeRedacOutline.selectionModel().
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)
2015-07-02 09:15:35 +12:00
self.updateThingsVisible(idx)
2015-06-28 00:06:35 +12:00
2015-06-30 00:21:57 +12:00
def openIndexes(self, indexes, newTab=False):
for i in indexes:
self.setCurrentModelIndex(i, newTab)
2015-06-28 00:06:35 +12:00
def setCurrentModelIndex(self, index, newTab=False):
if not index.isValid():
2015-06-30 00:21:57 +12:00
title = self.tr("Root")
else:
title = index.internalPointer().title()
# Checking if tab is already openned
for w in self.allTabs():
if w.currentIndex == index:
self.tab.setCurrentWidget(w)
return
if qApp.keyboardModifiers() & Qt.ControlModifier:
newTab = True
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)
2015-06-28 00:06:35 +12:00
###############################################################################
# UI
###############################################################################
2015-07-02 09:15:35 +12:00
def updateThingsVisible(self, index):
if index.isValid():
visible = index.internalPointer().isFolder()
else:
visible = True
# Hides / show textFormat
self.textFormat.updateFromIndex(index)
self.btnRedacFolderText.setVisible(visible)
self.btnRedacFolderCork.setVisible(visible)
self.btnRedacFolderOutline.setVisible(visible)
self.sldCorkSizeFactor.setVisible(visible)
self.btnRedacFullscreen.setVisible(not visible)
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)
2015-06-28 00:06:35 +12:00
2015-06-30 00:33:30 +12:00
def updateStats(self):
if not self.currentEditor():
return
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
2015-06-28 00:06:35 +12:00
if not item:
2015-06-30 00:21:57 +12:00
item = self.mw.mdlOutline.rootItem
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)
#mw = qApp.activeWindow()
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(
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(
locale.format("%d", wc, grouping=True)))
2015-06-28 00:06:35 +12:00
###############################################################################
# VIEWS
###############################################################################
def setFolderView(self, view):
if self.currentEditor():
self.currentEditor().setFolderView(view)
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
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()
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()
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()
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)
2015-06-28 00:06:35 +12:00
###############################################################################
# DICT AND STUFF LIKE THAT
###############################################################################
def setDict(self, dict):
2015-06-30 00:21:57 +12:00
print(dict)
for w in self.allTabs():
w.setDict(dict)
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)
2015-06-28 00:06:35 +12:00