Debian Codegraph @

core/libs/album/widgets/albumselectwidget.cpp

raw
1 /* ============================================================
2 *
3 * This file is a part of digiKam project
4 * https://www.digikam.org
5 *
6 * Date : 2009-26-02
7 * Description : a widget to select a physical album
8 *
9 * SPDX-FileCopyrightText: 2009-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
10 * SPDX-FileCopyrightText: 2009-2010 by Johannes Wienke <languitar at semipol dot de>
11 *
12 * SPDX-License-Identifier: GPL-2.0-or-later
13 *
14 * ============================================================ */
15
16 #include "albumselectwidget.h"
17
18 // Qt includes
19
20 #include <QIcon>
21 #include <QGridLayout>
22 #include <QApplication>
23 #include <QStyle>
24 #include <QPushButton>
25 #include <QAction>
26 #include <QTimer>
27
28 // KDE includes
29
30 #include <kconfiggroup.h>
31 #include <klocalizedstring.h>
32 #include <ksharedconfig.h>
33
34 // Local includes
35
36 #include "digikam_globals.h"
37 #include "digikam_debug.h"
38 #include "album.h"
39 #include "albummodificationhelper.h"
40 #include "albumthumbnailloader.h"
41 #include "collectionmanager.h"
42 #include "contextmenuhelper.h"
43
44 namespace Digikam
45 {
46
47 class Q_DECL_HIDDEN AlbumSelectTreeView::Private
48 {
49 public:
50
51 Private() = default;
52
53 public:
54
55 AlbumModificationHelper* albumModificationHelper = nullptr;
56 QAction* newAlbumAction = nullptr;
57 };
58
59 AlbumSelectTreeView::AlbumSelectTreeView(AlbumModel* const model,
60 AlbumModificationHelper* const albumModificationHelper,
61 QWidget* const parent)
62 : AlbumTreeView(parent),
63 d (new Private)
64 {
65 setPAlbumModel(model);
66 d->albumModificationHelper = albumModificationHelper;
67 d->newAlbumAction = new QAction(QIcon::fromTheme(QLatin1String("folder-new")),
68 i18n("Create New Album"), this);
69 }
70
71 AlbumSelectTreeView::~AlbumSelectTreeView()
72 {
73 delete d;
74 }
75
76 void AlbumSelectTreeView::addCustomContextMenuActions(ContextMenuHelper& cmh, Album* album)
77 {
78 cmh.addAction(d->newAlbumAction);
79 d->newAlbumAction->setEnabled(album);
80 }
81
82 void AlbumSelectTreeView::handleCustomContextMenuAction(QAction* action, const AlbumPointer<Album>& album)
83 {
84 Album* const a = album;
85 PAlbum* const palbum = dynamic_cast<PAlbum*>(a);
86
87 if (palbum && (action == d->newAlbumAction))
88 {
89 d->albumModificationHelper->slotAlbumNew(palbum);
90 }
91 }
92
93 void AlbumSelectTreeView::slotNewAlbum()
94 {
95 PAlbum* const palbum = currentPAlbum();
96
97 if (palbum)
98 {
99 PAlbum* const createdAlbum = d->albumModificationHelper->slotAlbumNew(palbum); // cppcheck-suppress constVariablePointer
100
101 if (createdAlbum)
102 {
103 setCurrentAlbums(QList<Album*>() << createdAlbum, false);
104 }
105 }
106 }
107
108 // --------------------------------------------------------------------------------------------------------
109
110 class Q_DECL_HIDDEN AlbumSelectWidget::Private
111 {
112 public:
113
114 Private() = default;
115
116 public:
117
118 PAlbum* complAlbum = nullptr;
119 AlbumModel* albumModel = nullptr;
120 AlbumSelectTreeView* albumTreeView = nullptr;
121
122 AlbumModificationHelper* albumModificationHelper = nullptr;
123
124 SearchTextBarDb* searchBar = nullptr;
125
126 QPushButton* newAlbumBtn = nullptr;
127 };
128
129 AlbumSelectWidget::AlbumSelectWidget(QWidget* const parent,
130 PAlbum* const albumToSelect, bool completerSelect)
131 : QWidget(parent),
132 d (new Private)
133 {
134 setObjectName(QLatin1String("AlbumSelectWidget"));
135
136 d->albumModificationHelper = new AlbumModificationHelper(this, this);
137
138 // TODO let this class implement StateSavingObject
139
140 KConfigGroup group = KSharedConfig::openConfig()->group(objectName());
141
142 QGridLayout* const grid = new QGridLayout(this);
143 d->albumModel = new AlbumModel(AbstractAlbumModel::IgnoreRootAlbum, this);
144 d->albumTreeView = new AlbumSelectTreeView(d->albumModel, d->albumModificationHelper, this);
145 d->albumTreeView->setDragEnabled(false);
146 d->albumTreeView->setRootIsDecorated(true);
147 d->albumTreeView->setDropIndicatorShown(false);
148 d->albumTreeView->setAcceptDrops(false);
149 d->albumTreeView->setSelectAlbumOnClick(false);
150 d->albumTreeView->setSelectOnContextMenu(false);
151 d->albumTreeView->setEnableContextMenu(true);
152 d->albumTreeView->setSortingEnabled(true);
153 d->albumTreeView->setConfigGroup(group);
154 d->albumTreeView->setEntryPrefix(QLatin1String("AlbumTreeView"));
155
156 d->searchBar = new SearchTextBarDb(this, QLatin1String("AlbumSelectWidgetSearchBar"));
157 d->searchBar->setModel(d->albumModel, AbstractAlbumModel::AlbumIdRole, AbstractAlbumModel::AlbumTitleRole);
158 d->searchBar->setFilterModel(d->albumTreeView->albumFilterModel());
159 d->searchBar->setConfigGroup(group);
160 d->albumTreeView->setEntryPrefix(QLatin1String("AlbumTreeView"));
161
162 d->newAlbumBtn = new QPushButton(i18n("&New Album"), this);
163 d->newAlbumBtn->setToolTip(i18n("Create new album"));
164 d->newAlbumBtn->setIcon(QIcon::fromTheme(QLatin1String("folder-new")));
165
166 grid->addWidget(d->albumTreeView, 0, 0, 1, 2);
167 grid->addWidget(d->searchBar, 1, 0, 1, 1);
168 grid->addWidget(d->newAlbumBtn, 1, 1, 1, 1);
169 grid->setRowStretch(0, 10);
170 grid->setContentsMargins(QMargins());
171 grid->setSpacing(layoutSpacing());
172
173 // ------------------------------------------------------------------------------------
174
175 d->albumTreeView->loadState();
176 d->searchBar->loadState();
177
178 // ------------------------------------------------------------------------------------
179
180 PAlbum* select = albumToSelect; // cppcheck-suppress constVariablePointer
181
182 if (!select)
183 {
184 select = AlbumManager::instance()->currentPAlbum();
185 }
186
187 d->albumTreeView->setCurrentAlbums(QList<Album*>() << select, false);
188
189 bool enabled = !(!currentAlbum() || currentAlbum()->isRoot());
190 d->newAlbumBtn->setEnabled(enabled);
191
192 // ------------------------------------------------------------------------------------
193
194 connect(d->albumTreeView, SIGNAL(currentAlbumChanged(Album*)),
195 this, SLOT(slotSelectionChanged()));
196
197 connect(AlbumManager::instance(), SIGNAL(signalAlbumRenamed(Album*)),
198 this, SLOT(slotAlbumRenamed(Album*)));
199
200 if (completerSelect)
201 {
202 connect(d->searchBar, SIGNAL(completerHighlighted(int)),
203 this, SLOT(slotCompleterHighlighted(int)));
204
205 connect(d->searchBar, SIGNAL(completerActivated()),
206 this, SIGNAL(completerActivated()));
207 }
208
209 connect(d->newAlbumBtn, SIGNAL(clicked()),
210 d->albumTreeView, SLOT(slotNewAlbum()));
211
212 if (completerSelect)
213 {
214 d->searchBar->setFocus();
215 }
216 }
217
218 AlbumSelectWidget::~AlbumSelectWidget()
219 {
220 d->albumTreeView->saveState();
221 d->searchBar->saveState();
222 delete d;
223 }
224
225 PAlbum* AlbumSelectWidget::currentAlbum() const
226 {
227 return d->albumTreeView->currentPAlbum();
228 }
229
230 void AlbumSelectWidget::setCurrentAlbum(PAlbum* const albumToSelect)
231 {
232 d->albumTreeView->setCurrentAlbums(QList<Album*>() << albumToSelect);
233 }
234
235 QUrl AlbumSelectWidget::currentAlbumUrl() const
236 {
237 const PAlbum* const palbum = d->albumTreeView->currentPAlbum();
238
239 if (palbum)
240 {
241 return palbum->fileUrl();
242 }
243
244 return QUrl();
245 }
246
247 void AlbumSelectWidget::setCurrentAlbumUrl(const QUrl& albumUrl)
248 {
249 PAlbum* const urlAlbum = AlbumManager::instance()->findPAlbum(albumUrl); // cppcheck-suppress constVariablePointer
250
251 if (urlAlbum)
252 {
253 d->albumTreeView->setCurrentAlbums(QList<Album*>() << urlAlbum);
254 }
255 else
256 {
257 qCDebug(DIGIKAM_GENERAL_LOG) << "Cannot find an album for " << albumUrl;
258 }
259 }
260
261 void AlbumSelectWidget::slotAlbumRenamed(Album* album)
262 {
263 if (!album || (album->type() != Album::PHYSICAL))
264 {
265 return;
266 }
267
268 QModelIndex index = d->albumModel->indexForAlbum(album);
269
270 if (index.isValid())
271 {
272 slotSelectionChanged();
273 }
274 }
275
276 void AlbumSelectWidget::slotCompleterHighlighted(int albumId)
277 {
278 PAlbum* const album = AlbumManager::instance()->findPAlbum(albumId);
279
280 if (album)
281 {
282 d->complAlbum = album;
283 QTimer::singleShot(0, this, SLOT(slotCompleterTimer()));
284 }
285 }
286
287 void AlbumSelectWidget::slotCompleterTimer()
288 {
289 if (d->complAlbum)
290 {
291 d->albumTreeView->setCurrentAlbums(QList<Album*>() << d->complAlbum);
292 }
293 }
294
295 void AlbumSelectWidget::slotSelectionChanged()
296 {
297 bool enabled = !(!currentAlbum() || currentAlbum()->isRoot());
298 d->newAlbumBtn->setEnabled(enabled);
299
300 Q_EMIT itemSelectionChanged();
301 }
302
303 } // namespace Digikam
304
305 #include "moc_albumselectwidget.cpp"