Selaa lähdekoodia

wxGUI/querying: enable to redirect output to console

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@58038 15284696-431f-4ddb-bdfa-cd5b030d7da7
Anna Petrášová 11 vuotta sitten
vanhempi
commit
fbbad6a199
2 muutettua tiedostoa jossa 45 lisäystä ja 6 poistoa
  1. 36 6
      gui/wxpython/gui_core/query.py
  2. 9 0
      gui/wxpython/mapdisp/frame.py

+ 36 - 6
gui/wxpython/gui_core/query.py

@@ -26,12 +26,17 @@ from core.utils import _
 from gui_core.treeview import TreeListView
 from gui_core.treeview import TreeListView
 from core.treemodel import TreeModel, DictNode
 from core.treemodel import TreeModel, DictNode
 
 
+from grass.pydispatch.signal import Signal
+
 class QueryDialog(wx.Dialog):
 class QueryDialog(wx.Dialog):
     def __init__(self, parent, data = None):
     def __init__(self, parent, data = None):
         wx.Dialog.__init__(self, parent, id = wx.ID_ANY,
         wx.Dialog.__init__(self, parent, id = wx.ID_ANY,
                            title = _("Query results"),
                            title = _("Query results"),
                            size = (420, 400),
                            size = (420, 400),
                            style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)
                            style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)
+        # send query output to console
+        self.redirectOutput = Signal('QueryDialog.redirectOutput')
+
         self.data = data
         self.data = data
 
 
         self.panel = wx.Panel(self, id = wx.ID_ANY)
         self.panel = wx.Panel(self, id = wx.ID_ANY)
@@ -61,13 +66,17 @@ class QueryDialog(wx.Dialog):
         copy = wx.Button(self.panel, id = wx.ID_ANY, label = _("Copy all to clipboard"))
         copy = wx.Button(self.panel, id = wx.ID_ANY, label = _("Copy all to clipboard"))
         copy.Bind(wx.EVT_BUTTON, self.Copy)
         copy.Bind(wx.EVT_BUTTON, self.Copy)
         self.Bind(wx.EVT_CLOSE, self.OnClose)
         self.Bind(wx.EVT_CLOSE, self.OnClose)
+        self.redirect = wx.CheckBox(self.panel, label=_("Redirect to console"))
+        self.redirect.SetValue(False)
+        self.redirect.Bind(wx.EVT_CHECKBOX, lambda evt: self._onRedirect(evt.IsChecked()))
 
 
         hbox = wx.BoxSizer(wx.HORIZONTAL)
         hbox = wx.BoxSizer(wx.HORIZONTAL)
+        hbox.Add(item=self.redirect, proportion=0, flag=wx.EXPAND | wx.RIGHT, border=5)
         hbox.AddStretchSpacer(1)
         hbox.AddStretchSpacer(1)
-        hbox.Add(item = copy, proportion = 0, flag = wx.EXPAND | wx.RIGHT, border = 5)
-        hbox.Add(item = close, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 0)
+        hbox.Add(item=copy, proportion=0, flag=wx.EXPAND | wx.RIGHT, border=5)
+        hbox.Add(item=close, proportion=0, flag=wx.EXPAND | wx.ALL, border=0)
 
 
-        self.mainSizer.Add(item = hbox, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 5)
+        self.mainSizer.Add(item=hbox, proportion=0, flag=wx.EXPAND | wx.ALL, border=5)
         self.panel.SetSizer(self.mainSizer)
         self.panel.SetSizer(self.mainSizer)
         self.mainSizer.Fit(self.panel)
         self.mainSizer.Fit(self.panel)
         # for Windows
         # for Windows
@@ -80,6 +89,9 @@ class QueryDialog(wx.Dialog):
         self.tree.SetModel(self._model)
         self.tree.SetModel(self._model)
         self.tree.SetExpansionState(state)
         self.tree.SetExpansionState(state)
 
 
+        if self.redirect.IsChecked():
+            self.redirectOutput.emit(output=self._textToRedirect())
+
     def Copy(self, event):
     def Copy(self, event):
         text = printResults(self._model, self._colNames[1])
         text = printResults(self._model, self._colNames[1])
         self._copyText(text)
         self._copyText(text)
@@ -127,6 +139,22 @@ class QueryDialog(wx.Dialog):
         for id in ids:
         for id in ids:
             self.Unbind(wx.EVT_MENU, id=id)
             self.Unbind(wx.EVT_MENU, id=id)
 
 
+    def _onRedirect(self, redirect):
+        """!Emits instructions to redirect query results.
+        
+        @param redirect True to start redirecting, False to stop        
+        """
+        if redirect:
+            self.redirectOutput.emit(output=_("Query results:"), style='cmd')
+            self.redirectOutput.emit(output=self._textToRedirect())
+        else:
+            self.redirectOutput.emit(output=_(" "), style='cmd')
+
+    def _textToRedirect(self):
+        text = printResults(self._model, self._colNames[1])
+        text += '\n' + "-"* 50 + '\n'
+        return text
+
     def _cutLabel(self, label):
     def _cutLabel(self, label):
         limit = 15
         limit = 15
         if len(label) > limit:
         if len(label) > limit:
@@ -143,6 +171,8 @@ class QueryDialog(wx.Dialog):
             wx.TheClipboard.Close()
             wx.TheClipboard.Close()
 
 
     def OnClose(self, event):
     def OnClose(self, event):
+        if self.redirect.IsChecked():
+            self._onRedirect(False)
         self.Destroy()
         self.Destroy()
         event.Skip()
         event.Skip()
 
 
@@ -178,7 +208,8 @@ def printResults(model, valueCol):
     @param valueCol column name with value to be printed
     @param valueCol column name with value to be printed
     """
     """
     def printTree(node, textList, valueCol, indent=0):
     def printTree(node, textList, valueCol, indent=0):
-        textList.append(indent*' ' + node.label + ': ' + node.data.get(valueCol, ''))
+        if node.data.get(valueCol, '') or node.children:
+            textList.append(indent*' ' + node.label + ': ' + node.data.get(valueCol, ''))
         for child in node.children:
         for child in node.children:
             printTree(node=child, textList=textList, valueCol=valueCol, indent=indent + 2)
             printTree(node=child, textList=textList, valueCol=valueCol, indent=indent + 2)
     
     
@@ -194,8 +225,7 @@ def PrepareQueryResults(coordinates, result):
     Adds coordinates, improves vector results tree structure.
     Adds coordinates, improves vector results tree structure.
     """
     """
     data = []
     data = []
-    data.append({_("east"): coordinates[0]})
-    data.append({_("north"): coordinates[1]})
+    data.append({_("east, north"): ", ".join(map(str, coordinates))})
     for part in result:
     for part in result:
         if 'Map' in part:
         if 'Map' in part:
             itemText = part['Map']
             itemText = part['Map']

+ 9 - 0
gui/wxpython/mapdisp/frame.py

@@ -726,12 +726,21 @@ class MapFrame(SingleMapFrame):
         else:
         else:
             self.dialogs['query'] = QueryDialog(parent = self, data = result)
             self.dialogs['query'] = QueryDialog(parent = self, data = result)
             self.dialogs['query'].Bind(wx.EVT_CLOSE, self._oncloseQueryDialog)
             self.dialogs['query'].Bind(wx.EVT_CLOSE, self._oncloseQueryDialog)
+            self.dialogs['query'].redirectOutput.connect(self._onRedirectQueryOutput)
             self.dialogs['query'].Show()
             self.dialogs['query'].Show()
 
 
     def _oncloseQueryDialog(self, event):
     def _oncloseQueryDialog(self, event):
         self.dialogs['query'] = None
         self.dialogs['query'] = None
         event.Skip()
         event.Skip()
 
 
+    def _onRedirectQueryOutput(self, output, style='log'):
+        """!Writes query output into console"""
+        # TODO: fix switching tab
+        if style == 'log':
+            self._giface.WriteLog(output)
+        elif style == 'cmd':
+            self._giface.WriteCmdLog(output)
+
     def _queryHighlight(self, vectQuery):
     def _queryHighlight(self, vectQuery):
         """!Highlight category from query."""
         """!Highlight category from query."""
         cats = name = None
         cats = name = None