24
24
25
25
from PyQt4 .QtCore import *
26
26
from PyQt4 .QtGui import *
27
+ from qgis .utils import iface
27
28
import sys
28
29
import traceback
29
30
import code
30
31
31
32
32
33
_init_commands = ["from qgis.core import *" , "import qgis.utils" ]
33
34
34
-
35
35
_console = None
36
36
37
37
def show_console ():
38
38
""" called from QGIS to open the console """
39
39
global _console
40
40
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 ()
47
49
50
+
48
51
49
52
_old_stdout = sys .stdout
50
53
_console_output = None
51
54
55
+
52
56
# hook for python console so all output will be redirected
53
57
# and then shown in console
54
58
def console_displayhook (obj ):
@@ -69,26 +73,26 @@ def flush(self):
69
73
70
74
sys .stdout = QgisOutputCatcher ()
71
75
72
-
73
- class PythonConsole (QWidget ):
76
+ class PythonConsole (QDockWidget ):
74
77
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 )
77
83
self .edit = PythonEdit ()
78
- self .l = QVBoxLayout ()
79
84
self .l .addWidget (self .edit )
80
- self .setLayout (self .l )
85
+ self .setWidget (self .widget )
81
86
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
+
85
91
86
92
def sizeHint (self ):
87
93
return QSize (500 ,300 )
88
94
89
95
def closeEvent (self , event ):
90
- s = QSettings ()
91
- s .setValue ("/python/console/geometry" , QVariant (self .saveGeometry ()))
92
96
QWidget .closeEvent (self , event )
93
97
94
98
0 commit comments