Commit 9ce57149 authored by Michael Büsch's avatar Michael Büsch

gui: Add editor auto-indent

Signed-off-by: Michael Büsch's avatarMichael Buesch <m@bues.ch>
parent 745066e4
......@@ -10,4 +10,3 @@ S7 compatibility:
GUI:
- Make font size configurable (and save it in project file)
- Save window status (open MDI windows, positions, etc) in project file
- Auto-indent in editor
......@@ -24,6 +24,7 @@ from awlsim.common.compat import *
from awlsim.gui.util import *
from awlsim.gui.cpuwidget import *
from awlsim.gui.sourcecodeedit import *
class EditSubWidget(QWidget):
......@@ -333,15 +334,14 @@ class CpuStatsContextMenu(QMenu):
def __actionToggled(self, newState):
self.closed.emit()
class EditWidget(QPlainTextEdit):
class EditWidget(SourceCodeEdit):
codeChanged = Signal()
visibleRangeChanged = Signal()
__aniChars = ( ' ', '.', 'o', '0', 'O', '0', 'o', '.' )
def __init__(self, mainWidget):
QPlainTextEdit.__init__(self, mainWidget)
self.mainWidget = mainWidget
def __init__(self, parent=None):
SourceCodeEdit.__init__(self, parent)
self.__aniTimer = QTimer(self)
self.__aniTimer.setSingleShot(False)
......@@ -351,7 +351,7 @@ class EditWidget(QPlainTextEdit):
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.setLineWrapMode(QPlainTextEdit.NoWrap)
self.setLineWrapMode(SourceCodeEdit.NoWrap)
self.setTabStopWidth(self.tabStopWidth() // 2)
self.headerWidget = HeaderSubWidget(self)
......@@ -579,7 +579,7 @@ class EditWidget(QPlainTextEdit):
0)
def sizeHint(self):
sh = QPlainTextEdit.sizeHint(self)
sh = SourceCodeEdit.sizeHint(self)
sh.setWidth(650 +\
self.lineNumWidgetWidth() +\
self.cpuStatsWidgetWidth())
......@@ -639,7 +639,7 @@ class EditWidget(QPlainTextEdit):
self.__updateMargins()
def resizeEvent(self, ev):
QPlainTextEdit.resizeEvent(self, ev)
SourceCodeEdit.resizeEvent(self, ev)
self.__updateGeo()
__runStateToText = {
......
# -*- coding: utf-8 -*-
#
# AWL simulator - Generic source code edit widget
#
# Copyright 2014 Michael Buesch <m@bues.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
from __future__ import division, absolute_import, print_function, unicode_literals
from awlsim.common.compat import *
from awlsim.gui.util import *
class SourceCodeEdit(QPlainTextEdit):
def __init__(self, parent=None):
QPlainTextEdit.__init__(self, parent)
self.enableAutoIndent()
def enableAutoIndent(self, enable=True):
self.__autoIndentEn = enable
def __autoIndentHandleNewline(self):
if not self.__autoIndentEn:
return
# Move cursor to last line and get its text.
cursor = self.textCursor()
if not cursor.movePosition(QTextCursor.Up,
QTextCursor.MoveAnchor, 1):
return
cursor.select(QTextCursor.LineUnderCursor)
prevLine = cursor.selectedText()
if not prevLine:
return
# Get the indent-string (that is the whitespace line prefix)
for i, c in enumerate(prevLine):
if not c.isspace():
break
else:
i += 1
indentStr = prevLine[:i]
# Insert the indent string into the current line
cursor = self.textCursor()
cursor.insertText(indentStr)
cursor.movePosition(QTextCursor.EndOfLine,
QTextCursor.MoveAnchor, 1)
self.setTextCursor(cursor)
def keyPressEvent(self, ev):
QPlainTextEdit.keyPressEvent(self, ev)
if ev.key() in (Qt.Key_Return, Qt.Key_Enter):
self.__autoIndentHandleNewline()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment