~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to tools/designer/src/lib/shared/spacer_widget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the designer application of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "spacer_widget_p.h"
 
30
#include "layoutinfo_p.h"
 
31
 
 
32
#include <QtDesigner/abstractformwindow.h>
 
33
 
 
34
#include <QtGui/QLayout>
 
35
#include <QtGui/QPainter>
 
36
#include <QtCore/qdebug.h>
 
37
 
 
38
Spacer::Spacer(QWidget *parent)
 
39
    : QWidget(parent),
 
40
      orient(Qt::Vertical), interactive(true), sh(20, 40)
 
41
{
 
42
    setAttribute(Qt::WA_MouseNoMask);
 
43
    m_formWindow = QDesignerFormWindowInterface::findFormWindow(this);
 
44
 
 
45
    setSizeType(QSizePolicy::Expanding);
 
46
}
 
47
 
 
48
void Spacer::paintEvent(QPaintEvent *)
 
49
{
 
50
    // Only draw spacers when we're editting widgets
 
51
    if (m_formWindow != 0 && m_formWindow->currentTool() != 0)
 
52
        return;
 
53
 
 
54
    QPainter p(this);
 
55
    p.setPen(Qt::blue);
 
56
 
 
57
    if (orient == Qt::Horizontal) {
 
58
        const int dist = 3;
 
59
        const int amplitude = qMin(3, height() / 3);
 
60
        const int base = height() / 2;
 
61
        int i = 0;
 
62
        p.setPen(Qt::white);
 
63
        for (i = 0; i < width() / 3 +2; ++i)
 
64
            p.drawLine(i * dist, base - amplitude, i * dist + dist / 2, base + amplitude);
 
65
        p.setPen(Qt::blue);
 
66
        for (i = 0; i < width() / 3 +2; ++i)
 
67
            p.drawLine(i * dist + dist / 2, base + amplitude, i * dist + dist, base - amplitude);
 
68
        int y = height()/2;
 
69
        p.drawLine(0, y-10, 0, y+10);
 
70
        p.drawLine(width() - 1, y-10, width() - 1, y+10);
 
71
    } else {
 
72
        const int dist = 3;
 
73
        const int amplitude = qMin(3, width() / 3);
 
74
        const int base = width() / 2;
 
75
        int i = 0;
 
76
        p.setPen(Qt::white);
 
77
        for (i = 0; i < height() / 3 +2; ++i)
 
78
            p.drawLine(base - amplitude, i * dist, base + amplitude,i * dist + dist / 2);
 
79
        p.setPen(Qt::blue);
 
80
        for (i = 0; i < height() / 3 +2; ++i)
 
81
            p.drawLine(base + amplitude, i * dist + dist / 2, base - amplitude, i * dist + dist);
 
82
        int x = width()/2;
 
83
        p.drawLine(x-10, 0, x+10, 0);
 
84
        p.drawLine(x-10, height() - 1, x+10, height() - 1);
 
85
    }
 
86
}
 
87
 
 
88
void Spacer::resizeEvent(QResizeEvent* e)
 
89
{
 
90
    QWidget::resizeEvent(e);
 
91
    updateMask();
 
92
 
 
93
    if (!interactive)
 
94
        return;
 
95
 
 
96
    if (!parentWidget() || (m_formWindow && LayoutInfo::layoutType(m_formWindow->core(), parentWidget()) == LayoutInfo::NoLayout))
 
97
        sh = size();
 
98
}
 
99
 
 
100
void Spacer::updateMask()
 
101
{
 
102
    QRegion r(rect());
 
103
    if (orient == Qt::Horizontal) {
 
104
        const int amplitude = qMin(3, height() / 3);
 
105
        const int base = height() / 2;
 
106
        r = r.subtract(QRect(1, 0, width() - 2, base - amplitude));
 
107
        r = r.subtract(QRect(1, base + amplitude, width() - 2, height() - base - amplitude));
 
108
    } else {
 
109
        const int amplitude = qMin(3, width() / 3);
 
110
        const int base = width() / 2;
 
111
        r = r.subtract(QRect(0, 1, base - amplitude, height() - 2));
 
112
        r = r.subtract(QRect(base + amplitude, 1, width() - base - amplitude, height() - 2));
 
113
    }
 
114
    setMask(r);
 
115
}
 
116
 
 
117
void Spacer::setSizeType(QSizePolicy::Policy t)
 
118
{
 
119
    QSizePolicy sizeP;
 
120
    if (orient == Qt::Vertical)
 
121
        sizeP = QSizePolicy(QSizePolicy::Minimum, t);
 
122
    else
 
123
        sizeP = QSizePolicy(t, QSizePolicy::Minimum);
 
124
    setSizePolicy(sizeP);
 
125
}
 
126
 
 
127
 
 
128
QSizePolicy::Policy Spacer::sizeType() const
 
129
{
 
130
    if (orient == Qt::Vertical)
 
131
        return sizePolicy().verticalPolicy();
 
132
    return sizePolicy().horizontalPolicy();
 
133
}
 
134
 
 
135
Qt::Alignment Spacer::alignment() const
 
136
{
 
137
    if (orient == Qt::Vertical)
 
138
        return Qt::AlignHCenter;
 
139
    return Qt::AlignVCenter;
 
140
}
 
141
 
 
142
QSize Spacer::minimumSize() const
 
143
{
 
144
    QSize s = QSize(20,20);
 
145
    if (sizeType() == QSizePolicy::Expanding)
 
146
        if (orient == Qt::Vertical)
 
147
            s.rheight() = 0;
 
148
        else
 
149
            s.rwidth() = 0;
 
150
    return s;
 
151
}
 
152
 
 
153
QSize Spacer::sizeHint() const
 
154
{
 
155
    return sh;
 
156
}
 
157
 
 
158
void Spacer::setSizeHint(const QSize &s)
 
159
{
 
160
    sh = s;
 
161
 
 
162
    if (!parentWidget() || (m_formWindow && LayoutInfo::layoutType(m_formWindow->core(), parentWidget()) == LayoutInfo::NoLayout))
 
163
        resize(sizeHint());
 
164
 
 
165
    updateGeometry();
 
166
}
 
167
 
 
168
Qt::Orientation Spacer::orientation() const
 
169
{
 
170
    return orient;
 
171
}
 
172
 
 
173
void Spacer::setOrientation(Qt::Orientation o)
 
174
{
 
175
    if (orient == o)
 
176
        return;
 
177
 
 
178
    QSizePolicy::Policy st = sizeType();
 
179
    orient = o;
 
180
    setSizeType(st);
 
181
 
 
182
    if (interactive) {
 
183
        sh = QSize(sh.height(), sh.width());
 
184
        if (!parentWidget() || (m_formWindow && LayoutInfo::layoutType(m_formWindow->core(), parentWidget()) == LayoutInfo::NoLayout))
 
185
            resize(height(), width());
 
186
    }
 
187
 
 
188
    updateMask();
 
189
    update();
 
190
    updateGeometry();
 
191
}