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 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2008-03-13
* Description : Image files selection dialog - Improved Qt based file dialog.
*
* SPDX-FileCopyrightText: 2008-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "imagedialog_p.h"
namespace Digikam
{
ImageDialog::ImageDialog(QWidget* const parent, const QUrl& url,
bool singleSelect, const QString& caption)
: QObject(parent),
d (new Private)
{
QString all;
d->fileFormats = supportedImageMimeTypes(QIODevice::ReadOnly, all);
qCDebug(DIGIKAM_GENERAL_LOG) << "file formats=" << d->fileFormats;
d->toolTip = new ImageDialogToolTip();
d->toolTipTimer = new QTimer(this);
connect(d->toolTipTimer, SIGNAL(timeout()),
this, SLOT(slotToolTip()));
d->dlg = new DFileDialog(parent);
d->dlg->setWindowTitle(caption);
d->dlg->setDirectoryUrl(url);
d->dlg->setNameFilters(d->fileFormats);
d->dlg->selectNameFilter(d->fileFormats.last());
d->dlg->setAcceptMode(QFileDialog::AcceptOpen);
d->dlg->setFileMode(singleSelect ? QFileDialog::ExistingFile
: QFileDialog::ExistingFiles);
if ((d->dlg->options() & QFileDialog::DontUseNativeDialog) || !isRunningOnNativeKDE())
{
d->provider = new ImageDialogIconProvider();
d->dlg->setIconProvider(d->provider);
connect(d->provider, &ImageDialogIconProvider::signalThumbnailRefresh,
this, [this]()
{
d->dlg->setIconProvider(d->provider);
}
);
const auto items = d->dlg->findChildren<QAbstractItemView*>();
for (auto* item : items)
{
qCDebug(DIGIKAM_GENERAL_LOG) << item;
item->installEventFilter(this);
item->setMouseTracking(true);
}
}
d->dlg->exec();
if (d->dlg && d->dlg->hasAcceptedUrls())
{
d->urls = d->dlg->selectedUrls();
}
}
ImageDialog::~ImageDialog()
{
delete d->toolTip;
delete d->dlg;
delete d->provider;
delete d;
}
void ImageDialog::setEnableToolTips(bool val)
{
d->showToolTips = val;
if (!val)
{
hideToolTip();
}
}
void ImageDialog::hideToolTip()
{
d->toolTipIndex = QModelIndex();
d->toolTipTimer->stop();
slotToolTip();
}
void ImageDialog::slotToolTip()
{
d->toolTip->setData(d->toolTipView, d->toolTipIndex, d->toolTipUrl);
}
bool ImageDialog::acceptToolTip(const QUrl& url) const
{
if (url.isValid())
{
QFileInfo info(url.toLocalFile());
if (info.isFile() && !info.isSymLink() && !info.isRoot())
{
return (!info.absoluteFilePath().isEmpty());
}
}
return false;
}
bool ImageDialog::eventFilter(QObject* obj, QEvent* ev)
{
if (d->dlg)
{
QAbstractItemView* const view = dynamic_cast<QAbstractItemView*>(obj);
if (view)
{
if ((ev->type() == QEvent::HoverMove) && (qApp->mouseButtons() == Qt::NoButton))
{
QHoverEvent* const hev = dynamic_cast<QHoverEvent*>(ev);
if (hev)
{
QModelIndex index = view->indexAt(view->viewport()->mapFromGlobal(QCursor::pos()));
if (index.isValid())
{
QString name = index.data(Qt::DisplayRole).toString();
if (!name.isEmpty())
{
QUrl url = QUrl::fromLocalFile(QDir::fromNativeSeparators(d->dlg->directoryUrl().toLocalFile() +<--- Shadow variable
QLatin1Char('/') + name));
if (d->showToolTips)
{
if (!d->dlg->isActiveWindow())
{
hideToolTip();
return false;
}
if (index != d->toolTipIndex)
{
hideToolTip();
if (acceptToolTip(url))
{
d->toolTipView = view;
d->toolTipIndex = index;
d->toolTipUrl = url;
d->toolTipTimer->setSingleShot(true);
d->toolTipTimer->start(500);
}
}
if ((index == d->toolTipIndex) && !acceptToolTip(url))
{
hideToolTip();
}
}
return false;
}
}
else
{
hideToolTip();
return false;
}
}
}
else if (
(ev->type() == QEvent::HoverLeave) ||
(ev->type() == QEvent::FocusOut) ||
(ev->type() == QEvent::Wheel) ||
(ev->type() == QEvent::KeyPress) ||
(ev->type() == QEvent::Paint)
)
{
hideToolTip();
return false;
}
}
}
// Pass the event on to the parent class.
return QObject::eventFilter(obj, ev);
}
QStringList ImageDialog::fileFormats() const
{
return d->fileFormats;
}
QUrl ImageDialog::url() const
{
if (d->urls.isEmpty())
{
return QUrl();
}
return d->urls.first();
}
QList<QUrl> ImageDialog::urls() const
{
return d->urls;
}
QUrl ImageDialog::getImageURL(QWidget* const parent, const QUrl& url, const QString& caption)
{
ImageDialog dlg(parent, url, true, caption.isEmpty() ? i18n("Select an Item") : caption);
if (dlg.url() != QUrl())
{
return dlg.url();
}
else
{
return QUrl();
}
}
QList<QUrl> ImageDialog::getImageURLs(QWidget* const parent, const QUrl& url, const QString& caption)
{
ImageDialog dlg(parent, url, false, caption.isEmpty() ? i18n("Select Items") : caption);
if (!dlg.urls().isEmpty())
{
return dlg.urls();
}
else
{
return QList<QUrl>();
}
}
} // namespace Digikam
#include "moc_imagedialog.cpp"
|