1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the designer application of the Qt Toolkit.
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.
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.
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.
21
** Contact info@trolltech.com if any conditions of this licensing are
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.
27
****************************************************************************/
29
#include "tabordereditor.h"
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>
39
#include <QtDesigner/QtDesigner>
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>
52
using namespace qdesigner_internal;
54
static QRect fixRect(const QRect &r)
56
return QRect(r.x(), r.y(), r.width() - 1, r.height() - 1);
59
TabOrderEditor::TabOrderEditor(QDesignerFormWindowInterface *form, QWidget *parent)
60
: QWidget(parent), m_font_metrics(font())
64
m_undo_stack = form->commandHistory();
65
connect(form, SIGNAL(widgetRemoved(QWidget*)), this, SLOT(widgetRemoved(QWidget*)));
67
QFont font = this->font();
68
font.setPointSize(font.pointSize()*2);
71
m_font_metrics = QFontMetrics(font);
74
setAttribute(Qt::WA_MouseTracking, true);
77
QDesignerFormWindowInterface *TabOrderEditor::formWindow() const
82
void TabOrderEditor::setBackground(QWidget *background)
84
if (background == m_bg_widget) {
88
m_bg_widget = background;
92
void TabOrderEditor::updateBackground()
94
if (m_bg_widget == 0) {
99
m_bg_pixmap = QPixmap::grabWidget(m_bg_widget);
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); */
110
void TabOrderEditor::widgetRemoved(QWidget*)
114
QRect TabOrderEditor::indicatorRect(int index) const
116
if (index < 0 || index >= m_tab_order_list.size())
119
QWidget *w = m_tab_order_list.at(index);
120
QString text = QString::number(index + 1);
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);
131
static bool isWidgetVisible(QWidget *widget)
133
while (widget && widget->parentWidget()) {
134
if (!widget->isVisibleTo(widget->parentWidget()))
137
widget = widget->parentWidget();
143
void TabOrderEditor::paintEvent(QPaintEvent *e)
146
p.setClipRegion(e->region());
148
if (m_bg_pixmap.isNull())
151
p.drawPixmap(m_bg_pixmap.rect(), m_bg_pixmap);
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))
158
QRect r = indicatorRect(i);
162
c.setAlpha(BG_ALPHA);
164
p.drawRect(fixRect(r));
167
p.drawText(r, QString::number(i + 1), QTextOption(Qt::AlignCenter));
171
bool TabOrderEditor::skipWidget(QWidget *w) const
173
if (qobject_cast<QLayoutWidget*>(w)
174
|| w == formWindow()->mainContainer()
178
if (!formWindow()->isManaged(w)) {
182
if (qobject_cast<QDesignerPromotedWidget*>(w) != 0)
185
QExtensionManager *ext = formWindow()->core()->extensionManager();
186
if (QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(ext, w)) {
187
int index = sheet->indexOf(QLatin1String("focusPolicy"));
190
Qt::FocusPolicy q = (Qt::FocusPolicy) Utils::valueOf(sheet->property(index), &ok);
191
return !ok || q == Qt::NoFocus;
198
void TabOrderEditor::initTabOrder()
200
m_tab_order_list.clear();
202
QDesignerFormEditorInterface *core = formWindow()->core();
204
if (QDesignerMetaDataBaseItemInterface *item = core->metaDataBase()->item(formWindow())) {
205
m_tab_order_list = item->tabOrder();
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);
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))
224
if (!m_tab_order_list.contains(widget))
225
m_tab_order_list.append(widget);
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);
235
void TabOrderEditor::mouseMoveEvent(QMouseEvent *e)
238
if (m_indicator_region.contains(e->pos()))
239
setCursor(Qt::PointingHandCursor);
241
setCursor(QCursor());
244
int TabOrderEditor::widgetIndexAt(const QPoint &pos) const
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())
250
if (indicatorRect(i).contains(pos)) {
259
void TabOrderEditor::mousePressEvent(QMouseEvent *e)
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)) {
268
QMouseEvent event(QEvent::MouseButtonPress,
269
child->mapFromGlobal(e->globalPos()),
270
e->button(), e->buttons(), e->modifiers());
272
qApp->sendEvent(child, &event);
274
QMouseEvent event2(QEvent::MouseButtonRelease,
275
child->mapFromGlobal(e->globalPos()),
276
e->button(), e->buttons(), e->modifiers());
278
qApp->sendEvent(child, &event2);
286
int target_index = widgetIndexAt(e->pos());
287
if (target_index == -1)
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));
297
if (m_current_index == m_tab_order_list.size())
300
TabOrderCommand *cmd = new TabOrderCommand(formWindow());
301
cmd->init(m_tab_order_list);
302
formWindow()->commandHistory()->push(cmd);
305
void TabOrderEditor::mouseDoubleClickEvent(QMouseEvent *e)
311
void TabOrderEditor::resizeEvent(QResizeEvent *e)
314
QWidget::resizeEvent(e);