summaryrefslogtreecommitdiffstats
path: root/libkipi/libkipi/imagedialog.cpp
blob: 75009e18863bde62551c4bf6ba94f6b6a13882ca (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
/* ============================================================
 * File   : imagedialog.cpp
 * Authors: KIPI team developers (see AUTHORS files for details)
 *      
 * Date   : 2004-05
 * Description : an image files selector dialog.
 *
 * 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 <tqguardedptr.h>
#include <tqlabel.h>
#include <tqsplitter.h>
#include <tqlayout.h>
#include <tqframe.h>
#include <tqpushbutton.h>
#include <tqtimer.h>

// KDE includes.

#include <kdebug.h>
#include <klistview.h>
#include <klocale.h>
#include <kstandarddirs.h>
#include <kio/previewjob.h>
#include <kapplication.h>
#include <kaboutdata.h>
#include <khelpmenu.h>
#include <kiconloader.h>
#include <kpopupmenu.h>

// Include files for libKipi.

#include "libkipi/version.h"

// Local includes.

#include "imagedialog.h"

const int PREVIEW_SIZE = 128;

namespace KIPI
{

struct AlbumLVI : public KListViewItem {
    AlbumLVI(KListView* parent, const KIPI::ImageCollection& album)
    : KListViewItem(parent, album.name())
    , _album(album) {}

    const KIPI::ImageCollection& _album;
};


struct ImageLVI : public KListViewItem {
    ImageLVI(KListView* parent, const KURL& url)
    : KListViewItem(parent, url.fileName()), _url(url) {}

    KURL _url;
};


struct ImageDialog::Private {
    KURL _url;
    KURL::List _urls;
    KIPI::Interface* _interface;
    KListView* _albumList;
    KListView* _imageList;
    TQLabel* _preview;
    TQValueList<ImageCollection> _albums;
    bool _singleSelection;
};


ImageDialog::ImageDialog(TQWidget* parent, KIPI::Interface* interface,
                         bool singleSelection)
           : KDialogBase(KDialogBase::Plain, i18n("Select Image From Album"), Help|Ok|Cancel,
                         Ok, parent, "album-dialog", true, true)                         
{
    d = new Private;
    d->_interface=interface;
    d->_singleSelection = singleSelection;
        
    TQWidget* box = plainPage();
    TQVBoxLayout *dvlay = new TQVBoxLayout( box, 6 );

    //---------------------------------------------
   
    TQFrame *headerFrame = new TQFrame( box );
    headerFrame->setFrameStyle(TQFrame::Panel|TQFrame::Sunken);
    TQHBoxLayout* layout = new TQHBoxLayout( headerFrame );
    layout->setMargin( 2 ); // to make sure the frame gets displayed
    layout->setSpacing( 0 );
    TQLabel *pixmapLabelLeft = new TQLabel( headerFrame, "pixmapLabelLeft" );
    pixmapLabelLeft->setScaledContents( false );
    layout->addWidget( pixmapLabelLeft );
    TQLabel *labelTitle = new TQLabel( i18n("Select Image From Album"), headerFrame, "labelTitle" );
    layout->addWidget( labelTitle );
    layout->setStretchFactor( labelTitle, 1 );
    dvlay->addWidget( headerFrame );
    
    TQString directory;
    TDEGlobal::dirs()->addResourceType("kipi_banner_left", TDEGlobal::dirs()->kde_default("data") + "kipi/data");
    directory = TDEGlobal::dirs()->findResourceDir("kipi_banner_left", "banner_left.png");
    
    pixmapLabelLeft->setPaletteBackgroundColor( TQColor(201, 208, 255) );
    pixmapLabelLeft->setPixmap( TQPixmap( directory + "banner_left.png" ) );
    labelTitle->setPaletteBackgroundColor( TQColor(201, 208, 255) );

    //---------------------------------------------
    
    // About data and help button.
    
    TDEAboutData* about = new TDEAboutData("kipiplugins",
                                       I18N_NOOP("Image selector dialog"), 
                                       kipi_version,
                                       I18N_NOOP("A Kipi dialog for image selection"),
                                       TDEAboutData::License_GPL,
                                       "(c) 2004,2005, Kipi development team", 
                                       0,
                                       "http://extragear.kde.org/apps/kipi");
    
    TQPushButton *helpButton = actionButton( Help );
    KHelpMenu* helpMenu = new KHelpMenu(this, about, false);
    helpMenu->menu()->removeItemAt(0);
    helpMenu->menu()->insertItem(i18n("Kipi Plugins Handbooks"), this, TQT_SLOT(slotHelp()), 0, -1, 0);
    helpButton->setPopup( helpMenu->menu() );

    //---------------------------------------------

    TQSplitter* splitter = new TQSplitter(box);
        
    d->_albumList=new KListView(splitter);
    d->_albumList->addColumn(i18n("Album Name"));
    d->_albumList->setMinimumWidth(200);
    d->_albumList->setResizeMode(TQListView::LastColumn);

    d->_imageList=new KListView(splitter);
    d->_imageList->addColumn(i18n("Image Name"));
    d->_imageList->setMinimumWidth(200);
    d->_imageList->setSelectionMode(singleSelection ? TQListView::Single :
                                    TQListView::Extended);
    d->_imageList->setResizeMode(TQListView::LastColumn);
    
    d->_preview=new TQLabel(splitter);
    d->_preview->setAlignment(AlignHCenter | AlignVCenter | WordBreak);
    d->_preview->setFixedWidth(PREVIEW_SIZE);
    d->_preview->setText(i18n("No image selected"));

    dvlay->addWidget( splitter );

    d->_albums=d->_interface->allAlbums();
    TQValueList<ImageCollection>::ConstIterator it=d->_albums.begin();
    
    for(; it!=d->_albums.end(); ++it) {
        new AlbumLVI(d->_albumList, *it);
    }
    TQTimer::singleShot(0, this, TQT_SLOT(slotInitialShow()));

    connect(d->_albumList, TQT_SIGNAL(selectionChanged(TQListViewItem*)),
            this, TQT_SLOT(fillImageList(TQListViewItem*)) );

    if (singleSelection)
        connect(d->_imageList, TQT_SIGNAL(selectionChanged(TQListViewItem*)),
                this, TQT_SLOT(slotImageSelected(TQListViewItem*)) );
    else
        connect(d->_imageList, TQT_SIGNAL(selectionChanged()),
                this, TQT_SLOT(slotImagesSelected()));

    enableButtonOK(false);
}


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


KURL ImageDialog::url() const {
    return d->_url;
}

KURL::List ImageDialog::urls() const
{
    return d->_urls;    
}

KURL ImageDialog::getImageURL(TQWidget* parent, KIPI::Interface* interface) {
    ImageDialog dlg(parent, interface, true);
    if (dlg.exec() == TQDialog::Accepted) {
        return dlg.url();
    } else {
        return KURL();
    }
}

KURL::List ImageDialog::getImageURLs(TQWidget* parent, Interface* interface)
{
    ImageDialog dlg(parent, interface, false);
    if (dlg.exec() == TQDialog::Accepted)
        return dlg.urls();
    else
    {
        KURL::List urls;
        return urls;
    }
}


void ImageDialog::fillImageList(TQListViewItem* item) {
    d->_imageList->clear();
    if (!item) return;

    const KIPI::ImageCollection& album=static_cast<AlbumLVI*>(item)->_album;
    KURL::List images=album.images();

    KURL::List::ConstIterator it=images.begin();
    for (;it!=images.end(); ++it) {
        new ImageLVI(d->_imageList, *it);
    }
}


void ImageDialog::slotImageSelected(TQListViewItem* item) {
    if (!item) {
        enableButtonOK(false);
        d->_preview->setText(i18n("No image selected"));
        d->_url=KURL();
        return;
    }
    enableButtonOK(true);
    d->_url=static_cast<ImageLVI*>(item)->_url;
    d->_preview->clear();

    KIO::PreviewJob* thumbJob = KIO::filePreview(d->_url, PREVIEW_SIZE);
    connect( thumbJob, TQT_SIGNAL(gotPreview(const KFileItem*, const TQPixmap&)),
             TQT_SLOT(slotGotPreview(const KFileItem* , const TQPixmap&)));
}

void ImageDialog::slotImagesSelected()
{
    d->_url = KURL();
    d->_urls.clear();
    d->_preview->clear();

    TQListViewItem* selectedItem = 0;
    TQListViewItem* listItem = d->_imageList->firstChild();
    while (listItem)
    {
        if (listItem->isSelected())
        {
            selectedItem = listItem;
            d->_urls.append(static_cast<ImageLVI*>(listItem)->_url);
        }
        listItem = listItem->nextSibling();
    }

    if (!selectedItem)
    {
        enableButtonOK(false);
        d->_preview->setText(i18n("No images selected"));
        d->_url=KURL();
        d->_urls.clear();
        return;
    }

    enableButtonOK(true);
    
    if (d->_urls.count() == 1)
    {
        d->_url = d->_urls.first();

        KIO::PreviewJob* thumbJob = KIO::filePreview(d->_url, PREVIEW_SIZE);
        connect( thumbJob, TQT_SIGNAL(gotPreview(const KFileItem*, const TQPixmap&)),
                 TQT_SLOT(slotGotPreview(const KFileItem* , const TQPixmap&)));
    }
    else
    {
        d->_url = d->_urls.first();
        d->_preview->setText(i18n("1 image selected", "%n images selected", d->_urls.count()));
    }
}

void ImageDialog::slotGotPreview(const KFileItem*, const TQPixmap& pix) {

    d->_preview->setPixmap(pix);
}

void ImageDialog::slotHelp( void )
{
    TDEApplication::kApplication()->invokeHelp("", "kipi-plugins");
}

void ImageDialog::slotInitialShow()
{
    ImageCollection current = d->_interface->currentAlbum();

    TQListViewItemIterator it( d->_albumList );
    while ( it.current() )
    {
        AlbumLVI* lvi = static_cast<AlbumLVI*>( it.current() );
        if ( lvi->_album == current )
        {
            d->_albumList->ensureItemVisible( lvi );
            d->_albumList->setSelected( lvi, true );
            break;
        }
        ++it;
    }
}

} // namespace KIPI

#include "imagedialog.moc"