diff options
author | Sebastian Koslowski <koslowski@kit.edu> | 2015-06-10 16:26:36 +0200 |
---|---|---|
committer | Sebastian Koslowski <koslowski@kit.edu> | 2015-07-17 16:24:00 +0200 |
commit | dcc0f96034a4547d4ea71d67e5b1b244ee997045 (patch) | |
tree | 3de2d884b3e9a6539c52e13776a823f10e832ff1 /grc/python | |
parent | 72048e6e8144d4e0a342a41753ec68d357beb04c (diff) |
grc: add QT GUI hier blocks (#727)
Diffstat (limited to 'grc/python')
-rw-r--r-- | grc/python/Block.py | 15 | ||||
-rw-r--r-- | grc/python/Generator.py | 37 | ||||
-rw-r--r-- | grc/python/flow_graph.tmpl | 33 |
3 files changed, 72 insertions, 13 deletions
diff --git a/grc/python/Block.py b/grc/python/Block.py index 517019c74a..548c991318 100644 --- a/grc/python/Block.py +++ b/grc/python/Block.py @@ -19,11 +19,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA from collections import defaultdict +from .. base.Constants import BLOCK_FLAG_NEED_QT_GUI, BLOCK_FLAG_NEED_WX_GUI from .. base.Block import Block as _Block from .. gui.Block import Block as _GUIBlock from . FlowGraph import _variable_matcher import extract_docs + class Block(_Block, _GUIBlock): def is_virtual_sink(self): return self.get_key() == 'virtual_sink' @@ -99,13 +101,22 @@ class Block(_Block, _GUIBlock): self.get_parent().evaluate(value) except Exception as err: self.add_error_message('Value "%s" cannot be evaluated:\n%s' % (value, err)) + # check if this is a GUI block and matches the selected generate option current_generate_option = self.get_parent().get_option('generate_options') - for label, option in (('WX GUI', 'wx_gui'), ('QT GUI', 'qt_gui')): - if self.get_name().startswith(label) and current_generate_option != option: + + def check_generate_mode(label, flag, valid_options): + block_requires_mode = ( + flag in self.get_flags() or + self.get_name().upper().startswith(label) + ) + if block_requires_mode and current_generate_option not in valid_options: self.add_error_message("Can't generate this block in mode " + repr(current_generate_option)) + check_generate_mode('WX GUI', BLOCK_FLAG_NEED_WX_GUI, ('wx_gui',)) + check_generate_mode('QT GUI', BLOCK_FLAG_NEED_QT_GUI, ('qt_gui', 'hb_qt_gui')) + def rewrite(self): """ Add and remove ports to adjust for the nports. diff --git a/grc/python/Generator.py b/grc/python/Generator.py index d9e92cd31f..98b671dde5 100644 --- a/grc/python/Generator.py +++ b/grc/python/Generator.py @@ -27,6 +27,7 @@ from Cheetah.Template import Template from .. gui import Messages from .. base import ParseXML from .. base import odict +from .. base.Constants import BLOCK_FLAG_NEED_QT_GUI from . Constants import TOP_BLOCK_FILE_MODE, FLOW_GRAPH_TEMPLATE, \ XTERM_EXECUTABLE, HIER_BLOCK_FILE_MODE, HIER_BLOCKS_LIB_DIR, BLOCK_DTD @@ -47,9 +48,13 @@ class Generator(object): """ self._generate_options = flow_graph.get_option('generate_options') if self._generate_options == 'hb': - self._generator = HierBlockGenerator(flow_graph, file_path) + generator_cls = HierBlockGenerator + elif self._generate_options == 'hb_qt_gui': + generator_cls = QtHierBlockGenerator else: - self._generator = TopBlockGenerator(flow_graph, file_path) + generator_cls = TopBlockGenerator + + self._generator = generator_cls(flow_graph, file_path) def get_generate_options(self): return self._generate_options @@ -87,7 +92,7 @@ class TopBlockGenerator(object): """generate output and write it to files""" # do throttle warning throttling_blocks = filter(lambda b: b.throtteling(), self._flow_graph.get_enabled_blocks()) - if not throttling_blocks and self._generate_options != 'hb': + if not throttling_blocks and not self._generate_options.startswith('hb'): Messages.send_warning("This flow graph may not have flow control: " "no audio or RF hardware blocks found. " "Add a Misc->Throttle block to your flow " @@ -325,3 +330,29 @@ class HierBlockGenerator(TopBlockGenerator): n = {'block': block_n} return n + + +class QtHierBlockGenerator(HierBlockGenerator): + + def _build_block_n_from_flow_graph_io(self): + n = HierBlockGenerator._build_block_n_from_flow_graph_io(self) + block_n = n['block'] + + if not block_n['name'].upper().startswith('QT GUI'): + block_n['name'] = 'QT GUI ' + block_n['name'] + + block_n.insert_after('category', 'flags', BLOCK_FLAG_NEED_QT_GUI) + + gui_hint_param = odict() + gui_hint_param['name'] = 'GUI Hint' + gui_hint_param['key'] = 'gui_hint' + gui_hint_param['value'] = '' + gui_hint_param['type'] = 'gui_hint' + gui_hint_param['hide'] = 'part' + block_n['param'].append(gui_hint_param) + + block_n['make'] += ( + "\n#set $win = 'self.%s' % $id" + "\n${gui_hint()($win)}" + ) + return n diff --git a/grc/python/flow_graph.tmpl b/grc/python/flow_graph.tmpl index 814b513213..3dd772b6ee 100644 --- a/grc/python/flow_graph.tmpl +++ b/grc/python/flow_graph.tmpl @@ -1,4 +1,4 @@ -#if $generate_options != 'hb' +#if not $generate_options.startswith('hb') #!/usr/bin/env python2 #end if ######################################################## @@ -61,7 +61,8 @@ sys.path.append(os.environ.get('GRC_HIER_PATH', os.path.expanduser('~/.grc_gnura #end if #for $imp in $imports -$(imp.replace(" # grc-generated hier_block", "")) +##$(imp.replace(" # grc-generated hier_block", "")) +$imp #end for ######################################################## @@ -76,6 +77,7 @@ $(imp.replace(" # grc-generated hier_block", "")) #if $generate_options == 'wx_gui' #import gtk #set $icon = gtk.IconTheme().lookup_icon('gnuradio-grc', 32, 0) + class $(class_name)(grc_wxgui.top_block_gui): def __init__($param_str): @@ -85,7 +87,7 @@ class $(class_name)(grc_wxgui.top_block_gui): self.SetIcon(wx.Icon(_icon_path, wx.BITMAP_TYPE_ANY)) #end if #elif $generate_options == 'qt_gui' -from distutils.version import StrictVersion + class $(class_name)(gr.top_block, Qt.QWidget): def __init__($param_str): @@ -110,16 +112,21 @@ class $(class_name)(gr.top_block, Qt.QWidget): self.settings = Qt.QSettings("GNU Radio", "$class_name") self.restoreGeometry(self.settings.value("geometry").toByteArray()) - #elif $generate_options == 'no_gui' + class $(class_name)(gr.top_block): def __init__($param_str): gr.top_block.__init__(self, "$title") -#elif $generate_options == 'hb' +#elif $generate_options.startswith('hb') #set $in_sigs = $flow_graph.get_hier_block_stream_io('in') #set $out_sigs = $flow_graph.get_hier_block_stream_io('out') + +#if $generate_options == 'hb_qt_gui' +class $(class_name)(gr.hier_block2, Qt.QWidget): +#else class $(class_name)(gr.hier_block2): +#end if #def make_io_sig($io_sigs) #set $size_strs = ['%s*%s'%(io_sig['size'], io_sig['vlen']) for io_sig in $io_sigs] #if len($io_sigs) == 0 @@ -143,6 +150,14 @@ gr.io_signaturev($(len($io_sigs)), $(len($io_sigs)), [$(', '.join($size_strs))]) #for $pad in $flow_graph.get_hier_block_message_io('out') self.message_port_register_hier_in("$pad['label']") #end for + #if $generate_options == 'hb_qt_gui' + + Qt.QWidget.__init__(self) + self.top_layout = Qt.QVBoxLayout() + self.top_grid_layout = Qt.QGridLayout() + self.top_layout.addLayout(self.top_grid_layout) + self.setLayout(self.top_layout) + #end if #end if #if $flow_graph.get_option('thread_safe_setters') @@ -304,7 +319,7 @@ $param.get_make()#slurp #end if $short_id#slurp #end def -#if $generate_options != 'hb' +#if not $generate_options.startswith('hb') if __name__ == '__main__': parser = OptionParser(option_class=eng_option, usage="%prog: [options]") @@ -334,7 +349,8 @@ if __name__ == '__main__': tb.Wait() #end if #elif $generate_options == 'qt_gui' - if(StrictVersion(Qt.qVersion()) >= StrictVersion("4.5.0")): + from distutils.version import StrictVersion + if StrictVersion(Qt.qVersion()) >= StrictVersion("4.5.0"): Qt.QApplication.setGraphicsSystem(gr.prefs().get_string('qtgui','style','raster')) qapp = Qt.QApplication(sys.argv) tb = $(class_name)($(', '.join($params_eq_list))) @@ -346,6 +362,7 @@ if __name__ == '__main__': #end if #end if tb.show() + def quitting(): tb.stop() tb.wait() @@ -358,7 +375,7 @@ if __name__ == '__main__': sys.stderr.write("Monitor '{0}' does not have an enable ('en') parameter.".format("tb.$m.get_id()")) #end for qapp.exec_() - tb = None #to clean up Qt widgets + tb = None # to clean up Qt widgets #elif $generate_options == 'no_gui' tb = $(class_name)($(', '.join($params_eq_list))) #set $run_options = $flow_graph.get_option('run_options') |