Cleanup
[carla.git] / source / frontend / patchcanvas / canvasicon.py
blobb9bd3715eb2abbc2d991563677218b7a9d765726
1 #!/usr/bin/env python3
2 # SPDX-FileCopyrightText: 2011-2024 Filipe Coelho <falktx@falktx.com>
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 # ------------------------------------------------------------------------------------------------------------
6 # Imports (Global)
8 from qt_compat import qt_config
10 if qt_config == 5:
11 from PyQt5.QtCore import qCritical, QRectF
12 from PyQt5.QtGui import QPainter
13 from PyQt5.QtSvg import QGraphicsSvgItem, QSvgRenderer
14 from PyQt5.QtWidgets import QGraphicsColorizeEffect
15 elif qt_config == 6:
16 from PyQt6.QtCore import qCritical, QRectF
17 from PyQt6.QtGui import QPainter
18 from PyQt6.QtSvg import QSvgRenderer
19 from PyQt6.QtSvgWidgets import QGraphicsSvgItem
20 from PyQt6.QtWidgets import QGraphicsColorizeEffect
22 # ------------------------------------------------------------------------------------------------------------
23 # Imports (Custom)
25 from . import (
26 canvas,
27 icon2str,
28 CanvasIconType,
29 ICON_APPLICATION,
30 ICON_HARDWARE,
31 ICON_DISTRHO,
32 ICON_FILE,
33 ICON_PLUGIN,
34 ICON_LADISH_ROOM,
37 # ------------------------------------------------------------------------------------------------------------
39 class CanvasIcon(QGraphicsSvgItem):
40 def __init__(self, icon, name, parent):
41 QGraphicsSvgItem.__init__(self)
42 self.setParentItem(parent)
44 self.m_renderer = None
45 self.p_size = QRectF(0, 0, 0, 0)
47 self.m_colorFX = QGraphicsColorizeEffect(self)
48 self.m_colorFX.setColor(canvas.theme.box_text.color())
50 self.setGraphicsEffect(self.m_colorFX)
51 self.setIcon(icon, name)
53 def setIcon(self, icon, name):
54 name = name.lower()
55 icon_path = ""
57 if icon == ICON_APPLICATION:
58 self.p_size = QRectF(3, 2, 19, 18)
60 if "audacious" in name:
61 icon_path = ":/scalable/pb_audacious.svg"
62 self.p_size = QRectF(5, 4, 16, 16)
63 elif "clementine" in name:
64 icon_path = ":/scalable/pb_clementine.svg"
65 self.p_size = QRectF(5, 4, 16, 16)
66 elif "distrho" in name:
67 icon_path = ":/scalable/pb_distrho.svg"
68 self.p_size = QRectF(5, 4, 16, 16)
69 elif "jamin" in name:
70 icon_path = ":/scalable/pb_jamin.svg"
71 self.p_size = QRectF(5, 3, 16, 16)
72 elif "mplayer" in name:
73 icon_path = ":/scalable/pb_mplayer.svg"
74 self.p_size = QRectF(5, 4, 16, 16)
75 elif "vlc" in name:
76 icon_path = ":/scalable/pb_vlc.svg"
77 self.p_size = QRectF(5, 3, 16, 16)
79 else:
80 icon_path = ":/scalable/pb_generic.svg"
81 self.p_size = QRectF(4, 3, 16, 16)
83 elif icon == ICON_HARDWARE:
84 icon_path = ":/scalable/pb_hardware.svg"
85 self.p_size = QRectF(5, 2, 16, 16)
87 elif icon == ICON_DISTRHO:
88 icon_path = ":/scalable/pb_distrho.svg"
89 self.p_size = QRectF(5, 4, 16, 16)
91 elif icon == ICON_FILE:
92 icon_path = ":/scalable/pb_file.svg"
93 self.p_size = QRectF(5, 4, 16, 16)
95 elif icon == ICON_PLUGIN:
96 icon_path = ":/scalable/pb_plugin.svg"
97 self.p_size = QRectF(5, 4, 16, 16)
99 elif icon == ICON_LADISH_ROOM:
100 # TODO - make a unique ladish-room icon
101 icon_path = ":/scalable/pb_hardware.svg"
102 self.p_size = QRectF(5, 2, 16, 16)
104 else:
105 self.p_size = QRectF(0, 0, 0, 0)
106 qCritical("PatchCanvas::CanvasIcon.setIcon(%s, %s) - unsupported icon requested" % (
107 icon2str(icon), name.encode()))
108 return
110 self.m_renderer = QSvgRenderer(icon_path, canvas.scene)
111 self.setSharedRenderer(self.m_renderer)
112 self.update()
114 def type(self):
115 return CanvasIconType
117 def boundingRect(self):
118 return self.p_size
120 def paint(self, painter, option, widget):
121 if not self.m_renderer:
122 QGraphicsSvgItem.paint(self, painter, option, widget)
123 return
125 painter.save()
126 painter.setRenderHint(QPainter.Antialiasing, False)
127 painter.setRenderHint(QPainter.TextAntialiasing, False)
128 self.m_renderer.render(painter, self.p_size)
129 painter.restore()
131 # ------------------------------------------------------------------------------------------------------------