-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclickableimagelabel.cpp
261 lines (190 loc) · 7.04 KB
/
clickableimagelabel.cpp
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
#include "ClickableImageLabel.h"
#include <QGraphicsPixmapItem>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QDesktopServices>
#include <QUrl>
#include <QDir>
#include <QMenu>
#include <QAction>
#include <QContextMenuEvent>
#include <QProcess>
#include <QMimeData>
#include <QImageReader>
ClickableImageLabel::ClickableImageLabel(QWidget *parent) : QLabel(parent) {
actSendToImg2Img = new QAction("Send to img2img",this);
actSendToInpaint = new QAction("Send to inpaint",this);
actSendToUpscale = new QAction("Send to upscale",this);
actSendToFavorites = new QAction("Save to favorites",this);
connect(actSendToImg2Img, &QAction::triggered, this, &ClickableImageLabel::OnClickSendToImg2Img);
connect(actSendToInpaint, &QAction::triggered, this, &ClickableImageLabel::OnClickSendToInpaint);
connect(actSendToUpscale, &QAction::triggered, this, &ClickableImageLabel::OnClickSendToUpscale);
connect(actSendToFavorites, &QAction::triggered, this, &ClickableImageLabel::Favorite);
}
ClickableImageLabel::~ClickableImageLabel()
{
}
void ClickableImageLabel::loadImage(const QString &imagePath)
{
OriginalFilePath = imagePath;
SetImage(new QImage(imagePath), &OriginalFilePath, true);
}
void ClickableImageLabel::SetImage(QImage* Img, QString* Path, bool TransferOwnership)
{
OriginalImage = maybe_ptr<QImage>(Img, TransferOwnership);
setPixmap(
QPixmap::fromImage(OriginalImage->scaled(PreviewSize, Qt::KeepAspectRatio, Qt::SmoothTransformation))
);
if (Path)
OriginalFilePath = *Path;
else
OriginalFilePath = "";
}
void ClickableImageLabel::ResetImage()
{
if (OriginalImage)
OriginalImage.reset();
OriginalFilePath = "";
QPixmap EmptyFill(PreviewSize);
EmptyFill.fill(Qt::white);
setPixmap(EmptyFill);
}
void ClickableImageLabel::SetImagePreview(QImage &Img)
{
OriginalImage.reset();
setPixmap(
QPixmap::fromImage(Img.scaled(PreviewSize, Qt::KeepAspectRatio, Qt::SmoothTransformation))
);
}
QSize ClickableImageLabel::getPreviewSize() const
{
return PreviewSize;
}
void ClickableImageLabel::setPreviewSize(const QSize &newPreviewSize)
{
PreviewSize = newPreviewSize;
if (OriginalImage)
setPixmap(
QPixmap::fromImage(OriginalImage->scaled(PreviewSize, Qt::KeepAspectRatio, Qt::SmoothTransformation))
);
else
ResetImage();
}
void ClickableImageLabel::mousePressEvent(QMouseEvent* event) {
QLabel::mousePressEvent(event); // Call the base class implementation
if (!OriginalImage)
return;
if (event->button() != Qt::MouseButton::LeftButton)
return;
if (OriginalImage->size().width() > 1024 && OriginalImage->size().height() > 1024)
{
// Open the image in windows image viewer
QDesktopServices::openUrl(QUrl::fromLocalFile(OriginalFilePath));
return;
}
// Create and show the dialog when the label is clicked
QDialog *dialog = new QDialog(this);
dialog->setWindowTitle("Image Preview");
QVBoxLayout *layout = new QVBoxLayout(dialog);
QLabel *imageLabel = new QLabel(dialog);
imageLabel->setPixmap(QPixmap::fromImage(*OriginalImage));
layout->addWidget(imageLabel);
dialog->resize(OriginalImage->size());
dialog->exec(); // Show the dialog modally
}
void ClickableImageLabel::contextMenuEvent(QContextMenuEvent *event) {
QLabel::contextMenuEvent(event);
if (OriginalFilePath.isEmpty())
return;
QMenu contextMenu(this);
QAction action("Show in folder", this);
connect(&action, &QAction::triggered, this, &ClickableImageLabel::showInFolder);
contextMenu.addAction(&action);
// emit OnMenuOpen(&contextMenu);
if (!isUpscaleResult)
{
contextMenu.addAction(actSendToImg2Img);
contextMenu.addAction(actSendToInpaint);
contextMenu.addAction(actSendToUpscale);
contextMenu.addAction(actSendToFavorites);
}
contextMenu.exec(event->globalPos());
}
void ClickableImageLabel::showInFolder() {
if (!OriginalFilePath.isEmpty()) {
// Ensure the path is in Windows format (backslashes)
QString winPath = QDir::toNativeSeparators(OriginalFilePath);
QStringList args;
args << "/select," << winPath;
QProcess::startDetached("explorer", args);
}
}
void ClickableImageLabel::Favorite() {
QString FavesDir = QCoreApplication::applicationDirPath() + "/favorites/";
// Ensure the favorites directory exists; create it if it doesn't
QDir dir(FavesDir);
if (!dir.exists()) {
dir.mkpath(FavesDir);
}
if (!OriginalFilePath.isEmpty()) {
// Construct the base filename for the new favorite file
QString baseFileName = "fave_";
QString fileExtension = ".png";
// Find the lowest number that can be used without overwriting an existing file
int num = 1;
QString newFileName;
do {
newFileName = baseFileName + QString::number(num++) + fileExtension;
} while (dir.exists(newFileName));
// Construct the full new file path
QString newFilePath = FavesDir + newFileName;
// Copy the original image file to the new favorite file path
bool success = QFile::copy(OriginalFilePath, newFilePath);
if (!success) {
qDebug() << "Could not copy the file to the favorites directory: " << newFilePath;
}
} else {
qDebug() << "Original file path is not set.";
}
}
void ClickableImageLabel::OnClickSendToImg2Img()
{
emit SendImageToImg2Img(OriginalImage.get());
}
void ClickableImageLabel::OnClickSendToInpaint()
{
emit SendImageToInpaint(OriginalImage.get());
}
void ClickableImageLabel::OnClickSendToUpscale()
{
emit SendImageToUpscale(OriginalImage.get(), false);
}
void ClickableImageLabel::dragEnterEvent(QDragEnterEvent *event) {
// Check if event contains URLs and accept it if there's at least one image file
if (event->mimeData()->hasUrls() && !event->mimeData()->urls().isEmpty()) {
QList<QUrl> urls = event->mimeData()->urls();
for (const QUrl &url : urls) {
QFileInfo fileInfo(url.toLocalFile());
if (fileInfo.exists() && fileInfo.isFile() && QImageReader::imageFormat(fileInfo.filePath()).isEmpty() == false) {
event->acceptProposedAction();
return;
}
}
}
}
void ClickableImageLabel::dropEvent(QDropEvent *event) {
// Process the first valid image URL from the dropped items
const QMimeData *mimeData = event->mimeData();
if (mimeData->hasUrls()) {
QList<QUrl> urls = mimeData->urls();
for (const QUrl &url : urls) {
QString filePath = url.toLocalFile();
QFileInfo fileInfo(filePath);
if (fileInfo.exists() && fileInfo.isFile() && QImageReader::imageFormat(filePath).isEmpty() == false) {
loadImage(filePath); // Assuming loadImage() loads and displays the image
event->acceptProposedAction();
return;
}
}
}
}