Skip to content

Commit 1fa7abb

Browse files
author
wonder
committed
Docking of Python console - contributed by Nathan Woodrow - with some improvements.
git-svn-id: http://svn.osgeo.org/qgis/trunk@14998 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent c4743a0 commit 1fa7abb

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

python/console.py

+22-18
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,35 @@
2424

2525
from PyQt4.QtCore import *
2626
from PyQt4.QtGui import *
27+
from qgis.utils import iface
2728
import sys
2829
import traceback
2930
import code
3031

3132

3233
_init_commands = ["from qgis.core import *", "import qgis.utils"]
3334

34-
3535
_console = None
3636

3737
def show_console():
3838
""" called from QGIS to open the console """
3939
global _console
4040
if _console is None:
41-
_console = PythonConsole()
42-
_console.show()
43-
_console.raise_()
44-
_console.setWindowState( _console.windowState() & ~Qt.WindowMinimized )
45-
_console.activateWindow()
46-
41+
_console = PythonConsole(iface.mainWindow())
42+
_console.show() # force show even if it was restored as hidden
43+
else:
44+
_console.setVisible(not _console.isVisible())
45+
# set focus to the edit box so the user can start typing
46+
if _console.isVisible():
47+
_console.activateWindow()
48+
_console.edit.setFocus()
4749

50+
4851

4952
_old_stdout = sys.stdout
5053
_console_output = None
5154

55+
5256
# hook for python console so all output will be redirected
5357
# and then shown in console
5458
def console_displayhook(obj):
@@ -69,26 +73,26 @@ def flush(self):
6973

7074
sys.stdout = QgisOutputCatcher()
7175

72-
73-
class PythonConsole(QWidget):
76+
class PythonConsole(QDockWidget):
7477
def __init__(self, parent=None):
75-
QWidget.__init__(self, parent)
76-
78+
QDockWidget.__init__(self, parent)
79+
self.setObjectName("Python Console")
80+
self.setAllowedAreas(Qt.BottomDockWidgetArea)
81+
self.widget = QWidget()
82+
self.l = QVBoxLayout(self.widget)
7783
self.edit = PythonEdit()
78-
self.l = QVBoxLayout()
7984
self.l.addWidget(self.edit)
80-
self.setLayout(self.l)
85+
self.setWidget(self.widget)
8186
self.setWindowTitle(QCoreApplication.translate("PythonConsole", "Python Console"))
82-
83-
s = QSettings()
84-
self.restoreGeometry(s.value("/python/console/geometry").toByteArray())
87+
# try to restore position from stored main window state
88+
if not iface.mainWindow().restoreDockWidget(self):
89+
iface.mainWindow().addDockWidget(Qt.BottomDockWidgetArea, self)
90+
8591

8692
def sizeHint(self):
8793
return QSize(500,300)
8894

8995
def closeEvent(self, event):
90-
s = QSettings()
91-
s.setValue("/python/console/geometry", QVariant(self.saveGeometry()))
9296
QWidget.closeEvent(self, event)
9397

9498

0 commit comments

Comments
 (0)