aboutsummaryrefslogtreecommitdiffstats
path: root/grc/gui_qt/components/console.py
blob: 4592824fc1c391a90a33f6e11b77fc678aebadbe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# Copyright 2014-2020 Free Software Foundation, Inc.
# This file is part of GNU Radio
#
# GNU Radio Companion is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# GNU Radio Companion is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA

from __future__ import absolute_import, print_function

# Standard modules
import html
import logging
import textwrap

# Third-party  modules

from qtpy import QtCore, QtGui, QtWidgets

# Custom modules
from .. import base

# Shortcuts
Action = QtWidgets.QAction
Menu = QtWidgets.QMenu
Toolbar = QtWidgets.QToolBar
Icons = QtGui.QIcon.fromTheme
Keys = QtGui.QKeySequence

# Logging
log = logging.getLogger(f"grc.application.{__name__}")


HTML = '''
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\"
\"http://www.w3.org/TR/REC-html40/strict.dtd\">
<html>
<head>
    <meta name=\"qrichtext\" content=\"1\" />
    <style type=\"text/css\">
        p, li {
            white-space: pre-wrap;
        }

        /* Not currently used because entries are wrapped with <pre> */
        body {
            /* font-family:  \'Ubuntu\'; */
            font-family:  \'MS Shell Dlg 2\';
            font-size:    10pt;
            font-weight:  400;
            font-style:   normal;
            font-color:   black;
            margin:       0px;
            text-indent:  0px;
            -qt-block-indent:   0;
        }

        pre {
            display: block;
            font-family: monospace;
            white-space: pre;
            font-color:  black;
            margin:      1em 0;
        }

    </style>
</head>
</html>
'''


class Console(QtWidgets.QDockWidget, base.Component):
    def __init__(self, level):
        super(Console, self).__init__()

        self.setObjectName('console')
        self.setWindowTitle('Console')
        self.level = level

        ### GUI Widgets

        # Create the layout widget
        container = QtWidgets.QWidget(self)
        container.setObjectName('console::container')
        self._container = container

        layout = QtWidgets.QHBoxLayout(container)
        layout.setObjectName('console::layout')
        layout.setSpacing(0)
        layout.setContentsMargins(5, 0, 5, 5)
        self._layout = layout

        # Console output widget
        text = QtWidgets.QTextEdit(container)
        text.setObjectName('console::text')
        text.setUndoRedoEnabled(False)
        text.setReadOnly(True)
        text.setCursorWidth(0)
        text.setTextInteractionFlags(QtCore.Qt.TextSelectableByKeyboard | QtCore.Qt.TextSelectableByMouse)
        text.setHtml(textwrap.dedent(HTML))
        self._text = text

        # Add widgets to the component
        layout.addWidget(text)
        container.setLayout(layout)
        self.setWidget(container)

        ### Translation support

        #self.setWindowTitle(_translate("", "Library", None))
        #library.headerItem().setText(0, _translate("", "Blocks", None))
        #QtCore.QMetaObject.connectSlotsByName(blockLibraryDock)

        ### Setup actions

        # TODO: Move to the base controller and set actions as class attributes
        # Automatically create the actions, menus and toolbars.
        # Child controllers need to call the register functions to integrate into the mainwindow
        self.actions = {}
        self.menus = {}
        self.toolbars = {}
        self.createActions(self.actions)
        self.createMenus(self.actions, self.menus)
        self.createToolbars(self.actions, self.toolbars)
        self.connectSlots()

        # Register the dock widget through the AppController.
        # The AppController then tries to find a saved dock location from the preferences
        # before calling the MainWindow Controller to add the widget.
        self.app.registerDockWidget(self, location=self.settings.window.CONSOLE_DOCK_LOCATION)

        # Register the menus
        self.app.registerMenu(self.menus["console"])

        # Register a new handler for the root logger that outputs messages of
        #  INFO and HIGHER to the reports view
        handler = ReportsHandler(self.add_line)
        handler.setLevel(self.level)

        # Need to add this handler to the parent of the controller's logger
        log.parent.addHandler(handler)
        self.handler = handler

        self.actions['show_level'].setChecked = True
        self.handler.show_level = True
        self.enabled = False

    def enable(self):
        self.enabled = True

    ### Actions

    def createActions(self, actions):
        ''' Defines all actions for this view. '''

        log.debug("Creating actions")

        # File Actions
        actions['save'] = Action(Icons("document-save"), _("save"), self, statusTip=_("save-tooltip"))
        actions['clear'] = Action(Icons("document-close"), _("clear"), self, statusTip=_("clear-tooltip"))
        actions['show_level'] = Action(_("show-level"), self, statusTip=_("show-level"), checkable=True, checked=True)

        actions['auto_scroll'] = Action(_("auto-scroll"), self, statusTip=_("auto-scroll"), checkable=True, checked=True)

    def createMenus(self, actions, menus):
        ''' Setup the view's menus '''

        log.debug("Creating menus")

        console_menu = QtWidgets.QMenu("&Console")
        console_menu.setObjectName("console::menu")

        # Not needed, we have FileHandler logging in main.py
        #console_menu.addAction(actions["save"])

        console_menu.addAction(actions["clear"])
        console_menu.addAction(actions["show_level"])
        console_menu.addAction(actions["auto_scroll"])
        menus["console"] = console_menu

    def createToolbars(self, actions, toolbars):
        log.debug("Creating toolbars")

    def add_line(self, line):
        # TODO: Support multiple columns for the HTML. DO better with the spacing
        #  and margins in the output
        if self.enabled:
            self._text.append(line)
            if self.actions["auto_scroll"].isChecked():
                self._text.verticalScrollBar().setValue(
                    self._text.verticalScrollBar().maximum())

    # Handlers for the view actions
    def clear_triggered(self):
        self._text.clear()

    def save_triggered(self):
        log.warning("Save reports not implemented")

    def show_level_toggled(self, checked):
        self.handler.show_level = checked


class ReportsHandler(logging.Handler):  # Inherit from logging.Handler
    ''' Writes out logs to the reporst window '''

    def __init__(self, add_line, show_level=True, short_level=True):
        # run the regular Handler __init__
        logging.Handler.__init__(self)

        self.add_line = add_line  # Function for adding a line to the view
        self.show_level = show_level  # Dynamically show levels
        self.short_level = short_level  # Default to true, changed by properties

        self.formatLevelLength = self.formatLevelShort
        if not short_level:
            self.formatLevelLength = self.formatLevelLong

    def emit(self, record):
        # Just handle all formatting here
        if self.show_level:
            level = self.formatLevel(record.levelname)
            message = html.escape(record.msg)
            output = self.formatOutput()
            self.add_line(output.format(level, message))
        else:
            message = html.escape(record.msg)
            output = self.formatOutput()
            self.add_line(output.format(message))

    def formatLevel(self, levelname):
        output = "{0}{1}{2}"
        level = self.formatLevelLength(levelname)
        if levelname == "INFO":
            return output.format("<font color=\"Green\"><b>", level, "</b></font>")
        elif levelname == "WARNING":
            return output.format("<font color=\"Orange\"><b>", level, "</b></font>")
        elif levelname == "ERROR":
            return output.format("<font color=\"Red\"><b>", level, "</b></font>")
        elif levelname == "CRITICAL":
            return output.format("<font color=\"Red\"><b>", level, "</b></font>")
        else:
            return output.format("<font color=\"Blue\"><b>", level, "</b></font>")

    def formatLevelShort(self, levelname):
        return f'[{levelname[0:1]}]'

    def formatLevelLong(self, levelname):
        output = "{0:<10}"
        if levelname in ["DEBUG", "INFO", "WARNING"]:
            return output.format(f'[{levelname.capitalize()}]')
        else:
            return output.format(f'[{levelname.upper()}]')

    def formatOutput(self):
        ''' Returns the correct output format based on internal settings '''
        if self.show_level:
            if self.short_level:
                return "<tr><td width=\"25\">{0}</td><td><pre>{1}</pre></td></tr>"
            return "<tr><td width=\"75\">{0}</td><td><pre>{1}</pre></td></tr>"
        return "<tr><td><pre>{0}</pre></td></tr>"