1 // vim: set tabstop=4 shiftwidth=4 noexpandtab:
3 Gwenview: an image viewer
4 Copyright 2008 Aurélien Gâteau <aurelien.gateau@free.fr>
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 "widgetfloater.moc"
37 struct WidgetFloaterPrivate
{
39 QPointer
<QWidget
> mChild
;
40 Qt::Alignment mAlignment
;
42 int mHorizontalMargin
;
45 void updateChildGeometry() {
50 int childWidth
, childHeight
;
51 int parentWidth
, parentHeight
;
53 childWidth
= mChild
->width();
54 childHeight
= mChild
->height();
56 parentWidth
= mParent
->width();
57 parentHeight
= mParent
->height();
59 if (mAlignment
& Qt::AlignLeft
) {
60 posX
= mHorizontalMargin
;
61 } else if (mAlignment
& Qt::AlignHCenter
) {
62 posX
= (parentWidth
- childWidth
) / 2;
64 posX
= parentWidth
- childWidth
- mHorizontalMargin
;
67 if (mAlignment
& Qt::AlignTop
) {
68 posY
= mVerticalMargin
;
69 } else if (mAlignment
& Qt::AlignVCenter
) {
70 posY
= (parentHeight
- childHeight
) / 2;
72 posY
= parentHeight
- childHeight
- mVerticalMargin
;
75 mChild
->move(posX
, posY
);
80 WidgetFloater::WidgetFloater(QWidget
* parent
)
82 , d(new WidgetFloaterPrivate
) {
85 d
->mParent
->installEventFilter(this);
87 d
->mAlignment
= Qt::AlignCenter
;
88 d
->mHorizontalMargin
= KDialog::marginHint();
89 d
->mVerticalMargin
= KDialog::marginHint();
93 WidgetFloater::~WidgetFloater() {
98 void WidgetFloater::setChildWidget(QWidget
* child
) {
100 d
->mChild
->removeEventFilter(this);
103 d
->mChild
->setParent(d
->mParent
);
104 d
->mChild
->installEventFilter(this);
105 d
->updateChildGeometry();
111 void WidgetFloater::setAlignment(Qt::Alignment alignment
) {
112 d
->mAlignment
= alignment
;
113 d
->updateChildGeometry();
117 bool WidgetFloater::eventFilter(QObject
*, QEvent
* event
) {
118 switch (event
->type()) {
121 d
->updateChildGeometry();
130 void WidgetFloater::setHorizontalMargin(int value
) {
131 d
->mHorizontalMargin
= value
;
132 d
->updateChildGeometry();
136 int WidgetFloater::horizontalMargin() const {
137 return d
->mHorizontalMargin
;
141 void WidgetFloater::setVerticalMargin(int value
) {
142 d
->mVerticalMargin
= value
;
143 d
->updateChildGeometry();
147 int WidgetFloater::verticalMargin() const {
148 return d
->mVerticalMargin
;