1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date        : 20013-08-22
7  * Description : List View Item for List View Model
8  *
9  * Copyright (C) 2013 by Veaceslav Munteanu <veaceslav dot munteanu90 at gmail dot com>
10  *
11  * This program is free software; you can redistribute it
12  * and/or modify it under the terms of the GNU General
13  * Public License as published by the Free Software Foundation;
14  * either version 2, or (at your option)
15  * any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * ============================================================ */
23 
24 #include "tagmngrlistitem.h"
25 
26 // KDE includes
27 
28 #include <klocalizedstring.h>
29 
30 // Local includes
31 
32 #include "digikam_debug.h"
33 #include "albummanager.h"
34 #include "album.h"
35 
36 namespace Digikam
37 {
38 
39 class Q_DECL_HIDDEN ListItem::Private
40 {
41 public:
42 
Private()43     explicit Private()
44       : parentItem(nullptr)
45     {
46     }
47 
48     QList<ListItem*> childItems;
49     QList<ListItem*> toDelItems;
50     QList<QVariant>  itemData;
51     QList<int>       tagIds;
52     ListItem*        parentItem;
53 };
54 
ListItem(QList<QVariant> & data,ListItem * const parent)55 ListItem::ListItem(QList<QVariant>& data, ListItem* const parent)
56     : d(new Private())
57 {
58     d->parentItem = parent;
59     d->itemData.append(data);
60 
61     data.removeFirst();
62 
63     foreach (const QVariant& val, data)
64     {
65         d->tagIds.append(val.toInt());
66     }
67 }
68 
~ListItem()69 ListItem::~ListItem()
70 {
71     qDeleteAll(d->childItems);
72     qDeleteAll(d->toDelItems);
73 
74     delete d;
75 }
76 
deleteChild(ListItem * const item)77 void ListItem::deleteChild(ListItem* const item)
78 {
79     int row = d->childItems.indexOf(item);
80 
81     if (row != -1)
82     {
83         deleteChild(row);
84     }
85 }
86 
allChildren() const87 QList<ListItem*> ListItem::allChildren() const
88 {
89     return d->childItems;
90 }
91 
getTagIds() const92 QList<int> ListItem::getTagIds() const
93 {
94     return d->tagIds;
95 }
96 
appendChild(ListItem * const item)97 void ListItem::appendChild(ListItem* const item)
98 {
99     d->childItems.append(item);
100 }
101 
removeTagId(int tagId)102 void ListItem::removeTagId(int tagId)
103 {
104     d->tagIds.removeOne(tagId);
105 }
106 
child(int row) const107 ListItem* ListItem::child(int row) const
108 {
109     return d->childItems.value(row);
110 }
111 
childCount() const112 int ListItem::childCount() const
113 {
114     return d->childItems.count();
115 }
116 
deleteChild(int row)117 void ListItem::deleteChild(int row)
118 {
119     d->toDelItems << d->childItems.takeAt(row);
120 }
121 
removeAll()122 void ListItem::removeAll()
123 {
124     d->toDelItems << d->childItems;
125     d->childItems.clear();
126 }
127 
appendList(const QList<ListItem * > & items)128 void ListItem::appendList(const QList<ListItem*>& items)
129 {
130     d->childItems.append(items);
131 }
132 
columnCount() const133 int ListItem::columnCount() const
134 {
135     return d->itemData.count();
136 }
137 
data(int role) const138 QVariant ListItem::data(int role) const
139 {
140     switch (role)
141     {
142         case Qt::DisplayRole:
143         case Qt::ToolTipRole:
144         {
145             QString display;
146 
147             foreach (int tagId, d->tagIds)
148             {
149                 TAlbum* const album = AlbumManager::instance()->findTAlbum(tagId);
150 
151                 if (!album)
152                 {
153                     continue;
154                 }
155 
156                 display.append(album->title()+ QLatin1String(", "));
157 
158                 if (role == Qt::DisplayRole && display.size() > 30)
159                     break;
160             }
161 
162             if (display.isEmpty())
163             {
164                 display.append(i18nc("@info", "All Tags"));
165             }
166             else
167             {
168                 display.remove(display.size()-2, 2);
169             }
170 
171             return QVariant(display);
172         }
173 
174         default:
175         {
176             return QVariant();
177         }
178     }
179 }
180 
setData(const QList<QVariant> & data)181 void ListItem::setData(const QList<QVariant>& data)
182 {
183     d->itemData = data;
184 }
185 
parent() const186 ListItem* ListItem::parent() const
187 {
188     return d->parentItem;
189 }
190 
row() const191 int ListItem::row() const
192 {
193     if (d->parentItem)
194     {
195         return d->parentItem->allChildren().indexOf(const_cast<ListItem*>(this));
196     }
197 
198     return 0;
199 }
200 
containsItem(ListItem * const item) const201 ListItem* ListItem::containsItem(ListItem* const item) const
202 {
203     // We need to compare items and not pointers
204 
205     for (int it = 0 ; it < d->childItems.size() ; ++it)
206     {
207         if (item->equal(d->childItems.at(it)))
208         {
209             return d->childItems.at(it);
210         }
211     }
212 
213     return nullptr;
214 }
215 
equal(ListItem * const item) const216 bool ListItem::equal(ListItem* const item) const
217 {
218     return (d->tagIds == item->getTagIds());
219 }
220 
221 } // namespace Digikam
222