| 17 | |
| 18 | QFileIconProvider FolderIconSelector::iconPro; |
| 19 | FolderIconSelector::FolderIconSelector(const QString& dirPath, QWidget *parent) |
| 20 | : QWidget(parent) |
| 21 | , ui(new Ui::FolderIconSelector) |
| 22 | , dirPath(dirPath) |
| 23 | { |
| 24 | ui->setupUi(this); |
| 25 | // 不加这句的话,无法设置item高度(原因未知) |
| 26 | // https://blog.csdn.net/lilongwei_123/article/details/109503886 |
| 27 | ui->comboBox->setView(new QListView()); |
| 28 | |
| 29 | ui->comboBox->installEventFilter(this); |
| 30 | |
| 31 | connect(ui->btn_apply, &QPushButton::clicked, this, [=](){ |
| 32 | auto iconPath = ui->comboBox->currentData().toString(); |
| 33 | if (iconPath.isEmpty()) return; |
| 34 | |
| 35 | if (Util::isInDir(iconPath, dirPath)) // 若icon在folder内,则转为相对路径 |
| 36 | iconPath = QDir(dirPath).relativeFilePath(iconPath); |
| 37 | |
| 38 | qDebug() << "Folder:" << dirPath << "Icon:" << iconPath; |
| 39 | if (Util::setFolderIcon(dirPath, iconPath)) { |
| 40 | QPixmapCache::clear(); // 清除缓存,否则文件夹图标不会更新 |
| 41 | setIcon(iconPro.icon(dirPath)); |
| 42 | } else { |
| 43 | showActionFailed(); |
| 44 | // 貌似在不重启的情况下,无法UAC提权 |
| 45 | QMessageBox::warning(this, "Failed", "Failed to set folder icon.\nTry restart as [Administrator]."); |
| 46 | } |
| 47 | }); |
| 48 | |
| 49 | connect(ui->btn_select, &QToolButton::clicked, this, [=]{ |
| 50 | auto iconPath = QFileDialog::getOpenFileName(this, "Select an icon", dirPath, "Icon Files (*.ico *.exe)"); |
| 51 | if (iconPath.isEmpty()) return; |
| 52 | addIconCandidate(iconPath); |
| 53 | int index = ui->comboBox->count() - 1; |
| 54 | ui->comboBox->setCurrentIndex(index); |
| 55 | }); |
| 56 | |
| 57 | setIcon(iconPro.icon(dirPath)); |
| 58 | |
| 59 | // 异步,否则QCombobox的宽度会不太正常(不对齐) |
| 60 | QTimer::singleShot(0, this, [=](){ |
| 61 | QDir dir(dirPath); |
| 62 | ui->label->setText(dir.isRoot() ? dirPath : dir.dirName()); |
| 63 | auto files = Util::getExeFiles(dirPath); |
| 64 | // 展示可选exe图标 |
| 65 | ui->comboBox->setUpdatesEnabled(false); |
| 66 | for (const QString& path : files) |
| 67 | addIconCandidate(path); |
| 68 | |
| 69 | // 选中当前文件夹的图标 |
| 70 | auto iconPath = Util::getFolderIconPath(dirPath); |
| 71 | iconPath = QDir::toNativeSeparators(iconPath); |
| 72 | if (!iconPath.isEmpty()) { |
| 73 | int index = ui->comboBox->findData(iconPath, Qt::UserRole, Qt::MatchFixedString); // CaseInsensitive |
| 74 | if (index == -1) |
| 75 | index = findMatchedComboTextIndex(ui->label->text()); |
| 76 | if (index != -1) |
nothing calls this directly
no outgoing calls
no test coverage detected