Skip to content
Permalink
Browse files

Merge pull request #314 from slarosa/master

[FEATURE] Sharing snippets code from PyQGIS console
  • Loading branch information
brushtyler committed Nov 4, 2012
2 parents 6ecfc29 + ed4bc25 commit ef6b3c4d9f56645391989b48d14318c43e563d36
Binary file not shown.
@@ -483,6 +483,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>
@@ -24,7 +24,6 @@
from PyQt4.Qsci import (QsciScintilla,
QsciScintillaBase,
QsciLexerPython)

import sys

class writeOut:
@@ -114,8 +113,10 @@ def __init__(self, parent=None):
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 = QShortcut(QKeySequence.Copy, self)
self.copyShortcut.activated.connect(self.copy)
self.selectAllShortcut = QShortcut(QKeySequence.SelectAll, self)
self.selectAllShortcut.activated.connect(self.selectAll)

def refreshLexerProperties(self):
self.setLexers()
@@ -153,14 +154,36 @@ def clearConsole(self):
def contextMenuEvent(self, e):
menu = QMenu(self)
iconRun = QIcon(":/images/console/iconRunConsole.png")
runAction = menu.addAction(iconRun, "Enter Selected", self.enteredSelected, QKeySequence(Qt.CTRL + Qt.Key_E))
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)
menu.addSeparator()
copyAction = menu.addAction("Copy", self.copy, QKeySequence.Copy)
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):
@@ -188,3 +211,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) + " ##"
@@ -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):
@@ -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()
@@ -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':

0 comments on commit ef6b3c4

Please sign in to comment.
You can’t perform that action at this time.