dialogs/UriEditor.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Fri, 12 Feb 2021 22:00:07 +0100
branchsvghmi
changeset 3136 784c839d4259
parent 2521 48ebcbe7f19b
child 3750 f62625418bff
permissions -rw-r--r--
SVGHMI: Add a robust ScrollBar widget. HMI:ScrollBar@positionrange@size
2182
eeca1aff0691 Fix linter errors
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2179
diff changeset
     1
from __future__ import absolute_import
eeca1aff0691 Fix linter errors
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2179
diff changeset
     2
2001
bcbd41efd846 #2476 Added uri location editor, getting connectors from list in Beremiz.
dporopat <denis.poropat@smarteh.si>
parents:
diff changeset
     3
import wx
2329
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2182
diff changeset
     4
from connectors import ConnectorSchemes, EditorClassFromScheme
2332
03a94f862465 'Merged' URI editor dialog and Discovery dialog, also clean up related code
Edouard Tisserant
parents: 2331
diff changeset
     5
from controls.DiscoveryPanel import DiscoveryPanel
2001
bcbd41efd846 #2476 Added uri location editor, getting connectors from list in Beremiz.
dporopat <denis.poropat@smarteh.si>
parents:
diff changeset
     6
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2475
diff changeset
     7
2330
8c18b1a3e2bf URI Editor Dialog : moved it to /dialog, and fixed naming.
Edouard Tisserant
parents: 2329
diff changeset
     8
class UriEditor(wx.Dialog):
2001
bcbd41efd846 #2476 Added uri location editor, getting connectors from list in Beremiz.
dporopat <denis.poropat@smarteh.si>
parents:
diff changeset
     9
    def _init_ctrls(self, parent):
2332
03a94f862465 'Merged' URI editor dialog and Discovery dialog, also clean up related code
Edouard Tisserant
parents: 2331
diff changeset
    10
        self.UriTypeChoice = wx.Choice(parent=self, choices=self.choices)
2001
bcbd41efd846 #2476 Added uri location editor, getting connectors from list in Beremiz.
dporopat <denis.poropat@smarteh.si>
parents:
diff changeset
    11
        self.UriTypeChoice.SetSelection(0)
bcbd41efd846 #2476 Added uri location editor, getting connectors from list in Beremiz.
dporopat <denis.poropat@smarteh.si>
parents:
diff changeset
    12
        self.Bind(wx.EVT_CHOICE, self.OnTypeChoice, self.UriTypeChoice)
2329
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2182
diff changeset
    13
        self.editor_sizer = wx.BoxSizer(wx.HORIZONTAL)
2182
eeca1aff0691 Fix linter errors
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2179
diff changeset
    14
        self.ButtonSizer = self.CreateButtonSizer(wx.OK | wx.CANCEL)
2001
bcbd41efd846 #2476 Added uri location editor, getting connectors from list in Beremiz.
dporopat <denis.poropat@smarteh.si>
parents:
diff changeset
    15
bcbd41efd846 #2476 Added uri location editor, getting connectors from list in Beremiz.
dporopat <denis.poropat@smarteh.si>
parents:
diff changeset
    16
    def _init_sizers(self):
bcbd41efd846 #2476 Added uri location editor, getting connectors from list in Beremiz.
dporopat <denis.poropat@smarteh.si>
parents:
diff changeset
    17
        self.mainSizer = wx.BoxSizer(wx.VERTICAL)
bcbd41efd846 #2476 Added uri location editor, getting connectors from list in Beremiz.
dporopat <denis.poropat@smarteh.si>
parents:
diff changeset
    18
        typeSizer = wx.BoxSizer(wx.HORIZONTAL)
2331
91ae5a11a462 Cut more useless code from URI Editor
Edouard Tisserant
parents: 2330
diff changeset
    19
        typeSizer.Add(wx.StaticText(self, wx.ID_ANY, _("Scheme :")), border=5,
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2475
diff changeset
    20
                      flag=wx.ALIGN_CENTER_VERTICAL | wx.ALL)
2001
bcbd41efd846 #2476 Added uri location editor, getting connectors from list in Beremiz.
dporopat <denis.poropat@smarteh.si>
parents:
diff changeset
    21
        typeSizer.Add(self.UriTypeChoice, border=5, flag=wx.ALL)
bcbd41efd846 #2476 Added uri location editor, getting connectors from list in Beremiz.
dporopat <denis.poropat@smarteh.si>
parents:
diff changeset
    22
        self.mainSizer.Add(typeSizer)
bcbd41efd846 #2476 Added uri location editor, getting connectors from list in Beremiz.
dporopat <denis.poropat@smarteh.si>
parents:
diff changeset
    23
2329
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2182
diff changeset
    24
        self.mainSizer.Add(self.editor_sizer, border=5, flag=wx.ALL)
2331
91ae5a11a462 Cut more useless code from URI Editor
Edouard Tisserant
parents: 2330
diff changeset
    25
        self.mainSizer.Add(self.ButtonSizer, border=5,
91ae5a11a462 Cut more useless code from URI Editor
Edouard Tisserant
parents: 2330
diff changeset
    26
                           flag=wx.BOTTOM | wx.ALIGN_CENTER_HORIZONTAL)
2001
bcbd41efd846 #2476 Added uri location editor, getting connectors from list in Beremiz.
dporopat <denis.poropat@smarteh.si>
parents:
diff changeset
    27
        self.SetSizer(self.mainSizer)
2176
748017ca4151 Autoresize UriLocationEditor dialog window
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2007
diff changeset
    28
        self.Layout()
748017ca4151 Autoresize UriLocationEditor dialog window
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2007
diff changeset
    29
        self.Fit()
2001
bcbd41efd846 #2476 Added uri location editor, getting connectors from list in Beremiz.
dporopat <denis.poropat@smarteh.si>
parents:
diff changeset
    30
2334
d1470c052662 Added early implementation of IDManager.py. For now only used to select ID in URIEditor
Edouard Tisserant
parents: 2332
diff changeset
    31
    def __init__(self, parent, ctr, uri=''):
d1470c052662 Added early implementation of IDManager.py. For now only used to select ID in URIEditor
Edouard Tisserant
parents: 2332
diff changeset
    32
        self.ctr = ctr
2331
91ae5a11a462 Cut more useless code from URI Editor
Edouard Tisserant
parents: 2330
diff changeset
    33
        wx.Dialog.__init__(self,
2330
8c18b1a3e2bf URI Editor Dialog : moved it to /dialog, and fixed naming.
Edouard Tisserant
parents: 2329
diff changeset
    34
                           name='UriEditor', parent=parent,
2329
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2182
diff changeset
    35
                           title=_('URI Editor'))
2332
03a94f862465 'Merged' URI editor dialog and Discovery dialog, also clean up related code
Edouard Tisserant
parents: 2331
diff changeset
    36
        self.choices = [_("- Search local network -")] + ConnectorSchemes()
2001
bcbd41efd846 #2476 Added uri location editor, getting connectors from list in Beremiz.
dporopat <denis.poropat@smarteh.si>
parents:
diff changeset
    37
        self._init_ctrls(parent)
bcbd41efd846 #2476 Added uri location editor, getting connectors from list in Beremiz.
dporopat <denis.poropat@smarteh.si>
parents:
diff changeset
    38
        self._init_sizers()
2329
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2182
diff changeset
    39
        self.scheme = None
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2182
diff changeset
    40
        self.scheme_editor = None
2001
bcbd41efd846 #2476 Added uri location editor, getting connectors from list in Beremiz.
dporopat <denis.poropat@smarteh.si>
parents:
diff changeset
    41
        self.SetURI(uri)
bcbd41efd846 #2476 Added uri location editor, getting connectors from list in Beremiz.
dporopat <denis.poropat@smarteh.si>
parents:
diff changeset
    42
        self.CenterOnParent()
bcbd41efd846 #2476 Added uri location editor, getting connectors from list in Beremiz.
dporopat <denis.poropat@smarteh.si>
parents:
diff changeset
    43
bcbd41efd846 #2476 Added uri location editor, getting connectors from list in Beremiz.
dporopat <denis.poropat@smarteh.si>
parents:
diff changeset
    44
    def OnTypeChoice(self, event):
bcbd41efd846 #2476 Added uri location editor, getting connectors from list in Beremiz.
dporopat <denis.poropat@smarteh.si>
parents:
diff changeset
    45
        index = event.GetSelection()
2329
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2182
diff changeset
    46
        self._replaceSchemeEditor(event.GetString() if index > 0 else None)
2001
bcbd41efd846 #2476 Added uri location editor, getting connectors from list in Beremiz.
dporopat <denis.poropat@smarteh.si>
parents:
diff changeset
    47
bcbd41efd846 #2476 Added uri location editor, getting connectors from list in Beremiz.
dporopat <denis.poropat@smarteh.si>
parents:
diff changeset
    48
    def SetURI(self, uri):
2329
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2182
diff changeset
    49
        try:
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2475
diff changeset
    50
            scheme, loc = uri.strip().split("://", 1)
2332
03a94f862465 'Merged' URI editor dialog and Discovery dialog, also clean up related code
Edouard Tisserant
parents: 2331
diff changeset
    51
            scheme = scheme.upper()
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2475
diff changeset
    52
        except Exception:
2332
03a94f862465 'Merged' URI editor dialog and Discovery dialog, also clean up related code
Edouard Tisserant
parents: 2331
diff changeset
    53
            scheme = None
03a94f862465 'Merged' URI editor dialog and Discovery dialog, also clean up related code
Edouard Tisserant
parents: 2331
diff changeset
    54
2329
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2182
diff changeset
    55
        if scheme in ConnectorSchemes():
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2182
diff changeset
    56
            self.UriTypeChoice.SetStringSelection(scheme)
2332
03a94f862465 'Merged' URI editor dialog and Discovery dialog, also clean up related code
Edouard Tisserant
parents: 2331
diff changeset
    57
        else:
03a94f862465 'Merged' URI editor dialog and Discovery dialog, also clean up related code
Edouard Tisserant
parents: 2331
diff changeset
    58
            self.UriTypeChoice.SetSelection(0)
2475
39ba6a125a97 Fix exception dialog with non recognized URI schemes
Edouard Tisserant
parents: 2334
diff changeset
    59
            scheme = None
2332
03a94f862465 'Merged' URI editor dialog and Discovery dialog, also clean up related code
Edouard Tisserant
parents: 2331
diff changeset
    60
03a94f862465 'Merged' URI editor dialog and Discovery dialog, also clean up related code
Edouard Tisserant
parents: 2331
diff changeset
    61
        self._replaceSchemeEditor(scheme)
03a94f862465 'Merged' URI editor dialog and Discovery dialog, also clean up related code
Edouard Tisserant
parents: 2331
diff changeset
    62
03a94f862465 'Merged' URI editor dialog and Discovery dialog, also clean up related code
Edouard Tisserant
parents: 2331
diff changeset
    63
        if scheme is not None:
2329
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2182
diff changeset
    64
            self.scheme_editor.SetLoc(loc)
2001
bcbd41efd846 #2476 Added uri location editor, getting connectors from list in Beremiz.
dporopat <denis.poropat@smarteh.si>
parents:
diff changeset
    65
bcbd41efd846 #2476 Added uri location editor, getting connectors from list in Beremiz.
dporopat <denis.poropat@smarteh.si>
parents:
diff changeset
    66
    def GetURI(self):
2332
03a94f862465 'Merged' URI editor dialog and Discovery dialog, also clean up related code
Edouard Tisserant
parents: 2331
diff changeset
    67
        if self.scheme is None:
03a94f862465 'Merged' URI editor dialog and Discovery dialog, also clean up related code
Edouard Tisserant
parents: 2331
diff changeset
    68
            return self.scheme_editor.GetURI()
2001
bcbd41efd846 #2476 Added uri location editor, getting connectors from list in Beremiz.
dporopat <denis.poropat@smarteh.si>
parents:
diff changeset
    69
        else:
2329
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2182
diff changeset
    70
            return self.scheme+"://"+self.scheme_editor.GetLoc()
2001
bcbd41efd846 #2476 Added uri location editor, getting connectors from list in Beremiz.
dporopat <denis.poropat@smarteh.si>
parents:
diff changeset
    71
2329
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2182
diff changeset
    72
    def _replaceSchemeEditor(self, scheme):
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2182
diff changeset
    73
        self.scheme = scheme
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2475
diff changeset
    74
2329
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2182
diff changeset
    75
        if self.scheme_editor is not None:
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2182
diff changeset
    76
            self.editor_sizer.Detach(self.scheme_editor)
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2182
diff changeset
    77
            self.scheme_editor.Destroy()
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2182
diff changeset
    78
            self.scheme_editor = None
2001
bcbd41efd846 #2476 Added uri location editor, getting connectors from list in Beremiz.
dporopat <denis.poropat@smarteh.si>
parents:
diff changeset
    79
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2475
diff changeset
    80
        if scheme is not None:
2332
03a94f862465 'Merged' URI editor dialog and Discovery dialog, also clean up related code
Edouard Tisserant
parents: 2331
diff changeset
    81
            EditorClass = EditorClassFromScheme(scheme)
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2475
diff changeset
    82
            self.scheme_editor = EditorClass(scheme, self)
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2475
diff changeset
    83
        else:
2332
03a94f862465 'Merged' URI editor dialog and Discovery dialog, also clean up related code
Edouard Tisserant
parents: 2331
diff changeset
    84
            # None is for searching local network
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2475
diff changeset
    85
            self.scheme_editor = DiscoveryPanel(self)
2332
03a94f862465 'Merged' URI editor dialog and Discovery dialog, also clean up related code
Edouard Tisserant
parents: 2331
diff changeset
    86
03a94f862465 'Merged' URI editor dialog and Discovery dialog, also clean up related code
Edouard Tisserant
parents: 2331
diff changeset
    87
        self.editor_sizer.Add(self.scheme_editor)
03a94f862465 'Merged' URI editor dialog and Discovery dialog, also clean up related code
Edouard Tisserant
parents: 2331
diff changeset
    88
        self.scheme_editor.Refresh()
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2475
diff changeset
    89
2329
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2182
diff changeset
    90
        self.editor_sizer.Layout()
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2182
diff changeset
    91
        self.mainSizer.Layout()
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2182
diff changeset
    92
        self.Fit()