author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Wed, 18 Aug 2010 10:37:55 +0300 | |
changeset 33 | 3e2da88830cd |
parent 18 | 2f34d5167611 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the Qt Designer of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qdesigner_menu_p.h" |
|
43 |
#include "qdesigner_menubar_p.h" |
|
44 |
#include "qdesigner_toolbar_p.h" |
|
45 |
#include "qdesigner_command_p.h" |
|
46 |
#include "qdesigner_propertycommand_p.h" |
|
47 |
#include "actionrepository_p.h" |
|
48 |
#include "actionprovider_p.h" |
|
49 |
#include "actioneditor_p.h" |
|
50 |
#include "qdesigner_utils_p.h" |
|
51 |
#include "qdesigner_objectinspector_p.h" |
|
52 |
||
53 |
#include <QtCore/QTimer> |
|
54 |
#include <QtCore/qdebug.h> |
|
55 |
||
56 |
#include <QtDesigner/QDesignerFormEditorInterface> |
|
57 |
#include <QtDesigner/QDesignerWidgetFactoryInterface> |
|
58 |
#include <QtDesigner/QDesignerMetaDataBaseInterface> |
|
59 |
#include <QtDesigner/QExtensionManager> |
|
60 |
||
61 |
#include <QtGui/QAction> |
|
62 |
#include <QtGui/QApplication> |
|
63 |
#include <QtGui/QLineEdit> |
|
64 |
#include <QtGui/QPainter> |
|
65 |
#include <QtGui/QRubberBand> |
|
66 |
#include <QtGui/QToolTip> |
|
67 |
#include <QtGui/QToolBar> |
|
68 |
#include <QtGui/qevent.h> |
|
69 |
||
70 |
Q_DECLARE_METATYPE(QAction*) |
|
71 |
||
72 |
QT_BEGIN_NAMESPACE |
|
73 |
||
74 |
using namespace qdesigner_internal; |
|
75 |
||
76 |
// give the user a little more space to click on the sub menu rectangle |
|
77 |
static inline void extendClickableArea(QRect *subMenuRect, Qt::LayoutDirection dir) |
|
78 |
{ |
|
79 |
switch (dir) { |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
80 |
case Qt::LayoutDirectionAuto: // Should never happen |
0 | 81 |
case Qt::LeftToRight: |
82 |
subMenuRect->setLeft(subMenuRect->left() - 20); |
|
83 |
break; |
|
84 |
case Qt::RightToLeft: |
|
85 |
subMenuRect->setRight(subMenuRect->right() + 20); |
|
86 |
break; |
|
87 |
} |
|
88 |
} |
|
89 |
||
90 |
QDesignerMenu::QDesignerMenu(QWidget *parent) : |
|
91 |
QMenu(parent), |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
92 |
m_subMenuPixmap(QPixmap(QLatin1String(":/trolltech/formeditor/images/submenu.png"))), |
0 | 93 |
m_currentIndex(0), |
94 |
m_addItem(new SpecialMenuAction(this)), |
|
95 |
m_addSeparator(new SpecialMenuAction(this)), |
|
96 |
m_showSubMenuTimer(new QTimer(this)), |
|
97 |
m_deactivateWindowTimer(new QTimer(this)), |
|
98 |
m_adjustSizeTimer(new QTimer(this)), |
|
99 |
m_editor(new QLineEdit(this)), |
|
100 |
m_dragging(false), |
|
101 |
m_lastSubMenuIndex(-1) |
|
102 |
{ |
|
103 |
setContextMenuPolicy(Qt::DefaultContextMenu); |
|
104 |
setAcceptDrops(true); // ### fake |
|
105 |
setSeparatorsCollapsible(false); |
|
106 |
||
107 |
connect(m_adjustSizeTimer, SIGNAL(timeout()), this, SLOT(slotAdjustSizeNow())); |
|
108 |
m_addItem->setText(tr("Type Here")); |
|
109 |
addAction(m_addItem); |
|
110 |
||
111 |
m_addSeparator->setText(tr("Add Separator")); |
|
112 |
addAction(m_addSeparator); |
|
113 |
||
114 |
connect(m_showSubMenuTimer, SIGNAL(timeout()), this, SLOT(slotShowSubMenuNow())); |
|
115 |
||
116 |
connect(m_deactivateWindowTimer, SIGNAL(timeout()), this, SLOT(slotDeactivateNow())); |
|
117 |
||
118 |
m_editor->setObjectName(QLatin1String("__qt__passive_editor")); |
|
119 |
m_editor->hide(); |
|
120 |
||
121 |
m_editor->installEventFilter(this); |
|
122 |
installEventFilter(this); |
|
123 |
} |
|
124 |
||
125 |
QDesignerMenu::~QDesignerMenu() |
|
126 |
{ |
|
127 |
} |
|
128 |
||
129 |
void QDesignerMenu::slotAdjustSizeNow() |
|
130 |
{ |
|
131 |
// Not using a single-shot, since we want to compress the timers if many items are being |
|
132 |
// adjusted |
|
133 |
m_adjustSizeTimer->stop(); |
|
134 |
adjustSize(); |
|
135 |
} |
|
136 |
||
137 |
bool QDesignerMenu::handleEvent(QWidget *widget, QEvent *event) |
|
138 |
{ |
|
139 |
if (event->type() == QEvent::FocusIn || event->type() == QEvent::FocusOut) { |
|
140 |
update(); |
|
141 |
||
142 |
if (widget == m_editor) |
|
143 |
return false; |
|
144 |
} |
|
145 |
||
146 |
switch (event->type()) { |
|
147 |
default: break; |
|
148 |
||
149 |
case QEvent::MouseButtonPress: |
|
150 |
return handleMousePressEvent(widget, static_cast<QMouseEvent*>(event)); |
|
151 |
case QEvent::MouseButtonRelease: |
|
152 |
return handleMouseReleaseEvent(widget, static_cast<QMouseEvent*>(event)); |
|
153 |
case QEvent::MouseButtonDblClick: |
|
154 |
return handleMouseDoubleClickEvent(widget, static_cast<QMouseEvent*>(event)); |
|
155 |
case QEvent::MouseMove: |
|
156 |
return handleMouseMoveEvent(widget, static_cast<QMouseEvent*>(event)); |
|
157 |
case QEvent::ContextMenu: |
|
158 |
return handleContextMenuEvent(widget, static_cast<QContextMenuEvent*>(event)); |
|
159 |
case QEvent::KeyPress: |
|
160 |
return handleKeyPressEvent(widget, static_cast<QKeyEvent*>(event)); |
|
161 |
} |
|
162 |
||
163 |
return true; |
|
164 |
} |
|
165 |
||
166 |
void QDesignerMenu::startDrag(const QPoint &pos, Qt::KeyboardModifiers modifiers) |
|
167 |
{ |
|
168 |
const int index = findAction(pos); |
|
169 |
if (index >= realActionCount()) |
|
170 |
return; |
|
171 |
||
172 |
QAction *action = safeActionAt(index); |
|
173 |
||
174 |
QDesignerFormWindowInterface *fw = formWindow(); |
|
175 |
const Qt::DropAction dropAction = (modifiers & Qt::ControlModifier) ? Qt::CopyAction : Qt::MoveAction; |
|
176 |
if (dropAction == Qt::MoveAction) { |
|
177 |
RemoveActionFromCommand *cmd = new RemoveActionFromCommand(fw); |
|
178 |
cmd->init(this, action, actions().at(index + 1)); |
|
179 |
fw->commandHistory()->push(cmd); |
|
180 |
} |
|
181 |
||
182 |
QDrag *drag = new QDrag(this); |
|
183 |
drag->setPixmap(ActionRepositoryMimeData::actionDragPixmap(action)); |
|
184 |
drag->setMimeData(new ActionRepositoryMimeData(action, dropAction)); |
|
185 |
||
186 |
const int old_index = m_currentIndex; |
|
187 |
m_currentIndex = -1; |
|
188 |
||
189 |
if (drag->start(dropAction) == Qt::IgnoreAction) { |
|
190 |
if (dropAction == Qt::MoveAction) { |
|
191 |
QAction *previous = safeActionAt(index); |
|
192 |
InsertActionIntoCommand *cmd = new InsertActionIntoCommand(fw); |
|
193 |
cmd->init(this, action, previous); |
|
194 |
fw->commandHistory()->push(cmd); |
|
195 |
} |
|
196 |
||
197 |
m_currentIndex = old_index; |
|
198 |
} |
|
199 |
} |
|
200 |
||
201 |
bool QDesignerMenu::handleKeyPressEvent(QWidget * /*widget*/, QKeyEvent *e) |
|
202 |
{ |
|
203 |
m_showSubMenuTimer->stop(); |
|
204 |
||
205 |
if (m_editor->isHidden() && hasFocus()) { // In navigation mode |
|
206 |
switch (e->key()) { |
|
207 |
||
208 |
case Qt::Key_Delete: |
|
209 |
if (m_currentIndex == -1 || m_currentIndex >= realActionCount()) |
|
210 |
break; |
|
211 |
hideSubMenu(); |
|
212 |
deleteAction(); |
|
213 |
break; |
|
214 |
||
215 |
case Qt::Key_Left: |
|
216 |
e->accept(); |
|
217 |
moveLeft(); |
|
218 |
return true; |
|
219 |
||
220 |
case Qt::Key_Right: |
|
221 |
e->accept(); |
|
222 |
moveRight(); |
|
223 |
return true; // no update |
|
224 |
||
225 |
case Qt::Key_Up: |
|
226 |
e->accept(); |
|
227 |
moveUp(e->modifiers() & Qt::ControlModifier); |
|
228 |
return true; |
|
229 |
||
230 |
case Qt::Key_Down: |
|
231 |
e->accept(); |
|
232 |
moveDown(e->modifiers() & Qt::ControlModifier); |
|
233 |
return true; |
|
234 |
||
235 |
case Qt::Key_PageUp: |
|
236 |
m_currentIndex = 0; |
|
237 |
break; |
|
238 |
||
239 |
case Qt::Key_PageDown: |
|
240 |
m_currentIndex = actions().count() - 1; |
|
241 |
break; |
|
242 |
||
243 |
case Qt::Key_Enter: |
|
244 |
case Qt::Key_Return: |
|
245 |
case Qt::Key_F2: |
|
246 |
e->accept(); |
|
247 |
enterEditMode(); |
|
248 |
return true; // no update |
|
249 |
||
250 |
case Qt::Key_Escape: |
|
251 |
e->ignore(); |
|
252 |
setFocus(); |
|
253 |
hide(); |
|
254 |
closeMenuChain(); |
|
255 |
return true; |
|
256 |
||
257 |
case Qt::Key_Alt: |
|
258 |
case Qt::Key_Shift: |
|
259 |
case Qt::Key_Control: |
|
260 |
e->ignore(); |
|
261 |
setFocus(); // FIXME: this is because some other widget get the focus when CTRL is pressed |
|
262 |
return true; // no update |
|
263 |
||
264 |
default: { |
|
265 |
QAction *action = currentAction(); |
|
266 |
if (!action || action->isSeparator() || action == m_addSeparator) { |
|
267 |
e->ignore(); |
|
268 |
return true; |
|
269 |
} else if (!e->text().isEmpty() && e->text().at(0).toLatin1() >= 32) { |
|
270 |
showLineEdit(); |
|
271 |
QApplication::sendEvent(m_editor, e); |
|
272 |
e->accept(); |
|
273 |
} else { |
|
274 |
e->ignore(); |
|
275 |
} |
|
276 |
} |
|
277 |
return true; |
|
278 |
} |
|
279 |
} else if (m_editor->hasFocus()) { // In edit mode |
|
280 |
switch (e->key()) { |
|
281 |
default: |
|
282 |
e->ignore(); |
|
283 |
return false; |
|
284 |
||
285 |
case Qt::Key_Enter: |
|
286 |
case Qt::Key_Return: |
|
287 |
if (!m_editor->text().isEmpty()) { |
|
288 |
leaveEditMode(ForceAccept); |
|
289 |
m_editor->hide(); |
|
290 |
setFocus(); |
|
291 |
moveDown(false); |
|
292 |
break; |
|
293 |
} |
|
294 |
// fall through |
|
295 |
||
296 |
case Qt::Key_Escape: |
|
297 |
m_editor->hide(); |
|
298 |
setFocus(); |
|
299 |
break; |
|
300 |
} |
|
301 |
} |
|
302 |
||
303 |
e->accept(); |
|
304 |
update(); |
|
305 |
||
306 |
return true; |
|
307 |
} |
|
308 |
||
309 |
static void sendMouseEventTo(QWidget *target, const QPoint &targetPoint, const QMouseEvent *event) |
|
310 |
{ |
|
311 |
QMouseEvent e(event->type(), targetPoint, event->globalPos(), event->button(), event->buttons(), event->modifiers()); |
|
312 |
QApplication::sendEvent(target, &e); |
|
313 |
} |
|
314 |
||
315 |
bool QDesignerMenu::handleMouseDoubleClickEvent(QWidget *, QMouseEvent *event) |
|
316 |
{ |
|
317 |
event->accept(); |
|
318 |
m_startPosition = QPoint(); |
|
319 |
||
320 |
if ((event->buttons() & Qt::LeftButton) != Qt::LeftButton) |
|
321 |
return true; |
|
322 |
||
323 |
if (!rect().contains(event->pos())) { |
|
324 |
// special case for menubar |
|
325 |
QWidget *target = QApplication::widgetAt(event->globalPos()); |
|
326 |
QMenuBar *mb = qobject_cast<QMenuBar*>(target); |
|
327 |
QDesignerMenu *menu = qobject_cast<QDesignerMenu*>(target); |
|
328 |
if (mb != 0 || menu != 0) { |
|
329 |
const QPoint pt = target->mapFromGlobal(event->globalPos()); |
|
330 |
QAction *action = mb == 0 ? menu->actionAt(pt) : mb->actionAt(pt); |
|
331 |
if (action) |
|
332 |
sendMouseEventTo(target, pt, event); |
|
333 |
} |
|
334 |
return true; |
|
335 |
} |
|
336 |
||
337 |
m_currentIndex = findAction(event->pos()); |
|
338 |
QAction *action = safeActionAt(m_currentIndex); |
|
339 |
||
340 |
QRect pm_rect; |
|
341 |
if (action->menu() || hasSubMenuPixmap(action)) { |
|
342 |
pm_rect = subMenuPixmapRect(action); |
|
343 |
extendClickableArea(&pm_rect, layoutDirection()); |
|
344 |
} |
|
345 |
||
346 |
if (!pm_rect.contains(event->pos()) && m_currentIndex != -1) |
|
347 |
enterEditMode(); |
|
348 |
||
349 |
return true; |
|
350 |
} |
|
351 |
||
352 |
bool QDesignerMenu::handleMousePressEvent(QWidget * /*widget*/, QMouseEvent *event) |
|
353 |
{ |
|
354 |
if (!rect().contains(event->pos())) { |
|
355 |
QWidget *clickedWidget = QApplication::widgetAt(event->globalPos()); |
|
356 |
if (QMenuBar *mb = qobject_cast<QMenuBar*>(clickedWidget)) { |
|
357 |
const QPoint pt = mb->mapFromGlobal(event->globalPos()); |
|
358 |
if (QAction *action = mb->actionAt(pt)) { |
|
359 |
QMenu * menu = action->menu(); |
|
360 |
if (menu == findRootMenu()) { |
|
361 |
// propagate the mouse press event (but don't close the popup) |
|
362 |
sendMouseEventTo(mb, pt, event); |
|
363 |
return true; |
|
364 |
} |
|
365 |
} |
|
366 |
} |
|
367 |
||
368 |
if (QDesignerMenu *m = qobject_cast<QDesignerMenu *>(clickedWidget)) { |
|
369 |
m->hideSubMenu(); |
|
370 |
sendMouseEventTo(m, m->mapFromGlobal(event->globalPos()), event); |
|
371 |
} else { |
|
372 |
QDesignerMenu *root = findRootMenu(); |
|
373 |
root->hide(); |
|
374 |
root->hideSubMenu(); |
|
375 |
} |
|
376 |
if (clickedWidget) { |
|
377 |
if (QWidget *focusProxy = clickedWidget->focusProxy()) |
|
378 |
clickedWidget = focusProxy; |
|
379 |
if (clickedWidget->focusPolicy() != Qt::NoFocus) |
|
380 |
clickedWidget->setFocus(Qt::OtherFocusReason); |
|
381 |
} |
|
382 |
return true; |
|
383 |
} |
|
384 |
||
385 |
m_showSubMenuTimer->stop(); |
|
386 |
m_startPosition = QPoint(); |
|
387 |
event->accept(); |
|
388 |
||
389 |
if (event->button() != Qt::LeftButton) |
|
390 |
return true; |
|
391 |
||
392 |
m_startPosition = mapFromGlobal(event->globalPos()); |
|
393 |
||
394 |
const int index = findAction(m_startPosition); |
|
395 |
||
396 |
QAction *action = safeActionAt(index); |
|
397 |
QRect pm_rect = subMenuPixmapRect(action); |
|
398 |
extendClickableArea(&pm_rect, layoutDirection()); |
|
399 |
||
400 |
const int old_index = m_currentIndex; |
|
401 |
m_currentIndex = index; |
|
402 |
if ((hasSubMenuPixmap(action) || action->menu() != 0) |
|
403 |
&& pm_rect.contains(m_startPosition)) { |
|
404 |
if (m_currentIndex == m_lastSubMenuIndex) { |
|
405 |
hideSubMenu(); |
|
406 |
} else |
|
407 |
slotShowSubMenuNow(); |
|
408 |
} else { |
|
409 |
if (index == old_index) { |
|
410 |
if (m_currentIndex == m_lastSubMenuIndex) |
|
411 |
hideSubMenu(); |
|
412 |
} else { |
|
413 |
hideSubMenu(); |
|
414 |
} |
|
415 |
} |
|
416 |
||
417 |
update(); |
|
418 |
if (index != old_index) |
|
419 |
selectCurrentAction(); |
|
420 |
||
421 |
return true; |
|
422 |
} |
|
423 |
||
424 |
bool QDesignerMenu::handleMouseReleaseEvent(QWidget *, QMouseEvent *event) |
|
425 |
{ |
|
426 |
event->accept(); |
|
427 |
m_startPosition = QPoint(); |
|
428 |
||
429 |
return true; |
|
430 |
} |
|
431 |
||
432 |
bool QDesignerMenu::handleMouseMoveEvent(QWidget *, QMouseEvent *event) |
|
433 |
{ |
|
434 |
if ((event->buttons() & Qt::LeftButton) != Qt::LeftButton) |
|
435 |
return true; |
|
436 |
||
437 |
if (!rect().contains(event->pos())) { |
|
438 |
||
439 |
if (QMenuBar *mb = qobject_cast<QMenuBar*>(QApplication::widgetAt(event->globalPos()))) { |
|
440 |
const QPoint pt = mb->mapFromGlobal(event->globalPos()); |
|
441 |
QAction *action = mb->actionAt(pt); |
|
442 |
if (action && action->menu() == findRootMenu()) { |
|
443 |
// propagate the mouse press event (but don't close the popup) |
|
444 |
sendMouseEventTo(mb, pt, event); |
|
445 |
return true; |
|
446 |
} |
|
447 |
// hide the popup Qt will replay the event |
|
448 |
slotDeactivateNow(); |
|
449 |
} |
|
450 |
return true; |
|
451 |
} |
|
452 |
||
453 |
if (m_startPosition.isNull()) |
|
454 |
return true; |
|
455 |
||
456 |
event->accept(); |
|
457 |
||
458 |
const QPoint pos = mapFromGlobal(event->globalPos()); |
|
459 |
||
460 |
if ((pos - m_startPosition).manhattanLength() < qApp->startDragDistance()) |
|
461 |
return true; |
|
462 |
||
463 |
startDrag(m_startPosition, event->modifiers()); |
|
464 |
m_startPosition = QPoint(); |
|
465 |
||
466 |
return true; |
|
467 |
} |
|
468 |
||
469 |
bool QDesignerMenu::handleContextMenuEvent(QWidget *, QContextMenuEvent *event) |
|
470 |
{ |
|
471 |
event->accept(); |
|
472 |
||
473 |
const int index = findAction(mapFromGlobal(event->globalPos())); |
|
474 |
QAction *action = safeActionAt(index); |
|
475 |
if (qobject_cast<SpecialMenuAction*>(action)) |
|
476 |
return true; |
|
477 |
||
478 |
QMenu menu; |
|
479 |
QVariant itemData; |
|
480 |
qVariantSetValue(itemData, action); |
|
481 |
||
482 |
QAction *addSeparatorAction = menu.addAction(tr("Insert separator")); |
|
483 |
addSeparatorAction->setData(itemData); |
|
484 |
||
485 |
QAction *removeAction = 0; |
|
486 |
if (action->isSeparator()) |
|
487 |
removeAction = menu.addAction(tr("Remove separator")); |
|
488 |
else |
|
489 |
removeAction = menu.addAction(tr("Remove action '%1'").arg(action->objectName())); |
|
490 |
removeAction->setData(itemData); |
|
491 |
||
492 |
connect(addSeparatorAction, SIGNAL(triggered(bool)), this, SLOT(slotAddSeparator())); |
|
493 |
connect(removeAction, SIGNAL(triggered(bool)), this, SLOT(slotRemoveSelectedAction())); |
|
494 |
menu.exec(event->globalPos()); |
|
495 |
||
496 |
return true; |
|
497 |
} |
|
498 |
||
499 |
void QDesignerMenu::slotAddSeparator() |
|
500 |
{ |
|
501 |
QAction *action = qobject_cast<QAction *>(sender()); |
|
502 |
if (!action) |
|
503 |
return; |
|
504 |
||
505 |
QAction *a = qvariant_cast<QAction*>(action->data()); |
|
506 |
Q_ASSERT(a != 0); |
|
507 |
||
508 |
const int pos = actions().indexOf(a); |
|
509 |
QAction *action_before = 0; |
|
510 |
if (pos != -1) |
|
511 |
action_before = safeActionAt(pos); |
|
512 |
||
513 |
QDesignerFormWindowInterface *fw = formWindow(); |
|
514 |
fw->beginCommand(tr("Add separator")); |
|
515 |
QAction *sep = createAction(QString(), true); |
|
516 |
||
517 |
InsertActionIntoCommand *cmd = new InsertActionIntoCommand(fw); |
|
518 |
cmd->init(this, sep, action_before); |
|
519 |
fw->commandHistory()->push(cmd); |
|
520 |
||
521 |
if (parentMenu()) { |
|
522 |
QAction *parent_action = parentMenu()->currentAction(); |
|
523 |
if (parent_action->menu() == 0) { |
|
524 |
CreateSubmenuCommand *cmd = new CreateSubmenuCommand(fw); |
|
525 |
cmd->init(parentMenu(), parentMenu()->currentAction()); |
|
526 |
fw->commandHistory()->push(cmd); |
|
527 |
} |
|
528 |
} |
|
529 |
||
530 |
fw->endCommand(); |
|
531 |
} |
|
532 |
||
533 |
void QDesignerMenu::slotRemoveSelectedAction() |
|
534 |
{ |
|
535 |
if (QAction *action = qobject_cast<QAction *>(sender())) |
|
536 |
if (QAction *a = qvariant_cast<QAction*>(action->data())) |
|
537 |
deleteAction(a); |
|
538 |
} |
|
539 |
||
540 |
void QDesignerMenu::deleteAction(QAction *a) |
|
541 |
{ |
|
542 |
const int pos = actions().indexOf(a); |
|
543 |
QAction *action_before = 0; |
|
544 |
if (pos != -1) |
|
545 |
action_before = safeActionAt(pos + 1); |
|
546 |
||
547 |
QDesignerFormWindowInterface *fw = formWindow(); |
|
548 |
RemoveActionFromCommand *cmd = new RemoveActionFromCommand(fw); |
|
549 |
cmd->init(this, a, action_before); |
|
550 |
fw->commandHistory()->push(cmd); |
|
551 |
} |
|
552 |
||
553 |
QRect QDesignerMenu::subMenuPixmapRect(QAction *action) const |
|
554 |
{ |
|
555 |
const QRect g = actionGeometry(action); |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
556 |
const int x = layoutDirection() == Qt::LeftToRight ? (g.right() - m_subMenuPixmap.width() - 2) : 2; |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
557 |
const int y = g.top() + (g.height() - m_subMenuPixmap.height())/2 + 1; |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
558 |
return QRect(x, y, m_subMenuPixmap.width(), m_subMenuPixmap.height()); |
0 | 559 |
} |
560 |
||
561 |
bool QDesignerMenu::hasSubMenuPixmap(QAction *action) const |
|
562 |
{ |
|
563 |
return action != 0 |
|
564 |
&& qobject_cast<SpecialMenuAction*>(action) == 0 |
|
565 |
&& !action->isSeparator() |
|
566 |
&& !action->menu() |
|
567 |
&& canCreateSubMenu(action); |
|
568 |
} |
|
569 |
||
570 |
void QDesignerMenu::showEvent ( QShowEvent * event ) |
|
571 |
{ |
|
572 |
selectCurrentAction(); |
|
573 |
QMenu::showEvent (event); |
|
574 |
} |
|
575 |
||
576 |
void QDesignerMenu::paintEvent(QPaintEvent *event) |
|
577 |
{ |
|
578 |
QMenu::paintEvent(event); |
|
579 |
||
580 |
QPainter p(this); |
|
581 |
||
582 |
QAction *current = currentAction(); |
|
583 |
||
584 |
foreach (QAction *a, actions()) { |
|
585 |
const QRect g = actionGeometry(a); |
|
586 |
||
587 |
if (qobject_cast<SpecialMenuAction*>(a)) { |
|
588 |
QLinearGradient lg(g.left(), g.top(), g.left(), g.bottom()); |
|
589 |
lg.setColorAt(0.0, Qt::transparent); |
|
590 |
lg.setColorAt(0.7, QColor(0, 0, 0, 32)); |
|
591 |
lg.setColorAt(1.0, Qt::transparent); |
|
592 |
||
593 |
p.fillRect(g, lg); |
|
594 |
} else if (hasSubMenuPixmap(a)) { |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
595 |
p.drawPixmap(subMenuPixmapRect(a).topLeft(), m_subMenuPixmap); |
0 | 596 |
} |
597 |
} |
|
598 |
||
599 |
if (!hasFocus() || !current || m_dragging) |
|
600 |
return; |
|
601 |
||
602 |
if (QDesignerMenu *menu = parentMenu()) { |
|
603 |
if (menu->dragging()) |
|
604 |
return; |
|
605 |
} |
|
606 |
||
607 |
if (QDesignerMenuBar *menubar = qobject_cast<QDesignerMenuBar*>(parentWidget())) { |
|
608 |
if (menubar->dragging()) |
|
609 |
return; |
|
610 |
} |
|
611 |
||
612 |
const QRect g = actionGeometry(current); |
|
613 |
drawSelection(&p, g.adjusted(1, 1, -3, -3)); |
|
614 |
} |
|
615 |
||
616 |
bool QDesignerMenu::dragging() const |
|
617 |
{ |
|
618 |
return m_dragging; |
|
619 |
} |
|
620 |
||
621 |
QDesignerMenu *QDesignerMenu::findRootMenu() const |
|
622 |
{ |
|
623 |
if (parentMenu()) |
|
624 |
return parentMenu()->findRootMenu(); |
|
625 |
||
626 |
return const_cast<QDesignerMenu*>(this); |
|
627 |
} |
|
628 |
||
629 |
QDesignerMenu *QDesignerMenu::findActivatedMenu() const |
|
630 |
{ |
|
631 |
QList<QDesignerMenu*> candidates; |
|
632 |
candidates.append(const_cast<QDesignerMenu*>(this)); |
|
633 |
candidates += qFindChildren<QDesignerMenu*>(this); |
|
634 |
||
635 |
foreach (QDesignerMenu *m, candidates) { |
|
636 |
if (m == qApp->activeWindow()) |
|
637 |
return m; |
|
638 |
} |
|
639 |
||
640 |
return 0; |
|
641 |
} |
|
642 |
||
643 |
bool QDesignerMenu::eventFilter(QObject *object, QEvent *event) |
|
644 |
{ |
|
645 |
if (object != this && object != m_editor) { |
|
646 |
return false; |
|
647 |
} |
|
648 |
||
649 |
if (!m_editor->isHidden() && object == m_editor && event->type() == QEvent::FocusOut) { |
|
650 |
leaveEditMode(Default); |
|
651 |
m_editor->hide(); |
|
652 |
update(); |
|
653 |
return false; |
|
654 |
} |
|
655 |
||
656 |
bool dispatch = true; |
|
657 |
||
658 |
switch (event->type()) { |
|
659 |
default: break; |
|
660 |
||
661 |
case QEvent::WindowDeactivate: |
|
662 |
deactivateMenu(); |
|
663 |
break; |
|
664 |
case QEvent::ContextMenu: |
|
665 |
case QEvent::MouseButtonPress: |
|
666 |
case QEvent::MouseButtonRelease: |
|
667 |
case QEvent::MouseButtonDblClick: |
|
668 |
||
669 |
while (QApplication::activePopupWidget() && !qobject_cast<QDesignerMenu*>(QApplication::activePopupWidget())) { |
|
670 |
QApplication::activePopupWidget()->close(); |
|
671 |
} |
|
672 |
||
673 |
// fall through |
|
674 |
case QEvent::KeyPress: |
|
675 |
case QEvent::KeyRelease: |
|
676 |
case QEvent::MouseMove: |
|
677 |
dispatch = (object != m_editor); |
|
678 |
// no break |
|
679 |
||
680 |
case QEvent::Enter: |
|
681 |
case QEvent::Leave: |
|
682 |
case QEvent::FocusIn: |
|
683 |
case QEvent::FocusOut: |
|
684 |
if (dispatch) |
|
685 |
if (QWidget *widget = qobject_cast<QWidget*>(object)) |
|
686 |
if (widget == this || isAncestorOf(widget)) |
|
687 |
return handleEvent(widget, event); |
|
688 |
break; |
|
689 |
} |
|
690 |
||
691 |
return false; |
|
692 |
}; |
|
693 |
||
694 |
int QDesignerMenu::findAction(const QPoint &pos) const |
|
695 |
{ |
|
696 |
const int index = actionIndexAt(this, pos, Qt::Vertical); |
|
697 |
if (index == -1) |
|
698 |
return realActionCount(); |
|
699 |
||
700 |
return index; |
|
701 |
} |
|
702 |
||
703 |
void QDesignerMenu::adjustIndicator(const QPoint &pos) |
|
704 |
{ |
|
705 |
if (QDesignerActionProviderExtension *a = actionProvider()) { |
|
706 |
a->adjustIndicator(pos); |
|
707 |
} |
|
708 |
} |
|
709 |
||
710 |
QDesignerMenu::ActionDragCheck QDesignerMenu::checkAction(QAction *action) const |
|
711 |
{ |
|
712 |
if (!action || (action->menu() && action->menu()->parentWidget() != const_cast<QDesignerMenu*>(this))) |
|
713 |
return NoActionDrag; // menu action!! nothing to do |
|
714 |
||
715 |
if (!Utils::isObjectAncestorOf(formWindow()->mainContainer(), action)) |
|
716 |
return NoActionDrag; // the action belongs to another form window |
|
717 |
||
718 |
if (actions().contains(action)) |
|
719 |
return ActionDragOnSubMenu; // we already have the action in the menu |
|
720 |
||
721 |
return AcceptActionDrag; |
|
722 |
} |
|
723 |
||
724 |
void QDesignerMenu::dragEnterEvent(QDragEnterEvent *event) |
|
725 |
{ |
|
726 |
const ActionRepositoryMimeData *d = qobject_cast<const ActionRepositoryMimeData*>(event->mimeData()); |
|
727 |
if (!d || d->actionList().empty()) { |
|
728 |
event->ignore(); |
|
729 |
return; |
|
730 |
} |
|
731 |
||
732 |
QAction *action = d->actionList().first(); |
|
733 |
||
734 |
switch (checkAction(action)) { |
|
735 |
case NoActionDrag: |
|
736 |
event->ignore(); |
|
737 |
break; |
|
738 |
case ActionDragOnSubMenu: |
|
739 |
d->accept(event); |
|
740 |
m_dragging = true; |
|
741 |
break; |
|
742 |
case AcceptActionDrag: |
|
743 |
d->accept(event); |
|
744 |
m_dragging = true; |
|
745 |
adjustIndicator(event->pos()); |
|
746 |
break; |
|
747 |
} |
|
748 |
} |
|
749 |
||
750 |
void QDesignerMenu::dragMoveEvent(QDragMoveEvent *event) |
|
751 |
{ |
|
752 |
if (actionGeometry(m_addSeparator).contains(event->pos())) { |
|
753 |
event->ignore(); |
|
754 |
adjustIndicator(QPoint(-1, -1)); |
|
755 |
return; |
|
756 |
} |
|
757 |
||
758 |
const ActionRepositoryMimeData *d = qobject_cast<const ActionRepositoryMimeData*>(event->mimeData()); |
|
759 |
if (!d || d->actionList().empty()) { |
|
760 |
event->ignore(); |
|
761 |
return; |
|
762 |
} |
|
763 |
||
764 |
QAction *action = d->actionList().first(); |
|
765 |
const ActionDragCheck dc = checkAction(action); |
|
766 |
switch (dc) { |
|
767 |
case NoActionDrag: |
|
768 |
event->ignore(); |
|
769 |
break; |
|
770 |
case ActionDragOnSubMenu: |
|
771 |
case AcceptActionDrag: { // Do not pop up submenu of action being dragged |
|
772 |
const int newIndex = findAction(event->pos()); |
|
773 |
if (safeActionAt(newIndex) != action) { |
|
774 |
m_currentIndex = newIndex; |
|
775 |
if (m_lastSubMenuIndex != m_currentIndex) |
|
776 |
m_showSubMenuTimer->start(300); |
|
777 |
} |
|
778 |
if (dc == AcceptActionDrag) { |
|
779 |
adjustIndicator(event->pos()); |
|
780 |
d->accept(event); |
|
781 |
} else { |
|
782 |
event->ignore(); |
|
783 |
} |
|
784 |
} |
|
785 |
break; |
|
786 |
} |
|
787 |
} |
|
788 |
||
789 |
void QDesignerMenu::dragLeaveEvent(QDragLeaveEvent *) |
|
790 |
{ |
|
791 |
m_dragging = false; |
|
792 |
adjustIndicator(QPoint(-1, -1)); |
|
793 |
m_showSubMenuTimer->stop(); |
|
794 |
} |
|
795 |
||
796 |
void QDesignerMenu::dropEvent(QDropEvent *event) |
|
797 |
{ |
|
798 |
m_showSubMenuTimer->stop(); |
|
799 |
hideSubMenu(); |
|
800 |
m_dragging = false; |
|
801 |
||
802 |
QDesignerFormWindowInterface *fw = formWindow(); |
|
803 |
const ActionRepositoryMimeData *d = qobject_cast<const ActionRepositoryMimeData*>(event->mimeData()); |
|
804 |
if (!d || d->actionList().empty()) { |
|
805 |
event->ignore(); |
|
806 |
return; |
|
807 |
} |
|
808 |
QAction *action = d->actionList().first(); |
|
809 |
if (action && checkAction(action) == AcceptActionDrag) { |
|
810 |
event->acceptProposedAction(); |
|
811 |
int index = findAction(event->pos()); |
|
812 |
index = qMin(index, actions().count() - 1); |
|
813 |
||
814 |
fw->beginCommand(tr("Insert action")); |
|
815 |
InsertActionIntoCommand *cmd = new InsertActionIntoCommand(fw); |
|
816 |
cmd->init(this, action, safeActionAt(index)); |
|
817 |
fw->commandHistory()->push(cmd); |
|
818 |
||
819 |
m_currentIndex = index; |
|
820 |
||
821 |
if (parentMenu()) { |
|
822 |
QAction *parent_action = parentMenu()->currentAction(); |
|
823 |
if (parent_action->menu() == 0) { |
|
824 |
CreateSubmenuCommand *cmd = new CreateSubmenuCommand(fw); |
|
825 |
cmd->init(parentMenu(), parentMenu()->currentAction(), action); |
|
826 |
fw->commandHistory()->push(cmd); |
|
827 |
} |
|
828 |
} |
|
829 |
update(); |
|
830 |
fw->endCommand(); |
|
831 |
} else { |
|
832 |
event->ignore(); |
|
833 |
} |
|
834 |
adjustIndicator(QPoint(-1, -1)); |
|
835 |
} |
|
836 |
||
837 |
void QDesignerMenu::actionEvent(QActionEvent *event) |
|
838 |
{ |
|
839 |
QMenu::actionEvent(event); |
|
840 |
m_adjustSizeTimer->start(0); |
|
841 |
} |
|
842 |
||
843 |
QDesignerFormWindowInterface *QDesignerMenu::formWindow() const |
|
844 |
{ |
|
845 |
if (parentMenu()) |
|
846 |
return parentMenu()->formWindow(); |
|
847 |
||
848 |
return QDesignerFormWindowInterface::findFormWindow(parentWidget()); |
|
849 |
} |
|
850 |
||
851 |
QDesignerActionProviderExtension *QDesignerMenu::actionProvider() |
|
852 |
{ |
|
853 |
if (QDesignerFormWindowInterface *fw = formWindow()) { |
|
854 |
QDesignerFormEditorInterface *core = fw->core(); |
|
855 |
return qt_extension<QDesignerActionProviderExtension*>(core->extensionManager(), this); |
|
856 |
} |
|
857 |
||
858 |
return 0; |
|
859 |
} |
|
860 |
||
861 |
void QDesignerMenu::closeMenuChain() |
|
862 |
{ |
|
863 |
m_showSubMenuTimer->stop(); |
|
864 |
||
865 |
QWidget *w = this; |
|
866 |
while (w && qobject_cast<QMenu*>(w)) |
|
867 |
w = w->parentWidget(); |
|
868 |
||
869 |
if (w) { |
|
870 |
foreach (QMenu *subMenu, qFindChildren<QMenu*>(w)) { |
|
871 |
subMenu->hide(); |
|
872 |
} |
|
873 |
} |
|
874 |
||
875 |
m_lastSubMenuIndex = -1; |
|
876 |
} |
|
877 |
||
878 |
// Close submenu using the left/right keys according to layoutDirection(). |
|
879 |
// Return false to indicate the event must be propagated to the menu bar. |
|
880 |
bool QDesignerMenu::hideSubMenuOnCursorKey() |
|
881 |
{ |
|
882 |
if (parentMenu()) { |
|
883 |
hide(); |
|
884 |
return true; |
|
885 |
} |
|
886 |
closeMenuChain(); |
|
887 |
update(); |
|
888 |
if (parentMenuBar()) |
|
889 |
return false; |
|
890 |
return true; |
|
891 |
} |
|
892 |
||
893 |
// Open a submenu using the left/right keys according to layoutDirection(). |
|
894 |
// Return false to indicate the event must be propagated to the menu bar. |
|
895 |
bool QDesignerMenu::showSubMenuOnCursorKey() |
|
896 |
{ |
|
897 |
const QAction *action = currentAction(); |
|
898 |
||
899 |
if (qobject_cast<const SpecialMenuAction*>(action) || action->isSeparator()) { |
|
900 |
closeMenuChain(); |
|
901 |
if (parentMenuBar()) |
|
902 |
return false; |
|
903 |
return true; |
|
904 |
} |
|
905 |
m_lastSubMenuIndex = -1; // force a refresh |
|
906 |
slotShowSubMenuNow(); |
|
907 |
return true; |
|
908 |
} |
|
909 |
||
910 |
void QDesignerMenu::moveLeft() |
|
911 |
{ |
|
912 |
const bool handled = layoutDirection() == Qt::LeftToRight ? |
|
913 |
hideSubMenuOnCursorKey() : showSubMenuOnCursorKey(); |
|
914 |
if (!handled) |
|
915 |
parentMenuBar()->moveLeft(); |
|
916 |
} |
|
917 |
||
918 |
void QDesignerMenu::moveRight() |
|
919 |
{ |
|
920 |
const bool handled = layoutDirection() == Qt::LeftToRight ? |
|
921 |
showSubMenuOnCursorKey() : hideSubMenuOnCursorKey(); |
|
922 |
if (!handled) |
|
923 |
parentMenuBar()->moveRight(); |
|
924 |
} |
|
925 |
||
926 |
void QDesignerMenu::moveUp(bool ctrl) |
|
927 |
{ |
|
928 |
if (m_currentIndex == 0) { |
|
929 |
hide(); |
|
930 |
return; |
|
931 |
} |
|
932 |
||
933 |
if (ctrl) |
|
934 |
(void) swap(m_currentIndex, m_currentIndex - 1); |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
935 |
--m_currentIndex; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
936 |
m_currentIndex = qMax(0, m_currentIndex); |
0 | 937 |
// Always re-select, swapping destroys order |
938 |
update(); |
|
939 |
selectCurrentAction(); |
|
940 |
} |
|
941 |
||
942 |
void QDesignerMenu::moveDown(bool ctrl) |
|
943 |
{ |
|
944 |
if (m_currentIndex == actions().count() - 1) { |
|
945 |
return; |
|
946 |
} |
|
947 |
||
948 |
if (ctrl) |
|
949 |
(void) swap(m_currentIndex + 1, m_currentIndex); |
|
950 |
||
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
951 |
++m_currentIndex; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
952 |
m_currentIndex = qMin(actions().count() - 1, m_currentIndex); |
0 | 953 |
update(); |
954 |
if (!ctrl) |
|
955 |
selectCurrentAction(); |
|
956 |
} |
|
957 |
||
958 |
QAction *QDesignerMenu::currentAction() const |
|
959 |
{ |
|
960 |
if (m_currentIndex < 0 || m_currentIndex >= actions().count()) |
|
961 |
return 0; |
|
962 |
||
963 |
return safeActionAt(m_currentIndex); |
|
964 |
} |
|
965 |
||
966 |
int QDesignerMenu::realActionCount() const |
|
967 |
{ |
|
968 |
return actions().count() - 2; // 2 fake actions |
|
969 |
} |
|
970 |
||
971 |
void QDesignerMenu::selectCurrentAction() |
|
972 |
{ |
|
973 |
QAction *action = currentAction(); |
|
974 |
if (!action || action == m_addSeparator || action == m_addItem) |
|
975 |
return; |
|
976 |
||
977 |
QDesignerObjectInspector *oi = 0; |
|
978 |
if (QDesignerFormWindowInterface *fw = formWindow()) |
|
979 |
oi = qobject_cast<QDesignerObjectInspector *>(fw->core()->objectInspector()); |
|
980 |
||
981 |
if (!oi) |
|
982 |
return; |
|
983 |
||
984 |
oi->clearSelection(); |
|
985 |
if (QMenu *menu = action->menu()) |
|
986 |
oi->selectObject(menu); |
|
987 |
else |
|
988 |
oi->selectObject(action); |
|
989 |
} |
|
990 |
||
991 |
void QDesignerMenu::createRealMenuAction(QAction *action) |
|
992 |
{ |
|
993 |
if (action->menu()) |
|
994 |
return; // nothing to do |
|
995 |
||
996 |
QDesignerFormWindowInterface *fw = formWindow(); |
|
997 |
QDesignerFormEditorInterface *core = formWindow()->core(); |
|
998 |
||
999 |
QDesignerMenu *menu = findOrCreateSubMenu(action); |
|
1000 |
m_subMenus.remove(action); |
|
1001 |
||
1002 |
action->setMenu(menu); |
|
1003 |
menu->setTitle(action->text()); |
|
1004 |
||
1005 |
Q_ASSERT(fw); |
|
1006 |
||
1007 |
core->widgetFactory()->initialize(menu); |
|
1008 |
||
1009 |
const QString niceObjectName = ActionEditor::actionTextToName(menu->title(), QLatin1String("menu")); |
|
1010 |
menu->setObjectName(niceObjectName); |
|
1011 |
||
1012 |
core->metaDataBase()->add(menu); |
|
1013 |
fw->ensureUniqueObjectName(menu); |
|
1014 |
||
1015 |
QAction *menuAction = menu->menuAction(); |
|
1016 |
core->metaDataBase()->add(menuAction); |
|
1017 |
} |
|
1018 |
||
1019 |
void QDesignerMenu::removeRealMenu(QAction *action) |
|
1020 |
{ |
|
1021 |
QDesignerMenu *menu = qobject_cast<QDesignerMenu*>(action->menu()); |
|
1022 |
if (menu == 0) |
|
1023 |
return; |
|
1024 |
action->setMenu(0); |
|
1025 |
m_subMenus.insert(action, menu); |
|
1026 |
QDesignerFormEditorInterface *core = formWindow()->core(); |
|
1027 |
core->metaDataBase()->remove(menu); |
|
1028 |
} |
|
1029 |
||
1030 |
QDesignerMenu *QDesignerMenu::findOrCreateSubMenu(QAction *action) |
|
1031 |
{ |
|
1032 |
if (action->menu()) |
|
1033 |
return qobject_cast<QDesignerMenu*>(action->menu()); |
|
1034 |
||
1035 |
QDesignerMenu *menu = m_subMenus.value(action); |
|
1036 |
if (!menu) { |
|
1037 |
menu = new QDesignerMenu(this); |
|
1038 |
m_subMenus.insert(action, menu); |
|
1039 |
} |
|
1040 |
||
1041 |
return menu; |
|
1042 |
} |
|
1043 |
||
1044 |
bool QDesignerMenu::canCreateSubMenu(QAction *action) const // ### improve it's a bit too slow |
|
1045 |
{ |
|
1046 |
foreach (const QWidget *aw, action->associatedWidgets()) |
|
1047 |
if (aw != this) { |
|
1048 |
if (const QMenu *m = qobject_cast<const QMenu *>(aw)) { |
|
1049 |
if (m->actions().contains(action)) |
|
1050 |
return false; // sorry |
|
1051 |
} else { |
|
1052 |
if (const QToolBar *tb = qobject_cast<const QToolBar *>(aw)) |
|
1053 |
if (tb->actions().contains(action)) |
|
1054 |
return false; // sorry |
|
1055 |
} |
|
1056 |
} |
|
1057 |
return true; |
|
1058 |
} |
|
1059 |
||
1060 |
void QDesignerMenu::slotShowSubMenuNow() |
|
1061 |
{ |
|
1062 |
m_showSubMenuTimer->stop(); |
|
1063 |
||
1064 |
if (m_lastSubMenuIndex == m_currentIndex) |
|
1065 |
return; |
|
1066 |
||
1067 |
if (m_lastSubMenuIndex != -1) |
|
1068 |
hideSubMenu(); |
|
1069 |
||
1070 |
if (m_currentIndex >= realActionCount()) |
|
1071 |
return; |
|
1072 |
||
1073 |
QAction *action = currentAction(); |
|
1074 |
||
1075 |
if (action->isSeparator() || !canCreateSubMenu(action)) |
|
1076 |
return; |
|
1077 |
||
1078 |
if (QMenu *menu = findOrCreateSubMenu(action)) { |
|
1079 |
if (!menu->isVisible()) { |
|
1080 |
if ((menu->windowFlags() & Qt::Popup) != Qt::Popup) |
|
1081 |
menu->setWindowFlags(Qt::Popup); |
|
1082 |
const QRect g = actionGeometry(action); |
|
1083 |
if (layoutDirection() == Qt::LeftToRight) { |
|
1084 |
menu->move(mapToGlobal(g.topRight())); |
|
1085 |
} else { |
|
1086 |
// The position is not initially correct due to the unknown width, |
|
1087 |
// causing it to overlap a bit the first time it is invoked. |
|
1088 |
const QSize menuSize = menu->size(); |
|
1089 |
QPoint point = g.topLeft() - QPoint(menu->width() + 10, 0); |
|
1090 |
menu->move(mapToGlobal(point)); |
|
1091 |
} |
|
1092 |
menu->show(); |
|
1093 |
menu->setFocus(); |
|
1094 |
} else { |
|
1095 |
menu->raise(); |
|
1096 |
} |
|
1097 |
menu->setFocus(); |
|
1098 |
||
1099 |
m_lastSubMenuIndex = m_currentIndex; |
|
1100 |
} |
|
1101 |
} |
|
1102 |
||
1103 |
void QDesignerMenu::showSubMenu(QAction *action) |
|
1104 |
{ |
|
1105 |
m_showSubMenuTimer->stop(); |
|
1106 |
||
1107 |
if (m_editor->isVisible() || !action || qobject_cast<SpecialMenuAction*>(action) |
|
1108 |
|| action->isSeparator() || !isVisible()) |
|
1109 |
return; |
|
1110 |
||
1111 |
m_showSubMenuTimer->start(300); |
|
1112 |
} |
|
1113 |
||
1114 |
QDesignerMenu *QDesignerMenu::parentMenu() const |
|
1115 |
{ |
|
1116 |
return qobject_cast<QDesignerMenu*>(parentWidget()); |
|
1117 |
} |
|
1118 |
||
1119 |
QDesignerMenuBar *QDesignerMenu::parentMenuBar() const |
|
1120 |
{ |
|
1121 |
if (QDesignerMenuBar *mb = qobject_cast<QDesignerMenuBar*>(parentWidget())) { |
|
1122 |
return mb; |
|
1123 |
} else if (QDesignerMenu *m = parentMenu()) { |
|
1124 |
return m->parentMenuBar(); |
|
1125 |
} |
|
1126 |
||
1127 |
return 0; |
|
1128 |
} |
|
1129 |
||
1130 |
void QDesignerMenu::setVisible(bool visible) |
|
1131 |
{ |
|
1132 |
if (visible) |
|
1133 |
m_currentIndex = 0; |
|
1134 |
else |
|
1135 |
m_lastSubMenuIndex = -1; |
|
1136 |
||
1137 |
QMenu::setVisible(visible); |
|
1138 |
||
1139 |
} |
|
1140 |
||
1141 |
void QDesignerMenu::adjustSpecialActions() |
|
1142 |
{ |
|
1143 |
removeAction(m_addItem); |
|
1144 |
removeAction(m_addSeparator); |
|
1145 |
addAction(m_addItem); |
|
1146 |
addAction(m_addSeparator); |
|
1147 |
} |
|
1148 |
||
1149 |
bool QDesignerMenu::interactive(bool i) |
|
1150 |
{ |
|
1151 |
const bool old = m_interactive; |
|
1152 |
m_interactive = i; |
|
1153 |
return old; |
|
1154 |
} |
|
1155 |
||
1156 |
void QDesignerMenu::enterEditMode() |
|
1157 |
{ |
|
1158 |
if (m_currentIndex >= 0 && m_currentIndex <= realActionCount()) { |
|
1159 |
showLineEdit(); |
|
1160 |
} else { |
|
1161 |
hideSubMenu(); |
|
1162 |
QDesignerFormWindowInterface *fw = formWindow(); |
|
1163 |
fw->beginCommand(tr("Add separator")); |
|
1164 |
QAction *sep = createAction(QString(), true); |
|
1165 |
||
1166 |
InsertActionIntoCommand *cmd = new InsertActionIntoCommand(fw); |
|
1167 |
cmd->init(this, sep, safeActionAt(realActionCount())); |
|
1168 |
fw->commandHistory()->push(cmd); |
|
1169 |
||
1170 |
if (parentMenu()) { |
|
1171 |
QAction *parent_action = parentMenu()->currentAction(); |
|
1172 |
if (parent_action->menu() == 0) { |
|
1173 |
CreateSubmenuCommand *cmd = new CreateSubmenuCommand(fw); |
|
1174 |
cmd->init(parentMenu(), parentMenu()->currentAction()); |
|
1175 |
fw->commandHistory()->push(cmd); |
|
1176 |
} |
|
1177 |
} |
|
1178 |
||
1179 |
fw->endCommand(); |
|
1180 |
||
1181 |
m_currentIndex = actions().indexOf(m_addItem); |
|
1182 |
update(); |
|
1183 |
} |
|
1184 |
} |
|
1185 |
||
1186 |
void QDesignerMenu::leaveEditMode(LeaveEditMode mode) |
|
1187 |
{ |
|
1188 |
if (mode == Default) |
|
1189 |
return; |
|
1190 |
||
1191 |
QAction *action = 0; |
|
1192 |
||
1193 |
QDesignerFormWindowInterface *fw = formWindow(); |
|
1194 |
if (m_currentIndex < realActionCount()) { |
|
1195 |
action = safeActionAt(m_currentIndex); |
|
1196 |
fw->beginCommand(QApplication::translate("Command", "Set action text")); |
|
1197 |
} else { |
|
1198 |
Q_ASSERT(fw != 0); |
|
1199 |
fw->beginCommand(QApplication::translate("Command", "Insert action")); |
|
1200 |
action = createAction(ActionEditor::actionTextToName(m_editor->text())); |
|
1201 |
InsertActionIntoCommand *cmd = new InsertActionIntoCommand(fw); |
|
1202 |
cmd->init(this, action, currentAction()); |
|
1203 |
fw->commandHistory()->push(cmd); |
|
1204 |
} |
|
1205 |
||
1206 |
SetPropertyCommand *cmd = new SetPropertyCommand(fw); |
|
1207 |
cmd->init(action, QLatin1String("text"), m_editor->text()); |
|
1208 |
fw->commandHistory()->push(cmd); |
|
1209 |
||
1210 |
if (parentMenu()) { |
|
1211 |
QAction *parent_action = parentMenu()->currentAction(); |
|
1212 |
if (parent_action->menu() == 0) { |
|
1213 |
CreateSubmenuCommand *cmd = new CreateSubmenuCommand(fw); |
|
1214 |
cmd->init(parentMenu(), parentMenu()->currentAction(), action); |
|
1215 |
fw->commandHistory()->push(cmd); |
|
1216 |
} |
|
1217 |
} |
|
1218 |
||
1219 |
update(); |
|
1220 |
fw->endCommand(); |
|
1221 |
} |
|
1222 |
||
1223 |
QAction *QDesignerMenu::safeMenuAction(QDesignerMenu *menu) const |
|
1224 |
{ |
|
1225 |
QAction *action = menu->menuAction(); |
|
1226 |
||
1227 |
if (!action) |
|
1228 |
action = m_subMenus.key(menu); |
|
1229 |
||
1230 |
return action; |
|
1231 |
} |
|
1232 |
||
1233 |
void QDesignerMenu::showLineEdit() |
|
1234 |
{ |
|
1235 |
m_showSubMenuTimer->stop(); |
|
1236 |
||
1237 |
QAction *action = 0; |
|
1238 |
||
1239 |
if (m_currentIndex < realActionCount()) |
|
1240 |
action = safeActionAt(m_currentIndex); |
|
1241 |
else |
|
1242 |
action = m_addItem; |
|
1243 |
||
1244 |
if (action->isSeparator()) |
|
1245 |
return; |
|
1246 |
||
1247 |
hideSubMenu(); |
|
1248 |
||
1249 |
// open edit field for item name |
|
1250 |
setFocus(); |
|
1251 |
||
1252 |
const QString text = action != m_addItem ? action->text() : QString(); |
|
1253 |
m_editor->setText(text); |
|
1254 |
m_editor->selectAll(); |
|
1255 |
m_editor->setGeometry(actionGeometry(action).adjusted(1, 1, -2, -2)); |
|
1256 |
m_editor->show(); |
|
1257 |
m_editor->setFocus(); |
|
1258 |
} |
|
1259 |
||
1260 |
QAction *QDesignerMenu::createAction(const QString &objectName, bool separator) |
|
1261 |
{ |
|
1262 |
QDesignerFormWindowInterface *fw = formWindow(); |
|
1263 |
Q_ASSERT(fw); |
|
1264 |
return ToolBarEventFilter::createAction(fw, objectName, separator); |
|
1265 |
} |
|
1266 |
||
1267 |
// ### share with QDesignerMenu::swap |
|
1268 |
bool QDesignerMenu::swap(int a, int b) |
|
1269 |
{ |
|
1270 |
const int left = qMin(a, b); |
|
1271 |
int right = qMax(a, b); |
|
1272 |
||
1273 |
QAction *action_a = safeActionAt(left); |
|
1274 |
QAction *action_b = safeActionAt(right); |
|
1275 |
||
1276 |
if (action_a == action_b |
|
1277 |
|| !action_a |
|
1278 |
|| !action_b |
|
1279 |
|| qobject_cast<SpecialMenuAction*>(action_a) |
|
1280 |
|| qobject_cast<SpecialMenuAction*>(action_b)) |
|
1281 |
return false; // nothing to do |
|
1282 |
||
1283 |
right = qMin(right, realActionCount()); |
|
1284 |
if (right < 0) |
|
1285 |
return false; // nothing to do |
|
1286 |
||
1287 |
QDesignerFormWindowInterface *fw = formWindow(); |
|
1288 |
fw->beginCommand(QApplication::translate("Command", "Move action")); |
|
1289 |
||
1290 |
QAction *action_b_before = safeActionAt(right + 1); |
|
1291 |
||
1292 |
RemoveActionFromCommand *cmd1 = new RemoveActionFromCommand(fw); |
|
1293 |
cmd1->init(this, action_b, action_b_before, false); |
|
1294 |
fw->commandHistory()->push(cmd1); |
|
1295 |
||
1296 |
QAction *action_a_before = safeActionAt(left + 1); |
|
1297 |
||
1298 |
InsertActionIntoCommand *cmd2 = new InsertActionIntoCommand(fw); |
|
1299 |
cmd2->init(this, action_b, action_a_before, false); |
|
1300 |
fw->commandHistory()->push(cmd2); |
|
1301 |
||
1302 |
RemoveActionFromCommand *cmd3 = new RemoveActionFromCommand(fw); |
|
1303 |
cmd3->init(this, action_a, action_b, false); |
|
1304 |
fw->commandHistory()->push(cmd3); |
|
1305 |
||
1306 |
InsertActionIntoCommand *cmd4 = new InsertActionIntoCommand(fw); |
|
1307 |
cmd4->init(this, action_a, action_b_before, true); |
|
1308 |
fw->commandHistory()->push(cmd4); |
|
1309 |
||
1310 |
fw->endCommand(); |
|
1311 |
||
1312 |
return true; |
|
1313 |
} |
|
1314 |
||
1315 |
QAction *QDesignerMenu::safeActionAt(int index) const |
|
1316 |
{ |
|
1317 |
if (index < 0 || index >= actions().count()) |
|
1318 |
return 0; |
|
1319 |
||
1320 |
return actions().at(index); |
|
1321 |
} |
|
1322 |
||
1323 |
void QDesignerMenu::hideSubMenu() |
|
1324 |
{ |
|
1325 |
m_lastSubMenuIndex = -1; |
|
1326 |
foreach (QMenu *subMenu, qFindChildren<QMenu*>(this)) { |
|
1327 |
subMenu->hide(); |
|
1328 |
} |
|
1329 |
} |
|
1330 |
||
1331 |
void QDesignerMenu::deleteAction() |
|
1332 |
{ |
|
1333 |
QAction *action = currentAction(); |
|
1334 |
const int pos = actions().indexOf(action); |
|
1335 |
QAction *action_before = 0; |
|
1336 |
if (pos != -1) |
|
1337 |
action_before = safeActionAt(pos + 1); |
|
1338 |
||
1339 |
QDesignerFormWindowInterface *fw = formWindow(); |
|
1340 |
RemoveActionFromCommand *cmd = new RemoveActionFromCommand(fw); |
|
1341 |
cmd->init(this, action, action_before); |
|
1342 |
fw->commandHistory()->push(cmd); |
|
1343 |
||
1344 |
update(); |
|
1345 |
} |
|
1346 |
||
1347 |
void QDesignerMenu::deactivateMenu() |
|
1348 |
{ |
|
1349 |
m_deactivateWindowTimer->start(10); |
|
1350 |
} |
|
1351 |
||
1352 |
void QDesignerMenu::slotDeactivateNow() |
|
1353 |
{ |
|
1354 |
m_deactivateWindowTimer->stop(); |
|
1355 |
||
1356 |
if (m_dragging) |
|
1357 |
return; |
|
1358 |
||
1359 |
QDesignerMenu *root = findRootMenu(); |
|
1360 |
||
1361 |
if (! root->findActivatedMenu()) { |
|
1362 |
root->hide(); |
|
1363 |
root->hideSubMenu(); |
|
1364 |
} |
|
1365 |
} |
|
1366 |
||
1367 |
void QDesignerMenu::drawSelection(QPainter *p, const QRect &r) |
|
1368 |
{ |
|
1369 |
p->save(); |
|
1370 |
||
1371 |
QColor c = Qt::blue; |
|
1372 |
p->setPen(QPen(c, 1)); |
|
1373 |
c.setAlpha(32); |
|
1374 |
p->setBrush(c); |
|
1375 |
p->drawRect(r); |
|
1376 |
||
1377 |
p->restore(); |
|
1378 |
} |
|
1379 |
||
1380 |
void QDesignerMenu::keyPressEvent(QKeyEvent *event) |
|
1381 |
{ |
|
1382 |
event->ignore(); |
|
1383 |
} |
|
1384 |
||
1385 |
void QDesignerMenu::keyReleaseEvent(QKeyEvent *event) |
|
1386 |
{ |
|
1387 |
event->ignore(); |
|
1388 |
} |
|
1389 |
||
1390 |
QT_END_NAMESPACE |