File: workspace.py

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (573 lines) | stat: -rw-r--r-- 20,344 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
"""
@package lmgr::workspace

@brief Workspace manager class for creating, loading and saving workspaces

Class:
 - lmgr::WorkspaceManager

(C) 2021 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.
"""

import os
import tempfile

import xml.etree.ElementTree as etree

import wx
import wx.aui

from core.settings import UserSettings
from core.gcmd import RunCommand, GError, GMessage
from core.workspace import ProcessWorkspaceFile, WriteWorkspaceFile
from core.debug import Debug
from gui_core.menu import RecentFilesMenu


class WorkspaceManager:
    """Workspace Manager for creating, loading and saving workspaces."""

    def __init__(self, lmgr, giface):
        self.lmgr = lmgr
        self.workspaceFile = None
        self._giface = giface
        self.workspaceChanged = False  # track changes in workspace
        self.loadingWorkspace = False
        self._recent_files = None

        Debug.msg(1, "WorkspaceManager.__init__()")

        self._giface.workspaceChanged.connect(self.WorkspaceChanged)

    def WorkspaceChanged(self):
        "Update window title"
        self.workspaceChanged = True

    def New(self):
        """Create new workspace file
        Erase current workspace settings first
        """
        Debug.msg(4, "WorkspaceManager.New():")

        # start new map display if no display is available
        if not self.lmgr.currentPage:
            self.lmgr.NewDisplay()

        maptrees = [
            self.lmgr.notebookLayers.GetPage(i).maptree
            for i in range(self.lmgr.notebookLayers.GetPageCount())
        ]

        # ask user to save current settings
        if self.workspaceFile and self.workspaceChanged:
            self.Save()
        elif self.workspaceFile is None and any(tree.GetCount() for tree in maptrees):
            dlg = wx.MessageDialog(
                self.lmgr,
                message=_(
                    "Current workspace is not empty. "
                    "Do you want to store current settings "
                    "to workspace file?"
                ),
                caption=_("Create new workspace?"),
                style=wx.YES_NO | wx.YES_DEFAULT | wx.CANCEL | wx.ICON_QUESTION,
            )
            ret = dlg.ShowModal()
            if ret == wx.ID_YES:
                self.SaveAs()
            elif ret == wx.ID_CANCEL:
                dlg.Destroy()
                return

            dlg.Destroy()

        # delete all layers in map displays
        for maptree in maptrees:
            maptree.DeleteAllLayers()

        # delete all decorations
        for display in self.lmgr.GetAllMapDisplays():
            for overlayId in list(display.decorations):
                display.RemoveOverlay(overlayId)

        self.workspaceFile = None
        self.workspaceChanged = False
        self.lmgr._setTitle()

    def Open(self):
        """Open file with workspace definition"""
        dlg = wx.FileDialog(
            parent=self.lmgr,
            message=_("Choose workspace file"),
            defaultDir=os.getcwd(),
            wildcard=_("GRASS Workspace File (*.gxw)|*.gxw"),
        )

        filename = ""
        if dlg.ShowModal() == wx.ID_OK:
            filename = dlg.GetPath()

        if filename == "":
            return

        Debug.msg(4, "WorkspaceManager.Open(): filename=%s" % filename)

        # delete current layer tree content
        self.Close()
        self.loadingWorkspace = True
        self.Load(filename)
        self.loadingWorkspace = False
        self.lmgr._setTitle()

    def _tryToSwitchMapsetFromWorkspaceFile(self, gxwXml):
        returncode, errors = RunCommand(
            "g.mapset",
            dbase=gxwXml.database,
            project=gxwXml.location,
            mapset=gxwXml.mapset,
            getErrorMsg=True,
        )
        if returncode != 0:
            # TODO: use the function from grass.py
            reason = _("Most likely the database, location or mapset" " does not exist")
            details = errors
            message = _(
                "Unable to change to location and mapset"
                " specified in the workspace.\n"
                "Reason: {reason}\nDetails: {details}\n\n"
                "Do you want to proceed with opening"
                " the workspace anyway?"
            ).format(**locals())
            dlg = wx.MessageDialog(
                parent=self.lmgr,
                message=message,
                caption=_("Proceed with opening of the workspace?"),
                style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION,
            )
            dlg.CenterOnParent()
            if dlg.ShowModal() in [wx.ID_NO, wx.ID_CANCEL]:
                return False
        else:
            # TODO: copy from ChangeLocation function
            GMessage(
                parent=self.lmgr,
                message=_(
                    "Current location is <%(loc)s>.\n" "Current mapset is <%(mapset)s>."
                )
                % {"loc": gxwXml.location, "mapset": gxwXml.mapset},
            )
        return True

    def Load(self, filename):
        """Load layer tree definition stored in GRASS Workspace XML file (gxw)
        .. todo::
            Validate against DTD
        :return: True on success
        :return: False on error
        """
        # parse workspace file
        try:
            gxwXml = ProcessWorkspaceFile(etree.parse(filename))
        except Exception as e:
            GError(
                parent=self.lmgr,
                message=_(
                    "Reading workspace file <%s> failed.\n"
                    "Invalid file, unable to parse XML document."
                )
                % filename,
            )
            return False

        if gxwXml.database and gxwXml.location and gxwXml.mapset:
            if not self._tryToSwitchMapsetFromWorkspaceFile(gxwXml):
                return False

        # the really busy part starts here (mapset change is fast)
        busy = wx.BusyInfo(_("Please wait, loading workspace..."), parent=self.lmgr)
        wx.GetApp().Yield()

        #
        # load layer manager window properties
        #
        if (
            UserSettings.Get(
                group="general", key="workspace", subkey=["posManager", "enabled"]
            )
            is False
        ):
            if gxwXml.layerManager["pos"]:
                self.lmgr.SetPosition(gxwXml.layerManager["pos"])
            if gxwXml.layerManager["size"]:
                self.lmgr.SetSize(gxwXml.layerManager["size"])
            if gxwXml.layerManager["cwd"]:
                self.lmgr.cwdPath = gxwXml.layerManager["cwd"]
                if os.path.isdir(self.lmgr.cwdPath):
                    os.chdir(self.lmgr.cwdPath)

        #
        # start map displays first (list of layers can be empty)
        #
        displayId = 0
        mapdisplay = list()
        for display in gxwXml.displays:
            mapdisp = self.lmgr.NewDisplay(name=display["name"], show=False)
            mapdisplay.append(mapdisp)
            maptree = self.lmgr.notebookLayers.GetPage(displayId).maptree

            # set windows properties
            mapdisp.SetProperties(
                render=display["render"],
                mode=display["mode"],
                showCompExtent=display["showCompExtent"],
                alignExtent=display["alignExtent"],
                constrainRes=display["constrainRes"],
                projection=display["projection"]["enabled"],
            )

            if display["projection"]["enabled"]:
                if display["projection"]["epsg"]:
                    UserSettings.Set(
                        group="display",
                        key="projection",
                        subkey="epsg",
                        value=display["projection"]["epsg"],
                    )
                    if display["projection"]["proj"]:
                        UserSettings.Set(
                            group="display",
                            key="projection",
                            subkey="proj4",
                            value=display["projection"]["proj"],
                        )

            # set position and size of map display
            if not UserSettings.Get(
                group="general", key="workspace", subkey=["posDisplay", "enabled"]
            ):
                if display["pos"]:
                    mapdisp.SetPosition(display["pos"])
                if display["size"]:
                    mapdisp.SetSize(display["size"])

            # set extent if defined
            if display["extent"]:
                w, s, e, n, b, t = display["extent"]
                region = maptree.Map.region = maptree.Map.GetRegion(w=w, s=s, e=e, n=n)
                mapdisp.GetWindow().ResetZoomHistory()
                mapdisp.GetWindow().ZoomHistory(
                    region["n"], region["s"], region["e"], region["w"]
                )
            if "showStatusbar" in display and not display["showStatusbar"]:
                mapdisp.ShowStatusbar(False)
            if "showToolbars" in display and not display["showToolbars"]:
                for toolbar in mapdisp.GetToolbarNames():
                    mapdisp.RemoveToolbar(toolbar)
            if "isDocked" in display and not display["isDocked"]:
                mapdisp.OnDockUndock()

            displayId += 1
            mapdisp.Show()  # show mapdisplay
            # set render property to False to speed up loading layers
            mapdisp.mapWindowProperties.autoRender = False

        maptree = None
        selectList = []  # list of selected layers
        #
        # load list of map layers
        #
        for layer in gxwXml.layers:
            display = layer["display"]
            maptree = self.lmgr.notebookLayers.GetPage(display).maptree
            newItem = maptree.AddLayer(
                ltype=layer["type"],
                lname=layer["name"],
                lchecked=layer["checked"],
                lopacity=layer["opacity"],
                lcmd=layer["cmd"],
                lgroup=layer["group"],
                lnviz=layer["nviz"],
                lvdigit=layer["vdigit"],
                loadWorkspace=True,
            )

            if "selected" in layer:
                selectList.append((maptree, newItem, layer["selected"]))

        for maptree, layer, selected in selectList:
            if selected:
                if not layer.IsSelected():
                    maptree.SelectItem(layer, select=True)
            else:
                maptree.SelectItem(layer, select=False)

        del busy

        # set render property again when all layers are loaded
        for i, display in enumerate(gxwXml.displays):
            mapdisplay[i].mapWindowProperties.autoRender = display["render"]

            for overlay in gxwXml.overlays:
                # overlay["cmd"][0] name of command e.g. d.barscale, d.legend
                # overlay["cmd"][1:] parameters and flags
                if overlay["display"] == i:
                    if overlay["cmd"][0] == "d.legend.vect":
                        mapdisplay[i].AddLegendVect(overlay["cmd"])
                    if overlay["cmd"][0] == "d.legend":
                        mapdisplay[i].AddLegendRast(overlay["cmd"])
                    if overlay["cmd"][0] == "d.barscale":
                        mapdisplay[i].AddBarscale(overlay["cmd"])
                    if overlay["cmd"][0] == "d.northarrow":
                        mapdisplay[i].AddArrow(overlay["cmd"])
                    if overlay["cmd"][0] == "d.text":
                        mapdisplay[i].AddDtext(overlay["cmd"])

            # avoid double-rendering when loading workspace
            # mdisp.MapWindow2D.UpdateMap()
            # nviz
            if gxwXml.displays[i]["viewMode"] == "3d":
                mapdisplay[i].AddNviz()
                self.lmgr.nvizUpdateState(
                    view=gxwXml.nviz_state["view"],
                    iview=gxwXml.nviz_state["iview"],
                    light=gxwXml.nviz_state["light"],
                )
                mapdisplay[i].MapWindow3D.constants = gxwXml.nviz_state["constants"]
                for idx, constant in enumerate(mapdisplay[i].MapWindow3D.constants):
                    mapdisplay[i].MapWindow3D.AddConstant(constant, i + 1)
                for page in ("view", "light", "fringe", "constant", "cplane"):
                    self.lmgr.nvizUpdatePage(page)
                self.lmgr.nvizUpdateSettings()
                mapdisplay[i].toolbars["map"].combo.SetSelection(1)

        #
        # load layout
        #
        if UserSettings.Get(group="appearance", key="singleWindow", subkey="enabled"):
            if gxwXml.layout["panes"]:
                self.lmgr.GetAuiManager().LoadPerspective(gxwXml.layout["panes"])
            if gxwXml.layout["notebook"]:
                self.lmgr.GetAuiNotebook().LoadPerspective(gxwXml.layout["notebook"])

        self.workspaceFile = filename
        self.AddFileToHistory()
        return True

    def SaveAs(self):
        """Save workspace definition to selected file"""
        dlg = wx.FileDialog(
            parent=self.lmgr,
            message=_("Choose file to save current workspace"),
            defaultDir=os.getcwd(),
            wildcard=_("GRASS Workspace File (*.gxw)|*.gxw"),
            style=wx.FD_SAVE,
        )

        filename = ""
        if dlg.ShowModal() == wx.ID_OK:
            filename = dlg.GetPath()

        if filename == "":
            return False

        # check for extension
        if filename[-4:] != ".gxw":
            filename += ".gxw"

        if os.path.exists(filename):
            dlg = wx.MessageDialog(
                self.lmgr,
                message=_(
                    "Workspace file <%s> already exists. "
                    "Do you want to overwrite this file?"
                )
                % filename,
                caption=_("Save workspace"),
                style=wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION,
            )
            if dlg.ShowModal() != wx.ID_YES:
                dlg.Destroy()
                return False

        Debug.msg(4, "WorkspaceManager.SaveAs(): filename=%s" % filename)

        self.SaveToFile(filename)
        self.workspaceFile = filename
        self.lmgr._setTitle()

    def Save(self):
        """Save file with workspace definition"""
        if self.workspaceFile:
            dlg = wx.MessageDialog(
                self.lmgr,
                message=_(
                    "Workspace file <%s> already exists. "
                    "Do you want to overwrite this file?"
                )
                % self.workspaceFile,
                caption=_("Save workspace"),
                style=wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION,
            )
            if dlg.ShowModal() == wx.ID_NO:
                dlg.Destroy()
            else:
                Debug.msg(
                    4, "WorkspaceManager.Save(): filename=%s" % self.workspaceFile
                )
                self.SaveToFile(self.workspaceFile)
                self.lmgr._setTitle()
                self.workspaceChanged = False
        else:
            self.SaveAs()

    def SaveToFile(self, filename):
        """Save layer tree layout to workspace file
        :return: True on success, False on error
        """
        tmpfile = tempfile.TemporaryFile(mode="w+b")
        try:
            WriteWorkspaceFile(lmgr=self.lmgr, file=tmpfile)
        except Exception as e:
            GError(
                parent=self.lmgr,
                message=_("Writing current settings to workspace file " "failed."),
            )
            return False

        try:
            mfile = open(filename, "wb")
            tmpfile.seek(0)
            for line in tmpfile.readlines():
                mfile.write(line)
        except OSError:
            GError(
                parent=self.lmgr,
                message=_("Unable to open file <%s> for writing.") % filename,
            )
            return False

        mfile.close()

        self.AddFileToHistory(file_path=filename)

        return True

    def CanClosePage(self, caption):
        """Ask if page with map display(s) can be closed"""
        # save changes in the workspace
        maptree = self._giface.GetLayerTree()
        if self.workspaceChanged and UserSettings.Get(
            group="manager", key="askOnQuit", subkey="enabled"
        ):
            if self.workspaceFile:
                message = _("Do you want to save changes in the workspace?")
            else:
                message = _(
                    "Do you want to store current settings " "to workspace file?"
                )

            # ask user to save current settings
            if maptree.GetCount() > 0:
                dlg = wx.MessageDialog(
                    self.lmgr,
                    message=message,
                    caption=caption,
                    style=wx.YES_NO
                    | wx.YES_DEFAULT
                    | wx.CANCEL
                    | wx.ICON_QUESTION
                    | wx.CENTRE,
                )
                ret = dlg.ShowModal()
                dlg.Destroy()
                if ret == wx.ID_YES:
                    if not self.workspaceFile:
                        self.SaveAs()
                    else:
                        self.SaveToFile(self.workspaceFile)
                elif ret == wx.ID_CANCEL:
                    return False
        return True

    def Close(self):
        """Close file with workspace definition
        If workspace has been modified ask user to save the changes.
        """
        Debug.msg(4, "WorkspaceManager.Close(): file=%s" % self.workspaceFile)

        self.lmgr.DisplayCloseAll()
        self.workspaceFile = None
        self.workspaceChanged = False
        self.lmgr._setTitle()
        self.lmgr.displayIndex = 0
        self.lmgr.currentPage = None

    def CreateRecentFilesMenu(self, menu=None):
        """Create workspace recent files menu

        :param gui_core.menu.Menu menu: menu default arg is None

        :return None
        """
        if menu:
            menu_index = menu.FindMenu(_("File"))
            if menu_index == wx.NOT_FOUND:
                # try untranslated version
                menu_index = menu.FindMenu("File")
                if menu_index == wx.NOT_FOUND:
                    return
            file_menu = menu.GetMenu(menu_index)
            workspace_index = file_menu.FindItem(_("Workspace"))
            if workspace_index == wx.NOT_FOUND:
                workspace_index = file_menu.FindItem("Workspace")
                if workspace_index == wx.NOT_FOUND:
                    return
            workspace_item = file_menu.FindItemById(workspace_index)

            self._recent_files = RecentFilesMenu(
                app_name="main",
                parent_menu=workspace_item.GetSubMenu(),
                pos=0,
            )
            self._recent_files.file_requested.connect(self.OpenRecentFile)

    def AddFileToHistory(self, file_path=None):
        """Add file to history (recent files)

        :param str file_path: file path wit default arg None

        :return None
        """
        if not file_path:
            file_path = self.workspaceFile
        if self._recent_files:
            self._recent_files.AddFileToHistory(filename=file_path)

    def OpenRecentFile(self, path, file_exists, file_history):
        """Try open recent file and read content

        :param str path: file path
        :param bool file_exists: file path exists
        :param bool file_history: file history obj instance

        :return: None
        """
        if not file_exists:
            GError(
                _(
                    "File <{}> doesn't exist."
                    " It was probably moved or deleted.".format(path)
                ),
                parent=self.lmgr,
            )
        else:
            self.Close()
            self.loadingWorkspace = True
            self.Load(path)
            self.loadingWorkspace = False
            self.lmgr._setTitle()
            file_history.AddFileToHistory(filename=path)  # move up the list