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

« back to all changes in this revision

Viewing changes to tools/designer/src/lib/shared/layoutinfo.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 "layoutinfo_p.h"
 
30
 
 
31
#include <QtDesigner/QtDesigner>
 
32
 
 
33
#include <QtGui/QHBoxLayout>
 
34
#include <QtGui/QSplitter>
 
35
 
 
36
#include <QtCore/QMap>
 
37
#include <QtCore/qdebug.h>
 
38
 
 
39
LayoutInfo::Type LayoutInfo::layoutType(QDesignerFormEditorInterface *core, QWidget *w, QLayout *&layout)
 
40
{
 
41
    layout = 0;
 
42
 
 
43
    if (QDesignerContainerExtension *container = qt_extension<QDesignerContainerExtension*>(core->extensionManager(), w))
 
44
        w = container->widget(container->currentIndex());
 
45
 
 
46
    if (qobject_cast<QSplitter*>(w))
 
47
        return static_cast<QSplitter*>(w)->orientation() == Qt::Horizontal ? HBox : VBox;
 
48
 
 
49
    if (!w || !w->layout())
 
50
        return NoLayout;
 
51
 
 
52
    QLayout *lay = w->layout();
 
53
 
 
54
    if (lay && core->metaDataBase()->item(lay) == 0) {
 
55
        lay = qFindChild<QLayout*>(lay);
 
56
    }
 
57
    layout = lay;
 
58
 
 
59
#ifdef QD_DEBUG
 
60
    Q_ASSERT (lay == 0 || core->metaDataBase()->item(lay) != 0);
 
61
#endif
 
62
 
 
63
    return layoutType(core, lay);
 
64
}
 
65
 
 
66
/*!
 
67
  \overload
 
68
*/
 
69
LayoutInfo::Type LayoutInfo::layoutType(QDesignerFormEditorInterface *core, QLayout *layout)
 
70
{
 
71
    Q_UNUSED(core)
 
72
 
 
73
    if (qobject_cast<QHBoxLayout*>(layout))
 
74
        return HBox;
 
75
    else if (qobject_cast<QVBoxLayout*>(layout))
 
76
        return VBox;
 
77
    else if (qobject_cast<QGridLayout*>(layout))
 
78
        return Grid;
 
79
    return NoLayout;
 
80
}
 
81
 
 
82
/*!
 
83
  \overload
 
84
*/
 
85
LayoutInfo::Type LayoutInfo::layoutType(QDesignerFormEditorInterface *core, QWidget *w)
 
86
{
 
87
    QLayout *l = 0;
 
88
    return layoutType(core, w, l);
 
89
}
 
90
 
 
91
QWidget *LayoutInfo::layoutParent(QDesignerFormEditorInterface *core, QLayout *layout)
 
92
{
 
93
    Q_UNUSED(core)
 
94
 
 
95
    QObject *o = layout;
 
96
    while (o) {
 
97
        if (QWidget *widget = qobject_cast<QWidget*>(o))
 
98
            return widget;
 
99
 
 
100
        o = o->parent();
 
101
    }
 
102
    return 0;
 
103
}
 
104
 
 
105
void LayoutInfo::deleteLayout(QDesignerFormEditorInterface *core, QWidget *widget)
 
106
{
 
107
    if (QDesignerContainerExtension *container = qt_extension<QDesignerContainerExtension*>(core->extensionManager(), widget))
 
108
        widget = container->widget(container->currentIndex());
 
109
 
 
110
    Q_ASSERT(widget != 0);
 
111
 
 
112
    QLayout *layout = managedLayout(core, widget);
 
113
 
 
114
    if (layout == 0 || core->metaDataBase()->item(layout) != 0) {
 
115
        delete layout;
 
116
        return;
 
117
    }
 
118
 
 
119
    qWarning() << "trying to delete an unmanaged layout:" << "widget:" << widget << "layout:" << layout;
 
120
}
 
121
 
 
122
void LayoutInfo::cells(QLayout *layout, IntervalList *rows, IntervalList *columns)
 
123
{
 
124
    QMap<Interval, int> rowDict;
 
125
    QMap<Interval, int> columnDict;
 
126
 
 
127
    int i = 0;
 
128
    while (QLayoutItem *item = layout->itemAt(i)) {
 
129
        ++i;
 
130
 
 
131
        QRect g = item->geometry();
 
132
        columnDict.insert(Interval(g.left(), g.right()), 1);
 
133
        rowDict.insert(Interval(g.top(), g.bottom()), 1);
 
134
    }
 
135
 
 
136
    if (columns)
 
137
        *columns = columnDict.keys();
 
138
 
 
139
    if (rows)
 
140
        *rows = rowDict.keys();
 
141
}
 
142
 
 
143
bool LayoutInfo::isWidgetLaidout(QDesignerFormEditorInterface *core, QWidget *widget)
 
144
{
 
145
    Q_UNUSED(core);
 
146
 
 
147
    QWidget *parent = widget->parentWidget();
 
148
 
 
149
    if (qobject_cast<QSplitter*>(parent)) { // ### generalize
 
150
        return true;
 
151
    }
 
152
 
 
153
    if (parent && parent->layout()) {
 
154
        if (parent->layout()->indexOf(widget) != -1)
 
155
            return true;
 
156
 
 
157
        QList<QLayout*> childLayouts = qFindChildren<QLayout*>(parent->layout());
 
158
        foreach (QLayout *childLayout, childLayouts) {
 
159
            if (childLayout->indexOf(widget) != -1)
 
160
                return true;
 
161
        }
 
162
    }
 
163
 
 
164
    return false;
 
165
}
 
166
 
 
167
QLayout *LayoutInfo::managedLayout(QDesignerFormEditorInterface *core, QWidget *widget)
 
168
{
 
169
    if (widget == 0)
 
170
        return 0;
 
171
 
 
172
    QLayout *layout = widget->layout();
 
173
    QDesignerMetaDataBaseItemInterface *item = core->metaDataBase()->item(layout);
 
174
    if (layout != 0 && item == 0) {
 
175
        layout = qFindChild<QLayout*>(layout);
 
176
        item = core->metaDataBase()->item(layout);
 
177
    }
 
178
 
 
179
    return item != 0 ? layout : 0;
 
180
}
 
181