workspace.py 14.7 KB
Newer Older
xuebingbing's avatar
xuebingbing committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
"""
@package nviz.workspace

@brief wxNviz workspace settings

Classes:
 - workspace::NvizSettings

(C) 2007-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 Anna Kratochvilova <kratochanna gmail.com> (wxNviz / Google SoC 2011)
"""

import copy
import six

from core.settings import UserSettings

try:
    from nviz import wxnviz
except ImportError:
    wxnviz = None


class NvizSettings(object):

    def __init__(self):
        pass

    def SetConstantDefaultProp(self):
        """Set default constant data properties"""
        data = dict()
        for key, value in six.iteritems(UserSettings.Get(group='nviz',
                                                         key='constant')):
            data[key] = value
        color = str(data['color'][0]) + ':' + str(data['color']
                                                  [1]) + ':' + str(data['color'][2])
        data['color'] = color

        return data

    def SetSurfaceDefaultProp(self, data=None):
        """Set default surface data properties"""
        if not data:
            data = dict()
        for sec in ('attribute', 'draw', 'mask', 'position'):
            data[sec] = {}

        #
        # attributes
        #
        for attrb in ('shine', ):
            data['attribute'][attrb] = {}
            for key, value in six.iteritems(UserSettings.Get(group='nviz',
                                                             key='surface',
                                                             subkey=attrb)):
                data['attribute'][attrb][key] = value
            data['attribute'][attrb]['update'] = None

        #
        # draw
        #
        data['draw']['all'] = False  # apply only for current surface
        for control, value in six.iteritems(UserSettings.Get(group='nviz',
                                                             key='surface',
                                                             subkey='draw')):
            if control[:3] == 'res':
                if 'resolution' not in data['draw']:
                    data['draw']['resolution'] = {}
                if 'update' not in data['draw']['resolution']:
                    data['draw']['resolution']['update'] = None
                data['draw']['resolution'][control[4:]] = value
                continue

            if control == 'wire-color':
                value = str(value[0]) + ':' + str(value[1]
                                                  ) + ':' + str(value[2])
            elif control in ('mode', 'style', 'shading'):
                if 'mode' not in data['draw']:
                    data['draw']['mode'] = {}
                continue

            data['draw'][control] = {'value': value}
            data['draw'][control]['update'] = None

        value, desc = self.GetDrawMode(
            UserSettings.Get(
                group='nviz', key='surface', subkey=[
                    'draw', 'mode']), UserSettings.Get(
                group='nviz', key='surface', subkey=[
                    'draw', 'style']), UserSettings.Get(
                        group='nviz', key='surface', subkey=[
                            'draw', 'shading']))

        data['draw']['mode'] = {'value': value,
                                'desc': desc,
                                'update': None}
        # position
        for coord in ('x', 'y', 'z'):
            data['position'][coord] = UserSettings.Get(
                group='nviz', key='surface', subkey=['position', coord])
        data['position']['update'] = None

        return data

    def SetVolumeDefaultProp(self):
        """Set default volume data properties"""
        data = dict()
        for sec in ('attribute', 'draw', 'position'):
            data[sec] = dict()
            for sec in ('isosurface', 'slice'):
                data[sec] = list()

        #
        # draw
        #
        for control, value in six.iteritems(UserSettings.Get(group='nviz',
                                                             key='volume',
                                                             subkey='draw')):
            if control == 'shading':
                sel = UserSettings.Get(
                    group='nviz', key='volume', subkey=[
                        'draw', 'shading'])
                value, desc = self.GetDrawMode(shade=sel, string=False)

                data['draw']['shading'] = {}
                data['draw']['shading']['isosurface'] = {
                    'value': value, 'desc': desc['shading']}
                data['draw']['shading']['slice'] = {'value': value,
                                                    'desc': desc['shading']}
            elif control == 'mode':
                sel = UserSettings.Get(
                    group='nviz', key='volume', subkey=[
                        'draw', 'mode'])
                if sel == 0:
                    desc = 'isosurface'
                else:
                    desc = 'slice'
                data['draw']['mode'] = {'value': sel,
                                        'desc': desc, }
            elif control == 'box':
                box = UserSettings.Get(
                    group='nviz', key='volume', subkey=[
                        'draw', 'box'])
                data['draw']['box'] = {'enabled': box}

            else:
                data['draw'][control] = {}
                data['draw'][control]['isosurface'] = {'value': value}
                data['draw'][control]['slice'] = {'value': value}

            if 'update' not in data['draw'][control]:
                data['draw'][control]['update'] = None

        #
        # isosurface attributes
        #
        for attrb in ('shine', ):
            data['attribute'][attrb] = {}
            for key, value in six.iteritems(UserSettings.Get(group='nviz',
                                                             key='volume',
                                                             subkey=attrb)):
                data['attribute'][attrb][key] = value

        return data

    def SetIsosurfaceDefaultProp(self):
        """Set default isosurface properties"""
        data = dict()
        for attr in ('shine', 'topo', 'transp', 'color', 'inout'):
            data[attr] = {}
            data[attr]['update'] = None
            if attr == 'inout':
                data[attr]['value'] = 0
                continue
            for key, value in six.iteritems(UserSettings.Get(group='nviz',
                                                             key='volume',
                                                             subkey=attr)):
                data[attr][key] = value
        return data

    def SetSliceDefaultProp(self):
        """Set default slice properties"""
        data = dict()
        data['position'] = copy.deepcopy(
            UserSettings.Get(
                group='nviz',
                key='volume',
                subkey='slice_position'))
        data['position']['update'] = None

        data['transp'] = copy.deepcopy(
            UserSettings.Get(
                group='nviz',
                key='volume',
                subkey='transp'))
        return data

    def SetVectorDefaultProp(self, longDim, data=None):
        """Set default vector data properties"""
        if not data:
            data = dict()
        for sec in ('lines', 'points'):
            data[sec] = {}

        self.SetVectorLinesDefaultProp(data['lines'])
        self.SetVectorPointsDefaultProp(data['points'], longDim)

        return data

    def SetVectorLinesDefaultProp(self, data):
        """Set default vector properties -- lines"""
        # width
        data['width'] = {'value': UserSettings.Get(group='nviz', key='vector',
                                                   subkey=['lines', 'width'])}

        # color
        value = UserSettings.Get(group='nviz', key='vector',
                                 subkey=['lines', 'color'])
        color = str(value[0]) + ':' + str(value[1]) + ':' + str(value[2])
        data['color'] = {'value': color}

        # mode
        if UserSettings.Get(group='nviz', key='vector',
                            subkey=['lines', 'flat']):
            type = 'flat'

        else:
            type = 'surface'

        data['mode'] = {}
        data['mode']['type'] = type
        data['mode']['update'] = None

        # height
        data['height'] = {
            'value': UserSettings.Get(
                group='nviz',
                key='vector',
                subkey=[
                    'lines',
                    'height'])}
        # thematic
        data['thematic'] = {'rgbcolumn': UserSettings.Get(group='nviz', key='vector',
                                                          subkey=['lines', 'rgbcolumn']),
                            'sizecolumn': UserSettings.Get(group='nviz', key='vector',
                                                           subkey=['lines', 'sizecolumn']),
                            'layer': 1,
                            'usecolor': False,
                            'usewidth': False}
        if 'object' in data:
            for attrb in ('color', 'width', 'mode', 'height', 'thematic'):
                data[attrb]['update'] = None

    def SetVectorPointsDefaultProp(self, data, longDim):
        """Set default vector properties -- points"""
        size_fraction = 0.005
        # size
        autosize = UserSettings.Get(group='nviz', key='vector',
                                    subkey=['points', 'autosize'])
        if autosize:
            data['size'] = {'value': longDim * size_fraction}
        else:
            data['size'] = {'value': UserSettings.Get(group='nviz', key='vector',
                                                      subkey=['points', 'size'])}

        # width
        data['width'] = {'value': UserSettings.Get(group='nviz', key='vector',
                                                   subkey=['points', 'width'])}

        # marker
        data['marker'] = {
            'value': UserSettings.Get(
                group='nviz',
                key='vector',
                subkey=[
                    'points',
                    'marker'])}

        # color
        value = UserSettings.Get(group='nviz', key='vector',
                                 subkey=['points', 'color'])
        color = str(value[0]) + ':' + str(value[1]) + ':' + str(value[2])
        data['color'] = {'value': color}

        # mode
        data['mode'] = {'type': 'surface'}
# 'surface' : '', }

        # height
        data['height'] = {
            'value': UserSettings.Get(
                group='nviz',
                key='vector',
                subkey=[
                    'points',
                    'height'])}

        data['thematic'] = {
            'rgbcolumn': UserSettings.Get(
                group='nviz',
                key='vector',
                subkey=[
                    'points',
                    'rgbcolumn']),
            'sizecolumn': UserSettings.Get(
                group='nviz',
                key='vector',
                subkey=[
                    'points',
                    'sizecolumn']),
            'layer': 1,
            'usecolor': False,
            'usesize': False}
        if 'object' in data:
            for attrb in ('size', 'width', 'marker',
                          'color', 'height', 'thematic'):
                data[attrb]['update'] = None

    def GetDrawMode(self, mode=None, style=None, shade=None, string=False):
        """Get surface draw mode (value) from description/selection

        :param mode:
        :param style:
        :param shade:
        :param string: if True input parameters are strings otherwise
                       selections
        """
        if not wxnviz:
            return None

        value = 0
        desc = {}

        if string:
            if mode is not None:
                if mode == 'coarse':
                    value |= wxnviz.DM_WIRE
                elif mode == 'fine':
                    value |= wxnviz.DM_POLY
                else:  # both
                    value |= wxnviz.DM_WIRE_POLY

            if style is not None:
                if style == 'wire':
                    value |= wxnviz.DM_GRID_WIRE
                else:  # surface
                    value |= wxnviz.DM_GRID_SURF

            if shade is not None:
                if shade == 'flat':
                    value |= wxnviz.DM_FLAT
                else:  # surface
                    value |= wxnviz.DM_GOURAUD

            return value

        # -> string is False
        if mode is not None:
            if mode == 0:  # coarse
                value |= wxnviz.DM_WIRE
                desc['mode'] = 'coarse'
            elif mode == 1:  # fine
                value |= wxnviz.DM_POLY
                desc['mode'] = 'fine'
            else:  # both
                value |= wxnviz.DM_WIRE_POLY
                desc['mode'] = 'both'

        if style is not None:
            if style == 0:  # wire
                value |= wxnviz.DM_GRID_WIRE
                desc['style'] = 'wire'
            else:  # surface
                value |= wxnviz.DM_GRID_SURF
                desc['style'] = 'surface'

        if shade is not None:
            if shade == 0:
                value |= wxnviz.DM_FLAT
                desc['shading'] = 'flat'
            else:  # surface
                value |= wxnviz.DM_GOURAUD
                desc['shading'] = 'gouraud'

        return (value, desc)

    def SetDecorDefaultProp(self, type):
        """Set default arrow properties
        """
        data = {}

        # arrow
        if type == 'arrow':
            data['arrow'] = copy.deepcopy(UserSettings.Get(group='nviz',
                                                           key='arrow'))
            data['arrow']['color'] = "%d:%d:%d" % (
                UserSettings.Get(group='nviz', key='arrow',
                                 subkey='color')[:3])
            data['arrow'].update(
                copy.deepcopy(
                    UserSettings.Get(
                        group='nviz',
                        key='arrow',
                        settings_type='internal')))
            data['arrow']['show'] = False

        # arrow
        if type == 'scalebar':
            data['scalebar'] = copy.deepcopy(UserSettings.Get(group='nviz',
                                                              key='scalebar'))
            data['scalebar']['color'] = "%d:%d:%d" % (
                UserSettings.Get(group='nviz', key='scalebar',
                                 subkey='color')[:3])
            data['scalebar'].update(
                copy.deepcopy(
                    UserSettings.Get(
                        group='nviz',
                        key='scalebar',
                        settings_type='internal')))
            data['scalebar']['id'] = 0
        return data