Skip to content

Commit

Permalink
Add option to set default preview state (#435)
Browse files Browse the repository at this point in the history
Simultaneously remove obsolete option for default live-preview.

New tabs get additional triggerPreviewUpdate.
  • Loading branch information
red-kite authored and mitya57 committed Jan 26, 2019
1 parent 74767f6 commit 62f632c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion ReText/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def getBundledIcon(iconName):
'autoSave': False,
'defaultCodec': '',
'defaultMarkup': markups.MarkdownMarkup.name,
'defaultPreviewState': 'editor',
'detectEncoding': True,
'documentStatsEnabled': False,
'editorFont': QFont(),
Expand All @@ -77,7 +78,6 @@ def getBundledIcon(iconName):
'iconTheme': '',
'lastTabIndex': 0,
'lineNumbersEnabled': False,
'livePreviewByDefault': False,
'markdownDefaultFileExtension': '.mkd',
'openLastFilesOnStartup': False,
'paperSize': '',
Expand Down
15 changes: 12 additions & 3 deletions ReText/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from PyQt5.QtGui import QDesktopServices, QIcon
from PyQt5.QtWidgets import QCheckBox, QDialog, QDialogButtonBox, \
QFileDialog, QGridLayout, QLabel, QLineEdit, QPushButton, QSpinBox, \
QTabWidget, QVBoxLayout, QWidget
QComboBox, QTabWidget, QVBoxLayout, QWidget

MKD_EXTS_FILE = join(CONFIGURATION_DIR, 'markdown-extensions.txt')

Expand Down Expand Up @@ -88,7 +88,7 @@ def initConfigOptions(self):
(self.tr('Automatically open last documents on startup'), 'openLastFilesOnStartup'),
(self.tr('Number of recent documents'), 'recentDocumentsCount'),
(self.tr('Restore window geometry'), 'saveWindowGeometry'),
(self.tr('Use live preview by default'), 'livePreviewByDefault'),
(self.tr('Default preview state'), 'defaultPreviewState'),
(self.tr('Open external links in ReText window'), 'handleWebLinks'),
(self.tr('Markdown syntax extensions (comma-separated)'), 'markdownExtensions'),
(None, 'markdownExtensions'),
Expand Down Expand Up @@ -152,7 +152,14 @@ def getPageWidget(self, options):
layout.addWidget(self.configurators[name], index, 0, 1, 2)
continue
value = getattr(globalSettings, name)
if isinstance(value, bool):
if name == 'defaultPreviewState':
self.configurators[name] = QComboBox(self)
self.configurators[name].addItem(self.tr('Editor'), 'editor')
self.configurators[name].addItem(self.tr('Live preview'), 'live-preview')
self.configurators[name].addItem(self.tr('Normal preview'), 'normal-preview')
comboBoxIndex = self.configurators[name].findData(value)
self.configurators[name].setCurrentIndex(comboBoxIndex)
elif isinstance(value, bool):
self.configurators[name] = QCheckBox(self)
self.configurators[name].setChecked(value)
label.clicked.connect(self.configurators[name].nextCheckState)
Expand Down Expand Up @@ -192,6 +199,8 @@ def saveSettings(self):
value = configurator.value()
elif isinstance(configurator, QLineEdit):
value = configurator.text()
elif isinstance(configurator, QComboBox):
value = configurator.currentData()
elif isinstance(configurator, FileSelectButton):
value = configurator.fileName
setattr(globalSettings, name, value)
Expand Down
15 changes: 12 additions & 3 deletions ReText/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from ReText import (getBundledIcon, app_version, globalSettings,
readListFromSettings, writeListToSettings, datadirs)
from ReText.tab import (ReTextTab, ReTextWebKitPreview, ReTextWebEnginePreview,
PreviewNormal, PreviewLive)
PreviewDisabled, PreviewNormal, PreviewLive)
from ReText.dialogs import HtmlDialog, LocaleDialog
from ReText.config import ConfigDialog
from ReText.icontheme import get_icon_theme
Expand Down Expand Up @@ -509,13 +509,22 @@ def tabModificationStateChanged(self, tab):
self.setWindowModified(changed)

def createTab(self, fileName):
self.currentTab = ReTextTab(self, fileName,
previewState=int(globalSettings.livePreviewByDefault))
previewStatesByName = {
'editor': PreviewDisabled,
'normal-preview': PreviewNormal,
'live-preview': PreviewLive,
}
previewState = previewStatesByName.get(globalSettings.defaultPreviewState, PreviewDisabled)
if previewState == PreviewNormal and not fileName:
previewState = PreviewDisabled # Opening empty document in preview mode makes no sense
self.currentTab = ReTextTab(self, fileName, previewState)
self.currentTab.fileNameChanged.connect(lambda: self.tabFileNameChanged(self.currentTab))
self.currentTab.modificationStateChanged.connect(lambda: self.tabModificationStateChanged(self.currentTab))
self.currentTab.activeMarkupChanged.connect(lambda: self.tabActiveMarkupChanged(self.currentTab))
self.tabWidget.addTab(self.currentTab, self.tr("New document"))
self.currentTab.updateBoxesVisibility()
if previewState > 0:
QTimer.singleShot(500, self.currentTab.triggerPreviewUpdate)

def closeTab(self, ind):
if self.maybeSave(ind):
Expand Down
2 changes: 1 addition & 1 deletion configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ option name | type | description
`autoSave` | boolean | whether to automatically save documents (default: false)
`defaultCodec` | string | name of encoding to use by default (default: use system encoding)
`defaultMarkup` | string | name of markup to use for unknown files
`defaultPreviewState` | string | mode for new tabs: `editor`, `normal-preview` or `live-preview` (default: `editor`)
`detectEncoding` | boolean | whether to automatically detect files encoding; needs chardet package (default: true)
`documentStatsEnabled` | boolean | whether to show document stats (word count, character count) (default: false)
`editorFont` | string | font to use for editor: name (default: `monospace`)
Expand All @@ -23,7 +24,6 @@ option name | type | description
`highlightCurrentLine` | boolean | whether to highlight current line in editor (default: false)
`iconTheme` | string | name of the system icon theme to use (see below)
`lineNumbersEnabled` | boolean | whether to show column with line numbers in editor (default: false)
`livePreviewByDefault` | boolean | whether new tabs and windows should open in live preview mode (default: false)
`markdownDefaultFileExtension` | string | default file extension for Markdown files (default: `.mkd`)
`openLastFilesOnStartup` | boolean | whether to automatically open last documents on startup (default: false)
`paperSize` | string | name of default page size to use for print and export (e.g. A4, Letter)
Expand Down

0 comments on commit 62f632c

Please sign in to comment.