|
17 | 17 | #include <QKeyEvent> |
18 | 18 | #include <QLineEdit> |
19 | 19 | #include <QPushButton> |
| 20 | +#include <QRandomGenerator> |
20 | 21 | #include <QSortFilterProxyModel> |
21 | 22 |
|
22 | 23 | #include "Application.h" |
@@ -90,6 +91,8 @@ IconPickerDialog::IconPickerDialog(QWidget* parent) : QDialog(parent), ui(new Ui |
90 | 91 | connect(searchBar, &QLineEdit::textChanged, this, &IconPickerDialog::filterIcons); |
91 | 92 | // Prevent incorrect indices from e.g. filesystem changes |
92 | 93 | connect(APPLICATION->icons().get(), &IconList::iconUpdated, this, [this]() { proxyModel->invalidate(); }); |
| 94 | + auto randomButton = ui->buttonBox->addButton(tr("Random Icon"), QDialogButtonBox::ResetRole); |
| 95 | + connect(randomButton, &QPushButton::clicked, this, &IconPickerDialog::selectRandomIcon); |
93 | 96 | } |
94 | 97 |
|
95 | 98 | bool IconPickerDialog::eventFilter(QObject* obj, QEvent* evt) |
@@ -174,6 +177,20 @@ IconPickerDialog::~IconPickerDialog() |
174 | 177 | delete ui; |
175 | 178 | } |
176 | 179 |
|
| 180 | +void IconPickerDialog::selectRandomIcon() |
| 181 | +{ |
| 182 | + int rowAmount = ui->iconView->model()->rowCount(); |
| 183 | + int randomRowNum = QRandomGenerator::global()->bounded(rowAmount); |
| 184 | + QModelIndex index = ui->iconView->model()->index(randomRowNum, 0); |
| 185 | + |
| 186 | + ui->iconView->selectionModel()->select(index, QItemSelectionModel::Current | QItemSelectionModel::Select); |
| 187 | + ui->iconView->setCurrentIndex(index); |
| 188 | + ui->iconView->scrollTo(index); |
| 189 | + |
| 190 | + selectedIconKey = index.data(Qt::UserRole).toString(); |
| 191 | + buttonRemove->setEnabled(APPLICATION->icons()->iconFileExists(selectedIconKey)); |
| 192 | +} |
| 193 | + |
177 | 194 | void IconPickerDialog::openFolder() |
178 | 195 | { |
179 | 196 | DesktopServices::openPath(APPLICATION->icons()->iconDirectory(selectedIconKey), true); |
|
0 commit comments