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

« back to all changes in this revision

Viewing changes to lib/graphicswidgetfloater.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:
 
1
// vim: set tabstop=4 shiftwidth=4 expandtab:
 
2
/*
 
3
Gwenview: an image viewer
 
4
Copyright 2008 Aurélien Gâteau <agateau@kde.org>
 
5
 
 
6
This program is free software; you can redistribute it and/or
 
7
modify it under the terms of the GNU General Public License
 
8
as published by the Free Software Foundation; either version 2
 
9
of the License, or (at your option) any later version.
 
10
 
 
11
This program is distributed in the hope that it will be useful,
 
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
GNU General Public License for more details.
 
15
 
 
16
You should have received a copy of the GNU General Public License
 
17
along with this program; if not, write to the Free Software
 
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
 
19
 
 
20
*/
 
21
// Self
 
22
#include "graphicswidgetfloater.moc"
 
23
 
 
24
// Qt
 
25
#include <QEvent>
 
26
#include <QGraphicsWidget>
 
27
#include <QPointer>
 
28
 
 
29
// KDE
 
30
#include <kdebug.h>
 
31
#include <kdialog.h>
 
32
 
 
33
// Local
 
34
 
 
35
namespace Gwenview
 
36
{
 
37
 
 
38
struct GraphicsWidgetFloaterPrivate {
 
39
    QGraphicsWidget* mParent;
 
40
    QPointer<QGraphicsWidget> mChild;
 
41
    Qt::Alignment mAlignment;
 
42
 
 
43
    int mHorizontalMargin;
 
44
    int mVerticalMargin;
 
45
    bool mInsideUpdateChildGeometry;
 
46
 
 
47
    void updateChildGeometry()
 
48
    {
 
49
        if (!mChild) {
 
50
            return;
 
51
        }
 
52
        if (mInsideUpdateChildGeometry) {
 
53
            return;
 
54
        }
 
55
        mInsideUpdateChildGeometry = true;
 
56
 
 
57
        qreal posX, posY;
 
58
        qreal childWidth, childHeight;
 
59
        qreal parentWidth, parentHeight;
 
60
 
 
61
        childWidth = mChild->size().width();
 
62
        childHeight = mChild->size().height();
 
63
 
 
64
        parentWidth = mParent->size().width();
 
65
        parentHeight = mParent->size().height();
 
66
 
 
67
        if (mAlignment & Qt::AlignLeft) {
 
68
            posX = mHorizontalMargin;
 
69
        } else if (mAlignment & Qt::AlignHCenter) {
 
70
            posX = (parentWidth - childWidth) / 2;
 
71
        } else if (mAlignment & Qt::AlignJustify) {
 
72
            posX = mHorizontalMargin;
 
73
            childWidth = parentWidth - 2 * mHorizontalMargin;
 
74
            QRectF childGeometry = mChild->geometry();
 
75
            childGeometry.setWidth(childWidth);
 
76
            mChild->setGeometry(childGeometry);
 
77
        } else {
 
78
            posX = parentWidth - childWidth - mHorizontalMargin;
 
79
        }
 
80
 
 
81
        if (mAlignment & Qt::AlignTop) {
 
82
            posY = mVerticalMargin;
 
83
        } else if (mAlignment & Qt::AlignVCenter) {
 
84
            posY = (parentHeight - childHeight) / 2;
 
85
        } else {
 
86
            posY = parentHeight - childHeight - mVerticalMargin;
 
87
        }
 
88
 
 
89
        mChild->setGeometry(posX, posY, childWidth, childHeight);
 
90
 
 
91
        mInsideUpdateChildGeometry = false;
 
92
    }
 
93
};
 
94
 
 
95
GraphicsWidgetFloater::GraphicsWidgetFloater(QGraphicsWidget* parent)
 
96
: QObject(parent)
 
97
, d(new GraphicsWidgetFloaterPrivate)
 
98
{
 
99
    Q_ASSERT(parent);
 
100
    d->mParent = parent;
 
101
    d->mParent->installEventFilter(this);
 
102
    d->mChild = 0;
 
103
    d->mAlignment = Qt::AlignCenter;
 
104
    d->mHorizontalMargin = KDialog::marginHint();
 
105
    d->mVerticalMargin = KDialog::marginHint();
 
106
    d->mInsideUpdateChildGeometry = false;
 
107
}
 
108
 
 
109
GraphicsWidgetFloater::~GraphicsWidgetFloater()
 
110
{
 
111
    delete d;
 
112
}
 
113
 
 
114
void GraphicsWidgetFloater::setChildWidget(QGraphicsWidget* child)
 
115
{
 
116
    if (d->mChild) {
 
117
        d->mChild->removeEventFilter(this);
 
118
        disconnect(d->mChild, 0, this, 0);
 
119
    }
 
120
    d->mChild = child;
 
121
    d->mChild->setParent(d->mParent);
 
122
    d->mChild->installEventFilter(this);
 
123
    connect(d->mChild, SIGNAL(visibleChanged()), SLOT(slotChildVisibilityChanged()));
 
124
    d->updateChildGeometry();
 
125
    //d->mChild->raise();
 
126
    d->mChild->show();
 
127
}
 
128
 
 
129
void GraphicsWidgetFloater::setAlignment(Qt::Alignment alignment)
 
130
{
 
131
    d->mAlignment = alignment;
 
132
    d->updateChildGeometry();
 
133
}
 
134
 
 
135
bool GraphicsWidgetFloater::eventFilter(QObject*, QEvent* event)
 
136
{
 
137
    if (event->type() == QEvent::GraphicsSceneResize) {
 
138
        d->updateChildGeometry();
 
139
    }
 
140
    return false;
 
141
}
 
142
 
 
143
void GraphicsWidgetFloater::slotChildVisibilityChanged()
 
144
{
 
145
    if (d->mChild->isVisible()) {
 
146
        d->updateChildGeometry();
 
147
    }
 
148
}
 
149
 
 
150
void GraphicsWidgetFloater::setHorizontalMargin(int value)
 
151
{
 
152
    d->mHorizontalMargin = value;
 
153
    d->updateChildGeometry();
 
154
}
 
155
 
 
156
int GraphicsWidgetFloater::horizontalMargin() const
 
157
{
 
158
    return d->mHorizontalMargin;
 
159
}
 
160
 
 
161
void GraphicsWidgetFloater::setVerticalMargin(int value)
 
162
{
 
163
    d->mVerticalMargin = value;
 
164
    d->updateChildGeometry();
 
165
}
 
166
 
 
167
int GraphicsWidgetFloater::verticalMargin() const
 
168
{
 
169
    return d->mVerticalMargin;
 
170
}
 
171
 
 
172
} // namespace