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

« back to all changes in this revision

Viewing changes to lib/widgetfloater.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 noexpandtab:
 
1
// vim: set tabstop=4 shiftwidth=4 expandtab:
2
2
/*
3
3
Gwenview: an image viewer
4
4
Copyright 2008 AurĆ©lien GĆ¢teau <agateau@kde.org>
32
32
 
33
33
// Local
34
34
 
35
 
namespace Gwenview {
 
35
namespace Gwenview
 
36
{
36
37
 
37
38
struct WidgetFloaterPrivate {
38
 
        QWidget* mParent;
39
 
        QPointer<QWidget> mChild;
40
 
        Qt::Alignment mAlignment;
41
 
 
42
 
        int mHorizontalMargin;
43
 
        int mVerticalMargin;
44
 
        bool mInsideUpdateChildGeometry;
45
 
 
46
 
        void updateChildGeometry() {
47
 
                if (!mChild) {
48
 
                        return;
49
 
                }
50
 
                if (mInsideUpdateChildGeometry) {
51
 
                        return;
52
 
                }
53
 
                mInsideUpdateChildGeometry = true;
54
 
 
55
 
                int posX, posY;
56
 
                int childWidth, childHeight;
57
 
                int parentWidth, parentHeight;
58
 
 
59
 
                childWidth = mChild->width();
60
 
                childHeight = mChild->height();
61
 
 
62
 
                parentWidth = mParent->width();
63
 
                parentHeight = mParent->height();
64
 
 
65
 
                if (mAlignment & Qt::AlignLeft) {
66
 
                        posX = mHorizontalMargin;
67
 
                } else if (mAlignment & Qt::AlignHCenter) {
68
 
                        posX = (parentWidth - childWidth) / 2;
69
 
                } else if (mAlignment & Qt::AlignJustify) {
70
 
                        posX = mHorizontalMargin;
71
 
                        childWidth = parentWidth - 2 * mHorizontalMargin;
72
 
                        QRect childGeometry = mChild->geometry();
73
 
                        childGeometry.setWidth(childWidth);
74
 
                        mChild->setGeometry(childGeometry);
75
 
                } else {
76
 
                        posX = parentWidth - childWidth - mHorizontalMargin;
77
 
                }
78
 
 
79
 
                if (mAlignment & Qt::AlignTop) {
80
 
                        posY = mVerticalMargin;
81
 
                } else if (mAlignment & Qt::AlignVCenter) {
82
 
                        posY = (parentHeight - childHeight) / 2;
83
 
                } else {
84
 
                        posY = parentHeight - childHeight - mVerticalMargin;
85
 
                }
86
 
 
87
 
                mChild->move(posX, posY);
88
 
 
89
 
                mInsideUpdateChildGeometry = false;
90
 
        }
 
39
    QWidget* mParent;
 
40
    QPointer<QWidget> 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
        int posX, posY;
 
58
        int childWidth, childHeight;
 
59
        int parentWidth, parentHeight;
 
60
 
 
61
        childWidth = mChild->width();
 
62
        childHeight = mChild->height();
 
63
 
 
64
        parentWidth = mParent->width();
 
65
        parentHeight = mParent->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
            QRect 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->move(posX, posY);
 
90
 
 
91
        mInsideUpdateChildGeometry = false;
 
92
    }
91
93
};
92
94
 
93
 
 
94
95
WidgetFloater::WidgetFloater(QWidget* parent)
95
96
: QObject(parent)
96
 
, d(new WidgetFloaterPrivate) {
97
 
        Q_ASSERT(parent);
98
 
        d->mParent = parent;
99
 
        d->mParent->installEventFilter(this);
100
 
        d->mChild = 0;
101
 
        d->mAlignment = Qt::AlignCenter;
102
 
        d->mHorizontalMargin = KDialog::marginHint();
103
 
        d->mVerticalMargin = KDialog::marginHint();
104
 
        d->mInsideUpdateChildGeometry = false;
105
 
}
106
 
 
107
 
 
108
 
WidgetFloater::~WidgetFloater() {
109
 
        delete d;
110
 
}
111
 
 
112
 
 
113
 
void WidgetFloater::setChildWidget(QWidget* child) {
114
 
        if (d->mChild) {
115
 
                d->mChild->removeEventFilter(this);
116
 
        }
117
 
        d->mChild = child;
118
 
        d->mChild->setParent(d->mParent);
119
 
        d->mChild->installEventFilter(this);
120
 
        d->updateChildGeometry();
121
 
        d->mChild->raise();
122
 
        d->mChild->show();
123
 
}
124
 
 
125
 
 
126
 
void WidgetFloater::setAlignment(Qt::Alignment alignment) {
127
 
        d->mAlignment = alignment;
128
 
        d->updateChildGeometry();
129
 
}
130
 
 
131
 
 
132
 
bool WidgetFloater::eventFilter(QObject*, QEvent* event) {
133
 
        switch (event->type()) {
134
 
        case QEvent::Resize:
135
 
        case QEvent::Show:
136
 
                d->updateChildGeometry();
137
 
                break;
138
 
        default:
139
 
                break;
140
 
        }
141
 
        return false;
142
 
}
143
 
 
144
 
 
145
 
void WidgetFloater::setHorizontalMargin(int value) {
146
 
        d->mHorizontalMargin = value;
147
 
        d->updateChildGeometry();
148
 
}
149
 
 
150
 
 
151
 
int WidgetFloater::horizontalMargin() const {
152
 
        return d->mHorizontalMargin;
153
 
}
154
 
 
155
 
 
156
 
void WidgetFloater::setVerticalMargin(int value) {
157
 
        d->mVerticalMargin = value;
158
 
        d->updateChildGeometry();
159
 
}
160
 
 
161
 
 
162
 
int WidgetFloater::verticalMargin() const {
163
 
        return d->mVerticalMargin;
164
 
}
165
 
 
 
97
, d(new WidgetFloaterPrivate)
 
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
WidgetFloater::~WidgetFloater()
 
110
{
 
111
    delete d;
 
112
}
 
113
 
 
114
void WidgetFloater::setChildWidget(QWidget* child)
 
115
{
 
116
    if (d->mChild) {
 
117
        d->mChild->removeEventFilter(this);
 
118
    }
 
119
    d->mChild = child;
 
120
    d->mChild->setParent(d->mParent);
 
121
    d->mChild->installEventFilter(this);
 
122
    d->updateChildGeometry();
 
123
    d->mChild->raise();
 
124
    d->mChild->show();
 
125
}
 
126
 
 
127
void WidgetFloater::setAlignment(Qt::Alignment alignment)
 
128
{
 
129
    d->mAlignment = alignment;
 
130
    d->updateChildGeometry();
 
131
}
 
132
 
 
133
bool WidgetFloater::eventFilter(QObject*, QEvent* event)
 
134
{
 
135
    switch (event->type()) {
 
136
    case QEvent::Resize:
 
137
    case QEvent::Show:
 
138
        d->updateChildGeometry();
 
139
        break;
 
140
    default:
 
141
        break;
 
142
    }
 
143
    return false;
 
144
}
 
145
 
 
146
void WidgetFloater::setHorizontalMargin(int value)
 
147
{
 
148
    d->mHorizontalMargin = value;
 
149
    d->updateChildGeometry();
 
150
}
 
151
 
 
152
int WidgetFloater::horizontalMargin() const
 
153
{
 
154
    return d->mHorizontalMargin;
 
155
}
 
156
 
 
157
void WidgetFloater::setVerticalMargin(int value)
 
158
{
 
159
    d->mVerticalMargin = value;
 
160
    d->updateChildGeometry();
 
161
}
 
162
 
 
163
int WidgetFloater::verticalMargin() const
 
164
{
 
165
    return d->mVerticalMargin;
 
166
}
166
167
 
167
168
} // namespace