Skip to content

Commit

Permalink
[FEATURE] - Sharing snippets code from PyQGIS console
Browse files Browse the repository at this point in the history
- now you can share snippets code by codepad.org
- added much more actions in contextual menu
  • Loading branch information
slarosa committed Nov 3, 2012
1 parent 744314d commit b1d7a15
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 13 deletions.
Binary file added images/console/iconCodepadConsole.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions images/images.qrc
Expand Up @@ -480,6 +480,7 @@
<file>console/iconQtGuiConsole.png</file>
<file>console/iconRunConsole.png</file>
<file>console/iconAboutConsole.png</file>
<file>console/iconCodepadConsole.png</file>
<file>flags/sr_Cyrl.png</file>
<file>flags/sr_Latn.png</file>
</qresource>
Expand Down
52 changes: 49 additions & 3 deletions python/console/console_output.py
Expand Up @@ -24,7 +24,6 @@
from PyQt4.Qsci import (QsciScintilla,
QsciScintillaBase,
QsciLexerPython)

import sys

class writeOut:
Expand Down Expand Up @@ -107,15 +106,19 @@ def __init__(self, parent=None):
#self.setEdgeMode(QsciScintilla.EdgeLine)
#self.setEdgeColumn(80)
#self.setEdgeColor(QColor("#FF0000"))

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(Qt.CTRL + Qt.Key_C), self)
self.copyShortcut.activated.connect(self.copy)
self.selectAllShortcut = QShortcut(QKeySequence(Qt.CTRL + Qt.Key_A), self)
self.selectAllShortcut.activated.connect(self.selectAll)
self.pastebinShortcut = QShortcut(QKeySequence(Qt.CTRL + Qt.Key_V), self)
self.pastebinShortcut.activated.connect(self.pastebin)

def refreshLexerProperties(self):
self.setLexers()
Expand Down Expand Up @@ -153,14 +156,25 @@ def clearConsole(self):
def contextMenuEvent(self, e):
menu = QMenu(self)
iconRun = QIcon(":/images/console/iconRunConsole.png")
iconPastebin = QIcon(":/images/console/iconCodepadConsole.png")
iconClear = QIcon(":/images/console/iconClearConsole.png")
runAction = menu.addAction(iconRun, "Enter Selected", self.enteredSelected, QKeySequence(Qt.CTRL + Qt.Key_E))
clearAction = menu.addAction(iconClear, "Clear console", self.clearConsole)
menu.addSeparator()
copyAction = menu.addAction("Copy", self.copy, QKeySequence.Copy)
pastebinAction = menu.addAction(iconPastebin, "Share on codepad", self.pastebin, QKeySequence.Paste)
menu.addSeparator()
selectAllAction = menu.addAction("Select All", self.selectAll, QKeySequence.SelectAll)
runAction.setEnabled(False)
copyAction.setEnabled(False)
pastebinAction.setEnabled(False)
selectAllAction.setEnabled(False)
if self.hasSelectedText():
runAction.setEnabled(True)
copyAction.setEnabled(True)
pastebinAction.setEnabled(True)
if not self.text() == '':
selectAllAction.setEnabled(True)
action = menu.exec_(self.mapToGlobal(e.pos()))

def copy(self):
Expand Down Expand Up @@ -188,3 +202,35 @@ def keyPressEvent(self, e):
else:
# possible shortcut key sequence, accept it
e.accept()

def pastebin(self):
import urllib2, urllib
#listText = self.getTextFromEditor()
listText = self.selectedText().split('\n')
getCmd = []
for s in listText:
if s[0:3] in (">>>", "..."):
if not s[4] == "_":
s.replace(">>> ", "").replace("... ", "")
getCmd.append(unicode(s))
pasteText= u"\n".join(getCmd)
url = 'http://codepad.org'
values = {'lang' : 'Python',
'code' : pasteText,
'submit':'Submit'}
try:
response = urllib2.urlopen(url, urllib.urlencode(values))
url = response.read()
for href in url.split("</a>"):
if "Link:" in href:
ind=href.index('Link:')
found = href[ind+5:]
for i in found.split('">'):
if '<a href=' in i:
link = i.replace('<a href="',"").strip()
if link:
QApplication.clipboard().setText(link)
print "## URL copied to clipboard ##"
except urllib2.URLError, e:
print "## Connection error ##"
print "## " + str(e.args) + " ##"
14 changes: 4 additions & 10 deletions python/console/console_sci.py
Expand Up @@ -412,8 +412,11 @@ def contextMenuEvent(self, e):
copyAction = menu.addAction("Copy", self.copy, QKeySequence.Copy)
pasteAction = menu.addAction("Paste", self.paste, QKeySequence.Paste)
copyAction.setEnabled(False)
pasteAction.setEnabled(False)
if self.hasSelectedText():
copyAction.setEnabled(True)
if QApplication.clipboard().text() != "":
pasteAction.setEnabled(True)
action = menu.exec_(self.mapToGlobal(e.pos()))

def mousePressEvent(self, e):
Expand Down Expand Up @@ -509,7 +512,6 @@ def runCommand(self, cmd):
selCmdLenght = self.text(line).length()
self.setSelection(line, 0, line, selCmdLenght)
self.removeSelectedText()
#self.SendScintilla(QsciScintilla.SCI_NEWLINE)
if cmd in ('_save', '_clear', '_clearAll', '_pyqgis', '_api'):
if cmd == '_save':
self.writeHistoryFile()
Expand All @@ -520,18 +522,10 @@ def runCommand(self, cmd):
print QCoreApplication.translate("PythonConsole",
"## History cleared successfully ##")
elif cmd == '_clearAll':
res = QMessageBox.question(self, "Python Console",
QCoreApplication.translate("PythonConsole",
"Are you sure you want to completely\n"
"delete the command history ?"),
QMessageBox.Yes | QMessageBox.No)
if res == QMessageBox.No:
self.SendScintilla(QsciScintilla.SCI_DELETEBACK)
return
self.history = QStringList()
self.clearHistoryFile()
print QCoreApplication.translate("PythonConsole",
"## History cleared successfully ##")
"## Session and file history cleared successfully ##")
elif cmd == '_pyqgis':
webbrowser.open( "http://www.qgis.org/pyqgis-cookbook/" )
elif cmd == '_api':
Expand Down

0 comments on commit b1d7a15

Please sign in to comment.