| 1 |
import sys |
| 2 |
|
| 3 |
from qtpy import QtCore, QtGui, QtWidgets |
| 4 |
|
| 5 |
import qtawesome |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
DEFAULT_VIEW_COLUMNS = 5 |
| 11 |
VIEW_COLUMNS_OPTIONS = [5, 8, 10, 15, 20, 25, 30] |
| 12 |
AUTO_SEARCH_TIMEOUT = 500 |
| 13 |
ALL_COLLECTIONS = "All" |
| 14 |
|
| 15 |
|
| 16 |
class IconBrowser(QtWidgets.QMainWindow): |
| 17 |
""" |
| 18 |
A small browser window that allows the user to search through all icons from |
| 19 |
the available version of QtAwesome. You can also copy the name and python |
| 20 |
code for the currently selected icon. |
| 21 |
""" |
| 22 |
|
| 23 |
def __init__(self): |
| 24 |
super().__init__() |
| 25 |
|
| 26 |
qtawesome._instance() |
| 27 |
fontMaps = qtawesome._resource["iconic"].charmap |
| 28 |
|
| 29 |
iconNames = [] |
| 30 |
for fontCollection, fontData in fontMaps.items(): |
| 31 |
for iconName in fontData: |
| 32 |
iconNames.append("%s.%s" % (fontCollection, iconName)) |
| 33 |
|
| 34 |
self.setMinimumSize(300, 300) |
| 35 |
self.setWindowTitle("QtAwesome Icon Browser") |
| 36 |
self.setWindowIcon(qtawesome.icon("fa5s.icons")) |
| 37 |
|
| 38 |
self._filterTimer = QtCore.QTimer(self) |
| 39 |
self._filterTimer.setSingleShot(True) |
| 40 |
self._filterTimer.setInterval(AUTO_SEARCH_TIMEOUT) |
| 41 |
self._filterTimer.timeout.connect(self._updateFilter) |
| 42 |
|
| 43 |
model = IconModel() |
| 44 |
model.setStringList(sorted(iconNames)) |
| 45 |
|
| 46 |
self._proxyModel = QtCore.QSortFilterProxyModel() |
| 47 |
self._proxyModel.setSourceModel(model) |
| 48 |
self._proxyModel.setFilterCaseSensitivity(QtCore.Qt.CaseInsensitive) |
| 49 |
|
| 50 |
self._listView = IconListView(DEFAULT_VIEW_COLUMNS, parent=self) |
| 51 |
self._listView.setUniformItemSizes(True) |
| 52 |
self._listView.setViewMode(QtWidgets.QListView.IconMode) |
| 53 |
self._listView.setModel(self._proxyModel) |
| 54 |
self._listView.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) |
| 55 |
self._listView.doubleClicked.connect(self._copyIconText) |
| 56 |
self._listView.selectionModel().selectionChanged.connect(self._updateNameField) |
| 57 |
|
| 58 |
toolbar = QtWidgets.QHBoxLayout() |
| 59 |
|
| 60 |
|
| 61 |
self._comboFont = QtWidgets.QComboBox(self) |
| 62 |
self._comboFont.setToolTip( |
| 63 |
"Select the font prefix whose icons will be included in the filtering." |
| 64 |
) |
| 65 |
self._comboFont.setMaximumWidth(75) |
| 66 |
self._comboFont.addItems([ALL_COLLECTIONS] + sorted(fontMaps.keys())) |
| 67 |
self._comboFont.currentIndexChanged.connect(self._triggerImmediateUpdate) |
| 68 |
toolbar.addWidget(self._comboFont) |
| 69 |
|
| 70 |
self._lineEditFilter = QtWidgets.QLineEdit(self) |
| 71 |
self._lineEditFilter.setToolTip("Filter icons by name") |
| 72 |
self._lineEditFilter.setMaximumWidth(200) |
| 73 |
self._lineEditFilter.setToolTip("Filter icons by name") |
| 74 |
self._lineEditFilter.setAlignment(QtCore.Qt.AlignLeft) |
| 75 |
self._lineEditFilter.textChanged.connect(self._triggerDelayedUpdate) |
| 76 |
self._lineEditFilter.returnPressed.connect(self._triggerImmediateUpdate) |
| 77 |
self._lineEditFilter.setClearButtonEnabled(True) |
| 78 |
toolbar.addWidget(self._lineEditFilter, stretch=10) |
| 79 |
|
| 80 |
|
| 81 |
self._nameField = QtWidgets.QLineEdit(self) |
| 82 |
self._nameField.setPlaceholderText( |
| 83 |
"Full identifier of the currently selected icon" |
| 84 |
) |
| 85 |
self._nameField.setAlignment(QtCore.Qt.AlignCenter) |
| 86 |
self._nameField.setReadOnly(True) |
| 87 |
self._nameField.setMaximumWidth(250) |
| 88 |
fnt = self._nameField.font() |
| 89 |
fnt.setFamily("monospace") |
| 90 |
fnt.setBold(True) |
| 91 |
self._nameField.setFont(fnt) |
| 92 |
toolbar.addWidget(self._nameField, stretch=10) |
| 93 |
|
| 94 |
self._copyButton = QtWidgets.QPushButton("Copy Name", self) |
| 95 |
self._copyButton.setToolTip( |
| 96 |
"Copy selected icon full identifier to the clipboard" |
| 97 |
) |
| 98 |
self._copyButton.clicked.connect(self._copyIconText) |
| 99 |
self._copyButton.setDisabled(True) |
| 100 |
toolbar.addWidget(self._copyButton) |
| 101 |
toolbar.addStretch(1) |
| 102 |
|
| 103 |
|
| 104 |
self._comboStyle = QtWidgets.QComboBox(self) |
| 105 |
self._comboStyle.setToolTip( |
| 106 |
"Select color palette for the icons and the icon browser" |
| 107 |
) |
| 108 |
self._comboStyle.addItem(qtawesome.styles.DEFAULT_DARK_PALETTE, 0) |
| 109 |
self._comboStyle.addItem(qtawesome.styles.DEFAULT_LIGHT_PALETTE, 1) |
| 110 |
self._comboStyle.currentTextChanged.connect(self._updateStyle) |
| 111 |
toolbar.addWidget(self._comboStyle) |
| 112 |
|
| 113 |
|
| 114 |
self._comboColumns = QtWidgets.QComboBox(self) |
| 115 |
self._comboColumns.setToolTip( |
| 116 |
"Select number of columns the icons list is showing" |
| 117 |
) |
| 118 |
for num_columns in VIEW_COLUMNS_OPTIONS: |
| 119 |
self._comboColumns.addItem(str(num_columns), num_columns) |
| 120 |
self._comboColumns.setCurrentIndex( |
| 121 |
self._comboColumns.findData(DEFAULT_VIEW_COLUMNS) |
| 122 |
) |
| 123 |
self._comboColumns.currentTextChanged.connect(self._updateColumns) |
| 124 |
toolbar.addWidget(self._comboColumns) |
| 125 |
|
| 126 |
|
| 127 |
lyt = QtWidgets.QVBoxLayout() |
| 128 |
lyt.addLayout(toolbar) |
| 129 |
lyt.addWidget(self._listView) |
| 130 |
|
| 131 |
frame = QtWidgets.QFrame(self) |
| 132 |
frame.setLayout(lyt) |
| 133 |
|
| 134 |
self.setCentralWidget(frame) |
| 135 |
|
| 136 |
self.setTabOrder(self._comboFont, self._lineEditFilter) |
| 137 |
self.setTabOrder(self._lineEditFilter, self._comboStyle) |
| 138 |
self.setTabOrder(self._comboStyle, self._listView) |
| 139 |
self.setTabOrder(self._listView, self._nameField) |
| 140 |
self.setTabOrder(self._nameField, self._copyButton) |
| 141 |
self.setTabOrder(self._copyButton, self._comboFont) |
| 142 |
|
| 143 |
|
| 144 |
QtWidgets.QShortcut( |
| 145 |
QtGui.QKeySequence(QtCore.Qt.Key_Return), |
| 146 |
self, |
| 147 |
self._copyIconText, |
| 148 |
) |
| 149 |
QtWidgets.QShortcut( |
| 150 |
QtGui.QKeySequence("Ctrl+F"), |
| 151 |
self, |
| 152 |
self._lineEditFilter.setFocus, |
| 153 |
) |
| 154 |
|
| 155 |
self._lineEditFilter.setFocus() |
| 156 |
|
| 157 |
geo = self.geometry() |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
try: |
| 163 |
screen = QtGui.QGuiApplication.screenAt(QtGui.QCursor.pos()) |
| 164 |
centerPoint = screen.geometry().center() |
| 165 |
except AttributeError: |
| 166 |
desktop = QtWidgets.QApplication.desktop() |
| 167 |
screen = desktop.screenNumber(desktop.cursor().pos()) |
| 168 |
centerPoint = desktop.screenGeometry(screen).center() |
| 169 |
|
| 170 |
geo.moveCenter(centerPoint) |
| 171 |
self.setGeometry(geo) |
| 172 |
self._updateStyle(self._comboStyle.currentText()) |
| 173 |
|
| 174 |
def _updateStyle(self, text: str): |
| 175 |
_app = QtWidgets.QApplication.instance() |
| 176 |
if text == qtawesome.styles.DEFAULT_DARK_PALETTE: |
| 177 |
qtawesome.reset_cache() |
| 178 |
qtawesome.dark(_app) |
| 179 |
else: |
| 180 |
qtawesome.reset_cache() |
| 181 |
qtawesome.light(_app) |
| 182 |
|
| 183 |
def _updateColumns(self): |
| 184 |
self._listView.setColumns(self._comboColumns.currentData()) |
| 185 |
|
| 186 |
def _updateFilter(self): |
| 187 |
""" |
| 188 |
Update the string used for filtering in the proxy model with the |
| 189 |
current text from the line edit. |
| 190 |
""" |
| 191 |
reString = "" |
| 192 |
|
| 193 |
group = self._comboFont.currentText() |
| 194 |
if group != ALL_COLLECTIONS: |
| 195 |
reString += r"^%s\." % group |
| 196 |
|
| 197 |
searchTerm = self._lineEditFilter.text() |
| 198 |
if searchTerm: |
| 199 |
reString += ".*%s.*$" % searchTerm |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
try: |
| 205 |
self._proxyModel.setFilterRegularExpression(reString) |
| 206 |
except AttributeError: |
| 207 |
self._proxyModel.setFilterRegExp(reString) |
| 208 |
|
| 209 |
def _triggerDelayedUpdate(self): |
| 210 |
""" |
| 211 |
Reset the timer used for committing the search term to the proxy model. |
| 212 |
""" |
| 213 |
self._filterTimer.stop() |
| 214 |
self._filterTimer.start() |
| 215 |
|
| 216 |
def _triggerImmediateUpdate(self): |
| 217 |
""" |
| 218 |
Stop the timer used for committing the search term and update the |
| 219 |
proxy model immediately. |
| 220 |
""" |
| 221 |
self._filterTimer.stop() |
| 222 |
self._updateFilter() |
| 223 |
|
| 224 |
def _copyIconText(self): |
| 225 |
""" |
| 226 |
Copy the name of the currently selected icon to the clipboard. |
| 227 |
""" |
| 228 |
indexes = self._listView.selectedIndexes() |
| 229 |
if not indexes: |
| 230 |
return |
| 231 |
|
| 232 |
clipboard = QtWidgets.QApplication.instance().clipboard() |
| 233 |
clipboard.setText(indexes[0].data()) |
| 234 |
|
| 235 |
def _updateNameField(self): |
| 236 |
""" |
| 237 |
Update field to the name of the currently selected icon. |
| 238 |
""" |
| 239 |
indexes = self._listView.selectedIndexes() |
| 240 |
if not indexes: |
| 241 |
self._nameField.setText("") |
| 242 |
self._copyButton.setDisabled(True) |
| 243 |
return |
| 244 |
|
| 245 |
self._nameField.setText(indexes[0].data()) |
| 246 |
self._copyButton.setDisabled(False) |
| 247 |
|
| 248 |
|
| 249 |
class IconListView(QtWidgets.QListView): |
| 250 |
""" |
| 251 |
A QListView that scales it's grid size to ensure the same number of |
| 252 |
columns are always drawn. |
| 253 |
""" |
| 254 |
|
| 255 |
def __init__(self, columns, parent=None): |
| 256 |
super().__init__(parent) |
| 257 |
self._columns = columns |
| 258 |
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn) |
| 259 |
|
| 260 |
def setColumns(self, cols): |
| 261 |
""" |
| 262 |
Set columns number and resize. |
| 263 |
""" |
| 264 |
self._columns = cols |
| 265 |
self._resize() |
| 266 |
|
| 267 |
def _resize(self): |
| 268 |
""" |
| 269 |
Set grid and icon size taking into account the number of columns. |
| 270 |
""" |
| 271 |
|
| 272 |
width = self.viewport().width() - 30 |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
tileWidth = width / self._columns |
| 277 |
iconWidth = int(tileWidth * 0.8) |
| 278 |
|
| 279 |
tileWidth = int(tileWidth) |
| 280 |
|
| 281 |
self.setGridSize(QtCore.QSize(tileWidth, tileWidth)) |
| 282 |
self.setIconSize(QtCore.QSize(iconWidth, iconWidth)) |
| 283 |
|
| 284 |
def resizeEvent(self, event): |
| 285 |
""" |
| 286 |
Re-implemented to resize view following number of columns available. |
| 287 |
""" |
| 288 |
self._resize() |
| 289 |
return super().resizeEvent(event) |
| 290 |
|
| 291 |
|
| 292 |
class IconModel(QtCore.QStringListModel): |
| 293 |
def __init__(self): |
| 294 |
super().__init__() |
| 295 |
|
| 296 |
def flags(self, index): |
| 297 |
return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable |
| 298 |
|
| 299 |
def data(self, index, role): |
| 300 |
""" |
| 301 |
Re-implemented to return the icon for the current index. |
| 302 |
|
| 303 |
Parameters |
| 304 |
---------- |
| 305 |
index : QtCore.QModelIndex |
| 306 |
role : int |
| 307 |
|
| 308 |
Returns |
| 309 |
------- |
| 310 |
Any |
| 311 |
""" |
| 312 |
if role == QtCore.Qt.DecorationRole: |
| 313 |
iconString = self.data(index, role=QtCore.Qt.DisplayRole) |
| 314 |
return qtawesome.icon(iconString) |
| 315 |
return super().data(index, role) |
| 316 |
|
| 317 |
|
| 318 |
def run(): |
| 319 |
""" |
| 320 |
Start the IconBrowser and block until the process exits. |
| 321 |
""" |
| 322 |
app = QtWidgets.QApplication([]) |
| 323 |
qtawesome.dark(app) |
| 324 |
|
| 325 |
browser = IconBrowser() |
| 326 |
browser.show() |
| 327 |
|
| 328 |
sys.exit(app.exec_()) |
| 329 |
|
| 330 |
|
| 331 |
if __name__ == "__main__": |
| 332 |
run() |