~ubuntu-branches/ubuntu/raring/gwenview/raring-proposed

« back to all changes in this revision

Viewing changes to lib/documentview/documentview.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac, Philip Muškovac, Scott Kitterman
  • Date: 2011-12-24 18:54:55 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20111224185455-zxffs6i6m5cwwuze
Tags: 4:4.7.95-0ubuntu1
[ Philip Muškovac ]
* New upstream release candiate

[ Scott Kitterman ]
* Fix Ubuntu Vcs- header in debian/control

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
// Qt
25
25
#include <QAbstractScrollArea>
 
26
#include <QGraphicsLinearLayout>
26
27
#include <QGraphicsProxyWidget>
27
28
#include <QGraphicsScene>
28
29
#include <QGraphicsSceneMouseEvent>
31
32
#include <QPainter>
32
33
#include <QPropertyAnimation>
33
34
#include <QScrollBar>
34
 
#include <QToolButton>
35
35
#include <QVBoxLayout>
36
36
#include <QWeakPointer>
37
37
 
38
38
// KDE
39
 
#include <kdebug.h>
40
 
#include <kiconloader.h>
41
 
#include <klocale.h>
42
 
#include <kurl.h>
 
39
#include <KDebug>
 
40
#include <KIcon>
 
41
#include <KLocale>
 
42
#include <KUrl>
43
43
 
44
44
// Local
45
45
#include <lib/document/document.h>
52
52
#include <lib/documentview/rasterimageview.h>
53
53
#include <lib/documentview/svgviewadapter.h>
54
54
#include <lib/documentview/videoviewadapter.h>
 
55
#include <lib/graphicshudbutton.h>
55
56
#include <lib/graphicshudwidget.h>
56
57
#include <lib/graphicswidgetfloater.h>
57
58
#include <lib/gwenviewconfig.h>
75
76
 
76
77
static const int COMPARE_MARGIN = 4;
77
78
 
78
 
struct DocumentViewPrivate {
 
79
const int DocumentView::MaximumZoom = 16;
 
80
const int DocumentView::AnimDuration = 500;
 
81
 
 
82
struct DocumentViewPrivate
 
83
{
79
84
    DocumentView* q;
 
85
    int mSortKey; // Used to sort views when displayed in compare mode
80
86
    GraphicsHudWidget* mHud;
81
87
    BirdEyeView* mBirdEyeView;
82
88
    QWeakPointer<QPropertyAnimation> mMoveAnimation;
89
95
    Document::Ptr mDocument;
90
96
    bool mCurrent;
91
97
    bool mCompareMode;
 
98
    bool mEraseBorders;
92
99
 
93
100
    void setCurrentAdapter(AbstractDocumentViewAdapter* adapter)
94
101
    {
137
144
        floater->setChildWidget(mLoadingIndicator);
138
145
    }
139
146
 
140
 
    QToolButton* createHudButton(const QString& text, const char* iconName, bool showText)
 
147
    GraphicsHudButton* createHudButton(const QString& text, const char* iconName, bool showText)
141
148
    {
142
 
        QToolButton* button = new QToolButton;
 
149
        GraphicsHudButton* button = new GraphicsHudButton;
143
150
        if (showText) {
144
 
            button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
145
151
            button->setText(text);
146
152
        } else {
147
153
            button->setToolTip(text);
148
154
        }
149
 
        button->setIcon(SmallIcon(iconName));
 
155
        button->setIcon(KIcon(iconName));
150
156
        return button;
151
157
    }
152
158
 
153
159
    void setupHud()
154
160
    {
155
 
        QToolButton* trashButton = createHudButton(i18n("Trash"), "user-trash", false);
156
 
        QToolButton* deselectButton = createHudButton(i18n("Deselect"), "list-remove", true);
 
161
        GraphicsHudButton* trashButton = createHudButton(i18n("Trash"), "user-trash", false);
 
162
        GraphicsHudButton* deselectButton = createHudButton(i18n("Deselect"), "list-remove", true);
157
163
 
158
 
        QWidget* content = new QWidget;
159
 
        QHBoxLayout* layout = new QHBoxLayout(content);
160
 
        layout->setMargin(0);
161
 
        layout->setSpacing(4);
162
 
        layout->addWidget(trashButton);
163
 
        layout->addWidget(deselectButton);
 
164
        QGraphicsWidget* content = new QGraphicsWidget;
 
165
        QGraphicsLinearLayout* layout = new QGraphicsLinearLayout(content);
 
166
        layout->addItem(trashButton);
 
167
        layout->addItem(deselectButton);
164
168
 
165
169
        mHud = new GraphicsHudWidget(q);
166
170
        mHud->init(content, GraphicsHudWidget::OptionNone);
260
264
        mLoadingIndicator->hide();
261
265
    }
262
266
 
263
 
    void animate(QPropertyAnimation* anim)
264
 
    {
265
 
        QObject::connect(anim, SIGNAL(finished()),
266
 
                         q, SLOT(slotAnimationFinished()));
267
 
        anim->setDuration(500);
268
 
        // FIXME: If anim is not started with a QueuedConnection, then this fails:
269
 
        // - Start Gwenview
270
 
        // - Browse a folder
271
 
        // - Click an image => nothing appears!
272
 
        QMetaObject::invokeMethod(anim, "start", Qt::QueuedConnection);
273
 
    }
274
 
 
275
267
    void resizeAdapterWidget()
276
268
    {
277
269
        QRectF rect = QRectF(QPointF(0, 0), q->boundingRect().size());
284
276
    void fadeTo(qreal value)
285
277
    {
286
278
        if (mFadeAnimation.data()) {
287
 
            // Reuse existing fade animation
288
 
            mFadeAnimation.data()->stop();
289
 
            mFadeAnimation.data()->setStartValue(q->opacity());
290
 
            mFadeAnimation.data()->setEndValue(value);
291
 
            mFadeAnimation.data()->start();
292
 
            return;
 
279
            qreal endValue = mFadeAnimation.data()->endValue().toReal();
 
280
            if (qFuzzyCompare(value, endValue)) {
 
281
                // Same end value, don't change the actual animation
 
282
                return;
 
283
            }
293
284
        }
294
285
        // Create a new fade animation
295
286
        QPropertyAnimation* anim = new QPropertyAnimation(q, "opacity");
296
287
        anim->setStartValue(q->opacity());
297
288
        anim->setEndValue(value);
298
 
        animate(anim);
 
289
        if (qFuzzyCompare(value, 1)) {
 
290
            QObject::connect(anim, SIGNAL(finished()),
 
291
                            q, SLOT(slotFadeInFinished()));
 
292
        }
 
293
        anim->setDuration(DocumentView::AnimDuration);
 
294
        anim->start(QAbstractAnimation::DeleteWhenStopped);
 
295
 
299
296
        mFadeAnimation = anim;
300
297
    }
301
298
};
312
309
    d->mBirdEyeView = 0;
313
310
    d->mCurrent = false;
314
311
    d->mCompareMode = false;
 
312
    d->mEraseBorders = false;
315
313
 
316
314
    setOpacity(0);
317
315
 
397
395
    }
398
396
    createAdapterForDocument();
399
397
 
400
 
    connect(d->mDocument.data(), SIGNAL(downSampledImageReady()),
401
 
            SLOT(slotLoaded()));
402
 
    connect(d->mDocument.data(), SIGNAL(loaded(KUrl)),
403
 
            SLOT(slotLoaded()));
 
398
    connect(d->mAdapter.data(), SIGNAL(completed()),
 
399
            SLOT(slotCompleted()));
404
400
    connect(d->mDocument.data(), SIGNAL(loadingFailed(KUrl)),
405
401
            SLOT(slotLoadingFailed()));
406
402
    d->mAdapter->setDocument(d->mDocument);
407
403
    d->updateCaption();
408
 
 
409
 
    if (d->mDocument->loadingState() == Document::Loaded) {
410
 
        slotLoaded();
411
 
    }
412
404
}
413
405
 
414
406
bool DocumentView::isEmpty() const
426
418
    return d->mAdapter->rasterImageView();
427
419
}
428
420
 
429
 
void DocumentView::slotLoaded()
 
421
void DocumentView::slotCompleted()
430
422
{
431
423
    d->hideLoadingIndicator();
432
424
    d->updateCaption();
562
554
 
563
555
void DocumentView::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/)
564
556
{
565
 
    if (!d->mCompareMode) {
566
 
        return;
 
557
    QRectF visibleRect = mapRectFromItem(d->mAdapter->widget(), d->mAdapter->visibleDocumentRect());
 
558
    if (d->mEraseBorders) {
 
559
        QRegion borders = QRegion(boundingRect().toRect())
 
560
            - QRegion(visibleRect.toRect());
 
561
        Q_FOREACH(const QRect& rect, borders.rects()) {
 
562
            painter->eraseRect(rect);
 
563
        }
567
564
    }
568
 
    if (d->mCurrent) {
 
565
 
 
566
    if (d->mCompareMode && d->mCurrent) {
569
567
        painter->save();
570
568
        painter->setBrush(Qt::NoBrush);
571
569
        painter->setPen(QPen(palette().highlight().color(), 2));
572
570
        painter->setRenderHint(QPainter::Antialiasing);
573
 
        QRectF selectionRect = boundingRect().adjusted(2, 2, -2, -2);
 
571
        QRectF selectionRect = visibleRect.adjusted(-2, -2, 2, 2);
574
572
        painter->drawRoundedRect(selectionRect, 3, 3);
575
573
        painter->restore();
576
574
    }
677
675
    QPropertyAnimation* anim = new QPropertyAnimation(this, "geometry");
678
676
    anim->setStartValue(geometry());
679
677
    anim->setEndValue(rect);
680
 
    d->animate(anim);
 
678
    anim->setDuration(DocumentView::AnimDuration);
 
679
    anim->start(QAbstractAnimation::DeleteWhenStopped);
681
680
    d->mMoveAnimation = anim;
682
681
}
683
682
 
684
 
void DocumentView::fadeIn()
 
683
QPropertyAnimation* DocumentView::fadeIn()
685
684
{
686
685
    d->fadeTo(1);
 
686
    return d->mFadeAnimation.data();
687
687
}
688
688
 
689
689
void DocumentView::fadeOut()
691
691
    d->fadeTo(0);
692
692
}
693
693
 
694
 
void DocumentView::slotAnimationFinished()
 
694
void DocumentView::slotFadeInFinished()
695
695
{
696
 
    animationFinished(this);
 
696
    fadeInFinished(this);
697
697
}
698
698
 
699
699
bool DocumentView::sceneEventFilter(QGraphicsItem*, QEvent* event)
709
709
    return imageView() ? imageView()->currentTool() : 0;
710
710
}
711
711
 
 
712
int DocumentView::sortKey() const
 
713
{
 
714
    return d->mSortKey;
 
715
}
 
716
 
 
717
void DocumentView::setSortKey(int sortKey)
 
718
{
 
719
    d->mSortKey = sortKey;
 
720
}
 
721
 
 
722
void DocumentView::setEraseBorders(bool value)
 
723
{
 
724
    d->mEraseBorders = value;
 
725
}
 
726
 
 
727
void DocumentView::hideAndDeleteLater()
 
728
{
 
729
    hide();
 
730
    deleteLater();
 
731
}
 
732
 
712
733
} // namespace