1
// vim: set tabstop=4 shiftwidth=4 expandtab:
3
Gwenview: an image viewer
4
Copyright 2008 Aurélien Gâteau <agateau@kde.org>
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.
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.
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.
22
#include "graphicswidgetfloater.moc"
26
#include <QGraphicsWidget>
38
struct GraphicsWidgetFloaterPrivate {
39
QGraphicsWidget* mParent;
40
QPointer<QGraphicsWidget> mChild;
41
Qt::Alignment mAlignment;
43
int mHorizontalMargin;
45
bool mInsideUpdateChildGeometry;
47
void updateChildGeometry()
52
if (mInsideUpdateChildGeometry) {
55
mInsideUpdateChildGeometry = true;
58
qreal childWidth, childHeight;
59
qreal parentWidth, parentHeight;
61
childWidth = mChild->size().width();
62
childHeight = mChild->size().height();
64
parentWidth = mParent->size().width();
65
parentHeight = mParent->size().height();
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);
78
posX = parentWidth - childWidth - mHorizontalMargin;
81
if (mAlignment & Qt::AlignTop) {
82
posY = mVerticalMargin;
83
} else if (mAlignment & Qt::AlignVCenter) {
84
posY = (parentHeight - childHeight) / 2;
86
posY = parentHeight - childHeight - mVerticalMargin;
89
mChild->setGeometry(posX, posY, childWidth, childHeight);
91
mInsideUpdateChildGeometry = false;
95
GraphicsWidgetFloater::GraphicsWidgetFloater(QGraphicsWidget* parent)
97
, d(new GraphicsWidgetFloaterPrivate)
101
d->mParent->installEventFilter(this);
103
d->mAlignment = Qt::AlignCenter;
104
d->mHorizontalMargin = KDialog::marginHint();
105
d->mVerticalMargin = KDialog::marginHint();
106
d->mInsideUpdateChildGeometry = false;
109
GraphicsWidgetFloater::~GraphicsWidgetFloater()
114
void GraphicsWidgetFloater::setChildWidget(QGraphicsWidget* child)
117
d->mChild->removeEventFilter(this);
118
disconnect(d->mChild, 0, this, 0);
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();
129
void GraphicsWidgetFloater::setAlignment(Qt::Alignment alignment)
131
d->mAlignment = alignment;
132
d->updateChildGeometry();
135
bool GraphicsWidgetFloater::eventFilter(QObject*, QEvent* event)
137
if (event->type() == QEvent::GraphicsSceneResize) {
138
d->updateChildGeometry();
143
void GraphicsWidgetFloater::slotChildVisibilityChanged()
145
if (d->mChild->isVisible()) {
146
d->updateChildGeometry();
150
void GraphicsWidgetFloater::setHorizontalMargin(int value)
152
d->mHorizontalMargin = value;
153
d->updateChildGeometry();
156
int GraphicsWidgetFloater::horizontalMargin() const
158
return d->mHorizontalMargin;
161
void GraphicsWidgetFloater::setVerticalMargin(int value)
163
d->mVerticalMargin = value;
164
d->updateChildGeometry();
167
int GraphicsWidgetFloater::verticalMargin() const
169
return d->mVerticalMargin;