1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750 |
- """!
- @package gmodeler.py
- @brief wxGUI Graphical Modeler for creating, editing, and managing models
- Classes:
- - Model
- - ModelFrame
- - ModelCanvas
- - ModelObject
- - ModelAction
- - ModelSearchDialog
- - ModelData
- - ModelDataDialog
- - ModelRelation
- - ModelRelationDialog
- - ProcessModelFile
- - WriteModelFile
- - PreferencesDialog
- - PropertiesDialog
- - ModelParamDialog
- - ModelListCtrl
- - VariablePanel
- - ValiableListCtrl
- - ModelItem
- - ModelItemDialog
- - ModelLoop
- - ModelLoopDialog
- - ItemPanel
- - ItemListCtrl
- - ItemCheckListCtrl
- - ModelCondition
- - ModelConditionDialog
- - WritePythonFile
- (C) 2010-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>
- """
- import os
- import sys
- import time
- import traceback
- import getpass
- import stat
- import textwrap
- import tempfile
- import copy
- import re
- try:
- import xml.etree.ElementTree as etree
- except ImportError:
- import elementtree.ElementTree as etree # Python <= 2.4
- import globalvar
- import wx
- import wx.lib.ogl as ogl
- import wx.lib.flatnotebook as FN
- import wx.lib.colourselect as csel
- import wx.lib.mixins.listctrl as listmix
- import menu
- import menudata
- import toolbars
- import menuform
- import prompt
- import utils
- import goutput
- import gselect
- from debug import Debug
- from gcmd import GMessage, GException, GWarning, GError, RunCommand
- from gdialogs import ElementDialog, GetImageHandlers
- from preferences import PreferencesBaseDialog, globalSettings as UserSettings
- from ghelp import SearchModuleWindow
- from grass.script import core as grass
- from grass.script import task as gtask
- class Model(object):
- """!Class representing the model"""
- def __init__(self, canvas = None):
- self.items = list() # list of actions/loops/...
-
- # model properties
- self.properties = { 'name' : _("model"),
- 'description' : _("Script generated by wxGUI Graphical Modeler."),
- 'author' : getpass.getuser() }
- # model variables
- self.variables = dict()
- self.variablesParams = dict()
-
- self.canvas = canvas
-
- def GetCanvas(self):
- """!Get canvas or None"""
- return self.canvas
-
- def GetItems(self, objType = None):
- """!Get list of model items
- @param objType Object type to filter model objects
- """
- if not objType:
- return self.items
-
- result = list()
- for item in self.items:
- if isinstance(item, objType):
- result.append(item)
-
- return result
- def GetItem(self, aId):
- """!Get item of given id
- @param aId item id
-
- @return Model* instance
- @return None if no item found
- """
- ilist = self.GetItems()
- for item in ilist:
- if item.GetId() == aId:
- return item
-
- return None
- def GetNumItems(self, actionOnly = False):
- """!Get number of items"""
- if actionOnly:
- return len(self.GetItems(objType = ModelAction))
-
- return len(self.GetItems())
- def GetNextId(self):
- """!Get next id (data ignored)
- @return next id to be used (default: 1)
- """
- if len(self.items) < 1:
- return 1
-
- currId = self.items[-1].GetId()
- if currId > 0:
- return currId + 1
-
- return 1
- def GetProperties(self):
- """!Get model properties"""
- return self.properties
- def GetVariables(self, params = False):
- """!Get model variables"""
- if params:
- return self.variablesParams
-
- return self.variables
-
- def SetVariables(self, data):
- """!Set model variables"""
- self.variables = data
-
- def Reset(self):
- """!Reset model"""
- self.items = list()
-
- def RemoveItem(self, item):
- """!Remove item from model
-
- @return list of related items to remove/update
- """
- relList = list()
- upList = list()
-
- if not isinstance(item, ModelData):
- self.items.remove(item)
-
- if isinstance(item, ModelAction):
- for rel in item.GetRelations():
- relList.append(rel)
- data = rel.GetData()
- if len(data.GetRelations()) < 2:
- relList.append(data)
- else:
- upList.append(data)
-
- elif isinstance(item, ModelData):
- for rel in item.GetRelations():
- relList.append(rel)
- if rel.GetFrom() == self:
- relList.append(rel.GetTo())
- else:
- relList.append(rel.GetFrom())
-
- return relList, upList
-
- def FindAction(self, aId):
- """!Find action by id"""
- alist = self.GetItems(objType = ModelAction)
- for action in alist:
- if action.GetId() == aId:
- return action
-
- return None
- def GetData(self):
- """!Get list of data items"""
- result = list()
- dataItems = self.GetItems(objType = ModelData)
-
- for action in self.GetItems(objType = ModelAction):
- for rel in action.GetRelations():
- dataItem = rel.GetData()
- if dataItem not in result:
- result.append(dataItem)
- if dataItem in dataItems:
- dataItems.remove(dataItem)
-
- # standalone data
- if dataItems:
- result += dataItems
-
- return result
- def FindData(self, value, prompt):
- """!Find data item in the model
- @param value value
- @param prompt prompt
- @return ModelData instance
- @return None if not found
- """
- for data in self.GetData():
- if data.GetValue() == value and \
- data.GetPrompt() == prompt:
- return data
-
- return None
-
- def LoadModel(self, filename):
- """!Load model definition stored in GRASS Model XML file (gxm)
-
- @todo Validate against DTD
-
- Raise exception on error.
- """
- dtdFilename = os.path.join(globalvar.ETCWXDIR, "xml", "grass-gxm.dtd")
-
- # parse workspace file
- try:
- gxmXml = ProcessModelFile(etree.parse(filename))
- except StandardError, e:
- raise GException(e)
-
- if self.canvas:
- win = self.canvas.parent
- if gxmXml.pos:
- win.SetPosition(gxmXml.pos)
- if gxmXml.size:
- win.SetSize(gxmXml.size)
-
- # load properties
- self.properties = gxmXml.properties
- self.variables = gxmXml.variables
-
- # load model.GetActions()
- for action in gxmXml.actions:
- actionItem = ModelAction(parent = self,
- x = action['pos'][0],
- y = action['pos'][1],
- width = action['size'][0],
- height = action['size'][1],
- task = action['task'],
- id = action['id'])
-
- if action['disabled']:
- actionItem.Enable(False)
-
- self.AddItem(actionItem)
-
- task = actionItem.GetTask()
- parameterized = False
- valid = True
- for f in task.get_options()['flags']:
- if f.get('parameterized', False):
- parameterized = True
- break
- for p in task.get_options()['params']:
- if p.get('required', 'no') != 'no' and \
- p.get('value', '') == '' and \
- p.get('default', '') == '':
- valid = False
- if p.get('parameterized', False):
- parameterized = True
-
- actionItem.SetValid(valid)
- actionItem.SetParameterized(parameterized)
- actionItem.GetLog() # substitute variables (-> valid/invalid)
-
- # load data & relations
- for data in gxmXml.data:
- dataItem = ModelData(parent = self,
- x = data['pos'][0],
- y = data['pos'][1],
- width = data['size'][0],
- height = data['size'][1],
- prompt = data['prompt'],
- value = data['value'])
- dataItem.SetIntermediate(data['intermediate'])
-
- for rel in data['rels']:
- actionItem = self.FindAction(rel['id'])
- if rel['dir'] == 'from':
- relation = ModelRelation(parent = self, fromShape = dataItem,
- toShape = actionItem, param = rel['name'])
- else:
- relation = ModelRelation(parent = self, fromShape = actionItem,
- toShape = dataItem, param = rel['name'])
- relation.SetControlPoints(rel['points'])
- actionItem.AddRelation(relation)
- dataItem.AddRelation(relation)
- if self.canvas:
- dataItem.Update()
-
- # load loops
- for loop in gxmXml.loops:
- loopItem = ModelLoop(parent = self,
- x = loop['pos'][0],
- y = loop['pos'][1],
- width = loop['size'][0],
- height = loop['size'][1],
- text = loop['text'],
- id = loop['id'])
- self.AddItem(loopItem)
- # load conditions
- for condition in gxmXml.conditions:
- conditionItem = ModelCondition(parent = self,
- x = condition['pos'][0],
- y = condition['pos'][1],
- width = condition['size'][0],
- height = condition['size'][1],
- text = condition['text'],
- id = condition['id'])
- self.AddItem(conditionItem)
- # define loops & if/else items
- for loop in gxmXml.loops:
- alist = list()
- for aId in loop['items']:
- action = self.GetItem(aId)
- alist.append(action)
-
- loopItem = self.GetItem(loop['id'])
- loopItem.SetItems(alist)
-
- for action in loopItem.GetItems():
- action.SetBlock(loopItem)
-
- for condition in gxmXml.conditions:
- conditionItem = self.GetItem(condition['id'])
- for b in condition['items'].keys():
- alist = list()
- for aId in condition['items'][b]:
- action = self.GetItem(aId)
- alist.append(action)
- conditionItem.SetItems(alist, branch = b)
-
- items = conditionItem.GetItems()
- for b in items.keys():
- for action in items[b]:
- action.SetBlock(conditionItem)
-
- def AddItem(self, newItem):
- """!Add item to the list"""
- iId = newItem.GetId()
-
- i = 0
- for item in self.items:
- if item.GetId() > iId:
- self.items.insert(i, newItem)
- return
- i += 1
-
- self.items.append(newItem)
-
- def IsValid(self):
- """Return True if model is valid"""
- if self.Validate():
- return False
-
- return True
-
- def Validate(self):
- """!Validate model, return None if model is valid otherwise
- error string"""
- errList = list()
- for action in self.GetItems(objType = ModelAction):
- task = menuform.GUI(show = None).ParseCommand(cmd = action.GetLog(string = False))
- errList += task.getCmdError()
-
- return errList
- def RunAction(self, item, params, log, onDone, statusbar = None):
- """!Run given action
- @param item action item
- @param params parameters dict
- @param log logging window
- @param onDone on-done method
- @param statusbar wx.StatusBar instance or None
- """
- name = item.GetName()
- if name in params:
- paramsOrig = item.GetParams(dcopy = True)
- item.MergeParams(params[name])
-
- if statusbar:
- statusbar.SetStatusText(_('Running model...'), 0)
- log.RunCmd(command = item.GetLog(string = False),
- onDone = onDone)
-
- if name in params:
- item.SetParams(paramsOrig)
-
- def Run(self, log, onDone, parent = None):
- """!Run model
- @param log logging window (see goutput.GMConsole)
- @param onDone on-done method
- @param parent window for messages or None
- """
- if self.GetNumItems() < 1:
- GMessage(parent = parent,
- message = _('Model is empty. Nothing to run.'))
- return
-
- statusbar = None
- if isinstance(parent, wx.Frame):
- statusbar = parent.GetStatusBar()
-
- # validation
- if statusbar:
- statusbar.SetStatusText(_('Validating model...'), 0)
- errList = self.Validate()
- if statusbar:
- statusbar.SetStatusText('', 0)
- if errList:
- dlg = wx.MessageDialog(parent = parent,
- message = _('Model is not valid. Do you want to '
- 'run the model anyway?\n\n%s') % '\n'.join(errList),
- caption = _("Run model?"),
- style = wx.YES_NO | wx.NO_DEFAULT |
- wx.ICON_QUESTION | wx.CENTRE)
- ret = dlg.ShowModal()
- if ret != wx.ID_YES:
- return
-
- # parametrization
- params = self.Parameterize()
- if params:
- dlg = ModelParamDialog(parent = parent,
- params = params)
- dlg.CenterOnParent()
-
- ret = dlg.ShowModal()
- if ret != wx.ID_OK:
- dlg.Destroy()
- return
-
- err = dlg.GetErrors()
- if err:
- GError(parent = self, message = unicode('\n'.join(err)))
- return
-
- log.cmdThread.SetId(-1)
- for item in self.GetItems():
- if not item.IsEnabled():
- continue
- if isinstance(item, ModelAction):
- if item.GetBlockId():
- continue
- self.RunAction(item, params, log, onDone)
- elif isinstance(item, ModelLoop):
- cond = item.GetText()
- # substitute variables in condition
- variables = self.GetVariables()
- for variable in variables:
- pattern = re.compile('%' + variable)
- if pattern.search(cond):
- value = variables[variable].get('value', '')
- vtype = variables[variable].get('type', 'string')
- if vtype == 'string':
- value = '"' + value + '"'
- cond = pattern.sub(value, cond)
- # split condition
- condVar, condText = re.split('\s*in\s*', cond)
-
- for action in item.GetItems():
- for vars()[condVar] in eval(condText):
- if isinstance(action, ModelAction):
- self.RunAction(action, params, log, onDone)
-
- if params:
- dlg.Destroy()
-
- def DeleteIntermediateData(self, log):
- """!Detele intermediate data"""
- rast, vect, rast3d, msg = self.GetIntermediateData()
-
- if rast:
- log.RunCmd(['g.remove', 'rast=%s' %','.join(rast)])
- if rast3d:
- log.RunCmd(['g.remove', 'rast3d=%s' %','.join(rast3d)])
- if vect:
- log.RunCmd(['g.remove', 'vect=%s' %','.join(vect)])
-
- def GetIntermediateData(self):
- """!Get info about intermediate data"""
- rast = list()
- rast3d = list()
- vect = list()
- for data in self.GetData():
- if not data.IsIntermediate():
- continue
- name = data.GetValue()
- prompt = data.GetPrompt()
- if prompt == 'raster':
- rast.append(name)
- elif prompt == 'vector':
- vect.append(name)
- elif prompt == 'rast3d':
- rast3d.append(name)
-
- msg = ''
- if rast:
- msg += '\n\n%s: ' % _('Raster maps')
- msg += ', '.join(rast)
- if rast3d:
- msg += '\n\n%s: ' % _('3D raster maps')
- msg += ', '.join(rast3d)
- if vect:
- msg += '\n\n%s: ' % _('Vector maps')
- msg += ', '.join(vect)
-
- return rast, vect, rast3d, msg
- def Update(self):
- """!Update model"""
- for item in self.items:
- item.Update()
-
- def IsParameterized(self):
- """!Return True if model is parameterized"""
- if self.Parameterize():
- return True
-
- return False
-
- def Parameterize(self):
- """!Return parameterized options"""
- result = dict()
- idx = 0
- if self.variables:
- params = list()
- result[_("Variables")] = { 'flags' : list(),
- 'params' : params,
- 'idx' : idx }
- for name, values in self.variables.iteritems():
- gtype = values.get('type', 'string')
- if gtype in ('raster', 'vector'):
- gisprompt = True
- prompt = gtype
- if gtype == 'raster':
- element = 'cell'
- else:
- element = 'vector'
- ptype = 'string'
- else:
- gisprompt = False
- prompt = None
- element = None
- ptype = gtype
- params.append({ 'gisprompt' : gisprompt,
- 'multiple' : 'no',
- 'description' : values.get('description', ''),
- 'guidependency' : '',
- 'default' : '',
- 'age' : None,
- 'required' : 'yes',
- 'value' : values.get('value', ''),
- 'label' : '',
- 'guisection' : '',
- 'key_desc' : '',
- 'values' : list(),
- 'parameterized' : False,
- 'values_desc' : list(),
- 'prompt' : prompt,
- 'element' : element,
- 'type' : ptype,
- 'name' : name })
-
- idx += 1
-
- for action in self.GetItems(objType = ModelAction):
- if not action.IsEnabled():
- continue
- name = action.GetName()
- params = action.GetParams()
- for f in params['flags']:
- if f.get('parameterized', False):
- if name not in result:
- result[name] = { 'flags' : list(),
- 'params': list(),
- 'idx' : idx }
- result[name]['flags'].append(f)
- for p in params['params']:
- if p.get('parameterized', False):
- if name not in result:
- result[name] = { 'flags' : list(),
- 'params': list(),
- 'idx' : idx }
- result[name]['params'].append(p)
- idx += 1
- self.variablesParams = result # record parameters
-
- return result
-
- class ModelFrame(wx.Frame):
- def __init__(self, parent, id = wx.ID_ANY,
- title = _("GRASS GIS Graphical Modeler"), **kwargs):
- """!Graphical modeler main window
-
- @param parent parent window
- @param id window id
- @param title window title
- @param kwargs wx.Frames' arguments
- """
- self.parent = parent
- self.searchDialog = None # module search dialog
- self.baseTitle = title
- self.modelFile = None # loaded model
- self.modelChanged = False
-
- self.cursors = {
- "default" : wx.StockCursor(wx.CURSOR_ARROW),
- "cross" : wx.StockCursor(wx.CURSOR_CROSS),
- }
-
- wx.Frame.__init__(self, parent = parent, id = id, title = title, **kwargs)
- self.SetName("Modeler")
- self.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR, 'grass.ico'), wx.BITMAP_TYPE_ICO))
-
- self.menubar = menu.Menu(parent = self, data = menudata.ModelerData())
-
- self.SetMenuBar(self.menubar)
-
- self.toolbar = toolbars.ModelToolbar(parent = self)
- self.SetToolBar(self.toolbar)
-
- self.statusbar = self.CreateStatusBar(number = 1)
-
- self.notebook = menuform.GNotebook(parent = self,
- style = FN.FNB_FANCY_TABS | FN.FNB_BOTTOM |
- FN.FNB_NO_NAV_BUTTONS | FN.FNB_NO_X_BUTTON)
-
- self.canvas = ModelCanvas(self)
- self.canvas.SetBackgroundColour(wx.WHITE)
- self.canvas.SetCursor(self.cursors["default"])
-
- self.model = Model(self.canvas)
-
- self.variablePanel = VariablePanel(parent = self)
-
- self.itemPanel = ItemPanel(parent = self)
-
- self.goutput = goutput.GMConsole(parent = self, notebook = self.notebook)
-
- self.notebook.AddPage(page = self.canvas, text=_('Model'), name = 'model')
- self.notebook.AddPage(page = self.itemPanel, text=_('Items'), name = 'items')
- self.notebook.AddPage(page = self.variablePanel, text=_('Variables'), name = 'variables')
- self.notebook.AddPage(page = self.goutput, text=_('Command output'), name = 'output')
- wx.CallAfter(self.notebook.SetSelectionByName, 'model')
- wx.CallAfter(self.ModelChanged, False)
- self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
- self.Bind(wx.EVT_SIZE, self.OnSize)
-
- self._layout()
- self.SetMinSize((475, 300))
- self.SetSize((640, 480))
-
- # fix goutput's pane size
- if self.goutput:
- self.goutput.SetSashPosition(int(self.GetSize()[1] * .75))
-
- def _layout(self):
- """!Do layout"""
- sizer = wx.BoxSizer(wx.VERTICAL)
- sizer.Add(item = self.notebook, proportion = 1,
- flag = wx.EXPAND)
-
- self.SetAutoLayout(True)
- self.SetSizer(sizer)
- sizer.Fit(self)
-
- self.Layout()
- def _addEvent(self, item):
- """!Add event to item"""
- evthandler = ModelEvtHandler(self.statusbar,
- self)
- evthandler.SetShape(item)
- evthandler.SetPreviousHandler(item.GetEventHandler())
- item.SetEventHandler(evthandler)
- def GetCanvas(self):
- """!Get canvas"""
- return self.canvas
-
- def GetModel(self):
- """!Get model"""
- return self.model
-
- def ModelChanged(self, changed = True):
- """!Update window title"""
- self.modelChanged = changed
-
- if self.modelFile:
- if self.modelChanged:
- self.SetTitle(self.baseTitle + " - " + os.path.basename(self.modelFile) + '*')
- else:
- self.SetTitle(self.baseTitle + " - " + os.path.basename(self.modelFile))
- else:
- self.SetTitle(self.baseTitle)
-
- def OnVariables(self, event):
- """!Switch to variables page"""
- self.notebook.SetSelectionByName('variables')
-
- def OnRemoveItem(self, event):
- """!Remove shape
- """
- self.GetCanvas().RemoveSelected()
-
- def OnCanvasRefresh(self, event):
- """!Refresh canvas"""
- self.SetStatusText(_("Redrawing model..."), 0)
- self.GetCanvas().Refresh()
- self.SetStatusText("", 0)
- def OnCmdRun(self, event):
- """!Run command"""
- try:
- action = self.GetModel().GetItems()[event.pid]
- if hasattr(action, "task"):
- action.Update(running = True)
- except IndexError:
- pass
-
- def OnCmdDone(self, event):
- """!Command done (or aborted)"""
- try:
- action = self.GetModel().GetItems()[event.pid]
- if hasattr(action, "task"):
- action.Update(running = True)
- except IndexError:
- pass
-
- def OnCloseWindow(self, event):
- """!Close window"""
- if self.modelChanged and \
- UserSettings.Get(group='manager', key='askOnQuit', subkey='enabled'):
- if self.modelFile:
- message = _("Do you want to save changes in the model?")
- else:
- message = _("Do you want to store current model settings "
- "to model file?")
-
- # ask user to save current settings
- dlg = wx.MessageDialog(self,
- message = message,
- caption=_("Quit Graphical Modeler"),
- style = wx.YES_NO | wx.YES_DEFAULT |
- wx.CANCEL | wx.ICON_QUESTION | wx.CENTRE)
- ret = dlg.ShowModal()
- if ret == wx.ID_YES:
- if not self.modelFile:
- self.OnWorkspaceSaveAs()
- else:
- self.WriteModelFile(self.modelFile)
- elif ret == wx.ID_CANCEL:
- dlg.Destroy()
- return
- dlg.Destroy()
-
- self.Destroy()
- def OnSize(self, event):
- """Window resized, save to the model"""
- self.ModelChanged()
- event.Skip()
-
- def OnPreferences(self, event):
- """!Open preferences dialog"""
- dlg = PreferencesDialog(parent = self)
- dlg.CenterOnParent()
-
- dlg.ShowModal()
- self.canvas.Refresh()
-
- def OnHelp(self, event):
- """!Show help"""
- if self.parent and self.parent.GetName() == 'LayerManager':
- log = self.parent.GetLogWindow()
- log.RunCmd(['g.manual',
- 'entry=wxGUI.Modeler'])
- else:
- RunCommand('g.manual',
- quiet = True,
- entry = 'wxGUI.Modeler')
-
- def OnModelProperties(self, event):
- """!Model properties dialog"""
- dlg = PropertiesDialog(parent = self)
- dlg.CentreOnParent()
- properties = self.model.GetProperties()
- dlg.Init(properties)
- if dlg.ShowModal() == wx.ID_OK:
- self.ModelChanged()
- for key, value in dlg.GetValues().iteritems():
- properties[key] = value
- for action in self.model.GetItems(objType = ModelAction):
- action.GetTask().set_flag('overwrite', properties['overwrite'])
-
- dlg.Destroy()
-
- def OnDeleteData(self, event):
- """!Delete intermediate data"""
- rast, vect, rast3d, msg = self.model.GetIntermediateData()
-
- if not rast and not vect and not rast3d:
- GMessage(parent = self,
- message = _('Nothing to delete.'))
- return
-
- dlg = wx.MessageDialog(parent = self,
- message= _("Do you want to permanently delete data?%s" % msg),
- caption=_("Delete intermediate data?"),
- style=wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
-
- ret = dlg.ShowModal()
- if ret == wx.ID_YES:
- dlg.Destroy()
-
- if rast:
- self.goutput.RunCmd(['g.remove', 'rast=%s' %','.join(rast)])
- if rast3d:
- self.goutput.RunCmd(['g.remove', 'rast3d=%s' %','.join(rast3d)])
- if vect:
- self.goutput.RunCmd(['g.remove', 'vect=%s' %','.join(vect)])
-
- self.SetStatusText(_("%d maps deleted from current mapset") % \
- int(len(rast) + len(rast3d) + len(vect)))
- return
-
- dlg.Destroy()
-
- def OnModelNew(self, event):
- """!Create new model"""
- Debug.msg(4, "ModelFrame.OnModelNew():")
-
- # ask user to save current model
- if self.modelFile and self.modelChanged:
- self.OnModelSave()
- elif self.modelFile is None and \
- (self.model.GetNumItems() > 0 or len(self.model.GetData()) > 0):
- dlg = wx.MessageDialog(self, message=_("Current model is not empty. "
- "Do you want to store current settings "
- "to model file?"),
- caption=_("Create new model?"),
- style=wx.YES_NO | wx.YES_DEFAULT |
- wx.CANCEL | wx.ICON_QUESTION)
- ret = dlg.ShowModal()
- if ret == wx.ID_YES:
- self.OnModelSaveAs()
- elif ret == wx.ID_CANCEL:
- dlg.Destroy()
- return
-
- dlg.Destroy()
-
- # delete all items
- self.canvas.GetDiagram().DeleteAllShapes()
- self.model.Reset()
- self.canvas.Refresh()
-
- # no model file loaded
- self.modelFile = None
- self.modelChanged = False
- self.SetTitle(self.baseTitle)
-
- def OnModelOpen(self, event):
- """!Load model from file"""
- filename = ''
- dlg = wx.FileDialog(parent = self, message=_("Choose model file"),
- defaultDir = os.getcwd(),
- wildcard=_("GRASS Model File (*.gxm)|*.gxm"))
- if dlg.ShowModal() == wx.ID_OK:
- filename = dlg.GetPath()
-
- if not filename:
- return
-
- Debug.msg(4, "ModelFrame.OnModelOpen(): filename=%s" % filename)
-
- # close current model
- self.OnModelClose()
-
- self.LoadModelFile(filename)
-
- self.modelFile = filename
- self.SetTitle(self.baseTitle + " - " + os.path.basename(self.modelFile))
- self.SetStatusText(_('%(items)d items (%(actions)d actions) loaded into model') % \
- { 'items' : self.model.GetNumItems(),
- 'actions' : self.model.GetNumItems(actionOnly = True) }, 0)
-
- def OnModelSave(self, event = None):
- """!Save model to file"""
- if self.modelFile and self.modelChanged:
- dlg = wx.MessageDialog(self, message=_("Model file <%s> already exists. "
- "Do you want to overwrite this file?") % \
- self.modelFile,
- caption=_("Save model"),
- style=wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
- if dlg.ShowModal() == wx.ID_NO:
- dlg.Destroy()
- else:
- Debug.msg(4, "ModelFrame.OnModelSave(): filename=%s" % self.modelFile)
- self.WriteModelFile(self.modelFile)
- self.SetStatusText(_('File <%s> saved') % self.modelFile, 0)
- self.SetTitle(self.baseTitle + " - " + os.path.basename(self.modelFile))
- elif not self.modelFile:
- self.OnModelSaveAs(None)
-
- def OnModelSaveAs(self, event):
- """!Create model to file as"""
- filename = ''
- dlg = wx.FileDialog(parent = self,
- message = _("Choose file to save current model"),
- defaultDir = os.getcwd(),
- wildcard=_("GRASS Model File (*.gxm)|*.gxm"),
- style=wx.FD_SAVE)
-
-
- if dlg.ShowModal() == wx.ID_OK:
- filename = dlg.GetPath()
-
- if not filename:
- return
-
- # check for extension
- if filename[-4:] != ".gxm":
- filename += ".gxm"
-
- if os.path.exists(filename):
- dlg = wx.MessageDialog(parent = self,
- message=_("Model file <%s> already exists. "
- "Do you want to overwrite this file?") % filename,
- caption=_("File already exists"),
- style=wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
- if dlg.ShowModal() != wx.ID_YES:
- dlg.Destroy()
- return
-
- Debug.msg(4, "GMFrame.OnModelSaveAs(): filename=%s" % filename)
-
- self.WriteModelFile(filename)
- self.modelFile = filename
- self.SetTitle(self.baseTitle + " - " + os.path.basename(self.modelFile))
- self.SetStatusText(_('File <%s> saved') % self.modelFile, 0)
- def OnModelClose(self, event = None):
- """!Close model file"""
- Debug.msg(4, "ModelFrame.OnModelClose(): file=%s" % self.modelFile)
- # ask user to save current model
- if self.modelFile and self.modelChanged:
- self.OnModelSave()
- elif self.modelFile is None and \
- (self.model.GetNumItems() > 0 or len(self.model.GetData()) > 0):
- dlg = wx.MessageDialog(self, message=_("Current model is not empty. "
- "Do you want to store current settings "
- "to model file?"),
- caption=_("Create new model?"),
- style=wx.YES_NO | wx.YES_DEFAULT |
- wx.CANCEL | wx.ICON_QUESTION)
- ret = dlg.ShowModal()
- if ret == wx.ID_YES:
- self.OnModelSaveAs()
- elif ret == wx.ID_CANCEL:
- dlg.Destroy()
- return
-
- dlg.Destroy()
-
- self.modelFile = None
- self.SetTitle(self.baseTitle)
-
- self.canvas.GetDiagram().DeleteAllShapes()
- self.model.Reset()
-
- self.canvas.Refresh()
-
- def OnRunModel(self, event):
- """!Run entire model"""
- self.model.Run(self.goutput, self.OnDone, parent = self)
-
- def OnDone(self, cmd, returncode):
- """!Computation finished"""
- self.SetStatusText('', 0)
-
- def OnValidateModel(self, event, showMsg = True):
- """!Validate entire model"""
- if self.model.GetNumItems() < 1:
- GMessage(parent = self,
- message = _('Model is empty. Nothing to validate.'))
- return
-
-
- self.SetStatusText(_('Validating model...'), 0)
- errList = self.model.Validate()
- self.SetStatusText('', 0)
-
- if errList:
- GWarning(parent = self,
- message = _('Model is not valid.\n\n%s') % '\n'.join(errList))
- else:
- GMessage(parent = self,
- message = _('Model is valid.'))
-
- def OnExportImage(self, event):
- """!Export model to image (default image)
- """
- xminImg = 0
- xmaxImg = 0
- yminImg = 0
- ymaxImg = 0
- # get current size of canvas
- for shape in self.canvas.GetDiagram().GetShapeList():
- w, h = shape.GetBoundingBoxMax()
- x = shape.GetX()
- y = shape.GetY()
- xmin = x - w / 2
- xmax = x + w / 2
- ymin = y - h / 2
- ymax = y + h / 2
- if xmin < xminImg:
- xminImg = xmin
- if xmax > xmaxImg:
- xmaxImg = xmax
- if ymin < yminImg:
- yminImg = ymin
- if ymax > ymaxImg:
- ymaxImg = ymax
- size = wx.Size(int(xmaxImg - xminImg) + 50,
- int(ymaxImg - yminImg) + 50)
- bitmap = wx.EmptyBitmap(width = size.width, height = size.height)
-
- filetype, ltype = GetImageHandlers(wx.ImageFromBitmap(bitmap))
-
- dlg = wx.FileDialog(parent = self,
- message = _("Choose a file name to save the image (no need to add extension)"),
- defaultDir = "",
- defaultFile = "",
- wildcard = filetype,
- style=wx.SAVE | wx.FD_OVERWRITE_PROMPT)
-
- if dlg.ShowModal() == wx.ID_OK:
- path = dlg.GetPath()
- if not path:
- dlg.Destroy()
- return
-
- base, ext = os.path.splitext(path)
- fileType = ltype[dlg.GetFilterIndex()]['type']
- extType = ltype[dlg.GetFilterIndex()]['ext']
- if ext != extType:
- path = base + '.' + extType
-
- dc = wx.MemoryDC(bitmap)
- dc.SetBackground(wx.WHITE_BRUSH)
- dc.SetBackgroundMode(wx.SOLID)
-
- dc.BeginDrawing()
- self.canvas.GetDiagram().Clear(dc)
- self.canvas.GetDiagram().Redraw(dc)
- dc.EndDrawing()
-
- bitmap.SaveFile(path, fileType)
- self.SetStatusText(_("Model exported to <%s>") % path)
-
- dlg.Destroy()
-
- def OnExportPython(self, event):
- """!Export model to Python script"""
- filename = ''
- dlg = wx.FileDialog(parent = self,
- message = _("Choose file to save"),
- defaultDir = os.getcwd(),
- wildcard=_("Python script (*.py)|*.py"),
- style=wx.FD_SAVE)
-
- if dlg.ShowModal() == wx.ID_OK:
- filename = dlg.GetPath()
-
- if not filename:
- return
-
- # check for extension
- if filename[-3:] != ".py":
- filename += ".py"
-
- if os.path.exists(filename):
- dlg = wx.MessageDialog(self, message=_("File <%s> already exists. "
- "Do you want to overwrite this file?") % filename,
- caption=_("Save file"),
- style=wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
- if dlg.ShowModal() == wx.ID_NO:
- dlg.Destroy()
- return
-
- dlg.Destroy()
-
- fd = open(filename, "w")
- try:
- WritePythonFile(fd, self.model)
- finally:
- fd.close()
-
- # executable file
- os.chmod(filename, stat.S_IRWXU | stat.S_IWUSR)
-
- self.SetStatusText(_("Model exported to <%s>") % filename)
- def OnDefineRelation(self, event):
- """!Define relation between data and action items"""
- self.canvas.SetCursor(self.cursors["cross"])
- self.defineRelation = { 'from' : None,
- 'to' : None }
-
- def OnDefineLoop(self, event):
- """!Define new loop in the model"""
- self.ModelChanged()
-
- width, height = self.canvas.GetSize()
- loop = ModelLoop(self, x = width/2, y = height/2,
- id = self.model.GetNumItems() + 1)
- self.canvas.diagram.AddShape(loop)
- loop.Show(True)
-
- self._addEvent(loop)
- self.model.AddItem(loop)
-
- self.canvas.Refresh()
-
- def OnDefineCondition(self, event):
- """!Define new condition in the model"""
- self.ModelChanged()
-
- width, height = self.canvas.GetSize()
- cond = ModelCondition(self, x = width/2, y = height/2,
- id = self.model.GetNumItems() + 1)
- self.canvas.diagram.AddShape(cond)
- cond.Show(True)
-
- self._addEvent(cond)
- self.model.AddItem(cond)
-
- self.canvas.Refresh()
-
- def OnAddAction(self, event):
- """!Add action to model"""
- if self.searchDialog is None:
- self.searchDialog = ModelSearchDialog(self)
- self.searchDialog.CentreOnParent()
- else:
- self.searchDialog.Reset()
-
- if self.searchDialog.ShowModal() == wx.ID_CANCEL:
- self.searchDialog.Hide()
- return
-
- cmd = self.searchDialog.GetCmd()
- self.searchDialog.Hide()
-
- self.ModelChanged()
-
- # add action to canvas
- width, height = self.canvas.GetSize()
- action = ModelAction(self.model, cmd = cmd, x = width/2, y = height/2,
- id = self.model.GetNextId())
- overwrite = self.model.GetProperties().get('overwrite', None)
- if overwrite is not None:
- action.GetTask().set_flag('overwrite', overwrite)
-
- self.canvas.diagram.AddShape(action)
- action.Show(True)
- self._addEvent(action)
- self.model.AddItem(action)
-
- self.itemPanel.Update()
- self.canvas.Refresh()
- time.sleep(.1)
-
- # show properties dialog
- win = action.GetPropDialog()
- if not win and action.GetLog(string = False):
- module = menuform.GUI(parent = self, show = True).ParseCommand(action.GetLog(string = False),
- completed = (self.GetOptData, action, action.GetParams()))
- elif win and not win.IsShown():
- win.Show()
-
- if win:
- win.Raise()
-
- def OnAddData(self, event):
- """!Add data item to model
- """
- # add action to canvas
- width, height = self.canvas.GetSize()
- data = ModelData(self, x = width/2, y = height/2)
-
- dlg = ModelDataDialog(parent = self, shape = data)
- data.SetPropDialog(dlg)
- dlg.CentreOnParent()
- ret = dlg.ShowModal()
- dlg.Destroy()
- if ret != wx.ID_OK:
- return
-
- data.Update()
- self.canvas.diagram.AddShape(data)
- data.Show(True)
-
- self.ModelChanged()
-
- self._addEvent(data)
- self.model.AddItem(data)
-
- self.canvas.Refresh()
-
-
- def OnHelp(self, event):
- """!Display manual page"""
- grass.run_command('g.manual',
- entry = 'wxGUI.Modeler')
- def OnAbout(self, event):
- """!Display About window"""
- info = wx.AboutDialogInfo()
- info.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR, 'grass.ico'), wx.BITMAP_TYPE_ICO))
- info.SetName(_('wxGUI Graphical Modeler'))
- info.SetWebSite('http://grass.osgeo.org')
- info.SetDescription(_('(C) 2010 by the GRASS Development Team\n\n') +
- '\n'.join(textwrap.wrap(_('This program is free software under the GNU General Public License'
- '(>=v2). Read the file COPYING that comes with GRASS for details.'), 75)))
-
- wx.AboutBox(info)
-
- def GetOptData(self, dcmd, layer, params, propwin):
- """!Process action data"""
- if params: # add data items
- # for p in params['params']:
- # if p.get('prompt', '') in ('raster', 'vector', 'raster3d'):
- # try:
- # name, mapset = p.get('value', '').split('@', 1)
- # except (ValueError, IndexError):
- # continue
-
- # if mapset != grass.gisenv()['MAPSET']:
- # continue
-
- # # don't use fully qualified names
- # p['value'] = p.get('value', '').split('@')[0]
- # for idx in range(1, len(dcmd)):
- # if p.get('name', '') in dcmd[idx]:
- # dcmd[idx] = p.get('name', '') + '=' + p.get('value', '')
- # break
-
- width, height = self.canvas.GetSize()
- x = [width/2 + 200, width/2 - 200]
- for p in params['params']:
- if p.get('prompt', '') in ('raster', 'vector', 'raster3d') and \
- (p.get('value', None) or \
- (p.get('age', 'old') != 'old' and p.get('required', 'no') == 'yes')):
- data = layer.FindData(p.get('name', ''))
- if data:
- data.SetValue(p.get('value', ''))
- data.Update()
- continue
-
- data = self.model.FindData(p.get('value', ''),
- p.get('prompt', ''))
- if data:
- if p.get('age', 'old') == 'old':
- rel = ModelRelation(parent = self, fromShape = data,
- toShape = layer, param = p.get('name', ''))
- else:
- rel = ModelRelation(parent = self, fromShape = layer,
- toShape = data, param = p.get('name', ''))
- layer.AddRelation(rel)
- data.AddRelation(rel)
- self.AddLine(rel)
- data.Update()
- continue
-
- data = ModelData(self, value = p.get('value', ''),
- prompt = p.get('prompt', ''),
- x = x.pop(), y = height/2)
- self._addEvent(data)
- self.canvas.diagram.AddShape(data)
- data.Show(True)
-
- if p.get('age', 'old') == 'old':
- rel = ModelRelation(parent = self, fromShape = data,
- toShape = layer, param = p.get('name', ''))
- else:
- rel = ModelRelation(parent = self, fromShape = layer,
- toShape = data, param = p.get('name', ''))
- layer.AddRelation(rel)
- data.AddRelation(rel)
- self.AddLine(rel)
- data.Update()
-
- # valid ?
- valid = True
- for p in params['params']:
- if p.get('required', 'no') != 'no' and \
- p.get('value', '') == '' and \
- p.get('default', '') == '':
- valid = False
- break
- layer.SetValid(valid)
- # parameterized ?
- parameterized = False
- for f in params['flags']:
- if f.get('parameterized', False):
- parameterized = True
- break
- if not parameterized:
- for p in params['params']:
- if p.get('parameterized', False):
- parameterized = True
- break
- layer.SetParameterized(parameterized)
-
- self.canvas.Refresh()
-
- if dcmd:
- layer.SetProperties(params, propwin)
-
- self.SetStatusText(layer.GetLog(), 0)
-
- def AddLine(self, rel):
- """!Add connection between model objects
-
- @param rel relation
- """
- fromShape = rel.GetFrom()
- toShape = rel.GetTo()
-
- rel.SetCanvas(self)
- rel.SetPen(wx.BLACK_PEN)
- rel.SetBrush(wx.BLACK_BRUSH)
- rel.AddArrow(ogl.ARROW_ARROW)
- points = rel.GetControlPoints()
- rel.MakeLineControlPoints(2)
- if points:
- for x, y in points:
- rel.InsertLineControlPoint(point = wx.RealPoint(x, y))
-
- self._addEvent(rel)
- try:
- fromShape.AddLine(rel, toShape)
- except TypeError:
- pass # bug when connecting ModelCondition and ModelLoop - to be fixed
-
- self.canvas.diagram.AddShape(rel)
- rel.Show(True)
-
- def LoadModelFile(self, filename):
- """!Load model definition stored in GRASS Model XML file (gxm)
- """
- try:
- self.model.LoadModel(filename)
- except GException, e:
- GError(parent = self,
- message = _("Reading model file <%s> failed.\n"
- "Invalid file, unable to parse XML document.") % filename)
-
- self.modelFile = filename
- self.SetTitle(self.baseTitle + " - " + os.path.basename(self.modelFile))
-
- self.SetStatusText(_("Please wait, loading model..."), 0)
-
- # load actions
- for item in self.model.GetItems(objType = ModelAction):
- self._addEvent(item)
- self.canvas.diagram.AddShape(item)
- item.Show(True)
- # relations/data
- for rel in item.GetRelations():
- if rel.GetFrom() == item:
- dataItem = rel.GetTo()
- else:
- dataItem = rel.GetFrom()
- self._addEvent(dataItem)
- self.canvas.diagram.AddShape(dataItem)
- self.AddLine(rel)
- dataItem.Show(True)
-
- # load loops
- for item in self.model.GetItems(objType = ModelLoop):
- self._addEvent(item)
- self.canvas.diagram.AddShape(item)
- item.Show(True)
-
- # connect items in the loop
- self.DefineLoop(item)
- # load conditions
- for item in self.model.GetItems(objType = ModelCondition):
- self._addEvent(item)
- self.canvas.diagram.AddShape(item)
- item.Show(True)
-
- # connect items in the condition
- self.DefineCondition(item)
-
- # load variables
- self.variablePanel.Update()
- self.itemPanel.Update()
- self.SetStatusText('', 0)
-
- self.canvas.Refresh(True)
-
- def WriteModelFile(self, filename):
- """!Save model to model file, recover original file on error.
-
- @return True on success
- @return False on failure
- """
- self.ModelChanged(False)
- tmpfile = tempfile.TemporaryFile(mode='w+b')
- try:
- WriteModelFile(fd = tmpfile, model = self.model)
- except StandardError:
- GError(parent = self,
- message = _("Writing current settings to model file failed."))
- return False
-
- try:
- mfile = open(filename, "w")
- tmpfile.seek(0)
- for line in tmpfile.readlines():
- mfile.write(line)
- except IOError:
- wx.MessageBox(parent = self,
- message = _("Unable to open file <%s> for writing.") % filename,
- caption = _("Error"),
- style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
- return False
-
- mfile.close()
-
- return True
-
- def DefineLoop(self, loop):
- """!Define loop with given list of items"""
- parent = loop
- items = loop.GetItems()
- if not items:
- return
-
- # remove defined relations first
- for rel in loop.GetRelations():
- self.canvas.GetDiagram().RemoveShape(rel)
- loop.Clear()
-
- for item in items:
- rel = ModelRelation(parent = self, fromShape = parent, toShape = item)
- dx = item.GetX() - parent.GetX()
- dy = item.GetY() - parent.GetY()
- loop.AddRelation(rel)
- if dx != 0:
- rel.SetControlPoints(((parent.GetX(), parent.GetY() + dy / 2),
- (parent.GetX() + dx, parent.GetY() + dy / 2)))
- self.AddLine(rel)
- parent = item
-
- # close loop
- item = loop.GetItems()[-1]
- rel = ModelRelation(parent = self, fromShape = item, toShape = loop)
- loop.AddRelation(rel)
- self.AddLine(rel)
- dx = (item.GetX() - loop.GetX()) + loop.GetWidth() / 2 + 50
- dy = item.GetHeight() / 2 + 50
- rel.MakeLineControlPoints(0)
- rel.InsertLineControlPoint(point = wx.RealPoint(loop.GetX() - loop.GetWidth() / 2 ,
- loop.GetY()))
- rel.InsertLineControlPoint(point = wx.RealPoint(item.GetX(),
- item.GetY() + item.GetHeight() / 2))
- rel.InsertLineControlPoint(point = wx.RealPoint(item.GetX(),
- item.GetY() + dy))
- rel.InsertLineControlPoint(point = wx.RealPoint(item.GetX() - dx,
- item.GetY() + dy))
- rel.InsertLineControlPoint(point = wx.RealPoint(item.GetX() - dx,
- loop.GetY()))
-
- self.canvas.Refresh()
- def DefineCondition(self, condition):
- """!Define if-else statement with given list of items"""
- parent = condition
- items = condition.GetItems()
- if not items['if'] and not items['else']:
- return
-
- # remove defined relations first
- for rel in condition.GetRelations():
- self.canvas.GetDiagram().RemoveShape(rel)
- condition.Clear()
- dxIf = condition.GetX() + condition.GetWidth() / 2
- dxElse = condition.GetX() - condition.GetWidth() / 2
- dy = condition.GetY()
- for branch in items.keys():
- for item in items[branch]:
- rel = ModelRelation(parent = self, fromShape = parent,
- toShape = item)
- condition.AddRelation(rel)
- self.AddLine(rel)
- rel.MakeLineControlPoints(0)
- if branch == 'if':
- rel.InsertLineControlPoint(point = wx.RealPoint(item.GetX() - item.GetWidth() / 2, item.GetY()))
- rel.InsertLineControlPoint(point = wx.RealPoint(dxIf, dy))
- else:
- rel.InsertLineControlPoint(point = wx.RealPoint(dxElse, dy))
- rel.InsertLineControlPoint(point = wx.RealPoint(item.GetX() - item.GetWidth() / 2, item.GetY()))
- parent = item
-
- self.canvas.Refresh()
-
- class ModelCanvas(ogl.ShapeCanvas):
- """!Canvas where model is drawn"""
- def __init__(self, parent):
- self.parent = parent
- ogl.OGLInitialize()
- ogl.ShapeCanvas.__init__(self, parent)
-
- self.diagram = ogl.Diagram()
- self.SetDiagram(self.diagram)
- self.diagram.SetCanvas(self)
-
- self.SetScrollbars(20, 20, 1000/20, 1000/20)
-
- self.Bind(wx.EVT_CHAR, self.OnChar)
-
- def OnChar(self, event):
- """!Key pressed"""
- kc = event.GetKeyCode()
- diagram = self.GetDiagram()
- if kc == wx.WXK_DELETE:
- self.RemoveSelected()
-
- def RemoveSelected(self):
- """!Remove selected shapes"""
- self.parent.ModelChanged()
-
- diagram = self.GetDiagram()
- for shape in diagram.GetShapeList():
- if not shape.Selected():
- continue
- remList, upList = self.parent.GetModel().RemoveItem(shape)
- shape.Select(False)
- diagram.RemoveShape(shape)
- del shape
- for item in remList:
- diagram.RemoveShape(item)
- item.__del__()
-
- for item in upList:
- item.Update()
-
- self.Refresh()
-
- class ModelObject:
- def __init__(self, id = -1):
- self.id = id
- self.rels = list() # list of ModelRelations
-
- self.isEnabled = True
- self.inBlock = list() # list of related loops/conditions
-
- def __del__(self):
- pass
-
- def GetId(self):
- """!Get id"""
- return self.id
-
- def AddRelation(self, rel):
- """!Record new relation
- """
- self.rels.append(rel)
- def GetRelations(self, fdir = None):
- """!Get list of relations
-
- @param fdir True for 'from'
- """
- if fdir is None:
- return self.rels
-
- result = list()
- for rel in self.rels:
- if fdir == 'from':
- if rel.GetFrom() == self:
- result.append(rel)
- else:
- if rel.GetTo() == self:
- result.append(rel)
-
- return result
-
- def IsEnabled(self):
- """!Get True if action is enabled, otherwise False"""
- return self.isEnabled
-
- def Enable(self, enabled = True):
- """!Enable/disable action"""
- self.isEnabled = enabled
- self.Update()
- def Update(self):
- pass
- def SetBlock(self, item):
- """!Add object to the block (loop/condition)
- @param item reference to ModelLoop or ModelCondition which
- defines loops/condition
- """
- if item not in self.inBlock:
- self.inBlock.append(item)
-
- def UnSetBlock(self, item):
- """!Remove object from the block (loop/consition)
- @param item reference to ModelLoop or ModelCondition which
- defines loops/codition
- """
- if item in self.inBlock:
- self.inBlock.remove(item)
-
- def GetBlock(self):
- """!Get list of related ModelObject(s) which defines block
- (loop/condition)
- @return list of ModelObjects
- """
- return self.inBlock
-
- def GetBlockId(self):
- """!Get list of related ids which defines block
- @return list of ids
- """
- ret = list()
- for mo in self.inBlock:
- ret.append(mo.GetId())
-
- return ret
-
- class ModelAction(ModelObject, ogl.RectangleShape):
- """!Action class (GRASS module)"""
- def __init__(self, parent, x, y, id = -1, cmd = None, task = None, width = None, height = None):
- ModelObject.__init__(self, id)
-
- self.parent = parent
- self.task = task
-
- if not width:
- width = UserSettings.Get(group='modeler', key='action', subkey=('size', 'width'))
- if not height:
- height = UserSettings.Get(group='modeler', key='action', subkey=('size', 'height'))
-
- if cmd:
- self.task = menuform.GUI(show = None).ParseCommand(cmd = cmd)
- else:
- if task:
- self.task = task
- else:
- self.task = None
-
- self.propWin = None
-
- self.data = list() # list of connected data items
-
- self.isValid = False
- self.isParameterized = False
-
- if self.parent.GetCanvas():
- ogl.RectangleShape.__init__(self, width, height)
-
- self.SetCanvas(self.parent)
- self.SetX(x)
- self.SetY(y)
- self.SetPen(wx.BLACK_PEN)
- self._setPen()
- self._setBrush()
- self.SetId(id)
-
- def _setBrush(self, running = False):
- """!Set brush"""
- if running:
- color = UserSettings.Get(group='modeler', key='action',
- subkey=('color', 'running'))
- elif not self.isEnabled:
- color = UserSettings.Get(group='modeler', key='action',
- subkey=('color', 'disabled'))
- elif self.isValid:
- color = UserSettings.Get(group='modeler', key='action',
- subkey=('color', 'valid'))
- else:
- color = UserSettings.Get(group='modeler', key='action',
- subkey=('color', 'invalid'))
-
- wxColor = wx.Color(color[0], color[1], color[2])
- self.SetBrush(wx.Brush(wxColor))
-
- def _setPen(self):
- """!Set pen"""
- if self.isParameterized:
- width = int(UserSettings.Get(group='modeler', key='action',
- subkey=('width', 'parameterized')))
- else:
- width = int(UserSettings.Get(group='modeler', key='action',
- subkey=('width', 'default')))
- pen = self.GetPen()
- pen.SetWidth(width)
- self.SetPen(pen)
- def SetId(self, id):
- """!Set id"""
- self.id = id
- cmd = self.task.getCmd(ignoreErrors = True)
- if cmd and len(cmd) > 0:
- self.ClearText()
- self.AddText('(%d) %s' % (self.id, cmd[0]))
- else:
- self.AddText('(%d) <<%s>>' % (self.id, _("unknown")))
-
- def SetProperties(self, params, propwin):
- """!Record properties dialog"""
- self.task.params = params['params']
- self.task.flags = params['flags']
- self.propWin = propwin
- def GetPropDialog(self):
- """!Get properties dialog"""
- return self.propWin
- def GetLog(self, string = True):
- """!Get logging info"""
- cmd = self.task.getCmd(ignoreErrors = True, ignoreRequired = True)
-
- # substitute variables
- variables = self.parent.GetVariables()
- fparams = self.parent.GetVariables(params = True)
- params = None
- for values in fparams.itervalues():
- params = values['params']
- break
-
- for variable in variables:
- pattern= re.compile('%' + variable)
- value = None
- if params:
- for p in params:
- if variable == p.get('name', ''):
- value = p.get('value', '')
- break
- if not value:
- value = variables[variable].get('value', '')
-
- for idx in range(len(cmd)):
- if pattern.search(cmd[idx]):
- if value:
- cmd[idx] = pattern.sub(value, cmd[idx])
- else:
- self.isValid = False
- break
- idx += 1
-
- if string:
- if cmd is None:
- return ''
- else:
- return ' '.join(cmd)
-
- return cmd
-
- def GetName(self):
- """!Get name"""
- cmd = self.task.getCmd(ignoreErrors = True)
- if cmd and len(cmd) > 0:
- return cmd[0]
-
- return _('unknown')
- def GetParams(self, dcopy = False):
- """!Get dictionary of parameters"""
- if dcopy:
- return copy.deepcopy(self.task.get_options())
-
- return self.task.get_options()
- def GetTask(self):
- """!Get grassTask instance"""
- return self.task
-
- def SetParams(self, params):
- """!Set dictionary of parameters"""
- self.task.params = params['params']
- self.task.flags = params['flags']
-
- def MergeParams(self, params):
- """!Merge dictionary of parameters"""
- if 'flags' in params:
- for f in params['flags']:
- self.task.set_flag(f['name'],
- f.get('value', False))
- if 'params' in params:
- for p in params['params']:
- self.task.set_param(p['name'],
- p.get('value', ''))
-
- def SetValid(self, isvalid):
- """!Set instance to be valid/invalid"""
- self.isValid = isvalid
- self._setBrush()
-
- def SetParameterized(self, isparameterized):
- """!Set action parameterized"""
- self.isParameterized = isparameterized
- if self.parent.GetCanvas():
- self._setPen()
-
- def IsParameterized(self):
- """!Check if action is parameterized"""
- return self.isParameterized
-
- def FindData(self, name):
- """!Find data item by name"""
- for rel in self.GetRelations():
- data = rel.GetData()
- if name == rel.GetName() and name in data.GetName():
- return data
-
- return None
- def Update(self, running = False):
- """!Update action"""
- if running:
- self._setBrush(running = True)
- else:
- self._setBrush()
- self._setPen()
- def OnDraw(self, dc):
- """!Draw action in canvas"""
- self._setBrush()
- self._setPen()
- ogl.RectangleShape.OnDraw(self, dc)
-
- class ModelData(ModelObject, ogl.EllipseShape):
- def __init__(self, parent, x, y, value = '', prompt = '', width = None, height = None):
- """Data item class
-
- @param parent window parent
- @param x, y position of the shape
- @param fname, tname list of parameter names from / to
- @param value value
- @param prompt type of GIS element
- @param width,height dimension of the shape
- """
- ModelObject.__init__(self)
-
- self.parent = parent
- self.value = value
- self.prompt = prompt
- self.intermediate = False
- self.propWin = None
- if not width:
- width = UserSettings.Get(group='modeler', key='data', subkey=('size', 'width'))
- if not height:
- height = UserSettings.Get(group='modeler', key='data', subkey=('size', 'height'))
-
- if self.parent.GetCanvas():
- ogl.EllipseShape.__init__(self, width, height)
-
- self.SetCanvas(self.parent)
- self.SetX(x)
- self.SetY(y)
- self.SetPen(wx.BLACK_PEN)
- self._setBrush()
-
- self._setText()
-
- def IsIntermediate(self):
- """!Checks if data item is intermediate"""
- return self.intermediate
-
- def SetIntermediate(self, im):
- """!Set intermediate flag"""
- self.intermediate = im
-
- def OnDraw(self, dc):
- pen = self.GetPen()
- pen.SetWidth(1)
- if self.intermediate:
- pen.SetStyle(wx.SHORT_DASH)
- else:
- pen.SetStyle(wx.SOLID)
- self.SetPen(pen)
-
- ogl.EllipseShape.OnDraw(self, dc)
-
- def GetLog(self, string = True):
- """!Get logging info"""
- name = list()
- for rel in self.GetRelations():
- name.append(rel.GetName())
- if name:
- return '/'.join(name) + '=' + self.value + ' (' + self.prompt + ')'
- else:
- return self.value + ' (' + self.prompt + ')'
- def GetName(self):
- """!Get list of names"""
- name = list()
- for rel in self.GetRelations():
- name.append(rel.GetName())
-
- return name
-
- def GetPrompt(self):
- """!Get prompt"""
- return self.prompt
- def SetPrompt(self, prompt):
- """!Set prompt
-
- @param prompt
- """
- self.prompt = prompt
-
- def GetValue(self):
- """!Get value"""
- return self.value
- def SetValue(self, value):
- """!Set value
- @param value
- """
- self.value = value
- self._setText()
- for direction in ('from', 'to'):
- for rel in self.GetRelations(direction):
- if direction == 'from':
- action = rel.GetTo()
- else:
- action = rel.GetFrom()
-
- task = menuform.GUI(show = None).ParseCommand(cmd = action.GetLog(string = False))
- task.set_param(rel.GetName(), self.value)
- action.SetParams(params = task.get_options())
-
- def GetPropDialog(self):
- """!Get properties dialog"""
- return self.propWin
- def SetPropDialog(self, win):
- """!Get properties dialog"""
- self.propWin = win
- def _setBrush(self):
- """!Set brush"""
- if self.prompt == 'raster':
- color = UserSettings.Get(group = 'modeler', key = 'data',
- subkey = ('color', 'raster'))
- elif self.prompt == 'raster3d':
- color = UserSettings.Get(group = 'modeler', key = 'data',
- subkey = ('color', 'raster3d'))
- elif self.prompt == 'vector':
- color = UserSettings.Get(group = 'modeler', key = 'data',
- subkey = ('color', 'vector'))
- else:
- color = UserSettings.Get(group = 'modeler', key = 'action',
- subkey = ('color', 'invalid'))
- wxColor = wx.Color(color[0], color[1], color[2])
- self.SetBrush(wx.Brush(wxColor))
-
- def _setPen(self):
- """!Set pen"""
- isParameterized = False
- for rel in self.GetRelations('from'):
- if rel.GetTo().IsParameterized():
- isParameterized = True
- break
- if not isParameterized:
- for rel in self.GetRelations('to'):
- if rel.GetFrom().IsParameterized():
- isParameterized = True
- break
- if isParameterized:
- width = int(UserSettings.Get(group = 'modeler', key = 'action',
- subkey = ('width', 'parameterized')))
- else:
- width = int(UserSettings.Get(group = 'modeler', key = 'action',
- subkey = ('width', 'default')))
- pen = self.GetPen()
- pen.SetWidth(width)
- self.SetPen(pen)
-
- def _setText(self):
- """!Update text"""
- self.ClearText()
- name = []
- for rel in self.GetRelations():
- name.append(rel.GetName())
- self.AddText('/'.join(name))
- if self.value:
- self.AddText(self.value)
- else:
- self.AddText(_('<not defined>'))
-
- def Update(self):
- """!Update action"""
- self._setBrush()
- self._setPen()
- self._setText()
-
- class ModelDataDialog(ElementDialog):
- """!Data item properties dialog"""
- def __init__(self, parent, shape, id = wx.ID_ANY, title = _("Data properties"),
- style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER):
- self.parent = parent
- self.shape = shape
-
- label, etype = self._getLabel()
- ElementDialog.__init__(self, parent, title, label = label, etype = etype)
-
- self.element = gselect.Select(parent = self.panel,
- type = prompt)
- self.element.SetValue(shape.GetValue())
-
- self.Bind(wx.EVT_BUTTON, self.OnOK, self.btnOK)
- self.Bind(wx.EVT_BUTTON, self.OnCancel, self.btnCancel)
-
- self.PostInit()
-
- if shape.GetValue():
- self.btnOK.Enable()
-
- self._layout()
- self.SetMinSize(self.GetSize())
-
- def _getLabel(self):
- etype = False
- prompt = self.shape.GetPrompt()
- if prompt == 'raster':
- label = _('Name of raster map:')
- elif prompt == 'vector':
- label = _('Name of vector map:')
- else:
- etype = True
- label = _('Name of element:')
- return label, etype
-
- def _layout(self):
- """!Do layout"""
- self.dataSizer.Add(self.element, proportion=0,
- flag=wx.EXPAND | wx.ALL, border=1)
-
- self.panel.SetSizer(self.sizer)
- self.sizer.Fit(self)
- def OnOK(self, event):
- """!Ok pressed"""
- self.shape.SetValue(self.GetElement())
- if self.etype:
- elem = self.GetType()
- if elem == 'rast':
- self.shape.SetPrompt('raster')
- elif elem == 'vect':
- self.shape.SetPrompt('raster')
-
- self.parent.canvas.Refresh()
- self.parent.SetStatusText('', 0)
- self.shape.SetPropDialog(None)
-
- if self.IsModal():
- event.Skip()
- else:
- self.Destroy()
-
- def OnCancel(self, event):
- """!Cancel pressed"""
- self.shape.SetPropDialog(None)
- if self.IsModal():
- event.Skip()
- else:
- self.Destroy()
-
- class ModelEvtHandler(ogl.ShapeEvtHandler):
- """!Model event handler class"""
- def __init__(self, log, frame):
- ogl.ShapeEvtHandler.__init__(self)
- self.log = log
- self.frame = frame
- self.x = self.y = None
-
- def OnLeftClick(self, x, y, keys = 0, attachment = 0):
- """!Left mouse button pressed -> select item & update statusbar"""
- shape = self.GetShape()
- canvas = shape.GetCanvas()
- dc = wx.ClientDC(canvas)
- canvas.PrepareDC(dc)
-
- if hasattr(self.frame, 'defineRelation'):
- drel = self.frame.defineRelation
- if drel['from'] is None:
- drel['from'] = shape
- elif drel['to'] is None:
- drel['to'] = shape
- rel = ModelRelation(parent = self.frame, fromShape = drel['from'],
- toShape = drel['to'])
- dlg = ModelRelationDialog(parent = self.frame,
- shape = rel)
- if dlg.IsValid():
- ret = dlg.ShowModal()
- if ret == wx.ID_OK:
- option = dlg.GetOption()
- rel.SetName(option)
- drel['from'].AddRelation(rel)
- drel['to'].AddRelation(rel)
- drel['from'].Update()
- params = { 'params' : [{ 'name' : option,
- 'value' : drel['from'].GetValue()}] }
- drel['to'].MergeParams(params)
- self.frame.AddLine(rel)
-
- dlg.Destroy()
- del self.frame.defineRelation
-
- if shape.Selected():
- shape.Select(False, dc)
- else:
- redraw = False
- shapeList = canvas.GetDiagram().GetShapeList()
- toUnselect = list()
-
- for s in shapeList:
- if s.Selected():
- toUnselect.append(s)
-
- shape.Select(True, dc)
-
- for s in toUnselect:
- s.Select(False, dc)
-
- canvas.Refresh(False)
- if hasattr(shape, "GetLog"):
- self.log.SetStatusText(shape.GetLog(), 0)
- else:
- self.log.SetStatusText('', 0)
-
- def OnLeftDoubleClick(self, x, y, keys = 0, attachment = 0):
- """!Left mouse button pressed (double-click) -> show properties"""
- self.OnProperties()
-
- def OnProperties(self, event = None):
- """!Show properties dialog"""
- self.frame.ModelChanged()
- shape = self.GetShape()
- if isinstance(shape, ModelAction):
- module = menuform.GUI(parent = self.frame, show = True).ParseCommand(shape.GetLog(string = False),
- completed = (self.frame.GetOptData, shape, shape.GetParams()))
-
- elif isinstance(shape, ModelData):
- dlg = ModelDataDialog(parent = self.frame, shape = shape)
- shape.SetPropDialog(dlg)
- dlg.CentreOnParent()
- dlg.Show()
-
- elif isinstance(shape, ModelLoop):
- dlg = ModelLoopDialog(parent = self.frame, shape = shape)
- dlg.CentreOnParent()
- if dlg.ShowModal() == wx.ID_OK:
- shape.SetText(dlg.GetCondition())
- alist = list()
- ids = dlg.GetItems()
- for aId in ids['unchecked']:
- action = self.frame.GetModel().GetItem(aId)
- action.UnSetBlock(shape)
- for aId in ids['checked']:
- action = self.frame.GetModel().GetItem(aId)
- action.SetBlock(shape)
- if action:
- alist.append(action)
- shape.SetItems(alist)
- self.frame.DefineLoop(shape)
- self.frame.GetCanvas().Refresh()
-
- dlg.Destroy()
-
- elif isinstance(shape, ModelCondition):
- dlg = ModelConditionDialog(parent = self.frame, shape = shape)
- dlg.CentreOnParent()
- if dlg.ShowModal() == wx.ID_OK:
- shape.SetText(dlg.GetCondition())
- ids = dlg.GetItems()
- for b in ids.keys():
- alist = list()
- for aId in ids[b]['unchecked']:
- action = self.frame.GetModel().GetItem(aId)
- action.UnSetBlock(shape)
- for aId in ids[b]['checked']:
- action = self.frame.GetModel().GetItem(aId)
- action.SetBlock(shape)
- if action:
- alist.append(action)
- shape.SetItems(alist, branch = b)
- self.frame.DefineCondition(shape)
- self.frame.GetCanvas().Refresh()
-
- dlg.Destroy()
-
- def OnBeginDragLeft(self, x, y, keys = 0, attachment = 0):
- """!Drag shape (begining)"""
- self.frame.ModelChanged()
- if self._previousHandler:
- self._previousHandler.OnBeginDragLeft(x, y, keys, attachment)
-
- def OnEndDragLeft(self, x, y, keys = 0, attachment = 0):
- """!Drag shape (end)"""
- if self._previousHandler:
- self._previousHandler.OnEndDragLeft(x, y, keys, attachment)
-
- shape = self.GetShape()
- if isinstance(shape, ModelLoop):
- self.frame.DefineLoop(shape)
- elif isinstance(shape, ModelCondition):
- self.frame.DefineCondition(shape)
-
- for mo in shape.GetBlock():
- if isinstance(mo, ModelLoop):
- self.frame.DefineLoop(mo)
- elif isinstance(mo, ModelCondition):
- self.frame.DefineCondition(mo)
-
- def OnEndSize(self, x, y):
- """!Resize shape"""
- self.frame.ModelChanged()
- if self._previousHandler:
- self._previousHandler.OnEndSize(x, y)
-
- def OnRightClick(self, x, y, keys = 0, attachment = 0):
- """!Right click -> pop-up menu"""
- if not hasattr (self, "popupID1"):
- self.popupID1 = wx.NewId()
- self.popupID2 = wx.NewId()
- self.popupID3 = wx.NewId()
- self.popupID4 = wx.NewId()
- # record coordinates
- self.x = x
- self.y = y
-
- shape = self.GetShape()
- popupMenu = wx.Menu()
- popupMenu.Append(self.popupID1, text=_('Remove'))
- self.frame.Bind(wx.EVT_MENU, self.OnRemove, id = self.popupID1)
- if isinstance(shape, ModelAction):
- if shape.IsEnabled():
- popupMenu.Append(self.popupID3, text=_('Disable'))
- self.frame.Bind(wx.EVT_MENU, self.OnDisable, id = self.popupID3)
- else:
- popupMenu.Append(self.popupID3, text=_('Enable'))
- self.frame.Bind(wx.EVT_MENU, self.OnEnable, id = self.popupID3)
-
- if isinstance(shape, ModelRelation):
- popupMenu.AppendSeparator()
- popupMenu.Append(self.popupID2, text=_('Add control point'))
- self.frame.Bind(wx.EVT_MENU, self.OnAddPoint, id = self.popupID2)
- popupMenu.Append(self.popupID3, text=_('Remove control point'))
- self.frame.Bind(wx.EVT_MENU, self.OnRemovePoint, id = self.popupID3)
- if len(shape.GetLineControlPoints()) == 2:
- popupMenu.Enable(self.popupID3, False)
-
- if isinstance(shape, ModelData) and '@' not in shape.GetValue():
- popupMenu.AppendSeparator()
- popupMenu.Append(self.popupID3, text=_('Intermediate'),
- kind = wx.ITEM_CHECK)
- if self.GetShape().IsIntermediate():
- popupMenu.Check(self.popupID3, True)
-
- self.frame.Bind(wx.EVT_MENU, self.OnIntermediate, id = self.popupID3)
-
- if isinstance(shape, ModelData) or \
- isinstance(shape, ModelAction) or \
- isinstance(shape, ModelLoop):
- popupMenu.AppendSeparator()
- popupMenu.Append(self.popupID2, text=_('Properties'))
- self.frame.Bind(wx.EVT_MENU, self.OnProperties, id = self.popupID2)
-
- if isinstance(shape, ModelAction):
- popupMenu.Append(self.popupID4, text=_('Change ID'))
- self.frame.Bind(wx.EVT_MENU, self.OnChangeId, id = self.popupID3)
-
- self.frame.PopupMenu(popupMenu)
- popupMenu.Destroy()
- def OnChangeId(self, event):
- """!Change action id"""
- pass
-
- def OnDisable(self, event):
- """!Disable action"""
- action = self.GetShape()
- action.Enable(False)
- self.frame.ModelChanged()
- self.frame.canvas.Refresh()
- def OnEnable(self, event):
- """!Disable action"""
- action = self.GetShape()
- action.Enable(True)
- self.frame.ModelChanged()
- self.frame.canvas.Refresh()
-
- def OnAddPoint(self, event):
- """!Add control point"""
- shape = self.GetShape()
- shape.InsertLineControlPoint(point = wx.RealPoint(self.x, self.y))
- shape.ResetShapes()
- shape.Select(True)
- self.frame.ModelChanged()
- self.frame.canvas.Refresh()
-
- def OnRemovePoint(self, event):
- """!Remove control point"""
- shape = self.GetShape()
- shape.DeleteLineControlPoint()
- shape.Select(False)
- shape.Select(True)
- self.frame.ModelChanged()
- self.frame.canvas.Refresh()
-
- def OnIntermediate(self, event):
- """!Mark data as intermediate"""
- self.frame.ModelChanged()
- shape = self.GetShape()
- shape.SetIntermediate(event.IsChecked())
- self.frame.canvas.Refresh()
- def OnRemove(self, event):
- """!Remove shape
- """
- self.frame.GetCanvas().RemoveSelected()
- self.frame.itemPanel.Update()
-
- class ModelSearchDialog(wx.Dialog):
- def __init__(self, parent, id = wx.ID_ANY, title = _("Add new GRASS module to the model"),
- style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER, **kwargs):
- """!Graphical modeler module search window
-
- @param parent parent window
- @param id window id
- @param title window title
- @param kwargs wx.Dialogs' arguments
- """
- self.parent = parent
-
- wx.Dialog.__init__(self, parent = parent, id = id, title = title, **kwargs)
- self.SetName("ModelerDialog")
- self.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR, 'grass.ico'), wx.BITMAP_TYPE_ICO))
-
- self.panel = wx.Panel(parent = self, id = wx.ID_ANY)
-
- self.cmdBox = wx.StaticBox(parent = self.panel, id = wx.ID_ANY,
- label=" %s " % _("Command"))
-
- self.cmd_prompt = prompt.GPromptSTC(parent = self)
- self.search = SearchModuleWindow(parent = self.panel, cmdPrompt = self.cmd_prompt, showTip = True)
- wx.CallAfter(self.cmd_prompt.SetFocus)
-
- # get commands
- items = self.cmd_prompt.GetCommandItems()
-
- self.btnCancel = wx.Button(self.panel, wx.ID_CANCEL)
- self.btnOk = wx.Button(self.panel, wx.ID_OK)
- self.btnOk.SetDefault()
- self.btnOk.Enable(False)
- self.cmd_prompt.Bind(wx.EVT_KEY_UP, self.OnText)
- self.search.searchChoice.Bind(wx.EVT_CHOICE, self.OnText)
- self.Bind(wx.EVT_BUTTON, self.OnOk, self.btnOk)
-
- self._layout()
-
- self.SetSize((500, 275))
-
- def _layout(self):
- cmdSizer = wx.StaticBoxSizer(self.cmdBox, wx.VERTICAL)
- cmdSizer.Add(item = self.cmd_prompt, proportion = 1,
- flag = wx.EXPAND)
-
- btnSizer = wx.StdDialogButtonSizer()
- btnSizer.AddButton(self.btnCancel)
- btnSizer.AddButton(self.btnOk)
- btnSizer.Realize()
-
- mainSizer = wx.BoxSizer(wx.VERTICAL)
- mainSizer.Add(item = self.search, proportion = 0,
- flag = wx.EXPAND | wx.ALL, border = 3)
- mainSizer.Add(item = cmdSizer, proportion = 1,
- flag = wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP, border = 3)
- mainSizer.Add(item = btnSizer, proportion = 0,
- flag = wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border = 5)
-
- self.panel.SetSizer(mainSizer)
- mainSizer.Fit(self.panel)
-
- self.Layout()
- def GetPanel(self):
- """!Get dialog panel"""
- return self.panel
- def GetCmd(self):
- """!Get command"""
- line = self.cmd_prompt.GetCurLine()[0].strip()
- if len(line) == 0:
- list()
-
- try:
- cmd = utils.split(str(line))
- except UnicodeError:
- cmd = utils.split(utils.EncodeString((line)))
-
- return cmd
-
- def OnOk(self, event):
- """!Button 'OK' pressed"""
- self.btnOk.SetFocus()
- cmd = self.GetCmd()
-
- if len(cmd) < 1:
- GError(parent = self,
- message = _("Command not defined.\n\n"
- "Unable to add new action to the model."))
- return
-
- if cmd[0] not in globalvar.grassCmd['all']:
- GError(parent = self,
- message = _("'%s' is not a GRASS module.\n\n"
- "Unable to add new action to the model.") % cmd[0])
- return
-
- self.EndModal(wx.ID_OK)
-
- def OnText(self, event):
- """!Text in prompt changed"""
- if self.cmd_prompt.AutoCompActive():
- event.Skip()
- return
-
- if isinstance(event, wx.KeyEvent):
- entry = self.cmd_prompt.GetTextLeft()
- elif isinstance(event, wx.stc.StyledTextEvent):
- entry = event.GetText()
- else:
- entry = event.GetString()
-
- if entry:
- self.btnOk.Enable()
- else:
- self.btnOk.Enable(False)
-
- event.Skip()
-
- def Reset(self):
- """!Reset dialog"""
- self.search.Reset()
- self.cmd_prompt.OnCmdErase(None)
- self.btnOk.Enable(False)
- self.cmd_prompt.SetFocus()
- class ModelRelation(ogl.LineShape):
- """!Data - action relation"""
- def __init__(self, parent, fromShape, toShape, param = ''):
- self.fromShape = fromShape
- self.toShape = toShape
- self.param = param
- self.parent = parent
-
- self._points = None
-
- if self.parent.GetCanvas():
- ogl.LineShape.__init__(self)
-
- def __del__(self):
- self.fromShape.rels.remove(self)
- self.toShape.rels.remove(self)
- def GetFrom(self):
- """!Get id of 'from' shape"""
- return self.fromShape
-
- def GetTo(self):
- """!Get id of 'to' shape"""
- return self.toShape
-
- def GetData(self):
- """!Get related ModelData instance
- @return ModelData instance
- @return None if not found
- """
- if isinstance(self.fromShape, ModelData):
- return self.fromShape
- elif isinstance(self.toShape, ModelData):
- return self.toShape
-
- return None
-
- def GetName(self):
- """!Get parameter name"""
- return self.param
-
- def ResetShapes(self):
- """!Reset related objects"""
- self.fromShape.ResetControlPoints()
- self.toShape.ResetControlPoints()
- self.ResetControlPoints()
-
- def SetControlPoints(self, points):
- """!Set control points"""
- self._points = points
-
- def GetControlPoints(self):
- """!Get list of control points"""
- return self._points
-
- def _setPen(self):
- """!Set pen"""
- pen = self.GetPen()
- pen.SetWidth(1)
- pen.SetStyle(wx.SOLID)
- self.SetPen(pen)
-
- def OnDraw(self, dc):
- """!Draw relation"""
- self._setPen()
- ogl.LineShape.OnDraw(self, dc)
-
- def SetName(self, param):
- self.param = param
-
- class ModelRelationDialog(wx.Dialog):
- """!Relation properties dialog"""
- def __init__(self, parent, shape, id = wx.ID_ANY, title = _("Relation properties"),
- style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER, **kwargs):
- self.parent = parent
- self.shape = shape
-
- options = self._getOptions()
- if not options:
- self.valid = False
- return
-
- self.valid = True
- wx.Dialog.__init__(self, parent, id, title, style = style, **kwargs)
- self.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR, 'grass.ico'), wx.BITMAP_TYPE_ICO))
-
- self.panel = wx.Panel(parent = self, id = wx.ID_ANY)
-
- self.fromBox = wx.StaticBox(parent = self.panel, id = wx.ID_ANY,
- label = " %s " % _("From"))
- self.toBox = wx.StaticBox(parent = self.panel, id = wx.ID_ANY,
- label = " %s " % _("To"))
-
- self.option = wx.ComboBox(parent = self.panel, id = wx.ID_ANY,
- style = wx.CB_READONLY,
- choices = options)
- self.option.Bind(wx.EVT_COMBOBOX, self.OnOption)
-
- self.btnCancel = wx.Button(self.panel, wx.ID_CANCEL)
- self.btnOk = wx.Button(self.panel, wx.ID_OK)
- self.btnOk.Enable(False)
-
- self._layout()
- def _layout(self):
- mainSizer = wx.BoxSizer(wx.VERTICAL)
- fromSizer = wx.StaticBoxSizer(self.fromBox, wx.VERTICAL)
- self._layoutShape(shape = self.shape.GetFrom(), sizer = fromSizer)
- toSizer = wx.StaticBoxSizer(self.toBox, wx.VERTICAL)
- self._layoutShape(shape = self.shape.GetTo(), sizer = toSizer)
- btnSizer = wx.StdDialogButtonSizer()
- btnSizer.AddButton(self.btnCancel)
- btnSizer.AddButton(self.btnOk)
- btnSizer.Realize()
-
- mainSizer.Add(item = fromSizer, proportion = 0,
- flag = wx.EXPAND | wx.ALL, border = 5)
- mainSizer.Add(item = toSizer, proportion = 0,
- flag = wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, border = 5)
- mainSizer.Add(item = btnSizer, proportion = 0,
- flag = wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border = 5)
-
- self.panel.SetSizer(mainSizer)
- mainSizer.Fit(self.panel)
-
- self.Layout()
- self.SetSize(self.GetBestSize())
-
- def _layoutShape(self, shape, sizer):
- if isinstance(shape, ModelData):
- sizer.Add(item = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
- label = _("Data: %s") % shape.GetLog()),
- proportion = 1, flag = wx.EXPAND | wx.ALL,
- border = 5)
- elif isinstance(shape, ModelAction):
- gridSizer = wx.GridBagSizer (hgap = 5, vgap = 5)
- gridSizer.Add(item = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
- label = _("Command:")),
- pos = (0, 0))
- gridSizer.Add(item = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
- label = shape.GetName()),
- pos = (0, 1))
- gridSizer.Add(item = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
- label = _("Option:")),
- flag = wx.ALIGN_CENTER_VERTICAL,
- pos = (1, 0))
- gridSizer.Add(item = self.option,
- pos = (1, 1))
- sizer.Add(item = gridSizer,
- proportion = 1, flag = wx.EXPAND | wx.ALL,
- border = 5)
-
- def _getOptions(self):
- """!Get relevant options"""
- items = []
- fromShape = self.shape.GetFrom()
- if not isinstance(fromShape, ModelData):
- GError(parent = self.parent,
- message = _("Relation doesn't start with data item.\n"
- "Unable to add relation."))
- return items
-
- toShape = self.shape.GetTo()
- if not isinstance(toShape, ModelAction):
- GError(parent = self.parent,
- message = _("Relation doesn't point to GRASS command.\n"
- "Unable to add relation."))
- return items
-
- prompt = fromShape.GetPrompt()
- task = toShape.GetTask()
- for p in task.get_options()['params']:
- if p.get('prompt', '') == prompt and \
- 'name' in p:
- items.append(p['name'])
-
- if not items:
- GError(parent = self.parent,
- message = _("No relevant option found.\n"
- "Unable to add relation."))
- return items
-
- def GetOption(self):
- """!Get selected option"""
- return self.option.GetStringSelection()
-
- def IsValid(self):
- """!Check if relation is valid"""
- return self.valid
-
- def OnOption(self, event):
- """!Set option"""
- if event.GetString():
- self.btnOk.Enable()
- else:
- self.btnOk.Enable(False)
-
- class ProcessModelFile:
- """!Process GRASS model file (gxm)"""
- def __init__(self, tree):
- """!A ElementTree handler for the GXM XML file, as defined in
- grass-gxm.dtd.
- """
- self.tree = tree
- self.root = self.tree.getroot()
-
- # list of actions, data
- self.properties = dict()
- self.variables = dict()
- self.actions = list()
- self.data = list()
- self.loops = list()
- self.conditions = list()
-
- self._processWindow()
- self._processProperties()
- self._processVariables()
- self._processItems()
- self._processData()
-
- def _filterValue(self, value):
- """!Filter value
-
- @param value
- """
- value = value.replace('<', '<')
- value = value.replace('>', '>')
-
- return value
-
- def _getNodeText(self, node, tag, default = ''):
- """!Get node text"""
- p = node.find(tag)
- if p is not None:
- if p.text:
- return utils.normalize_whitespace(p.text)
- else:
- return ''
-
- return default
-
- def _processWindow(self):
- """!Process window properties"""
- node = self.root.find('window')
- if node is None:
- self.pos = self.size = None
- return
-
- self.pos, self.size = self._getDim(node)
-
- def _processProperties(self):
- """!Process model properties"""
- node = self.root.find('properties')
- if node is None:
- return
- for key in ('name', 'description', 'author'):
- self._processProperty(node, key)
-
- for f in node.findall('flag'):
- name = f.get('name', '')
- if name == 'overwrite':
- self.properties['overwrite'] = True
-
- def _processProperty(self, pnode, name):
- """!Process given property"""
- node = pnode.find(name)
- if node is not None:
- self.properties[name] = node.text
- else:
- self.properties[name] = ''
- def _processVariables(self):
- """!Process model variables"""
- vnode = self.root.find('variables')
- if vnode is None:
- return
- for node in vnode.findall('variable'):
- name = node.get('name', '')
- if not name:
- continue # should not happen
- self.variables[name] = { 'type' : node.get('type', 'string') }
- for key in ('description', 'value'):
- self._processVariable(node, name, key)
-
- def _processVariable(self, pnode, name, key):
- """!Process given variable"""
- node = pnode.find(key)
- if node is not None:
- if node.text:
- self.variables[name][key] = node.text
- def _processItems(self):
- """!Process model items (actions, loops, conditions)"""
- self._processActions()
- self._processLoops()
- self._processConditions()
-
- def _processActions(self):
- """!Process model file"""
- for action in self.root.findall('action'):
- pos, size = self._getDim(action)
- disabled = False
-
- task = action.find('task')
- if task is not None:
- if task.find('disabled') is not None:
- disabled = True
- task = self._processTask(task)
- else:
- task = None
-
- aId = int(action.get('id', -1))
-
- self.actions.append({ 'pos' : pos,
- 'size' : size,
- 'task' : task,
- 'id' : aId,
- 'disabled' : disabled })
-
- def _getDim(self, node):
- """!Get position and size of shape"""
- pos = size = None
- posAttr = node.get('pos', None)
- if posAttr:
- posVal = map(int, posAttr.split(','))
- try:
- pos = (posVal[0], posVal[1])
- except:
- pos = None
-
- sizeAttr = node.get('size', None)
- if sizeAttr:
- sizeVal = map(int, sizeAttr.split(','))
- try:
- size = (sizeVal[0], sizeVal[1])
- except:
- size = None
-
- return pos, size
-
- def _processData(self):
- """!Process model file"""
- for data in self.root.findall('data'):
- pos, size = self._getDim(data)
- param = data.find('data-parameter')
- prompt = value = None
- if param is not None:
- prompt = param.get('prompt', None)
- value = self._filterValue(self._getNodeText(param, 'value'))
-
- if data.find('intermediate') is None:
- intermediate = False
- else:
- intermediate = True
-
- rels = list()
- for rel in data.findall('relation'):
- defrel = { 'id' : int(rel.get('id', -1)),
- 'dir' : rel.get('dir', 'to'),
- 'name' : rel.get('name', '') }
- points = list()
- for point in rel.findall('point'):
- x = self._filterValue(self._getNodeText(point, 'x'))
- y = self._filterValue(self._getNodeText(point, 'y'))
- points.append((float(x), float(y)))
- defrel['points'] = points
- rels.append(defrel)
-
- self.data.append({ 'pos' : pos,
- 'size': size,
- 'prompt' : prompt,
- 'value' : value,
- 'intermediate' : intermediate,
- 'rels' : rels })
-
- def _processTask(self, node):
- """!Process task
- @return grassTask instance
- @return None on error
- """
- cmd = list()
- parameterized = list()
-
- name = node.get('name', None)
- if not name:
- return None
-
- cmd.append(name)
-
- # flags
- for f in node.findall('flag'):
- flag = f.get('name', '')
- if f.get('parameterized', '0') == '1':
- parameterized.append(('flag', flag))
- if f.get('value', '1') == '0':
- continue
- if len(flag) > 1:
- cmd.append('--' + flag)
- else:
- cmd.append('-' + flag)
- # parameters
- for p in node.findall('parameter'):
- name = p.get('name', '')
- if p.find('parameterized') is not None:
- parameterized.append(('param', name))
- cmd.append('%s=%s' % (name,
- self._filterValue(self._getNodeText(p, 'value'))))
-
- task, err = menuform.GUI(show = None, checkError = True).ParseCommand(cmd = cmd)
- if err:
- GWarning(os.linesep.join(err))
-
- for opt, name in parameterized:
- if opt == 'flag':
- task.set_flag(name, True, element = 'parameterized')
- else:
- task.set_param(name, True, element = 'parameterized')
-
- return task
- def _processLoops(self):
- """!Process model loops"""
- for node in self.root.findall('loop'):
- pos, size = self._getDim(node)
- text = self._filterValue(self._getNodeText(node, 'condition')).strip()
- aid = list()
- for anode in node.findall('item'):
- try:
- aid.append(int(anode.text))
- except ValueError:
- pass
-
- self.loops.append({ 'pos' : pos,
- 'size' : size,
- 'text' : text,
- 'id' : int(node.get('id', -1)),
- 'items' : aid })
-
- def _processConditions(self):
- """!Process model conditions"""
- for node in self.root.findall('if-else'):
- pos, size = self._getDim(node)
- text = self._filterValue(self._getNodeText(node, 'condition')).strip()
- aid = { 'if' : list(),
- 'else' : list() }
- for b in aid.keys():
- bnode = node.find(b)
- if bnode is None:
- continue
- for anode in bnode.findall('item'):
- try:
- aid[b].append(int(anode.text))
- except ValueError:
- pass
-
- self.conditions.append({ 'pos' : pos,
- 'size' : size,
- 'text' : text,
- 'id' : int(node.get('id', -1)),
- 'items' : aid })
-
- class WriteModelFile:
- """!Generic class for writing model file"""
- def __init__(self, fd, model):
- self.fd = fd
- self.model = model
- self.properties = model.GetProperties()
- self.variables = model.GetVariables()
- self.items = model.GetItems()
-
- self.indent = 0
-
- self._header()
-
- self._window()
- self._properties()
- self._variables()
- self._items()
-
- dataList = list()
- for action in model.GetItems(objType = ModelAction):
- for rel in action.GetRelations():
- dataItem = rel.GetData()
- if dataItem not in dataList:
- dataList.append(dataItem)
- self._data(dataList)
-
- self._footer()
- def _filterValue(self, value):
- """!Make value XML-valid"""
- value = value.replace('<', '<')
- value = value.replace('>', '>')
-
- return value
-
- def _header(self):
- """!Write header"""
- self.fd.write('<?xml version="1.0" encoding="UTF-8"?>\n')
- self.fd.write('<!DOCTYPE gxm SYSTEM "grass-gxm.dtd">\n')
- self.fd.write('%s<gxm>\n' % (' ' * self.indent))
- self.indent += 4
-
- def _footer(self):
- """!Write footer"""
- self.indent -= 4
- self.fd.write('%s</gxm>\n' % (' ' * self.indent))
- def _window(self):
- """!Write window properties"""
- canvas = self.model.GetCanvas()
- if canvas is None:
- return
- win = canvas.parent
- pos = win.GetPosition()
- size = win.GetSize()
- self.fd.write('%s<window pos="%d,%d" size="%d,%d" />\n' % \
- (' ' * self.indent, pos[0], pos[1], size[0], size[1]))
-
- def _properties(self):
- """!Write model properties"""
- self.fd.write('%s<properties>\n' % (' ' * self.indent))
- self.indent += 4
- if self.properties['name']:
- self.fd.write('%s<name>%s</name>\n' % (' ' * self.indent, self.properties['name']))
- if self.properties['description']:
- self.fd.write('%s<description>%s</description>\n' % (' ' * self.indent, self.properties['description']))
- if self.properties['author']:
- self.fd.write('%s<author>%s</author>\n' % (' ' * self.indent, self.properties['author']))
-
- if 'overwrite' in self.properties and \
- self.properties['overwrite']:
- self.fd.write('%s<flag name="overwrite" />\n' % (' ' * self.indent))
- self.indent -= 4
- self.fd.write('%s</properties>\n' % (' ' * self.indent))
- def _variables(self):
- """!Write model variables"""
- if not self.variables:
- return
- self.fd.write('%s<variables>\n' % (' ' * self.indent))
- self.indent += 4
- for name, values in self.variables.iteritems():
- self.fd.write('%s<variable name="%s" type="%s">\n' % \
- (' ' * self.indent, name, values['type']))
- self.indent += 4
- if 'value' in values:
- self.fd.write('%s<value>%s</value>\n' % \
- (' ' * self.indent, values['value']))
- if 'description' in values:
- self.fd.write('%s<description>%s</description>\n' % \
- (' ' * self.indent, values['description']))
- self.indent -= 4
- self.fd.write('%s</variable>\n' % (' ' * self.indent))
- self.indent -= 4
- self.fd.write('%s</variables>\n' % (' ' * self.indent))
-
- def _items(self):
- """!Write actions/loops/conditions"""
- for item in self.items:
- if isinstance(item, ModelAction):
- self._action(item)
- elif isinstance(item, ModelLoop):
- self._loop(item)
- elif isinstance(item, ModelCondition):
- self._condition(item)
-
- def _action(self, action):
- """!Write actions"""
- self.fd.write('%s<action id="%d" name="%s" pos="%d,%d" size="%d,%d">\n' % \
- (' ' * self.indent, action.GetId(), action.GetName(), action.GetX(), action.GetY(),
- action.GetWidth(), action.GetHeight()))
- self.indent += 4
- self.fd.write('%s<task name="%s">\n' % (' ' * self.indent, action.GetLog(string = False)[0]))
- self.indent += 4
- if not action.IsEnabled():
- self.fd.write('%s<disabled />\n' % (' ' * self.indent))
- for key, val in action.GetParams().iteritems():
- if key == 'flags':
- for f in val:
- if f.get('value', False) or f.get('parameterized', False):
- if f.get('parameterized', False):
- if f.get('value', False) == False:
- self.fd.write('%s<flag name="%s" value="0" parameterized="1" />\n' %
- (' ' * self.indent, f.get('name', '')))
- else:
- self.fd.write('%s<flag name="%s" parameterized="1" />\n' %
- (' ' * self.indent, f.get('name', '')))
- else:
- self.fd.write('%s<flag name="%s" />\n' %
- (' ' * self.indent, f.get('name', '')))
- else: # parameter
- for p in val:
- if not p.get('value', ''):
- continue
- self.fd.write('%s<parameter name="%s">\n' %
- (' ' * self.indent, p.get('name', '')))
- self.indent += 4
- if p.get('parameterized', False):
- self.fd.write('%s<parameterized />\n' % (' ' * self.indent))
- self.fd.write('%s<value>%s</value>\n' %
- (' ' * self.indent, self._filterValue(p.get('value', ''))))
- self.indent -= 4
- self.fd.write('%s</parameter>\n' % (' ' * self.indent))
- self.indent -= 4
- self.fd.write('%s</task>\n' % (' ' * self.indent))
- self.indent -= 4
- self.fd.write('%s</action>\n' % (' ' * self.indent))
-
- def _data(self, dataList):
- """!Write data"""
- for data in dataList:
- self.fd.write('%s<data pos="%d,%d" size="%d,%d">\n' % \
- (' ' * self.indent, data.GetX(), data.GetY(),
- data.GetWidth(), data.GetHeight()))
- self.indent += 4
- self.fd.write('%s<data-parameter prompt="%s">\n' % \
- (' ' * self.indent, data.GetPrompt()))
- self.indent += 4
- self.fd.write('%s<value>%s</value>\n' %
- (' ' * self.indent, self._filterValue(data.GetValue())))
- self.indent -= 4
- self.fd.write('%s</data-parameter>\n' % (' ' * self.indent))
-
- if data.IsIntermediate():
- self.fd.write('%s<intermediate />\n' % (' ' * self.indent))
- # relations
- for ft in ('from', 'to'):
- for rel in data.GetRelations(ft):
- if ft == 'from':
- aid = rel.GetTo().GetId()
- else:
- aid = rel.GetFrom().GetId()
- self.fd.write('%s<relation dir="%s" id="%d" name="%s">\n' % \
- (' ' * self.indent, ft, aid, rel.GetName()))
- self.indent += 4
- for point in rel.GetLineControlPoints()[1:-1]:
- self.fd.write('%s<point>\n' % (' ' * self.indent))
- self.indent += 4
- x, y = point.Get()
- self.fd.write('%s<x>%d</x>\n' % (' ' * self.indent, int(x)))
- self.fd.write('%s<y>%d</y>\n' % (' ' * self.indent, int(y)))
- self.indent -= 4
- self.fd.write('%s</point>\n' % (' ' * self.indent))
- self.indent -= 4
- self.fd.write('%s</relation>\n' % (' ' * self.indent))
-
- self.indent -= 4
- self.fd.write('%s</data>\n' % (' ' * self.indent))
- def _loop(self, loop):
- """!Write loops"""
- self.fd.write('%s<loop id="%d" pos="%d,%d" size="%d,%d">\n' % \
- (' ' * self.indent, loop.GetId(), loop.GetX(), loop.GetY(),
- loop.GetWidth(), loop.GetHeight()))
- text = loop.GetText()
- self.indent += 4
- if text:
- self.fd.write('%s<condition>%s</condition>\n' %
- (' ' * self.indent, self._filterValue(text)))
- for item in loop.GetItems():
- self.fd.write('%s<item>%d</item>\n' %
- (' ' * self.indent, item.GetId()))
- self.indent -= 4
- self.fd.write('%s</loop>\n' % (' ' * self.indent))
- def _condition(self, condition):
- """!Write conditions"""
- bbox = condition.GetBoundingBoxMin()
- self.fd.write('%s<if-else id="%d" pos="%d,%d" size="%d,%d">\n' % \
- (' ' * self.indent, condition.GetId(), condition.GetX(), condition.GetY(),
- bbox[0], bbox[1]))
- text = condition.GetText()
- self.indent += 4
- if text:
- self.fd.write('%s<condition>%s</condition>\n' %
- (' ' * self.indent, self._filterValue(text)))
- items = condition.GetItems()
- for b in items.keys():
- if len(items[b]) < 1:
- continue
- self.fd.write('%s<%s>\n' % (' ' * self.indent, b))
- self.indent += 4
- for item in items[b]:
- self.fd.write('%s<item>%d</item>\n' %
- (' ' * self.indent, item.GetId()))
- self.indent -= 4
- self.fd.write('%s</%s>\n' % (' ' * self.indent, b))
-
- self.indent -= 4
- self.fd.write('%s</if-else>\n' % (' ' * self.indent))
-
- class PreferencesDialog(PreferencesBaseDialog):
- """!User preferences dialog"""
- def __init__(self, parent, settings = UserSettings,
- title = _("Modeler settings")):
-
- PreferencesBaseDialog.__init__(self, parent = parent, title = title,
- settings = settings)
-
- # create notebook pages
- self._createActionPage(self.notebook)
- self._createDataPage(self.notebook)
-
- self.SetMinSize(self.GetBestSize())
- self.SetSize(self.size)
- def _createActionPage(self, notebook):
- """!Create notebook page for action settings"""
- panel = wx.Panel(parent = notebook, id = wx.ID_ANY)
- notebook.AddPage(page = panel, text = _("Action"))
-
- # colors
- border = wx.BoxSizer(wx.VERTICAL)
- box = wx.StaticBox (parent = panel, id = wx.ID_ANY,
- label = " %s " % _("Validity"))
- sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
-
- gridSizer = wx.GridBagSizer (hgap = 3, vgap = 3)
- gridSizer.AddGrowableCol(0)
-
- row = 0
- gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
- label = _("Valid:")),
- flag = wx.ALIGN_LEFT |
- wx.ALIGN_CENTER_VERTICAL,
- pos = (row, 0))
- vColor = csel.ColourSelect(parent = panel, id = wx.ID_ANY,
- colour = self.settings.Get(group='modeler', key='action', subkey=('color', 'valid')),
- size = globalvar.DIALOG_COLOR_SIZE)
- vColor.SetName('GetColour')
- self.winId['modeler:action:color:valid'] = vColor.GetId()
-
- gridSizer.Add(item = vColor,
- flag = wx.ALIGN_RIGHT |
- wx.ALIGN_CENTER_VERTICAL,
- pos = (row, 1))
- row += 1
- gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
- label = _("Invalid:")),
- flag = wx.ALIGN_LEFT |
- wx.ALIGN_CENTER_VERTICAL,
- pos = (row, 0))
- iColor = csel.ColourSelect(parent = panel, id = wx.ID_ANY,
- colour = self.settings.Get(group='modeler', key='action', subkey=('color', 'invalid')),
- size = globalvar.DIALOG_COLOR_SIZE)
- iColor.SetName('GetColour')
- self.winId['modeler:action:color:invalid'] = iColor.GetId()
-
- gridSizer.Add(item = iColor,
- flag = wx.ALIGN_RIGHT |
- wx.ALIGN_CENTER_VERTICAL,
- pos = (row, 1))
- row += 1
- gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
- label = _("Running:")),
- flag = wx.ALIGN_LEFT |
- wx.ALIGN_CENTER_VERTICAL,
- pos = (row, 0))
- rColor = csel.ColourSelect(parent = panel, id = wx.ID_ANY,
- colour = self.settings.Get(group='modeler', key='action', subkey=('color', 'running')),
- size = globalvar.DIALOG_COLOR_SIZE)
- rColor.SetName('GetColour')
- self.winId['modeler:action:color:running'] = rColor.GetId()
-
- gridSizer.Add(item = rColor,
- flag = wx.ALIGN_RIGHT |
- wx.ALIGN_CENTER_VERTICAL,
- pos = (row, 1))
- row += 1
- gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
- label = _("Disabled:")),
- flag = wx.ALIGN_LEFT |
- wx.ALIGN_CENTER_VERTICAL,
- pos = (row, 0))
- rColor = csel.ColourSelect(parent = panel, id = wx.ID_ANY,
- colour = self.settings.Get(group='modeler', key='action', subkey=('color', 'disabled')),
- size = globalvar.DIALOG_COLOR_SIZE)
- rColor.SetName('GetColour')
- self.winId['modeler:action:color:disabled'] = rColor.GetId()
-
- gridSizer.Add(item = rColor,
- flag = wx.ALIGN_RIGHT |
- wx.ALIGN_CENTER_VERTICAL,
- pos = (row, 1))
- sizer.Add(item = gridSizer, proportion = 1, flag = wx.ALL | wx.EXPAND, border = 5)
- border.Add(item = sizer, proportion = 0, flag = wx.LEFT | wx.RIGHT | wx.BOTTOM | wx.EXPAND, border = 3)
-
- # size
- box = wx.StaticBox (parent = panel, id = wx.ID_ANY,
- label = " %s " % _("Shape size"))
- sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
-
- gridSizer = wx.GridBagSizer (hgap=3, vgap=3)
- gridSizer.AddGrowableCol(0)
- row = 0
- gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
- label = _("Width:")),
- flag = wx.ALIGN_LEFT |
- wx.ALIGN_CENTER_VERTICAL,
- pos = (row, 0))
-
- width = wx.SpinCtrl(parent = panel, id = wx.ID_ANY,
- min = 0, max = 500,
- initial = self.settings.Get(group='modeler', key='action', subkey=('size', 'width')))
- width.SetName('GetValue')
- self.winId['modeler:action:size:width'] = width.GetId()
-
- gridSizer.Add(item = width,
- flag = wx.ALIGN_RIGHT |
- wx.ALIGN_CENTER_VERTICAL,
- pos = (row, 1))
- row += 1
- gridSizer.Add(item = wx.StaticText(parent=panel, id=wx.ID_ANY,
- label=_("Height:")),
- flag = wx.ALIGN_LEFT |
- wx.ALIGN_CENTER_VERTICAL,
- pos=(row, 0))
-
- height = wx.SpinCtrl(parent = panel, id = wx.ID_ANY,
- min = 0, max = 500,
- initial = self.settings.Get(group='modeler', key='action', subkey=('size', 'height')))
- height.SetName('GetValue')
- self.winId['modeler:action:size:height'] = height.GetId()
-
- gridSizer.Add(item = height,
- flag = wx.ALIGN_RIGHT |
- wx.ALIGN_CENTER_VERTICAL,
- pos = (row, 1))
-
- sizer.Add(item=gridSizer, proportion=1, flag=wx.ALL | wx.EXPAND, border=5)
- border.Add(item=sizer, proportion=0, flag=wx.LEFT | wx.RIGHT | wx.BOTTOM | wx.EXPAND, border=3)
-
- panel.SetSizer(border)
-
- return panel
- def _createDataPage(self, notebook):
- """!Create notebook page for data settings"""
- panel = wx.Panel(parent = notebook, id = wx.ID_ANY)
- notebook.AddPage(page = panel, text = _("Data"))
-
- # colors
- border = wx.BoxSizer(wx.VERTICAL)
- box = wx.StaticBox (parent = panel, id = wx.ID_ANY,
- label = " %s " % _("Type"))
- sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
-
- gridSizer = wx.GridBagSizer (hgap = 3, vgap = 3)
- gridSizer.AddGrowableCol(0)
-
- row = 0
- gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
- label = _("Raster:")),
- flag = wx.ALIGN_LEFT |
- wx.ALIGN_CENTER_VERTICAL,
- pos = (row, 0))
- rColor = csel.ColourSelect(parent = panel, id = wx.ID_ANY,
- colour = self.settings.Get(group='modeler', key='data', subkey=('color', 'raster')),
- size = globalvar.DIALOG_COLOR_SIZE)
- rColor.SetName('GetColour')
- self.winId['modeler:data:color:raster'] = rColor.GetId()
-
- gridSizer.Add(item = rColor,
- flag = wx.ALIGN_RIGHT |
- wx.ALIGN_CENTER_VERTICAL,
- pos = (row, 1))
- row += 1
- gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
- label = _("3D raster:")),
- flag = wx.ALIGN_LEFT |
- wx.ALIGN_CENTER_VERTICAL,
- pos = (row, 0))
- r3Color = csel.ColourSelect(parent = panel, id = wx.ID_ANY,
- colour = self.settings.Get(group='modeler', key='data', subkey=('color', 'raster3d')),
- size = globalvar.DIALOG_COLOR_SIZE)
- r3Color.SetName('GetColour')
- self.winId['modeler:data:color:raster3d'] = r3Color.GetId()
-
- gridSizer.Add(item = r3Color,
- flag = wx.ALIGN_RIGHT |
- wx.ALIGN_CENTER_VERTICAL,
- pos = (row, 1))
-
- row += 1
- gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
- label = _("Vector:")),
- flag = wx.ALIGN_LEFT |
- wx.ALIGN_CENTER_VERTICAL,
- pos = (row, 0))
- vColor = csel.ColourSelect(parent = panel, id = wx.ID_ANY,
- colour = self.settings.Get(group='modeler', key='data', subkey=('color', 'vector')),
- size = globalvar.DIALOG_COLOR_SIZE)
- vColor.SetName('GetColour')
- self.winId['modeler:data:color:vector'] = vColor.GetId()
-
- gridSizer.Add(item = vColor,
- flag = wx.ALIGN_RIGHT |
- wx.ALIGN_CENTER_VERTICAL,
- pos = (row, 1))
-
- sizer.Add(item = gridSizer, proportion = 1, flag = wx.ALL | wx.EXPAND, border = 5)
- border.Add(item = sizer, proportion = 0, flag = wx.LEFT | wx.RIGHT | wx.BOTTOM | wx.EXPAND, border = 3)
- # size
- box = wx.StaticBox (parent = panel, id = wx.ID_ANY,
- label = " %s " % _("Shape size"))
- sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
-
- gridSizer = wx.GridBagSizer (hgap=3, vgap=3)
- gridSizer.AddGrowableCol(0)
-
- row = 0
- gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
- label = _("Width:")),
- flag = wx.ALIGN_LEFT |
- wx.ALIGN_CENTER_VERTICAL,
- pos = (row, 0))
-
- width = wx.SpinCtrl(parent = panel, id = wx.ID_ANY,
- min = 0, max = 500,
- initial = self.settings.Get(group='modeler', key='data', subkey=('size', 'width')))
- width.SetName('GetValue')
- self.winId['modeler:data:size:width'] = width.GetId()
-
- gridSizer.Add(item = width,
- flag = wx.ALIGN_RIGHT |
- wx.ALIGN_CENTER_VERTICAL,
- pos = (row, 1))
- row += 1
- gridSizer.Add(item = wx.StaticText(parent=panel, id=wx.ID_ANY,
- label=_("Height:")),
- flag = wx.ALIGN_LEFT |
- wx.ALIGN_CENTER_VERTICAL,
- pos=(row, 0))
-
- height = wx.SpinCtrl(parent = panel, id = wx.ID_ANY,
- min = 0, max = 500,
- initial = self.settings.Get(group='modeler', key='data', subkey=('size', 'height')))
- height.SetName('GetValue')
- self.winId['modeler:data:size:height'] = height.GetId()
-
- gridSizer.Add(item = height,
- flag = wx.ALIGN_RIGHT |
- wx.ALIGN_CENTER_VERTICAL,
- pos = (row, 1))
-
- sizer.Add(item=gridSizer, proportion=1, flag=wx.ALL | wx.EXPAND, border=5)
- border.Add(item=sizer, proportion=0, flag=wx.LEFT | wx.RIGHT | wx.BOTTOM | wx.EXPAND, border=3)
-
- panel.SetSizer(border)
-
- return panel
- def OnApply(self, event):
- """!Button 'Apply' pressed"""
- PreferencesBaseDialog.OnApply(self, event)
-
- self.parent.GetModel().Update()
- self.parent.GetCanvas().Refresh()
- def OnSave(self, event):
- """!Button 'Save' pressed"""
- PreferencesBaseDialog.OnSave(self, event)
-
- self.parent.GetModel().Update()
- self.parent.GetCanvas().Refresh()
- class PropertiesDialog(wx.Dialog):
- """!Model properties dialog
- """
- def __init__(self, parent, id = wx.ID_ANY,
- title = _('Model properties'),
- size = (350, 400),
- style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER):
- wx.Dialog.__init__(self, parent, id, title, size = size,
- style = style)
-
- self.metaBox = wx.StaticBox(parent = self, id = wx.ID_ANY,
- label=" %s " % _("Metadata"))
- self.cmdBox = wx.StaticBox(parent = self, id = wx.ID_ANY,
- label=" %s " % _("Commands"))
-
- self.name = wx.TextCtrl(parent = self, id = wx.ID_ANY,
- size = (300, 25))
- self.desc = wx.TextCtrl(parent = self, id = wx.ID_ANY,
- style = wx.TE_MULTILINE,
- size = (300, 50))
- self.author = wx.TextCtrl(parent = self, id = wx.ID_ANY,
- size = (300, 25))
-
- # commands
- self.overwrite = wx.CheckBox(parent = self, id=wx.ID_ANY,
- label=_("Allow output files to overwrite existing files"))
- self.overwrite.SetValue(UserSettings.Get(group='cmd', key='overwrite', subkey='enabled'))
-
- # buttons
- self.btnOk = wx.Button(self, wx.ID_OK)
- self.btnCancel = wx.Button(self, wx.ID_CANCEL)
- self.btnOk.SetDefault()
-
- self.btnOk.SetToolTipString(_("Apply properties"))
- self.btnOk.SetDefault()
- self.btnCancel.SetToolTipString(_("Close dialog and ignore changes"))
-
- self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
-
- self._layout()
- def _layout(self):
- metaSizer = wx.StaticBoxSizer(self.metaBox, wx.VERTICAL)
- gridSizer = wx.GridBagSizer(hgap = 3, vgap = 3)
- gridSizer.AddGrowableCol(0)
- gridSizer.AddGrowableRow(1)
- gridSizer.Add(item = wx.StaticText(parent = self, id = wx.ID_ANY,
- label = _("Name:")),
- flag = wx.ALIGN_LEFT |
- wx.ALIGN_CENTER_VERTICAL,
- pos = (0, 0))
- gridSizer.Add(item = self.name,
- flag = wx.ALIGN_LEFT |
- wx.ALIGN_CENTER_VERTICAL | wx.EXPAND,
- pos = (0, 1))
- gridSizer.Add(item = wx.StaticText(parent = self, id = wx.ID_ANY,
- label = _("Description:")),
- flag = wx.ALIGN_LEFT |
- wx.ALIGN_CENTER_VERTICAL,
- pos = (1, 0))
- gridSizer.Add(item = self.desc,
- flag = wx.ALIGN_LEFT |
- wx.ALIGN_CENTER_VERTICAL | wx.EXPAND,
- pos = (1, 1))
- gridSizer.Add(item = wx.StaticText(parent = self, id = wx.ID_ANY,
- label = _("Author(s):")),
- flag = wx.ALIGN_LEFT |
- wx.ALIGN_CENTER_VERTICAL,
- pos = (2, 0))
- gridSizer.Add(item = self.author,
- flag = wx.ALIGN_LEFT |
- wx.ALIGN_CENTER_VERTICAL | wx.EXPAND,
- pos = (2, 1))
- metaSizer.Add(item = gridSizer)
-
- cmdSizer = wx.StaticBoxSizer(self.cmdBox, wx.VERTICAL)
- cmdSizer.Add(item = self.overwrite,
- flag = wx.EXPAND | wx.ALL, border = 3)
-
- btnStdSizer = wx.StdDialogButtonSizer()
- btnStdSizer.AddButton(self.btnCancel)
- btnStdSizer.AddButton(self.btnOk)
- btnStdSizer.Realize()
-
- mainSizer = wx.BoxSizer(wx.VERTICAL)
- mainSizer.Add(item=metaSizer, proportion=1,
- flag=wx.EXPAND | wx.ALL, border=5)
- mainSizer.Add(item=cmdSizer, proportion=0,
- flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, border=5)
- mainSizer.Add(item=btnStdSizer, proportion=0,
- flag=wx.EXPAND | wx.ALL | wx.ALIGN_RIGHT, border=5)
-
- self.SetSizer(mainSizer)
- mainSizer.Fit(self)
- def OnCloseWindow(self, event):
- self.Hide()
-
- def GetValues(self):
- """!Get values"""
- return { 'name' : self.name.GetValue(),
- 'description' : self.desc.GetValue(),
- 'author' : self.author.GetValue(),
- 'overwrite' : self.overwrite.IsChecked() }
-
- def Init(self, prop):
- """!Initialize dialog"""
- self.name.SetValue(prop['name'])
- self.desc.SetValue(prop['description'])
- self.author.SetValue(prop['author'])
- if 'overwrite' in prop:
- self.overwrite.SetValue(prop['overwrite'])
- class ModelParamDialog(wx.Dialog):
- def __init__(self, parent, params, id = wx.ID_ANY, title = _("Model parameters"),
- style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER, **kwargs):
- """!Model parameters dialog
- """
- self.parent = parent
- self.params = params
- self.tasks = list() # list of tasks/pages
-
- wx.Dialog.__init__(self, parent = parent, id = id, title = title, style = style, **kwargs)
-
- if globalvar.hasAgw:
- self.notebook = FN.FlatNotebook(self, id = wx.ID_ANY,
- agwStyle = FN.FNB_FANCY_TABS |
- FN.FNB_BOTTOM |
- FN.FNB_NO_NAV_BUTTONS |
- FN.FNB_NO_X_BUTTON)
- else:
- self.notebook = FN.FlatNotebook(self, id = wx.ID_ANY,
- style = FN.FNB_FANCY_TABS |
- FN.FNB_BOTTOM |
- FN.FNB_NO_NAV_BUTTONS |
- FN.FNB_NO_X_BUTTON)
-
- panel = self._createPages()
- wx.CallAfter(self.notebook.SetSelection, 0)
-
- self.btnCancel = wx.Button(parent = self, id = wx.ID_CANCEL)
- self.btnRun = wx.Button(parent = self, id = wx.ID_OK,
- label = _("&Run"))
- self.btnRun.SetDefault()
-
- self._layout()
-
- size = self.GetBestSize()
- self.SetMinSize(size)
- self.SetSize((size.width, size.height +
- panel.constrained_size[1] -
- panel.panelMinHeight))
-
- def _layout(self):
- btnSizer = wx.StdDialogButtonSizer()
- btnSizer.AddButton(self.btnCancel)
- btnSizer.AddButton(self.btnRun)
- btnSizer.Realize()
-
- mainSizer = wx.BoxSizer(wx.VERTICAL)
- mainSizer.Add(item = self.notebook, proportion = 1,
- flag = wx.EXPAND)
- mainSizer.Add(item=btnSizer, proportion=0,
- flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5)
-
- self.SetSizer(mainSizer)
- mainSizer.Fit(self)
-
- def _createPages(self):
- """!Create for each parameterized module its own page"""
- nameOrdered = [''] * len(self.params.keys())
- for name, params in self.params.iteritems():
- nameOrdered[params['idx']] = name
- for name in nameOrdered:
- params = self.params[name]
- panel = self._createPage(name, params)
- self.notebook.AddPage(panel, text = name)
-
- return panel
-
- def _createPage(self, name, params):
- """!Define notebook page"""
- if name in globalvar.grassCmd['all']:
- task = gtask.grassTask(name)
- else:
- task = gtask.grassTask()
- task.flags = params['flags']
- task.params = params['params']
-
- panel = menuform.cmdPanel(parent = self, id = wx.ID_ANY, task = task)
- self.tasks.append(task)
-
- return panel
- def GetErrors(self):
- """!Check for errors, get list of messages"""
- errList = list()
- for task in self.tasks:
- errList += task.getCmdError()
-
- return errList
-
- class ModelListCtrl(wx.ListCtrl,
- listmix.ListCtrlAutoWidthMixin,
- listmix.TextEditMixin,
- listmix.ColumnSorterMixin):
- def __init__(self, parent, columns, id = wx.ID_ANY,
- style = wx.LC_REPORT | wx.BORDER_NONE |
- wx.LC_SORT_ASCENDING |wx.LC_HRULES |
- wx.LC_VRULES, **kwargs):
- """!List of model variables"""
- self.parent = parent
- self.columns = columns
- self.shape = None
- try:
- self.frame = parent.parent
- except AttributeError:
- self.frame = None
-
- wx.ListCtrl.__init__(self, parent, id = id, style = style, **kwargs)
- listmix.ListCtrlAutoWidthMixin.__init__(self)
- listmix.TextEditMixin.__init__(self)
- listmix.ColumnSorterMixin.__init__(self, 4)
-
- i = 0
- for col in columns:
- self.InsertColumn(i, col)
- self.SetColumnWidth(i, wx.LIST_AUTOSIZE_USEHEADER)
- i += 1
-
- self.itemDataMap = {} # requested by sorter
- self.itemCount = 0
-
- self.Bind(wx.EVT_LIST_BEGIN_LABEL_EDIT, self.OnBeginEdit)
- self.Bind(wx.EVT_LIST_END_LABEL_EDIT, self.OnEndEdit)
- self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColClick)
- self.Bind(wx.EVT_COMMAND_RIGHT_CLICK, self.OnRightUp) #wxMSW
- self.Bind(wx.EVT_RIGHT_UP, self.OnRightUp) #wxGTK
-
- def OnBeginEdit(self, event):
- """!Editing of item started"""
- event.Allow()
- def OnEndEdit(self, event):
- """!Finish editing of item"""
- pass
-
- def OnColClick(self, event):
- """!Click on column header (order by)"""
- event.Skip()
- class VariablePanel(wx.Panel):
- def __init__(self, parent, id = wx.ID_ANY,
- **kwargs):
- """!Manage model variables panel
- """
- self.parent = parent
-
- wx.Panel.__init__(self, parent = parent, id = id, **kwargs)
-
- self.listBox = wx.StaticBox(parent = self, id = wx.ID_ANY,
- label=" %s " % _("List of variables - right-click to delete"))
-
- self.list = VariableListCtrl(parent = self,
- columns = [_("Name"), _("Data type"),
- _("Default value"), _("Description")])
-
- # add new category
- self.addBox = wx.StaticBox(parent = self, id = wx.ID_ANY,
- label = " %s " % _("Add new variable"))
- self.name = wx.TextCtrl(parent = self, id = wx.ID_ANY)
- wx.CallAfter(self.name.SetFocus)
- self.type = wx.Choice(parent = self, id = wx.ID_ANY,
- choices = [_("integer"),
- _("float"),
- _("string"),
- _("raster"),
- _("vector")])
- self.value = wx.TextCtrl(parent = self, id = wx.ID_ANY)
- self.desc = wx.TextCtrl(parent = self, id = wx.ID_ANY)
-
- # buttons
- self.btnAdd = wx.Button(parent = self, id = wx.ID_ADD)
- self.btnAdd.SetToolTipString(_("Add new variable to the model"))
- self.btnAdd.Enable(False)
-
- # bindings
- self.name.Bind(wx.EVT_TEXT, self.OnText)
- self.value.Bind(wx.EVT_TEXT, self.OnText)
- self.desc.Bind(wx.EVT_TEXT, self.OnText)
- self.btnAdd.Bind(wx.EVT_BUTTON, self.OnAdd)
-
- self._layout()
- def _layout(self):
- """!Layout dialog"""
- listSizer = wx.StaticBoxSizer(self.listBox, wx.VERTICAL)
- listSizer.Add(item = self.list, proportion = 1,
- flag = wx.EXPAND)
-
- addSizer = wx.StaticBoxSizer(self.addBox, wx.VERTICAL)
- gridSizer = wx.GridBagSizer(hgap = 5, vgap = 5)
- gridSizer.AddGrowableCol(1)
- gridSizer.Add(item = wx.StaticText(parent = self, id = wx.ID_ANY,
- label = "%s:" % _("Name")),
- flag = wx.ALIGN_CENTER_VERTICAL,
- pos = (0, 0))
- gridSizer.Add(item = self.name,
- pos = (0, 1),
- flag = wx.EXPAND)
- gridSizer.Add(item = wx.StaticText(parent = self, id = wx.ID_ANY,
- label = "%s:" % _("Data type")),
- flag = wx.ALIGN_CENTER_VERTICAL,
- pos = (0, 2))
- gridSizer.Add(item = self.type,
- pos = (0, 3))
- gridSizer.Add(item = wx.StaticText(parent = self, id = wx.ID_ANY,
- label = "%s:" % _("Default value")),
- flag = wx.ALIGN_CENTER_VERTICAL,
- pos = (1, 0))
- gridSizer.Add(item = self.value,
- pos = (1, 1), span = (1, 3),
- flag = wx.EXPAND)
- gridSizer.Add(item = wx.StaticText(parent = self, id = wx.ID_ANY,
- label = "%s:" % _("Description")),
- flag = wx.ALIGN_CENTER_VERTICAL,
- pos = (2, 0))
- gridSizer.Add(item = self.desc,
- pos = (2, 1), span = (1, 3),
- flag = wx.EXPAND)
- addSizer.Add(item = gridSizer,
- flag = wx.EXPAND)
- addSizer.Add(item = self.btnAdd, proportion = 0,
- flag = wx.TOP | wx.ALIGN_RIGHT, border = 5)
-
- mainSizer = wx.BoxSizer(wx.VERTICAL)
- mainSizer.Add(item = listSizer, proportion = 1,
- flag = wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border = 5)
- mainSizer.Add(item = addSizer, proportion = 0,
- flag = wx.EXPAND | wx.ALIGN_CENTER |
- wx.LEFT | wx.RIGHT | wx.BOTTOM, border = 5)
-
- self.SetSizer(mainSizer)
- mainSizer.Fit(self)
-
- def OnText(self, event):
- """!Text entered"""
- if self.name.GetValue():
- self.btnAdd.Enable()
- else:
- self.btnAdd.Enable(False)
-
- def OnAdd(self, event):
- """!Add new variable to the list"""
- msg = self.list.Append(self.name.GetValue(),
- self.type.GetStringSelection(),
- self.value.GetValue(),
- self.desc.GetValue())
- self.name.SetValue('')
- self.name.SetFocus()
-
- if msg:
- GError(parent = self,
- message = msg)
- else:
- self.type.SetSelection(0)
- self.value.SetValue('')
- self.desc.SetValue('')
- self.UpdateModelVariables()
-
- def UpdateModelVariables(self):
- """!Update model variables"""
- variables = dict()
- for values in self.list.GetData().itervalues():
- name = values[0]
- variables[name] = { 'type' : str(values[1]) }
- if values[2]:
- variables[name]['value'] = values[2]
- if values[3]:
- variables[name]['description'] = values[3]
-
- self.parent.GetModel().SetVariables(variables)
- self.parent.ModelChanged()
- def Update(self):
- """!Reload list of variables"""
- self.list.OnReload(None)
-
- class VariableListCtrl(ModelListCtrl):
- def __init__(self, parent, columns, **kwargs):
- """!List of model variables"""
- ModelListCtrl.__init__(self, parent, columns, **kwargs)
-
- def GetListCtrl(self):
- """!Used by ColumnSorterMixin"""
- return self
-
- def GetData(self):
- """!Get list data"""
- return self.itemDataMap
-
- def Populate(self, data):
- """!Populate the list"""
- self.itemDataMap = dict()
- i = 0
- for name, values in data.iteritems():
- self.itemDataMap[i] = [name, values['type'],
- values.get('value', ''),
- values.get('description', '')]
- i += 1
-
- self.itemCount = len(self.itemDataMap.keys())
- self.DeleteAllItems()
- i = 0
- for name, vtype, value, desc in self.itemDataMap.itervalues():
- index = self.InsertStringItem(sys.maxint, name)
- self.SetStringItem(index, 0, name)
- self.SetStringItem(index, 1, vtype)
- self.SetStringItem(index, 2, value)
- self.SetStringItem(index, 3, desc)
- self.SetItemData(index, i)
- i += 1
-
- def Append(self, name, vtype, value, desc):
- """!Append new item to the list
- @return None on success
- @return error string
- """
- for iname, ivtype, ivalue, idesc in self.itemDataMap.itervalues():
- if iname == name:
- return _("Variable <%s> already exists in the model. "
- "Adding variable failed.") % name
-
- index = self.InsertStringItem(sys.maxint, name)
- self.SetStringItem(index, 0, name)
- self.SetStringItem(index, 1, vtype)
- self.SetStringItem(index, 2, value)
- self.SetStringItem(index, 3, desc)
- self.SetItemData(index, self.itemCount)
-
- self.itemDataMap[self.itemCount] = [name, vtype, value, desc]
- self.itemCount += 1
-
- return None
- def OnRemove(self, event):
- """!Remove selected variable(s) from the model"""
- item = self.GetFirstSelected()
- while item != -1:
- self.DeleteItem(item)
- del self.itemDataMap[item]
- item = self.GetFirstSelected()
- self.parent.UpdateModelVariables()
-
- event.Skip()
-
- def OnRemoveAll(self, event):
- """!Remove all variable(s) from the model"""
- dlg = wx.MessageBox(parent=self,
- message=_("Do you want to delete all variables from "
- "the model?"),
- caption=_("Delete variables"),
- style=wx.YES_NO | wx.CENTRE)
- if dlg != wx.YES:
- return
-
- self.DeleteAllItems()
- self.itemDataMap = dict()
-
- self.parent.UpdateModelVariables()
-
- def OnEndEdit(self, event):
- """!Finish editing of item"""
- itemIndex = event.GetIndex()
- columnIndex = event.GetColumn()
- nameOld = self.GetItem(itemIndex, 0).GetText()
- if columnIndex == 0: # TODO
- event.Veto()
-
- self.itemDataMap[itemIndex][columnIndex] = event.GetText()
-
- self.parent.UpdateModelVariables()
- def OnReload(self, event):
- """!Reload list of variables"""
- self.Populate(self.parent.parent.GetModel().GetVariables())
- def OnRightUp(self, event):
- """!Mouse right button up"""
- if not hasattr(self, "popupID1"):
- self.popupID1 = wx.NewId()
- self.popupID2 = wx.NewId()
- self.popupID3 = wx.NewId()
- self.Bind(wx.EVT_MENU, self.OnRemove, id = self.popupID1)
- self.Bind(wx.EVT_MENU, self.OnRemoveAll, id = self.popupID2)
- self.Bind(wx.EVT_MENU, self.OnReload, id = self.popupID3)
-
- # generate popup-menu
- menu = wx.Menu()
- menu.Append(self.popupID1, _("Delete selected"))
- menu.Append(self.popupID2, _("Delete all"))
- if self.GetFirstSelected() == -1:
- menu.Enable(self.popupID1, False)
- menu.Enable(self.popupID2, False)
-
- menu.AppendSeparator()
- menu.Append(self.popupID3, _("Reload"))
-
- self.PopupMenu(menu)
- menu.Destroy()
-
- class ModelItem(ModelObject):
- def __init__(self, parent, x, y, id = -1, width = None, height = None, text = '', items = []):
- """!Abstract class for loops and conditions"""
- ModelObject.__init__(self, id)
- self.parent = parent
- self.text = text
- self.items = items # list of items in the loop
-
- def GetText(self):
- """!Get loop text"""
- return self.text
- def GetItems(self):
- """!Get items (id)"""
- return self.items
- def SetId(self, id):
- """!Set loop id"""
- self.id = id
- def SetText(self, cond):
- """!Set loop text (condition)"""
- self.text = cond
- self.ClearText()
- self.AddText('(' + str(self.id) + ') ' + self.text)
- def GetLog(self):
- """!Get log info"""
- if self.text:
- return _("Condition: ") + self.text
- else:
- return _("Condition: not defined")
- def AddRelation(self, rel):
- """!Record relation"""
- self.rels.append(rel)
-
- def Clear(self):
- """!Clear object, remove rels"""
- self.rels = list()
-
- class ModelItemDialog(wx.Dialog):
- """!Abstract item properties dialog"""
- def __init__(self, parent, shape, title, id = wx.ID_ANY,
- style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER, **kwargs):
- self.parent = parent
- self.shape = shape
-
- wx.Dialog.__init__(self, parent, id, title = title, style = style, **kwargs)
-
- self.panel = wx.Panel(parent = self, id = wx.ID_ANY)
-
- self.condBox = wx.StaticBox(parent = self.panel, id = wx.ID_ANY,
- label=" %s " % _("Condition"))
- self.condText = wx.TextCtrl(parent = self.panel, id = wx.ID_ANY,
- value = shape.GetText())
-
- self.itemList = ItemCheckListCtrl(parent = self.panel,
- window = self,
- columns = [_("ID"), _("Name"),
- _("Command")],
- shape = shape)
- self.itemList.Populate(self.parent.GetModel().GetItems())
-
- self.btnCancel = wx.Button(parent = self.panel, id = wx.ID_CANCEL)
- self.btnOk = wx.Button(parent = self.panel, id = wx.ID_OK)
- self.btnOk.SetDefault()
-
- def _layout(self):
- """!Do layout (virtual method)"""
- pass
-
- def GetCondition(self):
- """!Get loop condition"""
- return self.condText.GetValue()
-
- class ModelLoop(ModelItem, ogl.RectangleShape):
- def __init__(self, parent, x, y, id = -1, width = None, height = None, text = '', items = []):
- """!Defines a loop"""
- ModelItem.__init__(self, parent, x, y, id, width, height, text, items)
-
- if not width:
- width = UserSettings.Get(group='modeler', key='loop', subkey=('size', 'width'))
- if not height:
- height = UserSettings.Get(group='modeler', key='loop', subkey=('size', 'height'))
-
- if self.parent.GetCanvas():
- ogl.RectangleShape.__init__(self, width, height)
-
- self.SetCanvas(self.parent)
- self.SetX(x)
- self.SetY(y)
- self.SetPen(wx.BLACK_PEN)
- self.SetCornerRadius(100)
- if text:
- self.AddText('(' + str(self.id) + ') ' + text)
- else:
- self.AddText('(' + str(self.id) + ')')
-
- def GetName(self):
- """!Get name"""
- return _("loop")
-
- def SetItems(self, items):
- """!Set items (id)"""
- self.items = items
- class ModelLoopDialog(ModelItemDialog):
- """!Loop properties dialog"""
- def __init__(self, parent, shape, id = wx.ID_ANY, title = _("Loop properties"),
- style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER, **kwargs):
- ModelItemDialog.__init__(self, parent, shape, title,
- style = style, **kwargs)
-
- self.listBox = wx.StaticBox(parent = self.panel, id = wx.ID_ANY,
- label=" %s " % _("List of items in loop"))
-
- self._layout()
- self.SetMinSize(self.GetSize())
- self.SetSize((500, 400))
-
- def _layout(self):
- """!Do layout"""
- sizer = wx.BoxSizer(wx.VERTICAL)
-
- condSizer = wx.StaticBoxSizer(self.condBox, wx.VERTICAL)
- condSizer.Add(item = self.condText, proportion = 1,
- flag = wx.EXPAND)
-
- listSizer = wx.StaticBoxSizer(self.listBox, wx.VERTICAL)
- listSizer.Add(item = self.itemList, proportion = 1,
- flag = wx.EXPAND)
-
- btnSizer = wx.StdDialogButtonSizer()
- btnSizer.AddButton(self.btnCancel)
- btnSizer.AddButton(self.btnOk)
- btnSizer.Realize()
- sizer.Add(item = condSizer, proportion = 0,
- flag = wx.EXPAND | wx.ALL, border = 3)
- sizer.Add(item = listSizer, proportion = 1,
- flag = wx.EXPAND | wx.LEFT | wx.RIGHT, border = 3)
- sizer.Add(item = btnSizer, proportion=0,
- flag = wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5)
-
- self.panel.SetSizer(sizer)
- sizer.Fit(self.panel)
-
- self.Layout()
-
- def GetItems(self):
- """!Get list of selected actions"""
- return self.itemList.GetItems()
- class ItemPanel(wx.Panel):
- def __init__(self, parent, id = wx.ID_ANY,
- **kwargs):
- """!Manage model items
- """
- self.parent = parent
-
- wx.Panel.__init__(self, parent = parent, id = id, **kwargs)
-
- self.listBox = wx.StaticBox(parent = self, id = wx.ID_ANY,
- label=" %s " % _("List of items - right-click to delete"))
-
- self.list = ItemListCtrl(parent = self,
- columns = [_("ID"), _("Name"), _("In block"),
- _("Command / Condition")])
-
- self._layout()
- def _layout(self):
- """!Layout dialog"""
- listSizer = wx.StaticBoxSizer(self.listBox, wx.VERTICAL)
- listSizer.Add(item = self.list, proportion = 1,
- flag = wx.EXPAND)
-
- mainSizer = wx.BoxSizer(wx.VERTICAL)
- mainSizer.Add(item = listSizer, proportion = 1,
- flag = wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border = 5)
-
- self.SetSizer(mainSizer)
- mainSizer.Fit(self)
-
- def Update(self):
- """!Reload list of variables"""
- self.list.OnReload(None)
-
- class ItemListCtrl(ModelListCtrl):
- def __init__(self, parent, columns, disablePopup = False, **kwargs):
- """!List of model actions"""
- self.disablePopup = disablePopup
-
- ModelListCtrl.__init__(self, parent, columns, **kwargs)
- self.SetColumnWidth(1, 100)
- self.SetColumnWidth(2, 65)
-
- def GetListCtrl(self):
- """!Used by ColumnSorterMixin"""
- return self
-
- def GetData(self):
- """!Get list data"""
- return self.itemDataMap
-
- def Populate(self, data):
- """!Populate the list"""
- self.itemDataMap = dict()
-
- if self.shape:
- if isinstance(self.shape, ModelCondition):
- if self.GetName() == 'ElseBlockList':
- shapeItems = map(lambda x: x.GetId(), self.shape.GetItems()['else'])
- else:
- shapeItems = map(lambda x: x.GetId(), self.shape.GetItems()['if'])
- else:
- shapeItems = map(lambda x: x.GetId(), self.shape.GetItems())
- else:
- shapeItems = list()
- i = 0
- if len(self.columns) == 3: # ItemCheckList
- checked = list()
- for action in data:
- if isinstance(action, ModelData):
- continue
-
- if len(self.columns) == 3:
- self.itemDataMap[i] = [str(action.GetId()),
- action.GetName(),
- action.GetLog()]
- aId = action.GetBlockId()
- if action.GetId() in shapeItems:
- checked.append(aId)
- else:
- checked.append(None)
- else:
- bId = action.GetBlockId()
- if not bId:
- bId = ''
- self.itemDataMap[i] = [str(action.GetId()),
- action.GetName(),
- ','.join(map(str, bId)),
- action.GetLog()]
-
- i += 1
-
- self.itemCount = len(self.itemDataMap.keys())
- self.DeleteAllItems()
- i = 0
- if len(self.columns) == 3:
- for aid, name, desc in self.itemDataMap.itervalues():
- index = self.InsertStringItem(sys.maxint, aid)
- self.SetStringItem(index, 0, aid)
- self.SetStringItem(index, 1, name)
- self.SetStringItem(index, 2, desc)
- self.SetItemData(index, i)
- if checked[i]:
- self.CheckItem(index, True)
- i += 1
- else:
- for aid, name, inloop, desc in self.itemDataMap.itervalues():
- index = self.InsertStringItem(sys.maxint, aid)
- self.SetStringItem(index, 0, aid)
- self.SetStringItem(index, 1, name)
- self.SetStringItem(index, 2, inloop)
- self.SetStringItem(index, 3, desc)
- self.SetItemData(index, i)
- i += 1
-
- def OnRemove(self, event):
- """!Remove selected action(s) from the model"""
- model = self.frame.GetModel()
- canvas = self.frame.GetCanvas()
-
- item = self.GetFirstSelected()
- while item != -1:
- self.DeleteItem(item)
- del self.itemDataMap[item]
-
- aId = self.GetItem(item, 0).GetText()
- action = model.GetItem(int(aId))
- if not action:
- item = self.GetFirstSelected()
- continue
-
- model.RemoveItem(action)
- canvas.GetDiagram().RemoveShape(action)
- self.frame.ModelChanged()
-
- item = self.GetFirstSelected()
-
- canvas.Refresh()
-
- event.Skip()
-
- def OnRemoveAll(self, event):
- """!Remove all variable(s) from the model"""
- deleteDialog = wx.MessageBox(parent=self,
- message=_("Selected data records (%d) will permanently deleted "
- "from table. Do you want to delete them?") % \
- (len(self.listOfSQLStatements)),
- caption=_("Delete records"),
- style=wx.YES_NO | wx.CENTRE)
- if deleteDialog != wx.YES:
- return False
-
- self.DeleteAllItems()
- self.itemDataMap = dict()
- self.parent.UpdateModelVariables()
- def OnEndEdit(self, event):
- """!Finish editing of item"""
- itemIndex = event.GetIndex()
- columnIndex = event.GetColumn()
-
- self.itemDataMap[itemIndex][columnIndex] = event.GetText()
-
- aId = int(self.GetItem(itemIndex, 0).GetText())
- action = self.frame.GetModel().GetItem(aId)
- if not action:
- event.Veto()
- if columnIndex == 0:
- action.SetId(int(event.GetText()))
-
- self.frame.ModelChanged()
- def OnReload(self, event = None):
- """!Reload list of actions"""
- self.Populate(self.frame.GetModel().GetItems())
- def OnRightUp(self, event):
- """!Mouse right button up"""
- if self.disablePopup:
- return
-
- if not hasattr(self, "popupID1"):
- self.popupID1 = wx.NewId()
- self.popupID2 = wx.NewId()
- self.popupID3 = wx.NewId()
- self.popupID4 = wx.NewId()
- self.Bind(wx.EVT_MENU, self.OnRemove, id = self.popupID1)
- self.Bind(wx.EVT_MENU, self.OnRemoveAll, id = self.popupID2)
- self.Bind(wx.EVT_MENU, self.OnReload, id = self.popupID3)
- self.Bind(wx.EVT_MENU, self.OnNormalize, id = self.popupID4)
- # generate popup-menu
- menu = wx.Menu()
- menu.Append(self.popupID1, _("Delete selected"))
- menu.Append(self.popupID2, _("Delete all"))
- if self.GetFirstSelected() == -1:
- menu.Enable(self.popupID1, False)
- menu.Enable(self.popupID2, False)
-
- menu.AppendSeparator()
- menu.Append(self.popupID4, _("Normalize"))
- menu.Append(self.popupID3, _("Reload"))
-
- self.PopupMenu(menu)
- menu.Destroy()
-
- def OnNormalize(self, event):
- """!Update id of actions"""
- model = self.frame.GetModel()
-
- aId = 1
- for item in model.GetItems():
- item.SetId(aId)
- aId += 1
-
- self.OnReload(None)
- self.frame.GetCanvas().Refresh()
- self.frame.ModelChanged()
- class ItemCheckListCtrl(ItemListCtrl, listmix.CheckListCtrlMixin):
- def __init__(self, parent, shape, columns, window = None, **kwargs):
- self.parent = parent
- self.window = window
-
- ItemListCtrl.__init__(self, parent, columns, disablePopup = True, **kwargs)
- listmix.CheckListCtrlMixin.__init__(self)
- self.SetColumnWidth(0, 50)
-
- self.shape = shape
-
- def OnBeginEdit(self, event):
- """!Disable editing"""
- event.Veto()
-
- def OnCheckItem(self, index, flag):
- """!Item checked/unchecked"""
- name = self.GetName()
- if name == 'IfBlockList' and self.window:
- self.window.OnCheckItemIf(index, flag)
- elif name == 'ElseBlockList' and self.window:
- self.window.OnCheckItemElse(index, flag)
-
- def GetItems(self):
- """!Get list of selected actions"""
- ids = { 'checked' : list(),
- 'unchecked' : list() }
- for i in range(self.GetItemCount()):
- iId = int(self.GetItem(i, 0).GetText())
- if self.IsChecked(i):
- ids['checked'].append(iId)
- else:
- ids['unchecked'].append(iId)
-
- return ids
- def CheckItemById(self, aId, flag):
- """!Check/uncheck given item by id"""
- for i in range(self.GetItemCount()):
- iId = int(self.GetItem(i, 0).GetText())
- if iId == aId:
- self.CheckItem(i, flag)
- break
-
- class ModelCondition(ModelItem, ogl.PolygonShape):
- def __init__(self, parent, x, y, id = -1, width = None, height = None, text = '',
- items = { 'if' : [], 'else' : [] }):
- """!Defines a if-else condition"""
- ModelItem.__init__(self, parent, x, y, id, width, height, text, items)
-
- if not width:
- self.width = UserSettings.Get(group='modeler', key='if-else', subkey=('size', 'width'))
- else:
- self.width = width
- if not height:
- self.height = UserSettings.Get(group='modeler', key='if-else', subkey=('size', 'height'))
- else:
- self.height = height
-
- if self.parent.GetCanvas():
- ogl.PolygonShape.__init__(self)
-
- points = [(0, - self.height / 2),
- (self.width / 2, 0),
- (0, self.height / 2),
- (- self.width / 2, 0)]
- self.Create(points)
-
- self.SetCanvas(self.parent)
- self.SetX(x)
- self.SetY(y)
- self.SetPen(wx.BLACK_PEN)
- if text:
- self.AddText('(' + str(self.id) + ') ' + text)
- else:
- self.AddText('(' + str(self.id) + ')')
- def GetName(self):
- """!Get name"""
- return _("if-else")
- def GetWidth(self):
- """!Get object width"""
- return self.width
- def GetHeight(self):
- """!Get object height"""
- return self.height
- def SetItems(self, items, branch = 'if'):
- """!Set items (id)
- @param items list of items
- @param branch 'if' / 'else'
- """
- if branch in ['if', 'else']:
- self.items[branch] = items
-
- class ModelConditionDialog(ModelItemDialog):
- """!Condition properties dialog"""
- def __init__(self, parent, shape, id = wx.ID_ANY, title = _("If-else properties"),
- style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER, **kwargs):
- ModelItemDialog.__init__(self, parent, shape, title,
- style = style, **kwargs)
-
- self.listBoxIf = wx.StaticBox(parent = self.panel, id = wx.ID_ANY,
- label=" %s " % _("List of items in 'if' block"))
- self.itemListIf = self.itemList
- self.itemListIf.SetName('IfBlockList')
-
- self.listBoxElse = wx.StaticBox(parent = self.panel, id = wx.ID_ANY,
- label=" %s " % _("List of items in 'else' block"))
- self.itemListElse = ItemCheckListCtrl(parent = self.panel,
- window = self,
- columns = [_("ID"), _("Name"),
- _("Command")],
- shape = shape)
- self.itemListElse.SetName('ElseBlockList')
- self.itemListElse.Populate(self.parent.GetModel().GetItems())
-
- self._layout()
- self.SetMinSize(self.GetSize())
- self.SetSize((500, 400))
-
- def _layout(self):
- """!Do layout"""
- sizer = wx.BoxSizer(wx.VERTICAL)
-
- condSizer = wx.StaticBoxSizer(self.condBox, wx.VERTICAL)
- condSizer.Add(item = self.condText, proportion = 1,
- flag = wx.EXPAND)
-
- listIfSizer = wx.StaticBoxSizer(self.listBoxIf, wx.VERTICAL)
- listIfSizer.Add(item = self.itemListIf, proportion = 1,
- flag = wx.EXPAND)
- listElseSizer = wx.StaticBoxSizer(self.listBoxElse, wx.VERTICAL)
- listElseSizer.Add(item = self.itemListElse, proportion = 1,
- flag = wx.EXPAND)
-
- btnSizer = wx.StdDialogButtonSizer()
- btnSizer.AddButton(self.btnCancel)
- btnSizer.AddButton(self.btnOk)
- btnSizer.Realize()
- sizer.Add(item = condSizer, proportion = 0,
- flag = wx.EXPAND | wx.ALL, border = 3)
- sizer.Add(item = listIfSizer, proportion = 1,
- flag = wx.EXPAND | wx.LEFT | wx.RIGHT, border = 3)
- sizer.Add(item = listElseSizer, proportion = 1,
- flag = wx.EXPAND | wx.LEFT | wx.RIGHT, border = 3)
- sizer.Add(item = btnSizer, proportion=0,
- flag = wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5)
-
- self.panel.SetSizer(sizer)
- sizer.Fit(self.panel)
-
- self.Layout()
- def OnCheckItemIf(self, index, flag):
- """!Item in if-block checked/unchecked"""
- if flag is False:
- return
-
- aId = int(self.itemListIf.GetItem(index, 0).GetText())
- if aId in self.itemListElse.GetItems()['checked']:
- self.itemListElse.CheckItemById(aId, False)
-
- def OnCheckItemElse(self, index, flag):
- """!Item in else-block checked/unchecked"""
- if flag is False:
- return
-
- aId = int(self.itemListElse.GetItem(index, 0).GetText())
- if aId in self.itemListIf.GetItems()['checked']:
- self.itemListIf.CheckItemById(aId, False)
-
- def GetItems(self):
- """!Get items"""
- return { 'if' : self.itemListIf.GetItems(),
- 'else' : self.itemListElse.GetItems() }
- class WritePythonFile:
- def __init__(self, fd, model):
- """!Class for exporting model to Python script
- @param fd file desciptor
- """
- self.fd = fd
- self.model = model
- self.indent = 4
- self._writePython()
-
- def _writePython(self):
- """!Write model to file"""
- properties = self.model.GetProperties()
-
- self.fd.write(
- r"""#!/usr/bin/env python
- #
- ############################################################################
- #
- # MODULE: %s
- #
- # AUTHOR(S): %s
- #
- # PURPOSE: %s
- #
- # DATE: %s
- #
- #############################################################################
- """ % (properties['name'],
- properties['author'],
- properties['description'],
- time.asctime()))
-
- self.fd.write(
- r"""
- import sys
- import os
- import atexit
- import grass.script as grass
- """)
-
- # cleanup()
- rast, vect, rast3d, msg = self.model.GetIntermediateData()
- self.fd.write(
- r"""
- def cleanup():
- """)
- if rast:
- self.fd.write(
- r""" grass.run_command('g.remove',
- rast=%s)
- """ % ','.join(map(lambda x: "'" + x + "'", rast)))
- if vect:
- self.fd.write(
- r""" grass.run_command('g.remove',
- vect = %s)
- """ % ','.join(map(lambda x: "'" + x + "'", vect)))
- if rast3d:
- self.fd.write(
- r""" grass.run_command('g.remove',
- rast3d = %s)
- """ % ','.join(map(lambda x: "'" + x + "'", rast3d)))
- if not rast and not vect and not rast3d:
- self.fd.write(' pass\n')
-
- self.fd.write("\ndef main():\n")
- for item in self.model.GetItems():
- self._writePythonItem(item)
-
- self.fd.write("\n return 0\n")
-
- self.fd.write(
- r"""
- if __name__ == "__main__":
- options, flags = grass.parser()
- atexit.register(cleanup)
- sys.exit(main())
- """)
-
- def _writePythonItem(self, item, ignoreBlock = True):
- """!Write model object to Python file"""
- if isinstance(item, ModelAction):
- if ignoreBlock and item.GetBlockId(): # ignore items in loops of conditions
- return
- self._writePythonAction(item)
- elif isinstance(item, ModelLoop) or isinstance(item, ModelCondition):
- # substitute condition
- variables = self.model.GetVariables()
- cond = item.GetText()
- for variable in variables:
- pattern= re.compile('%' + variable)
- if pattern.search(cond):
- value = variables[variable].get('value', '')
- if variables[variable].get('type', 'string') == 'string':
- value = '"' + value + '"'
- cond = pattern.sub(value, cond)
- if isinstance(item, ModelLoop):
- self.fd.write('%sfor %s:\n' % (' ' * self.indent, cond))
- self.indent += 4
- for action in item.GetItems():
- self._writePythonItem(action, ignoreBlock = False)
- self.indent -= 4
- else: # ModelCondition
- self.fd.write('%sif %s:\n' % (' ' * self.indent, cond))
- self.indent += 4
- condItems = item.GetItems()
- for action in condItems['if']:
- self._writePythonItem(action, ignoreBlock = False)
- if condItems['else']:
- self.indent -= 4
- self.fd.write('%selse:\n' % (' ' * self.indent))
- self.indent += 4
- for action in condItems['else']:
- self._writePythonItem(action, ignoreBlock = False)
- self.indent += 4
-
- def _writePythonAction(self, item):
- """!Write model action to Python file"""
- task = menuform.GUI(show = None).ParseCommand(cmd = item.GetLog(string = False))
- opts = task.get_options()
- flags = ''
- params = list()
- strcmd = "%sgrass.run_command(" % (' ' * self.indent)
- cmdIndent = len(strcmd)
- for f in opts['flags']:
- if f.get('value', False) == True:
- name = f.get('name', '')
- if len(name) > 1:
- params.append('%s = True' % name)
- else:
- flags += name
-
- for p in opts['params']:
- name = p.get('name', None)
- value = p.get('value', None)
- if name and value:
- ptype = p.get('type', 'string')
- if ptype == 'string':
- params.append('%s = "%s"' % (name, value))
- else:
- params.append("%s = %s" % (name, value))
-
- self.fd.write(strcmd + '"%s"' % task.get_name())
- if flags:
- self.fd.write(",\n%sflags = '%s'" % (' ' * cmdIndent, flags))
- if len(params) > 0:
- self.fd.write(",\n")
- for opt in params[:-1]:
- self.fd.write("%s%s,\n" % (' ' * cmdIndent, opt))
- self.fd.write("%s%s)\n" % (' ' * cmdIndent, params[-1]))
- else:
- self.fd.write(")\n")
-
- def main():
- import gettext
- gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode = True)
-
- app = wx.PySimpleApp()
- wx.InitAllImageHandlers()
- frame = ModelFrame(parent = None)
- if len(sys.argv) > 1:
- frame.LoadModelFile(sys.argv[1])
- frame.Show()
-
- app.MainLoop()
-
- if __name__ == "__main__":
- main()
|