|
10 | 10 |
|
11 | 11 | import json |
12 | 12 | import os |
13 | | -from typing import Dict, Sequence |
| 13 | +from typing import Dict, Literal, Sequence |
14 | 14 |
|
15 | 15 | import numpy as np |
| 16 | +import qtpy.QtCore as QC |
16 | 17 | import qtpy.QtGui as QG |
17 | 18 | from guidata.configtools import get_module_data_path |
18 | 19 | from qwt import QwtInterval, toQImage |
|
23 | 24 | # from guidata.dataset.datatypes import NoDefault |
24 | 25 | FULLRANGE = QwtInterval(0.0, 1.0) |
25 | 26 | DEFAULT = EditableColormap(name="jet") |
| 27 | +SQUARE_ICON_SIZE = 16 |
| 28 | +RECT_ICON_SIZE_W, RECT_ICON_SIZE_H = 80, 16 |
26 | 29 |
|
27 | 30 | CmapDictType = Dict[str, EditableColormap] |
28 | 31 |
|
@@ -82,34 +85,85 @@ def save_colormaps(json_filename: str, colormaps: CmapDictType): |
82 | 85 |
|
83 | 86 |
|
84 | 87 | def build_icon_from_cmap( |
85 | | - cmap: EditableColormap, width: int = 16, height: int = 16 |
| 88 | + cmap: EditableColormap, |
| 89 | + width: int = SQUARE_ICON_SIZE, |
| 90 | + height: int = SQUARE_ICON_SIZE, |
| 91 | + orientation: Literal["h", "v"] = "v", |
| 92 | + margin: int = 0, |
86 | 93 | ) -> QG.QIcon: |
87 | 94 | """Builds an icon representing the colormap |
88 | 95 |
|
89 | 96 | Args: |
90 | 97 | cmap: colormap |
91 | 98 | width: icon width |
92 | 99 | height: icon height |
| 100 | + orientation: orientation of the colormap in the icon. Can be "h" for horizontal |
| 101 | + or "v" for vertical |
| 102 | + margin: margin around the colormap in the icon. Beware that the margin is |
| 103 | + included in the given icon size. For example, if margin is 1 and width is 16, |
| 104 | + the actual colormap width will be 14 (16 - 2 * 1). This was done to prevent |
| 105 | + interpolation on display. |
93 | 106 | """ |
94 | | - data = np.zeros((width, height), np.uint8) |
95 | | - line = np.linspace(0, 255, width) |
96 | | - data[:, :] = line[:, np.newaxis] |
| 107 | + |
| 108 | + padded_width = width - 2 * margin |
| 109 | + padded_height = height - 2 * margin |
| 110 | + |
| 111 | + data = np.zeros((padded_height, padded_width), np.uint8) |
| 112 | + |
| 113 | + if orientation == "h": |
| 114 | + line = np.linspace(0, 255, padded_width) |
| 115 | + data[:, :] = line[:] |
| 116 | + else: |
| 117 | + line = np.linspace(0, 255, padded_height) |
| 118 | + data[:, :] = line[:, np.newaxis] |
| 119 | + |
97 | 120 | img = toQImage(data) |
98 | 121 | img.setColorTable(cmap.colorTable(FULLRANGE)) |
99 | | - return QG.QIcon(QG.QPixmap.fromImage(img)) |
| 122 | + cm_pxmap = QG.QPixmap.fromImage(img) |
| 123 | + |
| 124 | + if margin == 0: |
| 125 | + return QG.QIcon(cm_pxmap) |
| 126 | + |
| 127 | + padded_pixmap = QG.QPixmap(width, height) |
| 128 | + padded_pixmap.fill(QC.Qt.GlobalColor.transparent) |
| 129 | + |
| 130 | + # Create a painter to draw onto the new pixmap |
| 131 | + painter = QG.QPainter(padded_pixmap) |
| 132 | + painter.drawPixmap(margin, margin, cm_pxmap) |
| 133 | + painter.end() |
100 | 134 |
|
| 135 | + icon = QG.QIcon(padded_pixmap) |
101 | 136 |
|
102 | | -def build_icon_from_cmap_name(cmap_name: str) -> QG.QIcon: |
| 137 | + return icon |
| 138 | + |
| 139 | + |
| 140 | +def build_icon_from_cmap_name( |
| 141 | + cmap_name: str, |
| 142 | + width: int = SQUARE_ICON_SIZE, |
| 143 | + height: int = SQUARE_ICON_SIZE, |
| 144 | + orientation: Literal["h", "v"] = "v", |
| 145 | + margin: int = 0, |
| 146 | +) -> QG.QIcon: |
103 | 147 | """Builds an QIcon representing the colormap from the colormap name found in |
104 | 148 | ALL_COLORMAPS global variable. |
105 | 149 |
|
106 | 150 | Args: |
107 | 151 | cmap_name: colormap name to search in ALL_COLORMAPS |
| 152 | + width: icon width |
| 153 | + height: icon height |
| 154 | + orientation: orientation of the colormap in the icon. Can be "h" for horizontal |
| 155 | + or "v" for vertical |
| 156 | + margin: margin around the colormap in the icon. Beware that the margin is |
| 157 | + included in the given icon size. For example, if margin is 1 and width is 16, |
| 158 | + the actual colormap width will be 14 (16 - 2 * 1). This was done to prevent |
| 159 | + interpolation on display. |
108 | 160 |
|
109 | 161 | Returns: |
110 | 162 | QIcon representing the colormap |
111 | 163 | """ |
112 | | - return build_icon_from_cmap(get_cmap(cmap_name.lower())) |
| 164 | + return build_icon_from_cmap( |
| 165 | + get_cmap(cmap_name.lower()), width, height, orientation, margin |
| 166 | + ) |
113 | 167 |
|
114 | 168 |
|
115 | 169 | def get_cmap(cmap_name: str) -> EditableColormap: |
|
0 commit comments