Custom patches for GNS3 network topology simulator & planning software.
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.

266 lines
10 KiB

2 years ago
  1. Author: Pekka Helenius <pekka.helenius@fjordtek.com>
  2. Date: Sun, 6 Jun 2021 15:15:14 +0200
  3. Subject: [PATCH] GNS3 GUI: Link colors.
  4. Add link color support for GNS3 GUI.
  5. --- a/gns3/items/serial_link_item.py
  6. +++ b/gns3/items/serial_link_item.py
  7. @@ -50,10 +50,16 @@
  8. LinkItem.adjust(self)
  9. - if self._hovered:
  10. - self.setPen(QtGui.QPen(QtCore.Qt.red, self._pen_width + 1, QtCore.Qt.SolidLine, QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin))
  11. - else:
  12. - self.setPen(QtGui.QPen(QtCore.Qt.darkRed, self._pen_width, QtCore.Qt.SolidLine, QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin))
  13. + try:
  14. + if self._hovered:
  15. + self.setPen(QtGui.QPen(QtCore.Qt.red, self._link._link_style["width"] + 1, QtCore.Qt.SolidLine, QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin))
  16. + else:
  17. + self.setPen(QtGui.QPen(QtGui.QColor(self._link._link_style["color"]), self._link._link_style["width"], self._link._link_style["type"], QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin))
  18. + except:
  19. + if self._hovered:
  20. + self.setPen(QtGui.QPen(QtCore.Qt.red, self._pen_width + 1, QtCore.Qt.SolidLine, QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin))
  21. + else:
  22. + self.setPen(QtGui.QPen(QtCore.Qt.darkRed, self._pen_width, QtCore.Qt.SolidLine, QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin))
  23. # get source to destination angle
  24. vector_angle = math.atan2(self.dy, self.dx)
  25. --- a/gns3/items/link_item.py
  26. +++ b/gns3/items/link_item.py
  27. @@ -25,6 +25,7 @@
  28. from ..packet_capture import PacketCapture
  29. from ..dialogs.filter_dialog import FilterDialog
  30. +from ..dialogs.style_editor_dialog_link import StyleEditorDialogLink
  31. from ..utils.get_icon import get_icon
  32. @@ -40,7 +41,6 @@
  33. self.parentItem().mousePressEvent(event)
  34. event.accept()
  35. -
  36. class LinkItem(QtWidgets.QGraphicsPathItem):
  37. """
  38. @@ -137,6 +137,21 @@
  39. def _suspendActionSlot(self, *args):
  40. self._link.toggleSuspend()
  41. + @qslot
  42. + def _styleActionSlot(self, *args):
  43. + style_dialog = StyleEditorDialogLink(self, self._main_window)
  44. + style_dialog.show()
  45. + style_dialog.exec_()
  46. +
  47. + def setLinkStyle(self, link_style):
  48. + self._link._link_style["color"] = link_style["color"]
  49. + self._link._link_style["width"] = link_style["width"]
  50. + self._link._link_style["type"] = link_style["type"]
  51. +
  52. + # This refers to functions in link.py!
  53. + self._link.setLinkStyle(link_style)
  54. + self._link.update()
  55. +
  56. def delete(self):
  57. """
  58. Delete this link
  59. @@ -266,6 +281,12 @@
  60. resume_action.triggered.connect(self._suspendActionSlot)
  61. menu.addAction(resume_action)
  62. + # style
  63. + style_action = QtWidgets.QAction("Style", menu)
  64. + style_action.setIcon(get_icon("node_conception.svg"))
  65. + style_action.triggered.connect(self._styleActionSlot)
  66. + menu.addAction(style_action)
  67. +
  68. # delete
  69. delete_action = QtWidgets.QAction("Delete", menu)
  70. delete_action.setIcon(get_icon('delete.svg'))
  71. --- a/gns3/items/ethernet_link_item.py
  72. +++ b/gns3/items/ethernet_link_item.py
  73. @@ -1,5 +1,6 @@
  74. # -*- coding: utf-8 -*-
  75. #
  76. +# Copyright (C) 2019 Pekka Helenius
  77. # Copyright (C) 2014 GNS3 Technologies Inc.
  78. #
  79. # This program is free software: you can redistribute it and/or modify
  80. @@ -51,10 +52,16 @@
  81. LinkItem.adjust(self)
  82. - if self._hovered:
  83. - self.setPen(QtGui.QPen(QtCore.Qt.red, self._pen_width + 1, QtCore.Qt.SolidLine, QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin))
  84. - else:
  85. - self.setPen(QtGui.QPen(QtCore.Qt.black, self._pen_width, QtCore.Qt.SolidLine, QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin))
  86. + try:
  87. + if self._hovered:
  88. + self.setPen(QtGui.QPen(QtCore.Qt.red, self._link._link_style["width"] + 1, QtCore.Qt.SolidLine, QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin))
  89. + else:
  90. + self.setPen(QtGui.QPen(QtGui.QColor(self._link._link_style["color"]), self._link._link_style["width"], self._link._link_style["type"], QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin))
  91. + except:
  92. + if self._hovered:
  93. + self.setPen(QtGui.QPen(QtCore.Qt.red, self._pen_width + 1, QtCore.Qt.SolidLine, QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin))
  94. + else:
  95. + self.setPen(QtGui.QPen(QtGui.QColor("#000000"), self._pen_width, QtCore.Qt.SolidLine, QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin))
  96. # draw a line between nodes
  97. path = QtGui.QPainterPath(self.source)
  98. --- /dev/null 2019-05-22 14:27:07.219999993 +0300
  99. +++ b/gns3/dialogs/style_editor_dialog_link.py
  100. @@ -0,0 +1,112 @@
  101. +# -*- coding: utf-8 -*-
  102. +#
  103. +# Copyright (C) 2019 Pekka Helenius
  104. +# Copyright (C) 2014 GNS3 Technologies Inc.
  105. +#
  106. +# This program is free software: you can redistribute it and/or modify
  107. +# it under the terms of the GNU General Public License as published by
  108. +# the Free Software Foundation, either version 3 of the License, or
  109. +# (at your option) any later version.
  110. +#
  111. +# This program is distributed in the hope that it will be useful,
  112. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  113. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  114. +# GNU General Public License for more details.
  115. +#
  116. +# You should have received a copy of the GNU General Public License
  117. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  118. +
  119. +"""
  120. +Style editor to edit Link items.
  121. +"""
  122. +
  123. +from ..qt import QtCore, QtWidgets, QtGui
  124. +from ..ui.style_editor_dialog_ui import Ui_StyleEditorDialog
  125. +
  126. +
  127. +class StyleEditorDialogLink(QtWidgets.QDialog, Ui_StyleEditorDialog):
  128. +
  129. + """
  130. + Style editor dialog.
  131. +
  132. + :param parent: parent widget
  133. + :param link: selected link
  134. + """
  135. +
  136. + def __init__(self, link, parent):
  137. +
  138. + super().__init__(parent)
  139. + self.setupUi(self)
  140. +
  141. + self._link = link
  142. + self._link_style = {}
  143. +
  144. + self.uiBorderColorPushButton.clicked.connect(self._setBorderColorSlot)
  145. + self.uiButtonBox.button(QtWidgets.QDialogButtonBox.Apply).clicked.connect(self._applyPreferencesSlot)
  146. +
  147. + self.uiBorderStyleComboBox.addItem("Solid", QtCore.Qt.SolidLine)
  148. + self.uiBorderStyleComboBox.addItem("Dash", QtCore.Qt.DashLine)
  149. + self.uiBorderStyleComboBox.addItem("Dot", QtCore.Qt.DotLine)
  150. + self.uiBorderStyleComboBox.addItem("Dash Dot", QtCore.Qt.DashDotLine)
  151. + self.uiBorderStyleComboBox.addItem("Dash Dot Dot", QtCore.Qt.DashDotDotLine)
  152. +
  153. + self.uiColorLabel.hide()
  154. + self.uiColorPushButton.hide()
  155. + self._color = None
  156. +
  157. + self.uiRotationLabel.hide()
  158. + self.uiRotationSpinBox.hide()
  159. +
  160. + pen = link.pen()
  161. +
  162. + self._border_color = pen.color()
  163. + self.uiBorderColorPushButton.setStyleSheet("background-color: rgba({}, {}, {}, {});".format(self._border_color.red(),
  164. + self._border_color.green(),
  165. + self._border_color.blue(),
  166. + self._border_color.alpha()))
  167. + self.uiBorderWidthSpinBox.setValue(pen.width())
  168. + index = self.uiBorderStyleComboBox.findData(pen.style())
  169. + if index != -1:
  170. + self.uiBorderStyleComboBox.setCurrentIndex(index)
  171. +
  172. + def _setBorderColorSlot(self):
  173. + """
  174. + Slot to select the border color.
  175. + """
  176. +
  177. + color = QtWidgets.QColorDialog.getColor(self._border_color, self, "Select Color", QtWidgets.QColorDialog.ShowAlphaChannel)
  178. + if color.isValid():
  179. + self._border_color = color
  180. + self.uiBorderColorPushButton.setStyleSheet("background-color: rgba({}, {}, {}, {});".format(self._border_color.red(),
  181. + self._border_color.green(),
  182. + self._border_color.blue(),
  183. + self._border_color.alpha()))
  184. +
  185. + def _applyPreferencesSlot(self):
  186. + """
  187. + Applies the new style settings.
  188. + """
  189. +
  190. + border_style = QtCore.Qt.PenStyle(self.uiBorderStyleComboBox.itemData(self.uiBorderStyleComboBox.currentIndex()))
  191. + pen = QtGui.QPen(self._border_color, self.uiBorderWidthSpinBox.value(), border_style, QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin)
  192. +
  193. + self._link.setPen(pen)
  194. +
  195. + new_link_style = {}
  196. + new_link_style["color"] = self._border_color.name()
  197. + new_link_style["width"] = self.uiBorderWidthSpinBox.value()
  198. + new_link_style["type"] = border_style
  199. +
  200. + # Store values
  201. + self._link.setLinkStyle(new_link_style)
  202. +
  203. + def done(self, result):
  204. + """
  205. + Called when the dialog is closed.
  206. +
  207. + :param result: boolean (accepted or rejected)
  208. + """
  209. +
  210. + if result:
  211. + self._applyPreferencesSlot()
  212. + super().done(result)
  213. --- a/gns3/link.py
  214. +++ b/gns3/link.py
  215. @@ -90,6 +90,8 @@
  216. self._nodes = []
  217. + self._link_style = {}
  218. +
  219. self._source_node.addLink(self)
  220. self._destination_node.addLink(self)
  221. @@ -127,6 +129,8 @@
  222. self._updateLabels()
  223. if "filters" in result:
  224. self._filters = result["filters"]
  225. + if "link_style" in result:
  226. + self._link_style = result["link_style"]
  227. if "suspend" in result:
  228. self._suspend = result["suspend"]
  229. self.updated_link_signal.emit(self._id)
  230. @@ -209,6 +213,7 @@
  231. }
  232. ],
  233. "filters": self._filters,
  234. + "link_style": self._link_style,
  235. "suspend": self._suspend
  236. }
  237. if self._source_port.label():
  238. @@ -462,3 +467,9 @@
  239. :params filters: List of filters
  240. """
  241. self._filters = filters
  242. +
  243. + def setLinkStyle(self, link_style):
  244. + """
  245. + :params _link_style: Set link style attributes
  246. + """
  247. + self._link_style = link_style