~ubuntu-branches/ubuntu/saucy/digikam/saucy

« back to all changes in this revision

Viewing changes to libs/models/albummodel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christian Mangold
  • Date: 2010-04-09 21:30:01 UTC
  • mfrom: (1.2.28 upstream)
  • Revision ID: james.westby@ubuntu.com-20100409213001-4bfyibrd359rn7o3
Tags: 2:1.2.0-0ubuntu1
* New upstream release (LP: #560576)
* Remove all patches, fixed upstream
  - Remove quilt build-depend

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 
28
28
#include <kcalendarsystem.h>
29
29
#include <kglobal.h>
 
30
#include <kiconloader.h>
30
31
#include <klocale.h>
 
32
#include <kdebug.h>
31
33
 
32
34
// Local includes
33
35
 
34
36
#include "albummanager.h"
 
37
#include "albumsettings.h"
35
38
#include "albumthumbnailloader.h"
36
39
 
37
40
namespace Digikam
46
49
    setupThumbnailLoading();
47
50
}
48
51
 
 
52
AlbumModel::~AlbumModel()
 
53
{
 
54
 
 
55
}
 
56
 
49
57
PAlbum *AlbumModel::albumForIndex(const QModelIndex& index) const
50
58
{
51
59
    return static_cast<PAlbum*>(AbstractCheckableAlbumModel::albumForIndex(index));
52
60
}
53
61
 
54
 
QVariant AlbumModel::decorationRole(Album *album) const
 
62
QVariant AlbumModel::decorationRoleData(Album *album) const
55
63
{
56
64
    // asynchronous signals are handled by parent class
57
65
    return AlbumThumbnailLoader::instance()->getAlbumThumbnailDirectly(static_cast<PAlbum *>(album));
78
86
    return static_cast<TAlbum*>(AbstractCheckableAlbumModel::albumForIndex(index));
79
87
}
80
88
 
81
 
QVariant TagModel::decorationRole(Album *album) const
 
89
QVariant TagModel::decorationRoleData(Album *album) const
82
90
{
83
91
    return AlbumThumbnailLoader::instance()->getTagThumbnailDirectly(static_cast<TAlbum *>(album), true);
84
92
}
91
99
// ------------------------------------------------------------------
92
100
 
93
101
SearchModel::SearchModel(QObject *parent)
94
 
            : AbstractSpecificAlbumModel(Album::SEARCH,
 
102
            : AbstractCheckableAlbumModel(Album::SEARCH,
95
103
                                         AlbumManager::instance()->findSAlbum(0),
96
104
                                         IgnoreRootAlbum, parent)
97
105
{
 
106
 
 
107
    setShowCount(false);
 
108
 
 
109
    // handle search icons
 
110
    albumSettingsChanged();
 
111
    connect(AlbumSettings::instance(), SIGNAL(setupChanged()),
 
112
            this, SLOT(albumSettingsChanged()));
 
113
 
98
114
}
99
115
 
100
116
SAlbum *SearchModel::albumForIndex(const QModelIndex& index) const
101
117
{
102
 
    return static_cast<SAlbum*>(AbstractSpecificAlbumModel::albumForIndex(index));
103
 
}
104
 
 
105
 
void SearchModel::setSearchType(DatabaseSearch::Type type)
106
 
{
107
 
    m_searchType = type;
108
 
}
109
 
 
110
 
void SearchModel::listNormalSearches()
111
 
{
112
 
    m_searchType = -1;
113
 
}
114
 
 
115
 
void SearchModel::listAllSearches()
116
 
{
117
 
    m_searchType = -2;
 
118
    return static_cast<SAlbum*>(AbstractCheckableAlbumModel::albumForIndex(index));
118
119
}
119
120
 
120
121
void SearchModel::setReplaceNames(QHash<QString, QString> replaceNames)
122
123
    m_replaceNames = replaceNames;
123
124
}
124
125
 
125
 
void SearchModel::setPixmap(const QPixmap& pix)
126
 
{
127
 
    m_pixmap = pix;
 
126
void SearchModel::addReplaceName(const QString& technicalName, const QString& userVisibleName)
 
127
{
 
128
    m_replaceNames.insert(technicalName, userVisibleName);
 
129
}
 
130
 
 
131
void SearchModel::setPixmapForNormalSearches(const QPixmap& pix)
 
132
{
 
133
    m_pixmaps.insert(-1, pix);
 
134
}
 
135
 
 
136
void SearchModel::setDefaultPixmap(const QPixmap& pix)
 
137
{
 
138
    m_pixmaps.insert(-2, pix);
 
139
}
 
140
 
 
141
void SearchModel::setPixmapForTimelineSearches(const QPixmap& pix)
 
142
{
 
143
    m_pixmaps.insert(DatabaseSearch::TimeLineSearch, pix);
 
144
}
 
145
 
 
146
void SearchModel::setPixmapForHaarSearches(const QPixmap& pix)
 
147
{
 
148
    m_pixmaps.insert(DatabaseSearch::HaarSearch, pix);
 
149
}
 
150
 
 
151
void SearchModel::setPixmapForMapSearches(const QPixmap& pix)
 
152
{
 
153
    m_pixmaps.insert(DatabaseSearch::MapSearch, pix);
 
154
}
 
155
 
 
156
void SearchModel::setPixmapForDuplicatesSearches(const QPixmap& pix)
 
157
{
 
158
    m_pixmaps.insert(DatabaseSearch::DuplicatesSearch, pix);
128
159
}
129
160
 
130
161
QVariant SearchModel::albumData(Album *a, int role) const
131
162
{
132
 
    if (role == Qt::DisplayRole)
 
163
    if (role == Qt::DisplayRole || role == AlbumTitleRole)
133
164
    {
134
 
        QString name = a->title();
135
 
        return m_replaceNames.value(name, name);
 
165
        SAlbum *salbum = static_cast<SAlbum*>(a);
 
166
        QString title = a->title();
 
167
        QString displayTitle = salbum->displayTitle();
 
168
        return m_replaceNames.value(title, displayTitle);
136
169
    }
137
170
    else if (role == Qt::DecorationRole)
138
 
        return m_pixmap;
139
 
 
140
 
    return AbstractSpecificAlbumModel::albumData(a, role);
141
 
}
142
 
 
143
 
bool SearchModel::filterAlbum(Album *album) const
144
 
{
145
 
    if (AbstractSpecificAlbumModel::filterAlbum(album))
146
171
    {
147
 
        SAlbum *salbum = static_cast<SAlbum*>(album);
148
 
        if (m_searchType == -1)
149
 
            return salbum->isNormalSearch();
150
 
        else if (m_searchType == -2)
151
 
            return true;
152
 
        else
153
 
            return salbum->type() == (DatabaseSearch::Type)m_searchType;
 
172
        SAlbum *salbum = static_cast<SAlbum*>(a);
 
173
        QPixmap pixmap = m_pixmaps.value(salbum->searchType());
 
174
        if (pixmap.isNull() && salbum->isNormalSearch())
 
175
        {
 
176
            pixmap = m_pixmaps.value(-1);
 
177
        }
 
178
        if (pixmap.isNull())
 
179
        {
 
180
            pixmap = m_pixmaps.value(-2);
 
181
        }
 
182
        return pixmap;
154
183
    }
155
 
    return false;
 
184
 
 
185
    return AbstractCheckableAlbumModel::albumData(a, role);
 
186
}
 
187
 
 
188
Album* SearchModel::albumForId(int id) const
 
189
{
 
190
    return AlbumManager::instance()->findSAlbum(id);
 
191
}
 
192
 
 
193
void SearchModel::albumSettingsChanged()
 
194
{
 
195
    setPixmapForMapSearches(SmallIcon("applications-internet", AlbumSettings::instance()->getTreeViewIconSize()));
 
196
    setPixmapForHaarSearches(SmallIcon("tools-wizard", AlbumSettings::instance()->getTreeViewIconSize()));
 
197
    setPixmapForNormalSearches(SmallIcon("edit-find", AlbumSettings::instance()->getTreeViewIconSize()));
 
198
    setPixmapForTimelineSearches(SmallIcon("chronometer", AlbumSettings::instance()->getTreeViewIconSize()));
156
199
}
157
200
 
158
201
// ------------------------------------------------------------------
162
205
                                         AlbumManager::instance()->findDAlbum(0),
163
206
                                         IgnoreRootAlbum, parent)
164
207
{
 
208
    connect(AlbumManager::instance(), SIGNAL(signalDAlbumsDirty(const QMap<YearMonth, int>&)),
 
209
            this, SLOT(setYearMonthMap(const QMap<YearMonth, int>&)));
 
210
    setYearMonthMap(AlbumManager::instance()->getDAlbumsCount());
165
211
}
166
212
 
167
213
DAlbum *DateAlbumModel::albumForIndex(const QModelIndex& index) const
169
215
    return static_cast<DAlbum*>(AbstractCountingAlbumModel::albumForIndex(index));
170
216
}
171
217
 
 
218
QModelIndex DateAlbumModel::monthIndexForDate(const QDate &date) const
 
219
{
 
220
 
 
221
    // iterate over all years
 
222
    for (int yearIndex = 0; yearIndex < rowCount(); ++yearIndex)
 
223
    {
 
224
        QModelIndex year = index(yearIndex, 0);
 
225
        DAlbum *yearAlbum = albumForIndex(year);
 
226
 
 
227
        // do not search through months if we are sure, that the year already
 
228
        // does not match
 
229
        if (yearAlbum && (yearAlbum->range() == DAlbum::Year)
 
230
                      && (yearAlbum->date().year() != date.year()))
 
231
        {
 
232
            continue;
 
233
        }
 
234
 
 
235
        // search the album with the correct month
 
236
        for (int monthIndex = 0; monthIndex < rowCount(year); ++monthIndex)
 
237
        {
 
238
            QModelIndex month = index(monthIndex, 0, year);
 
239
            DAlbum *monthAlbum = albumForIndex(month);
 
240
            if (monthAlbum && (monthAlbum->range() == DAlbum::Month)
 
241
                           && (monthAlbum->date().year() == date.year())
 
242
                           && (monthAlbum->date().month() == date.month()))
 
243
            {
 
244
                return month;
 
245
            }
 
246
        }
 
247
 
 
248
    }
 
249
 
 
250
    return QModelIndex();
 
251
 
 
252
}
 
253
 
172
254
void DateAlbumModel::setPixmaps(const QPixmap& forYearAlbums, const QPixmap& forMonthAlbums)
173
255
{
174
256
    m_yearPixmap = forYearAlbums;
184
266
        return KGlobal::locale()->calendar()->monthName(dalbum->date(), KCalendarSystem::LongName);
185
267
}
186
268
 
187
 
QVariant DateAlbumModel::decorationRole(Album *album) const
 
269
QVariant DateAlbumModel::decorationRoleData(Album *album) const
188
270
{
189
271
    DAlbum *dalbum = static_cast<DAlbum*>(album);
190
272
    if (dalbum->range() == DAlbum::Year)
193
275
        return m_monthPixmap;
194
276
}
195
277
 
 
278
QVariant DateAlbumModel::sortRoleData(Album *a) const
 
279
{
 
280
    DAlbum *dalbum = static_cast<DAlbum*>(a);
 
281
    if (dalbum) {
 
282
        return dalbum->date();
 
283
    }
 
284
    kError() << "There must be a data album.";
 
285
    return QDate();
 
286
}
 
287
 
196
288
Album* DateAlbumModel::albumForId(int id) const
197
289
{
198
290
    return AlbumManager::instance()->findDAlbum(id);
200
292
 
201
293
void DateAlbumModel::setYearMonthMap(const QMap<YearMonth, int>& yearMonthMap)
202
294
{
 
295
 
203
296
    AlbumIterator it(rootAlbum());
204
297
 
 
298
    QMap<int, int> albumToCountMap;
205
299
    while (it.current())
206
300
    {
207
301
        DAlbum *dalbum = static_cast<DAlbum*>(*it);
208
302
        QDate date = dalbum->date();
209
303
 
210
 
        if (dalbum->range() == DAlbum::Month)
211
 
        {
212
 
            QMap<YearMonth, int>::const_iterator it2 = yearMonthMap.constFind(YearMonth(date.year(), date.month()));
213
 
            if ( it2 != yearMonthMap.constEnd() )
214
 
                setCount(*it, it2.value());
215
 
        }
216
 
        else
217
 
        {
218
 
            int count = 0;
219
 
            for ( QMap<YearMonth, int>::const_iterator it2 = yearMonthMap.constBegin();
220
 
                    it2 != yearMonthMap.constEnd(); ++it2 )
 
304
        switch (dalbum->range())
 
305
        {
 
306
        case DAlbum::Month:
221
307
            {
222
 
                if (it2.key().first == date.year())
223
 
                    count += it2.value();
 
308
                QMap<YearMonth, int>::const_iterator it2 = yearMonthMap.constFind(YearMonth(date.year(), date.month()));
 
309
                if ( it2 != yearMonthMap.constEnd() )
 
310
                {
 
311
                    albumToCountMap.insert((*it)->id(), it2.value());
 
312
                }
 
313
                break;
224
314
            }
225
 
            setCount(*it, count);
 
315
        case DAlbum::Year:
 
316
            // a year itself cannot contain images and therefore always has count 0
 
317
            albumToCountMap.insert((*it)->id(), 0);
 
318
            break;
 
319
        default:
 
320
            kError() << "Untreated DAlbum range " << dalbum->range();
 
321
            albumToCountMap.insert((*it)->id(), 0);
 
322
            break;
226
323
        }
 
324
 
227
325
        ++it;
228
326
    }
 
327
 
 
328
    setCountMap(albumToCountMap);
 
329
 
229
330
}
230
331
 
231
332
} // namespace Digikam