Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[pyqgis-console] cleaning up code and minor fixes
  • Loading branch information
slarosa committed Apr 16, 2013
1 parent b67d360 commit 07abb34
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 84 deletions.
4 changes: 2 additions & 2 deletions python/console/console.py
Expand Up @@ -352,7 +352,7 @@ def __init__(self, parent=None):
self.toolBarEditor.addAction(self.uncommentEditorButton)
self.toolBarEditor.addSeparator()
self.toolBarEditor.addAction(self.runScriptEditorButton)

## Menu Import Class
self.classMenu = QMenu(self)
self.classMenu.addAction(self.loadSextanteButton)
Expand Down Expand Up @@ -523,7 +523,7 @@ def openScriptFile(self):
else:
tabName = filename.split('/')[-1]
self.tabEditorWidget.newTabEditor(tabName, filename)

lastDirPath = QFileInfo(filename).path()
settings.setValue("pythonConsole/lastDirPath", QVariant(filename))
self.tabListScript.append(filename)
Expand Down
86 changes: 34 additions & 52 deletions python/console/console_editor.py
Expand Up @@ -168,16 +168,14 @@ def __init__(self, parent=None):

def autoComplete(self):
self.autoCompleteFromAll()

#self.modificationChanged.connect(self.textEdited)

def on_margin_clicked(self, nmargin, nline, modifiers):
# Toggle marker for the line the margin was clicked on
if self.markersAtLine(nline) != 0:
self.markerDelete(nline, self.ARROW_MARKER_NUM)
else:
self.markerAdd(nline, self.ARROW_MARKER_NUM)

def refreshLexerProperties(self):
self.setLexers()

Expand Down Expand Up @@ -217,19 +215,19 @@ def setLexers(self):
self.lexer.setAPIs(self.api)

self.setLexer(self.lexer)

def move_cursor_to_end(self):
"""Move cursor to end of text"""
line, index = self.get_end_pos()
self.setCursorPosition(line, index)
self.ensureCursorVisible()
self.ensureLineVisible(line)

def get_end_pos(self):
"""Return (line, index) position of the last character"""
line = self.lines() - 1
return (line, self.text(line).length())

def contextMenuEvent(self, e):
menu = QMenu(self)
iconRun = QgsApplication.getThemeIcon("console/iconRunConsole.png")
Expand Down Expand Up @@ -302,7 +300,7 @@ def contextMenuEvent(self, e):
if QApplication.clipboard().text() != "":
pasteAction.setEnabled(True)
action = menu.exec_(self.mapToGlobal(e.pos()))

def codepad(self):
import urllib2, urllib
listText = self.selectedText().split('\n')
Expand Down Expand Up @@ -336,11 +334,12 @@ def codepad(self):
except urllib2.URLError, e:
msgText = QCoreApplication.translate('PythonConsole', 'Connection error: ')
self.parent.pc.callWidgetMessageBarEditor(msgText + str(e.args))

def hideEditor(self):
Ed = self.parent.pc.widgetEditor
Ed.hide()

self.parent.pc.showEditorButton.setChecked(False)

def commentEditorCode(self, commentCheck):
if self.hasSelectedText():
startLine, _, endLine, _ = self.getSelection()
Expand All @@ -357,7 +356,6 @@ def commentEditorCode(self, commentCheck):
else:
self.insert(selCmd)
self.setCursorPosition(endLine, selCmd.length() - 2)

else:
line, pos = self.getCursorPosition()
selCmd = self.text(line)
Expand Down Expand Up @@ -423,36 +421,36 @@ def runScriptCode(self):
#execfile(unicode(filename))
except IOError, error:
print 'Cannot execute file %s. Error: %s' % (filename, error.strerror)

def runSelectedCode(self):
cmd = self.selectedText()
self.parent.pc.shell.insertFromDropPaste(cmd)
self.parent.pc.shell.entered()
self.setFocus()

def getTextFromEditor(self):
text = self.text()
textList = text.split("\n")
return textList

class EditorTab(QWidget):
def __init__(self, parent, parentConsole, filename, *args):
QWidget.__init__(self, parent=None, *args)
self.mw = parent
self.pc = parentConsole
self.path = None

self.fileExcuteList = {}
self.fileExcuteList = dict()

self.newEditor = Editor(self)
self.newEditor.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.newEditor.modificationChanged.connect(self.modified)
if filename:
self.newEditor.setText(open(filename, "r").read())
self.newEditor.setModified(False)
self.path = filename

# Creates layout for message bar
self.layout = QGridLayout(self.newEditor)
self.layout.setContentsMargins(0, 0, 0, 0)
Expand All @@ -463,28 +461,14 @@ def __init__(self, parent, parentConsole, filename, *args):
sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed)
self.infoBar.setSizePolicy(sizePolicy)
self.layout.addWidget(self.infoBar, 0, 0, 1, 1)

self.tabLayout = QGridLayout(self)
self.tabLayout.setContentsMargins(0, 0, 0, 0)
self.tabLayout.addWidget(self.newEditor)

self.keyFilter = KeyFilter(parent, self)
self.setEventFilter(self.keyFilter)

# def openFile(self):
# scriptFile = QFileDialog.getOpenFileName(
# self, "Open File", "", "Script file (*.py)")
# if os.path.exists(scriptFile):
# self.newEditor.setText(open(scriptFile, "r").read())
# self.newEditor.setModified(False)
# fN = scriptFile.split('/')[-1]
# if fN:
# self.mw.setTabTitle(self, fN)
# self.path = scriptFile
# index = self.mw.currentIndex()
# idx = unicode(index)
# self.fileExcuteList[idx] = unicode(scriptFile)


def save(self):
if self.path is None:
self.path = str(QFileDialog().getSaveFileName(self,
Expand Down Expand Up @@ -514,27 +498,27 @@ def save(self):
self.mw.setTabTitle(self, fN)
self.newEditor.setModified(False)
self.pc.updateTabListScript(self.path, action='append')

def changeFont(self):
self.newEditor.refreshLexerProperties()

def modified(self, modified):
self.mw.tabModified(self, modified)

def close(self):
self.mw._removeTab(self, tab2index=True)

def setEventFilter(self, filter):
self.newEditor.installEventFilter(filter)

def newTab(self):
self.mw.newTabEditor()

class EditorTabWidget(QTabWidget):
def __init__(self, parent):
QTabWidget.__init__(self, parent=None)
self.parent = parent

# Layout for top frame (restore tabs)
self.layoutTopFrame = QGridLayout(self)
self.layoutTopFrame.setContentsMargins(0, 0, 0, 0)
Expand All @@ -549,7 +533,7 @@ def __init__(self, parent):
label = QCoreApplication.translate("PythonConsole",
"Click on button to restore all tabs from last session.")
self.label = QLabel(label)

self.restoreTabsButton = QToolButton()
toolTipRestore = QCoreApplication.translate("PythonConsole",
"Restore tabs")
Expand All @@ -559,7 +543,7 @@ def __init__(self, parent):
self.restoreTabsButton.setCursor(Qt.PointingHandCursor)
self.restoreTabsButton.setStyleSheet('QToolButton:hover{border: none } \
QToolButton:pressed{border: none}')

self.clButton = QToolButton()
toolTipClose = QCoreApplication.translate("PythonConsole",
"Close")
Expand All @@ -570,14 +554,14 @@ def __init__(self, parent):
self.clButton.setStyleSheet('QToolButton:hover{border: none } \
QToolButton:pressed{border: none}')
self.clButton.setAutoRaise(True)

sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed)
self.topFrame.setSizePolicy(sizePolicy)
self.layoutTopFrame.addWidget(self.topFrame, 0, 0, 1, 1)
self.layoutTopFrame2.addWidget(self.label, 0, 1, 1, 1)
self.layoutTopFrame2.addWidget(self.restoreTabsButton, 0, 0, 1, 1)
self.layoutTopFrame2.addWidget(self.clButton, 0, 2, 1, 1)

self.topFrame.hide()
self.connect(self.restoreTabsButton, SIGNAL('clicked()'), self.restoreTabs)
self.connect(self.clButton, SIGNAL('clicked()'), self.closeRestore)
Expand All @@ -589,12 +573,12 @@ def __init__(self, parent):
self.newTabEditor(filename=None)
if self.restoreTabList:
self.topFrame.show()

self.setDocumentMode(True)
self.setMovable(True)
#self.setTabsClosable(True)
self.setTabPosition(QTabWidget.South)

# Menu button list tabs
self.fileTabMenu = QMenu(self)
self.connect(self.fileTabMenu, SIGNAL("aboutToShow()"),
Expand All @@ -608,18 +592,16 @@ def __init__(self, parent):
self.fileTabButton.setPopupMode(QToolButton.InstantPopup)
self.fileTabButton.setMenu(self.fileTabMenu)
self.setCornerWidget(self.fileTabButton, Qt.TopRightCorner)
#self.connect(self.closeTabButton, SIGNAL('clicked()'), self.buttonClosePressed)

self.connect(self, SIGNAL("tabCloseRequested(int)"), self._removeTab)

# Open button
self.newTabButton = QToolButton(self)
self.newTabButton.setToolTip('New Tab')
self.newTabButton.setAutoRaise(True)
self.newTabButton.setIcon(QgsApplication.getThemeIcon("console/iconNewTabEditorConsole.png"))
self.setCornerWidget(self.newTabButton, Qt.TopLeftCorner)
self.connect(self.newTabButton, SIGNAL('clicked()'), self.newTabEditor)

def newTabEditor(self, tabName=None, filename=None):
nr = self.count()
if not tabName:
Expand All @@ -633,12 +615,12 @@ def newTabEditor(self, tabName=None, filename=None):
self.iconTab = QgsApplication.getThemeIcon('console/iconTabEditorConsole.png')
self.addTab(self.tab, self.iconTab, tabName)
self.setCurrentWidget(self.tab)

def tabModified(self, tab, modified):
index = self.indexOf(tab)
color = Qt.darkGray if modified else Qt.black
self.tabBar().setTabTextColor(index, color)

def closeTab(self, tab):
# Check if file has been saved
#if isModified:
Expand Down
17 changes: 2 additions & 15 deletions python/console/console_output.py
Expand Up @@ -119,20 +119,13 @@ def __init__(self, parent=None):
self.setWrapMode(QsciScintilla.WrapCharacter)
self.SendScintilla(QsciScintilla.SCI_SETHSCROLLBAR, 0)

#self.runShortcut = QShortcut(QKeySequence(Qt.CTRL + Qt.Key_E), self)
#self.runShortcut.activated.connect(self.enteredSelected)
# Reimplemeted copy action to prevent paste prompt (>>>,...) in command view
self.copyShortcut = QShortcut(QKeySequence.Copy, self)
self.copyShortcut.activated.connect(self.copy)
self.selectAllShortcut = QShortcut(QKeySequence.SelectAll, self)
self.selectAllShortcut.activated.connect(self.selectAll)

def insertInitText(self):
# txtInit = QCoreApplication.translate("PythonConsole",
# "## To access Quantum GIS environment from this console\n"
# "## use iface object (instance of QgisInterface class).\n"
# "## Type help(iface) for more info and list of methods.\n\n")
# initText = self.setText(txtInit)
txtInit = QCoreApplication.translate("PythonConsole",
"Python %1 on %2\n"
"## Type help(iface) for more info and list of methods.\n").arg(sys.version, socket.gethostname())
Expand Down Expand Up @@ -162,13 +155,7 @@ def setLexers(self):

self.setLexer(self.lexer)

# def getTextFromEditor(self):
# text = self.text()
# textList = text.split("\n")
# return textList

def clearConsole(self):
#self.SendScintilla(QsciScintilla.SCI_CLEARALL)
self.setText('')
self.insertInitText()
self.shell.setFocus()
Expand Down Expand Up @@ -219,11 +206,12 @@ def hideToolBar(self):
tB = self.parent.toolBar
tB.hide() if tB.isVisible() else tB.show()
self.shell.setFocus()

def showEditor(self):
Ed = self.parent.widgetEditor
if not Ed.isVisible():
Ed.show()
self.parent.showEditorButton.setChecked(True)
self.shell.setFocus()

def copy(self):
Expand Down Expand Up @@ -255,4 +243,3 @@ def keyPressEvent(self, e):
def widgetMessageBar(self, iface, text):
timeout = iface.messageTimeout()
self.infoBar.pushMessage('Console', text, QgsMessageBar.INFO, timeout)

15 changes: 0 additions & 15 deletions python/console/console_sci.py
Expand Up @@ -245,20 +245,6 @@ def new_prompt(self, prompt):
def refreshLexerProperties(self):
self.setLexers()

#def check_selection(self):
#"""
#Check if selected text is r/w,
#otherwise remove read-only parts of selection
#"""
##if self.current_prompt_pos is None:
##self.move_cursor_to_end()
##return
#line_from, index_from, line_to, index_to = self.getSelection()
#pline, pindex = self.getCursorPosition()
#if line_from < pline or \
#(line_from == pline and index_from < pindex):
#self.setSelection(pline, pindex, line_to, index_to)

def displayPrompt(self, more=False):
self.append("... ") if more else self.append(">>> ")
self.move_cursor_to_end()
Expand Down Expand Up @@ -338,7 +324,6 @@ def keyPressEvent(self, e):
if e.key() in (Qt.Key_Left, Qt.Key_Right, Qt.Key_Home, Qt.Key_End):
QsciScintilla.keyPressEvent(self, e)
return

# all other keystrokes get sent to the input line
self.move_cursor_to_end()

Expand Down

0 comments on commit 07abb34

Please sign in to comment.