summaryrefslogtreecommitdiffstats
path: root/digikam/libs/dialogs/imagedialog.cpp
blob: 5304126f3437a779eebcc1abe9eeac050baaca4b (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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2008-03-13
 * Description : image files selector dialog.
 * 
 * Copyright (C) 2008 by Gilles Caulier <caulier dot gilles at gmail dot com> 
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU 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 General Public License for more details.
 *
 * ============================================================ */

// TQt includes.

#include <tqlabel.h>
#include <tqlayout.h>
#include <tqguardedptr.h>
#include <tqtimer.h>

// KDE includes.

#include <tdeapplication.h>
#include <tdelocale.h>
#include <kstandarddirs.h>
#include <tdefiledialog.h>
#include <kimageio.h>
#include <kiconloader.h>

// LibKDcraw includes.

#include <libkdcraw/version.h>
#include <libkdcraw/kdcraw.h>

#if KDCRAW_VERSION < 0x000106
#include <libkdcraw/dcrawbinary.h>
#endif

// Local includes.

#include "ddebug.h"
#include "dmetadata.h"
#include "thumbnailsize.h"
#include "thumbnailjob.h"
#include "imagedialog.h"
#include "imagedialog.moc"

namespace Digikam
{

class ImageDialogPreviewPrivate 
{

public:

    ImageDialogPreviewPrivate()
    {
        imageLabel = 0;
        infoLabel  = 0;
        thumbJob   = 0;
        timer      = 0;
    }

    TQTimer                    *timer;

    TQLabel                    *imageLabel;
    TQLabel                    *infoLabel;

    KURL                       currentURL;

    DMetadata                  metaIface;

    TQGuardedPtr<ThumbnailJob>  thumbJob;
};

ImageDialogPreview::ImageDialogPreview(TQWidget *parent)
                  : KPreviewWidgetBase(parent)
{
    d = new ImageDialogPreviewPrivate;

    TQVBoxLayout *vlay = new TQVBoxLayout(this);
    d->imageLabel     = new TQLabel(this);
    d->imageLabel->setAlignment(TQt::AlignHCenter | TQt::AlignVCenter);
    d->imageLabel->setSizePolicy(TQSizePolicy(TQSizePolicy::Expanding, TQSizePolicy::Expanding));

    d->infoLabel = new TQLabel(this);

    vlay->setMargin(0);
    vlay->setSpacing(KDialog::spacingHint());
    vlay->addWidget(d->imageLabel);
    vlay->addWidget(d->infoLabel);

    setSupportedMimeTypes(KImageIO::mimeTypes());

    d->timer = new TQTimer(this);

    connect(d->timer, TQT_SIGNAL(timeout()), 
            this, TQT_SLOT(showPreview()) );
}

ImageDialogPreview::~ImageDialogPreview() 
{
    if (!d->thumbJob.isNull())
    {
        d->thumbJob->kill();
        d->thumbJob = 0;
    }
    delete d;
}

TQSize ImageDialogPreview::sizeHint() const
{
    return TQSize(256, 256);
}

void ImageDialogPreview::resizeEvent(TQResizeEvent *)
{
    d->timer->start(100, true);
}

void ImageDialogPreview::showPreview()
{
    KURL url(d->currentURL);
    clearPreview();
    showPreview(url);
}

void ImageDialogPreview::showPreview(const KURL& url)
{
    if (!url.isValid()) 
    {
        clearPreview();
        return;
    }

    if (url != d->currentURL) 
    {
        clearPreview();
        d->currentURL = url;

        if (!d->thumbJob.isNull())
        {
        d->thumbJob->kill();
        d->thumbJob = 0;
        }

        d->thumbJob = new ThumbnailJob(url, ThumbnailSize::Huge, true, true);

        connect(d->thumbJob, TQT_SIGNAL(signalThumbnail(const KURL&, const TQPixmap&)),
                this, TQT_SLOT(slotGotThumbnail(const KURL&, const TQPixmap&)));

        connect(d->thumbJob, TQT_SIGNAL(signalFailed(const KURL&)),
                this, TQT_SLOT(slotFailedThumbnail(const KURL&)));

        d->metaIface.load(d->currentURL.path());
        PhotoInfoContainer info = d->metaIface.getPhotographInformations();
        if (!info.isEmpty())
        {
            TQString identify;
            TQString make, model, dateTime, aperture, focalLength, exposureTime, sensitivity;
            TQString unavailable(i18n("<i>unavailable</i>"));
            TQString cellBeg("<tr><td><nobr><font size=-1>");
            TQString cellMid("</font></nobr></td><td><nobr><font size=-1>");
            TQString cellEnd("</font></nobr></td></tr>");

            if (info.make.isEmpty()) make = unavailable;
            else make = info.make;

            if (info.model.isEmpty()) model = unavailable;
            else model = info.model;

            if (!info.dateTime.isValid()) dateTime = unavailable;
            else dateTime = TDEGlobal::locale()->formatDateTime(info.dateTime, true, true);

            if (info.aperture.isEmpty()) aperture = unavailable; 
            else aperture = info.aperture;

            if (info.focalLength.isEmpty()) focalLength = unavailable; 
            else focalLength = info.focalLength;

            if (info.exposureTime.isEmpty()) exposureTime = unavailable; 
            else exposureTime = info.exposureTime;

            if (info.sensitivity.isEmpty()) sensitivity = unavailable; 
            else sensitivity = i18n("%1 ISO").arg(info.sensitivity);

            identify = "<table cellspacing=0 cellpadding=0>";
            identify += cellBeg + i18n("Make:")        + cellMid + make         + cellEnd;
            identify += cellBeg + i18n("Model:")       + cellMid + model        + cellEnd;
            identify += cellBeg + i18n("Created:")     + cellMid + dateTime     + cellEnd;
            identify += cellBeg + i18n("Aperture:")    + cellMid + aperture     + cellEnd;
            identify += cellBeg + i18n("Focal:")       + cellMid + focalLength  + cellEnd;
            identify += cellBeg + i18n("Exposure:")    + cellMid + exposureTime + cellEnd;
            identify += cellBeg + i18n("Sensitivity:") + cellMid + sensitivity  + cellEnd;
            identify += "</table>";

            d->infoLabel->setText(identify);
        }
        else
            d->infoLabel->clear();
    }
}

void ImageDialogPreview::slotGotThumbnail(const KURL& url, const TQPixmap& pix)
{
    if (url == d->currentURL)
    {
        TQPixmap pixmap;
        TQSize s = d->imageLabel->contentsRect().size();

        if (s.width() < pix.width() || s.height() < pix.height())
            pixmap = pix.convertToImage().smoothScale(s, TQImage::ScaleMin);
        else 
            pixmap = pix;

        d->imageLabel->setPixmap(pixmap);
    }
}

void ImageDialogPreview::slotFailedThumbnail(const KURL& /*url*/)
{
    TDEIconLoader* iconLoader = TDEApplication::kApplication()->iconLoader();
    d->imageLabel->setPixmap(iconLoader->loadIcon("image-x-generic", TDEIcon::NoGroup, 128,
                             TDEIcon::DefaultState, 0, true));
}

void ImageDialogPreview::clearPreview()
{
    d->imageLabel->clear();
    d->infoLabel->clear();
    d->currentURL = KURL();
}

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

class ImageDialogPrivate 
{

public:

    ImageDialogPrivate()
    {
        singleSelect = false;
    }

    bool       singleSelect;

    TQString    fileformats;

    KURL       url;
    KURL::List urls;
};

ImageDialog::ImageDialog(TQWidget* parent, const KURL &url, bool singleSelect, const TQString& caption)
{
    d = new ImageDialogPrivate;
    d->singleSelect = singleSelect;

    TQStringList patternList = TQStringList::split('\n', KImageIO::pattern(KImageIO::Reading));

    // All Images from list must been always the first entry given by KDE API
    TQString allPictures = patternList[0];

#if KDCRAW_VERSION < 0x000106
    // Add other files format witch are missing to All Images" type mime provided by KDE and remplace current.
    if (KDcrawIface::DcrawBinary::instance()->versionIsRight())
    {
        allPictures.insert(allPictures.find("|"), TQString(KDcrawIface::DcrawBinary::instance()->rawFiles()) + TQString(" *.JPE *.TIF"));
        patternList.remove(patternList[0]);
        patternList.prepend(allPictures);
        // Added RAW file formats supported by dcraw program like a type mime. 
        // Nota: we cannot use here "image/x-raw" type mime from KDE because it uncomplete 
        // or unavailable (see file #121242 in B.K.O).
        patternList.append(i18n("\n%1|Camera RAW files").arg(TQString(KDcrawIface::DcrawBinary::instance()->rawFiles())));
    }
#else
    allPictures.insert(allPictures.find("|"), TQString(KDcrawIface::KDcraw::rawFiles()) + TQString(" *.JPE *.TIF"));
    patternList.remove(patternList[0]);
    patternList.prepend(allPictures);
    // Added RAW file formats supported by dcraw program like a type mime. 
    // Nota: we cannot use here "image/x-raw" type mime from KDE because it uncomplete 
    // or unavailable (see file #121242 in B.K.O).
    patternList.append(i18n("\n%1|Camera RAW files").arg(TQString(KDcrawIface::KDcraw::rawFiles())));
#endif

    d->fileformats = patternList.join("\n");

    DDebug() << "fileformats=" << d->fileformats << endl;

    KFileDialog dlg(url.path(), d->fileformats, parent, "imageFileOpenDialog", false);
    ImageDialogPreview *preview = new ImageDialogPreview(&dlg);
    dlg.setPreviewWidget(preview);
    dlg.setOperationMode(KFileDialog::Opening);

    if (d->singleSelect)
    {
        dlg.setMode(KFile::File);
        if (caption.isEmpty()) dlg.setCaption(i18n("Select an Image"));
        else dlg.setCaption(caption);
        dlg.exec();
        d->url = dlg.selectedURL();
    }
    else
    {
        dlg.setMode(KFile::Files);
        if (caption.isEmpty()) dlg.setCaption(i18n("Select Images"));
        else dlg.setCaption(caption);
        dlg.exec();
        d->urls = dlg.selectedURLs();
    }
}

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

bool ImageDialog::singleSelect() const 
{
    return d->singleSelect;
}

TQString ImageDialog::fileformats() const 
{
    return d->fileformats;
}

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

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

KURL::List ImageDialog::getImageURLs(TQWidget* parent, const KURL& url, const TQString& caption)
{
    ImageDialog dlg(parent, url, false, caption);
    if (!dlg.urls().isEmpty())
        return dlg.urls();
    else
        return KURL::List();
}

KURL ImageDialog::getImageURL(TQWidget* parent, const KURL& url, const TQString& caption)
{
    ImageDialog dlg(parent, url, true, caption);
    if (dlg.url() != KURL())
        return dlg.url();
    else
        return KURL();
}

} // namespace Digikam