Skip to content
Permalink
Browse files

[pyqgis-console] add a context menu for tabs

  • Loading branch information
slarosa committed May 5, 2013
1 parent 635a1ee commit 507756a7f79e50568462bbce2b5bb1bbc818d21a
Showing with 87 additions and 38 deletions.
  1. +16 −12 python/console/console.py
  2. +71 −26 python/console/console_editor.py
@@ -657,12 +657,17 @@ def saveScriptFile(self):

def saveAsScriptFile(self):
tabWidget = self.tabEditorWidget.currentWidget()
index = self.tabEditorWidget.currentIndex()
if tabWidget is None:
return
if tabWidget.path is None:
pathFileName = self.tabEditorWidget.tabText(index) + '.py'
else:
pathFileName = tabWidget.path
saveAsFileTr = QCoreApplication.translate("PythonConsole", "Save File As")
filename = QFileDialog.getSaveFileName(self,
saveAsFileTr,
tabWidget.path, "Script file (*.py)")
pathFileName, "Script file (*.py)")
if not filename.isEmpty():
#print tabWidget.path
self.tabListScript.remove(unicode(tabWidget.path))
@@ -688,17 +693,16 @@ def callWidgetMessageBarEditor(self, text, level, timed):
self.tabEditorWidget.widgetMessageBar(iface, text, level, timed)

def updateTabListScript(self, script, action=None):
if script != '':
settings = QSettings()
if script == 'empty':
self.tabListScript = []
if script is not None and not action and script != 'empty':
self.tabListScript.remove(script)
if action:
if script not in self.tabListScript:
self.tabListScript.append(script)
settings.setValue("pythonConsole/tabScripts",
QVariant(self.tabListScript))
settings = QSettings()
if script == 'empty':
self.tabListScript = []
if script is not None and not action and script != 'empty':
self.tabListScript.remove(script)
if action:
if script not in self.tabListScript:
self.tabListScript.append(script)
settings.setValue("pythonConsole/tabScripts",
QVariant(self.tabListScript))

def saveSettingsConsole(self):
settings = QSettings()
@@ -263,19 +263,19 @@ def contextMenuEvent(self, e):
iconRun = QgsApplication.getThemeIcon("console/iconRunConsole.png")
iconRunScript = QgsApplication.getThemeIcon("console/iconRunScriptConsole.png")
iconCodePad = QgsApplication.getThemeIcon("console/iconCodepadConsole.png")
iconNewEditor = QgsApplication.getThemeIcon("console/iconTabEditorConsole.png")
#iconNewEditor = QgsApplication.getThemeIcon("console/iconTabEditorConsole.png")
iconCommentEditor = QgsApplication.getThemeIcon("console/iconCommentEditorConsole.png")
iconUncommentEditor = QgsApplication.getThemeIcon("console/iconUncommentEditorConsole.png")
iconSettings = QgsApplication.getThemeIcon("console/iconSettingsConsole.png")
iconFind = QgsApplication.getThemeIcon("console/iconSearchEditorConsole.png")
hideEditorAction = menu.addAction("Hide Editor",
self.hideEditor)
menu.addSeparator()
newTabAction = menu.addAction(iconNewEditor,
"New Tab",
self.parent.newTab, 'Ctrl+T')
closeTabAction = menu.addAction("Close Tab",
self.parent.close, 'Ctrl+W')
# menu.addSeparator()
# newTabAction = menu.addAction(iconNewEditor,
# "New Tab",
# self.parent.newTab, 'Ctrl+T')
# closeTabAction = menu.addAction("Close Tab",
# self.parent.close, 'Ctrl+W')
menu.addSeparator()
runSelected = menu.addAction(iconRun,
"Enter selected",
@@ -325,11 +325,11 @@ def contextMenuEvent(self, e):
runSelected.setEnabled(False)
copyAction.setEnabled(False)
selectAllAction.setEnabled(False)
closeTabAction.setEnabled(False)
# closeTabAction.setEnabled(False)
undoAction.setEnabled(False)
redoAction.setEnabled(False)
if self.parent.tw.count() > 1:
closeTabAction.setEnabled(True)
# if self.parent.tw.count() > 1:
# closeTabAction.setEnabled(True)
if self.hasSelectedText():
runSelected.setEnabled(True)
copyAction.setEnabled(True)
@@ -637,11 +637,12 @@ def loadFile(self, filename, modified):

def save(self):
if self.path is None:
index = self.tw.currentIndex()
saveTr = QCoreApplication.translate('PythonConsole',
'Python Console: Save file')
self.path = str(QFileDialog().getSaveFileName(self,
saveTr,
"*.py",
self.tw.tabText(index) + '.py',
"Script file (*.py)"))
# If the user didn't select a file, abort the save operation
if len(self.path) == 0:
@@ -688,6 +689,7 @@ def __init__(self, parent):
QTabWidget.__init__(self, parent=None)
self.parent = parent

self.idx = -1
# Layout for top frame (restore tabs)
self.layoutTopFrame = QGridLayout(self)
self.layoutTopFrame.setContentsMargins(0, 0, 0, 0)
@@ -777,13 +779,56 @@ def __init__(self, parent):
# Open button
self.newTabButton = QToolButton()
txtToolTipNewTab = QCoreApplication.translate("PythonConsole",
"New Tab")
"New Editor")
self.newTabButton.setToolTip(txtToolTipNewTab)
self.newTabButton.setAutoRaise(True)
self.newTabButton.setIcon(QgsApplication.getThemeIcon("console/iconNewTabEditorConsole.png"))
self.newTabButton.setIconSize(QSize(24, 24))
self.setCornerWidget(self.newTabButton, Qt.TopLeftCorner)
self.connect(self.newTabButton, SIGNAL('clicked()'), self.newTabEditor)

def contextMenuEvent(self, e):
tabBar = self.tabBar()
self.idx = tabBar.tabAt(e.pos())
cW = self.currentWidget()
menu = QMenu(self)
menu.addSeparator()
newTabAction = menu.addAction("New Editor",
self.newTabEditor)
menu.addSeparator()
closeTabAction = menu.addAction("Close Tab",
cW.close)
closeAllTabAction = menu.addAction("Close All",
self.closeAll)
closeOthersTabAction = menu.addAction("Close Others",
self.closeOthers)
menu.addSeparator()
saveAction = menu.addAction("Save",
cW.save)
saveAsAction = menu.addAction("Save As",
self.parent.saveAsScriptFile)
closeTabAction.setEnabled(False)
closeAllTabAction.setEnabled(False)
closeOthersTabAction.setEnabled(False)
if self.count() > 1:
closeTabAction.setEnabled(True)
closeAllTabAction.setEnabled(True)
closeOthersTabAction.setEnabled(True)
action = menu.exec_(self.mapToGlobal(e.pos()))

def closeOthers(self):
idx = self.idx
countTab = self.count()
for i in range(countTab - 1, idx, -1) + range(idx - 1, -1, -1):
self._removeTab(i)

def closeAll(self):
countTab = self.count()
cI = self.currentIndex()
for i in range(countTab - 1, 0, -1):
self._removeTab(i)
self.newTabEditor(tabName='Untitled-0')
self._removeTab(0)

def enableToolBarEditor(self, enable):
if self.topFrame.isVisible():
@@ -850,7 +895,8 @@ def _removeTab(self, tab, tab2index=False):
self.parent.updateTabListScript(self.widget(tab).path)
self.removeTab(tab)
else:
if self.widget(tab).path in self.restoreTabList:
if self.widget(tab).path is not None or \
self.widget(tab).path in self.restoreTabList:
self.parent.updateTabListScript(self.widget(tab).path)
if self.count() <= 2:
self.setTabsClosable(False)
@@ -875,19 +921,18 @@ def closeCurrentWidget(self):

def restoreTabs(self):
for script in self.restoreTabList:
if script != '':
pathFile = unicode(script.toString())
if os.path.exists(pathFile):
tabName = pathFile.split('/')[-1]
self.newTabEditor(tabName, pathFile)
else:
errOnRestore = QCoreApplication.translate("PythonConsole",
"Unable to restore the file: \n%1\n") \
.arg(pathFile)
print '## Error: '
s = errOnRestore
sys.stderr.write(s)
self.parent.updateTabListScript(pathFile)
pathFile = unicode(script.toString())
if os.path.exists(pathFile):
tabName = pathFile.split('/')[-1]
self.newTabEditor(tabName, pathFile)
else:
errOnRestore = QCoreApplication.translate("PythonConsole",
"Unable to restore the file: \n%1\n") \
.arg(pathFile)
print '## Error: '
s = errOnRestore
sys.stderr.write(s)
self.parent.updateTabListScript(pathFile)
if self.count() < 1:
self.newTabEditor(filename=None)
self.topFrame.close()

0 comments on commit 507756a

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