From dec3bf564e5ca3b6fc103b3eb66a80e8abe8229b Mon Sep 17 00:00:00 2001
From: Sebastian Koslowski <koslowski@kit.edu>
Date: Fri, 5 Feb 2016 11:43:26 +0100
Subject: grc: nicer block documentation tooltip and properties dialog tab

---
 grc/gui/PropsDialog.py | 38 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)

(limited to 'grc/gui/PropsDialog.py')

diff --git a/grc/gui/PropsDialog.py b/grc/gui/PropsDialog.py
index bf7d31d391..7c66a77a54 100644
--- a/grc/gui/PropsDialog.py
+++ b/grc/gui/PropsDialog.py
@@ -97,7 +97,8 @@ class PropsDialog(gtk.Dialog):
             self._params_boxes.append((tab, label, vbox))
 
         # Docs for the block
-        self._docs_text_display = SimpleTextDisplay()
+        self._docs_text_display = doc_view = SimpleTextDisplay()
+        doc_view.get_buffer().create_tag('b', weight=pango.WEIGHT_BOLD)
         self._docs_box = gtk.ScrolledWindow()
         self._docs_box.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
         self._docs_box.add_with_viewport(self._docs_text_display)
@@ -200,10 +201,43 @@ class PropsDialog(gtk.Dialog):
         messages = '\n\n'.join(self._block.get_error_messages())
         self._error_messages_text_display.set_text(messages)
         # update the docs box
-        self._docs_text_display.set_text(self._block.get_doc())
+        self._update_docs_page()
         # update the generated code
         self._update_generated_code_page()
 
+    def _update_docs_page(self):
+        """Show documentation from XML and try to display best matching docstring"""
+        buffer = self._docs_text_display.get_buffer()
+        buffer.delete(buffer.get_start_iter(), buffer.get_end_iter())
+        pos = buffer.get_end_iter()
+
+        docstrings = self._block.get_doc()
+        if not docstrings:
+            return
+
+        # show documentation string from block xml
+        from_xml = docstrings.pop('', '')
+        for line in from_xml.splitlines():
+            if line.lstrip() == line and line.endswith(':'):
+                buffer.insert_with_tags_by_name(pos, line + '\n', 'b')
+            else:
+                buffer.insert(pos, line + '\n')
+        if from_xml:
+            buffer.insert(pos, '\n')
+
+        # if given the current parameters an exact match can be made
+        block_constructor = self._block.get_make().rsplit('.', 2)[-1]
+        block_class = block_constructor.partition('(')[0].strip()
+        if block_class in docstrings:
+            docstrings = {block_class: docstrings[block_class]}
+
+        # show docstring(s) extracted from python sources
+        for cls_name, docstring in docstrings.iteritems():
+            buffer.insert_with_tags_by_name(pos, cls_name + '\n', 'b')
+            buffer.insert(pos, docstring + '\n\n')
+        pos.backward_chars(2)
+        buffer.delete(pos, buffer.get_end_iter())
+
     def _update_generated_code_page(self):
         if not self._code_text_display:
             return  # user disabled code preview
-- 
cgit v1.2.3