settings.py 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  1. """!
  2. @package core.settings
  3. @brief Default GUI settings
  4. List of classes:
  5. - settings::Settings
  6. Usage:
  7. @code
  8. from core.settings import UserSettings
  9. @endcode
  10. (C) 2007-2011 by the GRASS Development Team
  11. This program is free software under the GNU General Public License
  12. (>=v2). Read the file COPYING that comes with GRASS for details.
  13. @author Martin Landa <landa.martin gmail.com>
  14. @author Luca Delucchi <lucadeluge gmail.com> (language choice)
  15. """
  16. import os
  17. import sys
  18. import copy
  19. import types
  20. import locale
  21. from core import globalvar
  22. from core.gcmd import GException, GError
  23. from core.utils import GetSettingsPath, PathJoin, rgb2str
  24. class Settings:
  25. """!Generic class where to store settings"""
  26. def __init__(self):
  27. # settings file
  28. self.filePath = os.path.join(GetSettingsPath(), 'wx')
  29. # key/value separator
  30. self.sep = ';'
  31. # define default settings
  32. self._defaultSettings() # -> self.defaultSettings
  33. # read settings from the file
  34. self.userSettings = copy.deepcopy(self.defaultSettings)
  35. try:
  36. self.ReadSettingsFile()
  37. except GException, e:
  38. print >> sys.stderr, e.value
  39. # define internal settings
  40. self._internalSettings() # -> self.internalSettings
  41. def _generateLocale(self):
  42. """!Generate locales
  43. """
  44. import os
  45. try:
  46. self.locs = os.listdir(os.path.join(os.environ['GISBASE'], 'locale'))
  47. self.locs.sort()
  48. # Add a default choice to not override system locale
  49. self.locs.insert(0, 'system')
  50. except:
  51. # No NLS
  52. self.locs = ['system']
  53. return 'system'
  54. def _defaultSettings(self):
  55. """!Define default settings
  56. """
  57. try:
  58. projFile = PathJoin(os.environ["GRASS_PROJSHARE"], 'epsg')
  59. except KeyError:
  60. projFile = ''
  61. id_loc = self._generateLocale()
  62. self.defaultSettings = {
  63. #
  64. # general
  65. #
  66. 'general': {
  67. # use default window layout (layer manager, displays, ...)
  68. 'defWindowPos' : {
  69. 'enabled' : True,
  70. 'dim' : '0,0,%d,%d,%d,0,%d,%d' % \
  71. (globalvar.GM_WINDOW_SIZE[0],
  72. globalvar.GM_WINDOW_SIZE[1],
  73. globalvar.GM_WINDOW_SIZE[0],
  74. globalvar.MAP_WINDOW_SIZE[0],
  75. globalvar.MAP_WINDOW_SIZE[1])
  76. },
  77. # workspace
  78. 'workspace' : {
  79. 'posDisplay' : {
  80. 'enabled' : False
  81. },
  82. 'posManager' : {
  83. 'enabled' : False
  84. },
  85. },
  86. },
  87. 'manager' : {
  88. # show opacity level widget
  89. 'changeOpacityLevel' : {
  90. 'enabled' : False
  91. },
  92. # ask when removing layer from layer tree
  93. 'askOnRemoveLayer' : {
  94. 'enabled' : True
  95. },
  96. # ask when quiting wxGUI or closing display
  97. 'askOnQuit' : {
  98. 'enabled' : True
  99. },
  100. # hide tabs
  101. 'hideTabs' : {
  102. 'search' : False,
  103. 'pyshell' : False,
  104. },
  105. 'copySelectedTextToClipboard' : {
  106. 'enabled' : False
  107. },
  108. },
  109. #
  110. # appearance
  111. #
  112. 'appearance': {
  113. 'outputfont' : {
  114. 'type' : 'Courier New',
  115. 'size': '10',
  116. },
  117. # expand/collapse element list
  118. 'elementListExpand' : {
  119. 'selection' : 0
  120. },
  121. 'menustyle' : {
  122. 'selection' : 1
  123. },
  124. 'gSelectPopupHeight' : {
  125. 'value' : 200
  126. },
  127. 'iconTheme' : {
  128. 'type' : 'grass'
  129. },
  130. },
  131. #
  132. # language
  133. #
  134. 'language': {
  135. 'locale': {
  136. 'lc_all' : id_loc
  137. }
  138. },
  139. #
  140. # display
  141. #
  142. 'display': {
  143. 'font' : {
  144. 'type' : '',
  145. 'encoding': 'ISO-8859-1',
  146. },
  147. 'driver': {
  148. 'type': 'cairo'
  149. },
  150. 'alignExtent' : {
  151. 'enabled' : True
  152. },
  153. 'compResolution' : {
  154. 'enabled' : False
  155. },
  156. 'autoRendering': {
  157. 'enabled' : True
  158. },
  159. 'autoZooming' : {
  160. 'enabled' : False
  161. },
  162. 'statusbarMode': {
  163. 'selection' : 0
  164. },
  165. 'bgcolor': {
  166. 'color' : (255, 255, 255, 255),
  167. },
  168. 'mouseWheelZoom' : {
  169. 'selection' : 1,
  170. },
  171. 'scrollDirection' : {
  172. 'selection' : 0,
  173. },
  174. },
  175. #
  176. # projection
  177. #
  178. 'projection' : {
  179. 'statusbar' : {
  180. 'proj4' : '',
  181. 'epsg' : '',
  182. 'projFile' : projFile,
  183. },
  184. 'format' : {
  185. 'll' : 'DMS',
  186. 'precision' : 2,
  187. },
  188. },
  189. #
  190. # Attribute Table Manager
  191. #
  192. 'atm' : {
  193. 'highlight' : {
  194. 'color' : (255, 255, 0, 255),
  195. 'width' : 2
  196. },
  197. 'leftDbClick' : {
  198. 'selection' : 1 # draw selected
  199. },
  200. 'askOnDeleteRec' : {
  201. 'enabled' : True
  202. },
  203. 'keycolumn' : {
  204. 'value' : 'cat'
  205. },
  206. 'encoding' : {
  207. 'value' : '',
  208. }
  209. },
  210. #
  211. # Command
  212. #
  213. 'cmd': {
  214. 'overwrite' : {
  215. 'enabled' : False
  216. },
  217. 'closeDlg' : {
  218. 'enabled' : False
  219. },
  220. 'verbosity' : {
  221. 'selection' : 'grassenv'
  222. },
  223. 'addNewLayer' : {
  224. 'enabled' : True,
  225. },
  226. 'interactiveInput' : {
  227. 'enabled' : True,
  228. },
  229. },
  230. #
  231. # d.rast
  232. #
  233. 'rasterLayer': {
  234. 'opaque': {
  235. 'enabled' : False
  236. },
  237. 'colorTable': {
  238. 'enabled' : False,
  239. 'selection' : 'rainbow'
  240. },
  241. },
  242. #
  243. # d.vect
  244. #
  245. 'vectorLayer': {
  246. 'featureColor': {
  247. 'color' : (0, 0, 0),
  248. 'transparent' : {
  249. 'enabled': False
  250. }
  251. },
  252. 'areaFillColor': {
  253. 'color' : (200, 200, 200),
  254. 'transparent' : {
  255. 'enabled': False
  256. }
  257. },
  258. 'line': {
  259. 'width' : 0,
  260. },
  261. 'point': {
  262. 'symbol': 'basic/x',
  263. 'size' : 5,
  264. },
  265. 'showType': {
  266. 'point' : {
  267. 'enabled' : True
  268. },
  269. 'line' : {
  270. 'enabled' : True
  271. },
  272. 'centroid' : {
  273. 'enabled' : True
  274. },
  275. 'boundary' : {
  276. 'enabled' : True
  277. },
  278. 'area' : {
  279. 'enabled' : True
  280. },
  281. 'face' : {
  282. 'enabled' : True
  283. },
  284. },
  285. },
  286. #
  287. # vdigit
  288. #
  289. 'vdigit' : {
  290. # symbology
  291. 'symbol' : {
  292. 'newSegment' : {
  293. 'enabled' : None,
  294. 'color' : (255, 0, 0, 255)
  295. }, # red
  296. 'newLine' : {
  297. 'enabled' : None,
  298. 'color' : (0, 86, 45, 255)
  299. }, # dark green
  300. 'highlight' : {
  301. 'enabled' : None,
  302. 'color' : (255, 255, 0, 255)
  303. }, # yellow
  304. 'highlightDupl' : {
  305. 'enabled' : None,
  306. 'color' : (255, 72, 0, 255)
  307. }, # red
  308. 'point' : {
  309. 'enabled' : True,
  310. 'color' : (0, 0, 0, 255)
  311. }, # black
  312. 'line' : {
  313. 'enabled' : True,
  314. 'color' : (0, 0, 0, 255)
  315. }, # black
  316. 'boundaryNo' : {
  317. 'enabled' : True,
  318. 'color' : (126, 126, 126, 255)
  319. }, # grey
  320. 'boundaryOne' : {
  321. 'enabled' : True,
  322. 'color' : (0, 255, 0, 255)
  323. }, # green
  324. 'boundaryTwo' : {
  325. 'enabled' : True,
  326. 'color' : (255, 135, 0, 255)
  327. }, # orange
  328. 'centroidIn' : {
  329. 'enabled' : True,
  330. 'color' : (0, 0, 255, 255)
  331. }, # blue
  332. 'centroidOut' : {
  333. 'enabled' : True,
  334. 'color' : (165, 42, 42, 255)
  335. }, # brown
  336. 'centroidDup' : {
  337. 'enabled' : True,
  338. 'color' : (156, 62, 206, 255)
  339. }, # violet
  340. 'nodeOne' : {
  341. 'enabled' : True,
  342. 'color' : (255, 0, 0, 255)
  343. }, # red
  344. 'nodeTwo' : {
  345. 'enabled' : True,
  346. 'color' : (0, 86, 45, 255)
  347. }, # dark green
  348. 'vertex' : {
  349. 'enabled' : False,
  350. 'color' : (255, 20, 147, 255)
  351. }, # deep pink
  352. 'area' : {
  353. 'enabled' : True,
  354. 'color' : (217, 255, 217, 255)
  355. }, # green
  356. 'direction' : {
  357. 'enabled' : False,
  358. 'color' : (255, 0, 0, 255)
  359. }, # red
  360. },
  361. # display
  362. 'lineWidth' : {
  363. 'value' : 2,
  364. 'units' : 'screen pixels'
  365. },
  366. # snapping
  367. 'snapping' : {
  368. 'value' : 10,
  369. 'units' : 'screen pixels'
  370. },
  371. 'snapToVertex' : {
  372. 'enabled' : False
  373. },
  374. # digitize new record
  375. 'addRecord' : {
  376. 'enabled' : True
  377. },
  378. 'layer' :{
  379. 'value' : 1
  380. },
  381. 'category' : {
  382. 'value' : 1
  383. },
  384. 'categoryMode' : {
  385. 'selection' : 0
  386. },
  387. # delete existing feature(s)
  388. 'delRecord' : {
  389. 'enabled' : True
  390. },
  391. # query tool
  392. 'query' : {
  393. 'selection' : 0,
  394. 'box' : True
  395. },
  396. 'queryLength' : {
  397. 'than-selection' : 0,
  398. 'thresh' : 0
  399. },
  400. 'queryDangle' : {
  401. 'than-selection' : 0,
  402. 'thresh' : 0
  403. },
  404. # select feature (point, line, centroid, boundary)
  405. 'selectType': {
  406. 'point' : {
  407. 'enabled' : True
  408. },
  409. 'line' : {
  410. 'enabled' : True
  411. },
  412. 'centroid' : {
  413. 'enabled' : True
  414. },
  415. 'boundary' : {
  416. 'enabled' : True
  417. },
  418. },
  419. 'selectThresh' : {
  420. 'value' : 10,
  421. 'units' : 'screen pixels'
  422. },
  423. 'checkForDupl' : {
  424. 'enabled' : False
  425. },
  426. 'selectInside' : {
  427. 'enabled' : False
  428. },
  429. # exit
  430. 'saveOnExit' : {
  431. 'enabled' : False,
  432. },
  433. # break lines on intersection
  434. 'breakLines' : {
  435. 'enabled' : False,
  436. },
  437. },
  438. #
  439. # plots for profiles, histograms, and scatterplots
  440. #
  441. 'profile': {
  442. 'raster' : {
  443. 'pcolor' : (0, 0, 255, 255), # line color
  444. 'pwidth' : 1, # line width
  445. 'pstyle' : 'solid', # line pen style
  446. 'datatype' : 'cell', # raster type
  447. },
  448. 'font' : {
  449. 'titleSize' : 12,
  450. 'axisSize' : 11,
  451. 'legendSize' : 10,
  452. },
  453. 'marker' : {
  454. 'color' : (0, 0, 0, 255),
  455. 'fill' : 'transparent',
  456. 'size' : 2,
  457. 'type' : 'triangle',
  458. 'legend' : _('Segment break'),
  459. },
  460. 'grid' : {
  461. 'color' : (200, 200, 200, 255),
  462. 'enabled' : True,
  463. },
  464. 'x-axis' : {
  465. 'type' : 'auto', # axis format
  466. 'min' : 0, # axis min for custom axis range
  467. 'max': 0, # axis max for custom axis range
  468. 'log' : False,
  469. },
  470. 'y-axis' : {
  471. 'type' : 'auto', # axis format
  472. 'min' : 0, # axis min for custom axis range
  473. 'max': 0, # axis max for custom axis range
  474. 'log' : False,
  475. },
  476. 'legend' : {
  477. 'enabled' : True
  478. },
  479. },
  480. 'histogram': {
  481. 'raster' : {
  482. 'pcolor' : (0, 0, 255, 255), # line color
  483. 'pwidth' : 1, # line width
  484. 'pstyle' : 'solid', # line pen style
  485. 'datatype' : 'cell', # raster type
  486. },
  487. 'font' : {
  488. 'titleSize' : 12,
  489. 'axisSize' : 11,
  490. 'legendSize' : 10,
  491. },
  492. 'grid' : {
  493. 'color' : (200, 200, 200, 255),
  494. 'enabled' : True,
  495. },
  496. 'x-axis' : {
  497. 'type' : 'auto', # axis format
  498. 'min' : 0, # axis min for custom axis range
  499. 'max' : 0, # axis max for custom axis range
  500. 'log' : False,
  501. },
  502. 'y-axis' : {
  503. 'type' : 'auto', # axis format
  504. 'min' : 0, # axis min for custom axis range
  505. 'max' : 0, # axis max for custom axis range
  506. 'log' : False,
  507. },
  508. 'legend' : {
  509. 'enabled' : True
  510. },
  511. },
  512. 'scatter': {
  513. 'rasters' : {
  514. 'pcolor' : (0, 0, 255, 255),
  515. 'pfill' : 'solid',
  516. 'psize' : 1,
  517. 'ptype' : 'dot',
  518. 'plegend' : _('Data point'),
  519. 0 : {'datatype' : 'CELL'},
  520. 1 : {'datatype' : 'CELL'},
  521. },
  522. 'font' : {
  523. 'titleSize' : 12,
  524. 'axisSize' : 11,
  525. 'legendSize' : 10,
  526. },
  527. 'grid' : {
  528. 'color' : (200, 200, 200, 255),
  529. 'enabled' : True,
  530. },
  531. 'x-axis' : {
  532. 'type' : 'auto', # axis format
  533. 'min' : 0, # axis min for custom axis range
  534. 'max' : 0, # axis max for custom axis range
  535. 'log' : False,
  536. },
  537. 'y-axis' : {
  538. 'type' : 'auto', # axis format
  539. 'min' : 0, # axis min for custom axis range
  540. 'max' : 0, # axis max for custom axis range
  541. 'log' : False,
  542. },
  543. 'legend' : {
  544. 'enabled' : True
  545. },
  546. },
  547. 'gcpman' : {
  548. 'rms' : {
  549. 'highestonly' : True,
  550. 'sdfactor' : 1,
  551. },
  552. 'symbol' : {
  553. 'color' : (0, 0, 255, 255),
  554. 'hcolor' : (255, 0, 0, 255),
  555. 'scolor' : (0, 255, 0, 255),
  556. 'ucolor' : (255, 165, 0, 255),
  557. 'unused' : True,
  558. 'size' : 8,
  559. 'width' : 2,
  560. },
  561. },
  562. 'nviz' : {
  563. 'view' : {
  564. 'persp' : {
  565. 'value' : 20,
  566. 'step' : 2,
  567. },
  568. 'position' : {
  569. 'x' : 0.84,
  570. 'y' : 0.16,
  571. },
  572. 'twist' : {
  573. 'value' : 0,
  574. },
  575. 'z-exag' : {
  576. 'min' : 0,
  577. 'max' : 10,
  578. 'value': 1,
  579. },
  580. 'background' : {
  581. 'color' : (255, 255, 255, 255), # white
  582. },
  583. },
  584. 'fly' : {
  585. 'exag' : {
  586. 'move' : 5,
  587. 'turn' : 5,
  588. }
  589. },
  590. 'animation' : {
  591. 'fps' : 24,
  592. 'prefix' : _("animation")
  593. },
  594. 'surface' : {
  595. 'shine': {
  596. 'map' : False,
  597. 'value' : 60.0,
  598. },
  599. 'color' : {
  600. 'map' : True,
  601. 'value' : (100, 100, 100, 255), # constant: grey
  602. },
  603. 'draw' : {
  604. 'wire-color' : (136, 136, 136, 255),
  605. 'mode' : 1, # fine
  606. 'style' : 1, # surface
  607. 'shading' : 1, # gouraud
  608. 'res-fine' : 6,
  609. 'res-coarse' : 9,
  610. },
  611. 'position' : {
  612. 'x' : 0,
  613. 'y' : 0,
  614. 'z' : 0,
  615. },
  616. },
  617. 'constant' : {
  618. 'color' : (100, 100, 100, 255),
  619. 'value' : 0.0,
  620. 'transp' : 0,
  621. 'resolution': 6
  622. },
  623. 'vector' : {
  624. 'lines' : {
  625. 'show' : False,
  626. 'width' : 2,
  627. 'color' : (0, 0, 255, 255), # blue
  628. 'flat' : False,
  629. 'height' : 0,
  630. 'rgbcolumn': None,
  631. 'sizecolumn': None,
  632. },
  633. 'points' : {
  634. 'show' : False,
  635. 'size' : 100,
  636. 'width' : 2,
  637. 'marker' : 2,
  638. 'color' : (0, 0, 255, 255), # blue
  639. 'height' : 0,
  640. 'rgbcolumn': None,
  641. 'sizecolumn': None,
  642. }
  643. },
  644. 'volume' : {
  645. 'color' : {
  646. 'map' : True,
  647. 'value' : (100, 100, 100, 255), # constant: grey
  648. },
  649. 'draw' : {
  650. 'mode' : 0, # isosurfaces
  651. 'shading' : 1, # gouraud
  652. 'resolution' : 3, # polygon resolution
  653. 'box' : False # draw wire box
  654. },
  655. 'shine': {
  656. 'map' : False,
  657. 'value' : 60,
  658. },
  659. 'topo': {
  660. 'map' : None,
  661. 'value' : 0.0
  662. },
  663. 'transp': {
  664. 'map' : None,
  665. 'value': 0
  666. },
  667. 'mask': {
  668. 'map' : None,
  669. 'value': ''
  670. },
  671. 'slice_position': {
  672. 'x1' : 0,
  673. 'x2' : 1,
  674. 'y1' : 0,
  675. 'y2' : 1,
  676. 'z1' : 0,
  677. 'z2' : 1,
  678. 'axis' : 0,
  679. }
  680. },
  681. 'cplane' : {
  682. 'shading': 4,
  683. 'rotation':{
  684. 'rot': 0,
  685. 'tilt': 0
  686. },
  687. 'position':{
  688. 'x' : 0,
  689. 'y' : 0,
  690. 'z' : 0
  691. }
  692. },
  693. 'light' : {
  694. 'position' : {
  695. 'x' : 0.68,
  696. 'y' : -0.68,
  697. 'z' : 80,
  698. },
  699. 'bright' : 80,
  700. 'color' : (255, 255, 255, 255), # white
  701. 'ambient' : 20,
  702. },
  703. 'fringe' : {
  704. 'elev' : 55,
  705. 'color' : (128, 128, 128, 255), # grey
  706. },
  707. 'arrow': {
  708. 'color': (0, 0, 0),
  709. },
  710. 'scalebar': {
  711. 'color': (0, 0, 0),
  712. }
  713. },
  714. 'modeler' : {
  715. 'disabled': {
  716. 'color': (211, 211, 211, 255), # light grey
  717. },
  718. 'action' : {
  719. 'color' : {
  720. 'valid' : (180, 234, 154, 255), # light green
  721. 'invalid' : (255, 255, 255, 255), # white
  722. 'running' : (255, 0, 0, 255), # red
  723. },
  724. 'size' : {
  725. 'width' : 125,
  726. 'height' : 50,
  727. },
  728. 'width': {
  729. 'parameterized' : 2,
  730. 'default' : 1,
  731. },
  732. },
  733. 'data' : {
  734. 'color': {
  735. 'raster' : (215, 215, 248, 255), # light blue
  736. 'raster3d' : (215, 248, 215, 255), # light green
  737. 'vector' : (248, 215, 215, 255), # light red
  738. },
  739. 'size' : {
  740. 'width' : 175,
  741. 'height' : 50,
  742. },
  743. },
  744. 'loop' : {
  745. 'color' : {
  746. 'valid' : (234, 226, 154, 255), # light yellow
  747. },
  748. 'size' : {
  749. 'width' : 175,
  750. 'height' : 40,
  751. },
  752. },
  753. 'if-else' : {
  754. 'size' : {
  755. 'width' : 150,
  756. 'height' : 40,
  757. },
  758. },
  759. },
  760. }
  761. # quick fix, http://trac.osgeo.org/grass/ticket/1233
  762. # TODO
  763. if sys.platform == 'darwin':
  764. self.defaultSettings['general']['defWindowPos']['enabled'] = False
  765. def _internalSettings(self):
  766. """!Define internal settings (based on user settings)
  767. """
  768. self.internalSettings = {}
  769. for group in self.userSettings.keys():
  770. self.internalSettings[group] = {}
  771. for key in self.userSettings[group].keys():
  772. self.internalSettings[group][key] = {}
  773. # self.internalSettings['general']["mapsetPath"]['value'] = self.GetMapsetPath()
  774. self.internalSettings['appearance']['elementListExpand']['choices'] = \
  775. (_("Collapse all except PERMANENT and current"),
  776. _("Collapse all except PERMANENT"),
  777. _("Collapse all except current"),
  778. _("Collapse all"),
  779. _("Expand all"))
  780. self.internalSettings['language']['locale']['choices'] = tuple(self.locs)
  781. self.internalSettings['atm']['leftDbClick']['choices'] = (_('Edit selected record'),
  782. _('Display selected'))
  783. self.internalSettings['cmd']['verbosity']['choices'] = ('grassenv',
  784. 'verbose',
  785. 'quiet')
  786. self.internalSettings['appearance']['iconTheme']['choices'] = ('grass',)
  787. self.internalSettings['appearance']['menustyle']['choices'] = \
  788. (_("Classic (labels only)"),
  789. _("Combined (labels and module names)"),
  790. _("Professional (module names only)"))
  791. self.internalSettings['appearance']['gSelectPopupHeight']['min'] = 50
  792. # there is also maxHeight given to TreeCtrlComboPopup.GetAdjustedSize
  793. self.internalSettings['appearance']['gSelectPopupHeight']['max'] = 1000
  794. self.internalSettings['display']['driver']['choices'] = ['cairo', 'png']
  795. self.internalSettings['display']['statusbarMode']['choices'] = None # set during MapFrame init
  796. self.internalSettings['display']['mouseWheelZoom']['choices'] = (_('Zoom and recenter'),
  797. _('Zoom to mouse cursor'),
  798. _('Nothing'))
  799. self.internalSettings['display']['scrollDirection']['choices'] = (_('Scroll forward to zoom in'),
  800. _('Scroll back to zoom in'))
  801. self.internalSettings['nviz']['view'] = {}
  802. self.internalSettings['nviz']['view']['twist'] = {}
  803. self.internalSettings['nviz']['view']['twist']['min'] = -180
  804. self.internalSettings['nviz']['view']['twist']['max'] = 180
  805. self.internalSettings['nviz']['view']['persp'] = {}
  806. self.internalSettings['nviz']['view']['persp']['min'] = 1
  807. self.internalSettings['nviz']['view']['persp']['max'] = 100
  808. self.internalSettings['nviz']['view']['height'] = {}
  809. self.internalSettings['nviz']['view']['height']['value'] = -1
  810. self.internalSettings['nviz']['view']['z-exag'] = {}
  811. self.internalSettings['nviz']['view']['z-exag']['llRatio'] = 1
  812. self.internalSettings['nviz']['view']['rotation'] = None
  813. self.internalSettings['nviz']['view']['focus'] = {}
  814. self.internalSettings['nviz']['view']['focus']['x'] = -1
  815. self.internalSettings['nviz']['view']['focus']['y'] = -1
  816. self.internalSettings['nviz']['view']['focus']['z'] = -1
  817. self.internalSettings['nviz']['view']['dir'] = {}
  818. self.internalSettings['nviz']['view']['dir']['x'] = -1
  819. self.internalSettings['nviz']['view']['dir']['y'] = -1
  820. self.internalSettings['nviz']['view']['dir']['z'] = -1
  821. self.internalSettings['nviz']['view']['dir']['use'] = False
  822. for decor in ('arrow', 'scalebar'):
  823. self.internalSettings['nviz'][decor] = {}
  824. self.internalSettings['nviz'][decor]['position'] = {}
  825. self.internalSettings['nviz'][decor]['position']['x'] = 0
  826. self.internalSettings['nviz'][decor]['position']['y'] = 0
  827. self.internalSettings['nviz'][decor]['size'] = 100
  828. self.internalSettings['nviz']['vector'] = {}
  829. self.internalSettings['nviz']['vector']['points'] = {}
  830. self.internalSettings['nviz']['vector']['points']['marker'] = ("x",
  831. _("box"),
  832. _("sphere"),
  833. _("cube"),
  834. _("diamond"),
  835. _("dtree"),
  836. _("ctree"),
  837. _("aster"),
  838. _("gyro"),
  839. _("histogram"))
  840. self.internalSettings['vdigit']['bgmap'] = {}
  841. self.internalSettings['vdigit']['bgmap']['value'] = ''
  842. def ReadSettingsFile(self, settings = None):
  843. """!Reads settings file (mapset, location, gisdbase)"""
  844. if settings is None:
  845. settings = self.userSettings
  846. self._readFile(self.filePath, settings)
  847. # set environment variables
  848. font = self.Get(group = 'display', key = 'font', subkey = 'type')
  849. enc = self.Get(group = 'display', key = 'font', subkey = 'encoding')
  850. if font:
  851. os.environ["GRASS_FONT"] = font
  852. if enc:
  853. os.environ["GRASS_ENCODING"] = enc
  854. def _readFile(self, filename, settings = None):
  855. """!Read settings from file to dict
  856. @param filename settings file path
  857. @param settings dict where to store settings (None for self.userSettings)
  858. """
  859. if settings is None:
  860. settings = self.userSettings
  861. if not os.path.exists(filename):
  862. return
  863. try:
  864. fd = open(filename, "r")
  865. except IOError:
  866. sys.stderr.write(_("Unable to read settings file <%s>\n") % filename)
  867. return
  868. try:
  869. line = ''
  870. for line in fd.readlines():
  871. line = line.rstrip('%s' % os.linesep)
  872. group, key = line.split(self.sep)[0:2]
  873. kv = line.split(self.sep)[2:]
  874. subkeyMaster = None
  875. if len(kv) % 2 != 0: # multiple (e.g. nviz)
  876. subkeyMaster = kv[0]
  877. del kv[0]
  878. idx = 0
  879. while idx < len(kv):
  880. if subkeyMaster:
  881. subkey = [subkeyMaster, kv[idx]]
  882. else:
  883. subkey = kv[idx]
  884. value = kv[idx+1]
  885. value = self._parseValue(value, read = True)
  886. self.Append(settings, group, key, subkey, value)
  887. idx += 2
  888. except ValueError, e:
  889. print >> sys.stderr, _("Error: Reading settings from file <%(file)s> failed.\n"
  890. "\t\tDetails: %(detail)s\n"
  891. "\t\tLine: '%(line)s'\n") % { 'file' : filename,
  892. 'detail' : e,
  893. 'line' : line }
  894. fd.close()
  895. fd.close()
  896. def SaveToFile(self, settings = None):
  897. """!Save settings to the file"""
  898. if settings is None:
  899. settings = self.userSettings
  900. dirPath = GetSettingsPath()
  901. if not os.path.exists(dirPath):
  902. try:
  903. os.mkdir(dirPath)
  904. except:
  905. GError(_('Unable to create settings directory'))
  906. return
  907. try:
  908. file = open(self.filePath, "w")
  909. for group in settings.keys():
  910. for key in settings[group].keys():
  911. subkeys = settings[group][key].keys()
  912. file.write('%s%s%s%s' % (group, self.sep, key, self.sep))
  913. for idx in range(len(subkeys)):
  914. value = settings[group][key][subkeys[idx]]
  915. if type(value) == types.DictType:
  916. if idx > 0:
  917. file.write('%s%s%s%s%s' % (os.linesep, group, self.sep, key, self.sep))
  918. file.write('%s%s' % (subkeys[idx], self.sep))
  919. kvalues = settings[group][key][subkeys[idx]].keys()
  920. srange = range(len(kvalues))
  921. for sidx in srange:
  922. svalue = self._parseValue(settings[group][key][subkeys[idx]][kvalues[sidx]])
  923. file.write('%s%s%s' % (kvalues[sidx], self.sep,
  924. svalue))
  925. if sidx < len(kvalues) - 1:
  926. file.write('%s' % self.sep)
  927. else:
  928. if idx > 0 and \
  929. type(settings[group][key][subkeys[idx - 1]]) == types.DictType:
  930. file.write('%s%s%s%s%s' % (os.linesep, group, self.sep, key, self.sep))
  931. value = self._parseValue(settings[group][key][subkeys[idx]])
  932. file.write('%s%s%s' % (subkeys[idx], self.sep, value))
  933. if idx < len(subkeys) - 1 and \
  934. type(settings[group][key][subkeys[idx + 1]]) != types.DictType:
  935. file.write('%s' % self.sep)
  936. file.write(os.linesep)
  937. except IOError, e:
  938. raise GException(e)
  939. except StandardError, e:
  940. raise GException(_('Writing settings to file <%(file)s> failed.'
  941. '\n\nDetails: %(detail)s') % { 'file' : self.filePath,
  942. 'detail' : e })
  943. file.close()
  944. def _parseValue(self, value, read = False):
  945. """!Parse value to be store in settings file"""
  946. if read: # -> read settings (cast values)
  947. if value == 'True':
  948. value = True
  949. elif value == 'False':
  950. value = False
  951. elif value == 'None':
  952. value = None
  953. elif ':' in value: # -> color
  954. try:
  955. value = tuple(map(int, value.split(':')))
  956. except ValueError: # -> string
  957. pass
  958. else:
  959. try:
  960. value = int(value)
  961. except ValueError:
  962. try:
  963. value = float(value)
  964. except ValueError:
  965. pass
  966. else: # -> write settings
  967. if type(value) == type(()): # -> color
  968. value = str(value[0]) + ':' +\
  969. str(value[1]) + ':' + \
  970. str(value[2])
  971. return value
  972. def Get(self, group, key = None, subkey = None, internal = False):
  973. """!Get value by key/subkey
  974. Raise KeyError if key is not found
  975. @param group settings group
  976. @param key (value, None)
  977. @param subkey (value, list or None)
  978. @param internal use internal settings instead
  979. @return value
  980. """
  981. if internal is True:
  982. settings = self.internalSettings
  983. else:
  984. settings = self.userSettings
  985. try:
  986. if subkey is None:
  987. if key is None:
  988. return settings[group]
  989. else:
  990. return settings[group][key]
  991. else:
  992. if type(subkey) == type(tuple()) or \
  993. type(subkey) == type(list()):
  994. return settings[group][key][subkey[0]][subkey[1]]
  995. else:
  996. return settings[group][key][subkey]
  997. except KeyError:
  998. print >> sys.stderr, "Settings: unable to get value '%s:%s:%s'\n" % \
  999. (group, key, subkey)
  1000. def Set(self, group, value, key = None, subkey = None, internal = False):
  1001. """!Set value of key/subkey
  1002. Raise KeyError if group/key is not found
  1003. @param group settings group
  1004. @param key key (value, None)
  1005. @param subkey subkey (value, list or None)
  1006. @param value value
  1007. @param internal use internal settings instead
  1008. """
  1009. if internal is True:
  1010. settings = self.internalSettings
  1011. else:
  1012. settings = self.userSettings
  1013. try:
  1014. if subkey is None:
  1015. if key is None:
  1016. settings[group] = value
  1017. else:
  1018. settings[group][key] = value
  1019. else:
  1020. if type(subkey) == type(tuple()) or \
  1021. type(subkey) == type(list()):
  1022. settings[group][key][subkey[0]][subkey[1]] = value
  1023. else:
  1024. settings[group][key][subkey] = value
  1025. except KeyError:
  1026. raise GException("%s '%s:%s:%s'" % (_("Unable to set "), group, key, subkey))
  1027. def Append(self, dict, group, key, subkey, value):
  1028. """!Set value of key/subkey
  1029. Create group/key/subkey if not exists
  1030. @param dict settings dictionary to use
  1031. @param group settings group
  1032. @param key key
  1033. @param subkey subkey (value or list)
  1034. @param value value
  1035. """
  1036. if group not in dict:
  1037. dict[group] = {}
  1038. if key not in dict[group]:
  1039. dict[group][key] = {}
  1040. if type(subkey) == types.ListType:
  1041. # TODO: len(subkey) > 2
  1042. if subkey[0] not in dict[group][key]:
  1043. dict[group][key][subkey[0]] = {}
  1044. try:
  1045. dict[group][key][subkey[0]][subkey[1]] = value
  1046. except TypeError:
  1047. print >> sys.stderr, _("Unable to parse settings '%s'") % value + \
  1048. ' (' + group + ':' + key + ':' + subkey[0] + ':' + subkey[1] + ')'
  1049. else:
  1050. try:
  1051. dict[group][key][subkey] = value
  1052. except TypeError:
  1053. print >> sys.stderr, _("Unable to parse settings '%s'") % value + \
  1054. ' (' + group + ':' + key + ':' + subkey + ')'
  1055. def GetDefaultSettings(self):
  1056. """!Get default user settings"""
  1057. return self.defaultSettings
  1058. def Reset(self, key = None):
  1059. """!Reset to default settings
  1060. @key key in settings dict (None for all keys)
  1061. """
  1062. if not key:
  1063. self.userSettings = copy.deepcopy(self.defaultSettings)
  1064. else:
  1065. self.userSettings[key] = copy.deepcopy(self.defaultSettings[key])
  1066. UserSettings = Settings()
  1067. def GetDisplayVectSettings():
  1068. settings = list()
  1069. if not UserSettings.Get(group = 'vectorLayer', key = 'featureColor', subkey = ['transparent', 'enabled']):
  1070. featureColor = UserSettings.Get(group = 'vectorLayer', key = 'featureColor', subkey = 'color')
  1071. settings.append('color=%s' % rgb2str.get(featureColor, ':'.join(map(str,featureColor))))
  1072. else:
  1073. settings.append('color=none')
  1074. if not UserSettings.Get(group = 'vectorLayer', key = 'areaFillColor', subkey = ['transparent', 'enabled']):
  1075. fillColor = UserSettings.Get(group = 'vectorLayer', key = 'areaFillColor', subkey = 'color')
  1076. settings.append('fcolor=%s' % rgb2str.get(fillColor, ':'.join(map(str,fillColor))))
  1077. else:
  1078. settings.append('fcolor=none')
  1079. settings.append('width=%s' % UserSettings.Get(group = 'vectorLayer', key = 'line', subkey = 'width'))
  1080. settings.append('icon=%s' % UserSettings.Get(group = 'vectorLayer', key = 'point', subkey = 'symbol'))
  1081. settings.append('size=%s' % UserSettings.Get(group = 'vectorLayer', key = 'point', subkey = 'size'))
  1082. types = []
  1083. for ftype in ['point', 'line', 'boundary', 'centroid', 'area', 'face']:
  1084. if UserSettings.Get(group = 'vectorLayer', key = 'showType', subkey = [ftype, 'enabled']):
  1085. types.append(ftype)
  1086. settings.append('type=%s' % ','.join(types))
  1087. return settings