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

« back to all changes in this revision

Viewing changes to tools/designer/src/components/tabordereditor/tabordereditor.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 "tabordereditor.h"
 
30
 
 
31
#include <QtGui/QLabel>
 
32
#include <QtGui/QPainter>
 
33
#include <QtCore/qdebug.h>
 
34
#include <QtGui/QRegion>
 
35
#include <QtGui/qevent.h>
 
36
#include <QtGui/QFontMetrics>
 
37
#include <QtGui/QApplication>
 
38
 
 
39
#include <QtDesigner/QtDesigner>
 
40
 
 
41
#include <qtundo_p.h>
 
42
#include <qdesigner_command_p.h>
 
43
#include <qdesigner_widget_p.h>
 
44
#include <qdesigner_utils_p.h>
 
45
#include <qlayout_widget_p.h>
 
46
#include <qdesigner_promotedwidget_p.h>
 
47
 
 
48
#define BG_ALPHA                32
 
49
#define VBOX_MARGIN             1
 
50
#define HBOX_MARGIN             4
 
51
 
 
52
using namespace qdesigner_internal;
 
53
 
 
54
static QRect fixRect(const QRect &r)
 
55
{
 
56
    return QRect(r.x(), r.y(), r.width() - 1, r.height() - 1);
 
57
}
 
58
 
 
59
TabOrderEditor::TabOrderEditor(QDesignerFormWindowInterface *form, QWidget *parent)
 
60
    : QWidget(parent), m_font_metrics(font())
 
61
{
 
62
    m_form_window = form;
 
63
    m_bg_widget = 0;
 
64
    m_undo_stack = form->commandHistory();
 
65
    connect(form, SIGNAL(widgetRemoved(QWidget*)), this, SLOT(widgetRemoved(QWidget*)));
 
66
 
 
67
    QFont font = this->font();
 
68
    font.setPointSize(font.pointSize()*2);
 
69
    font.setBold(true);
 
70
    setFont(font);
 
71
    m_font_metrics = QFontMetrics(font);
 
72
    m_current_index = 0;
 
73
 
 
74
    setAttribute(Qt::WA_MouseTracking, true);
 
75
}
 
76
 
 
77
QDesignerFormWindowInterface *TabOrderEditor::formWindow() const
 
78
{
 
79
    return m_form_window;
 
80
}
 
81
 
 
82
void TabOrderEditor::setBackground(QWidget *background)
 
83
{
 
84
    if (background == m_bg_widget) {
 
85
        return;
 
86
    }
 
87
 
 
88
    m_bg_widget = background;
 
89
    updateBackground();
 
90
}
 
91
 
 
92
void TabOrderEditor::updateBackground()
 
93
{
 
94
    if (m_bg_widget == 0) {
 
95
        // nothing to do
 
96
        return;
 
97
    }
 
98
 
 
99
    m_bg_pixmap = QPixmap::grabWidget(m_bg_widget);
 
100
 
 
101
/*    QPainter p(&m_bg_pixmap);
 
102
    p.setPen(QColor(0, 0, 255, 22));
 
103
    for (int y = 0; y < m_bg_pixmap.height(); y += 2)
 
104
        p.drawLine(0, y, m_bg_pixmap.width(), y); */
 
105
 
 
106
    initTabOrder();
 
107
    update();
 
108
}
 
109
 
 
110
void TabOrderEditor::widgetRemoved(QWidget*)
 
111
{
 
112
}
 
113
 
 
114
QRect TabOrderEditor::indicatorRect(int index) const
 
115
{
 
116
    if (index < 0 || index >= m_tab_order_list.size())
 
117
        return QRect();
 
118
 
 
119
    QWidget *w = m_tab_order_list.at(index);
 
120
    QString text = QString::number(index + 1);
 
121
 
 
122
    QPoint tl = mapFromGlobal(w->mapToGlobal(w->rect().topLeft()));
 
123
    QSize size = m_font_metrics.size(Qt::TextSingleLine, text);
 
124
    QRect r(tl - QPoint(size.width(), size.height())/2, size);
 
125
    r = QRect(r.left() - HBOX_MARGIN, r.top() - VBOX_MARGIN,
 
126
                r.width() + HBOX_MARGIN*2, r.height() + VBOX_MARGIN*2);
 
127
 
 
128
    return r;
 
129
}
 
130
 
 
131
static bool isWidgetVisible(QWidget *widget)
 
132
{
 
133
    while (widget && widget->parentWidget()) {
 
134
        if (!widget->isVisibleTo(widget->parentWidget()))
 
135
            return false;
 
136
 
 
137
        widget = widget->parentWidget();
 
138
    }
 
139
 
 
140
    return true;
 
141
}
 
142
 
 
143
void TabOrderEditor::paintEvent(QPaintEvent *e)
 
144
{
 
145
    QPainter p(this);
 
146
    p.setClipRegion(e->region());
 
147
 
 
148
    if (m_bg_pixmap.isNull())
 
149
        updateBackground();
 
150
 
 
151
    p.drawPixmap(m_bg_pixmap.rect(), m_bg_pixmap);
 
152
 
 
153
    for (int i = 0; i < m_tab_order_list.size(); ++i) {
 
154
        QWidget *widget = m_tab_order_list.at(i);
 
155
        if (!isWidgetVisible(widget))
 
156
            continue;
 
157
 
 
158
        QRect r = indicatorRect(i);
 
159
 
 
160
        QColor c = Qt::blue;
 
161
        p.setPen(c);
 
162
        c.setAlpha(BG_ALPHA);
 
163
        p.setBrush(c);
 
164
        p.drawRect(fixRect(r));
 
165
 
 
166
        p.setPen(Qt::white);
 
167
        p.drawText(r, QString::number(i + 1), QTextOption(Qt::AlignCenter));
 
168
    }
 
169
}
 
170
 
 
171
bool TabOrderEditor::skipWidget(QWidget *w) const
 
172
{
 
173
    if (qobject_cast<QLayoutWidget*>(w)
 
174
            || w == formWindow()->mainContainer()
 
175
            || w->isHidden())
 
176
        return true;
 
177
 
 
178
    if (!formWindow()->isManaged(w)) {
 
179
        return true;
 
180
    }
 
181
 
 
182
    if (qobject_cast<QDesignerPromotedWidget*>(w) != 0)
 
183
        return true;
 
184
 
 
185
    QExtensionManager *ext = formWindow()->core()->extensionManager();
 
186
    if (QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(ext, w)) {
 
187
        int index = sheet->indexOf(QLatin1String("focusPolicy"));
 
188
        if (index != -1) {
 
189
            bool ok = false;
 
190
            Qt::FocusPolicy q = (Qt::FocusPolicy) Utils::valueOf(sheet->property(index), &ok);
 
191
            return !ok || q == Qt::NoFocus;
 
192
        }
 
193
    }
 
194
 
 
195
    return true;
 
196
}
 
197
 
 
198
void TabOrderEditor::initTabOrder()
 
199
{
 
200
    m_tab_order_list.clear();
 
201
 
 
202
    QDesignerFormEditorInterface *core = formWindow()->core();
 
203
 
 
204
    if (QDesignerMetaDataBaseItemInterface *item = core->metaDataBase()->item(formWindow())) {
 
205
        m_tab_order_list = item->tabOrder();
 
206
    }
 
207
 
 
208
    // Remove any widgets that have been removed form the form
 
209
    for (int i = 0; i < m_tab_order_list.size(); ) {
 
210
        QWidget *w = m_tab_order_list.at(i);
 
211
        if (!formWindow()->mainContainer()->isAncestorOf(w) || skipWidget(w))
 
212
            m_tab_order_list.removeAt(i);
 
213
        else
 
214
            ++i;
 
215
    }
 
216
 
 
217
    // Append any widgets that are in the form but are not in the tab order
 
218
    QDesignerFormWindowCursorInterface *cursor = formWindow()->cursor();
 
219
    for (int i = 0; i < cursor->widgetCount(); ++i) {
 
220
        QWidget *widget = cursor->widget(i);
 
221
        if (skipWidget(widget))
 
222
            continue;
 
223
 
 
224
        if (!m_tab_order_list.contains(widget))
 
225
            m_tab_order_list.append(widget);
 
226
    }
 
227
 
 
228
    m_indicator_region = QRegion();
 
229
    for (int i = 0; i < m_tab_order_list.size(); ++i) {
 
230
        if (m_tab_order_list.at(i)->isVisible())
 
231
            m_indicator_region |= indicatorRect(i);
 
232
    }
 
233
}
 
234
 
 
235
void TabOrderEditor::mouseMoveEvent(QMouseEvent *e)
 
236
{
 
237
    e->accept();
 
238
    if (m_indicator_region.contains(e->pos()))
 
239
        setCursor(Qt::PointingHandCursor);
 
240
    else
 
241
        setCursor(QCursor());
 
242
}
 
243
 
 
244
int TabOrderEditor::widgetIndexAt(const QPoint &pos) const
 
245
{
 
246
    int target_index = -1;
 
247
    for (int i = 0; i < m_tab_order_list.size(); ++i) {
 
248
        if (!m_tab_order_list.at(i)->isVisible())
 
249
            continue;
 
250
        if (indicatorRect(i).contains(pos)) {
 
251
            target_index = i;
 
252
            break;
 
253
        }
 
254
    }
 
255
 
 
256
    return target_index;
 
257
}
 
258
 
 
259
void TabOrderEditor::mousePressEvent(QMouseEvent *e)
 
260
{
 
261
    e->accept();
 
262
 
 
263
    if (!m_indicator_region.contains(e->pos())) {
 
264
        if (QWidget *child = m_bg_widget->childAt(e->pos())) {
 
265
            QDesignerFormEditorInterface *core = m_form_window->core();
 
266
            if (core->widgetFactory()->isPassiveInteractor(child)) {
 
267
 
 
268
                QMouseEvent event(QEvent::MouseButtonPress,
 
269
                                    child->mapFromGlobal(e->globalPos()),
 
270
                                    e->button(), e->buttons(), e->modifiers());
 
271
 
 
272
                qApp->sendEvent(child, &event);
 
273
 
 
274
                QMouseEvent event2(QEvent::MouseButtonRelease,
 
275
                                    child->mapFromGlobal(e->globalPos()),
 
276
                                    e->button(), e->buttons(), e->modifiers());
 
277
 
 
278
                qApp->sendEvent(child, &event2);
 
279
 
 
280
                updateBackground();
 
281
            }
 
282
        }
 
283
        return;
 
284
    }
 
285
 
 
286
    int target_index = widgetIndexAt(e->pos());
 
287
    if (target_index == -1)
 
288
        return;
 
289
 
 
290
    update(indicatorRect(target_index));
 
291
    update(indicatorRect(m_current_index));
 
292
    m_tab_order_list.swap(target_index, m_current_index);
 
293
    update(indicatorRect(target_index));
 
294
    update(indicatorRect(m_current_index));
 
295
 
 
296
    ++m_current_index;
 
297
    if (m_current_index == m_tab_order_list.size())
 
298
        m_current_index = 0;
 
299
 
 
300
    TabOrderCommand *cmd = new TabOrderCommand(formWindow());
 
301
    cmd->init(m_tab_order_list);
 
302
    formWindow()->commandHistory()->push(cmd);
 
303
}
 
304
 
 
305
void TabOrderEditor::mouseDoubleClickEvent(QMouseEvent *e)
 
306
{
 
307
    m_current_index = 0;
 
308
    mousePressEvent(e);
 
309
}
 
310
 
 
311
void TabOrderEditor::resizeEvent(QResizeEvent *e)
 
312
{
 
313
    updateBackground();
 
314
    QWidget::resizeEvent(e);
 
315
}
 
316