polish
[kdegraphics.git] / gwenview / lib / widgetfloater.cpp
blobc7ccdd3c83a928d7856d59d99ec39763da8c0de5
1 // vim: set tabstop=4 shiftwidth=4 noexpandtab:
2 /*
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.
21 // Self
22 #include "widgetfloater.moc"
24 // Qt
25 #include <QEvent>
26 #include <QPointer>
27 #include <QWidget>
29 // KDE
30 #include <kdebug.h>
31 #include <kdialog.h>
33 // Local
35 namespace Gwenview {
37 struct WidgetFloaterPrivate {
38 QWidget* mParent;
39 QPointer<QWidget> mChild;
40 Qt::Alignment mAlignment;
42 int mHorizontalMargin;
43 int mVerticalMargin;
45 void updateChildGeometry() {
46 if (!mChild) {
47 return;
49 int posX, posY;
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;
63 } else {
64 posX = parentWidth - childWidth - mHorizontalMargin;
67 if (mAlignment & Qt::AlignTop) {
68 posY = mVerticalMargin;
69 } else if (mAlignment & Qt::AlignVCenter) {
70 posY = (parentHeight - childHeight) / 2;
71 } else {
72 posY = parentHeight - childHeight - mVerticalMargin;
75 mChild->move(posX, posY);
80 WidgetFloater::WidgetFloater(QWidget* parent)
81 : QObject(parent)
82 , d(new WidgetFloaterPrivate) {
83 Q_ASSERT(parent);
84 d->mParent = parent;
85 d->mParent->installEventFilter(this);
86 d->mChild = 0;
87 d->mAlignment = Qt::AlignCenter;
88 d->mHorizontalMargin = KDialog::marginHint();
89 d->mVerticalMargin = KDialog::marginHint();
93 WidgetFloater::~WidgetFloater() {
94 delete d;
98 void WidgetFloater::setChildWidget(QWidget* child) {
99 if (d->mChild) {
100 d->mChild->removeEventFilter(this);
102 d->mChild = child;
103 d->mChild->setParent(d->mParent);
104 d->mChild->installEventFilter(this);
105 d->updateChildGeometry();
106 d->mChild->raise();
107 d->mChild->show();
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()) {
119 case QEvent::Resize:
120 case QEvent::Show:
121 d->updateChildGeometry();
122 break;
123 default:
124 break;
126 return false;
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;
152 } // namespace