Tools for GNS3 GUI
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
6.1KB

  1. From 7849e44ba8322cb579fa3e1a12eec890c521be36 Mon Sep 17 00:00:00 2001
  2. From: Bernhard Ehlers <none@b-ehlers.de>
  3. Date: Sun, 8 Mar 2020 10:16:53 +0100
  4. Subject: [PATCH 8/8] Tools - Enable tools to run when no project is open
  5. ---
  6. gns3/main_window.py | 5 ++++-
  7. gns3/tool.py | 41 ++++++++++++++++++++++++++---------------
  8. tools.md | 6 ++++--
  9. 3 files changed, 34 insertions(+), 18 deletions(-)
  10. diff --git a/gns3/main_window.py b/gns3/main_window.py
  11. index 46cecf59..1620dce6 100644
  12. --- a/gns3/main_window.py
  13. +++ b/gns3/main_window.py
  14. @@ -170,6 +170,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
  15. # add tools to tools menu
  16. tool_list = self.tools.menuTools()
  17. + self.disableWhenNoProjectTools = []
  18. if tool_list:
  19. self.uiToolsMenu.addSeparator()
  20. for tool in tool_list:
  21. @@ -177,6 +178,8 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
  22. action.setData(tool)
  23. action.triggered.connect(self.startToolSlot)
  24. self.uiToolsMenu.addAction(action)
  25. + if not tool.menu_always:
  26. + self.disableWhenNoProjectTools.append(action)
  27. # set the window icon
  28. self.setWindowIcon(QtGui.QIcon(":/images/gns3.ico"))
  29. @@ -204,7 +207,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
  30. self.uiDeleteProjectAction,
  31. self.uiImportExportConfigsAction,
  32. self.uiLockAllAction
  33. - ]
  34. + ] + self.disableWhenNoProjectTools
  35. # This widgets are not enabled if it's a remote controller (no access to the local file system)
  36. self.disableWhenRemoteContollerWidgets = [
  37. diff --git a/gns3/tool.py b/gns3/tool.py
  38. index 55dcd562..ea52e378 100644
  39. --- a/gns3/tool.py
  40. +++ b/gns3/tool.py
  41. @@ -56,6 +56,13 @@ else:
  42. return arg
  43. +class MenuState(Enum):
  44. + """ enum for menu option """
  45. + disable = 0
  46. + enable = 1
  47. + always = 2
  48. +
  49. +
  50. class ContextState(Enum):
  51. """ enum for context option """
  52. disable = 0
  53. @@ -78,13 +85,15 @@ class Tool:
  54. def __init__(self, name, path, options):
  55. self._name = name
  56. self._path = path
  57. - self.confirm_close = self._enum_opt(options, 'confirm_close',
  58. - ConfirmCloseState,
  59. - ConfirmCloseState.enable)
  60. + menu = self._enum_opt(options, 'menu', MenuState, MenuState.enable)
  61. + self.menu = menu != MenuState.disable
  62. + self.menu_always = menu == MenuState.always
  63. self.context = self._enum_opt(options, 'context',
  64. ContextState, ContextState.enable)
  65. - self.menu = bool(options.get('menu', True))
  66. self.terminal = bool(options.get('terminal', False))
  67. + self.confirm_close = self._enum_opt(options, 'confirm_close',
  68. + ConfirmCloseState,
  69. + ConfirmCloseState.enable)
  70. def _enum_opt(self, options, opt_name, enum_class, default):
  71. """ get enum option """
  72. @@ -197,21 +206,23 @@ end run
  73. def run(self, project, item_list):
  74. """ run tool """
  75. - if not project:
  76. + if not project and not self.menu_always:
  77. return
  78. param_file = self.conn_param_file()
  79. if not param_file:
  80. return
  81. - args = [self._path, __version__, param_file, project.id()]
  82. - for item in item_list:
  83. - if isinstance(item, NodeItem):
  84. - args.append("nodes/" + item.node().node_id())
  85. - elif isinstance(item, LinkItem):
  86. - args.append("links/" + item.link().link_id())
  87. - elif isinstance(item, TextItem):
  88. - args.append("text_drawings/" + item.drawing_id())
  89. - elif isinstance(item, DrawingItem):
  90. - args.append("drawings/" + item.drawing_id())
  91. + args = [self._path, __version__, param_file]
  92. + if project:
  93. + args.append(project.id())
  94. + for item in item_list:
  95. + if isinstance(item, NodeItem):
  96. + args.append("nodes/" + item.node().node_id())
  97. + elif isinstance(item, LinkItem):
  98. + args.append("links/" + item.link().link_id())
  99. + elif isinstance(item, TextItem):
  100. + args.append("text_drawings/" + item.drawing_id())
  101. + elif isinstance(item, DrawingItem):
  102. + args.append("drawings/" + item.drawing_id())
  103. try:
  104. if self.terminal:
  105. diff --git a/tools.md b/tools.md
  106. index c2ffcc2a..09cf83fb 100644
  107. --- a/tools.md
  108. +++ b/tools.md
  109. @@ -12,7 +12,7 @@ called by the GUI with the following parameters:
  110. |------|--------------------------------------------|
  111. | 1 | GNS3 version (for compatibility checks) |
  112. | 2 | Filename of connection setup parameters |
  113. -| 3 | Project UUID |
  114. +| 3 | Project UUID, if a project is open |
  115. | 4+ | List of selected items, can be empty |
  116. The file with the connection setup parameters contains
  117. @@ -33,11 +33,13 @@ It can set the following options:
  118. | Option | Meaning | Allowed Values | Default |
  119. |---------------|--------------------------|-----------------------------------|-----------|
  120. | name | name of tool | any string | base name |
  121. -| menu | show in main menu | false / true | true |
  122. +| menu | show in main menu | "disable" / "enable" / "always" | "enable" |
  123. | context | show in context menu | "disable" / "enable" / "node" | "enable" |
  124. | terminal | run in terminal window | false / true | false |
  125. | confirm_close | confirm closing terminal | "disable" / "enable" / "on_error" | "enable" |
  126. +The menu option "enable" activates a tool only if a
  127. +project is open, "always" will enable it in any case.
  128. With the context option "node" a tool is only shown in
  129. the context menu, when at least one node is selected.
  130. Instead of "disable" or "enable" the boolean values
  131. --
  132. 2.15.1 (Apple Git-101)