Debian Codegraph @

core/libs/album/treeview/abstractcheckablealbumtreeview.cpp

raw
1 /* ============================================================
2 *
3 * This file is a part of digiKam project
4 * https://www.digikam.org
5 *
6 * Date : 2009-03-25
7 * Description : Tree View for album models
8 *
9 * SPDX-FileCopyrightText: 2009-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
10 * SPDX-FileCopyrightText: 2010-2011 by Andi Clemens <andi dot clemens at gmail dot com>
11 * SPDX-FileCopyrightText: 2014 by Mohamed_Anwer <m_dot_anwer at gmx dot com>
12 * SPDX-FileCopyrightText: 2014 by Michael G. Hansen <mike at mghansen dot de>
13 * SPDX-FileCopyrightText: 2009-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
14 *
15 * SPDX-License-Identifier: GPL-2.0-or-later
16 *
17 * ============================================================ */
18
19 #include "abstractcheckablealbumtreeview.h"
20
21 // KDE includes
22
23 #include <kconfiggroup.h>
24
25 // Local includes
26
27 #include "abstractalbumtreeview_p.h"
28
29 namespace Digikam
30 {
31
32 class Q_DECL_HIDDEN AbstractCheckableAlbumTreeView::Private
33 {
34 public:
35
36 Private() = default;
37
38 public:
39
40 const QString configCheckedAlbumsEntry = QLatin1String("Checked");
41 const QString configPartiallyCheckedAlbumsEntry = QLatin1String("PartiallyChecked");
42 const QString configRestoreCheckedEntry = QLatin1String("RestoreChecked");
43
44 QList<int> checkedAlbumIds;
45 QList<int> partiallyCheckedAlbumIds;
46 };
47
48 // --------------------------------------------------------
49
50 AbstractCheckableAlbumTreeView::AbstractCheckableAlbumTreeView(QWidget* const parent, Flags flags)
51 : AbstractCountingAlbumTreeView(parent, flags & ~CreateDefaultFilterModel),
52 d (new Private)
53 {
54 m_checkOnMiddleClick = true;
55 m_restoreCheckState = false;
56
57 if (flags & CreateDefaultFilterModel)
58 {
59 setAlbumFilterModel(new CheckableAlbumFilterModel(this));
60 }
61 }
62
63 AbstractCheckableAlbumTreeView::~AbstractCheckableAlbumTreeView()
64 {
65 delete d;
66 }
67
68 AbstractCheckableAlbumModel* AbstractCheckableAlbumTreeView::checkableAlbumModel() const
69 {
70 return dynamic_cast<AbstractCheckableAlbumModel*>(m_albumModel);
71 }
72
73 CheckableAlbumFilterModel* AbstractCheckableAlbumTreeView::checkableAlbumFilterModel() const
74 {
75 return dynamic_cast<CheckableAlbumFilterModel*> (m_albumFilterModel);
76 }
77
78 void AbstractCheckableAlbumTreeView::setAlbumModel(AbstractCheckableAlbumModel* const model)
79 {
80 AbstractCountingAlbumTreeView::setAlbumModel(model);
81 }
82
83 void AbstractCheckableAlbumTreeView::setCheckableAlbumFilterModel(CheckableAlbumFilterModel* const filterModel)
84 {
85 AbstractCountingAlbumTreeView::setAlbumFilterModel(filterModel);
86 }
87
88 void AbstractCheckableAlbumTreeView::setCheckOnMiddleClick(bool doThat)
89 {
90 m_checkOnMiddleClick = doThat;
91 }
92
93 void AbstractCheckableAlbumTreeView::middleButtonPressed(Album* a)
94 {
95 AbstractCheckableAlbumModel* const model = static_cast<AbstractCheckableAlbumModel*>(m_albumModel);
96
97 if (!model)
98 {
99 return;
100 }
101
102 if (model->isCheckable())
103 {
104 if (model->isTristate())
105 {
106 switch (model->checkState(a))
107 {
108 case Qt::Unchecked:
109 {
110 model->setCheckState(a, Qt::PartiallyChecked);
111 break;
112 }
113
114 case Qt::PartiallyChecked:
115 {
116 model->setCheckState(a, Qt::Checked);
117 break;
118 }
119
120 case Qt::Checked:
121 {
122 model->setCheckState(a, Qt::Unchecked);
123 break;
124 }
125 }
126 }
127 else
128 {
129 model->toggleChecked(a);
130 }
131 }
132 }
133
134 bool AbstractCheckableAlbumTreeView::isRestoreCheckState() const
135 {
136 return m_restoreCheckState;
137 }
138
139 void AbstractCheckableAlbumTreeView::setRestoreCheckState(bool restore)
140 {
141 m_restoreCheckState = restore;
142 }
143
144 void AbstractCheckableAlbumTreeView::doLoadState()
145 {
146 AbstractCountingAlbumTreeView::doLoadState();
147
148 KConfigGroup group = getConfigGroup();
149
150 if (!m_restoreCheckState)
151 {
152 m_restoreCheckState = group.readEntry(entryName(d->configRestoreCheckedEntry), false);
153 }
154
155 if (!m_restoreCheckState || !checkableAlbumModel()->isCheckable())
156 {
157 return;
158 }
159
160 const QStringList checkedAlbums = group.readEntry(entryName(d->configCheckedAlbumsEntry), QStringList());
161
162 d->checkedAlbumIds.clear();
163
164 for (const QString& albumId : std::as_const(checkedAlbums))
165 {
166 bool ok;
167 const int id = albumId.toInt(&ok);
168
169 if (ok)
170 {
171 d->checkedAlbumIds << id;
172 }
173 }
174
175 const QStringList partiallyCheckedAlbums = group.readEntry(entryName(d->configPartiallyCheckedAlbumsEntry), QStringList());
176 d->partiallyCheckedAlbumIds.clear();
177
178 for (const QString& albumId : std::as_const(partiallyCheckedAlbums))
179 {
180 bool ok;
181 const int id = albumId.toInt(&ok);
182
183 if (ok)
184 {
185 d->partiallyCheckedAlbumIds << id;
186 }
187 }
188
189 // initially sync with the albums that are already in the model
190
191 restoreCheckStateForHierarchy(QModelIndex());
192 horizontalScrollBar()->setValue(0);
193 }
194
195 void AbstractCheckableAlbumTreeView::rowsInserted(const QModelIndex& parent, int start, int end)
196 {
197 AbstractCountingAlbumTreeView::rowsInserted(parent, start, end);
198
199 if (!d->checkedAlbumIds.isEmpty())
200 {
201 for (int i = start ; i <= end ; ++i)
202 {
203 const QModelIndex child = checkableAlbumModel()->index(i, 0, parent);
204 restoreCheckState(child);
205 }
206 }
207 }
208
209 void AbstractCheckableAlbumTreeView::restoreCheckStateForHierarchy(const QModelIndex& index)
210 {
211 // recurse children
212
213 for (int i = 0 ; i < checkableAlbumModel()->rowCount(index) ; ++i)
214 {
215 const QModelIndex child = checkableAlbumModel()->index(i, 0, index);
216 restoreCheckState(child);
217 restoreCheckStateForHierarchy(child);
218 }
219 }
220
221 void AbstractCheckableAlbumTreeView::restoreCheckState(const QModelIndex& index)
222 {
223 Album* const album = checkableAlbumModel()->albumForIndex(index);
224
225 if (!album || !(album->id()))
226 {
227 return;
228 }
229
230 if (d->checkedAlbumIds.contains(album->id()))
231 {
232 checkableAlbumModel()->setCheckState(album, Qt::Checked);
233 d->checkedAlbumIds.removeOne(album->id());
234 }
235
236 if (d->partiallyCheckedAlbumIds.contains(album->id()))
237 {
238 checkableAlbumModel()->setCheckState(album, Qt::PartiallyChecked);
239 d->partiallyCheckedAlbumIds.removeOne(album->id());
240 }
241 }
242
243 void AbstractCheckableAlbumTreeView::doSaveState()
244 {
245 AbstractCountingAlbumTreeView::doSaveState();
246
247 KConfigGroup group = getConfigGroup();
248
249 group.writeEntry(entryName(d->configRestoreCheckedEntry), m_restoreCheckState);
250
251 if (!m_restoreCheckState || !checkableAlbumModel()->isCheckable())
252 {
253 return;
254 }
255
256 const QList<Album*> checkedAlbums = checkableAlbumModel()->checkedAlbums();
257 QStringList checkedIds;
258
259 for (const Album* const album : std::as_const(checkedAlbums))
260 {
261 checkedIds << QString::number(album->id());
262 }
263
264 group.writeEntry(entryName(d->configCheckedAlbumsEntry), checkedIds);
265
266 if (!checkableAlbumModel()->isTristate())
267 {
268 return;
269 }
270
271 const QList<Album*> partiallyCheckedAlbums = checkableAlbumModel()->partiallyCheckedAlbums();
272 QStringList partiallyCheckedIds;
273
274 for (const Album* const album : std::as_const(partiallyCheckedAlbums))
275 {
276 partiallyCheckedIds << QString::number(album->id());
277 }
278
279 group.writeEntry(entryName(d->configPartiallyCheckedAlbumsEntry), partiallyCheckedIds);
280 }
281
282 } // namespace Digikam
283
284 #include "moc_abstractcheckablealbumtreeview.cpp"