Skip to content

Commit c6fd1bc

Browse files
committed
Add utility function for loading images
1 parent c286c94 commit c6fd1bc

File tree

3 files changed

+53
-36
lines changed

3 files changed

+53
-36
lines changed

mal_gui/MainWindow.py

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222

2323
from qt_material import apply_stylesheet,list_themes
2424

25+
from maltoolbox.language import LanguageGraph, LanguageClassesFactory
26+
from maltoolbox.model import Model
27+
2528
from .ModelScene import ModelScene
2629
from .ModelView import ModelView
2730
from .ObjectExplorer.AssetBase import AssetBase
@@ -35,8 +38,7 @@
3538
from .DockedWindows.AttackStepsWindow import AttackStepsWindow
3639
from .DockedWindows.AssetRelationsWindow import AssetRelationsWindow
3740

38-
from maltoolbox.language import LanguageGraph, LanguageClassesFactory
39-
from maltoolbox.model import Model
41+
from .file_utils import image_path
4042

4143
# Used to create absolute paths of assets
4244
PACKAGE_DIR = Path(__file__).resolve().parent
@@ -64,32 +66,32 @@ def __init__(self,app,malLanguageMarFilePath):
6466
self.modelFileName = None
6567

6668
assetImages = {
67-
"Application": "images/application.png",
68-
"Credentials": "images/credentials.png",
69-
"Data": "images/datastore.png",
70-
"Group": "images/group.png",
71-
"Hardware": "images/hardware.png",
72-
"HardwareVulnerability": "images/hardwareVulnerability.png",
73-
"IDPS": "images/idps.png",
74-
"Identity": "images/identity.png",
75-
"Privileges": "images/privileges.png",
76-
"Information": "images/information.png",
77-
"Network": "images/network.png",
78-
"ConnectionRule": "images/connectionRule.png",
79-
"PhysicalZone": "images/physicalZone.png",
80-
"RoutingFirewall": "images/routingFirewall.png",
81-
"SoftwareProduct": "images/softwareProduct.png",
82-
"SoftwareVulnerability": "images/softwareVulnerability.png",
83-
"User": "images/user.png"
69+
"Application": image_path("application.png"),
70+
"Credentials": image_path("credentials.png"),
71+
"Data": image_path("datastore.png"),
72+
"Group": image_path("group.png"),
73+
"Hardware": image_path("hardware.png"),
74+
"HardwareVulnerability": image_path("hardwareVulnerability.png"),
75+
"IDPS": image_path("idps.png"),
76+
"Identity": image_path("identity.png"),
77+
"Privileges": image_path("privileges.png"),
78+
"Information": image_path("information.png"),
79+
"Network": image_path("network.png"),
80+
"ConnectionRule": image_path("connectionRule.png"),
81+
"PhysicalZone": image_path("physicalZone.png"),
82+
"RoutingFirewall": image_path("routingFirewall.png"),
83+
"SoftwareProduct": image_path("softwareProduct.png"),
84+
"SoftwareVulnerability": image_path("softwareVulnerability.png"),
85+
"User": image_path("user.png")
8486
}
8587

86-
self.eyeUnhideIconImage = str(PACKAGE_DIR / "images" / "eyeUnhide.png")
87-
self.eyeHideIconImage = str(PACKAGE_DIR / "images" / "eyeHide.png")
88-
self.rgbColorIconImage = str(PACKAGE_DIR / "images" / "rgbColor.png")
88+
self.eyeUnhideIconImage = image_path("eyeUnhide.png")
89+
self.eyeHideIconImage = image_path("eyeHide.png")
90+
self.rgbColorIconImage = image_path("rgbColor.png")
8991

9092
#Create a registry as a dictionary containing name as key and class as value
9193
self.assetFactory = AssetFactory()
92-
attacker_icon = str(PACKAGE_DIR / "images" / "attacker.png")
94+
attacker_icon = image_path("attacker.png")
9395
self.assetFactory.registerAsset("Attacker", attacker_icon)
9496

9597
# Create the MAL language graph, language classes factory, and
@@ -101,12 +103,11 @@ def __init__(self,app,malLanguageMarFilePath):
101103

102104
for asset in self.langGraph.assets:
103105
if not asset.is_abstract:
104-
asset_image_path = str(PACKAGE_DIR / assetImages[asset.name])
105106
self.assetFactory.registerAsset(
106107
asset.name,
107-
asset_image_path
108+
assetImages[asset.name]
108109
)
109-
110+
110111
#assetFactory registration should complete before injecting into ModelScene
111112
self.scene = ModelScene(self.assetFactory, self.langGraph, self.lcs,self.model, self)
112113
self.view = ModelView(self.scene, self)
@@ -287,46 +288,46 @@ def updateAssetRelationsWindow(self, assetItem):
287288

288289
def createActions(self):
289290

290-
zoom_in_icon = str(PACKAGE_DIR / "images" / "zoomIn.png")
291+
zoom_in_icon = image_path("zoomIn.png")
291292
self.zoomInAction = QAction(QIcon(zoom_in_icon), "ZoomIn", self)
292293
self.zoomInAction.triggered.connect(self.zoomIn)
293294

294-
zoom_out_icon = str(PACKAGE_DIR / "images" / "zoomOut.png")
295+
zoom_out_icon = image_path("zoomOut.png")
295296
self.zoomOutAction = QAction(QIcon(zoom_out_icon), "ZoomOut", self)
296297
self.zoomOutAction.triggered.connect(self.zoomOut)
297298

298299
#undo Action
299-
undo_icon = str(PACKAGE_DIR / "images" / "undoIcon.png")
300+
undo_icon = image_path("undoIcon.png")
300301
self.undoAction = QAction(QIcon(undo_icon), "Undo", self)
301302
self.undoAction.setShortcut("Ctrl+z")
302303
self.undoAction.triggered.connect(self.scene.undoStack.undo)
303304

304305
#redo Action
305-
redo_icon = str(PACKAGE_DIR / "images" / "redoIcon.png")
306+
redo_icon = image_path("redoIcon.png")
306307
self.redoAction = QAction(QIcon(redo_icon), "Redo", self)
307308
self.redoAction.setShortcut("Ctrl+Shift+z")
308309
self.redoAction.triggered.connect(self.scene.undoStack.redo)
309310

310311
#cut Action
311-
cut_icon = str(PACKAGE_DIR / "images" / "cutIcon.png")
312+
cut_icon = image_path("cutIcon.png")
312313
self.cutAction = QAction(QIcon(cut_icon), "Cut", self)
313314
self.cutAction.setShortcut("Ctrl+x")
314315
self.cutAction.triggered.connect(lambda: self.scene.cutAssets(self.scene.selectedItems()))
315316

316317
#copy Action
317-
copy_icon = str(PACKAGE_DIR / "images" / "copyIcon.png")
318+
copy_icon = image_path("copyIcon.png")
318319
self.copyAction = QAction(QIcon(copy_icon), "Copy", self)
319320
self.copyAction.setShortcut("Ctrl+c")
320321
self.copyAction.triggered.connect(lambda: self.scene.copyAssets(self.scene.selectedItems()))
321322

322323
#paste Action
323-
paste_icon = str(PACKAGE_DIR / "images" / "pasteIcon.png")
324+
paste_icon = image_path("pasteIcon.png")
324325
self.pasteAction = QAction(QIcon(paste_icon), "Paste", self)
325326
self.pasteAction.setShortcut("Ctrl+v")
326327
self.pasteAction.triggered.connect(lambda: self.scene.pasteAssets(QPointF(0,0)))
327328

328329
#delete Action
329-
delete_icon = str(PACKAGE_DIR / "images" / "deleteIcon.png")
330+
delete_icon = image_path("deleteIcon.png")
330331
self.deleteAction = QAction(QIcon(delete_icon), "Delete", self)
331332
self.deleteAction.setShortcut("Delete")
332333
self.deleteAction.triggered.connect(lambda: self.scene.deleteAssets(self.scene.selectedItems()))
@@ -409,7 +410,7 @@ def createToolbar(self):
409410
self.toolbar.addSeparator()
410411

411412
#Fit To Window
412-
fit_to_view_icon = str(PACKAGE_DIR / "images" / "fitToView.png")
413+
fit_to_view_icon = image_path("fitToView.png")
413414
fitToViewButton = QPushButton(QIcon(fit_to_view_icon), "Fit To View")
414415
self.toolbar.addWidget(fitToViewButton)
415416
fitToViewButton.clicked.connect(self.fitToViewButtonClicked)

mal_gui/UndoRedoCommands/ContainerizeAssetsCommand.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from ..ObjectExplorer.AssetBase import AssetBase
55
from ..AssetsContainer.AssetsContainer import AssetsContainer
6+
from ..file_utils import image_path
67

78
class ContainerizeAssetsCommand(QUndoCommand):
89
def __init__(self, scene, items, parent=None):
@@ -23,7 +24,13 @@ def __init__(self, scene, items, parent=None):
2324
if hasattr(item, 'connections'):
2425
self.connections.extend(item.connections.copy())
2526

26-
self.newAssetsContainer = AssetsContainer("AssetContainer", "ContainerName", "images/assetContainer.png","images/assetContainerPlusSymbol.png","images/assetContainerMinusSymbol.png")
27+
self.newAssetsContainer = AssetsContainer(
28+
"AssetContainer",
29+
"ContainerName",
30+
image_path("assetContainer.png"),
31+
image_path("assetContainerPlusSymbol.png"),
32+
image_path("assetContainerMinusSymbol.png")
33+
)
2734
self.newAssetsContainer.build()
2835

2936
def redo(self):

mal_gui/file_utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""Utilities for loading files"""
2+
3+
from pathlib import Path
4+
5+
PACKAGE_DIR = Path(__file__).resolve().parent
6+
7+
def image_path(filename):
8+
"""From a filename, return the absolute path of the image"""
9+
return str(PACKAGE_DIR / "images" / filename)

0 commit comments

Comments
 (0)