manuskript/src/ui/editors/fullScreenEditor.py

262 lines
9.8 KiB
Python
Raw Normal View History

2015-06-20 04:47:45 +12:00
#!/usr/bin/env python
#--!-- coding: utf8 --!--
from qt import *
from enums import *
from ui.views.textEditView import *
from ui.editors.themes import *
from functions import *
import settings
class fullScreenEditor(QWidget):
2015-06-21 10:36:25 +12:00
def __init__(self, index, parent=None):
2015-06-20 04:47:45 +12:00
QWidget.__init__(self, parent)
self._background = None
2015-06-21 10:36:25 +12:00
self._index = index
2015-06-20 04:47:45 +12:00
self._theme = findThemePath(settings.fullScreenTheme)
self._themeDatas = loadThemeDatas(self._theme)
self.setMouseTracking(True)
# Text editor
2015-06-21 10:36:25 +12:00
self.editor = textEditView(self, index=index, spellcheck=settings.spellcheck, dict=settings.dict)
2015-06-20 04:47:45 +12:00
self.editor.setFrameStyle(QFrame.NoFrame)
2015-06-21 10:36:25 +12:00
#f = QFile(appPath("resources/themes/preview.txt"))
#f.open(QIODevice.ReadOnly)
#self.editor.setPlainText(QTextStream(f).readAll()*5)
2015-06-20 04:47:45 +12:00
self.editor.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.editor.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.editor.installEventFilter(self)
self.editor.setMouseTracking(True)
2015-06-21 07:35:57 +12:00
self.editor.setVerticalScrollBar(myScrollBar())
2015-06-20 04:47:45 +12:00
self.scrollBar = self.editor.verticalScrollBar()
self.scrollBar.setParent(self)
2015-06-21 10:36:25 +12:00
# Top Panel
2015-06-21 07:35:57 +12:00
self.topPanel = myPanel(parent=self)
2015-06-21 10:36:25 +12:00
self.topPanel.layout().addStretch(1)
self.btnSpellCheck = QPushButton()
self.btnSpellCheck.setFlat(True)
self.btnSpellCheck.setIcon(QIcon.fromTheme("tools-check-spelling"))
self.btnSpellCheck.setCheckable(True)
self.btnSpellCheck.setChecked(self.editor.spellcheck)
self.btnSpellCheck.toggled.connect(self.editor.toggleSpellcheck)
self.topPanel.layout().addWidget(self.btnSpellCheck)
self.topPanel.layout().addStretch(1)
2015-06-21 07:35:57 +12:00
b = QPushButton(self)
b.setIcon(qApp.style().standardIcon(QStyle.SP_DialogCloseButton))
b.clicked.connect(self.close)
b.setFlat(True)
2015-06-21 10:36:25 +12:00
self.topPanel.layout().addWidget(b)
2015-06-21 07:35:57 +12:00
2015-06-21 10:36:25 +12:00
# Bottom Panel
self.bottomPanel = myPanel(parent=self)
self.bottomPanel.layout().addSpacing(24)
2015-06-21 07:35:57 +12:00
self.lstThemes = QComboBox(self)
self.lstThemes.setAttribute(Qt.WA_TranslucentBackground)
paths = allPaths("resources/themes")
for p in paths:
lst = [i for i in os.listdir(p) if os.path.splitext(i)[1] == ".theme"]
for t in lst:
themeIni = os.path.join(p, t)
self.lstThemes.addItem(os.path.splitext(t)[0])
self.lstThemes.currentTextChanged.connect(self.setTheme)
2015-06-21 10:36:25 +12:00
self.lstThemes.setMaximumSize(QSize(300, QFontMetrics(qApp.font()).height()))
self.bottomPanel.layout().addWidget(QLabel(self.tr("Theme:"), self))
self.bottomPanel.layout().addWidget(self.lstThemes)
self.bottomPanel.layout().addStretch(1)
self.lblProgress = QLabel(self)
self.lblProgress.setMaximumSize(QSize(200, 14))
self.lblProgress.setMinimumSize(QSize(100, 14))
self.lblWC = QLabel(self)
self.bottomPanel.layout().addWidget(self.lblWC)
self.bottomPanel.layout().addWidget(self.lblProgress)
self.updateStatusBar()
self.bottomPanel.layout().addSpacing(24)
2015-06-20 04:47:45 +12:00
2015-06-23 20:50:19 +12:00
# Connection
self._index.model().dataChanged.connect(self.dataChanged)
2015-06-20 04:47:45 +12:00
#self.updateTheme()
self.showFullScreen()
#self.showMaximized()
#self.show()
def setTheme(self, themeName):
2015-06-21 10:36:25 +12:00
settings.fullScreenTheme = themeName
2015-06-21 07:35:57 +12:00
self._theme = findThemePath(themeName)
2015-06-20 04:47:45 +12:00
self._themeDatas = loadThemeDatas(self._theme)
self.updateTheme()
def updateTheme(self):
rect = self.geometry()
self._background = generateTheme(self._themeDatas, rect)
setThemeEditorDatas(self.editor, self._themeDatas, self._background, rect)
2015-06-21 07:35:57 +12:00
# Colors
if self._themeDatas["Foreground/Color"] == self._themeDatas["Background/Color"] or \
self._themeDatas["Foreground/Opacity"] < 5:
self._bgcolor = QColor(self._themeDatas["Text/Color"])
self._fgcolor = QColor(self._themeDatas["Background/Color"])
else:
self._bgcolor = QColor(self._themeDatas["Foreground/Color"])
self._bgcolor.setAlpha(self._themeDatas["Foreground/Opacity"] * 255 / 100)
self._fgcolor = QColor(self._themeDatas["Text/Color"])
2015-06-21 10:36:25 +12:00
if self._themeDatas["Text/Color"] == self._themeDatas["Foreground/Color"]:
self._fgcolor = QColor(self._themeDatas["Background/Color"])
2015-06-21 07:35:57 +12:00
# ScrollBar
2015-06-20 04:47:45 +12:00
r = self.editor.geometry()
w = qApp.style().pixelMetric(QStyle.PM_ScrollBarExtent)
r.setWidth(w)
2015-06-21 10:36:25 +12:00
r.moveRight(rect.right() - rect.left())
2015-06-20 04:47:45 +12:00
self.scrollBar.setGeometry(r)
self.scrollBar.setVisible(False)
2015-06-21 02:45:54 +12:00
p = self.scrollBar.palette()
b = QBrush(self._background.copy(self.scrollBar.geometry()))
p.setBrush(QPalette.Base, b)
self.scrollBar.setPalette(p)
2015-06-20 04:47:45 +12:00
2015-06-21 07:35:57 +12:00
self.scrollBar.setColor(self._bgcolor)
# Panels
r = QRect(0, 0, 0, 24)
r.setWidth(rect.width())
2015-06-21 10:36:25 +12:00
#r.moveLeft(rect.center().x() - r.width() / 2)
2015-06-21 07:35:57 +12:00
self.topPanel.setGeometry(r)
self.topPanel.setVisible(False)
2015-06-21 10:36:25 +12:00
r.moveBottom(rect.bottom() - rect.top())
2015-06-21 07:35:57 +12:00
self.bottomPanel.setGeometry(r)
self.bottomPanel.setVisible(False)
self.topPanel.setColor(self._bgcolor)
self.bottomPanel.setColor(self._bgcolor)
# Lst theme
2015-06-21 10:36:25 +12:00
#p = self.lstThemes.palette()
p = self.palette()
2015-06-21 07:35:57 +12:00
p.setBrush(QPalette.Button, self._bgcolor)
p.setBrush(QPalette.ButtonText, self._fgcolor)
p.setBrush(QPalette.WindowText, self._fgcolor)
2015-06-21 10:36:25 +12:00
for panel in (self.bottomPanel, self.topPanel):
for i in range(panel.layout().count()):
item = panel.layout().itemAt(i)
if item.widget():
item.widget().setPalette(p)
#self.lstThemes.setPalette(p)
#self.lblWC.setPalette(p)
2015-06-21 07:35:57 +12:00
self.update()
2015-06-20 04:47:45 +12:00
def paintEvent(self, event):
if self._background:
painter = QPainter(self)
2015-06-21 02:45:54 +12:00
painter.setClipRegion(event.region())
2015-06-20 04:47:45 +12:00
painter.drawPixmap(event.rect(), self._background, event.rect())
painter.end()
def resizeEvent(self, event):
self.updateTheme()
def keyPressEvent(self, event):
if event.key() in [Qt.Key_Escape, Qt.Key_F11]:
self.close()
else:
QWidget.keyPressEvent(self, event)
def mouseMoveEvent(self, event):
r = self.geometry()
2015-06-21 07:35:57 +12:00
for w in [self.scrollBar, self.topPanel, self.bottomPanel]:
2015-06-20 04:47:45 +12:00
w.setVisible(w.geometry().contains(event.pos()))
def eventFilter(self, obj, event):
2015-06-21 10:36:25 +12:00
if obj == self.editor and event.type() == QEvent.Enter:
for w in [self.scrollBar, self.topPanel, self.bottomPanel]:
w.setVisible(False)
2015-06-20 04:47:45 +12:00
return QWidget.eventFilter(self, obj, event)
2015-06-21 10:36:25 +12:00
def dataChanged(self, topLeft, bottomRight):
if not self._index:
return
if topLeft.row() <= self._index.row() <= bottomRight.row():
self.updateStatusBar()
def updateStatusBar(self):
if self._index:
item = self._index.internalPointer()
wc = item.data(Outline.wordCount.value)
goal = item.data(Outline.goal.value)
pg = item.data(Outline.goalPercentage.value)
if goal:
rect = self.lblProgress.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, pg, 2)
p.end()
self.lblProgress.setPixmap(self.px)
self.lblWC.setText(self.tr("{} words / {}").format(wc, goal))
else:
2015-06-23 20:50:19 +12:00
self.lblProgress.hide()
self.lblWC.setText(self.tr("{} words").format(wc))
2015-06-20 04:47:45 +12:00
class myScrollBar(QScrollBar):
def __init__(self, color=Qt.white, parent=None):
QScrollBar.__init__(self, parent)
self._color = color
2015-06-21 02:45:54 +12:00
#self.setAttribute(Qt.WA_TranslucentBackground)
self.timer = QTimer()
2015-06-21 10:36:25 +12:00
self.timer.setInterval(500)
2015-06-21 02:45:54 +12:00
self.timer.setSingleShot(True)
self.timer.timeout.connect(self.hide)
self.valueChanged.connect(lambda v: self.timer.start())
self.valueChanged.connect(self.show)
2015-06-21 07:35:57 +12:00
def setColor(self, color):
self._color = color
2015-06-20 04:47:45 +12:00
def paintEvent(self, event):
opt = QStyleOptionSlider()
self.initStyleOption(opt)
style = qApp.style()
painter = QPainter(self)
2015-06-21 02:45:54 +12:00
# Background (Necessary with Qt 5.2 it seems, not with 5.4)
painter.save()
painter.setPen(Qt.NoPen)
painter.setBrush(self.palette().brush(QPalette.Base))
painter.drawRect(event.rect())
painter.restore()
2015-06-20 04:47:45 +12:00
#slider
r = style.subControlRect(style.CC_ScrollBar, opt, style.SC_ScrollBarSlider)
painter.fillRect(r, self._color)
painter.end()
class myPanel(QWidget):
def __init__(self, color=Qt.white, parent=None):
QWidget.__init__(self, parent)
self._color = color
self.show()
self.setAttribute(Qt.WA_TranslucentBackground)
2015-06-21 07:35:57 +12:00
self.setLayout(QHBoxLayout())
self.layout().setContentsMargins(0, 0, 0, 0)
def setColor(self, color):
self._color = color
2015-06-20 04:47:45 +12:00
def paintEvent(self, event):
r = event.rect()
painter = QPainter(self)
painter.fillRect(r, self._color)