~ubuntu-branches/ubuntu/intrepid/digikam/intrepid

« back to all changes in this revision

Viewing changes to digikam/digikam/datefolderview.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mark Purcell
  • Date: 2008-07-17 20:25:39 UTC
  • mfrom: (1.2.15 upstream) (3.1.2 lenny)
  • Revision ID: james.westby@ubuntu.com-20080717202539-6n7dtirbkoo7qvhd
Tags: 2:0.9.4-1
* New upstream release
  - digiKam 0.9.4 Release Plan (KDE3) ~ 13 July 08 (Closes: #490144)
* DEB_CONFIGURE_EXTRA_FLAGS := --without-included-sqlite3
* Debhelper compatibility level V7
* Install pixmaps in debian/*.install
* Add debian/digikam.lintian-overrides

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 * http://www.digikam.org
5
5
 *
6
6
 * Date        : 2005-04-27
7
 
 * Description : a view for date albums.
 
7
 * Description : a folder view for date albums.
8
8
 * 
9
9
 * Copyright (C) 2005 by Renchi Raju <renchi@pooh.tam.uiuc.edu>
 
10
 * Copyright (C) 2006-2008 by Gilles Caulier <caulier dot gilles at gmail dot com>
10
11
 *
11
12
 * This program is free software; you can redistribute it
12
13
 * and/or modify it under the terms of the GNU General
28
29
#include <qfont.h>
29
30
#include <qpainter.h>
30
31
#include <qstyle.h>
 
32
#include <qfileinfo.h>
31
33
 
32
34
// KDE includes.
33
35
 
44
46
 
45
47
// Local includes.
46
48
 
47
 
#include "ddebug.h"
48
49
#include "album.h"
49
 
#include "albummanager.h"
 
50
#include "albumdb.h"
50
51
#include "albumsettings.h"
 
52
#include "ddebug.h"
 
53
#include "folderview.h"
51
54
#include "monthwidget.h"
52
 
#include "folderitem.h"
53
 
#include "folderview.h"
54
55
#include "datefolderview.h"
55
56
#include "datefolderview.moc"
56
57
 
57
58
namespace Digikam
58
59
{
59
60
 
 
61
class DateFolderItem : public FolderItem
 
62
{
 
63
 
 
64
public:
 
65
 
 
66
    DateFolderItem(QListView* parent, DAlbum* album);
 
67
    DateFolderItem(QListViewItem* parent, DAlbum* album);
 
68
 
 
69
    ~DateFolderItem();
 
70
 
 
71
    void refresh();
 
72
 
 
73
    int     compare(QListViewItem *i, int, bool) const;
 
74
    QString date() const;
 
75
    QString name() const;
 
76
 
 
77
    DAlbum* album() const;
 
78
 
 
79
    int count() const;
 
80
    void setCount(int v);
 
81
 
 
82
private:
 
83
 
 
84
    int               m_count;
 
85
 
 
86
    QString           m_name;
 
87
 
 
88
    DAlbum           *m_album;
 
89
};
 
90
 
 
91
DateFolderItem::DateFolderItem(QListView* parent, DAlbum* album)
 
92
              : FolderItem(parent, QString(), true)
 
93
{
 
94
    m_count = 0;
 
95
    m_album = album;
 
96
    m_name  = QString::number(album->date().year());
 
97
    setText(0, m_name);
 
98
}
 
99
 
 
100
DateFolderItem::DateFolderItem(QListViewItem* parent, DAlbum* album)
 
101
              : FolderItem(parent, QString())
 
102
{
 
103
    m_count = 0;
 
104
    m_album = album;
 
105
#if KDE_IS_VERSION(3,2,0)
 
106
    m_name  = KGlobal::locale()->calendar()->monthName(m_album->date(), false);
 
107
#else
 
108
    m_name  = KGlobal::locale()->monthName(m_album->date(), false);
 
109
#endif
 
110
    setText(0, m_name);
 
111
}
 
112
 
 
113
DateFolderItem::~DateFolderItem()
 
114
{
 
115
}
 
116
 
 
117
void DateFolderItem::refresh()
 
118
{
 
119
    if (AlbumSettings::instance()->getShowFolderTreeViewItemsCount())
 
120
        setText(0, QString("%1 (%2)").arg(m_name).arg(m_count));
 
121
    else
 
122
        setText(0, m_name);
 
123
}
 
124
 
 
125
int DateFolderItem::compare(QListViewItem* i, int , bool ) const
 
126
{
 
127
    if (!i)
 
128
        return 0;
 
129
 
 
130
    DateFolderItem* dItem = dynamic_cast<DateFolderItem*>(i);
 
131
    if (m_album->date() == dItem->m_album->date())
 
132
        return 0;
 
133
    else if (m_album->date() > dItem->m_album->date())
 
134
        return 1;
 
135
    else
 
136
        return -1;
 
137
}
 
138
 
 
139
QString DateFolderItem::date() const
 
140
{
 
141
    return m_album->date().toString();
 
142
}
 
143
 
 
144
QString DateFolderItem::name() const
 
145
{
 
146
    return m_name;
 
147
}
 
148
 
 
149
DAlbum* DateFolderItem::album() const
 
150
{
 
151
    return m_album;
 
152
}
 
153
 
 
154
int DateFolderItem::count() const
 
155
{
 
156
    return m_count;
 
157
}
 
158
 
 
159
void DateFolderItem::setCount(int v)
 
160
{
 
161
    m_count = v;
 
162
    refresh();
 
163
}
 
164
 
 
165
// -----------------------------------------------------------------
 
166
 
60
167
class DateFolderViewPriv
61
168
{
62
169
public:
64
171
    DateFolderViewPriv()
65
172
    {
66
173
        active    = false;
67
 
        listview  = 0;
68
 
        monthview = 0;
69
 
    }
70
 
 
71
 
    bool         active;
72
 
 
73
 
    QString      selected;
74
 
 
75
 
    FolderView  *listview;
76
 
    MonthWidget *monthview;
77
 
};
78
 
 
79
 
class DateFolderItem : public FolderItem
80
 
{
81
 
public:
82
 
 
83
 
    DateFolderItem(QListView* parent, const QString& name)
84
 
        : FolderItem(parent, name, true), m_album(0)
85
 
    {
86
 
    }
87
 
 
88
 
    DateFolderItem(QListViewItem* parent, const QString& name, DAlbum* album)
89
 
        : FolderItem(parent, name), m_album(album)
90
 
    {
91
 
    }
92
 
 
93
 
    int compare(QListViewItem* i, int , bool ) const
94
 
    {
95
 
        if (!i)
96
 
            return 0;
97
 
        
98
 
        DateFolderItem* dItem = dynamic_cast<DateFolderItem*>(i);
99
 
        if (!dItem || !dItem->m_album)
100
 
        {
101
 
            return text(0).localeAwareCompare(i->text(0));
102
 
        } 
103
 
 
104
 
        if (m_album->date() == dItem->m_album->date())
105
 
            return 0;
106
 
        else if (m_album->date() > dItem->m_album->date())
107
 
            return 1;
108
 
        else
109
 
            return -1;
110
 
    }
111
 
    
112
 
    QString date() const
113
 
    {
114
 
        // If an album is set, return it's date, otherwise just the year
115
 
        return m_album ? m_album->date().toString() : text(0);
116
 
    }
117
 
    
118
 
    DAlbum* m_album;
 
174
        listview  = 0;
 
175
        monthview = 0;
 
176
    }
 
177
 
 
178
    bool          active;
 
179
 
 
180
    QString       selected;
 
181
 
 
182
    FolderView   *listview;
 
183
 
 
184
    MonthWidget  *monthview;
119
185
};
120
186
 
121
187
DateFolderView::DateFolderView(QWidget* parent)
125
191
    d->listview  = new FolderView(this);
126
192
    d->monthview = new MonthWidget(this);
127
193
 
128
 
    d->listview->addColumn(i18n("My Dates"));
 
194
    d->listview->addColumn(i18n("My Calendar"));
129
195
    d->listview->setResizeMode(QListView::LastColumn);
130
196
    d->listview->setRootIsDecorated(true);
131
197
 
132
198
    connect(AlbumManager::instance(), SIGNAL(signalAlbumAdded(Album*)),
133
 
            SLOT(slotAlbumAdded(Album*)));
134
 
            
 
199
            this, SLOT(slotAlbumAdded(Album*)));
 
200
 
135
201
    connect(AlbumManager::instance(), SIGNAL(signalAlbumDeleted(Album*)),
136
 
            SLOT(slotAlbumDeleted(Album*)));
137
 
            
 
202
            this, SLOT(slotAlbumDeleted(Album*)));
 
203
 
138
204
    connect(AlbumManager::instance(), SIGNAL(signalAllDAlbumsLoaded()),
139
 
            SLOT(slotAllDAlbumsLoaded()));    
140
 
            
 
205
            this, SLOT(slotAllDAlbumsLoaded()));
 
206
 
141
207
    connect(AlbumManager::instance(), SIGNAL(signalAlbumsCleared()),
142
208
            d->listview, SLOT(clear()));
143
209
 
 
210
    connect(AlbumManager::instance(), SIGNAL(signalDAlbumsDirty(const QMap<YearMonth, int>&)),
 
211
            this, SLOT(slotRefresh(const QMap<YearMonth, int>&)));
 
212
 
144
213
    connect(d->listview, SIGNAL(selectionChanged()),
145
 
            SLOT(slotSelectionChanged()));
 
214
            this, SLOT(slotSelectionChanged()));
146
215
}
147
216
 
148
217
DateFolderView::~DateFolderView()
155
224
{
156
225
    if (d->active == val)
157
226
        return;
158
 
    
 
227
 
159
228
    d->active = val;
160
229
    if (d->active)
161
230
    {
180
249
        return;
181
250
 
182
251
    DAlbum* album = (DAlbum*)a;
183
 
    
184
 
    QDate date = album->date();
185
 
 
186
 
    QString yr = QString::number(date.year());
187
 
    
188
 
#if KDE_IS_VERSION(3,2,0)
189
 
    QString mo = KGlobal::locale()->calendar()->monthName(date, false);
190
 
#else
191
 
    QString mo = KGlobal::locale()->monthName(date, false);
192
 
#endif
193
 
    
194
 
    QListViewItem* parent = d->listview->findItem(yr, 0);
195
 
    if (!parent)
196
 
    {
197
 
        parent = new DateFolderItem(d->listview, yr);
198
 
        parent->setPixmap(0, SmallIcon("date", AlbumSettings::instance()->getDefaultTreeIconSize()));
199
 
    }
200
 
 
201
 
    DateFolderItem* item = new DateFolderItem(parent, mo, album);
202
 
    item->setPixmap(0, SmallIcon("date", AlbumSettings::instance()->getDefaultTreeIconSize()));
203
 
 
204
 
    album->setExtraData(this, item);
 
252
    QDate date    = album->date();
 
253
 
 
254
    if (album->range() == DAlbum::Year)
 
255
    {
 
256
        DateFolderItem* item = new DateFolderItem(d->listview, album);
 
257
        item->setPixmap(0, SmallIcon("date", AlbumSettings::instance()->getDefaultTreeIconSize()));
 
258
        album->setExtraData(this, item);
 
259
        return;
 
260
    }
 
261
 
 
262
    QString yr            = QString::number(date.year());
 
263
    QListViewItem* parent = findRootItemByYear(yr);
 
264
 
 
265
    if (parent)
 
266
    {
 
267
        DateFolderItem* item = new DateFolderItem(parent, album);
 
268
        item->setPixmap(0, SmallIcon("date", AlbumSettings::instance()->getDefaultTreeIconSize()));
 
269
        album->setExtraData(this, item);
 
270
    }
205
271
}
206
272
 
207
273
void DateFolderView::slotAlbumDeleted(Album* a)
223
289
{
224
290
    if (!d->active)
225
291
        return;
226
 
    
 
292
 
 
293
    d->monthview->setActive(false);
 
294
 
227
295
    QListViewItem* selItem = 0;
228
 
    
229
296
    QListViewItemIterator it( d->listview );
230
297
    while (it.current())
231
298
    {
237
304
        ++it;
238
305
    }
239
306
 
240
 
    d->monthview->setActive(false);
241
 
    
242
307
    if (!selItem)
243
308
    {
244
309
        AlbumManager::instance()->setCurrentAlbum(0);
246
311
    }
247
312
 
248
313
    DateFolderItem* dateItem = dynamic_cast<DateFolderItem*>(selItem);
249
 
    
250
 
    if (!dateItem || !dateItem->m_album)
 
314
    if (!dateItem)
251
315
    {
252
316
        AlbumManager::instance()->setCurrentAlbum(0);
253
 
        d->monthview->setActive(false);
 
317
        return;
254
318
    }
255
 
    else
 
319
 
 
320
    AlbumManager::instance()->setCurrentAlbum(dateItem->album());
 
321
 
 
322
    if (dateItem->album()->range() == DAlbum::Month)
256
323
    {
257
 
        AlbumManager::instance()->setCurrentAlbum(dateItem->m_album);
258
 
 
259
 
        QDate date = dateItem->m_album->date();        
 
324
        QDate date = dateItem->album()->date();
260
325
        d->monthview->setActive(true);
261
326
        d->monthview->setYearMonth(date.year(), date.month());
262
327
    }
266
331
{
267
332
    KConfig *config = kapp->config();
268
333
    config->setGroup(name());
269
 
    
 
334
 
270
335
    QString selected;
271
336
    if(config->hasKey("LastSelectedItem"))
272
337
    {
278
343
    {
279
344
        openFolders = config->readListEntry("OpenFolders");
280
345
    }
281
 
    
 
346
 
282
347
    DateFolderItem *item;
283
348
    QString id;
284
349
    QListViewItemIterator it(d->listview);
285
350
    for( ; it.current(); ++it)
286
 
    {        
 
351
    {
287
352
        item = dynamic_cast<DateFolderItem*>(it.current());
288
353
        id = item->date();
289
354
        if(openFolders.contains(id))
290
355
            d->listview->setOpen(item, true);
291
356
        else
292
357
            d->listview->setOpen(item, false);
293
 
        
 
358
 
294
359
        if(id == selected)
295
360
            d->listview->setSelected(item, true);
296
 
    }    
 
361
    }
297
362
}
298
363
 
299
364
void DateFolderView::gotoDate(const QDate& dt)
300
365
{
301
366
    DateFolderItem *item = 0;
302
367
    QDate           id;
303
 
    
 
368
 
304
369
    QDate date = QDate(dt.year(), dt.month(), 1);
305
370
 
306
371
    // Find that date in the side-bar list.
307
372
    QListViewItemIterator it(d->listview);
308
373
    for( ; it.current(); ++it)
309
 
    {        
 
374
    {
310
375
        item = dynamic_cast<DateFolderItem*>(it.current());
311
 
        if (item->m_album)
 
376
        if (item->album())
312
377
        {
313
 
            id = item->m_album->date();
 
378
            id = item->album()->date();
314
379
            if(id == date)
315
380
            {
316
381
                d->listview->setSelected(item, true);
324
389
{
325
390
    KConfig *config = kapp->config();
326
391
    config->setGroup(name());
327
 
   
 
392
 
328
393
    DateFolderItem *item = dynamic_cast<DateFolderItem*>(d->listview->selectedItem());
329
394
    if(item)
330
395
        config->writeEntry("LastSelectedItem", item->date());
331
 
    
 
396
 
332
397
    QStringList openFolders;
333
398
    QListViewItemIterator it(d->listview);
334
399
    item = dynamic_cast<DateFolderItem*>(d->listview->firstChild());
346
411
{
347
412
    if(!item)
348
413
        return;
349
 
    
 
414
 
350
415
    d->listview->setSelected(item, true);
351
416
    d->listview->ensureItemVisible(item);
352
417
}
353
418
 
 
419
QListViewItem *DateFolderView::findRootItemByYear(const QString& year)
 
420
{
 
421
    QListViewItemIterator it(d->listview);
 
422
 
 
423
    while (it.current())
 
424
    {
 
425
        DateFolderItem* item = dynamic_cast<DateFolderItem*>(*it);
 
426
        if (item)
 
427
        {
 
428
            if (item->album()->range() == DAlbum::Year && item->name() == year)
 
429
                return item;
 
430
        }
 
431
        ++it;
 
432
    }
 
433
    return 0;
 
434
}
 
435
 
 
436
void DateFolderView::refresh()
 
437
{
 
438
    QListViewItemIterator it(d->listview);
 
439
 
 
440
    while (it.current())
 
441
    {
 
442
        DateFolderItem* item = dynamic_cast<DateFolderItem*>(*it);
 
443
        if (item)
 
444
            item->refresh();
 
445
        ++it;
 
446
    }
 
447
}
 
448
 
 
449
void DateFolderView::slotRefresh(const QMap<YearMonth, int>& yearMonthMap)
 
450
{
 
451
    QListViewItemIterator it(d->listview);
 
452
 
 
453
    while (it.current())
 
454
    {
 
455
        DateFolderItem* item = dynamic_cast<DateFolderItem*>(*it);
 
456
        if (item)
 
457
        {
 
458
            QDate date = item->album()->date();
 
459
 
 
460
            if (item->album()->range() == DAlbum::Month)
 
461
            {
 
462
                QMap<YearMonth, int>::const_iterator it2 = yearMonthMap.find(YearMonth(date.year(), date.month()));
 
463
                if ( it2 != yearMonthMap.end() )
 
464
                    item->setCount(it2.data());
 
465
            }
 
466
            else
 
467
            {
 
468
                int count = 0;
 
469
                for ( QMap<YearMonth, int>::const_iterator it2 = yearMonthMap.begin();
 
470
                      it2 != yearMonthMap.end(); ++it2 )
 
471
                {
 
472
                    if (it2.key().first == date.year())
 
473
                        count += it2.data();
 
474
                }
 
475
                item->setCount(count);
 
476
            }   
 
477
        }
 
478
        ++it;
 
479
    }
 
480
}
 
481
 
354
482
}  // namespace Digikam
355