Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement softwrapping at user specified margin line #313

Merged
merged 4 commits into from
Sep 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ReText/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def getBundledIcon(iconName):
'relativeLineNumbers': False,
'restDefaultFileExtension': '.rst',
'rightMargin': 0,
'rightMarginWrap': False,
'saveWindowGeometry': False,
'spellCheck': False,
'spellCheckLocale': '',
Expand Down
13 changes: 13 additions & 0 deletions ReText/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def __init__(self, parent):
buttonBox.accepted.connect(self.saveSettings)
buttonBox.rejected.connect(self.close)
self.initWidgets()
self.configurators['rightMargin'].valueChanged.connect(self.handleRightMarginSet)
self.layout.addWidget(buttonBox, len(self.options), 0, 1, 2)

def initConfigOptions(self):
Expand All @@ -83,6 +84,7 @@ def initConfigOptions(self):
(self.tr('Tab key inserts spaces'), 'tabInsertsSpaces'),
(self.tr('Tabulation width'), 'tabWidth'),
(self.tr('Draw vertical line at column'), 'rightMargin'),
(self.tr('Enable soft wrap'), 'rightMarginWrap'),
(self.tr('Show document stats'), 'documentStatsEnabled'),
(self.tr('Interface'), None),
(self.tr('Icon theme name'), 'iconTheme'),
Expand Down Expand Up @@ -122,6 +124,8 @@ def initWidgets(self):
if isinstance(value, bool):
self.configurators[name] = QCheckBox(self)
self.configurators[name].setChecked(value)
if name == 'rightMarginWrap' and (globalSettings.rightMargin == 0):
self.configurators[name].setEnabled(False)
elif isinstance(value, int):
self.configurators[name] = QSpinBox(self)
if name == 'tabWidth':
Expand All @@ -137,6 +141,14 @@ def initWidgets(self):
self.layout.addWidget(label, index, 0)
self.layout.addWidget(self.configurators[name], index, 1, Qt.AlignRight)

def handleRightMarginSet(self, value):
if value > 0:
self.configurators['rightMarginWrap'].setEnabled(True)
self.configurators['rightMarginWrap'].setChecked(globalSettings.rightMarginWrap)
else:
self.configurators['rightMarginWrap'].setChecked(False)
self.configurators['rightMarginWrap'].setEnabled(False)

def saveSettings(self):
for option in self.options:
name = option[1]
Expand Down Expand Up @@ -170,5 +182,6 @@ def applySettings(self):
print(e, file=sys.stderr)
for tab in self.parent.iterateTabs():
tab.editBox.updateFont()
tab.editBox.setWrapModeAndWidth()
tab.editBox.viewport().update()
self.parent.updateStyleSheet()
10 changes: 9 additions & 1 deletion ReText/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,20 @@ def __init__(self, parent):
self.statistics = (0, 0, 0)
self.statsArea = TextInfoArea(self)
self.updateFont()
self.setWrapModeAndWidth()
self.document().blockCountChanged.connect(self.updateLineNumberAreaWidth)
self.cursorPositionChanged.connect(self.highlightCurrentLine)
self.document().contentsChange.connect(self.contentsChange)
if globalSettings.useFakeVim:
self.installFakeVimHandler()

def setWrapModeAndWidth(self):
if globalSettings.rightMarginWrap and (self.rect().topRight().x() > self.marginx):
self.setLineWrapMode(QTextEdit.FixedPixelWidth)
self.setLineWrapColumnOrWidth(self.marginx)
else:
self.setLineWrapMode(QTextEdit.WidgetWidth)

def updateFont(self):
self.setFont(globalSettings.editorFont)
metrics = self.fontMetrics()
Expand Down Expand Up @@ -250,6 +258,7 @@ def resizeEvent(self, event):
self.lineNumberAreaWidth(), rect.height())
self.infoArea.updateTextAndGeometry()
self.statsArea.updateTextAndGeometry()
self.setWrapModeAndWidth()

def highlightCurrentLine(self):
if globalSettings.relativeLineNumbers:
Expand Down Expand Up @@ -495,4 +504,3 @@ def getText(self):
'count of words, alphanumeric characters, all characters')
words, alphaNums, characters = self.editor.statistics
return template % (words, alphaNums, characters)