workspace.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. """
  2. @package nviz.workspace
  3. @brief wxNviz workspace settings
  4. Classes:
  5. - workspace::NvizSettings
  6. (C) 2007-2011 by the GRASS Development Team
  7. This program is free software under the GNU General Public License
  8. (>=v2). Read the file COPYING that comes with GRASS for details.
  9. @author Anna Kratochvilova <kratochanna gmail.com> (wxNviz / Google SoC 2011)
  10. """
  11. import copy
  12. import six
  13. from core.settings import UserSettings
  14. try:
  15. from nviz import wxnviz
  16. except ImportError:
  17. wxnviz = None
  18. class NvizSettings(object):
  19. def __init__(self):
  20. pass
  21. def SetConstantDefaultProp(self):
  22. """Set default constant data properties"""
  23. data = dict()
  24. for key, value in six.iteritems(UserSettings.Get(group="nviz", key="constant")):
  25. data[key] = value
  26. color = (
  27. str(data["color"][0])
  28. + ":"
  29. + str(data["color"][1])
  30. + ":"
  31. + str(data["color"][2])
  32. )
  33. data["color"] = color
  34. return data
  35. def SetSurfaceDefaultProp(self, data=None):
  36. """Set default surface data properties"""
  37. if not data:
  38. data = dict()
  39. for sec in ("attribute", "draw", "mask", "position"):
  40. data[sec] = {}
  41. #
  42. # attributes
  43. #
  44. for attrb in ("shine",):
  45. data["attribute"][attrb] = {}
  46. for key, value in six.iteritems(
  47. UserSettings.Get(group="nviz", key="surface", subkey=attrb)
  48. ):
  49. data["attribute"][attrb][key] = value
  50. data["attribute"][attrb]["update"] = None
  51. #
  52. # draw
  53. #
  54. data["draw"]["all"] = False # apply only for current surface
  55. for control, value in six.iteritems(
  56. UserSettings.Get(group="nviz", key="surface", subkey="draw")
  57. ):
  58. if control[:3] == "res":
  59. if "resolution" not in data["draw"]:
  60. data["draw"]["resolution"] = {}
  61. if "update" not in data["draw"]["resolution"]:
  62. data["draw"]["resolution"]["update"] = None
  63. data["draw"]["resolution"][control[4:]] = value
  64. continue
  65. if control == "wire-color":
  66. value = str(value[0]) + ":" + str(value[1]) + ":" + str(value[2])
  67. elif control in ("mode", "style", "shading"):
  68. if "mode" not in data["draw"]:
  69. data["draw"]["mode"] = {}
  70. continue
  71. data["draw"][control] = {"value": value}
  72. data["draw"][control]["update"] = None
  73. value, desc = self.GetDrawMode(
  74. UserSettings.Get(group="nviz", key="surface", subkey=["draw", "mode"]),
  75. UserSettings.Get(group="nviz", key="surface", subkey=["draw", "style"]),
  76. UserSettings.Get(group="nviz", key="surface", subkey=["draw", "shading"]),
  77. )
  78. data["draw"]["mode"] = {"value": value, "desc": desc, "update": None}
  79. # position
  80. for coord in ("x", "y", "z"):
  81. data["position"][coord] = UserSettings.Get(
  82. group="nviz", key="surface", subkey=["position", coord]
  83. )
  84. data["position"]["update"] = None
  85. return data
  86. def SetVolumeDefaultProp(self):
  87. """Set default volume data properties"""
  88. data = dict()
  89. for sec in ("attribute", "draw", "position"):
  90. data[sec] = dict()
  91. for sec in ("isosurface", "slice"):
  92. data[sec] = list()
  93. #
  94. # draw
  95. #
  96. for control, value in six.iteritems(
  97. UserSettings.Get(group="nviz", key="volume", subkey="draw")
  98. ):
  99. if control == "shading":
  100. sel = UserSettings.Get(
  101. group="nviz", key="volume", subkey=["draw", "shading"]
  102. )
  103. value, desc = self.GetDrawMode(shade=sel, string=False)
  104. data["draw"]["shading"] = {}
  105. data["draw"]["shading"]["isosurface"] = {
  106. "value": value,
  107. "desc": desc["shading"],
  108. }
  109. data["draw"]["shading"]["slice"] = {
  110. "value": value,
  111. "desc": desc["shading"],
  112. }
  113. elif control == "mode":
  114. sel = UserSettings.Get(
  115. group="nviz", key="volume", subkey=["draw", "mode"]
  116. )
  117. if sel == 0:
  118. desc = "isosurface"
  119. else:
  120. desc = "slice"
  121. data["draw"]["mode"] = {
  122. "value": sel,
  123. "desc": desc,
  124. }
  125. elif control == "box":
  126. box = UserSettings.Get(
  127. group="nviz", key="volume", subkey=["draw", "box"]
  128. )
  129. data["draw"]["box"] = {"enabled": box}
  130. else:
  131. data["draw"][control] = {}
  132. data["draw"][control]["isosurface"] = {"value": value}
  133. data["draw"][control]["slice"] = {"value": value}
  134. if "update" not in data["draw"][control]:
  135. data["draw"][control]["update"] = None
  136. #
  137. # isosurface attributes
  138. #
  139. for attrb in ("shine",):
  140. data["attribute"][attrb] = {}
  141. for key, value in six.iteritems(
  142. UserSettings.Get(group="nviz", key="volume", subkey=attrb)
  143. ):
  144. data["attribute"][attrb][key] = value
  145. return data
  146. def SetIsosurfaceDefaultProp(self):
  147. """Set default isosurface properties"""
  148. data = dict()
  149. for attr in ("shine", "topo", "transp", "color", "inout"):
  150. data[attr] = {}
  151. data[attr]["update"] = None
  152. if attr == "inout":
  153. data[attr]["value"] = 0
  154. continue
  155. for key, value in six.iteritems(
  156. UserSettings.Get(group="nviz", key="volume", subkey=attr)
  157. ):
  158. data[attr][key] = value
  159. return data
  160. def SetSliceDefaultProp(self):
  161. """Set default slice properties"""
  162. data = dict()
  163. data["position"] = copy.deepcopy(
  164. UserSettings.Get(group="nviz", key="volume", subkey="slice_position")
  165. )
  166. data["position"]["update"] = None
  167. data["transp"] = copy.deepcopy(
  168. UserSettings.Get(group="nviz", key="volume", subkey="transp")
  169. )
  170. return data
  171. def SetVectorDefaultProp(self, longDim, data=None):
  172. """Set default vector data properties"""
  173. if not data:
  174. data = dict()
  175. for sec in ("lines", "points"):
  176. data[sec] = {}
  177. self.SetVectorLinesDefaultProp(data["lines"])
  178. self.SetVectorPointsDefaultProp(data["points"], longDim)
  179. return data
  180. def SetVectorLinesDefaultProp(self, data):
  181. """Set default vector properties -- lines"""
  182. # width
  183. data["width"] = {
  184. "value": UserSettings.Get(
  185. group="nviz", key="vector", subkey=["lines", "width"]
  186. )
  187. }
  188. # color
  189. value = UserSettings.Get(group="nviz", key="vector", subkey=["lines", "color"])
  190. color = str(value[0]) + ":" + str(value[1]) + ":" + str(value[2])
  191. data["color"] = {"value": color}
  192. # mode
  193. if UserSettings.Get(group="nviz", key="vector", subkey=["lines", "flat"]):
  194. type = "flat"
  195. else:
  196. type = "surface"
  197. data["mode"] = {}
  198. data["mode"]["type"] = type
  199. data["mode"]["update"] = None
  200. # height
  201. data["height"] = {
  202. "value": UserSettings.Get(
  203. group="nviz", key="vector", subkey=["lines", "height"]
  204. )
  205. }
  206. # thematic
  207. data["thematic"] = {
  208. "rgbcolumn": UserSettings.Get(
  209. group="nviz", key="vector", subkey=["lines", "rgbcolumn"]
  210. ),
  211. "sizecolumn": UserSettings.Get(
  212. group="nviz", key="vector", subkey=["lines", "sizecolumn"]
  213. ),
  214. "layer": 1,
  215. "usecolor": False,
  216. "usewidth": False,
  217. }
  218. if "object" in data:
  219. for attrb in ("color", "width", "mode", "height", "thematic"):
  220. data[attrb]["update"] = None
  221. def SetVectorPointsDefaultProp(self, data, longDim):
  222. """Set default vector properties -- points"""
  223. size_fraction = 0.005
  224. # size
  225. autosize = UserSettings.Get(
  226. group="nviz", key="vector", subkey=["points", "autosize"]
  227. )
  228. if autosize:
  229. data["size"] = {"value": longDim * size_fraction}
  230. else:
  231. data["size"] = {
  232. "value": UserSettings.Get(
  233. group="nviz", key="vector", subkey=["points", "size"]
  234. )
  235. }
  236. # width
  237. data["width"] = {
  238. "value": UserSettings.Get(
  239. group="nviz", key="vector", subkey=["points", "width"]
  240. )
  241. }
  242. # marker
  243. data["marker"] = {
  244. "value": UserSettings.Get(
  245. group="nviz", key="vector", subkey=["points", "marker"]
  246. )
  247. }
  248. # color
  249. value = UserSettings.Get(group="nviz", key="vector", subkey=["points", "color"])
  250. color = str(value[0]) + ":" + str(value[1]) + ":" + str(value[2])
  251. data["color"] = {"value": color}
  252. # mode
  253. data["mode"] = {"type": "surface"}
  254. # 'surface' : '', }
  255. # height
  256. data["height"] = {
  257. "value": UserSettings.Get(
  258. group="nviz", key="vector", subkey=["points", "height"]
  259. )
  260. }
  261. data["thematic"] = {
  262. "rgbcolumn": UserSettings.Get(
  263. group="nviz", key="vector", subkey=["points", "rgbcolumn"]
  264. ),
  265. "sizecolumn": UserSettings.Get(
  266. group="nviz", key="vector", subkey=["points", "sizecolumn"]
  267. ),
  268. "layer": 1,
  269. "usecolor": False,
  270. "usesize": False,
  271. }
  272. if "object" in data:
  273. for attrb in ("size", "width", "marker", "color", "height", "thematic"):
  274. data[attrb]["update"] = None
  275. def GetDrawMode(self, mode=None, style=None, shade=None, string=False):
  276. """Get surface draw mode (value) from description/selection
  277. :param mode:
  278. :param style:
  279. :param shade:
  280. :param string: if True input parameters are strings otherwise
  281. selections
  282. """
  283. if not wxnviz:
  284. return None
  285. value = 0
  286. desc = {}
  287. if string:
  288. if mode is not None:
  289. if mode == "coarse":
  290. value |= wxnviz.DM_WIRE
  291. elif mode == "fine":
  292. value |= wxnviz.DM_POLY
  293. else: # both
  294. value |= wxnviz.DM_WIRE_POLY
  295. if style is not None:
  296. if style == "wire":
  297. value |= wxnviz.DM_GRID_WIRE
  298. else: # surface
  299. value |= wxnviz.DM_GRID_SURF
  300. if shade is not None:
  301. if shade == "flat":
  302. value |= wxnviz.DM_FLAT
  303. else: # surface
  304. value |= wxnviz.DM_GOURAUD
  305. return value
  306. # -> string is False
  307. if mode is not None:
  308. if mode == 0: # coarse
  309. value |= wxnviz.DM_WIRE
  310. desc["mode"] = "coarse"
  311. elif mode == 1: # fine
  312. value |= wxnviz.DM_POLY
  313. desc["mode"] = "fine"
  314. else: # both
  315. value |= wxnviz.DM_WIRE_POLY
  316. desc["mode"] = "both"
  317. if style is not None:
  318. if style == 0: # wire
  319. value |= wxnviz.DM_GRID_WIRE
  320. desc["style"] = "wire"
  321. else: # surface
  322. value |= wxnviz.DM_GRID_SURF
  323. desc["style"] = "surface"
  324. if shade is not None:
  325. if shade == 0:
  326. value |= wxnviz.DM_FLAT
  327. desc["shading"] = "flat"
  328. else: # surface
  329. value |= wxnviz.DM_GOURAUD
  330. desc["shading"] = "gouraud"
  331. return (value, desc)
  332. def SetDecorDefaultProp(self, type):
  333. """Set default arrow properties"""
  334. data = {}
  335. # arrow
  336. if type == "arrow":
  337. data["arrow"] = copy.deepcopy(UserSettings.Get(group="nviz", key="arrow"))
  338. data["arrow"]["color"] = "%d:%d:%d" % (
  339. UserSettings.Get(group="nviz", key="arrow", subkey="color")[:3]
  340. )
  341. data["arrow"].update(
  342. copy.deepcopy(
  343. UserSettings.Get(
  344. group="nviz", key="arrow", settings_type="internal"
  345. )
  346. )
  347. )
  348. data["arrow"]["show"] = False
  349. # arrow
  350. if type == "scalebar":
  351. data["scalebar"] = copy.deepcopy(
  352. UserSettings.Get(group="nviz", key="scalebar")
  353. )
  354. data["scalebar"]["color"] = "%d:%d:%d" % (
  355. UserSettings.Get(group="nviz", key="scalebar", subkey="color")[:3]
  356. )
  357. data["scalebar"].update(
  358. copy.deepcopy(
  359. UserSettings.Get(
  360. group="nviz", key="scalebar", settings_type="internal"
  361. )
  362. )
  363. )
  364. data["scalebar"]["id"] = 0
  365. return data