1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
import sys
from qtpy import QtCore, QtGui, QtWidgets
import qtawesome
# TODO: Set icon colour and copy code with color kwarg
VIEW_COLUMNS = 5
AUTO_SEARCH_TIMEOUT = 500
ALL_COLLECTIONS = 'All'
class IconBrowser(QtWidgets.QMainWindow):
"""
A small browser window that allows the user to search through all icons from
the available version of QtAwesome. You can also copy the name and python
code for the currently selected icon.
"""
def __init__(self):
super().__init__()
self.setMinimumSize(400, 300)
self.setWindowTitle('QtAwesome Icon Browser')
qtawesome._instance()
fontMaps = qtawesome._resource['iconic'].charmap
iconNames = []
for fontCollection, fontData in fontMaps.items():
for iconName in fontData:
iconNames.append('%s.%s' % (fontCollection, iconName))
self._filterTimer = QtCore.QTimer(self)
self._filterTimer.setSingleShot(True)
self._filterTimer.setInterval(AUTO_SEARCH_TIMEOUT)
self._filterTimer.timeout.connect(self._updateFilter)
model = IconModel()
model.setStringList(sorted(iconNames))
self._proxyModel = QtCore.QSortFilterProxyModel()
self._proxyModel.setSourceModel(model)
self._proxyModel.setFilterCaseSensitivity(QtCore.Qt.CaseInsensitive)
self._listView = IconListView(self)
self._listView.setUniformItemSizes(True)
self._listView.setViewMode(QtWidgets.QListView.IconMode)
self._listView.setModel(self._proxyModel)
self._listView.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self._listView.doubleClicked.connect(self._copyIconText)
self._listView.selectionModel().selectionChanged.connect(self._updateNameField)
self._lineEdit = QtWidgets.QLineEdit(self)
self._lineEdit.setAlignment(QtCore.Qt.AlignCenter)
self._lineEdit.textChanged.connect(self._triggerDelayedUpdate)
self._lineEdit.returnPressed.connect(self._triggerImmediateUpdate)
self._comboBox = QtWidgets.QComboBox(self)
self._comboBox.setMinimumWidth(75)
self._comboBox.currentIndexChanged.connect(self._triggerImmediateUpdate)
self._comboBox.addItems([ALL_COLLECTIONS] + sorted(fontMaps.keys()))
lyt = QtWidgets.QHBoxLayout()
lyt.setContentsMargins(0, 0, 0, 0)
lyt.addWidget(self._comboBox)
lyt.addWidget(self._lineEdit)
self._combo_style = QtWidgets.QComboBox(self)
self._combo_style.addItems([
qtawesome.styles.DEFAULT_DARK_PALETTE,
qtawesome.styles.DEFAULT_LIGHT_PALETTE])
self._combo_style.currentTextChanged.connect(self._updateStyle)
lyt.addWidget(self._combo_style)
searchBarFrame = QtWidgets.QFrame(self)
searchBarFrame.setLayout(lyt)
self._nameField = QtWidgets.QLineEdit(self)
self._nameField.setAlignment(QtCore.Qt.AlignCenter)
self._nameField.setReadOnly(True)
self._copyButton = QtWidgets.QPushButton('Copy Name', self)
self._copyButton.clicked.connect(self._copyIconText)
lyt = QtWidgets.QVBoxLayout()
lyt.addWidget(searchBarFrame)
lyt.addWidget(self._listView)
lyt.addWidget(self._nameField)
lyt.addWidget(self._copyButton)
frame = QtWidgets.QFrame(self)
frame.setLayout(lyt)
self.setCentralWidget(frame)
self.setTabOrder(self._comboBox, self._lineEdit)
self.setTabOrder(self._lineEdit, self._combo_style)
self.setTabOrder(self._combo_style, self._listView)
self.setTabOrder(self._listView, self._nameField)
self.setTabOrder(self._nameField, self._copyButton)
self.setTabOrder(self._copyButton, self._comboBox)
QtWidgets.QShortcut(
QtGui.QKeySequence(QtCore.Qt.Key_Return),
self,
self._copyIconText,
)
QtWidgets.QShortcut(
QtGui.QKeySequence("Ctrl+F"),
self,
self._lineEdit.setFocus,
)
self._lineEdit.setFocus()
geo = self.geometry()
# QApplication.desktop() has been removed in Qt 6.
# Instead, QGuiApplication.screenAt(QPoint) is supported
# in Qt 5.10 or later.
try:
screen = QtGui.QGuiApplication.screenAt(QtGui.QCursor.pos())
centerPoint = screen.geometry().center()
except AttributeError:
desktop = QtWidgets.QApplication.desktop()
screen = desktop.screenNumber(desktop.cursor().pos())
centerPoint = desktop.screenGeometry(screen).center()
geo.moveCenter(centerPoint)
self.setGeometry(geo)
def _updateStyle(self, text: str):
_app = QtWidgets.QApplication.instance()
if text == qtawesome.styles.DEFAULT_DARK_PALETTE:
qtawesome.reset_cache()
qtawesome.dark(_app)
else:
qtawesome.reset_cache()
qtawesome.light(_app)
def _updateFilter(self):
"""
Update the string used for filtering in the proxy model with the
current text from the line edit.
"""
reString = ""
group = self._comboBox.currentText()
if group != ALL_COLLECTIONS:
reString += r"^%s\." % group
searchTerm = self._lineEdit.text()
if searchTerm:
reString += ".*%s.*$" % searchTerm
# QSortFilterProxyModel.setFilterRegExp has been removed in Qt 6.
# Instead, QSortFilterProxyModel.setFilterRegularExpression is
# supported in Qt 5.12 or later.
try:
self._proxyModel.setFilterRegularExpression(reString)
except AttributeError:
self._proxyModel.setFilterRegExp(reString)
def _triggerDelayedUpdate(self):
"""
Reset the timer used for committing the search term to the proxy model.
"""
self._filterTimer.stop()
self._filterTimer.start()
def _triggerImmediateUpdate(self):
"""
Stop the timer used for committing the search term and update the
proxy model immediately.
"""
self._filterTimer.stop()
self._updateFilter()
def _copyIconText(self):
"""
Copy the name of the currently selected icon to the clipboard.
"""
indexes = self._listView.selectedIndexes()
if not indexes:
return
clipboard = QtWidgets.QApplication.instance().clipboard()
clipboard.setText(indexes[0].data())
def _updateNameField(self):
"""
Update field to the name of the currently selected icon.
"""
indexes = self._listView.selectedIndexes()
if not indexes:
self._nameField.setText("")
else:
self._nameField.setText(indexes[0].data())
class IconListView(QtWidgets.QListView):
"""
A QListView that scales it's grid size to ensure the same number of
columns are always drawn.
"""
def __init__(self, parent=None):
super().__init__(parent)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
def resizeEvent(self, event):
"""
Re-implemented to re-calculate the grid size to provide scaling icons
Parameters
----------
event : QtCore.QEvent
"""
width = self.viewport().width() - 30
# The minus 30 above ensures we don't end up with an item width that
# can't be drawn the expected number of times across the view without
# being wrapped. Without this, the view can flicker during resize
tileWidth = width / VIEW_COLUMNS
iconWidth = int(tileWidth * 0.8)
# tileWidth needs to be an integer for setGridSize
tileWidth = int(tileWidth)
self.setGridSize(QtCore.QSize(tileWidth, tileWidth))
self.setIconSize(QtCore.QSize(iconWidth, iconWidth))
return super().resizeEvent(event)
class IconModel(QtCore.QStringListModel):
def __init__(self):
super().__init__()
def flags(self, index):
return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
def data(self, index, role):
"""
Re-implemented to return the icon for the current index.
Parameters
----------
index : QtCore.QModelIndex
role : int
Returns
-------
Any
"""
if role == QtCore.Qt.DecorationRole:
iconString = self.data(index, role=QtCore.Qt.DisplayRole)
return qtawesome.icon(iconString)
return super().data(index, role)
def run():
"""
Start the IconBrowser and block until the process exits.
"""
app = QtWidgets.QApplication([])
qtawesome.dark(app)
browser = IconBrowser()
browser.show()
sys.exit(app.exec_())
if __name__ == '__main__':
run()
|