|
| 1 | +#include "icoconverterwidget.hpp" |
| 2 | +#include "sizeselectorwidget.hpp" |
| 3 | + |
| 4 | +#include <graphics/imageview.h> |
| 5 | +#include <utils/icoutils.hpp> |
| 6 | +#include <utils/utils.h> |
| 7 | + |
| 8 | +#include <QtWidgets> |
| 9 | + |
| 10 | +class IcoConverterWidget::IcoConverterWidgetPrivate |
| 11 | +{ |
| 12 | +public: |
| 13 | + explicit IcoConverterWidgetPrivate(IcoConverterWidget *q) |
| 14 | + : q_ptr(q) |
| 15 | + { |
| 16 | + imageView = new Graphics::ImageView(q_ptr); |
| 17 | + |
| 18 | + sizeSelectorWidget = new SizeSelectorWidget(q_ptr); |
| 19 | + sizeSelectorWidget->setSizes(Utils::defaultIcoSizes); |
| 20 | + |
| 21 | + colorLabel = new QLabel(q_ptr); |
| 22 | + colorLabel->setFrameShape(QFrame::Box); |
| 23 | + colorLabel->setAutoFillBackground(true); |
| 24 | + updateColorLabel(); |
| 25 | + |
| 26 | + pathLineEdit = new QLineEdit(q_ptr); |
| 27 | + auto path = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation); |
| 28 | + if (path.isEmpty()) { |
| 29 | + path = QDir::homePath(); |
| 30 | + } |
| 31 | + pathLineEdit->setText(path); |
| 32 | + |
| 33 | + convertButton = new QPushButton(IcoConverterWidget::tr("Convert"), q_ptr); |
| 34 | + } |
| 35 | + ~IcoConverterWidgetPrivate() {} |
| 36 | + |
| 37 | + void updateColorLabel() |
| 38 | + { |
| 39 | + auto pal = colorLabel->palette(); |
| 40 | + pal.setColor(QPalette::Window, color); |
| 41 | + pal.setColor(QPalette::WindowText, color.lightness() > 128 ? Qt::black : Qt::white); |
| 42 | + colorLabel->setPalette(pal); |
| 43 | + colorLabel->setText(color.name(QColor::HexArgb)); |
| 44 | + |
| 45 | + colorLabel->update(); |
| 46 | + } |
| 47 | + |
| 48 | + IcoConverterWidget *q_ptr; |
| 49 | + |
| 50 | + Graphics::ImageView *imageView; |
| 51 | + SizeSelectorWidget *sizeSelectorWidget; |
| 52 | + QLabel *colorLabel; |
| 53 | + QColor color = Qt::transparent; |
| 54 | + QLineEdit *pathLineEdit; |
| 55 | + QPushButton *convertButton; |
| 56 | +}; |
| 57 | + |
| 58 | +IcoConverterWidget::IcoConverterWidget(QWidget *parent) |
| 59 | + : Viewer(parent) |
| 60 | + , d_ptr(new IcoConverterWidgetPrivate(this)) |
| 61 | +{ |
| 62 | + setupUI(); |
| 63 | + buildConnect(); |
| 64 | +} |
| 65 | + |
| 66 | +IcoConverterWidget::~IcoConverterWidget() {} |
| 67 | + |
| 68 | +void IcoConverterWidget::onOpenImage() |
| 69 | +{ |
| 70 | + const auto filename = openImage(); |
| 71 | + if (filename.isEmpty()) { |
| 72 | + return; |
| 73 | + } |
| 74 | + d_ptr->imageView->createScene(filename); |
| 75 | +} |
| 76 | + |
| 77 | +void IcoConverterWidget::onChangedImage(int index) |
| 78 | +{ |
| 79 | + if (index < 0 || index >= m_thumbnailList.size()) { |
| 80 | + return; |
| 81 | + } |
| 82 | + const auto data = m_thumbnailList.at(index); |
| 83 | + d_ptr->imageView->createScene(data.fileInfo().absoluteFilePath()); |
| 84 | +} |
| 85 | + |
| 86 | +void IcoConverterWidget::onPickSquareColor() |
| 87 | +{ |
| 88 | + auto color = QColorDialog::getColor(d_ptr->color, this, tr("Select Background Color")); |
| 89 | + if (!color.isValid()) { |
| 90 | + return; |
| 91 | + } |
| 92 | + d_ptr->color = color; |
| 93 | + d_ptr->updateColorLabel(); |
| 94 | +} |
| 95 | + |
| 96 | +void IcoConverterWidget::onBrowse() |
| 97 | +{ |
| 98 | + auto dir = QFileDialog::getExistingDirectory(this, |
| 99 | + tr("Select Directory"), |
| 100 | + d_ptr->pathLineEdit->text()); |
| 101 | + if (dir.isEmpty()) { |
| 102 | + return; |
| 103 | + } |
| 104 | + d_ptr->pathLineEdit->setText(dir); |
| 105 | +} |
| 106 | + |
| 107 | +void IcoConverterWidget::onConvert() |
| 108 | +{ |
| 109 | + auto path = d_ptr->pathLineEdit->text(); |
| 110 | + if (path.isEmpty()) { |
| 111 | + QMessageBox::warning(this, tr("Warning"), tr("Please select a directory.")); |
| 112 | + return; |
| 113 | + } |
| 114 | + if (!QDir(path).exists()) { |
| 115 | + QMessageBox::warning(this, tr("Warning"), tr("Directory not exist.")); |
| 116 | + return; |
| 117 | + } |
| 118 | + auto image = d_ptr->imageView->pixmap().toImage(); |
| 119 | + if (image.isNull()) { |
| 120 | + QMessageBox::warning(this, tr("Warning"), tr("No image loaded.")); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + d_ptr->convertButton->setEnabled(false); |
| 125 | + |
| 126 | + auto filename = QString("%1_%2.ico") |
| 127 | + .arg(QFileInfo(m_urlLabel->text().trimmed()).baseName(), |
| 128 | + QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss")); |
| 129 | + path = QDir(path).filePath(filename); |
| 130 | + auto sizes = d_ptr->sizeSelectorWidget->sizes(); |
| 131 | + |
| 132 | + Utils::asynchronous<bool>([image, path, sizes, color = d_ptr->color]() { |
| 133 | + return Utils::writeIcons(image, path, color, sizes); |
| 134 | + }); |
| 135 | + QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(path).absolutePath())); |
| 136 | + |
| 137 | + d_ptr->convertButton->setEnabled(true); |
| 138 | +} |
| 139 | + |
| 140 | +void IcoConverterWidget::setupUI() |
| 141 | +{ |
| 142 | + auto *splitter = new QSplitter(Qt::Horizontal, this); |
| 143 | + splitter->addWidget(d_ptr->imageView); |
| 144 | + splitter->addWidget(toolWidget()); |
| 145 | + splitter->setStretchFactor(0, 1); |
| 146 | + splitter->setStretchFactor(1, 1); |
| 147 | + splitter->setSizes({INT_MAX, 1}); |
| 148 | + |
| 149 | + auto *layout = new QVBoxLayout(this); |
| 150 | + layout->setContentsMargins({}); |
| 151 | + layout->addWidget(splitter); |
| 152 | + layout->addWidget(m_imageListView); |
| 153 | +} |
| 154 | + |
| 155 | +auto IcoConverterWidget::toolWidget() -> QWidget * |
| 156 | +{ |
| 157 | + auto *pickColorButton = new QPushButton(tr("Pick Color"), this); |
| 158 | + connect(pickColorButton, &QPushButton::clicked, this, &IcoConverterWidget::onPickSquareColor); |
| 159 | + |
| 160 | + auto *browserButton = new QPushButton(tr("Browse"), this); |
| 161 | + connect(browserButton, &QPushButton::clicked, this, &IcoConverterWidget::onBrowse); |
| 162 | + |
| 163 | + auto *saveBox = new QGroupBox(tr("Save Options"), this); |
| 164 | + auto *saveLayout = new QVBoxLayout(saveBox); |
| 165 | + saveLayout->addWidget(d_ptr->sizeSelectorWidget); |
| 166 | + saveLayout->addWidget(new QLabel(tr("Background Color:"), saveBox)); |
| 167 | + saveLayout->addWidget(d_ptr->colorLabel); |
| 168 | + saveLayout->addWidget(pickColorButton); |
| 169 | + saveLayout->addWidget(new QLabel(tr("Save Path:"), saveBox)); |
| 170 | + saveLayout->addWidget(d_ptr->pathLineEdit); |
| 171 | + saveLayout->addWidget(browserButton); |
| 172 | + |
| 173 | + auto *widget = new QWidget(this); |
| 174 | + widget->setMaximumWidth(300); |
| 175 | + auto *rightLayout = new QVBoxLayout(widget); |
| 176 | + rightLayout->addWidget(m_openButton); |
| 177 | + rightLayout->addWidget(m_infoBox); |
| 178 | + rightLayout->addStretch(); |
| 179 | + rightLayout->addWidget(saveBox); |
| 180 | + rightLayout->addWidget(d_ptr->convertButton); |
| 181 | + rightLayout->addStretch(); |
| 182 | + |
| 183 | + return widget; |
| 184 | +} |
| 185 | + |
| 186 | +void IcoConverterWidget::buildConnect() |
| 187 | +{ |
| 188 | + connect(m_openButton, &QPushButton::clicked, this, &IcoConverterWidget::onOpenImage); |
| 189 | + |
| 190 | + connect(d_ptr->imageView, |
| 191 | + &Graphics::ImageView::scaleFactorChanged, |
| 192 | + this, |
| 193 | + &IcoConverterWidget::onScaleFactorChanged); |
| 194 | + connect(d_ptr->imageView, |
| 195 | + &Graphics::ImageView::imageSizeChanged, |
| 196 | + this, |
| 197 | + &IcoConverterWidget::onImageSizeChanged); |
| 198 | + connect(d_ptr->imageView, |
| 199 | + &Graphics::ImageView::imageUrlChanged, |
| 200 | + this, |
| 201 | + &IcoConverterWidget::onImageChanged); |
| 202 | + connect(m_imageListView, &ImageListView::changeItem, this, &IcoConverterWidget::onChangedImage); |
| 203 | + |
| 204 | + connect(d_ptr->convertButton, &QPushButton::clicked, this, &IcoConverterWidget::onConvert); |
| 205 | +} |
0 commit comments