~ubuntu-branches/ubuntu/precise/gwenview/precise-proposed

« back to all changes in this revision

Viewing changes to lib/slidecontainer.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:54 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20111215141754-z043hyx69dulbggf
Tags: upstream-4.7.90
ImportĀ upstreamĀ versionĀ 4.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
// Self
21
21
#include "slidecontainer.moc"
22
22
 
 
23
// KDE
 
24
#include <kdebug.h>
 
25
 
23
26
// Qt
24
27
#include <QEvent>
25
 
 
26
 
namespace Gwenview {
27
 
 
 
28
#include <QPropertyAnimation>
 
29
#include <QResizeEvent>
 
30
 
 
31
namespace Gwenview
 
32
{
 
33
 
 
34
static const int SLIDE_DURATION = 250;
28
35
 
29
36
SlideContainer::SlideContainer(QWidget* parent)
30
 
: QFrame(parent) {
31
 
        mContent = 0;
32
 
        mTimeLine = new QTimeLine(500, this);
33
 
        connect(mTimeLine, SIGNAL(valueChanged(qreal)), SLOT(slotTimeLineChanged(qreal)) );
34
 
        connect(mTimeLine, SIGNAL(finished()), SLOT(slotTimeLineFinished()) );
35
 
        hide();
36
 
}
37
 
 
38
 
 
39
 
QWidget* SlideContainer::content() const {
40
 
        return mContent;
41
 
}
42
 
 
43
 
 
44
 
void SlideContainer::setContent(QWidget* content) {
45
 
        if (mContent) {
46
 
                mContent->setParent(0);
47
 
        }
48
 
        mContent = content;
49
 
        mContent->setParent(this);
50
 
        mContent->hide();
51
 
        mContent->installEventFilter(this);
52
 
}
53
 
 
54
 
 
55
 
void SlideContainer::slideIn() {
56
 
        mContent->adjustSize();
57
 
        if (mTimeLine->direction() == QTimeLine::Backward) {
58
 
                mTimeLine->setDirection(QTimeLine::Forward);
59
 
        }
60
 
        if (!isVisible() && mTimeLine->state() == QTimeLine::NotRunning) {
61
 
                show();
62
 
                mTimeLine->start();
63
 
        }
64
 
}
65
 
 
66
 
 
67
 
void SlideContainer::slideOut() {
68
 
        if (mTimeLine->direction() == QTimeLine::Forward) {
69
 
                mTimeLine->setDirection(QTimeLine::Backward);
70
 
        }
71
 
        if (mTimeLine->state() == QTimeLine::NotRunning) {
72
 
                mTimeLine->start();
73
 
        }
74
 
}
75
 
 
76
 
 
77
 
void SlideContainer::slotTimeLineChanged(qreal value) {
78
 
        int posY = int( (value - 1.0) * height() );
79
 
        mContent->setGeometry(0, posY, width(), height());
80
 
        mContent->show();
81
 
}
82
 
 
83
 
 
84
 
void SlideContainer::slotTimeLineFinished() {
85
 
        if (mTimeLine->direction() == QTimeLine::Backward) {
86
 
                hide();
87
 
        }
88
 
}
89
 
 
90
 
 
91
 
QSize SlideContainer::sizeHint() const {
92
 
        if (mContent) {
93
 
                return mContent->sizeHint();
94
 
        } else {
95
 
                return QSize();
96
 
        }
97
 
}
98
 
 
99
 
 
100
 
QSize SlideContainer::minimumSizeHint() const {
101
 
        if (mContent) {
102
 
                return mContent->minimumSizeHint();
103
 
        } else {
104
 
                return QSize();
105
 
        }
106
 
}
107
 
 
108
 
 
109
 
void SlideContainer::resizeEvent(QResizeEvent*) {
110
 
        if (mContent) {
111
 
                mContent->resize(size());
112
 
        }
113
 
}
114
 
 
115
 
 
116
 
bool SlideContainer::eventFilter(QObject*, QEvent* event) {
117
 
        if (event->type() == QEvent::Resize) {
118
 
                mContent->resize(width(), mContent->height());
119
 
        }
120
 
        return false;
121
 
}
122
 
 
 
37
: QFrame(parent)
 
38
{
 
39
    mContent = 0;
 
40
    mSlidingOut = false;
 
41
    setFixedHeight(0);
 
42
}
 
43
 
 
44
QWidget* SlideContainer::content() const
 
45
{
 
46
    return mContent;
 
47
}
 
48
 
 
49
void SlideContainer::setContent(QWidget* content)
 
50
{
 
51
    if (mContent) {
 
52
        mContent->setParent(0);
 
53
        mContent->removeEventFilter(this);
 
54
    }
 
55
    mContent = content;
 
56
    if (mContent) {
 
57
        mContent->setParent(this);
 
58
        mContent->installEventFilter(this);
 
59
        mContent->hide();
 
60
    }
 
61
}
 
62
 
 
63
void SlideContainer::animTo(int newHeight)
 
64
{
 
65
    delete mAnim.data();
 
66
    QPropertyAnimation* anim = new QPropertyAnimation(this, "slideHeight", this);
 
67
    anim->setDuration(SLIDE_DURATION);
 
68
    anim->setStartValue(slideHeight());
 
69
    anim->setEndValue(newHeight);
 
70
    anim->start(QAbstractAnimation::DeleteWhenStopped);
 
71
    mAnim = anim;
 
72
}
 
73
 
 
74
void SlideContainer::slideIn()
 
75
{
 
76
    mSlidingOut = false;
 
77
    mContent->show();
 
78
    mContent->adjustSize();
 
79
    delete mAnim.data();
 
80
    if (height() == mContent->height()) {
 
81
        return;
 
82
    }
 
83
    animTo(mContent->height());
 
84
}
 
85
 
 
86
void SlideContainer::slideOut()
 
87
{
 
88
    if (height() == 0) {
 
89
        return;
 
90
    }
 
91
    mSlidingOut = true;
 
92
    animTo(0);
 
93
    connect(mAnim.data(), SIGNAL(finished()), SLOT(slotSlidedOut()));
 
94
}
 
95
 
 
96
void SlideContainer::slotSlidedOut()
 
97
{
 
98
    mSlidingOut = false;
 
99
}
 
100
 
 
101
QSize SlideContainer::sizeHint() const
 
102
{
 
103
    if (mContent) {
 
104
        return mContent->sizeHint();
 
105
    } else {
 
106
        return QSize();
 
107
    }
 
108
}
 
109
 
 
110
QSize SlideContainer::minimumSizeHint() const
 
111
{
 
112
    if (mContent) {
 
113
        return mContent->minimumSizeHint();
 
114
    } else {
 
115
        return QSize();
 
116
    }
 
117
}
 
118
 
 
119
void SlideContainer::resizeEvent(QResizeEvent* event)
 
120
{
 
121
    if (mContent) {
 
122
        if (event->oldSize().width() != width()) {
 
123
            adjustContentGeometry();
 
124
        }
 
125
    }
 
126
}
 
127
 
 
128
void SlideContainer::adjustContentGeometry()
 
129
{
 
130
    if (mContent) {
 
131
        mContent->setGeometry(0, height() - mContent->height(), width(), mContent->height());
 
132
    }
 
133
}
 
134
 
 
135
bool SlideContainer::eventFilter(QObject*, QEvent* event)
 
136
{
 
137
    if (event->type() == QEvent::Resize) {
 
138
        if (!mSlidingOut && height() != 0) {
 
139
            animTo(mContent->height());
 
140
        }
 
141
    }
 
142
    return false;
 
143
}
 
144
 
 
145
int SlideContainer::slideHeight() const
 
146
{
 
147
    return isVisible() ? height() : 0;
 
148
}
 
149
 
 
150
void SlideContainer::setSlideHeight(int value)
 
151
{
 
152
    setFixedHeight(value);
 
153
    adjustContentGeometry();
 
154
}
123
155
 
124
156
} // namespace