Skip to content

Commit b1d7a15

Browse files
committedNov 3, 2012
[FEATURE] - Sharing snippets code from PyQGIS console
- now you can share snippets code by codepad.org - added much more actions in contextual menu
1 parent 744314d commit b1d7a15

File tree

4 files changed

+54
-13
lines changed

4 files changed

+54
-13
lines changed
 

‎images/console/iconCodepadConsole.png

919 Bytes
Loading

‎images/images.qrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@
480480
<file>console/iconQtGuiConsole.png</file>
481481
<file>console/iconRunConsole.png</file>
482482
<file>console/iconAboutConsole.png</file>
483+
<file>console/iconCodepadConsole.png</file>
483484
<file>flags/sr_Cyrl.png</file>
484485
<file>flags/sr_Latn.png</file>
485486
</qresource>

‎python/console/console_output.py

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from PyQt4.Qsci import (QsciScintilla,
2525
QsciScintillaBase,
2626
QsciLexerPython)
27-
2827
import sys
2928

3029
class writeOut:
@@ -107,15 +106,19 @@ def __init__(self, parent=None):
107106
#self.setEdgeMode(QsciScintilla.EdgeLine)
108107
#self.setEdgeColumn(80)
109108
#self.setEdgeColor(QColor("#FF0000"))
110-
109+
111110
self.setWrapMode(QsciScintilla.WrapCharacter)
112111
self.SendScintilla(QsciScintilla.SCI_SETHSCROLLBAR, 0)
113-
112+
114113
self.runShortcut = QShortcut(QKeySequence(Qt.CTRL + Qt.Key_E), self)
115114
self.runShortcut.activated.connect(self.enteredSelected)
116115
# Reimplemeted copy action to prevent paste prompt (>>>,...) in command view
117116
self.copyShortcut = QShortcut(QKeySequence(Qt.CTRL + Qt.Key_C), self)
118117
self.copyShortcut.activated.connect(self.copy)
118+
self.selectAllShortcut = QShortcut(QKeySequence(Qt.CTRL + Qt.Key_A), self)
119+
self.selectAllShortcut.activated.connect(self.selectAll)
120+
self.pastebinShortcut = QShortcut(QKeySequence(Qt.CTRL + Qt.Key_V), self)
121+
self.pastebinShortcut.activated.connect(self.pastebin)
119122

120123
def refreshLexerProperties(self):
121124
self.setLexers()
@@ -153,14 +156,25 @@ def clearConsole(self):
153156
def contextMenuEvent(self, e):
154157
menu = QMenu(self)
155158
iconRun = QIcon(":/images/console/iconRunConsole.png")
159+
iconPastebin = QIcon(":/images/console/iconCodepadConsole.png")
160+
iconClear = QIcon(":/images/console/iconClearConsole.png")
156161
runAction = menu.addAction(iconRun, "Enter Selected", self.enteredSelected, QKeySequence(Qt.CTRL + Qt.Key_E))
162+
clearAction = menu.addAction(iconClear, "Clear console", self.clearConsole)
157163
menu.addSeparator()
158164
copyAction = menu.addAction("Copy", self.copy, QKeySequence.Copy)
165+
pastebinAction = menu.addAction(iconPastebin, "Share on codepad", self.pastebin, QKeySequence.Paste)
166+
menu.addSeparator()
167+
selectAllAction = menu.addAction("Select All", self.selectAll, QKeySequence.SelectAll)
159168
runAction.setEnabled(False)
160169
copyAction.setEnabled(False)
170+
pastebinAction.setEnabled(False)
171+
selectAllAction.setEnabled(False)
161172
if self.hasSelectedText():
162173
runAction.setEnabled(True)
163174
copyAction.setEnabled(True)
175+
pastebinAction.setEnabled(True)
176+
if not self.text() == '':
177+
selectAllAction.setEnabled(True)
164178
action = menu.exec_(self.mapToGlobal(e.pos()))
165179

166180
def copy(self):
@@ -188,3 +202,35 @@ def keyPressEvent(self, e):
188202
else:
189203
# possible shortcut key sequence, accept it
190204
e.accept()
205+
206+
def pastebin(self):
207+
import urllib2, urllib
208+
#listText = self.getTextFromEditor()
209+
listText = self.selectedText().split('\n')
210+
getCmd = []
211+
for s in listText:
212+
if s[0:3] in (">>>", "..."):
213+
if not s[4] == "_":
214+
s.replace(">>> ", "").replace("... ", "")
215+
getCmd.append(unicode(s))
216+
pasteText= u"\n".join(getCmd)
217+
url = 'http://codepad.org'
218+
values = {'lang' : 'Python',
219+
'code' : pasteText,
220+
'submit':'Submit'}
221+
try:
222+
response = urllib2.urlopen(url, urllib.urlencode(values))
223+
url = response.read()
224+
for href in url.split("</a>"):
225+
if "Link:" in href:
226+
ind=href.index('Link:')
227+
found = href[ind+5:]
228+
for i in found.split('">'):
229+
if '<a href=' in i:
230+
link = i.replace('<a href="',"").strip()
231+
if link:
232+
QApplication.clipboard().setText(link)
233+
print "## URL copied to clipboard ##"
234+
except urllib2.URLError, e:
235+
print "## Connection error ##"
236+
print "## " + str(e.args) + " ##"

‎python/console/console_sci.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,11 @@ def contextMenuEvent(self, e):
412412
copyAction = menu.addAction("Copy", self.copy, QKeySequence.Copy)
413413
pasteAction = menu.addAction("Paste", self.paste, QKeySequence.Paste)
414414
copyAction.setEnabled(False)
415+
pasteAction.setEnabled(False)
415416
if self.hasSelectedText():
416417
copyAction.setEnabled(True)
418+
if QApplication.clipboard().text() != "":
419+
pasteAction.setEnabled(True)
417420
action = menu.exec_(self.mapToGlobal(e.pos()))
418421

419422
def mousePressEvent(self, e):
@@ -509,7 +512,6 @@ def runCommand(self, cmd):
509512
selCmdLenght = self.text(line).length()
510513
self.setSelection(line, 0, line, selCmdLenght)
511514
self.removeSelectedText()
512-
#self.SendScintilla(QsciScintilla.SCI_NEWLINE)
513515
if cmd in ('_save', '_clear', '_clearAll', '_pyqgis', '_api'):
514516
if cmd == '_save':
515517
self.writeHistoryFile()
@@ -520,18 +522,10 @@ def runCommand(self, cmd):
520522
print QCoreApplication.translate("PythonConsole",
521523
"## History cleared successfully ##")
522524
elif cmd == '_clearAll':
523-
res = QMessageBox.question(self, "Python Console",
524-
QCoreApplication.translate("PythonConsole",
525-
"Are you sure you want to completely\n"
526-
"delete the command history ?"),
527-
QMessageBox.Yes | QMessageBox.No)
528-
if res == QMessageBox.No:
529-
self.SendScintilla(QsciScintilla.SCI_DELETEBACK)
530-
return
531525
self.history = QStringList()
532526
self.clearHistoryFile()
533527
print QCoreApplication.translate("PythonConsole",
534-
"## History cleared successfully ##")
528+
"## Session and file history cleared successfully ##")
535529
elif cmd == '_pyqgis':
536530
webbrowser.open( "http://www.qgis.org/pyqgis-cookbook/" )
537531
elif cmd == '_api':

0 commit comments

Comments
 (0)
Please sign in to comment.