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

« back to all changes in this revision

Viewing changes to digikam/libs/dialogs/imagedialog.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:
 
1
/* ============================================================
 
2
 *
 
3
 * This file is a part of digiKam project
 
4
 * http://www.digikam.org
 
5
 *
 
6
 * Date        : 2008-03-13
 
7
 * Description : image files selector dialog.
 
8
 * 
 
9
 * Copyright (C) 2008 by Gilles Caulier <caulier dot gilles at gmail dot com> 
 
10
 *
 
11
 * This program is free software; you can redistribute it
 
12
 * and/or modify it under the terms of the GNU General
 
13
 * Public License as published by the Free Software Foundation;
 
14
 * either version 2, or (at your option)
 
15
 * any later version.
 
16
 * 
 
17
 * This program is distributed in the hope that it will be useful,
 
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
 * GNU General Public License for more details.
 
21
 *
 
22
 * ============================================================ */
 
23
 
 
24
// Qt includes.
 
25
 
 
26
#include <qlabel.h>
 
27
#include <qlayout.h>
 
28
#include <qguardedptr.h>
 
29
#include <qtimer.h>
 
30
 
 
31
// KDE includes.
 
32
 
 
33
#include <kapplication.h>
 
34
#include <klocale.h>
 
35
#include <kstandarddirs.h>
 
36
#include <kfiledialog.h>
 
37
#include <kimageio.h>
 
38
#include <kiconloader.h>
 
39
 
 
40
// LibKDcraw includes.
 
41
 
 
42
#include <libkdcraw/dcrawbinary.h>
 
43
 
 
44
// Local includes.
 
45
 
 
46
#include "ddebug.h"
 
47
#include "dmetadata.h"
 
48
#include "thumbnailsize.h"
 
49
#include "thumbnailjob.h"
 
50
#include "imagedialog.h"
 
51
#include "imagedialog.moc"
 
52
 
 
53
namespace Digikam
 
54
{
 
55
 
 
56
class ImageDialogPreviewPrivate 
 
57
{
 
58
 
 
59
public:
 
60
 
 
61
    ImageDialogPreviewPrivate()
 
62
    {
 
63
        imageLabel = 0;
 
64
        infoLabel  = 0;
 
65
        thumbJob   = 0;
 
66
        timer      = 0;
 
67
    }
 
68
 
 
69
    QTimer                    *timer;
 
70
 
 
71
    QLabel                    *imageLabel;
 
72
    QLabel                    *infoLabel;
 
73
 
 
74
    KURL                       currentURL;
 
75
 
 
76
    DMetadata                  metaIface;
 
77
 
 
78
    QGuardedPtr<ThumbnailJob>  thumbJob;
 
79
};
 
80
 
 
81
ImageDialogPreview::ImageDialogPreview(QWidget *parent)
 
82
                  : KPreviewWidgetBase(parent)
 
83
{
 
84
    d = new ImageDialogPreviewPrivate;
 
85
 
 
86
    QVBoxLayout *vlay = new QVBoxLayout(this);
 
87
    d->imageLabel     = new QLabel(this);
 
88
    d->imageLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
 
89
    d->imageLabel->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
 
90
 
 
91
    d->infoLabel = new QLabel(this);
 
92
 
 
93
    vlay->setMargin(0);
 
94
    vlay->setSpacing(KDialog::spacingHint());
 
95
    vlay->addWidget(d->imageLabel);
 
96
    vlay->addWidget(d->infoLabel);
 
97
 
 
98
    setSupportedMimeTypes(KImageIO::mimeTypes());
 
99
 
 
100
    d->timer = new QTimer(this);
 
101
 
 
102
    connect(d->timer, SIGNAL(timeout()), 
 
103
            this, SLOT(showPreview()) );
 
104
}
 
105
 
 
106
ImageDialogPreview::~ImageDialogPreview() 
 
107
{
 
108
    if (!d->thumbJob.isNull())
 
109
    {
 
110
        d->thumbJob->kill();
 
111
        d->thumbJob = 0;
 
112
    }
 
113
    delete d;
 
114
}
 
115
 
 
116
QSize ImageDialogPreview::sizeHint() const
 
117
{
 
118
    return QSize(256, 256);
 
119
}
 
120
 
 
121
void ImageDialogPreview::resizeEvent(QResizeEvent *)
 
122
{
 
123
    d->timer->start(100, true);
 
124
}
 
125
 
 
126
void ImageDialogPreview::showPreview()
 
127
{
 
128
    KURL url(d->currentURL);
 
129
    clearPreview();
 
130
    showPreview(url);
 
131
}
 
132
 
 
133
void ImageDialogPreview::showPreview(const KURL& url)
 
134
{
 
135
    if (!url.isValid()) 
 
136
    {
 
137
        clearPreview();
 
138
        return;
 
139
    }
 
140
 
 
141
    if (url != d->currentURL) 
 
142
    {
 
143
        clearPreview();
 
144
        d->currentURL = url;
 
145
 
 
146
        if (!d->thumbJob.isNull())
 
147
        {
 
148
        d->thumbJob->kill();
 
149
        d->thumbJob = 0;
 
150
        }
 
151
 
 
152
        d->thumbJob = new ThumbnailJob(url, ThumbnailSize::Huge, true, true);
 
153
 
 
154
        connect(d->thumbJob, SIGNAL(signalThumbnail(const KURL&, const QPixmap&)),
 
155
                this, SLOT(slotGotThumbnail(const KURL&, const QPixmap&)));
 
156
 
 
157
        connect(d->thumbJob, SIGNAL(signalFailed(const KURL&)),
 
158
                this, SLOT(slotFailedThumbnail(const KURL&)));
 
159
 
 
160
        d->metaIface.load(d->currentURL.path());
 
161
        PhotoInfoContainer info = d->metaIface.getPhotographInformations();
 
162
        if (!info.isEmpty())
 
163
        {
 
164
            QString identify;
 
165
            QString make, model, dateTime, aperture, focalLength, exposureTime, sensitivity;
 
166
            QString unavailable(i18n("<i>unavailable</i>"));
 
167
            QString cellBeg("<tr><td><nobr><font size=-1>");
 
168
            QString cellMid("</font></nobr></td><td><nobr><font size=-1>");
 
169
            QString cellEnd("</font></nobr></td></tr>");
 
170
 
 
171
            if (info.make.isEmpty()) make = unavailable;
 
172
            else make = info.make;
 
173
 
 
174
            if (info.model.isEmpty()) model = unavailable;
 
175
            else model = info.model;
 
176
 
 
177
            if (info.dateTime.isValid()) dateTime = unavailable;
 
178
            else dateTime = KGlobal::locale()->formatDateTime(info.dateTime, true, true);
 
179
 
 
180
            if (info.aperture.isEmpty()) aperture = unavailable; 
 
181
            else aperture = info.aperture;
 
182
 
 
183
            if (info.focalLength.isEmpty()) focalLength = unavailable; 
 
184
            else focalLength = info.focalLength;
 
185
 
 
186
            if (info.exposureTime.isEmpty()) exposureTime = unavailable; 
 
187
            else exposureTime = info.exposureTime;
 
188
 
 
189
            if (info.sensitivity.isEmpty()) sensitivity = unavailable; 
 
190
            else sensitivity = i18n("%1 ISO").arg(info.sensitivity);
 
191
 
 
192
            identify = "<table cellspacing=0 cellpadding=0>";
 
193
            identify += cellBeg + i18n("Make:")        + cellMid + make         + cellEnd;
 
194
            identify += cellBeg + i18n("Model:")       + cellMid + model        + cellEnd;
 
195
            identify += cellBeg + i18n("Created:")     + cellMid + dateTime     + cellEnd;
 
196
            identify += cellBeg + i18n("Aperture:")    + cellMid + aperture     + cellEnd;
 
197
            identify += cellBeg + i18n("Focal:")       + cellMid + focalLength  + cellEnd;
 
198
            identify += cellBeg + i18n("Exposure:")    + cellMid + exposureTime + cellEnd;
 
199
            identify += cellBeg + i18n("Sensitivity:") + cellMid + sensitivity  + cellEnd;
 
200
            identify += "</table>";
 
201
 
 
202
            d->infoLabel->setText(identify);
 
203
        }
 
204
        else
 
205
            d->infoLabel->clear();
 
206
    }
 
207
}
 
208
 
 
209
void ImageDialogPreview::slotGotThumbnail(const KURL& url, const QPixmap& pix)
 
210
{
 
211
    if (url == d->currentURL)
 
212
    {
 
213
        QPixmap pixmap;
 
214
        QSize s = d->imageLabel->contentsRect().size();
 
215
 
 
216
        if (s.width() < pix.width() || s.height() < pix.height())
 
217
            pixmap = pix.convertToImage().smoothScale(s, QImage::ScaleMin);
 
218
        else 
 
219
            pixmap = pix;
 
220
 
 
221
        d->imageLabel->setPixmap(pixmap);
 
222
    }
 
223
}
 
224
 
 
225
void ImageDialogPreview::slotFailedThumbnail(const KURL& /*url*/)
 
226
{
 
227
    KIconLoader* iconLoader = KApplication::kApplication()->iconLoader();
 
228
    d->imageLabel->setPixmap(iconLoader->loadIcon("image", KIcon::NoGroup, 128,
 
229
                             KIcon::DefaultState, 0, true));
 
230
}
 
231
 
 
232
void ImageDialogPreview::clearPreview()
 
233
{
 
234
    d->imageLabel->clear();
 
235
    d->infoLabel->clear();
 
236
    d->currentURL = KURL();
 
237
}
 
238
 
 
239
// ------------------------------------------------------------------------
 
240
 
 
241
class ImageDialogPrivate 
 
242
{
 
243
 
 
244
public:
 
245
 
 
246
    ImageDialogPrivate()
 
247
    {
 
248
        singleSelect = false;
 
249
    }
 
250
 
 
251
    bool       singleSelect;
 
252
 
 
253
    QString    fileformats;
 
254
 
 
255
    KURL       url;
 
256
    KURL::List urls;
 
257
};
 
258
 
 
259
ImageDialog::ImageDialog(QWidget* parent, const KURL &url, bool singleSelect, const QString& caption)
 
260
{
 
261
    d = new ImageDialogPrivate;
 
262
    d->singleSelect = singleSelect;
 
263
 
 
264
    QStringList patternList = QStringList::split('\n', KImageIO::pattern(KImageIO::Reading));
 
265
 
 
266
    // All Images from list must been always the first entry given by KDE API
 
267
    QString allPictures = patternList[0];
 
268
 
 
269
    // Add other files format witch are missing to All Images" type mime provided by KDE and remplace current.
 
270
    if (KDcrawIface::DcrawBinary::instance()->versionIsRight())
 
271
    {
 
272
        allPictures.insert(allPictures.find("|"), QString(KDcrawIface::DcrawBinary::instance()->rawFiles()) + QString(" *.JPE *.TIF"));
 
273
        patternList.remove(patternList[0]);
 
274
        patternList.prepend(allPictures);
 
275
    }
 
276
 
 
277
    // Added RAW file formats supported by dcraw program like a type mime. 
 
278
    // Nota: we cannot use here "image/x-raw" type mime from KDE because it uncomplete 
 
279
    // or unavailable(dcraw_0)(see file #121242 in B.K.O).
 
280
    if (KDcrawIface::DcrawBinary::instance()->versionIsRight())
 
281
    {
 
282
        patternList.append(i18n("\n%1|Camera RAW files").arg(QString(KDcrawIface::DcrawBinary::instance()->rawFiles())));
 
283
    }
 
284
 
 
285
    d->fileformats = patternList.join("\n");
 
286
 
 
287
    DDebug() << "fileformats=" << d->fileformats << endl;
 
288
 
 
289
    KFileDialog dlg(url.path(), d->fileformats, parent, "imageFileOpenDialog", false);
 
290
    ImageDialogPreview *preview = new ImageDialogPreview(&dlg);
 
291
    dlg.setPreviewWidget(preview);
 
292
    dlg.setOperationMode(KFileDialog::Opening);
 
293
 
 
294
    if (d->singleSelect)
 
295
    {
 
296
        dlg.setMode(KFile::File);
 
297
        if (caption.isEmpty()) dlg.setCaption(i18n("Select an Image"));
 
298
        else dlg.setCaption(caption);
 
299
        dlg.exec();
 
300
        d->url = dlg.selectedURL();
 
301
    }
 
302
    else
 
303
    {
 
304
        dlg.setMode(KFile::Files);
 
305
        if (caption.isEmpty()) dlg.setCaption(i18n("Select Images"));
 
306
        else dlg.setCaption(caption);
 
307
        dlg.exec();
 
308
        d->urls = dlg.selectedURLs();
 
309
    }
 
310
}
 
311
 
 
312
ImageDialog::~ImageDialog() 
 
313
{
 
314
    delete d;
 
315
}
 
316
 
 
317
bool ImageDialog::singleSelect() const 
 
318
{
 
319
    return d->singleSelect;
 
320
}
 
321
 
 
322
QString ImageDialog::fileformats() const 
 
323
{
 
324
    return d->fileformats;
 
325
}
 
326
 
 
327
KURL ImageDialog::url() const
 
328
{
 
329
    return d->url;
 
330
}
 
331
 
 
332
KURL::List ImageDialog::urls() const
 
333
{
 
334
    return d->urls;
 
335
}
 
336
 
 
337
KURL::List ImageDialog::getImageURLs(QWidget* parent, const KURL& url, const QString& caption)
 
338
{
 
339
    ImageDialog dlg(parent, url, false, caption);
 
340
    if (!dlg.urls().isEmpty())
 
341
        return dlg.urls();
 
342
    else
 
343
        return KURL::List();
 
344
}
 
345
 
 
346
KURL ImageDialog::getImageURL(QWidget* parent, const KURL& url, const QString& caption)
 
347
{
 
348
    ImageDialog dlg(parent, url, true, caption);
 
349
    if (dlg.url() != KURL())
 
350
        return dlg.url();
 
351
    else
 
352
        return KURL();
 
353
}
 
354
 
 
355
} // namespace Digikam