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 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
|
"""!
@package gui_core.prompt
@brief wxGUI command prompt
Classes:
- prompt::PromptListCtrl
- prompt::TextCtrlAutoComplete
- prompt::GPrompt
- prompt::GPromptPopUp
- prompt::GPromptSTC
(C) 2009-2011 by the GRASS Development Team
This program is free software under the GNU General Public License
(>=v2). Read the file COPYING that comes with GRASS for details.
@author Martin Landa <landa.martin gmail.com>
@author Michael Barton <michael.barton@asu.edu>
@author Vaclav Petras <wenzeslaus gmail.com> (copy&paste customization)
"""
import os
import sys
import difflib
import codecs
import wx
import wx.stc
import wx.lib.mixins.listctrl as listmix
from grass.script import core as grass
from grass.script import task as gtask
from core import globalvar
from core import utils
from lmgr.menudata import ManagerData
from core.gcmd import EncodeString, DecodeString, GetRealCmd
class PromptListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
"""!PopUp window used by GPromptPopUp"""
def __init__(self, parent, id = wx.ID_ANY, pos = wx.DefaultPosition,
size = wx.DefaultSize, style = 0):
wx.ListCtrl.__init__(self, parent, id, pos, size, style)
listmix.ListCtrlAutoWidthMixin.__init__(self)
class TextCtrlAutoComplete(wx.ComboBox, listmix.ColumnSorterMixin):
"""!Auto complete text area used by GPromptPopUp"""
def __init__ (self, parent, statusbar,
id = wx.ID_ANY, choices = [], **kwargs):
"""!Constructor works just like wx.TextCtrl except you can pass in a
list of choices. You can also change the choice list at any time
by calling setChoices.
Inspired by http://wiki.wxpython.org/TextCtrlAutoComplete
"""
self.statusbar = statusbar
if 'style' in kwargs:
kwargs['style'] = wx.TE_PROCESS_ENTER | kwargs['style']
else:
kwargs['style'] = wx.TE_PROCESS_ENTER
wx.ComboBox.__init__(self, parent, id, **kwargs)
# some variables
self._choices = choices
self._hideOnNoMatch = True
self._module = None # currently selected module
self._choiceType = None # type of choice (module, params, flags, raster, vector ...)
self._screenheight = wx.SystemSettings.GetMetric(wx.SYS_SCREEN_Y)
self._historyItem = 0 # last item
# sort variable needed by listmix
self.itemDataMap = dict()
# widgets
try:
self.dropdown = wx.PopupWindow(self)
except NotImplementedError:
self.Destroy()
raise NotImplementedError
# create the list and bind the events
self.dropdownlistbox = PromptListCtrl(parent = self.dropdown,
style = wx.LC_REPORT | wx.LC_SINGLE_SEL | \
wx.LC_SORT_ASCENDING | wx.LC_NO_HEADER,
pos = wx.Point(0, 0))
listmix.ColumnSorterMixin.__init__(self, 1)
# set choices (list of GRASS modules)
self._choicesCmd = globalvar.grassCmd
self._choicesMap = dict()
for type in ('raster', 'vector'):
self._choicesMap[type] = grass.list_strings(type = type[:4])
# first search for GRASS module
self.SetChoices(self._choicesCmd)
self.SetMinSize(self.GetSize())
# bindings...
self.Bind(wx.EVT_KILL_FOCUS, self.OnControlChanged)
self.Bind(wx.EVT_TEXT, self.OnEnteredText)
self.Bind(wx.EVT_TEXT_ENTER, self.OnRunCmd)
self.Bind(wx.EVT_KEY_DOWN , self.OnKeyDown)
### self.Bind(wx.EVT_LEFT_DOWN, self.OnClick)
# if need drop down on left click
self.dropdown.Bind(wx.EVT_LISTBOX , self.OnListItemSelected, self.dropdownlistbox)
self.dropdownlistbox.Bind(wx.EVT_LEFT_DOWN, self.OnListClick)
self.dropdownlistbox.Bind(wx.EVT_LEFT_DCLICK, self.OnListDClick)
self.dropdownlistbox.Bind(wx.EVT_LIST_COL_CLICK, self.OnListColClick)
self.Bind(wx.EVT_COMBOBOX, self.OnCommandSelect)
def _updateDataList(self, choices):
"""!Update data list"""
# delete, if need, all the previous data
if self.dropdownlistbox.GetColumnCount() != 0:
self.dropdownlistbox.DeleteAllColumns()
self.dropdownlistbox.DeleteAllItems()
# and update the dict
if choices:
for numVal, data in enumerate(choices):
self.itemDataMap[numVal] = data
else:
numVal = 0
self.SetColumnCount(numVal)
def _setListSize(self):
"""!Set list size"""
choices = self._choices
longest = 0
for choice in choices:
longest = max(len(choice), longest)
longest += 3
itemcount = min(len( choices ), 7) + 2
charheight = self.dropdownlistbox.GetCharHeight()
charwidth = self.dropdownlistbox.GetCharWidth()
self.popupsize = wx.Size(charwidth*longest, charheight*itemcount)
self.dropdownlistbox.SetSize(self.popupsize)
self.dropdown.SetClientSize(self.popupsize)
def _showDropDown(self, show = True):
"""!Either display the drop down list (show = True) or hide it
(show = False).
"""
if show:
size = self.dropdown.GetSize()
width, height = self.GetSizeTuple()
x, y = self.ClientToScreenXY(0, height)
if size.GetWidth() != width:
size.SetWidth(width)
self.dropdown.SetSize(size)
self.dropdownlistbox.SetSize(self.dropdown.GetClientSize())
if (y + size.GetHeight()) < self._screenheight:
self.dropdown.SetPosition(wx.Point(x, y))
else:
self.dropdown.SetPosition(wx.Point(x, y - height - size.GetHeight()))
self.dropdown.Show(show)
def _listItemVisible(self):
"""!Moves the selected item to the top of the list ensuring it is
always visible.
"""
toSel = self.dropdownlistbox.GetFirstSelected()
if toSel == -1:
return
self.dropdownlistbox.EnsureVisible(toSel)
def _setModule(self, name):
"""!Set module's choices (flags, parameters)"""
# get module's description
if name in self._choicesCmd and not self._module:
try:
self._module = gtask.parse_interface(name)
except IOError:
self._module = None
# set choices (flags)
self._choicesMap['flag'] = self._module.get_list_flags()
for idx in range(len(self._choicesMap['flag'])):
item = self._choicesMap['flag'][idx]
desc = self._module.get_flag(item)['label']
if not desc:
desc = self._module.get_flag(item)['description']
self._choicesMap['flag'][idx] = '%s (%s)' % (item, desc)
# set choices (parameters)
self._choicesMap['param'] = self._module.get_list_params()
for idx in range(len(self._choicesMap['param'])):
item = self._choicesMap['param'][idx]
desc = self._module.get_param(item)['label']
if not desc:
desc = self._module.get_param(item)['description']
self._choicesMap['param'][idx] = '%s (%s)' % (item, desc)
def _setValueFromSelected(self):
"""!Sets the wx.TextCtrl value from the selected wx.ListCtrl item.
Will do nothing if no item is selected in the wx.ListCtrl.
"""
sel = self.dropdownlistbox.GetFirstSelected()
if sel < 0:
return
if self._colFetch != -1:
col = self._colFetch
else:
col = self._colSearch
itemtext = self.dropdownlistbox.GetItem(sel, col).GetText()
cmd = utils.split(str(self.GetValue()))
if len(cmd) > 0 and cmd[0] in self._choicesCmd:
# -> append text (skip last item)
if self._choiceType == 'param':
itemtext = itemtext.split(' ')[0]
self.SetValue(' '.join(cmd) + ' ' + itemtext + '=')
optType = self._module.get_param(itemtext)['prompt']
if optType in ('raster', 'vector'):
# -> raster/vector map
self.SetChoices(self._choicesMap[optType], optType)
elif self._choiceType == 'flag':
itemtext = itemtext.split(' ')[0]
if len(itemtext) > 1:
prefix = '--'
else:
prefix = '-'
self.SetValue(' '.join(cmd[:-1]) + ' ' + prefix + itemtext)
elif self._choiceType in ('raster', 'vector'):
self.SetValue(' '.join(cmd[:-1]) + ' ' + cmd[-1].split('=', 1)[0] + '=' + itemtext)
else:
# -> reset text
self.SetValue(itemtext + ' ')
# define module
self._setModule(itemtext)
# use parameters as default choices
self._choiceType = 'param'
self.SetChoices(self._choicesMap['param'], type = 'param')
self.SetInsertionPointEnd()
self._showDropDown(False)
def GetListCtrl(self):
"""!Method required by listmix.ColumnSorterMixin"""
return self.dropdownlistbox
def SetChoices(self, choices, type = 'module'):
"""!Sets the choices available in the popup wx.ListBox.
The items will be sorted case insensitively.
@param choices list of choices
@param type type of choices (module, param, flag, raster, vector)
"""
self._choices = choices
self._choiceType = type
self.dropdownlistbox.SetWindowStyleFlag(wx.LC_REPORT | wx.LC_SINGLE_SEL |
wx.LC_SORT_ASCENDING | wx.LC_NO_HEADER)
if not isinstance(choices, list):
self._choices = [ x for x in choices ]
if self._choiceType not in ('raster', 'vector'):
# do not sort raster/vector maps
utils.ListSortLower(self._choices)
self._updateDataList(self._choices)
self.dropdownlistbox.InsertColumn(0, "")
for num, colVal in enumerate(self._choices):
index = self.dropdownlistbox.InsertImageStringItem(sys.maxint, colVal, -1)
self.dropdownlistbox.SetStringItem(index, 0, colVal)
self.dropdownlistbox.SetItemData(index, num)
self._setListSize()
# there is only one choice for both search and fetch if setting a single column:
self._colSearch = 0
self._colFetch = -1
def OnClick(self, event):
"""Left mouse button pressed"""
sel = self.dropdownlistbox.GetFirstSelected()
if not self.dropdown.IsShown():
if sel > -1:
self.dropdownlistbox.Select(sel)
else:
self.dropdownlistbox.Select(0)
self._listItemVisible()
self._showDropDown()
else:
self.dropdown.Hide()
def OnCommandSelect(self, event):
"""!Command selected from history"""
self._historyItem = event.GetSelection() - len(self.GetItems())
self.SetFocus()
def OnListClick(self, evt):
"""!Left mouse button pressed"""
toSel, flag = self.dropdownlistbox.HitTest( evt.GetPosition() )
#no values on poition, return
if toSel == -1: return
self.dropdownlistbox.Select(toSel)
def OnListDClick(self, evt):
"""!Mouse button double click"""
self._setValueFromSelected()
def OnListColClick(self, evt):
"""!Left mouse button pressed on column"""
col = evt.GetColumn()
# reverse the sort
if col == self._colSearch:
self._ascending = not self._ascending
self.SortListItems( evt.GetColumn(), ascending=self._ascending )
self._colSearch = evt.GetColumn()
evt.Skip()
def OnListItemSelected(self, event):
"""!Item selected"""
self._setValueFromSelected()
event.Skip()
def OnEnteredText(self, event):
"""!Text entered"""
text = event.GetString()
if not text:
# control is empty; hide dropdown if shown:
if self.dropdown.IsShown():
self._showDropDown(False)
event.Skip()
return
try:
cmd = utils.split(str(text))
except ValueError, e:
self.statusbar.SetStatusText(str(e))
cmd = text.split(' ')
pattern = str(text)
if len(cmd) > 0 and cmd[0] in self._choicesCmd and not self._module:
self._setModule(cmd[0])
elif len(cmd) > 1 and cmd[0] in self._choicesCmd:
if self._module:
if len(cmd[-1].split('=', 1)) == 1:
# new option
if cmd[-1][0] == '-':
# -> flags
self.SetChoices(self._choicesMap['flag'], type = 'flag')
pattern = cmd[-1].lstrip('-')
else:
# -> options
self.SetChoices(self._choicesMap['param'], type = 'param')
pattern = cmd[-1]
else:
# value
pattern = cmd[-1].split('=', 1)[1]
else:
# search for GRASS modules
if self._module:
# -> switch back to GRASS modules list
self.SetChoices(self._choicesCmd)
self._module = None
self._choiceType = None
self._choiceType
self._choicesMap
found = False
choices = self._choices
for numCh, choice in enumerate(choices):
if choice.lower().startswith(pattern):
found = True
if found:
self._showDropDown(True)
item = self.dropdownlistbox.GetItem(numCh)
toSel = item.GetId()
self.dropdownlistbox.Select(toSel)
break
if not found:
self.dropdownlistbox.Select(self.dropdownlistbox.GetFirstSelected(), False)
if self._hideOnNoMatch:
self._showDropDown(False)
if self._module and '=' not in cmd[-1]:
message = ''
if cmd[-1][0] == '-': # flag
message = _("Warning: flag <%(flag)s> not found in '%(module)s'") % \
{ 'flag' : cmd[-1][1:], 'module' : self._module.name }
else: # option
message = _("Warning: option <%(param)s> not found in '%(module)s'") % \
{ 'param' : cmd[-1], 'module' : self._module.name }
self.statusbar.SetStatusText(message)
if self._module and len(cmd[-1]) == 2 and cmd[-1][-2] == '=':
optType = self._module.get_param(cmd[-1][:-2])['prompt']
if optType in ('raster', 'vector'):
# -> raster/vector map
self.SetChoices(self._choicesMap[optType], optType)
self._listItemVisible()
event.Skip()
def OnKeyDown (self, event):
"""!Do some work when the user press on the keys: up and down:
move the cursor left and right: move the search
"""
skip = True
sel = self.dropdownlistbox.GetFirstSelected()
visible = self.dropdown.IsShown()
KC = event.GetKeyCode()
if KC == wx.WXK_RIGHT:
# right -> show choices
if sel < (self.dropdownlistbox.GetItemCount() - 1):
self.dropdownlistbox.Select(sel + 1)
self._listItemVisible()
self._showDropDown()
skip = False
elif KC == wx.WXK_UP:
if visible:
if sel > 0:
self.dropdownlistbox.Select(sel - 1)
self._listItemVisible()
self._showDropDown()
skip = False
else:
self._historyItem -= 1
try:
self.SetValue(self.GetItems()[self._historyItem])
except IndexError:
self._historyItem += 1
elif KC == wx.WXK_DOWN:
if visible:
if sel < (self.dropdownlistbox.GetItemCount() - 1):
self.dropdownlistbox.Select(sel + 1)
self._listItemVisible()
self._showDropDown()
skip = False
else:
if self._historyItem < -1:
self._historyItem += 1
self.SetValue(self.GetItems()[self._historyItem])
if visible:
if event.GetKeyCode() == wx.WXK_RETURN:
self._setValueFromSelected()
skip = False
if event.GetKeyCode() == wx.WXK_ESCAPE:
self._showDropDown(False)
skip = False
if skip:
event.Skip()
def OnControlChanged(self, event):
"""!Control changed"""
if self.IsShown():
self._showDropDown(False)
event.Skip()
class GPrompt(object):
"""!Abstract class for interactive wxGUI prompt
See subclass GPromptPopUp and GPromptSTC.
"""
def __init__(self, parent):
self.parent = parent # GMConsole
self.panel = self.parent.GetPanel()
if self.parent.parent.GetName() not in ("LayerManager", "Modeler"):
self.standAlone = True
else:
self.standAlone = False
# dictionary of modules (description, keywords, ...)
if not self.standAlone:
if self.parent.parent.GetName() == 'Modeler':
self.moduleDesc = ManagerData().GetModules()
else:
self.moduleDesc = parent.parent.menubar.GetData().GetModules()
self.moduleList = self._getListOfModules()
self.mapList = self._getListOfMaps()
self.mapsetList = utils.ListOfMapsets()
else:
self.moduleDesc = self.moduleList = self.mapList = None
# auto complete items
self.autoCompList = list()
self.autoCompFilter = None
# command description (gtask.grassTask)
self.cmdDesc = None
self.cmdbuffer = self._readHistory()
self.cmdindex = len(self.cmdbuffer)
# list of traced commands
self.commands = list()
def _readHistory(self):
"""!Get list of commands from history file"""
hist = list()
env = grass.gisenv()
try:
fileHistory = codecs.open(os.path.join(env['GISDBASE'],
env['LOCATION_NAME'],
env['MAPSET'],
'.bash_history'),
encoding = 'utf-8', mode = 'r', errors='replace')
except IOError:
return hist
try:
for line in fileHistory.readlines():
hist.append(line.replace('\n', ''))
finally:
fileHistory.close()
return hist
def GetCommandDesc(self, cmd):
"""!Get description for given command"""
if cmd in self.moduleDesc:
return self.moduleDesc[cmd]['desc']
return ''
def GetCommandItems(self):
"""!Get list of available commands"""
items = list()
if self.autoCompFilter is not None:
mList = self.autoCompFilter
else:
mList = self.moduleList
if not mList:
return items
prefixes = mList.keys()
prefixes.sort()
for prefix in prefixes:
for command in mList[prefix]:
name = prefix + '.' + command
if name not in items:
items.append(name)
items.sort()
return items
def _getListOfModules(self):
"""!Get list of modules"""
result = dict()
for module in globalvar.grassCmd:
try:
group, name = module.split('.',1)
except ValueError:
continue # TODO
if group not in result:
result[group] = list()
result[group].append(name)
# for better auto-completion:
# not only result['r']={...,'colors.out',...}, but also result['r.colors']={'out',...}
for i in range(len(name.split('.'))-1):
group = '.'.join([group,name.split('.',1)[0]])
name = name.split('.',1)[1]
if group not in result:
result[group] = list()
result[group].append(name)
# sort list of names
for group in result.keys():
result[group].sort()
return result
def _getListOfMaps(self):
"""!Get list of maps"""
result = dict()
result['raster'] = grass.list_strings('rast')
result['vector'] = grass.list_strings('vect')
return result
def _runCmd(self, cmdString):
"""!Run command
@param cmdString command to run (given as a string)
"""
if self.parent.GetName() == "ModelerDialog":
self.parent.OnOk(None)
return
if not cmdString or self.standAlone:
return
if cmdString[:2] == 'd.' and not self.parent.parent.curr_page:
self.parent.parent.NewDisplay(show = True)
self.commands.append(cmdString) # trace commands
# parse command into list
try:
cmd = utils.split(str(cmdString))
except UnicodeError:
cmd = utils.split(EncodeString((cmdString)))
cmd = map(DecodeString, cmd)
# send the command list to the processor
if cmd[0] in ('r.mapcalc', 'r3.mapcalc') and len(cmd) == 1:
self.parent.parent.OnMapCalculator(event = None, cmd = cmd)
else:
self.parent.RunCmd(cmd)
# add command to history & clean prompt
self.UpdateCmdHistory(cmd)
self.OnCmdErase(None)
self.parent.parent.statusbar.SetStatusText('')
def OnUpdateStatusBar(self, event):
"""!Update Layer Manager status bar"""
if self.standAlone:
return
if event is None:
self.parent.parent.statusbar.SetStatusText("")
else:
self.parent.parent.statusbar.SetStatusText(_("Type GRASS command and run by pressing ENTER"))
event.Skip()
def GetPanel(self):
"""!Get main widget panel"""
return self.panel
def GetInput(self):
"""!Get main prompt widget"""
return self.input
def SetFilter(self, data, module = True):
"""!Set filter
@param data data dict
@param module True to filter modules, otherwise data
"""
if module:
if data:
self.moduleList = data
else:
self.moduleList = self._getListOfModules()
else:
if data:
self.dataList = data
else:
self.dataList = self._getListOfMaps()
def GetCommands(self):
"""!Get list of launched commands"""
return self.commands
def ClearCommands(self):
"""!Clear list of commands"""
del self.commands[:]
class GPromptPopUp(GPrompt, TextCtrlAutoComplete):
"""!Interactive wxGUI prompt - popup version"""
def __init__(self, parent):
GPrompt.__init__(self, parent)
### todo: fix TextCtrlAutoComplete to work also on Macs
### reason: missing wx.PopupWindow()
try:
TextCtrlAutoComplete.__init__(self, parent = self.panel, id = wx.ID_ANY,
value = "",
style = wx.TE_LINEWRAP | wx.TE_PROCESS_ENTER,
statusbar = self.parent.parent.statusbar)
self.SetItems(self._readHistory())
except NotImplementedError:
# wx.PopupWindow may be not available in wxMac
# see http://trac.wxwidgets.org/ticket/9377
wx.TextCtrl.__init__(parent = self.panel, id = wx.ID_ANY,
value = "",
style=wx.TE_LINEWRAP | wx.TE_PROCESS_ENTER,
size = (-1, 25))
self.searchBy.Enable(False)
self.search.Enable(False)
self.SetFont(wx.Font(10, wx.FONTFAMILY_MODERN, wx.NORMAL, wx.NORMAL, 0, ''))
wx.CallAfter(self.SetInsertionPoint, 0)
# bidnings
self.Bind(wx.EVT_TEXT_ENTER, self.OnRunCmd)
self.Bind(wx.EVT_TEXT, self.OnUpdateStatusBar)
def OnCmdErase(self, event):
"""!Erase command prompt"""
self.input.SetValue('')
def OnRunCmd(self, event):
"""!Run command"""
self._runCmd(event.GetString())
class GPromptSTC(GPrompt, wx.stc.StyledTextCtrl):
"""!Styled wxGUI prompt with autocomplete and calltips"""
def __init__(self, parent, id = wx.ID_ANY, margin = False):
GPrompt.__init__(self, parent)
wx.stc.StyledTextCtrl.__init__(self, self.panel, id)
#
# styles
#
self.SetWrapMode(True)
self.SetUndoCollection(True)
#
# create command and map lists for autocompletion
#
self.AutoCompSetIgnoreCase(False)
#
# line margins
#
# TODO print number only from cmdlog
self.SetMarginWidth(1, 0)
self.SetMarginWidth(2, 0)
if margin:
self.SetMarginType(0, wx.stc.STC_MARGIN_NUMBER)
self.SetMarginWidth(0, 30)
else:
self.SetMarginWidth(0, 0)
#
# miscellaneous
#
self.SetViewWhiteSpace(False)
self.SetUseTabs(False)
self.UsePopUp(True)
self.SetSelBackground(True, "#FFFF00")
self.SetUseHorizontalScrollBar(True)
#
# bindings
#
self.Bind(wx.EVT_WINDOW_DESTROY, self.OnDestroy)
self.Bind(wx.EVT_CHAR, self.OnChar)
self.Bind(wx.EVT_KEY_DOWN, self.OnKeyPressed)
self.Bind(wx.stc.EVT_STC_AUTOCOMP_SELECTION, self.OnItemSelected)
self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemChanged)
def OnTextSelectionChanged(self, event):
"""!Copy selected text to clipboard and skip event.
The same function is in GMStc class (goutput.py).
"""
self.Copy()
event.Skip()
def OnItemChanged(self, event):
"""!Change text in statusbar
if the item selection in the auto-completion list is changed"""
# list of commands
if self.toComplete['entity'] == 'command':
item = self.toComplete['cmd'].rpartition('.')[0] + '.' + self.autoCompList[event.GetIndex()]
try:
desc = self.moduleDesc[item]['desc']
except KeyError:
desc = ''
self.ShowStatusText(desc)
# list of flags
elif self.toComplete['entity'] == 'flags':
desc = self.cmdDesc.get_flag(self.autoCompList[event.GetIndex()])['description']
self.ShowStatusText(desc)
# list of parameters
elif self.toComplete['entity'] == 'params':
item = self.cmdDesc.get_param(self.autoCompList[event.GetIndex()])
desc = item['name'] + '=' + item['type']
if not item['required']:
desc = '[' + desc + ']'
desc += ': ' + item['description']
self.ShowStatusText(desc)
# list of flags and commands
elif self.toComplete['entity'] == 'params+flags':
if self.autoCompList[event.GetIndex()][0] == '-':
desc = self.cmdDesc.get_flag(self.autoCompList[event.GetIndex()].strip('-'))['description']
else:
item = self.cmdDesc.get_param(self.autoCompList[event.GetIndex()])
desc = item['name'] + '=' + item['type']
if not item['required']:
desc = '[' + desc + ']'
desc += ': ' + item['description']
self.ShowStatusText(desc)
else:
self.ShowStatusText('')
def OnItemSelected(self, event):
"""!Item selected from the list"""
lastWord = self.GetWordLeft()
# to insert selection correctly if selected word partly matches written text
match = difflib.SequenceMatcher(None, event.GetText(), lastWord)
matchTuple = match.find_longest_match(0, len(event.GetText()), 0, len(lastWord))
compl = event.GetText()[matchTuple[2]:]
text = self.GetTextLeft() + compl
# add space or '=' at the end
end = '='
for char in ('.','-','='):
if text.split(' ')[-1].find(char) >= 0:
end = ' '
compl += end
text += end
self.AddText(compl)
pos = len(text)
self.SetCurrentPos(pos)
cmd = text.strip().split(' ')[0]
if not self.cmdDesc or cmd != self.cmdDesc.get_name():
if cmd in ('r.mapcalc', 'v.type'):
cmd = cmd + '_wrapper'
if cmd in ('r.mapcalc', 'r3.mapcalc') and \
self.parent.parent.GetName() == 'LayerManager':
self.parent.parent.OnMapCalculator(event = None, cmd = [cmd])
# add command to history & clean prompt
self.UpdateCmdHistory([cmd])
self.OnCmdErase(None)
else:
try:
self.cmdDesc = gtask.parse_interface(GetRealCmd(cmd))
except IOError:
self.cmdDesc = None
def UpdateCmdHistory(self, cmd):
"""!Update command history
@param cmd command given as a list
"""
# add command to history
self.cmdbuffer.append(' '.join(cmd))
# keep command history to a managable size
if len(self.cmdbuffer) > 200:
del self.cmdbuffer[0]
self.cmdindex = len(self.cmdbuffer)
def EntityToComplete(self):
"""!Determines which part of command (flags, parameters) should
be completed at current cursor position"""
entry = self.GetTextLeft()
toComplete = dict(cmd=None, entity=None)
try:
cmd = entry.split()[0].strip()
except IndexError:
return toComplete
try:
splitted = utils.split(str(entry))
except ValueError: # No closing quotation error
return toComplete
if len(splitted) > 0 and cmd in globalvar.grassCmd:
toComplete['cmd'] = cmd
if entry[-1] == ' ':
words = entry.split(' ')
if any(word.startswith('-') for word in words):
toComplete['entity'] = 'params'
else:
toComplete['entity'] = 'params+flags'
else:
# get word left from current position
word = self.GetWordLeft(withDelimiter = True)
if word[0] == '=' and word[-1] == '@':
toComplete['entity'] = 'mapsets'
elif word[0] == '=':
# get name of parameter
paramName = self.GetWordLeft(withDelimiter = False, ignoredDelimiter = '=').strip('=')
if paramName:
try:
param = self.cmdDesc.get_param(paramName)
except (ValueError, AttributeError):
return toComplete
else:
return toComplete
if param['values']:
toComplete['entity'] = 'param values'
elif param['prompt'] == 'raster' and param['element'] == 'cell':
toComplete['entity'] = 'raster map'
elif param['prompt'] == 'vector' and param['element'] == 'vector':
toComplete['entity'] = 'vector map'
elif word[0] == '-':
toComplete['entity'] = 'flags'
elif word[0] == ' ':
toComplete['entity'] = 'params'
else:
toComplete['entity'] = 'command'
toComplete['cmd'] = cmd
return toComplete
def GetWordLeft(self, withDelimiter = False, ignoredDelimiter = None):
"""!Get word left from current cursor position. The beginning
of the word is given by space or chars: .,-=
@param withDelimiter returns the word with the initial delimeter
@param ignoredDelimiter finds the word ignoring certain delimeter
"""
textLeft = self.GetTextLeft()
parts = list()
if ignoredDelimiter is None:
ignoredDelimiter = ''
for char in set(' .,-=') - set(ignoredDelimiter):
if not withDelimiter:
delimiter = ''
else:
delimiter = char
parts.append(delimiter + textLeft.rpartition(char)[2])
return min(parts, key=lambda x: len(x))
def ShowList(self):
"""!Show sorted auto-completion list if it is not empty"""
if len(self.autoCompList) > 0:
self.autoCompList.sort()
self.AutoCompShow(lenEntered = 0, itemList = ' '.join(self.autoCompList))
def OnKeyPressed(self, event):
"""!Key pressed capture special treatment for tabulator to show help"""
pos = self.GetCurrentPos()
if event.GetKeyCode() == wx.WXK_TAB:
# show GRASS command calltips (to hide press 'ESC')
entry = self.GetTextLeft()
try:
cmd = entry.split()[0].strip()
except IndexError:
cmd = ''
if cmd not in globalvar.grassCmd:
return
info = gtask.command_info(GetRealCmd(cmd))
self.CallTipSetBackground("#f4f4d1")
self.CallTipSetForeground("BLACK")
self.CallTipShow(pos, info['usage'] + '\n\n' + info['description'])
elif event.GetKeyCode() in (wx.WXK_RETURN, wx.WXK_NUMPAD_ENTER) and \
not self.AutoCompActive():
# run command on line when <return> is pressed
self._runCmd(self.GetCurLine()[0].strip())
elif event.GetKeyCode() in [wx.WXK_UP, wx.WXK_DOWN] and \
not self.AutoCompActive():
# Command history using up and down
if len(self.cmdbuffer) < 1:
return
self.DocumentEnd()
# move through command history list index values
if event.GetKeyCode() == wx.WXK_UP:
self.cmdindex = self.cmdindex - 1
if event.GetKeyCode() == wx.WXK_DOWN:
self.cmdindex = self.cmdindex + 1
if self.cmdindex < 0:
self.cmdindex = 0
if self.cmdindex > len(self.cmdbuffer) - 1:
self.cmdindex = len(self.cmdbuffer) - 1
try:
txt = self.cmdbuffer[self.cmdindex]
except KeyError:
txt = ''
# clear current line and insert command history
self.DelLineLeft()
self.DelLineRight()
pos = self.GetCurrentPos()
self.InsertText(pos, txt)
self.LineEnd()
self.ShowStatusText('')
else:
event.Skip()
def OnChar(self, event):
"""!Key char capture for autocompletion, calltips, and command history
@todo event.ControlDown() for manual autocomplete
"""
# keycodes used: "." = 46, "=" = 61, "-" = 45
pos = self.GetCurrentPos()
# complete command after pressing '.'
if event.GetKeyCode() == 46:
self.autoCompList = list()
entry = self.GetTextLeft()
self.InsertText(pos, '.')
self.CharRight()
self.toComplete = self.EntityToComplete()
try:
if self.toComplete['entity'] == 'command':
for command in globalvar.grassCmd:
try:
if command.find(self.toComplete['cmd']) == 0:
dotNumber = list(self.toComplete['cmd']).count('.')
self.autoCompList.append(command.split('.',dotNumber)[-1])
except UnicodeDecodeError, e: # TODO: fix it
sys.stderr.write(DecodeString(command) + ": " + unicode(e))
except (KeyError, TypeError):
return
self.ShowList()
# complete flags after pressing '-'
elif (event.GetKeyCode() == 45) \
or event.GetKeyCode() == wx.WXK_NUMPAD_SUBTRACT \
or event.GetKeyCode() == wx.WXK_SUBTRACT:
self.autoCompList = list()
entry = self.GetTextLeft()
self.InsertText(pos, '-')
self.CharRight()
self.toComplete = self.EntityToComplete()
if self.toComplete['entity'] == 'flags' and self.cmdDesc:
if self.GetTextLeft()[-2:] == ' -': # complete e.g. --quite
for flag in self.cmdDesc.get_options()['flags']:
if len(flag['name']) == 1:
self.autoCompList.append(flag['name'])
else:
for flag in self.cmdDesc.get_options()['flags']:
if len(flag['name']) > 1:
self.autoCompList.append(flag['name'])
self.ShowList()
# complete map or values after parameter
elif event.GetKeyCode() == 61:
self.autoCompList = list()
self.InsertText(pos, '=')
self.CharRight()
self.toComplete = self.EntityToComplete()
if self.toComplete['entity'] == 'raster map':
self.autoCompList = self.mapList['raster']
elif self.toComplete['entity'] == 'vector map':
self.autoCompList = self.mapList['vector']
elif self.toComplete['entity'] == 'param values':
param = self.GetWordLeft(withDelimiter = False, ignoredDelimiter='=').strip(' =')
self.autoCompList = self.cmdDesc.get_param(param)['values']
self.ShowList()
# complete mapset ('@')
elif event.GetKeyCode() == 64:
self.autoCompList = list()
self.InsertText(pos, '@')
self.CharRight()
self.toComplete = self.EntityToComplete()
if self.toComplete['entity'] == 'mapsets':
self.autoCompList = self.mapsetList
self.ShowList()
# complete after pressing CTRL + Space
elif event.GetKeyCode() == wx.WXK_SPACE and event.ControlDown():
self.autoCompList = list()
self.toComplete = self.EntityToComplete()
if self.toComplete is None:
return
#complete command
if self.toComplete['entity'] == 'command':
for command in globalvar.grassCmd:
if command.find(self.toComplete['cmd']) == 0:
dotNumber = list(self.toComplete['cmd']).count('.')
self.autoCompList.append(command.split('.',dotNumber)[-1])
# complete flags in such situations (| is cursor):
# r.colors -| ...w, q, l
# r.colors -w| ...w, q, l
elif self.toComplete['entity'] == 'flags' and self.cmdDesc:
for flag in self.cmdDesc.get_options()['flags']:
if len(flag['name']) == 1:
self.autoCompList.append(flag['name'])
# complete parameters in such situations (| is cursor):
# r.colors -w | ...color, map, rast, rules
# r.colors col| ...color
elif self.toComplete['entity'] == 'params' and self.cmdDesc:
for param in self.cmdDesc.get_options()['params']:
if param['name'].find(self.GetWordLeft(withDelimiter=False)) == 0:
self.autoCompList.append(param['name'])
# complete flags or parameters in such situations (| is cursor):
# r.colors | ...-w, -q, -l, color, map, rast, rules
# r.colors color=grey | ...-w, -q, -l, color, map, rast, rules
elif self.toComplete['entity'] == 'params+flags' and self.cmdDesc:
self.autoCompList = list()
for param in self.cmdDesc.get_options()['params']:
self.autoCompList.append(param['name'])
for flag in self.cmdDesc.get_options()['flags']:
if len(flag['name']) == 1:
self.autoCompList.append('-' + flag['name'])
else:
self.autoCompList.append('--' + flag['name'])
self.ShowList()
# complete map or values after parameter
# r.buffer input=| ...list of raster maps
# r.buffer units=| ... feet, kilometers, ...
elif self.toComplete['entity'] == 'raster map':
self.autoCompList = list()
self.autoCompList = self.mapList['raster']
elif self.toComplete['entity'] == 'vector map':
self.autoCompList = list()
self.autoCompList = self.mapList['vector']
elif self.toComplete['entity'] == 'param values':
self.autoCompList = list()
param = self.GetWordLeft(withDelimiter = False, ignoredDelimiter='=').strip(' =')
self.autoCompList = self.cmdDesc.get_param(param)['values']
self.ShowList()
elif event.GetKeyCode() == wx.WXK_SPACE:
items = self.GetTextLeft().split()
if len(items) == 1:
cmd = items[0].strip()
if cmd in globalvar.grassCmd and \
cmd != 'r.mapcalc' and \
(not self.cmdDesc or cmd != self.cmdDesc.get_name()):
try:
self.cmdDesc = gtask.parse_interface(GetRealCmd(cmd))
except IOError:
self.cmdDesc = None
event.Skip()
else:
event.Skip()
def ShowStatusText(self, text):
"""!Sets statusbar text, if it's too long, it is cut off"""
maxLen = self.parent.parent.statusbar.GetFieldRect(0).GetWidth()/ 7 # any better way?
if len(text) < maxLen:
self.parent.parent.statusbar.SetStatusText(text)
else:
self.parent.parent.statusbar.SetStatusText(text[:maxLen]+'...')
def GetTextLeft(self):
"""!Returns all text left of the caret"""
pos = self.GetCurrentPos()
self.HomeExtend()
entry = self.GetSelectedText()
self.SetCurrentPos(pos)
return entry
def OnDestroy(self, event):
"""!The clipboard contents can be preserved after
the app has exited"""
wx.TheClipboard.Flush()
event.Skip()
def OnCmdErase(self, event):
"""!Erase command prompt"""
self.Home()
self.DelLineRight()
|