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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
|
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** 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 Technology Preview License Agreement accompanying
** this package.
**
** 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.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qdesigner_menubar_p.h"
#include "qdesigner_menu_p.h"
#include "qdesigner_command_p.h"
#include "qdesigner_propertycommand_p.h"
#include "actionrepository_p.h"
#include "actionprovider_p.h"
#include "actioneditor_p.h"
#include "qdesigner_utils_p.h"
#include "promotiontaskmenu_p.h"
#include "qdesigner_objectinspector_p.h"
#include <QtDesigner/QDesignerFormWindowInterface>
#include <QtDesigner/QDesignerFormEditorInterface>
#include <QtDesigner/QDesignerWidgetFactoryInterface>
#include <QtDesigner/QExtensionManager>
#include <QtCore/QMimeData>
#include <QtCore/qdebug.h>
#include <QtGui/QApplication>
#include <QtGui/QDrag>
#include <QtGui/QLineEdit>
#include <QtGui/QPainter>
#include <QtGui/qevent.h>
Q_DECLARE_METATYPE(QAction*)
QT_BEGIN_NAMESPACE
typedef QList<QAction *> ActionList;
using namespace qdesigner_internal;
namespace qdesigner_internal
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////
SpecialMenuAction::SpecialMenuAction(QObject *parent)
: QAction(parent)
{
}
SpecialMenuAction::~SpecialMenuAction()
{
}
} // namespace qdesigner_internal
/////////////////////////////////////////////////////////////////////////////////////////////////////////
QDesignerMenuBar::QDesignerMenuBar(QWidget *parent) :
QMenuBar(parent),
m_addMenu(new SpecialMenuAction(this)),
m_currentIndex(0),
m_interactive(true),
m_editor(new QLineEdit(this)),
m_dragging(false),
m_lastMenuActionIndex( -1),
m_promotionTaskMenu(new PromotionTaskMenu(this, PromotionTaskMenu::ModeSingleWidget, this))
{
setContextMenuPolicy(Qt::DefaultContextMenu);
setAcceptDrops(true); // ### fake
// Fake property: Keep the menu bar editable in the form even if a native menu bar is used.
setNativeMenuBar(false);
m_addMenu->setText(tr("Type Here"));
addAction(m_addMenu);
QFont italic;
italic.setItalic(true);
m_addMenu->setFont(italic);
m_editor->setObjectName(QLatin1String("__qt__passive_editor"));
m_editor->hide();
m_editor->installEventFilter(this);
installEventFilter(this);
}
QDesignerMenuBar::~QDesignerMenuBar()
{
}
void QDesignerMenuBar::paintEvent(QPaintEvent *event)
{
QMenuBar::paintEvent(event);
QPainter p(this);
foreach (QAction *a, actions()) {
if (qobject_cast<SpecialMenuAction*>(a)) {
const QRect g = actionGeometry(a);
QLinearGradient lg(g.left(), g.top(), g.left(), g.bottom());
lg.setColorAt(0.0, Qt::transparent);
lg.setColorAt(0.7, QColor(0, 0, 0, 32));
lg.setColorAt(1.0, Qt::transparent);
p.fillRect(g, lg);
}
}
QAction *action = currentAction();
if (m_dragging || !action)
return;
if (hasFocus()) {
const QRect g = actionGeometry(action);
QDesignerMenu::drawSelection(&p, g.adjusted(1, 1, -1, -1));
} else if (action->menu() && action->menu()->isVisible()) {
const QRect g = actionGeometry(action);
p.drawRect(g.adjusted(1, 1, -1, -1));
}
}
bool QDesignerMenuBar::handleEvent(QWidget *widget, QEvent *event)
{
if (!formWindow())
return false;
if (event->type() == QEvent::FocusIn || event->type() == QEvent::FocusOut)
update();
switch (event->type()) {
default: break;
case QEvent::MouseButtonDblClick:
return handleMouseDoubleClickEvent(widget, static_cast<QMouseEvent*>(event));
case QEvent::MouseButtonPress:
return handleMousePressEvent(widget, static_cast<QMouseEvent*>(event));
case QEvent::MouseButtonRelease:
return handleMouseReleaseEvent(widget, static_cast<QMouseEvent*>(event));
case QEvent::MouseMove:
return handleMouseMoveEvent(widget, static_cast<QMouseEvent*>(event));
case QEvent::ContextMenu:
return handleContextMenuEvent(widget, static_cast<QContextMenuEvent*>(event));
case QEvent::KeyPress:
return handleKeyPressEvent(widget, static_cast<QKeyEvent*>(event));
case QEvent::FocusIn:
case QEvent::FocusOut:
return widget != m_editor;
}
return true;
}
bool QDesignerMenuBar::handleMouseDoubleClickEvent(QWidget *, QMouseEvent *event)
{
if (!rect().contains(event->pos()))
return true;
if ((event->buttons() & Qt::LeftButton) != Qt::LeftButton)
return true;
event->accept();
m_startPosition = QPoint();
m_currentIndex = actionIndexAt(this, event->pos(), Qt::Horizontal);
if (m_currentIndex != -1) {
showLineEdit();
}
return true;
}
bool QDesignerMenuBar::handleKeyPressEvent(QWidget *, QKeyEvent *e)
{
if (m_editor->isHidden()) { // In navigation mode
switch (e->key()) {
case Qt::Key_Delete:
if (m_currentIndex == -1 || m_currentIndex >= realActionCount())
break;
hideMenu();
deleteMenu();
break;
case Qt::Key_Left:
e->accept();
moveLeft(e->modifiers() & Qt::ControlModifier);
return true;
case Qt::Key_Right:
e->accept();
moveRight(e->modifiers() & Qt::ControlModifier);
return true; // no update
case Qt::Key_Up:
e->accept();
moveUp();
return true;
case Qt::Key_Down:
e->accept();
moveDown();
return true;
case Qt::Key_PageUp:
m_currentIndex = 0;
break;
case Qt::Key_PageDown:
m_currentIndex = actions().count() - 1;
break;
case Qt::Key_Enter:
case Qt::Key_Return:
e->accept();
enterEditMode();
return true; // no update
case Qt::Key_Alt:
case Qt::Key_Shift:
case Qt::Key_Control:
case Qt::Key_Escape:
e->ignore();
setFocus(); // FIXME: this is because some other widget get the focus when CTRL is pressed
return true; // no update
default:
if (!e->text().isEmpty() && e->text().at(0).toLatin1() >= 32) {
showLineEdit();
QApplication::sendEvent(m_editor, e);
e->accept();
} else {
e->ignore();
}
return true;
}
} else { // In edit mode
switch (e->key()) {
default:
return false;
case Qt::Key_Control:
e->ignore();
return true;
case Qt::Key_Enter:
case Qt::Key_Return:
if (!m_editor->text().isEmpty()) {
leaveEditMode(ForceAccept);
if (m_lastFocusWidget)
m_lastFocusWidget->setFocus();
m_editor->hide();
showMenu();
break;
}
// fall through
case Qt::Key_Escape:
update();
setFocus();
break;
}
}
e->accept();
update();
return true;
}
void QDesignerMenuBar::startDrag(const QPoint &pos)
{
const int index = findAction(pos);
if (m_currentIndex == -1 || index >= realActionCount())
return;
QAction *action = safeActionAt(index);
QDesignerFormWindowInterface *fw = formWindow();
RemoveActionFromCommand *cmd = new RemoveActionFromCommand(fw);
cmd->init(this, action, actions().at(index + 1));
fw->commandHistory()->push(cmd);
adjustSize();
hideMenu(index);
QDrag *drag = new QDrag(this);
drag->setPixmap(ActionRepositoryMimeData::actionDragPixmap(action));
drag->setMimeData(new ActionRepositoryMimeData(action, Qt::MoveAction));
const int old_index = m_currentIndex;
m_currentIndex = -1;
if (drag->start(Qt::MoveAction) == Qt::IgnoreAction) {
InsertActionIntoCommand *cmd = new InsertActionIntoCommand(fw);
cmd->init(this, action, safeActionAt(index));
fw->commandHistory()->push(cmd);
m_currentIndex = old_index;
adjustSize();
}
}
bool QDesignerMenuBar::handleMousePressEvent(QWidget *, QMouseEvent *event)
{
m_startPosition = QPoint();
event->accept();
if (event->button() != Qt::LeftButton)
return true;
m_startPosition = event->pos();
const int newIndex = actionIndexAt(this, m_startPosition, Qt::Horizontal);
const bool changed = newIndex != m_currentIndex;
m_currentIndex = newIndex;
updateCurrentAction(changed);
return true;
}
bool QDesignerMenuBar::handleMouseReleaseEvent(QWidget *, QMouseEvent *event)
{
m_startPosition = QPoint();
if (event->button() != Qt::LeftButton)
return true;
event->accept();
m_currentIndex = actionIndexAt(this, event->pos(), Qt::Horizontal);
if (!m_editor->isVisible() && m_currentIndex != -1 && m_currentIndex < realActionCount())
showMenu();
return true;
}
bool QDesignerMenuBar::handleMouseMoveEvent(QWidget *, QMouseEvent *event)
{
if ((event->buttons() & Qt::LeftButton) != Qt::LeftButton)
return true;
if (m_startPosition.isNull())
return true;
const QPoint pos = mapFromGlobal(event->globalPos());
if ((pos - m_startPosition).manhattanLength() < qApp->startDragDistance())
return true;
const int index = actionIndexAt(this, m_startPosition, Qt::Horizontal);
if (index < actions().count()) {
hideMenu(index);
update();
}
startDrag(m_startPosition);
m_startPosition = QPoint();
return true;
}
ActionList QDesignerMenuBar::contextMenuActions()
{
ActionList rc;
if (QAction *action = safeActionAt(m_currentIndex)) {
if (!qobject_cast<SpecialMenuAction*>(action)) {
QVariant itemData;
qVariantSetValue(itemData, action);
QAction *remove_action = new QAction(tr("Remove Menu '%1'").arg(action->menu()->objectName()), 0);
remove_action->setData(itemData);
connect(remove_action, SIGNAL(triggered()), this, SLOT(deleteMenu()));
rc.push_back(remove_action);
QAction *sep = new QAction(0);
sep->setSeparator(true);
rc.push_back(sep);
}
}
m_promotionTaskMenu->addActions(formWindow(), PromotionTaskMenu::TrailingSeparator, rc);
QAction *remove_menubar = new QAction(tr("Remove Menu Bar"), 0);
connect(remove_menubar, SIGNAL(triggered()), this, SLOT(slotRemoveMenuBar()));
rc.push_back(remove_menubar);
return rc;
}
bool QDesignerMenuBar::handleContextMenuEvent(QWidget *, QContextMenuEvent *event)
{
event->accept();
m_currentIndex = actionIndexAt(this, mapFromGlobal(event->globalPos()), Qt::Horizontal);
update();
QMenu menu;
const ActionList al = contextMenuActions();
const ActionList::const_iterator acend = al.constEnd();
for (ActionList::const_iterator it = al.constBegin(); it != acend; ++it)
menu.addAction(*it);
menu.exec(event->globalPos());
return true;
}
void QDesignerMenuBar::slotRemoveMenuBar()
{
Q_ASSERT(formWindow() != 0);
QDesignerFormWindowInterface *fw = formWindow();
DeleteMenuBarCommand *cmd = new DeleteMenuBarCommand(fw);
cmd->init(this);
fw->commandHistory()->push(cmd);
}
void QDesignerMenuBar::focusOutEvent(QFocusEvent *event)
{
QMenuBar::focusOutEvent(event);
}
void QDesignerMenuBar::enterEditMode()
{
if (m_currentIndex >= 0 && m_currentIndex <= realActionCount()) {
showLineEdit();
}
}
void QDesignerMenuBar::leaveEditMode(LeaveEditMode mode)
{
m_editor->releaseKeyboard();
if (mode == Default)
return;
if (m_editor->text().isEmpty())
return;
QAction *action = 0;
QDesignerFormWindowInterface *fw = formWindow();
Q_ASSERT(fw);
if (m_currentIndex >= 0 && m_currentIndex < realActionCount()) {
action = safeActionAt(m_currentIndex);
fw->beginCommand(QApplication::translate("Command", "Change Title"));
} else {
fw->beginCommand(QApplication::translate("Command", "Insert Menu"));
const QString niceObjectName = ActionEditor::actionTextToName(m_editor->text(), QLatin1String("menu"));
QMenu *menu = qobject_cast<QMenu*>(fw->core()->widgetFactory()->createWidget(QLatin1String("QMenu"), this));
fw->core()->widgetFactory()->initialize(menu);
menu->setObjectName(niceObjectName);
menu->setTitle(tr("Menu"));
fw->ensureUniqueObjectName(menu);
action = menu->menuAction();
AddMenuActionCommand *cmd = new AddMenuActionCommand(fw);
cmd->init(action, m_addMenu, this, this);
fw->commandHistory()->push(cmd);
}
SetPropertyCommand *cmd = new SetPropertyCommand(fw);
cmd->init(action, QLatin1String("text"), m_editor->text());
fw->commandHistory()->push(cmd);
fw->endCommand();
}
void QDesignerMenuBar::showLineEdit()
{
QAction *action = 0;
if (m_currentIndex >= 0 && m_currentIndex < realActionCount())
action = safeActionAt(m_currentIndex);
else
action = m_addMenu;
if (action->isSeparator())
return;
// hideMenu();
m_lastFocusWidget = qApp->focusWidget();
// open edit field for item name
const QString text = action != m_addMenu ? action->text() : QString();
m_editor->setText(text);
m_editor->selectAll();
m_editor->setGeometry(actionGeometry(action));
m_editor->show();
qApp->setActiveWindow(m_editor);
m_editor->setFocus();
m_editor->grabKeyboard();
}
bool QDesignerMenuBar::eventFilter(QObject *object, QEvent *event)
{
if (object != this && object != m_editor)
return false;
if (!m_editor->isHidden() && object == m_editor && event->type() == QEvent::FocusOut) {
leaveEditMode(Default);
m_editor->hide();
update();
return true;
}
bool dispatch = true;
switch (event->type()) {
default: break;
case QEvent::KeyPress:
case QEvent::KeyRelease:
case QEvent::ContextMenu:
case QEvent::MouseMove:
case QEvent::MouseButtonPress:
case QEvent::MouseButtonRelease:
case QEvent::MouseButtonDblClick:
dispatch = (object != m_editor);
// no break
case QEvent::Enter:
case QEvent::Leave:
case QEvent::FocusIn:
case QEvent::FocusOut:
{
QWidget *widget = qobject_cast<QWidget*>(object);
if (dispatch && widget && (widget == this || isAncestorOf(widget)))
return handleEvent(widget, event);
} break;
case QEvent::Shortcut:
event->accept();
return true;
}
return false;
};
int QDesignerMenuBar::findAction(const QPoint &pos) const
{
const int index = actionIndexAt(this, pos, Qt::Horizontal);
if (index == -1)
return realActionCount();
return index;
}
void QDesignerMenuBar::adjustIndicator(const QPoint &pos)
{
const int index = findAction(pos);
QAction *action = safeActionAt(index);
Q_ASSERT(action != 0);
if (pos != QPoint(-1, -1)) {
QDesignerMenu *m = qobject_cast<QDesignerMenu*>(action->menu());
if (!m || m->parentMenu()) {
m_currentIndex = index;
showMenu(index);
}
}
if (QDesignerActionProviderExtension *a = actionProvider()) {
a->adjustIndicator(pos);
}
}
QDesignerMenuBar::ActionDragCheck QDesignerMenuBar::checkAction(QAction *action) const
{
// action belongs to another form
if (!action || !Utils::isObjectAncestorOf(formWindow()->mainContainer(), action))
return NoActionDrag;
if (!action->menu())
return ActionDragOnSubMenu; // simple action only on sub menus
QDesignerMenu *m = qobject_cast<QDesignerMenu*>(action->menu());
if (m && m->parentMenu())
return ActionDragOnSubMenu; // it looks like a submenu
if (actions().contains(action))
return ActionDragOnSubMenu; // we already have the action in the menubar
return AcceptActionDrag;
}
void QDesignerMenuBar::dragEnterEvent(QDragEnterEvent *event)
{
const ActionRepositoryMimeData *d = qobject_cast<const ActionRepositoryMimeData*>(event->mimeData());
if (!d || d->actionList().empty()) {
event->ignore();
return;
}
QAction *action = d->actionList().first();
switch (checkAction(action)) {
case NoActionDrag:
event->ignore();
break;
case ActionDragOnSubMenu:
m_dragging = true;
d->accept(event);
break;
case AcceptActionDrag:
m_dragging = true;
d->accept(event);
adjustIndicator(event->pos());
break;
}
}
void QDesignerMenuBar::dragMoveEvent(QDragMoveEvent *event)
{
const ActionRepositoryMimeData *d = qobject_cast<const ActionRepositoryMimeData*>(event->mimeData());
if (!d || d->actionList().empty()) {
event->ignore();
return;
}
QAction *action = d->actionList().first();
switch (checkAction(action)) {
case NoActionDrag:
event->ignore();
break;
case ActionDragOnSubMenu:
event->ignore();
showMenu(findAction(event->pos()));
break;
case AcceptActionDrag:
d->accept(event);
adjustIndicator(event->pos());
break;
}
}
void QDesignerMenuBar::dragLeaveEvent(QDragLeaveEvent *)
{
m_dragging = false;
adjustIndicator(QPoint(-1, -1));
}
void QDesignerMenuBar::dropEvent(QDropEvent *event)
{
m_dragging = false;
if (const ActionRepositoryMimeData *d = qobject_cast<const ActionRepositoryMimeData*>(event->mimeData())) {
QAction *action = d->actionList().first();
if (checkAction(action) == AcceptActionDrag) {
event->acceptProposedAction();
int index = findAction(event->pos());
index = qMin(index, actions().count() - 1);
QDesignerFormWindowInterface *fw = formWindow();
InsertActionIntoCommand *cmd = new InsertActionIntoCommand(fw);
cmd->init(this, action, safeActionAt(index));
fw->commandHistory()->push(cmd);
m_currentIndex = index;
update();
adjustIndicator(QPoint(-1, -1));
return;
}
}
event->ignore();
}
void QDesignerMenuBar::actionEvent(QActionEvent *event)
{
QMenuBar::actionEvent(event);
}
QDesignerFormWindowInterface *QDesignerMenuBar::formWindow() const
{
return QDesignerFormWindowInterface::findFormWindow(const_cast<QDesignerMenuBar*>(this));
}
QDesignerActionProviderExtension *QDesignerMenuBar::actionProvider()
{
if (QDesignerFormWindowInterface *fw = formWindow()) {
QDesignerFormEditorInterface *core = fw->core();
return qt_extension<QDesignerActionProviderExtension*>(core->extensionManager(), this);
}
return 0;
}
QAction *QDesignerMenuBar::currentAction() const
{
if (m_currentIndex < 0 || m_currentIndex >= actions().count())
return 0;
return safeActionAt(m_currentIndex);
}
int QDesignerMenuBar::realActionCount() const
{
return actions().count() - 1; // 1 fake actions
}
bool QDesignerMenuBar::dragging() const
{
return m_dragging;
}
void QDesignerMenuBar::moveLeft(bool ctrl)
{
if (layoutDirection() == Qt::LeftToRight) {
movePrevious(ctrl);
} else {
moveNext(ctrl);
}
}
void QDesignerMenuBar::moveRight(bool ctrl)
{
if (layoutDirection() == Qt::LeftToRight) {
moveNext(ctrl);
} else {
movePrevious(ctrl);
}
}
void QDesignerMenuBar::movePrevious(bool ctrl)
{
const bool swapped = ctrl && swapActions(m_currentIndex, m_currentIndex - 1);
const int newIndex = qMax(0, m_currentIndex - 1);
// Always re-select, swapping destroys order
if (swapped || newIndex != m_currentIndex) {
m_currentIndex = newIndex;
updateCurrentAction(true);
}
}
void QDesignerMenuBar::moveNext(bool ctrl)
{
const bool swapped = ctrl && swapActions(m_currentIndex + 1, m_currentIndex);
const int newIndex = qMin(actions().count() - 1, m_currentIndex + 1);
if (swapped || newIndex != m_currentIndex) {
m_currentIndex = newIndex;
updateCurrentAction(!ctrl);
}
}
void QDesignerMenuBar::moveUp()
{
update();
}
void QDesignerMenuBar::moveDown()
{
showMenu();
}
void QDesignerMenuBar::adjustSpecialActions()
{
removeAction(m_addMenu);
addAction(m_addMenu);
}
bool QDesignerMenuBar::interactive(bool i)
{
const bool old = m_interactive;
m_interactive = i;
return old;
}
void QDesignerMenuBar::hideMenu(int index)
{
if (index < 0 && m_currentIndex >= 0)
index = m_currentIndex;
if (index < 0 || index >= realActionCount())
return;
QAction *action = safeActionAt(index);
if (action && action->menu()) {
action->menu()->hide();
if (QDesignerMenu *menu = qobject_cast<QDesignerMenu*>(action->menu())) {
menu->closeMenuChain();
}
}
}
void QDesignerMenuBar::deleteMenu()
{
deleteMenuAction(currentAction());
}
void QDesignerMenuBar::deleteMenuAction(QAction *action)
{
if (action && !qobject_cast<SpecialMenuAction*>(action)) {
const int pos = actions().indexOf(action);
QAction *action_before = 0;
if (pos != -1)
action_before = safeActionAt(pos + 1);
QDesignerFormWindowInterface *fw = formWindow();
RemoveMenuActionCommand *cmd = new RemoveMenuActionCommand(fw);
cmd->init(action, action_before, this, this);
fw->commandHistory()->push(cmd);
}
}
void QDesignerMenuBar::showMenu(int index)
{
if (index < 0 && m_currentIndex >= 0)
index = m_currentIndex;
if (index < 0 || index >= realActionCount())
return;
m_currentIndex = index;
QAction *action = currentAction();
if (action && action->menu()) {
if (m_lastMenuActionIndex != -1 && m_lastMenuActionIndex != index) {
hideMenu(m_lastMenuActionIndex);
}
m_lastMenuActionIndex = index;
QMenu *menu = action->menu();
const QRect g = actionGeometry(action);
if (!menu->isVisible()) {
if ((menu->windowFlags() & Qt::Popup) != Qt::Popup)
menu->setWindowFlags(Qt::Popup);
menu->adjustSize();
if (layoutDirection() == Qt::LeftToRight) {
menu->move(mapToGlobal(g.bottomLeft()));
} else {
// The position is not initially correct due to the unknown width,
// causing it to overlap a bit the first time it is invoked.
const QSize menuSize = menu->size();
QPoint point = g.bottomRight() - QPoint(menu->width(), 0);
menu->move(mapToGlobal(point));
}
menu->setFocus(Qt::MouseFocusReason);
menu->raise();
menu->show();
} else {
menu->raise();
}
}
}
QAction *QDesignerMenuBar::safeActionAt(int index) const
{
if (index < 0 || index >= actions().count())
return 0;
return actions().at(index);
}
bool QDesignerMenuBar::swapActions(int a, int b)
{
const int left = qMin(a, b);
int right = qMax(a, b);
QAction *action_a = safeActionAt(left);
QAction *action_b = safeActionAt(right);
if (action_a == action_b
|| !action_a
|| !action_b
|| qobject_cast<SpecialMenuAction*>(action_a)
|| qobject_cast<SpecialMenuAction*>(action_b))
return false; // nothing to do
right = qMin(right, realActionCount());
if (right < 0)
return false; // nothing to do
formWindow()->beginCommand(QApplication::translate("Command", "Move action"));
QAction *action_b_before = safeActionAt(right + 1);
QDesignerFormWindowInterface *fw = formWindow();
RemoveActionFromCommand *cmd1 = new RemoveActionFromCommand(fw);
cmd1->init(this, action_b, action_b_before, false);
fw->commandHistory()->push(cmd1);
QAction *action_a_before = safeActionAt(left + 1);
InsertActionIntoCommand *cmd2 = new InsertActionIntoCommand(fw);
cmd2->init(this, action_b, action_a_before, false);
fw->commandHistory()->push(cmd2);
RemoveActionFromCommand *cmd3 = new RemoveActionFromCommand(fw);
cmd3->init(this, action_a, action_b, false);
fw->commandHistory()->push(cmd3);
InsertActionIntoCommand *cmd4 = new InsertActionIntoCommand(fw);
cmd4->init(this, action_a, action_b_before, true);
fw->commandHistory()->push(cmd4);
fw->endCommand();
return true;
}
void QDesignerMenuBar::keyPressEvent(QKeyEvent *event)
{
event->ignore();
}
void QDesignerMenuBar::keyReleaseEvent(QKeyEvent *event)
{
event->ignore();
}
void QDesignerMenuBar::updateCurrentAction(bool selectAction)
{
update();
if (!selectAction)
return;
QAction *action = currentAction();
if (!action || action == m_addMenu)
return;
QMenu *menu = action->menu();
if (!menu)
return;
QDesignerObjectInspector *oi = 0;
if (QDesignerFormWindowInterface *fw = formWindow())
oi = qobject_cast<QDesignerObjectInspector *>(fw->core()->objectInspector());
if (!oi)
return;
oi->clearSelection();
oi->selectObject(menu);
}
QT_END_NAMESPACE
|