1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt Designer of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://www.qtsoftware.com/contact.
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qdesigner_tabwidget_p.h"
#include "qdesigner_command_p.h"
#include "qdesigner_propertycommand_p.h"
#include "promotiontaskmenu_p.h"
#include "formwindowbase_p.h"
#include <QtDesigner/QDesignerFormWindowInterface>
#include <QtGui/QApplication>
#include <QtGui/QTabBar>
#include <QtGui/QAction>
#include <QtGui/QMouseEvent>
#include <QtGui/QMenu>
#include <QtGui/QLabel>
#include <QtGui/QTabWidget>
#include <QtCore/qdebug.h>
QT_BEGIN_NAMESPACE
namespace qdesigner_internal {
// Store tab widget as drag source
class MyMimeData : public QMimeData
{
Q_OBJECT
public:
MyMimeData(const QTabWidget *tab) : m_tab(tab) {}
static bool fromMyTab(const QMimeData *mimeData, const QTabWidget *tab) {
if (!mimeData)
return false;
const MyMimeData *m = qobject_cast<const MyMimeData *>(mimeData);
return m && m->m_tab == tab;
}
private:
const QTabWidget *m_tab;
};
} // namespace qdesigner_internal
// ------------- QTabWidgetEventFilter
QTabWidgetEventFilter::QTabWidgetEventFilter(QTabWidget *parent) :
QObject(parent),
m_tabWidget(parent),
m_dropIndicator(0),
m_dragPage(0),
m_mousePressed(false),
m_actionDeletePage(new QAction(tr("Delete"), this)),
m_actionInsertPage(new QAction(tr("Before Current Page"), this)),
m_actionInsertPageAfter(new QAction(tr("After Current Page"), this)),
m_pagePromotionTaskMenu(new qdesigner_internal::PromotionTaskMenu(0, qdesigner_internal::PromotionTaskMenu::ModeSingleWidget, this))
{
tabBar()->setAcceptDrops(true);
tabBar()->installEventFilter(this);
connect(m_actionInsertPage, SIGNAL(triggered()), this, SLOT(addPage()));
connect(m_actionInsertPageAfter, SIGNAL(triggered()), this, SLOT(addPageAfter()));
connect(m_actionDeletePage, SIGNAL(triggered()), this, SLOT(removeCurrentPage()));
}
QTabWidgetEventFilter::~QTabWidgetEventFilter()
{
}
void QTabWidgetEventFilter::install(QTabWidget *tabWidget)
{
new QTabWidgetEventFilter(tabWidget);
}
QTabWidgetEventFilter *QTabWidgetEventFilter::eventFilterOf(const QTabWidget *tabWidget)
{
// Look for 1st order children only..otherwise, we might get filters of nested tab widgets
const QObjectList children = tabWidget->children();
const QObjectList::const_iterator cend = children.constEnd();
for (QObjectList::const_iterator it = children.constBegin(); it != cend; ++it) {
QObject *o = *it;
if (!o->isWidgetType())
if (QTabWidgetEventFilter *ef = qobject_cast<QTabWidgetEventFilter*>(o))
return ef;
}
return 0;
}
QMenu *QTabWidgetEventFilter::addTabWidgetContextMenuActions(const QTabWidget *tabWidget, QMenu *popup)
{
QTabWidgetEventFilter *filter = eventFilterOf(tabWidget);
if (!filter)
return 0;
return filter->addContextMenuActions(popup);
}
QTabBar *QTabWidgetEventFilter::tabBar() const
{
// QTabWidget::tabBar() accessor is protected, grmbl...
if (!m_cachedTabBar) {
const QList<QTabBar *> tabBars = qFindChildren<QTabBar *>(m_tabWidget);
Q_ASSERT(tabBars.size() == 1);
m_cachedTabBar = tabBars.front();
}
return m_cachedTabBar;
}
static bool canMove(const QPoint &pressPoint, const QMouseEvent *e)
{
const QPoint pt = pressPoint - e->pos();
return pt.manhattanLength() > QApplication::startDragDistance();
}
bool QTabWidgetEventFilter::eventFilter(QObject *o, QEvent *e)
{
const QEvent::Type type = e->type();
// Do not try to locate tab bar and form window, etc. for uninteresting events and
// avoid asserts about missing tab bars when being destroyed
switch (type) {
case QEvent::MouseButtonDblClick:
case QEvent::MouseButtonPress:
case QEvent::MouseButtonRelease:
case QEvent::MouseMove:
case QEvent::DragLeave:
case QEvent::DragEnter:
case QEvent::DragMove:
case QEvent::Drop:
break;
default:
return false;
}
if (o != tabBar())
return false;
QDesignerFormWindowInterface *fw = formWindow();
if (!fw)
return false;
switch (type) {
case QEvent::MouseButtonDblClick:
break;
case QEvent::MouseButtonPress: {
QMouseEvent *mev = static_cast<QMouseEvent*>(e);
if (QDesignerFormWindowInterface *fw = formWindow()) {
fw->clearSelection();
fw->selectWidget(m_tabWidget, true);
}
if (mev->button() & Qt::LeftButton) {
m_mousePressed = true;
m_pressPoint = mev->pos();
QTabBar *tabbar = tabBar();
const int count = tabbar->count();
for (int i = 0; i < count; ++i) {
if (tabbar->tabRect(i).contains(m_pressPoint)) {
if (i != tabbar->currentIndex()) {
qdesigner_internal::SetPropertyCommand *cmd = new qdesigner_internal::SetPropertyCommand(fw);
cmd->init(m_tabWidget, QLatin1String("currentIndex"), i);
fw->commandHistory()->push(cmd);
}
break;
}
}
}
} break;
case QEvent::MouseButtonRelease:
m_mousePressed = false;
break;
case QEvent::MouseMove: {
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(e);
if (m_mousePressed && canMove(m_pressPoint, mouseEvent)) {
const int index = m_tabWidget->currentIndex();
if (index == -1)
break;
m_mousePressed = false;
QDrag *drg = new QDrag(m_tabWidget);
drg->setMimeData(new qdesigner_internal::MyMimeData(m_tabWidget));
m_dragIndex = index;
m_dragPage = m_tabWidget->currentWidget();
m_dragLabel = m_tabWidget->tabText(index);
m_dragIcon = m_tabWidget->tabIcon(index);
if (m_dragIcon.isNull()) {
QLabel *label = new QLabel(m_dragLabel);
label->adjustSize();
drg->setPixmap(QPixmap::grabWidget(label));
label->deleteLater();
} else {
drg->setPixmap(m_dragIcon.pixmap(22, 22));
}
m_tabWidget->removeTab(m_dragIndex);
const Qt::DropActions dropAction = drg->start(Qt::MoveAction);
if (dropAction == Qt::IgnoreAction) {
// abort
m_tabWidget->insertTab(m_dragIndex, m_dragPage, m_dragIcon, m_dragLabel);
m_tabWidget->setCurrentIndex(m_dragIndex);
}
if (m_dropIndicator)
m_dropIndicator->hide();
}
} break;
case QEvent::DragLeave: {
if (m_dropIndicator)
m_dropIndicator->hide();
} break;
case QEvent::DragEnter:
case QEvent::DragMove: {
QDragMoveEvent *de = static_cast<QDragMoveEvent*>(e);
if (!qdesigner_internal::MyMimeData::fromMyTab(de->mimeData(), m_tabWidget))
return false;
if (de->proposedAction() == Qt::MoveAction)
de->acceptProposedAction();
else {
de->setDropAction(Qt::MoveAction);
de->accept();
}
QRect rect;
const int index = pageFromPosition(de->pos(), rect);
if (!m_dropIndicator) {
m_dropIndicator = new QWidget(m_tabWidget);
QPalette p = m_dropIndicator->palette();
p.setColor(m_tabWidget->backgroundRole(), Qt::red);
m_dropIndicator->setPalette(p);
}
QPoint pos;
if (index == m_tabWidget->count())
pos = tabBar()->mapToParent(QPoint(rect.x() + rect.width(), rect.y()));
else
pos = tabBar()->mapToParent(QPoint(rect.x(), rect.y()));
m_dropIndicator->setGeometry(pos.x(), pos.y() , 3, rect.height());
m_dropIndicator->show();
} break;
case QEvent::Drop: {
QDropEvent *de = static_cast<QDropEvent*>(e);
if (!qdesigner_internal::MyMimeData::fromMyTab(de->mimeData(), m_tabWidget))
return false;
de->acceptProposedAction();
de->accept();
QRect rect;
const int newIndex = pageFromPosition(de->pos(), rect);
qdesigner_internal::MoveTabPageCommand *cmd = new qdesigner_internal::MoveTabPageCommand(fw);
m_tabWidget->insertTab(m_dragIndex, m_dragPage, m_dragIcon, m_dragLabel);
cmd->init(m_tabWidget, m_dragPage, m_dragIcon, m_dragLabel, m_dragIndex, newIndex);
fw->commandHistory()->push(cmd);
} break;
default:
break;
}
return false;
}
void QTabWidgetEventFilter::removeCurrentPage()
{
if (!m_tabWidget->currentWidget())
return;
if (QDesignerFormWindowInterface *fw = formWindow()) {
qdesigner_internal::DeleteTabPageCommand *cmd = new qdesigner_internal::DeleteTabPageCommand(fw);
cmd->init(m_tabWidget);
fw->commandHistory()->push(cmd);
}
}
void QTabWidgetEventFilter::addPage()
{
if (QDesignerFormWindowInterface *fw = formWindow()) {
qdesigner_internal::AddTabPageCommand *cmd = new qdesigner_internal::AddTabPageCommand(fw);
cmd->init(m_tabWidget, qdesigner_internal::AddTabPageCommand::InsertBefore);
fw->commandHistory()->push(cmd);
}
}
void QTabWidgetEventFilter::addPageAfter()
{
if (QDesignerFormWindowInterface *fw = formWindow()) {
qdesigner_internal::AddTabPageCommand *cmd = new qdesigner_internal::AddTabPageCommand(fw);
cmd->init(m_tabWidget, qdesigner_internal::AddTabPageCommand::InsertAfter);
fw->commandHistory()->push(cmd);
}
}
QDesignerFormWindowInterface *QTabWidgetEventFilter::formWindow() const
{
return QDesignerFormWindowInterface::findFormWindow(const_cast<QTabWidget*>(m_tabWidget));
}
// Get page from mouse position. Default to new page if in right half of last page?
int QTabWidgetEventFilter::pageFromPosition(const QPoint &pos, QRect &rect) const
{
int index = 0;
const QTabBar *tabbar = tabBar();
const int count = m_tabWidget->count();
for (; index < count; index++) {
const QRect rc = tabbar->tabRect(index);
if (rc.contains(pos)) {
rect = rc;
break;
}
}
if (index == count -1) {
QRect rect2 = rect;
rect2.setLeft(rect2.left() + rect2.width() / 2);
if (rect2.contains(pos))
index++;
}
return index;
}
QMenu *QTabWidgetEventFilter::addContextMenuActions(QMenu *popup)
{
QMenu *pageMenu = 0;
const int count = m_tabWidget->count();
m_actionDeletePage->setEnabled(count);
if (count) {
const int currentIndex = m_tabWidget->currentIndex();
const QString pageSubMenuLabel = tr("Page %1 of %2").arg(currentIndex + 1).arg(count);
pageMenu = popup->addMenu(pageSubMenuLabel);
pageMenu->addAction(m_actionDeletePage);
// Set up promotion menu for current widget.
if (QWidget *page = m_tabWidget->currentWidget ()) {
m_pagePromotionTaskMenu->setWidget(page);
m_pagePromotionTaskMenu->addActions(QDesignerFormWindowInterface::findFormWindow(m_tabWidget),
qdesigner_internal::PromotionTaskMenu::SuppressGlobalEdit,
pageMenu);
}
QMenu *insertPageMenu = popup->addMenu(tr("Insert Page"));
insertPageMenu->addAction(m_actionInsertPageAfter);
insertPageMenu->addAction(m_actionInsertPage);
} else {
QAction *insertPageAction = popup->addAction(tr("Insert Page"));
connect(insertPageAction, SIGNAL(triggered()), this, SLOT(addPage()));
}
popup->addSeparator();
return pageMenu;
}
// ----------- QTabWidgetPropertySheet
static const char *currentTabTextKey = "currentTabText";
static const char *currentTabNameKey = "currentTabName";
static const char *currentTabIconKey = "currentTabIcon";
static const char *currentTabToolTipKey = "currentTabToolTip";
static const char *currentTabWhatsThisKey = "currentTabWhatsThis";
QTabWidgetPropertySheet::QTabWidgetPropertySheet(QTabWidget *object, QObject *parent) :
QDesignerPropertySheet(object, parent),
m_tabWidget(object)
{
createFakeProperty(QLatin1String(currentTabTextKey), qVariantFromValue(qdesigner_internal::PropertySheetStringValue()));
createFakeProperty(QLatin1String(currentTabNameKey), QString());
createFakeProperty(QLatin1String(currentTabIconKey), qVariantFromValue(qdesigner_internal::PropertySheetIconValue()));
if (formWindowBase())
formWindowBase()->addReloadableProperty(this, indexOf(QLatin1String(currentTabIconKey)));
createFakeProperty(QLatin1String(currentTabToolTipKey), qVariantFromValue(qdesigner_internal::PropertySheetStringValue()));
createFakeProperty(QLatin1String(currentTabWhatsThisKey), qVariantFromValue(qdesigner_internal::PropertySheetStringValue()));
}
QTabWidgetPropertySheet::TabWidgetProperty QTabWidgetPropertySheet::tabWidgetPropertyFromName(const QString &name)
{
typedef QHash<QString, TabWidgetProperty> TabWidgetPropertyHash;
static TabWidgetPropertyHash tabWidgetPropertyHash;
if (tabWidgetPropertyHash.empty()) {
tabWidgetPropertyHash.insert(QLatin1String(currentTabTextKey), PropertyCurrentTabText);
tabWidgetPropertyHash.insert(QLatin1String(currentTabNameKey), PropertyCurrentTabName);
tabWidgetPropertyHash.insert(QLatin1String(currentTabIconKey), PropertyCurrentTabIcon);
tabWidgetPropertyHash.insert(QLatin1String(currentTabToolTipKey), PropertyCurrentTabToolTip);
tabWidgetPropertyHash.insert(QLatin1String(currentTabWhatsThisKey), PropertyCurrentTabWhatsThis);
}
return tabWidgetPropertyHash.value(name, PropertyTabWidgetNone);
}
void QTabWidgetPropertySheet::setProperty(int index, const QVariant &value)
{
const TabWidgetProperty tabWidgetProperty = tabWidgetPropertyFromName(propertyName(index));
if (tabWidgetProperty == PropertyTabWidgetNone) {
QDesignerPropertySheet::setProperty(index, value);
return;
}
// index-dependent
const int currentIndex = m_tabWidget->currentIndex();
QWidget *currentWidget = m_tabWidget->currentWidget();
if (!currentWidget)
return;
switch (tabWidgetProperty) {
case PropertyCurrentTabText:
m_tabWidget->setTabText(currentIndex, qvariant_cast<QString>(resolvePropertyValue(index, value)));
m_pageToData[currentWidget].text = qVariantValue<qdesigner_internal::PropertySheetStringValue>(value);
break;
case PropertyCurrentTabName:
currentWidget->setObjectName(value.toString());
break;
case PropertyCurrentTabIcon:
m_tabWidget->setTabIcon(currentIndex, qvariant_cast<QIcon>(resolvePropertyValue(index, value)));
m_pageToData[currentWidget].icon = qVariantValue<qdesigner_internal::PropertySheetIconValue>(value);
break;
case PropertyCurrentTabToolTip:
m_tabWidget->setTabToolTip(currentIndex, qvariant_cast<QString>(resolvePropertyValue(index, value)));
m_pageToData[currentWidget].tooltip = qVariantValue<qdesigner_internal::PropertySheetStringValue>(value);
break;
case PropertyCurrentTabWhatsThis:
m_tabWidget->setTabWhatsThis(currentIndex, qvariant_cast<QString>(resolvePropertyValue(index, value)));
m_pageToData[currentWidget].whatsthis = qVariantValue<qdesigner_internal::PropertySheetStringValue>(value);
break;
case PropertyTabWidgetNone:
break;
}
}
bool QTabWidgetPropertySheet::isEnabled(int index) const
{
if (tabWidgetPropertyFromName(propertyName(index)) == PropertyTabWidgetNone)
return QDesignerPropertySheet::isEnabled(index);
return m_tabWidget->currentIndex() != -1;
}
QVariant QTabWidgetPropertySheet::property(int index) const
{
const TabWidgetProperty tabWidgetProperty = tabWidgetPropertyFromName(propertyName(index));
if (tabWidgetProperty == PropertyTabWidgetNone)
return QDesignerPropertySheet::property(index);
// index-dependent
QWidget *currentWidget = m_tabWidget->currentWidget();
if (!currentWidget) {
if (tabWidgetProperty == PropertyCurrentTabIcon)
return qVariantFromValue(qdesigner_internal::PropertySheetIconValue());
if (tabWidgetProperty == PropertyCurrentTabText)
return qVariantFromValue(qdesigner_internal::PropertySheetStringValue());
if (tabWidgetProperty == PropertyCurrentTabToolTip)
return qVariantFromValue(qdesigner_internal::PropertySheetStringValue());
if (tabWidgetProperty == PropertyCurrentTabWhatsThis)
return qVariantFromValue(qdesigner_internal::PropertySheetStringValue());
return QVariant(QString());
}
// index-dependent
switch (tabWidgetProperty) {
case PropertyCurrentTabText:
return qVariantFromValue(m_pageToData.value(currentWidget).text);
case PropertyCurrentTabName:
return currentWidget->objectName();
case PropertyCurrentTabIcon:
return qVariantFromValue(m_pageToData.value(currentWidget).icon);
case PropertyCurrentTabToolTip:
return qVariantFromValue(m_pageToData.value(currentWidget).tooltip);
case PropertyCurrentTabWhatsThis:
return qVariantFromValue(m_pageToData.value(currentWidget).whatsthis);
case PropertyTabWidgetNone:
break;
}
return QVariant();
}
bool QTabWidgetPropertySheet::reset(int index)
{
const TabWidgetProperty tabWidgetProperty = tabWidgetPropertyFromName(propertyName(index));
if (tabWidgetProperty == PropertyTabWidgetNone)
return QDesignerPropertySheet::reset(index);
// index-dependent
QWidget *currentWidget = m_tabWidget->currentWidget();
if (!currentWidget)
return false;
// index-dependent
switch (tabWidgetProperty) {
case PropertyCurrentTabName:
setProperty(index, QString());
break;
case PropertyCurrentTabToolTip:
m_pageToData[currentWidget].tooltip = qdesigner_internal::PropertySheetStringValue();
setProperty(index, QString());
break;
case PropertyCurrentTabWhatsThis:
m_pageToData[currentWidget].whatsthis = qdesigner_internal::PropertySheetStringValue();
setProperty(index, QString());
break;
case PropertyCurrentTabText:
m_pageToData[currentWidget].text = qdesigner_internal::PropertySheetStringValue();
setProperty(index, QString());
break;
case PropertyCurrentTabIcon:
m_pageToData[currentWidget].icon = qdesigner_internal::PropertySheetIconValue();
setProperty(index, QIcon());
break;
case PropertyTabWidgetNone:
break;
}
return true;
}
bool QTabWidgetPropertySheet::checkProperty(const QString &propertyName)
{
switch (tabWidgetPropertyFromName(propertyName)) {
case PropertyCurrentTabText:
case PropertyCurrentTabName:
case PropertyCurrentTabToolTip:
case PropertyCurrentTabWhatsThis:
case PropertyCurrentTabIcon:
return false;
default:
break;
}
return true;
}
QT_END_NAMESPACE
#include "qdesigner_tabwidget.moc" // required for MyMimeData
|