summaryrefslogtreecommitdiffstats
path: root/libkipi/libkipi/imagecollectionselector.cpp
blob: 9a58fa3cd79bc5fb8e7f60b8a7fe244c0f48266e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/* ============================================================
 * File   : imagecollectionselector.cpp
 * Authors: KIPI team developers (see AUTHORS files for details)
 *	    
 * Date   : 2004-07
 * Description :
 *
 * Copyright 2004 by the KIPI team
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU Library General
 * Public License as published by the Free Software Foundation;
 * either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 *
 * ============================================================ */

// TQt includes.

#include <tqheader.h>
#include <tqlayout.h>
#include <tqpushbutton.h>
#include <tqlabel.h>
#include <tqvgroupbox.h>
#include <tqtimer.h>

// KDE includes.

#include <kbuttonbox.h>
#include <kdialog.h>
#include <tdelistview.h>
#include <tdelocale.h>
#include <tdeglobal.h>
#include <tdeio/previewjob.h>

// KIPI includes.

#include "libkipi/interface.h"

// Local includes.

#include "imagecollectionselector.h"

namespace KIPI 
{

class ImageCollectionItem : public TQCheckListItem
{
public:
    ImageCollectionItem(ImageCollectionSelector* selector,
                        TQListView * parent, ImageCollection collection)
    : TQCheckListItem( parent, collection.name(), TQCheckListItem::CheckBox),
      _imageCollection(collection), _selector(selector)
    {}

    ImageCollection imageCollection() const { return _imageCollection; }

protected:

    virtual void stateChange(bool val)
    {
        TQCheckListItem::stateChange(val);
        _selector->emitSelectionChanged();
    }
    
private:
    
    ImageCollection          _imageCollection;
    ImageCollectionSelector* _selector;
};

struct ImageCollectionSelector::Private {
    Interface* _interface;
    TDEListView* _list;
    TQLabel*    _thumbLabel;
    TQLabel*    _textLabel;
    TQListViewItem* _itemToSelect;
};


ImageCollectionSelector::ImageCollectionSelector(TQWidget* parent, Interface* interface, const char* name)
                       : TQWidget(parent, name)
{
    d=new Private;
    d->_interface=interface;
    d->_itemToSelect = 0;
    
    d->_list=new TDEListView(this);
    d->_list->setResizeMode( TQListView::LastColumn );
    d->_list->addColumn("");
    d->_list->header()->hide();

    connect(d->_list, TQT_SIGNAL(selectionChanged(TQListViewItem*)),
            TQT_SLOT(slotSelectionChanged(TQListViewItem*)));
    
    TQHBoxLayout* mainLayout=new TQHBoxLayout(this, 0, KDialog::spacingHint());
    mainLayout->addWidget(d->_list);

    TQVBoxLayout* rightLayout = new TQVBoxLayout(mainLayout, 0);

    KButtonBox* box=new KButtonBox(this,Qt::Vertical);
    rightLayout->addWidget(box);
    TQPushButton* selectAll=box->addButton(i18n("Select All"));
    TQPushButton* invertSelection=box->addButton(i18n("Invert Selection"));
    TQPushButton* selectNone=box->addButton(i18n("Select None"));
    box->layout();

    connect(selectAll, TQT_SIGNAL(clicked()),
            this, TQT_SLOT(slotSelectAll()) );
    connect(invertSelection, TQT_SIGNAL(clicked()),
            this, TQT_SLOT(slotInvertSelection()) );
    connect(selectNone, TQT_SIGNAL(clicked()), 
            this, TQT_SLOT(slotSelectNone()) );

    rightLayout->addItem(new TQSpacerItem(10,20,TQSizePolicy::Fixed,
                                         TQSizePolicy::Expanding));
    
    TQVGroupBox* rightBox = new TQVGroupBox(this);
    rightBox->setInsideMargin(KDialog::marginHint());
    rightBox->setInsideSpacing(KDialog::spacingHint());
    rightLayout->addWidget(rightBox);

    if (interface->hasFeature(AlbumsUseFirstImagePreview))
    {
        d->_thumbLabel = new TQLabel(rightBox);
        d->_thumbLabel->setFixedSize(TQSize(128,128));
        d->_thumbLabel->setAlignment(AlignHCenter | AlignVCenter);
    }
    else
    {
        d->_thumbLabel = 0;
    }
    d->_textLabel = new TQLabel(rightBox);
    
    fillList();
    TQTimer::singleShot(0, this, TQT_SLOT(slotInitialShow()));
}


ImageCollectionSelector::~ImageCollectionSelector() {
    delete d;
}


void ImageCollectionSelector::fillList() {
    TQValueList<ImageCollection> collections = d->_interface->allAlbums();
    d->_list->clear();
    ImageCollection current = d->_interface->currentAlbum();
    bool currentWasInList = false;

    /* note: the extensive use of blocksignals is to prevent bombarding
       the plugin with too many selection changed signals. do not remove
       them */
    
    blockSignals(true);
    for( TQValueList<ImageCollection>::Iterator it = collections.begin() ;
         it != collections.end() ; ++it )
    {
        ImageCollectionItem* item = new ImageCollectionItem( this, d->_list, *it);
        if (!currentWasInList && *it == current) {
            item->setOn(true);
            currentWasInList = true;
            if (!d->_itemToSelect)
                d->_itemToSelect = item;
        }
    }

    if (!currentWasInList) {
        slotSelectAll();
        d->_itemToSelect = d->_list->firstChild();
    }
    blockSignals(false);
}

void ImageCollectionSelector::emitSelectionChanged()
{
    emit selectionChanged();
}

TQValueList<ImageCollection> ImageCollectionSelector::selectedImageCollections() const {
    TQValueList<ImageCollection> list;

    TQListViewItemIterator it( d->_list );

    for (; it.current(); ++it) {
        ImageCollectionItem *item = static_cast<ImageCollectionItem*>( it.current() );

        if (item->isOn()) {
            list << item->imageCollection();
        }
    }

    return list;
}

void ImageCollectionSelector::slotSelectAll() {
    TQListViewItemIterator it( d->_list );

    /* note: the extensive use of blocksignals is to prevent bombarding
       the plugin with too many selection changed signals. do not remove
       them */
    blockSignals(true);
    for (; it.current(); ++it) {
        ImageCollectionItem *item = static_cast<ImageCollectionItem*>( it.current() );
        item->setOn(true);
    }
    blockSignals(false);
    
    emit selectionChanged();
}


void ImageCollectionSelector::slotInvertSelection() {
    TQListViewItemIterator it( d->_list );

    /* note: the extensive use of blocksignals is to prevent bombarding
       the plugin with too many selection changed signals. do not remove
       them */
    blockSignals(true);
    for (; it.current(); ++it) {
        ImageCollectionItem *item = static_cast<ImageCollectionItem*>( it.current() );
        item->setOn(!item->isOn());
    }
    blockSignals(false);

    emit selectionChanged();
}


void ImageCollectionSelector::slotSelectNone() {
    TQListViewItemIterator it( d->_list );

    /* note: the extensive use of blocksignals is to prevent bombarding
       the plugin with too many selection changed signals. do not remove
       them */
    blockSignals(true);
    for (; it.current(); ++it) {
        ImageCollectionItem *item = static_cast<ImageCollectionItem*>( it.current() );
        item->setOn(false);
    }
    blockSignals(false);
    
    emit selectionChanged();
}

void ImageCollectionSelector::slotSelectionChanged(TQListViewItem* listItem)
{
    if (d->_thumbLabel)
        d->_thumbLabel->clear();
    d->_textLabel->clear();

    if (!listItem)
        return;

    ImageCollectionItem* imcollItem =
        static_cast<ImageCollectionItem*>(listItem);

    if (d->_thumbLabel)
    {
        KURL::List images(imcollItem->imageCollection().images());
        if (!images.isEmpty())
        {
            TDEIO::PreviewJob* thumbJob = TDEIO::filePreview(images.first(), 128);
            connect( thumbJob, TQT_SIGNAL(gotPreview(const KFileItem*, const TQPixmap&)),
                     TQT_SLOT(slotGotPreview(const KFileItem* , const TQPixmap&)));
        }
    }
    
    // Layout the ImageCollection information nicely
    
    TQString cellBeg("<tr><td><nobr><font size=-1><i>");
    TQString cellMid("</i></font></nobr></td><td><font size=-1>");
    TQString cellEnd("</font></td></tr>");

    TQString text("<table cellspacing=0 cellpadding=0>");

    // number of images 
    text += cellBeg + i18n("Images:") +
            cellMid + TQString::number(imcollItem->imageCollection().images().count()) +
            cellEnd;

    // Optional features -------------------------------------------------------
    
    // Album Comments
    if (d->_interface->hasFeature(AlbumsHaveComments))
    {
        // Limit the comments string to 20 char...
        TQString comments = imcollItem->imageCollection().comment();
	if (!comments.isEmpty())
	{
	comments.truncate(20);
	comments.append("...");
	}
	
        text += cellBeg + i18n("Comments:") +
                cellMid + comments +
                cellEnd;
    }

    // Album Category
    if (d->_interface->hasFeature(AlbumsHaveCategory))
    {
        text += cellBeg + i18n("Category:") +
                cellMid + imcollItem->imageCollection().category() +
                cellEnd;
    }

    // Album Creation Date
    if (d->_interface->hasFeature(AlbumsHaveCreationDate))
    {
        TQDate date(imcollItem->imageCollection().date());
        text += cellBeg + i18n("Date:") +
                cellMid + TDEGlobal::locale()->formatDate(date) +
                cellEnd;
    }

    
    text += "</table>";

    d->_textLabel->setText(text);
    
    emit selectionChanged();
}

void ImageCollectionSelector::slotGotPreview(const KFileItem*, const TQPixmap& pix)
{
    d->_thumbLabel->setPixmap(pix);
}

void ImageCollectionSelector::slotInitialShow()
{
    if (d->_itemToSelect)
    {
        d->_list->setSelected(d->_itemToSelect, true);
        d->_list->ensureItemVisible(d->_itemToSelect);
        d->_itemToSelect = 0;
    }
    emit selectionChanged();
}

} // KIPI

#include "imagecollectionselector.moc"