0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the QtGui module 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 <qmenubar.h>
|
|
43 |
|
|
44 |
#include <qstyle.h>
|
|
45 |
#include <qlayout.h>
|
|
46 |
#include <qapplication.h>
|
|
47 |
#include <qdesktopwidget.h>
|
|
48 |
#ifndef QT_NO_ACCESSIBILITY
|
|
49 |
# include <qaccessible.h>
|
|
50 |
#endif
|
|
51 |
#include <qpainter.h>
|
|
52 |
#include <qstylepainter.h>
|
|
53 |
#include <qevent.h>
|
|
54 |
#include <qmainwindow.h>
|
|
55 |
#include <qtoolbar.h>
|
|
56 |
#include <qtoolbutton.h>
|
|
57 |
#include <qwhatsthis.h>
|
|
58 |
|
|
59 |
#ifndef QT_NO_MENUBAR
|
|
60 |
|
|
61 |
#ifdef QT3_SUPPORT
|
|
62 |
#include <private/qaction_p.h>
|
|
63 |
#include <qmenudata.h>
|
|
64 |
#endif
|
|
65 |
|
|
66 |
#include "qmenu_p.h"
|
|
67 |
#include "qmenubar_p.h"
|
|
68 |
#include "qdebug.h"
|
|
69 |
|
|
70 |
#ifdef Q_WS_WINCE
|
|
71 |
extern bool qt_wince_is_mobile(); //defined in qguifunctions_wce.cpp
|
|
72 |
#endif
|
|
73 |
|
|
74 |
QT_BEGIN_NAMESPACE
|
|
75 |
|
|
76 |
class QMenuBarExtension : public QToolButton
|
|
77 |
{
|
|
78 |
public:
|
|
79 |
explicit QMenuBarExtension(QWidget *parent);
|
|
80 |
|
|
81 |
QSize sizeHint() const;
|
|
82 |
void paintEvent(QPaintEvent *);
|
|
83 |
};
|
|
84 |
|
|
85 |
QMenuBarExtension::QMenuBarExtension(QWidget *parent)
|
|
86 |
: QToolButton(parent)
|
|
87 |
{
|
|
88 |
setObjectName(QLatin1String("qt_menubar_ext_button"));
|
|
89 |
setAutoRaise(true);
|
|
90 |
#ifndef QT_NO_MENU
|
|
91 |
setPopupMode(QToolButton::InstantPopup);
|
|
92 |
#endif
|
|
93 |
setIcon(style()->standardIcon(QStyle::SP_ToolBarHorizontalExtensionButton, 0, parentWidget()));
|
|
94 |
}
|
|
95 |
|
|
96 |
void QMenuBarExtension::paintEvent(QPaintEvent *)
|
|
97 |
{
|
|
98 |
QStylePainter p(this);
|
|
99 |
QStyleOptionToolButton opt;
|
|
100 |
initStyleOption(&opt);
|
|
101 |
// We do not need to draw both extention arrows
|
|
102 |
opt.features &= ~QStyleOptionToolButton::HasMenu;
|
|
103 |
p.drawComplexControl(QStyle::CC_ToolButton, opt);
|
|
104 |
}
|
|
105 |
|
|
106 |
|
|
107 |
QSize QMenuBarExtension::sizeHint() const
|
|
108 |
{
|
|
109 |
int ext = style()->pixelMetric(QStyle::PM_ToolBarExtensionExtent, 0, parentWidget());
|
|
110 |
return QSize(ext, ext);
|
|
111 |
}
|
|
112 |
|
|
113 |
|
|
114 |
/*!
|
|
115 |
\internal
|
|
116 |
*/
|
|
117 |
QAction *QMenuBarPrivate::actionAt(QPoint p) const
|
|
118 |
{
|
|
119 |
for(int i = 0; i < actions.size(); ++i) {
|
|
120 |
if(actionRect(actions.at(i)).contains(p))
|
|
121 |
return actions.at(i);
|
|
122 |
}
|
|
123 |
return 0;
|
|
124 |
}
|
|
125 |
|
|
126 |
QRect QMenuBarPrivate::menuRect(bool extVisible) const
|
|
127 |
{
|
|
128 |
Q_Q(const QMenuBar);
|
|
129 |
|
|
130 |
int hmargin = q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, q);
|
|
131 |
QRect result = q->rect();
|
|
132 |
result.adjust(hmargin, 0, -hmargin, 0);
|
|
133 |
|
|
134 |
if (extVisible) {
|
|
135 |
if (q->isRightToLeft())
|
|
136 |
result.setLeft(result.left() + extension->sizeHint().width());
|
|
137 |
else
|
|
138 |
result.setWidth(result.width() - extension->sizeHint().width());
|
|
139 |
}
|
|
140 |
|
|
141 |
if (leftWidget && leftWidget->isVisible()) {
|
|
142 |
QSize sz = leftWidget->sizeHint();
|
|
143 |
if (q->isRightToLeft())
|
|
144 |
result.setRight(result.right() - sz.width());
|
|
145 |
else
|
|
146 |
result.setLeft(result.left() + sz.width());
|
|
147 |
}
|
|
148 |
|
|
149 |
if (rightWidget && rightWidget->isVisible()) {
|
|
150 |
QSize sz = rightWidget->sizeHint();
|
|
151 |
if (q->isRightToLeft())
|
|
152 |
result.setLeft(result.left() + sz.width());
|
|
153 |
else
|
|
154 |
result.setRight(result.right() - sz.width());
|
|
155 |
}
|
|
156 |
|
|
157 |
return result;
|
|
158 |
}
|
|
159 |
|
|
160 |
bool QMenuBarPrivate::isVisible(QAction *action)
|
|
161 |
{
|
|
162 |
return !hiddenActions.contains(action);
|
|
163 |
}
|
|
164 |
|
|
165 |
void QMenuBarPrivate::updateGeometries()
|
|
166 |
{
|
|
167 |
Q_Q(QMenuBar);
|
|
168 |
if(!itemsDirty)
|
|
169 |
return;
|
|
170 |
int q_width = q->width()-(q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, q)*2);
|
|
171 |
int q_start = -1;
|
|
172 |
if(leftWidget || rightWidget) {
|
|
173 |
int vmargin = q->style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, q)
|
|
174 |
+ q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, q);
|
|
175 |
int hmargin = q->style()->pixelMetric(QStyle::PM_MenuBarHMargin, 0, q)
|
|
176 |
+ q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, q);
|
|
177 |
if (leftWidget && leftWidget->isVisible()) {
|
|
178 |
QSize sz = leftWidget->sizeHint();
|
|
179 |
q_width -= sz.width();
|
|
180 |
q_start = sz.width();
|
|
181 |
QPoint pos(hmargin, (q->height() - leftWidget->height()) / 2);
|
|
182 |
QRect vRect = QStyle::visualRect(q->layoutDirection(), q->rect(), QRect(pos, sz));
|
|
183 |
leftWidget->setGeometry(vRect);
|
|
184 |
}
|
|
185 |
if (rightWidget && rightWidget->isVisible()) {
|
|
186 |
QSize sz = rightWidget->sizeHint();
|
|
187 |
q_width -= sz.width();
|
|
188 |
QPoint pos(q->width() - sz.width() - hmargin, vmargin);
|
|
189 |
QRect vRect = QStyle::visualRect(q->layoutDirection(), q->rect(), QRect(pos, sz));
|
|
190 |
rightWidget->setGeometry(vRect);
|
|
191 |
}
|
|
192 |
}
|
|
193 |
|
|
194 |
#ifdef Q_WS_MAC
|
|
195 |
if(q->isNativeMenuBar()) {//nothing to see here folks, move along..
|
|
196 |
itemsDirty = false;
|
|
197 |
return;
|
|
198 |
}
|
|
199 |
#endif
|
|
200 |
calcActionRects(q_width, q_start);
|
|
201 |
currentAction = 0;
|
|
202 |
#ifndef QT_NO_SHORTCUT
|
|
203 |
if(itemsDirty) {
|
|
204 |
for(int j = 0; j < shortcutIndexMap.size(); ++j)
|
|
205 |
q->releaseShortcut(shortcutIndexMap.value(j));
|
|
206 |
shortcutIndexMap.resize(0); // faster than clear
|
|
207 |
for(int i = 0; i < actions.count(); i++)
|
|
208 |
shortcutIndexMap.append(q->grabShortcut(QKeySequence::mnemonic(actions.at(i)->text())));
|
|
209 |
}
|
|
210 |
#endif
|
|
211 |
itemsDirty = false;
|
|
212 |
|
|
213 |
hiddenActions.clear();
|
|
214 |
//this is the menu rectangle without any extension
|
|
215 |
QRect menuRect = this->menuRect(false);
|
|
216 |
|
|
217 |
//we try to see if the actions will fit there
|
|
218 |
bool hasHiddenActions = false;
|
|
219 |
for (int i = 0; i < actions.count(); ++i) {
|
|
220 |
const QRect &rect = actionRects.at(i);
|
|
221 |
if (rect.isValid() && !menuRect.contains(rect)) {
|
|
222 |
hasHiddenActions = true;
|
|
223 |
break;
|
|
224 |
}
|
|
225 |
}
|
|
226 |
|
|
227 |
//...and if not, determine the ones that fit on the menu with the extension visible
|
|
228 |
if (hasHiddenActions) {
|
|
229 |
menuRect = this->menuRect(true);
|
|
230 |
for (int i = 0; i < actions.count(); ++i) {
|
|
231 |
const QRect &rect = actionRects.at(i);
|
|
232 |
if (rect.isValid() && !menuRect.contains(rect)) {
|
|
233 |
hiddenActions.append(actions.at(i));
|
|
234 |
}
|
|
235 |
}
|
|
236 |
}
|
|
237 |
|
|
238 |
if (hiddenActions.count() > 0) {
|
|
239 |
QMenu *pop = extension->menu();
|
|
240 |
if (!pop) {
|
|
241 |
pop = new QMenu(q);
|
|
242 |
extension->setMenu(pop);
|
|
243 |
}
|
|
244 |
pop->clear();
|
|
245 |
pop->addActions(hiddenActions);
|
|
246 |
|
|
247 |
int vmargin = q->style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, q);
|
|
248 |
int x = q->isRightToLeft()
|
|
249 |
? menuRect.left() - extension->sizeHint().width() + 1
|
|
250 |
: menuRect.right();
|
|
251 |
extension->setGeometry(x, vmargin, extension->sizeHint().width(), menuRect.height() - vmargin*2);
|
|
252 |
extension->show();
|
|
253 |
} else {
|
|
254 |
extension->hide();
|
|
255 |
}
|
|
256 |
q->updateGeometry();
|
|
257 |
#ifdef QT3_SUPPORT
|
|
258 |
if (parent) {
|
|
259 |
QMenubarUpdatedEvent menubarUpdated(q);
|
|
260 |
QApplication::sendEvent(parent, &menubarUpdated);
|
|
261 |
}
|
|
262 |
#endif
|
|
263 |
}
|
|
264 |
|
|
265 |
QRect QMenuBarPrivate::actionRect(QAction *act) const
|
|
266 |
{
|
|
267 |
Q_Q(const QMenuBar);
|
|
268 |
const int index = actions.indexOf(act);
|
|
269 |
if (index == -1)
|
|
270 |
return QRect();
|
|
271 |
|
|
272 |
//makes sure the geometries are up-to-date
|
|
273 |
const_cast<QMenuBarPrivate*>(this)->updateGeometries();
|
|
274 |
|
|
275 |
if (index >= actionRects.count())
|
|
276 |
return QRect(); // that can happen in case of native menubar
|
|
277 |
|
|
278 |
QRect ret = actionRects.at(index);
|
|
279 |
return QStyle::visualRect(q->layoutDirection(), q->rect(), ret);
|
|
280 |
}
|
|
281 |
|
|
282 |
void QMenuBarPrivate::focusFirstAction()
|
|
283 |
{
|
|
284 |
if(!currentAction) {
|
|
285 |
updateGeometries();
|
|
286 |
int index = 0;
|
|
287 |
while (index < actions.count() && actionRects.at(index).isNull()) ++index;
|
|
288 |
if (index < actions.count())
|
|
289 |
setCurrentAction(actions.at(index));
|
|
290 |
}
|
|
291 |
}
|
|
292 |
|
|
293 |
void QMenuBarPrivate::setKeyboardMode(bool b)
|
|
294 |
{
|
|
295 |
Q_Q(QMenuBar);
|
|
296 |
if (b && !q->style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation, 0, q)) {
|
|
297 |
setCurrentAction(0);
|
|
298 |
return;
|
|
299 |
}
|
|
300 |
keyboardState = b;
|
|
301 |
if(b) {
|
|
302 |
QWidget *fw = QApplication::focusWidget();
|
|
303 |
if (fw != q)
|
|
304 |
keyboardFocusWidget = fw;
|
|
305 |
focusFirstAction();
|
|
306 |
q->setFocus(Qt::MenuBarFocusReason);
|
|
307 |
} else {
|
|
308 |
if(!popupState)
|
|
309 |
setCurrentAction(0);
|
|
310 |
if(keyboardFocusWidget) {
|
|
311 |
if (QApplication::focusWidget() == q)
|
|
312 |
keyboardFocusWidget->setFocus(Qt::MenuBarFocusReason);
|
|
313 |
keyboardFocusWidget = 0;
|
|
314 |
}
|
|
315 |
}
|
|
316 |
q->update();
|
|
317 |
}
|
|
318 |
|
|
319 |
void QMenuBarPrivate::popupAction(QAction *action, bool activateFirst)
|
|
320 |
{
|
|
321 |
Q_Q(QMenuBar);
|
|
322 |
if(!action || !action->menu() || closePopupMode)
|
|
323 |
return;
|
|
324 |
popupState = true;
|
|
325 |
if (action->isEnabled() && action->menu()->isEnabled()) {
|
|
326 |
closePopupMode = 0;
|
|
327 |
activeMenu = action->menu();
|
|
328 |
activeMenu->d_func()->causedPopup.widget = q;
|
|
329 |
activeMenu->d_func()->causedPopup.action = action;
|
|
330 |
|
|
331 |
QRect adjustedActionRect = actionRect(action);
|
|
332 |
QPoint pos(q->mapToGlobal(QPoint(adjustedActionRect.left(), adjustedActionRect.bottom() + 1)));
|
|
333 |
QSize popup_size = activeMenu->sizeHint();
|
|
334 |
|
|
335 |
QRect screenRect = QApplication::desktop()->screenGeometry(pos);
|
|
336 |
|
|
337 |
const bool fitUp = (q->mapToGlobal(adjustedActionRect.topLeft()).y() >= popup_size.height());
|
|
338 |
const bool fitDown = (pos.y() + popup_size.height() <= screenRect.bottom());
|
|
339 |
const int actionWidth = adjustedActionRect.width();
|
|
340 |
|
|
341 |
if (!fitUp && !fitDown) { //we should shift the menu
|
|
342 |
bool shouldShiftToRight = !q->isRightToLeft();
|
|
343 |
if (q->isRightToLeft() && popup_size.width() > pos.x())
|
|
344 |
shouldShiftToRight = true;
|
|
345 |
else if (actionWidth + popup_size.width() + pos.x() > screenRect.right())
|
|
346 |
shouldShiftToRight = false;
|
|
347 |
|
|
348 |
if (shouldShiftToRight)
|
|
349 |
pos.rx() += actionWidth;
|
|
350 |
else
|
|
351 |
pos.rx() -= popup_size.width();
|
|
352 |
} else if (q->isRightToLeft()) {
|
|
353 |
pos.setX(pos.x()-(popup_size.width() - actionWidth));
|
|
354 |
}
|
|
355 |
|
|
356 |
if(pos.x() < screenRect.x()) {
|
|
357 |
pos.setX(screenRect.x());
|
|
358 |
} else {
|
|
359 |
const int off = pos.x()+popup_size.width() - screenRect.right();
|
|
360 |
if(off > 0)
|
|
361 |
pos.setX(qMax(screenRect.x(), pos.x()-off));
|
|
362 |
|
|
363 |
}
|
|
364 |
|
|
365 |
if(!defaultPopDown || (fitUp && !fitDown))
|
|
366 |
pos.setY(qMax(screenRect.y(), q->mapToGlobal(QPoint(0, adjustedActionRect.top()-popup_size.height())).y()));
|
|
367 |
activeMenu->popup(pos);
|
|
368 |
if(activateFirst)
|
|
369 |
activeMenu->d_func()->setFirstActionActive();
|
|
370 |
}
|
|
371 |
q->update(actionRect(action));
|
|
372 |
}
|
|
373 |
|
|
374 |
void QMenuBarPrivate::setCurrentAction(QAction *action, bool popup, bool activateFirst)
|
|
375 |
{
|
|
376 |
if(currentAction == action && popup == popupState)
|
|
377 |
return;
|
|
378 |
|
|
379 |
autoReleaseTimer.stop();
|
|
380 |
|
|
381 |
doChildEffects = (popup && !activeMenu);
|
|
382 |
Q_Q(QMenuBar);
|
|
383 |
QWidget *fw = 0;
|
|
384 |
if(QMenu *menu = activeMenu) {
|
|
385 |
activeMenu = 0;
|
|
386 |
if (popup) {
|
|
387 |
fw = q->window()->focusWidget();
|
|
388 |
q->setFocus(Qt::NoFocusReason);
|
|
389 |
}
|
|
390 |
menu->hide();
|
|
391 |
}
|
|
392 |
|
|
393 |
if(currentAction)
|
|
394 |
q->update(actionRect(currentAction));
|
|
395 |
|
|
396 |
popupState = popup;
|
|
397 |
#ifndef QT_NO_STATUSTIP
|
|
398 |
QAction *previousAction = currentAction;
|
|
399 |
#endif
|
|
400 |
currentAction = action;
|
|
401 |
if (action) {
|
|
402 |
activateAction(action, QAction::Hover);
|
|
403 |
if(popup)
|
|
404 |
popupAction(action, activateFirst);
|
|
405 |
q->update(actionRect(action));
|
|
406 |
#ifndef QT_NO_STATUSTIP
|
|
407 |
} else if (previousAction) {
|
|
408 |
QString empty;
|
|
409 |
QStatusTipEvent tip(empty);
|
|
410 |
QApplication::sendEvent(q, &tip);
|
|
411 |
#endif
|
|
412 |
}
|
|
413 |
if (fw)
|
|
414 |
fw->setFocus(Qt::NoFocusReason);
|
|
415 |
}
|
|
416 |
|
|
417 |
void QMenuBarPrivate::calcActionRects(int max_width, int start) const
|
|
418 |
{
|
|
419 |
Q_Q(const QMenuBar);
|
|
420 |
|
|
421 |
if(!itemsDirty)
|
|
422 |
return;
|
|
423 |
|
|
424 |
//let's reinitialize the buffer
|
|
425 |
actionRects.resize(actions.count());
|
|
426 |
actionRects.fill(QRect());
|
|
427 |
|
|
428 |
const QStyle *style = q->style();
|
|
429 |
|
|
430 |
const int itemSpacing = style->pixelMetric(QStyle::PM_MenuBarItemSpacing, 0, q);
|
|
431 |
int max_item_height = 0, separator = -1, separator_start = 0, separator_len = 0;
|
|
432 |
|
|
433 |
//calculate size
|
|
434 |
const QFontMetrics fm = q->fontMetrics();
|
|
435 |
const int hmargin = style->pixelMetric(QStyle::PM_MenuBarHMargin, 0, q),
|
|
436 |
vmargin = style->pixelMetric(QStyle::PM_MenuBarVMargin, 0, q),
|
|
437 |
icone = style->pixelMetric(QStyle::PM_SmallIconSize, 0, q);
|
|
438 |
for(int i = 0; i < actions.count(); i++) {
|
|
439 |
QAction *action = actions.at(i);
|
|
440 |
if(!action->isVisible())
|
|
441 |
continue;
|
|
442 |
|
|
443 |
QSize sz;
|
|
444 |
|
|
445 |
//calc what I think the size is..
|
|
446 |
if(action->isSeparator()) {
|
|
447 |
if (style->styleHint(QStyle::SH_DrawMenuBarSeparator, 0, q))
|
|
448 |
separator = i;
|
|
449 |
continue; //we don't really position these!
|
|
450 |
} else {
|
|
451 |
const QString s = action->text();
|
|
452 |
QIcon is = action->icon();
|
|
453 |
// If an icon is set, only the icon is visible
|
|
454 |
if (!is.isNull())
|
|
455 |
sz = sz.expandedTo(QSize(icone, icone));
|
|
456 |
else if (!s.isEmpty())
|
|
457 |
sz = fm.size(Qt::TextShowMnemonic, s);
|
|
458 |
}
|
|
459 |
|
|
460 |
//let the style modify the above size..
|
|
461 |
QStyleOptionMenuItem opt;
|
|
462 |
q->initStyleOption(&opt, action);
|
|
463 |
sz = q->style()->sizeFromContents(QStyle::CT_MenuBarItem, &opt, sz, q);
|
|
464 |
|
|
465 |
if(!sz.isEmpty()) {
|
|
466 |
{ //update the separator state
|
|
467 |
int iWidth = sz.width() + itemSpacing;
|
|
468 |
if(separator == -1)
|
|
469 |
separator_start += iWidth;
|
|
470 |
else
|
|
471 |
separator_len += iWidth;
|
|
472 |
}
|
|
473 |
//maximum height
|
|
474 |
max_item_height = qMax(max_item_height, sz.height());
|
|
475 |
//append
|
|
476 |
actionRects[i] = QRect(0, 0, sz.width(), sz.height());
|
|
477 |
}
|
|
478 |
}
|
|
479 |
|
|
480 |
//calculate position
|
|
481 |
const int fw = q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, q);
|
|
482 |
int x = fw + ((start == -1) ? hmargin : start) + itemSpacing;
|
|
483 |
int y = fw + vmargin;
|
|
484 |
for(int i = 0; i < actions.count(); i++) {
|
|
485 |
QRect &rect = actionRects[i];
|
|
486 |
if (rect.isNull())
|
|
487 |
continue;
|
|
488 |
|
|
489 |
//resize
|
|
490 |
rect.setHeight(max_item_height);
|
|
491 |
|
|
492 |
//move
|
|
493 |
if(separator != -1 && i >= separator) { //after the separator
|
|
494 |
int left = (max_width - separator_len - hmargin - itemSpacing) + (x - separator_start - hmargin);
|
|
495 |
if(left < separator_start) { //wrap
|
|
496 |
separator_start = x = hmargin;
|
|
497 |
y += max_item_height;
|
|
498 |
}
|
|
499 |
rect.moveLeft(left);
|
|
500 |
} else {
|
|
501 |
rect.moveLeft(x);
|
|
502 |
}
|
|
503 |
rect.moveTop(y);
|
|
504 |
|
|
505 |
//keep moving along..
|
|
506 |
x += rect.width() + itemSpacing;
|
|
507 |
}
|
|
508 |
}
|
|
509 |
|
|
510 |
void QMenuBarPrivate::activateAction(QAction *action, QAction::ActionEvent action_e)
|
|
511 |
{
|
|
512 |
Q_Q(QMenuBar);
|
|
513 |
if (!action || !action->isEnabled())
|
|
514 |
return;
|
|
515 |
action->activate(action_e);
|
|
516 |
if (action_e == QAction::Hover)
|
|
517 |
action->showStatusText(q);
|
|
518 |
|
|
519 |
// if(action_e == QAction::Trigger)
|
|
520 |
// emit q->activated(action);
|
|
521 |
// else if(action_e == QAction::Hover)
|
|
522 |
// emit q->highlighted(action);
|
|
523 |
}
|
|
524 |
|
|
525 |
|
|
526 |
void QMenuBarPrivate::_q_actionTriggered()
|
|
527 |
{
|
|
528 |
Q_Q(QMenuBar);
|
|
529 |
if (QAction *action = qobject_cast<QAction *>(q->sender())) {
|
|
530 |
emit q->triggered(action);
|
|
531 |
#ifdef QT3_SUPPORT
|
|
532 |
emit q->activated(q->findIdForAction(action));
|
|
533 |
#endif
|
|
534 |
}
|
|
535 |
}
|
|
536 |
|
|
537 |
void QMenuBarPrivate::_q_actionHovered()
|
|
538 |
{
|
|
539 |
Q_Q(QMenuBar);
|
|
540 |
if (QAction *action = qobject_cast<QAction *>(q->sender())) {
|
|
541 |
emit q->hovered(action);
|
|
542 |
#ifndef QT_NO_ACCESSIBILITY
|
|
543 |
if (QAccessible::isActive()) {
|
|
544 |
int actionIndex = actions.indexOf(action);
|
|
545 |
++actionIndex;
|
|
546 |
QAccessible::updateAccessibility(q, actionIndex, QAccessible::Focus);
|
|
547 |
QAccessible::updateAccessibility(q, actionIndex, QAccessible::Selection);
|
|
548 |
}
|
|
549 |
#endif //QT_NO_ACCESSIBILITY
|
|
550 |
#ifdef QT3_SUPPORT
|
|
551 |
emit q->highlighted(q->findIdForAction(action));
|
|
552 |
#endif
|
|
553 |
}
|
|
554 |
}
|
|
555 |
|
|
556 |
/*!
|
|
557 |
Initialize \a option with the values from the menu bar and information from \a action. This method
|
|
558 |
is useful for subclasses when they need a QStyleOptionMenuItem, but don't want
|
|
559 |
to fill in all the information themselves.
|
|
560 |
|
|
561 |
\sa QStyleOption::initFrom() QMenu::initStyleOption()
|
|
562 |
*/
|
|
563 |
void QMenuBar::initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const
|
|
564 |
{
|
|
565 |
if (!option || !action)
|
|
566 |
return;
|
|
567 |
Q_D(const QMenuBar);
|
|
568 |
option->palette = palette();
|
|
569 |
option->state = QStyle::State_None;
|
|
570 |
if (isEnabled() && action->isEnabled())
|
|
571 |
option->state |= QStyle::State_Enabled;
|
|
572 |
else
|
|
573 |
option->palette.setCurrentColorGroup(QPalette::Disabled);
|
|
574 |
option->fontMetrics = fontMetrics();
|
|
575 |
if (d->currentAction && d->currentAction == action) {
|
|
576 |
option->state |= QStyle::State_Selected;
|
|
577 |
if (d->popupState && !d->closePopupMode)
|
|
578 |
option->state |= QStyle::State_Sunken;
|
|
579 |
}
|
|
580 |
if (hasFocus() || d->currentAction)
|
|
581 |
option->state |= QStyle::State_HasFocus;
|
|
582 |
option->menuRect = rect();
|
|
583 |
option->menuItemType = QStyleOptionMenuItem::Normal;
|
|
584 |
option->checkType = QStyleOptionMenuItem::NotCheckable;
|
|
585 |
option->text = action->text();
|
|
586 |
option->icon = action->icon();
|
|
587 |
}
|
|
588 |
|
|
589 |
/*!
|
|
590 |
\class QMenuBar
|
|
591 |
\brief The QMenuBar class provides a horizontal menu bar.
|
|
592 |
|
|
593 |
\ingroup mainwindow-classes
|
|
594 |
|
|
595 |
A menu bar consists of a list of pull-down menu items. You add
|
|
596 |
menu items with addMenu(). For example, asuming that \c menubar
|
|
597 |
is a pointer to a QMenuBar and \c fileMenu is a pointer to a
|
|
598 |
QMenu, the following statement inserts the menu into the menu bar:
|
|
599 |
\snippet doc/src/snippets/code/src_gui_widgets_qmenubar.cpp 0
|
|
600 |
|
|
601 |
The ampersand in the menu item's text sets Alt+F as a shortcut for
|
|
602 |
this menu. (You can use "\&\&" to get a real ampersand in the menu
|
|
603 |
bar.)
|
|
604 |
|
|
605 |
There is no need to lay out a menu bar. It automatically sets its
|
|
606 |
own geometry to the top of the parent widget and changes it
|
|
607 |
appropriately whenever the parent is resized.
|
|
608 |
|
|
609 |
\section1 Usage
|
|
610 |
|
|
611 |
In most main window style applications you would use the
|
|
612 |
\l{QMainWindow::}{menuBar()} function provided in QMainWindow,
|
|
613 |
adding \l{QMenu}s to the menu bar and adding \l{QAction}s to the
|
|
614 |
pop-up menus.
|
|
615 |
|
|
616 |
Example (from the \l{mainwindows/menus}{Menus} example):
|
|
617 |
|
|
618 |
\snippet examples/mainwindows/menus/mainwindow.cpp 9
|
|
619 |
|
|
620 |
Menu items may be removed with removeAction().
|
|
621 |
|
|
622 |
Widgets can be added to menus by using instances of the QWidgetAction
|
|
623 |
class to hold them. These actions can then be inserted into menus
|
|
624 |
in the usual way; see the QMenu documentation for more details.
|
|
625 |
|
|
626 |
\section1 Platform Dependent Look and Feel
|
|
627 |
|
|
628 |
Different platforms have different requirements for the appearance
|
|
629 |
of menu bars and their behavior when the user interacts with them.
|
|
630 |
For example, Windows systems are often configured so that the
|
|
631 |
underlined character mnemonics that indicate keyboard shortcuts
|
|
632 |
for items in the menu bar are only shown when the \gui{Alt} key is
|
|
633 |
pressed.
|
|
634 |
|
|
635 |
\table
|
|
636 |
|
|
637 |
\row \o \inlineimage plastique-menubar.png A menu bar shown in the
|
|
638 |
Plastique widget style.
|
|
639 |
|
|
640 |
\o The \l{QPlastiqueStyle}{Plastique widget style}, like most
|
|
641 |
other styles, handles the \gui{Help} menu in the same way as it
|
|
642 |
handles any other menu.
|
|
643 |
|
|
644 |
\row \o \inlineimage motif-menubar.png A menu bar shown in the
|
|
645 |
Motif widget style.
|
|
646 |
|
|
647 |
\o The \l{QMotifStyle}{Motif widget style} treats \gui{Help} menus
|
|
648 |
in a special way, placing them at right-hand end of the menu bar.
|
|
649 |
|
|
650 |
\endtable
|
|
651 |
|
|
652 |
\section1 QMenuBar on Mac OS X
|
|
653 |
|
|
654 |
QMenuBar on Mac OS X is a wrapper for using the system-wide menu bar.
|
|
655 |
If you have multiple menu bars in one dialog the outermost menu bar
|
|
656 |
(normally inside a widget with widget flag Qt::Window) will
|
|
657 |
be used for the system-wide menu bar.
|
|
658 |
|
|
659 |
Qt for Mac OS X also provides a menu bar merging feature to make
|
|
660 |
QMenuBar conform more closely to accepted Mac OS X menu bar layout.
|
|
661 |
The merging functionality is based on string matching the title of
|
|
662 |
a QMenu entry. These strings are translated (using QObject::tr())
|
|
663 |
in the "QMenuBar" context. If an entry is moved its slots will still
|
|
664 |
fire as if it was in the original place. The table below outlines
|
|
665 |
the strings looked for and where the entry is placed if matched:
|
|
666 |
|
|
667 |
\table
|
|
668 |
\header \i String matches \i Placement \i Notes
|
|
669 |
\row \i about.*
|
|
670 |
\i Application Menu | About <application name>
|
|
671 |
\i The application name is fetched from the \c {Info.plist} file
|
|
672 |
(see note below). If this entry is not found no About item
|
|
673 |
will appear in the Application Menu.
|
|
674 |
\row \i config, options, setup, settings or preferences
|
|
675 |
\i Application Menu | Preferences
|
|
676 |
\i If this entry is not found the Settings item will be disabled
|
|
677 |
\row \i quit or exit
|
|
678 |
\i Application Menu | Quit <application name>
|
|
679 |
\i If this entry is not found a default Quit item will be
|
|
680 |
created to call QApplication::quit()
|
|
681 |
\endtable
|
|
682 |
|
|
683 |
You can override this behavior by using the QAction::menuRole()
|
|
684 |
property.
|
|
685 |
|
|
686 |
If you want all windows in a Mac application to share one menu
|
|
687 |
bar, you must create a menu bar that does not have a parent.
|
|
688 |
Create a parent-less menu bar this way:
|
|
689 |
|
|
690 |
\snippet doc/src/snippets/code/src_gui_widgets_qmenubar.cpp 1
|
|
691 |
|
|
692 |
\bold{Note:} Do \e{not} call QMainWindow::menuBar() to create the
|
|
693 |
shared menu bar, because that menu bar will have the QMainWindow
|
|
694 |
as its parent. That menu bar would only be displayed for the
|
|
695 |
parent QMainWindow.
|
|
696 |
|
|
697 |
\bold{Note:} The text used for the application name in the menu
|
|
698 |
bar is obtained from the value set in the \c{Info.plist} file in
|
|
699 |
the application's bundle. See \l{Deploying an Application on
|
|
700 |
Mac OS X} for more information.
|
|
701 |
|
|
702 |
\section1 QMenuBar on Windows CE
|
|
703 |
|
|
704 |
QMenuBar on Windows CE is a wrapper for using the system-wide menu bar,
|
|
705 |
similar to the Mac. This feature is activated for Windows Mobile
|
|
706 |
and integrates QMenuBar with the native soft keys. The left soft
|
|
707 |
key can be controlled with QMenuBar::setDefaultAction() and the
|
|
708 |
right soft key can be used to access the menu bar.
|
|
709 |
|
|
710 |
The hovered() signal is not supported for the native menu
|
|
711 |
integration. Also, it is not possible to display an icon in a
|
|
712 |
native menu on Windows Mobile.
|
|
713 |
|
|
714 |
\section1 Examples
|
|
715 |
|
|
716 |
The \l{mainwindows/menus}{Menus} example shows how to use QMenuBar
|
|
717 |
and QMenu. The other \l{Main Window Examples}{main window
|
|
718 |
application examples} also provide menus using these classes.
|
|
719 |
|
|
720 |
\sa QMenu, QShortcut, QAction,
|
|
721 |
{http://developer.apple.com/documentation/UserExperience/Conceptual/AppleHIGuidelines/XHIGIntro/XHIGIntro.html}{Introduction to Apple Human Interface Guidelines},
|
|
722 |
{fowler}{GUI Design Handbook: Menu Bar}, {Menus Example}
|
|
723 |
*/
|
|
724 |
|
|
725 |
|
|
726 |
void QMenuBarPrivate::init()
|
|
727 |
{
|
|
728 |
Q_Q(QMenuBar);
|
|
729 |
q->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
|
|
730 |
q->setAttribute(Qt::WA_CustomWhatsThis);
|
|
731 |
#ifdef Q_WS_MAC
|
|
732 |
macCreateMenuBar(q->parentWidget());
|
|
733 |
if(mac_menubar)
|
|
734 |
q->hide();
|
|
735 |
#endif
|
|
736 |
#ifdef Q_WS_WINCE
|
|
737 |
if (qt_wince_is_mobile()) {
|
|
738 |
wceCreateMenuBar(q->parentWidget());
|
|
739 |
if(wce_menubar)
|
|
740 |
q->hide();
|
|
741 |
}
|
|
742 |
#endif
|
|
743 |
#ifdef Q_WS_S60
|
|
744 |
symbianCreateMenuBar(q->parentWidget());
|
|
745 |
if(symbian_menubar)
|
|
746 |
q->hide();
|
|
747 |
#endif
|
|
748 |
|
|
749 |
q->setBackgroundRole(QPalette::Button);
|
|
750 |
oldWindow = oldParent = 0;
|
|
751 |
#ifdef QT3_SUPPORT
|
|
752 |
doAutoResize = false;
|
|
753 |
#endif
|
|
754 |
handleReparent();
|
|
755 |
q->setMouseTracking(q->style()->styleHint(QStyle::SH_MenuBar_MouseTracking, 0, q));
|
|
756 |
|
|
757 |
extension = new QMenuBarExtension(q);
|
|
758 |
extension->setFocusPolicy(Qt::NoFocus);
|
|
759 |
extension->hide();
|
|
760 |
}
|
|
761 |
|
|
762 |
//Gets the next action for keyboard navigation
|
|
763 |
QAction *QMenuBarPrivate::getNextAction(const int _start, const int increment) const
|
|
764 |
{
|
|
765 |
Q_Q(const QMenuBar);
|
|
766 |
const_cast<QMenuBarPrivate*>(this)->updateGeometries();
|
|
767 |
bool allowActiveAndDisabled = q->style()->styleHint(QStyle::SH_Menu_AllowActiveAndDisabled, 0, q);
|
|
768 |
const int start = (_start == -1 && increment == -1) ? actions.count() : _start;
|
|
769 |
const int end = increment == -1 ? 0 : actions.count() - 1;
|
|
770 |
|
|
771 |
for (int i = start; start != end;) {
|
|
772 |
i += increment;
|
|
773 |
QAction *current = actions.at(i);
|
|
774 |
if (!actionRects.at(i).isNull() && (allowActiveAndDisabled || current->isEnabled()))
|
|
775 |
return current;
|
|
776 |
}
|
|
777 |
|
|
778 |
if (_start != -1) //let's try from the beginning or the end
|
|
779 |
return getNextAction(-1, increment);
|
|
780 |
|
|
781 |
return 0;
|
|
782 |
}
|
|
783 |
|
|
784 |
/*!
|
|
785 |
Constructs a menu bar with parent \a parent.
|
|
786 |
*/
|
|
787 |
QMenuBar::QMenuBar(QWidget *parent) : QWidget(*new QMenuBarPrivate, parent, 0)
|
|
788 |
{
|
|
789 |
Q_D(QMenuBar);
|
|
790 |
d->init();
|
|
791 |
}
|
|
792 |
|
|
793 |
#ifdef QT3_SUPPORT
|
|
794 |
/*!
|
|
795 |
Use one of the constructors that doesn't take the \a name
|
|
796 |
argument and then use setObjectName() instead.
|
|
797 |
*/
|
|
798 |
QMenuBar::QMenuBar(QWidget *parent, const char *name) : QWidget(*new QMenuBarPrivate, parent, 0)
|
|
799 |
{
|
|
800 |
Q_D(QMenuBar);
|
|
801 |
d->init();
|
|
802 |
setObjectName(QString::fromAscii(name));
|
|
803 |
}
|
|
804 |
#endif
|
|
805 |
|
|
806 |
/*!
|
|
807 |
Destroys the menu bar.
|
|
808 |
*/
|
|
809 |
QMenuBar::~QMenuBar()
|
|
810 |
{
|
|
811 |
#ifdef Q_WS_MAC
|
|
812 |
Q_D(QMenuBar);
|
|
813 |
d->macDestroyMenuBar();
|
|
814 |
#endif
|
|
815 |
#ifdef Q_WS_WINCE
|
|
816 |
Q_D(QMenuBar);
|
|
817 |
if (qt_wince_is_mobile())
|
|
818 |
d->wceDestroyMenuBar();
|
|
819 |
#endif
|
|
820 |
#ifdef Q_WS_S60
|
|
821 |
Q_D(QMenuBar);
|
|
822 |
d->symbianDestroyMenuBar();
|
|
823 |
#endif
|
|
824 |
}
|
|
825 |
|
|
826 |
/*!
|
|
827 |
\overload
|
|
828 |
|
|
829 |
This convenience function creates a new action with \a text.
|
|
830 |
The function adds the newly created action to the menu's
|
|
831 |
list of actions, and returns it.
|
|
832 |
|
|
833 |
\sa QWidget::addAction(), QWidget::actions()
|
|
834 |
*/
|
|
835 |
QAction *QMenuBar::addAction(const QString &text)
|
|
836 |
{
|
|
837 |
QAction *ret = new QAction(text, this);
|
|
838 |
addAction(ret);
|
|
839 |
return ret;
|
|
840 |
}
|
|
841 |
|
|
842 |
/*!
|
|
843 |
\overload
|
|
844 |
|
|
845 |
This convenience function creates a new action with the given \a
|
|
846 |
text. The action's triggered() signal is connected to the \a
|
|
847 |
receiver's \a member slot. The function adds the newly created
|
|
848 |
action to the menu's list of actions and returns it.
|
|
849 |
|
|
850 |
\sa QWidget::addAction(), QWidget::actions()
|
|
851 |
*/
|
|
852 |
QAction *QMenuBar::addAction(const QString &text, const QObject *receiver, const char* member)
|
|
853 |
{
|
|
854 |
QAction *ret = new QAction(text, this);
|
|
855 |
QObject::connect(ret, SIGNAL(triggered(bool)), receiver, member);
|
|
856 |
addAction(ret);
|
|
857 |
return ret;
|
|
858 |
}
|
|
859 |
|
|
860 |
/*!
|
|
861 |
Appends a new QMenu with \a title to the menu bar. The menu bar
|
|
862 |
takes ownership of the menu. Returns the new menu.
|
|
863 |
|
|
864 |
\sa QWidget::addAction() QMenu::menuAction()
|
|
865 |
*/
|
|
866 |
QMenu *QMenuBar::addMenu(const QString &title)
|
|
867 |
{
|
|
868 |
QMenu *menu = new QMenu(title, this);
|
|
869 |
addAction(menu->menuAction());
|
|
870 |
return menu;
|
|
871 |
}
|
|
872 |
|
|
873 |
/*!
|
|
874 |
Appends a new QMenu with \a icon and \a title to the menu bar. The menu bar
|
|
875 |
takes ownership of the menu. Returns the new menu.
|
|
876 |
|
|
877 |
\sa QWidget::addAction() QMenu::menuAction()
|
|
878 |
*/
|
|
879 |
QMenu *QMenuBar::addMenu(const QIcon &icon, const QString &title)
|
|
880 |
{
|
|
881 |
QMenu *menu = new QMenu(title, this);
|
|
882 |
menu->setIcon(icon);
|
|
883 |
addAction(menu->menuAction());
|
|
884 |
return menu;
|
|
885 |
}
|
|
886 |
|
|
887 |
/*!
|
|
888 |
Appends \a menu to the menu bar. Returns the menu's menuAction().
|
|
889 |
|
|
890 |
\note The returned QAction object can be used to hide the corresponding
|
|
891 |
menu.
|
|
892 |
|
|
893 |
\sa QWidget::addAction() QMenu::menuAction()
|
|
894 |
*/
|
|
895 |
QAction *QMenuBar::addMenu(QMenu *menu)
|
|
896 |
{
|
|
897 |
QAction *action = menu->menuAction();
|
|
898 |
addAction(action);
|
|
899 |
return action;
|
|
900 |
}
|
|
901 |
|
|
902 |
/*!
|
|
903 |
Appends a separator to the menu.
|
|
904 |
*/
|
|
905 |
QAction *QMenuBar::addSeparator()
|
|
906 |
{
|
|
907 |
QAction *ret = new QAction(this);
|
|
908 |
ret->setSeparator(true);
|
|
909 |
addAction(ret);
|
|
910 |
return ret;
|
|
911 |
}
|
|
912 |
|
|
913 |
/*!
|
|
914 |
This convenience function creates a new separator action, i.e. an
|
|
915 |
action with QAction::isSeparator() returning true. The function inserts
|
|
916 |
the newly created action into this menu bar's list of actions before
|
|
917 |
action \a before and returns it.
|
|
918 |
|
|
919 |
\sa QWidget::insertAction(), addSeparator()
|
|
920 |
*/
|
|
921 |
QAction *QMenuBar::insertSeparator(QAction *before)
|
|
922 |
{
|
|
923 |
QAction *action = new QAction(this);
|
|
924 |
action->setSeparator(true);
|
|
925 |
insertAction(before, action);
|
|
926 |
return action;
|
|
927 |
}
|
|
928 |
|
|
929 |
/*!
|
|
930 |
This convenience function inserts \a menu before action \a before
|
|
931 |
and returns the menus menuAction().
|
|
932 |
|
|
933 |
\sa QWidget::insertAction() addMenu()
|
|
934 |
*/
|
|
935 |
QAction *QMenuBar::insertMenu(QAction *before, QMenu *menu)
|
|
936 |
{
|
|
937 |
QAction *action = menu->menuAction();
|
|
938 |
insertAction(before, action);
|
|
939 |
return action;
|
|
940 |
}
|
|
941 |
|
|
942 |
/*!
|
|
943 |
Returns the QAction that is currently highlighted. A null pointer
|
|
944 |
will be returned if no action is currently selected.
|
|
945 |
*/
|
|
946 |
QAction *QMenuBar::activeAction() const
|
|
947 |
{
|
|
948 |
Q_D(const QMenuBar);
|
|
949 |
return d->currentAction;
|
|
950 |
}
|
|
951 |
|
|
952 |
/*!
|
|
953 |
\since 4.1
|
|
954 |
|
|
955 |
Sets the currently highlighted action to \a act.
|
|
956 |
*/
|
|
957 |
void QMenuBar::setActiveAction(QAction *act)
|
|
958 |
{
|
|
959 |
Q_D(QMenuBar);
|
|
960 |
d->setCurrentAction(act, true, false);
|
|
961 |
}
|
|
962 |
|
|
963 |
|
|
964 |
/*!
|
|
965 |
Removes all the actions from the menu bar.
|
|
966 |
|
|
967 |
\note On Mac OS X, menu items that have been merged to the system
|
|
968 |
menu bar are not removed by this function. One way to handle this
|
|
969 |
would be to remove the extra actions yourself. You can set the
|
|
970 |
\l{QAction::MenuRole}{menu role} on the different menus, so that
|
|
971 |
you know ahead of time which menu items get merged and which do
|
|
972 |
not. Then decide what to recreate or remove yourself.
|
|
973 |
|
|
974 |
\sa removeAction()
|
|
975 |
*/
|
|
976 |
void QMenuBar::clear()
|
|
977 |
{
|
|
978 |
QList<QAction*> acts = actions();
|
|
979 |
for(int i = 0; i < acts.size(); i++)
|
|
980 |
removeAction(acts[i]);
|
|
981 |
}
|
|
982 |
|
|
983 |
/*!
|
|
984 |
\property QMenuBar::defaultUp
|
|
985 |
\brief the popup orientation
|
|
986 |
|
|
987 |
The default popup orientation. By default, menus pop "down" the
|
|
988 |
screen. By setting the property to true, the menu will pop "up".
|
|
989 |
You might call this for menus that are \e below the document to
|
|
990 |
which they refer.
|
|
991 |
|
|
992 |
If the menu would not fit on the screen, the other direction is
|
|
993 |
used automatically.
|
|
994 |
*/
|
|
995 |
void QMenuBar::setDefaultUp(bool b)
|
|
996 |
{
|
|
997 |
Q_D(QMenuBar);
|
|
998 |
d->defaultPopDown = !b;
|
|
999 |
}
|
|
1000 |
|
|
1001 |
bool QMenuBar::isDefaultUp() const
|
|
1002 |
{
|
|
1003 |
Q_D(const QMenuBar);
|
|
1004 |
return !d->defaultPopDown;
|
|
1005 |
}
|
|
1006 |
|
|
1007 |
/*!
|
|
1008 |
\reimp
|
|
1009 |
*/
|
|
1010 |
void QMenuBar::resizeEvent(QResizeEvent *)
|
|
1011 |
{
|
|
1012 |
Q_D(QMenuBar);
|
|
1013 |
d->itemsDirty = true;
|
|
1014 |
d->updateGeometries();
|
|
1015 |
}
|
|
1016 |
|
|
1017 |
/*!
|
|
1018 |
\reimp
|
|
1019 |
*/
|
|
1020 |
void QMenuBar::paintEvent(QPaintEvent *e)
|
|
1021 |
{
|
|
1022 |
Q_D(QMenuBar);
|
|
1023 |
QPainter p(this);
|
|
1024 |
QRegion emptyArea(rect());
|
|
1025 |
|
|
1026 |
//draw the items
|
|
1027 |
for (int i = 0; i < d->actions.count(); ++i) {
|
|
1028 |
QAction *action = d->actions.at(i);
|
|
1029 |
QRect adjustedActionRect = d->actionRect(action);
|
|
1030 |
if (adjustedActionRect.isEmpty() || !d->isVisible(action))
|
|
1031 |
continue;
|
|
1032 |
if(!e->rect().intersects(adjustedActionRect))
|
|
1033 |
continue;
|
|
1034 |
|
|
1035 |
emptyArea -= adjustedActionRect;
|
|
1036 |
QStyleOptionMenuItem opt;
|
|
1037 |
initStyleOption(&opt, action);
|
|
1038 |
opt.rect = adjustedActionRect;
|
|
1039 |
p.setClipRect(adjustedActionRect);
|
|
1040 |
style()->drawControl(QStyle::CE_MenuBarItem, &opt, &p, this);
|
|
1041 |
}
|
|
1042 |
//draw border
|
|
1043 |
if(int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, this)) {
|
|
1044 |
QRegion borderReg;
|
|
1045 |
borderReg += QRect(0, 0, fw, height()); //left
|
|
1046 |
borderReg += QRect(width()-fw, 0, fw, height()); //right
|
|
1047 |
borderReg += QRect(0, 0, width(), fw); //top
|
|
1048 |
borderReg += QRect(0, height()-fw, width(), fw); //bottom
|
|
1049 |
p.setClipRegion(borderReg);
|
|
1050 |
emptyArea -= borderReg;
|
|
1051 |
QStyleOptionFrame frame;
|
|
1052 |
frame.rect = rect();
|
|
1053 |
frame.palette = palette();
|
|
1054 |
frame.state = QStyle::State_None;
|
|
1055 |
frame.lineWidth = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth);
|
|
1056 |
frame.midLineWidth = 0;
|
|
1057 |
style()->drawPrimitive(QStyle::PE_PanelMenuBar, &frame, &p, this);
|
|
1058 |
}
|
|
1059 |
p.setClipRegion(emptyArea);
|
|
1060 |
QStyleOptionMenuItem menuOpt;
|
|
1061 |
menuOpt.palette = palette();
|
|
1062 |
menuOpt.state = QStyle::State_None;
|
|
1063 |
menuOpt.menuItemType = QStyleOptionMenuItem::EmptyArea;
|
|
1064 |
menuOpt.checkType = QStyleOptionMenuItem::NotCheckable;
|
|
1065 |
menuOpt.rect = rect();
|
|
1066 |
menuOpt.menuRect = rect();
|
|
1067 |
style()->drawControl(QStyle::CE_MenuBarEmptyArea, &menuOpt, &p, this);
|
|
1068 |
}
|
|
1069 |
|
|
1070 |
/*!
|
|
1071 |
\reimp
|
|
1072 |
*/
|
|
1073 |
void QMenuBar::setVisible(bool visible)
|
|
1074 |
{
|
|
1075 |
#if defined(Q_WS_MAC) || defined(Q_OS_WINCE) || defined(Q_WS_S60)
|
|
1076 |
if (isNativeMenuBar())
|
|
1077 |
return;
|
|
1078 |
#endif
|
|
1079 |
QWidget::setVisible(visible);
|
|
1080 |
}
|
|
1081 |
|
|
1082 |
/*!
|
|
1083 |
\reimp
|
|
1084 |
*/
|
|
1085 |
void QMenuBar::mousePressEvent(QMouseEvent *e)
|
|
1086 |
{
|
|
1087 |
Q_D(QMenuBar);
|
|
1088 |
if(e->button() != Qt::LeftButton)
|
|
1089 |
return;
|
|
1090 |
|
|
1091 |
d->mouseDown = true;
|
|
1092 |
|
|
1093 |
QAction *action = d->actionAt(e->pos());
|
|
1094 |
if (!action || !d->isVisible(action)) {
|
|
1095 |
d->setCurrentAction(0);
|
|
1096 |
#ifndef QT_NO_WHATSTHIS
|
|
1097 |
if (QWhatsThis::inWhatsThisMode())
|
|
1098 |
QWhatsThis::showText(e->globalPos(), d->whatsThis, this);
|
|
1099 |
#endif
|
|
1100 |
return;
|
|
1101 |
}
|
|
1102 |
|
|
1103 |
if(d->currentAction == action && d->popupState) {
|
|
1104 |
if(QMenu *menu = d->activeMenu) {
|
|
1105 |
d->activeMenu = 0;
|
|
1106 |
menu->hide();
|
|
1107 |
}
|
|
1108 |
#ifdef Q_WS_WIN
|
|
1109 |
if((d->closePopupMode = style()->styleHint(QStyle::SH_MenuBar_DismissOnSecondClick)))
|
|
1110 |
update(d->actionRect(action));
|
|
1111 |
#endif
|
|
1112 |
} else {
|
|
1113 |
d->setCurrentAction(action, true);
|
|
1114 |
}
|
|
1115 |
}
|
|
1116 |
|
|
1117 |
/*!
|
|
1118 |
\reimp
|
|
1119 |
*/
|
|
1120 |
void QMenuBar::mouseReleaseEvent(QMouseEvent *e)
|
|
1121 |
{
|
|
1122 |
Q_D(QMenuBar);
|
|
1123 |
if(e->button() != Qt::LeftButton || !d->mouseDown)
|
|
1124 |
return;
|
|
1125 |
|
|
1126 |
d->mouseDown = false;
|
|
1127 |
QAction *action = d->actionAt(e->pos());
|
|
1128 |
if((d->closePopupMode && action == d->currentAction) || !action || !action->menu()) {
|
|
1129 |
//we set the current action before activating
|
|
1130 |
//so that we let the leave event set the current back to 0
|
|
1131 |
d->setCurrentAction(action, false);
|
|
1132 |
if(action)
|
|
1133 |
d->activateAction(action, QAction::Trigger);
|
|
1134 |
}
|
|
1135 |
d->closePopupMode = 0;
|
|
1136 |
}
|
|
1137 |
|
|
1138 |
/*!
|
|
1139 |
\reimp
|
|
1140 |
*/
|
|
1141 |
void QMenuBar::keyPressEvent(QKeyEvent *e)
|
|
1142 |
{
|
|
1143 |
Q_D(QMenuBar);
|
|
1144 |
d->updateGeometries();
|
|
1145 |
int key = e->key();
|
|
1146 |
if(isRightToLeft()) { // in reverse mode open/close key for submenues are reversed
|
|
1147 |
if(key == Qt::Key_Left)
|
|
1148 |
key = Qt::Key_Right;
|
|
1149 |
else if(key == Qt::Key_Right)
|
|
1150 |
key = Qt::Key_Left;
|
|
1151 |
}
|
|
1152 |
if(key == Qt::Key_Tab) //means right
|
|
1153 |
key = Qt::Key_Right;
|
|
1154 |
else if(key == Qt::Key_Backtab) //means left
|
|
1155 |
key = Qt::Key_Left;
|
|
1156 |
|
|
1157 |
bool key_consumed = false;
|
|
1158 |
switch(key) {
|
|
1159 |
case Qt::Key_Up:
|
|
1160 |
case Qt::Key_Down:
|
|
1161 |
case Qt::Key_Enter:
|
|
1162 |
case Qt::Key_Space:
|
|
1163 |
case Qt::Key_Return: {
|
|
1164 |
if(!style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation, 0, this) || !d->currentAction)
|
|
1165 |
break;
|
|
1166 |
if(d->currentAction->menu()) {
|
|
1167 |
d->popupAction(d->currentAction, true);
|
|
1168 |
} else if(key == Qt::Key_Enter || key == Qt::Key_Return || key == Qt::Key_Space) {
|
|
1169 |
d->activateAction(d->currentAction, QAction::Trigger);
|
|
1170 |
d->setCurrentAction(d->currentAction, false);
|
|
1171 |
d->setKeyboardMode(false);
|
|
1172 |
}
|
|
1173 |
key_consumed = true;
|
|
1174 |
break; }
|
|
1175 |
|
|
1176 |
case Qt::Key_Right:
|
|
1177 |
case Qt::Key_Left: {
|
|
1178 |
if(d->currentAction) {
|
|
1179 |
int index = d->actions.indexOf(d->currentAction);
|
|
1180 |
if (QAction *nextAction = d->getNextAction(index, key == Qt::Key_Left ? -1 : +1)) {
|
|
1181 |
d->setCurrentAction(nextAction, d->popupState, true);
|
|
1182 |
key_consumed = true;
|
|
1183 |
}
|
|
1184 |
}
|
|
1185 |
break; }
|
|
1186 |
|
|
1187 |
case Qt::Key_Escape:
|
|
1188 |
d->setCurrentAction(0);
|
|
1189 |
d->setKeyboardMode(false);
|
|
1190 |
key_consumed = true;
|
|
1191 |
break;
|
|
1192 |
|
|
1193 |
default:
|
|
1194 |
key_consumed = false;
|
|
1195 |
}
|
|
1196 |
|
|
1197 |
if(!key_consumed &&
|
|
1198 |
(!e->modifiers() ||
|
|
1199 |
(e->modifiers()&(Qt::MetaModifier|Qt::AltModifier))) && e->text().length()==1 && !d->popupState) {
|
|
1200 |
int clashCount = 0;
|
|
1201 |
QAction *first = 0, *currentSelected = 0, *firstAfterCurrent = 0;
|
|
1202 |
{
|
|
1203 |
QChar c = e->text()[0].toUpper();
|
|
1204 |
for(int i = 0; i < d->actions.size(); ++i) {
|
|
1205 |
if (d->actionRects.at(i).isNull())
|
|
1206 |
continue;
|
|
1207 |
QAction *act = d->actions.at(i);
|
|
1208 |
QString s = act->text();
|
|
1209 |
if(!s.isEmpty()) {
|
|
1210 |
int ampersand = s.indexOf(QLatin1Char('&'));
|
|
1211 |
if(ampersand >= 0) {
|
|
1212 |
if(s[ampersand+1].toUpper() == c) {
|
|
1213 |
clashCount++;
|
|
1214 |
if(!first)
|
|
1215 |
first = act;
|
|
1216 |
if(act == d->currentAction)
|
|
1217 |
currentSelected = act;
|
|
1218 |
else if (!firstAfterCurrent && currentSelected)
|
|
1219 |
firstAfterCurrent = act;
|
|
1220 |
}
|
|
1221 |
}
|
|
1222 |
}
|
|
1223 |
}
|
|
1224 |
}
|
|
1225 |
QAction *next_action = 0;
|
|
1226 |
if(clashCount >= 1) {
|
|
1227 |
if(clashCount == 1 || !d->currentAction || (currentSelected && !firstAfterCurrent))
|
|
1228 |
next_action = first;
|
|
1229 |
else
|
|
1230 |
next_action = firstAfterCurrent;
|
|
1231 |
}
|
|
1232 |
if(next_action) {
|
|
1233 |
key_consumed = true;
|
|
1234 |
d->setCurrentAction(next_action, true, true);
|
|
1235 |
}
|
|
1236 |
}
|
|
1237 |
if(key_consumed)
|
|
1238 |
e->accept();
|
|
1239 |
else
|
|
1240 |
e->ignore();
|
|
1241 |
}
|
|
1242 |
|
|
1243 |
/*!
|
|
1244 |
\reimp
|
|
1245 |
*/
|
|
1246 |
void QMenuBar::mouseMoveEvent(QMouseEvent *e)
|
|
1247 |
{
|
|
1248 |
Q_D(QMenuBar);
|
|
1249 |
if (!(e->buttons() & Qt::LeftButton))
|
|
1250 |
d->mouseDown = false;
|
|
1251 |
bool popupState = d->popupState || d->mouseDown;
|
|
1252 |
QAction *action = d->actionAt(e->pos());
|
|
1253 |
if ((action && d->isVisible(action)) || !popupState)
|
|
1254 |
d->setCurrentAction(action, popupState);
|
|
1255 |
}
|
|
1256 |
|
|
1257 |
/*!
|
|
1258 |
\reimp
|
|
1259 |
*/
|
|
1260 |
void QMenuBar::leaveEvent(QEvent *)
|
|
1261 |
{
|
|
1262 |
Q_D(QMenuBar);
|
|
1263 |
if((!hasFocus() && !d->popupState) ||
|
|
1264 |
(d->currentAction && d->currentAction->menu() == 0))
|
|
1265 |
d->setCurrentAction(0);
|
|
1266 |
}
|
|
1267 |
|
|
1268 |
/*!
|
|
1269 |
\reimp
|
|
1270 |
*/
|
|
1271 |
void QMenuBar::actionEvent(QActionEvent *e)
|
|
1272 |
{
|
|
1273 |
Q_D(QMenuBar);
|
|
1274 |
d->itemsDirty = true;
|
|
1275 |
#if defined (Q_WS_MAC) || defined(Q_OS_WINCE) || defined(Q_WS_S60)
|
|
1276 |
if (isNativeMenuBar()) {
|
|
1277 |
#ifdef Q_WS_MAC
|
|
1278 |
QMenuBarPrivate::QMacMenuBarPrivate *nativeMenuBar = d->mac_menubar;
|
|
1279 |
#elif defined(Q_WS_S60)
|
|
1280 |
QMenuBarPrivate::QSymbianMenuBarPrivate *nativeMenuBar = d->symbian_menubar;
|
|
1281 |
#else
|
|
1282 |
QMenuBarPrivate::QWceMenuBarPrivate *nativeMenuBar = d->wce_menubar;
|
|
1283 |
#endif
|
|
1284 |
if (!nativeMenuBar)
|
|
1285 |
return;
|
|
1286 |
if(e->type() == QEvent::ActionAdded)
|
|
1287 |
nativeMenuBar->addAction(e->action(), nativeMenuBar->findAction(e->before()));
|
|
1288 |
else if(e->type() == QEvent::ActionRemoved)
|
|
1289 |
nativeMenuBar->removeAction(e->action());
|
|
1290 |
else if(e->type() == QEvent::ActionChanged)
|
|
1291 |
nativeMenuBar->syncAction(e->action());
|
|
1292 |
}
|
|
1293 |
#endif
|
|
1294 |
|
|
1295 |
if(e->type() == QEvent::ActionAdded) {
|
|
1296 |
connect(e->action(), SIGNAL(triggered()), this, SLOT(_q_actionTriggered()));
|
|
1297 |
connect(e->action(), SIGNAL(hovered()), this, SLOT(_q_actionHovered()));
|
|
1298 |
} else if(e->type() == QEvent::ActionRemoved) {
|
|
1299 |
e->action()->disconnect(this);
|
|
1300 |
}
|
|
1301 |
if (isVisible()) {
|
|
1302 |
d->updateGeometries();
|
|
1303 |
update();
|
|
1304 |
}
|
|
1305 |
}
|
|
1306 |
|
|
1307 |
/*!
|
|
1308 |
\reimp
|
|
1309 |
*/
|
|
1310 |
void QMenuBar::focusInEvent(QFocusEvent *)
|
|
1311 |
{
|
|
1312 |
Q_D(QMenuBar);
|
|
1313 |
if(d->keyboardState)
|
|
1314 |
d->focusFirstAction();
|
|
1315 |
}
|
|
1316 |
|
|
1317 |
/*!
|
|
1318 |
\reimp
|
|
1319 |
*/
|
|
1320 |
void QMenuBar::focusOutEvent(QFocusEvent *)
|
|
1321 |
{
|
|
1322 |
Q_D(QMenuBar);
|
|
1323 |
if(!d->popupState) {
|
|
1324 |
d->setCurrentAction(0);
|
|
1325 |
d->setKeyboardMode(false);
|
|
1326 |
}
|
|
1327 |
}
|
|
1328 |
|
|
1329 |
/*!
|
|
1330 |
\reimp
|
|
1331 |
*/
|
|
1332 |
void QMenuBar::timerEvent (QTimerEvent *e)
|
|
1333 |
{
|
|
1334 |
Q_D(QMenuBar);
|
|
1335 |
if (e->timerId() == d->autoReleaseTimer.timerId()) {
|
|
1336 |
d->autoReleaseTimer.stop();
|
|
1337 |
d->setCurrentAction(0);
|
|
1338 |
}
|
|
1339 |
QWidget::timerEvent(e);
|
|
1340 |
}
|
|
1341 |
|
|
1342 |
|
|
1343 |
void QMenuBarPrivate::handleReparent()
|
|
1344 |
{
|
|
1345 |
Q_Q(QMenuBar);
|
|
1346 |
QWidget *newParent = q->parentWidget();
|
|
1347 |
//Note: if parent is reparented, then window may change even if parent doesn't
|
|
1348 |
|
|
1349 |
// we need to install an event filter on parent, and remove the old one
|
|
1350 |
|
|
1351 |
if (oldParent != newParent) {
|
|
1352 |
if (oldParent)
|
|
1353 |
oldParent->removeEventFilter(q);
|
|
1354 |
if (newParent)
|
|
1355 |
newParent->installEventFilter(q);
|
|
1356 |
}
|
|
1357 |
|
|
1358 |
//we also need event filter on top-level (for shortcuts)
|
|
1359 |
QWidget *newWindow = newParent ? newParent->window() : 0;
|
|
1360 |
|
|
1361 |
if (oldWindow != newWindow) {
|
|
1362 |
if (oldParent && oldParent != oldWindow)
|
|
1363 |
oldWindow->removeEventFilter(q);
|
|
1364 |
|
|
1365 |
if (newParent && newParent != newWindow)
|
|
1366 |
newWindow->installEventFilter(q);
|
|
1367 |
}
|
|
1368 |
|
|
1369 |
oldParent = newParent;
|
|
1370 |
oldWindow = newWindow;
|
|
1371 |
|
|
1372 |
#ifdef Q_WS_MAC
|
|
1373 |
if (q->isNativeMenuBar() && !macWidgetHasNativeMenubar(newParent)) {
|
|
1374 |
// If the new parent got a native menubar from before, keep that
|
|
1375 |
// menubar rather than replace it with this one (because a parents
|
|
1376 |
// menubar has precedence over children menubars).
|
|
1377 |
macDestroyMenuBar();
|
|
1378 |
macCreateMenuBar(newParent);
|
|
1379 |
}
|
|
1380 |
#endif
|
|
1381 |
|
|
1382 |
#ifdef Q_WS_WINCE
|
|
1383 |
if (qt_wince_is_mobile() && wce_menubar)
|
|
1384 |
wce_menubar->rebuild();
|
|
1385 |
#endif
|
|
1386 |
#ifdef Q_WS_S60
|
|
1387 |
if (symbian_menubar)
|
|
1388 |
symbian_menubar->rebuild();
|
|
1389 |
#endif
|
|
1390 |
|
|
1391 |
}
|
|
1392 |
|
|
1393 |
#ifdef QT3_SUPPORT
|
|
1394 |
/*!
|
|
1395 |
Sets whether the menu bar should automatically resize itself
|
|
1396 |
when its parent widget is resized.
|
|
1397 |
|
|
1398 |
This feature is provided to help porting to Qt 4. We recommend
|
|
1399 |
against using it in new code.
|
|
1400 |
|
|
1401 |
\sa autoGeometry()
|
|
1402 |
*/
|
|
1403 |
void QMenuBar::setAutoGeometry(bool b)
|
|
1404 |
{
|
|
1405 |
Q_D(QMenuBar);
|
|
1406 |
d->doAutoResize = b;
|
|
1407 |
}
|
|
1408 |
|
|
1409 |
/*!
|
|
1410 |
Returns true if the menu bar automatically resizes itself
|
|
1411 |
when its parent widget is resized; otherwise returns false.
|
|
1412 |
|
|
1413 |
This feature is provided to help porting to Qt 4. We recommend
|
|
1414 |
against using it in new code.
|
|
1415 |
|
|
1416 |
\sa setAutoGeometry()
|
|
1417 |
*/
|
|
1418 |
bool QMenuBar::autoGeometry() const
|
|
1419 |
{
|
|
1420 |
Q_D(const QMenuBar);
|
|
1421 |
return d->doAutoResize;
|
|
1422 |
}
|
|
1423 |
#endif
|
|
1424 |
|
|
1425 |
/*!
|
|
1426 |
\reimp
|
|
1427 |
*/
|
|
1428 |
void QMenuBar::changeEvent(QEvent *e)
|
|
1429 |
{
|
|
1430 |
Q_D(QMenuBar);
|
|
1431 |
if(e->type() == QEvent::StyleChange) {
|
|
1432 |
d->itemsDirty = true;
|
|
1433 |
setMouseTracking(style()->styleHint(QStyle::SH_MenuBar_MouseTracking, 0, this));
|
|
1434 |
if(parentWidget())
|
|
1435 |
resize(parentWidget()->width(), heightForWidth(parentWidget()->width()));
|
|
1436 |
d->updateGeometries();
|
|
1437 |
} else if (e->type() == QEvent::ParentChange) {
|
|
1438 |
d->handleReparent();
|
|
1439 |
} else if (e->type() == QEvent::FontChange
|
|
1440 |
|| e->type() == QEvent::ApplicationFontChange) {
|
|
1441 |
d->itemsDirty = true;
|
|
1442 |
d->updateGeometries();
|
|
1443 |
}
|
|
1444 |
QWidget::changeEvent(e);
|
|
1445 |
}
|
|
1446 |
|
|
1447 |
/*!
|
|
1448 |
\reimp
|
|
1449 |
*/
|
|
1450 |
bool QMenuBar::event(QEvent *e)
|
|
1451 |
{
|
|
1452 |
Q_D(QMenuBar);
|
|
1453 |
switch (e->type()) {
|
|
1454 |
case QEvent::KeyPress: {
|
|
1455 |
QKeyEvent *ke = (QKeyEvent*)e;
|
|
1456 |
#if 0
|
|
1457 |
if(!d->keyboardState) { //all keypresses..
|
|
1458 |
d->setCurrentAction(0);
|
|
1459 |
return ;
|
|
1460 |
}
|
|
1461 |
#endif
|
|
1462 |
if(ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
|
|
1463 |
keyPressEvent(ke);
|
|
1464 |
return true;
|
|
1465 |
}
|
|
1466 |
|
|
1467 |
} break;
|
|
1468 |
#ifndef QT_NO_SHORTCUT
|
|
1469 |
case QEvent::Shortcut: {
|
|
1470 |
QShortcutEvent *se = static_cast<QShortcutEvent *>(e);
|
|
1471 |
int shortcutId = se->shortcutId();
|
|
1472 |
for(int j = 0; j < d->shortcutIndexMap.size(); ++j) {
|
|
1473 |
if (shortcutId == d->shortcutIndexMap.value(j))
|
|
1474 |
d->_q_internalShortcutActivated(j);
|
|
1475 |
}
|
|
1476 |
} break;
|
|
1477 |
#endif
|
|
1478 |
case QEvent::Show:
|
|
1479 |
#ifdef QT3_SUPPORT
|
|
1480 |
if(QWidget *p = parentWidget()) {
|
|
1481 |
// If itemsDirty == true, updateGeometries sends the MenubarUpdated event.
|
|
1482 |
if (!d->itemsDirty) {
|
|
1483 |
QMenubarUpdatedEvent menubarUpdated(this);
|
|
1484 |
QApplication::sendEvent(p, &menubarUpdated);
|
|
1485 |
}
|
|
1486 |
}
|
|
1487 |
#endif
|
|
1488 |
d->_q_updateLayout();
|
|
1489 |
break;
|
|
1490 |
case QEvent::ShortcutOverride: {
|
|
1491 |
QKeyEvent *kev = static_cast<QKeyEvent*>(e);
|
|
1492 |
if (kev->key() == Qt::Key_Escape) {
|
|
1493 |
e->accept();
|
|
1494 |
return true;
|
|
1495 |
}
|
|
1496 |
}
|
|
1497 |
break;
|
|
1498 |
|
|
1499 |
#ifdef QT3_SUPPORT
|
|
1500 |
case QEvent::Hide: {
|
|
1501 |
if(QWidget *p = parentWidget()) {
|
|
1502 |
QMenubarUpdatedEvent menubarUpdated(this);
|
|
1503 |
QApplication::sendEvent(p, &menubarUpdated);
|
|
1504 |
}
|
|
1505 |
} break;
|
|
1506 |
#endif
|
|
1507 |
|
|
1508 |
#ifndef QT_NO_WHATSTHIS
|
|
1509 |
case QEvent::QueryWhatsThis:
|
|
1510 |
e->setAccepted(d->whatsThis.size());
|
|
1511 |
if (QAction *action = d->actionAt(static_cast<QHelpEvent*>(e)->pos())) {
|
|
1512 |
if (action->whatsThis().size() || action->menu())
|
|
1513 |
e->accept();
|
|
1514 |
}
|
|
1515 |
return true;
|
|
1516 |
#endif
|
|
1517 |
case QEvent::LayoutDirectionChange:
|
|
1518 |
d->_q_updateLayout();
|
|
1519 |
break;
|
|
1520 |
default:
|
|
1521 |
break;
|
|
1522 |
}
|
|
1523 |
return QWidget::event(e);
|
|
1524 |
}
|
|
1525 |
|
|
1526 |
/*!
|
|
1527 |
\reimp
|
|
1528 |
*/
|
|
1529 |
bool QMenuBar::eventFilter(QObject *object, QEvent *event)
|
|
1530 |
{
|
|
1531 |
Q_D(QMenuBar);
|
|
1532 |
if (object == parent() && object) {
|
|
1533 |
#ifdef QT3_SUPPORT
|
|
1534 |
if (d->doAutoResize && event->type() == QEvent::Resize) {
|
|
1535 |
QResizeEvent *e = (QResizeEvent *)event;
|
|
1536 |
int w = e->size().width();
|
|
1537 |
setGeometry(0, y(), w, heightForWidth(w));
|
|
1538 |
return false;
|
|
1539 |
}
|
|
1540 |
#endif
|
|
1541 |
if (event->type() == QEvent::ParentChange) //GrandparentChange
|
|
1542 |
d->handleReparent();
|
|
1543 |
}
|
|
1544 |
if (object == d->leftWidget || object == d->rightWidget) {
|
|
1545 |
switch (event->type()) {
|
|
1546 |
case QEvent::ShowToParent:
|
|
1547 |
case QEvent::HideToParent:
|
|
1548 |
d->_q_updateLayout();
|
|
1549 |
break;
|
|
1550 |
default:
|
|
1551 |
break;
|
|
1552 |
}
|
|
1553 |
}
|
|
1554 |
|
|
1555 |
if (style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation, 0, this)) {
|
|
1556 |
if (d->altPressed) {
|
|
1557 |
switch (event->type()) {
|
|
1558 |
case QEvent::KeyPress:
|
|
1559 |
case QEvent::KeyRelease:
|
|
1560 |
{
|
|
1561 |
QKeyEvent *kev = static_cast<QKeyEvent*>(event);
|
|
1562 |
if (kev->key() == Qt::Key_Alt || kev->key() == Qt::Key_Meta) {
|
|
1563 |
if (event->type() == QEvent::KeyPress) // Alt-press does not interest us, we have the shortcut-override event
|
|
1564 |
break;
|
|
1565 |
d->setKeyboardMode(!d->keyboardState);
|
|
1566 |
}
|
|
1567 |
}
|
|
1568 |
// fall through
|
|
1569 |
case QEvent::MouseButtonPress:
|
|
1570 |
case QEvent::MouseButtonRelease:
|
|
1571 |
case QEvent::MouseMove:
|
|
1572 |
case QEvent::FocusIn:
|
|
1573 |
case QEvent::FocusOut:
|
|
1574 |
case QEvent::ActivationChange:
|
|
1575 |
d->altPressed = false;
|
|
1576 |
qApp->removeEventFilter(this);
|
|
1577 |
break;
|
|
1578 |
default:
|
|
1579 |
break;
|
|
1580 |
}
|
|
1581 |
} else if (isVisible()) {
|
|
1582 |
if (event->type() == QEvent::ShortcutOverride) {
|
|
1583 |
QKeyEvent *kev = static_cast<QKeyEvent*>(event);
|
|
1584 |
if ((kev->key() == Qt::Key_Alt || kev->key() == Qt::Key_Meta)
|
|
1585 |
&& kev->modifiers() == Qt::AltModifier) {
|
|
1586 |
d->altPressed = true;
|
|
1587 |
qApp->installEventFilter(this);
|
|
1588 |
}
|
|
1589 |
}
|
|
1590 |
}
|
|
1591 |
}
|
|
1592 |
|
|
1593 |
return false;
|
|
1594 |
}
|
|
1595 |
|
|
1596 |
/*!
|
|
1597 |
\internal
|
|
1598 |
|
|
1599 |
Return the item at \a pt, or 0 if there is no item there or if it is
|
|
1600 |
a separator item.
|
|
1601 |
*/
|
|
1602 |
QAction *QMenuBar::actionAt(const QPoint &pt) const
|
|
1603 |
{
|
|
1604 |
Q_D(const QMenuBar);
|
|
1605 |
return d->actionAt(pt);
|
|
1606 |
}
|
|
1607 |
|
|
1608 |
/*!
|
|
1609 |
\internal
|
|
1610 |
|
|
1611 |
Returns the geometry of action \a act.
|
|
1612 |
*/
|
|
1613 |
QRect QMenuBar::actionGeometry(QAction *act) const
|
|
1614 |
{
|
|
1615 |
Q_D(const QMenuBar);
|
|
1616 |
return d->actionRect(act);
|
|
1617 |
}
|
|
1618 |
|
|
1619 |
/*!
|
|
1620 |
\reimp
|
|
1621 |
*/
|
|
1622 |
QSize QMenuBar::minimumSizeHint() const
|
|
1623 |
{
|
|
1624 |
Q_D(const QMenuBar);
|
|
1625 |
#if defined(Q_WS_MAC) || defined(Q_WS_WINCE) || defined(Q_WS_S60)
|
|
1626 |
const bool as_gui_menubar = !isNativeMenuBar();
|
|
1627 |
#else
|
|
1628 |
const bool as_gui_menubar = true;
|
|
1629 |
#endif
|
|
1630 |
|
|
1631 |
ensurePolished();
|
|
1632 |
QSize ret(0, 0);
|
|
1633 |
const_cast<QMenuBarPrivate*>(d)->updateGeometries();
|
|
1634 |
const int hmargin = style()->pixelMetric(QStyle::PM_MenuBarHMargin, 0, this);
|
|
1635 |
const int vmargin = style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, this);
|
|
1636 |
int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, this);
|
|
1637 |
int spaceBelowMenuBar = style()->styleHint(QStyle::SH_MainWindow_SpaceBelowMenuBar, 0, this);
|
|
1638 |
if(as_gui_menubar) {
|
|
1639 |
int w = parentWidget() ? parentWidget()->width() : QApplication::desktop()->width();
|
|
1640 |
d->calcActionRects(w - (2 * fw), 0);
|
|
1641 |
for (int i = 0; ret.isNull() && i < d->actions.count(); ++i)
|
|
1642 |
ret = d->actionRects.at(i).size();
|
|
1643 |
if (!d->extension->isHidden())
|
|
1644 |
ret += QSize(d->extension->sizeHint().width(), 0);
|
|
1645 |
ret += QSize(2*fw + hmargin, 2*fw + vmargin);
|
|
1646 |
}
|
|
1647 |
int margin = 2*vmargin + 2*fw + spaceBelowMenuBar;
|
|
1648 |
if(d->leftWidget) {
|
|
1649 |
QSize sz = d->leftWidget->minimumSizeHint();
|
|
1650 |
ret.setWidth(ret.width() + sz.width());
|
|
1651 |
if(sz.height() + margin > ret.height())
|
|
1652 |
ret.setHeight(sz.height() + margin);
|
|
1653 |
}
|
|
1654 |
if(d->rightWidget) {
|
|
1655 |
QSize sz = d->rightWidget->minimumSizeHint();
|
|
1656 |
ret.setWidth(ret.width() + sz.width());
|
|
1657 |
if(sz.height() + margin > ret.height())
|
|
1658 |
ret.setHeight(sz.height() + margin);
|
|
1659 |
}
|
|
1660 |
if(as_gui_menubar) {
|
|
1661 |
QStyleOptionMenuItem opt;
|
|
1662 |
opt.rect = rect();
|
|
1663 |
opt.menuRect = rect();
|
|
1664 |
opt.state = QStyle::State_None;
|
|
1665 |
opt.menuItemType = QStyleOptionMenuItem::Normal;
|
|
1666 |
opt.checkType = QStyleOptionMenuItem::NotCheckable;
|
|
1667 |
opt.palette = palette();
|
|
1668 |
return (style()->sizeFromContents(QStyle::CT_MenuBar, &opt,
|
|
1669 |
ret.expandedTo(QApplication::globalStrut()),
|
|
1670 |
this));
|
|
1671 |
}
|
|
1672 |
return ret;
|
|
1673 |
}
|
|
1674 |
|
|
1675 |
/*!
|
|
1676 |
\reimp
|
|
1677 |
*/
|
|
1678 |
QSize QMenuBar::sizeHint() const
|
|
1679 |
{
|
|
1680 |
Q_D(const QMenuBar);
|
|
1681 |
#if defined(Q_WS_MAC) || defined(Q_WS_WINCE) || defined(Q_WS_S60)
|
|
1682 |
const bool as_gui_menubar = !isNativeMenuBar();
|
|
1683 |
#else
|
|
1684 |
const bool as_gui_menubar = true;
|
|
1685 |
#endif
|
|
1686 |
|
|
1687 |
|
|
1688 |
ensurePolished();
|
|
1689 |
QSize ret(0, 0);
|
|
1690 |
const_cast<QMenuBarPrivate*>(d)->updateGeometries();
|
|
1691 |
const int hmargin = style()->pixelMetric(QStyle::PM_MenuBarHMargin, 0, this);
|
|
1692 |
const int vmargin = style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, this);
|
|
1693 |
int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, this);
|
|
1694 |
int spaceBelowMenuBar = style()->styleHint(QStyle::SH_MainWindow_SpaceBelowMenuBar, 0, this);
|
|
1695 |
if(as_gui_menubar) {
|
|
1696 |
const int w = parentWidget() ? parentWidget()->width() : QApplication::desktop()->width();
|
|
1697 |
d->calcActionRects(w - (2 * fw), 0);
|
|
1698 |
for (int i = 0; i < d->actionRects.count(); ++i) {
|
|
1699 |
const QRect &actionRect = d->actionRects.at(i);
|
|
1700 |
ret = ret.expandedTo(QSize(actionRect.x() + actionRect.width(), actionRect.y() + actionRect.height()));
|
|
1701 |
}
|
|
1702 |
//the action geometries already contain the top and left
|
|
1703 |
//margins. So we only need to add those from right and bottom.
|
|
1704 |
ret += QSize(fw + hmargin, fw + vmargin);
|
|
1705 |
}
|
|
1706 |
int margin = 2*vmargin + 2*fw + spaceBelowMenuBar;
|
|
1707 |
if(d->leftWidget) {
|
|
1708 |
QSize sz = d->leftWidget->sizeHint();
|
|
1709 |
ret.setWidth(ret.width() + sz.width());
|
|
1710 |
if(sz.height() + margin > ret.height())
|
|
1711 |
ret.setHeight(sz.height() + margin);
|
|
1712 |
}
|
|
1713 |
if(d->rightWidget) {
|
|
1714 |
QSize sz = d->rightWidget->sizeHint();
|
|
1715 |
ret.setWidth(ret.width() + sz.width());
|
|
1716 |
if(sz.height() + margin > ret.height())
|
|
1717 |
ret.setHeight(sz.height() + margin);
|
|
1718 |
}
|
|
1719 |
if(as_gui_menubar) {
|
|
1720 |
QStyleOptionMenuItem opt;
|
|
1721 |
opt.rect = rect();
|
|
1722 |
opt.menuRect = rect();
|
|
1723 |
opt.state = QStyle::State_None;
|
|
1724 |
opt.menuItemType = QStyleOptionMenuItem::Normal;
|
|
1725 |
opt.checkType = QStyleOptionMenuItem::NotCheckable;
|
|
1726 |
opt.palette = palette();
|
|
1727 |
return (style()->sizeFromContents(QStyle::CT_MenuBar, &opt,
|
|
1728 |
ret.expandedTo(QApplication::globalStrut()),
|
|
1729 |
this));
|
|
1730 |
}
|
|
1731 |
return ret;
|
|
1732 |
}
|
|
1733 |
|
|
1734 |
/*!
|
|
1735 |
\reimp
|
|
1736 |
*/
|
|
1737 |
int QMenuBar::heightForWidth(int) const
|
|
1738 |
{
|
|
1739 |
Q_D(const QMenuBar);
|
|
1740 |
#if defined(Q_WS_MAC) || defined(Q_WS_WINCE) || defined(Q_WS_S60)
|
|
1741 |
const bool as_gui_menubar = !isNativeMenuBar();
|
|
1742 |
#else
|
|
1743 |
const bool as_gui_menubar = true;
|
|
1744 |
#endif
|
|
1745 |
|
|
1746 |
const_cast<QMenuBarPrivate*>(d)->updateGeometries();
|
|
1747 |
int height = 0;
|
|
1748 |
const int vmargin = style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, this);
|
|
1749 |
int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, this);
|
|
1750 |
int spaceBelowMenuBar = style()->styleHint(QStyle::SH_MainWindow_SpaceBelowMenuBar, 0, this);
|
|
1751 |
if(as_gui_menubar) {
|
|
1752 |
for (int i = 0; i < d->actionRects.count(); ++i)
|
|
1753 |
height = qMax(height, d->actionRects.at(i).height());
|
|
1754 |
if (height) //there is at least one non-null item
|
|
1755 |
height += spaceBelowMenuBar;
|
|
1756 |
height += 2*fw;
|
|
1757 |
height += 2*vmargin;
|
|
1758 |
}
|
|
1759 |
int margin = 2*vmargin + 2*fw + spaceBelowMenuBar;
|
|
1760 |
if(d->leftWidget)
|
|
1761 |
height = qMax(d->leftWidget->sizeHint().height() + margin, height);
|
|
1762 |
if(d->rightWidget)
|
|
1763 |
height = qMax(d->rightWidget->sizeHint().height() + margin, height);
|
|
1764 |
if(as_gui_menubar) {
|
|
1765 |
QStyleOptionMenuItem opt;
|
|
1766 |
opt.init(this);
|
|
1767 |
opt.menuRect = rect();
|
|
1768 |
opt.state = QStyle::State_None;
|
|
1769 |
opt.menuItemType = QStyleOptionMenuItem::Normal;
|
|
1770 |
opt.checkType = QStyleOptionMenuItem::NotCheckable;
|
|
1771 |
return style()->sizeFromContents(QStyle::CT_MenuBar, &opt, QSize(0, height), this).height(); //not pretty..
|
|
1772 |
}
|
|
1773 |
return height;
|
|
1774 |
}
|
|
1775 |
|
|
1776 |
/*!
|
|
1777 |
\internal
|
|
1778 |
*/
|
|
1779 |
void QMenuBarPrivate::_q_internalShortcutActivated(int id)
|
|
1780 |
{
|
|
1781 |
Q_Q(QMenuBar);
|
|
1782 |
QAction *act = actions.at(id);
|
|
1783 |
setCurrentAction(act, true, true);
|
|
1784 |
if (act && !act->menu()) {
|
|
1785 |
activateAction(act, QAction::Trigger);
|
|
1786 |
//100 is the same as the default value in QPushButton::animateClick
|
|
1787 |
autoReleaseTimer.start(100, q);
|
|
1788 |
} else if (act && q->style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation, 0, q)) {
|
|
1789 |
// When we open a menu using a shortcut, we should end up in keyboard state
|
|
1790 |
setKeyboardMode(true);
|
|
1791 |
}
|
|
1792 |
}
|
|
1793 |
|
|
1794 |
void QMenuBarPrivate::_q_updateLayout()
|
|
1795 |
{
|
|
1796 |
Q_Q(QMenuBar);
|
|
1797 |
itemsDirty = true;
|
|
1798 |
if (q->isVisible()) {
|
|
1799 |
updateGeometries();
|
|
1800 |
q->update();
|
|
1801 |
}
|
|
1802 |
}
|
|
1803 |
|
|
1804 |
/*!
|
|
1805 |
\internal
|
|
1806 |
|
|
1807 |
This sets widget \a w to be shown directly on the left of the first or
|
|
1808 |
the right of the last menu item, depending on \a corner.
|
|
1809 |
*/
|
|
1810 |
void QMenuBar::setCornerWidget(QWidget *w, Qt::Corner corner)
|
|
1811 |
{
|
|
1812 |
Q_D(QMenuBar);
|
|
1813 |
switch (corner) {
|
|
1814 |
case Qt::TopLeftCorner:
|
|
1815 |
if (d->leftWidget)
|
|
1816 |
d->leftWidget->removeEventFilter(this);
|
|
1817 |
d->leftWidget = w;
|
|
1818 |
break;
|
|
1819 |
case Qt::TopRightCorner:
|
|
1820 |
if (d->rightWidget)
|
|
1821 |
d->rightWidget->removeEventFilter(this);
|
|
1822 |
d->rightWidget = w;
|
|
1823 |
break;
|
|
1824 |
default:
|
|
1825 |
qWarning("QMenuBar::setCornerWidget: Only TopLeftCorner and TopRightCorner are supported");
|
|
1826 |
return;
|
|
1827 |
}
|
|
1828 |
|
|
1829 |
if (w) {
|
|
1830 |
w->setParent(this);
|
|
1831 |
w->installEventFilter(this);
|
|
1832 |
}
|
|
1833 |
|
|
1834 |
d->_q_updateLayout();
|
|
1835 |
}
|
|
1836 |
|
|
1837 |
/*!
|
|
1838 |
\internal
|
|
1839 |
|
|
1840 |
Returns the widget in the left of the first or the right of the last menu
|
|
1841 |
item, depending on \a corner.
|
|
1842 |
*/
|
|
1843 |
QWidget *QMenuBar::cornerWidget(Qt::Corner corner) const
|
|
1844 |
{
|
|
1845 |
Q_D(const QMenuBar);
|
|
1846 |
QWidget *w = 0;
|
|
1847 |
switch(corner) {
|
|
1848 |
case Qt::TopLeftCorner:
|
|
1849 |
w = d->leftWidget;
|
|
1850 |
break;
|
|
1851 |
case Qt::TopRightCorner:
|
|
1852 |
w = d->rightWidget;
|
|
1853 |
break;
|
|
1854 |
default:
|
|
1855 |
qWarning("QMenuBar::cornerWidget: Only TopLeftCorner and TopRightCorner are supported");
|
|
1856 |
break;
|
|
1857 |
}
|
|
1858 |
|
|
1859 |
return w;
|
|
1860 |
}
|
|
1861 |
|
|
1862 |
/*!
|
|
1863 |
\property QMenuBar::nativeMenuBar
|
|
1864 |
\brief Whether or not a menubar will be used as a native menubar on platforms that support it
|
|
1865 |
\since 4.6
|
|
1866 |
|
|
1867 |
This property specifies whether or not the menubar should be used as a native menubar on platforms
|
|
1868 |
that support it. The currently supported platforms are Mac OS X and Windows CE. On these platforms
|
|
1869 |
if this property is true, the menubar is used in the native menubar and is not in the window of
|
|
1870 |
its parent, if false the menubar remains in the window. On other platforms the value of this
|
|
1871 |
attribute has no effect.
|
|
1872 |
|
|
1873 |
The default is to follow whether the Qt::AA_DontUseNativeMenuBar attribute
|
|
1874 |
is set for the application. Explicitly settings this property overrides
|
|
1875 |
the presence (or abscence) of the attribute.
|
|
1876 |
*/
|
|
1877 |
|
|
1878 |
void QMenuBar::setNativeMenuBar(bool nativeMenuBar)
|
|
1879 |
{
|
|
1880 |
Q_D(QMenuBar);
|
|
1881 |
if (d->nativeMenuBar == -1 || (nativeMenuBar != bool(d->nativeMenuBar))) {
|
|
1882 |
d->nativeMenuBar = nativeMenuBar;
|
|
1883 |
#ifdef Q_WS_MAC
|
|
1884 |
if (!d->nativeMenuBar) {
|
|
1885 |
extern void qt_mac_clear_menubar();
|
|
1886 |
qt_mac_clear_menubar();
|
|
1887 |
d->macDestroyMenuBar();
|
|
1888 |
const QList<QAction *> &menubarActions = actions();
|
|
1889 |
for (int i = 0; i < menubarActions.size(); ++i) {
|
|
1890 |
const QAction *action = menubarActions.at(i);
|
|
1891 |
if (QMenu *menu = action->menu()) {
|
|
1892 |
delete menu->d_func()->mac_menu;
|
|
1893 |
menu->d_func()->mac_menu = 0;
|
|
1894 |
}
|
|
1895 |
}
|
|
1896 |
} else {
|
|
1897 |
d->macCreateMenuBar(parentWidget());
|
|
1898 |
}
|
|
1899 |
macUpdateMenuBar();
|
|
1900 |
updateGeometry();
|
|
1901 |
setVisible(false);
|
|
1902 |
setVisible(true);
|
|
1903 |
#endif
|
|
1904 |
}
|
|
1905 |
}
|
|
1906 |
|
|
1907 |
bool QMenuBar::isNativeMenuBar() const
|
|
1908 |
{
|
|
1909 |
Q_D(const QMenuBar);
|
|
1910 |
if (d->nativeMenuBar == -1) {
|
|
1911 |
return !QApplication::instance()->testAttribute(Qt::AA_DontUseNativeMenuBar);
|
|
1912 |
}
|
|
1913 |
return d->nativeMenuBar;
|
|
1914 |
}
|
|
1915 |
|
|
1916 |
/*!
|
|
1917 |
\since 4.4
|
|
1918 |
|
|
1919 |
Sets the default action to \a act.
|
|
1920 |
|
|
1921 |
The default action is assigned to the left soft key. The menu is assigned
|
|
1922 |
to the right soft key.
|
|
1923 |
|
|
1924 |
Currently there is only support for the default action on Windows
|
|
1925 |
Mobile. All other platforms ignore the default action.
|
|
1926 |
|
|
1927 |
\sa defaultAction()
|
|
1928 |
*/
|
|
1929 |
|
|
1930 |
#ifdef Q_WS_WINCE
|
|
1931 |
void QMenuBar::setDefaultAction(QAction *act)
|
|
1932 |
{
|
|
1933 |
Q_D(QMenuBar);
|
|
1934 |
if (d->defaultAction == act)
|
|
1935 |
return;
|
|
1936 |
#ifdef Q_WS_WINCE
|
|
1937 |
if (qt_wince_is_mobile())
|
|
1938 |
if (d->defaultAction) {
|
|
1939 |
disconnect(d->defaultAction, SIGNAL(changed()), this, SLOT(_q_updateDefaultAction()));
|
|
1940 |
disconnect(d->defaultAction, SIGNAL(destroyed ()), this, SLOT(_q_updateDefaultAction()));
|
|
1941 |
}
|
|
1942 |
#endif
|
|
1943 |
d->defaultAction = act;
|
|
1944 |
#ifdef Q_WS_WINCE
|
|
1945 |
if (qt_wince_is_mobile())
|
|
1946 |
if (d->defaultAction) {
|
|
1947 |
connect(d->defaultAction, SIGNAL(changed()), this, SLOT(_q_updateDefaultAction()));
|
|
1948 |
connect(d->defaultAction, SIGNAL(destroyed()), this, SLOT(_q_updateDefaultAction()));
|
|
1949 |
}
|
|
1950 |
if (d->wce_menubar) {
|
|
1951 |
d->wce_menubar->rebuild();
|
|
1952 |
}
|
|
1953 |
#endif
|
|
1954 |
}
|
|
1955 |
|
|
1956 |
/*!
|
|
1957 |
\since 4.4
|
|
1958 |
|
|
1959 |
Returns the current default action.
|
|
1960 |
|
|
1961 |
\sa setDefaultAction()
|
|
1962 |
*/
|
|
1963 |
QAction *QMenuBar::defaultAction() const
|
|
1964 |
{
|
|
1965 |
return d_func()->defaultAction;
|
|
1966 |
}
|
|
1967 |
#endif
|
|
1968 |
|
|
1969 |
/*!
|
|
1970 |
\fn void QMenuBar::triggered(QAction *action)
|
|
1971 |
|
|
1972 |
This signal is emitted when an action in a menu belonging to this menubar
|
|
1973 |
is triggered as a result of a mouse click; \a action is the action that
|
|
1974 |
caused the signal to be emitted.
|
|
1975 |
|
|
1976 |
Normally, you connect each menu action to a single slot using
|
|
1977 |
QAction::triggered(), but sometimes you will want to connect
|
|
1978 |
several items to a single slot (most often if the user selects
|
|
1979 |
from an array). This signal is useful in such cases.
|
|
1980 |
|
|
1981 |
\sa hovered(), QAction::triggered()
|
|
1982 |
*/
|
|
1983 |
|
|
1984 |
/*!
|
|
1985 |
\fn void QMenuBar::hovered(QAction *action)
|
|
1986 |
|
|
1987 |
This signal is emitted when a menu action is highlighted; \a action
|
|
1988 |
is the action that caused the event to be sent.
|
|
1989 |
|
|
1990 |
Often this is used to update status information.
|
|
1991 |
|
|
1992 |
\sa triggered(), QAction::hovered()
|
|
1993 |
*/
|
|
1994 |
|
|
1995 |
|
|
1996 |
#ifdef QT3_SUPPORT
|
|
1997 |
/*!
|
|
1998 |
Use style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, this)
|
|
1999 |
instead.
|
|
2000 |
*/
|
|
2001 |
int QMenuBar::frameWidth() const
|
|
2002 |
{
|
|
2003 |
return style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, this);
|
|
2004 |
}
|
|
2005 |
|
|
2006 |
int QMenuBar::insertAny(const QIcon *icon, const QString *text, const QObject *receiver, const char *member,
|
|
2007 |
const QKeySequence *shortcut, const QMenu *popup, int id, int index)
|
|
2008 |
{
|
|
2009 |
QAction *act = popup ? popup->menuAction() : new QAction(this);
|
|
2010 |
if(id != -1)
|
|
2011 |
static_cast<QMenuItem*>(act)->setId(id);
|
|
2012 |
if(icon)
|
|
2013 |
act->setIcon(*icon);
|
|
2014 |
if(text)
|
|
2015 |
act->setText(*text);
|
|
2016 |
if(shortcut)
|
|
2017 |
act->setShortcut(*shortcut);
|
|
2018 |
if(receiver && member)
|
|
2019 |
QObject::connect(act, SIGNAL(triggered(bool)), receiver, member);
|
|
2020 |
if(index == -1 || index >= actions().count())
|
|
2021 |
addAction(act);
|
|
2022 |
else
|
|
2023 |
insertAction(actions().value(index), act);
|
|
2024 |
return findIdForAction(act);
|
|
2025 |
}
|
|
2026 |
|
|
2027 |
/*!
|
|
2028 |
\since 4.2
|
|
2029 |
|
|
2030 |
Use addSeparator() or insertAction() instead.
|
|
2031 |
|
|
2032 |
\oldcode
|
|
2033 |
menuBar->insertSeparator();
|
|
2034 |
\newcode
|
|
2035 |
menuBar->addSeparator();
|
|
2036 |
\endcode
|
|
2037 |
*/
|
|
2038 |
int QMenuBar::insertSeparator(int index)
|
|
2039 |
{
|
|
2040 |
QAction *act = new QAction(this);
|
|
2041 |
act->setSeparator(true);
|
|
2042 |
if(index == -1 || index >= actions().count())
|
|
2043 |
addAction(act);
|
|
2044 |
else
|
|
2045 |
insertAction(actions().value(index), act);
|
|
2046 |
return findIdForAction(act);
|
|
2047 |
}
|
|
2048 |
|
|
2049 |
/*!
|
|
2050 |
Use QAction::setData() instead.
|
|
2051 |
*/
|
|
2052 |
bool QMenuBar::setItemParameter(int id, int param)
|
|
2053 |
{
|
|
2054 |
if(QAction *act = findActionForId(id)) {
|
|
2055 |
act->d_func()->param = param;
|
|
2056 |
return true;
|
|
2057 |
}
|
|
2058 |
return false;
|
|
2059 |
}
|
|
2060 |
|
|
2061 |
/*!
|
|
2062 |
Use QAction::data() instead.
|
|
2063 |
*/
|
|
2064 |
int QMenuBar::itemParameter(int id) const
|
|
2065 |
{
|
|
2066 |
if(QAction *act = findActionForId(id))
|
|
2067 |
return act->d_func()->param;
|
|
2068 |
return id;
|
|
2069 |
}
|
|
2070 |
|
|
2071 |
QAction *QMenuBar::findActionForId(int id) const
|
|
2072 |
{
|
|
2073 |
QList<QAction *> list = actions();
|
|
2074 |
for (int i = 0; i < list.size(); ++i) {
|
|
2075 |
QAction *act = list.at(i);
|
|
2076 |
if (findIdForAction(act) == id)
|
|
2077 |
return act;
|
|
2078 |
}
|
|
2079 |
return 0;
|
|
2080 |
}
|
|
2081 |
|
|
2082 |
int QMenuBar::findIdForAction(QAction *act) const
|
|
2083 |
{
|
|
2084 |
Q_ASSERT(act);
|
|
2085 |
return act->d_func()->id;
|
|
2086 |
}
|
|
2087 |
#endif
|
|
2088 |
|
|
2089 |
/*!
|
|
2090 |
\enum QMenuBar::Separator
|
|
2091 |
|
|
2092 |
\compat
|
|
2093 |
|
|
2094 |
\value Never
|
|
2095 |
\value InWindowsStyle
|
|
2096 |
|
|
2097 |
*/
|
|
2098 |
|
|
2099 |
/*!
|
|
2100 |
\fn void QMenuBar::addAction(QAction *action)
|
|
2101 |
\overload
|
|
2102 |
|
|
2103 |
Appends the action \a action to the menu bar's list of actions.
|
|
2104 |
|
|
2105 |
\sa QMenu::addAction(), QWidget::addAction(), QWidget::actions()
|
|
2106 |
*/
|
|
2107 |
|
|
2108 |
/*!
|
|
2109 |
\fn uint QMenuBar::count() const
|
|
2110 |
|
|
2111 |
Use actions().count() instead.
|
|
2112 |
*/
|
|
2113 |
|
|
2114 |
/*!
|
|
2115 |
\fn int QMenuBar::insertItem(const QString &text, const QObject *receiver, const char* member, const QKeySequence& shortcut, int id, int index)
|
|
2116 |
|
|
2117 |
Use one of the insertAction() or addAction() overloads instead.
|
|
2118 |
*/
|
|
2119 |
|
|
2120 |
/*!
|
|
2121 |
\fn int QMenuBar::insertItem(const QIcon& icon, const QString &text, const QObject *receiver, const char* member, const QKeySequence& shortcut, int id, int index)
|
|
2122 |
|
|
2123 |
Use one of the insertAction() or addAction() overloads instead.
|
|
2124 |
*/
|
|
2125 |
|
|
2126 |
/*!
|
|
2127 |
\fn int QMenuBar::insertItem(const QPixmap &pixmap, const QObject *receiver, const char* member, const QKeySequence& shortcut, int id, int index)
|
|
2128 |
|
|
2129 |
Use one of the insertAction(), addAction(), insertMenu(), or
|
|
2130 |
addMenu() overloads instead.
|
|
2131 |
*/
|
|
2132 |
|
|
2133 |
/*!
|
|
2134 |
\fn int QMenuBar::insertItem(const QString &text, int id, int index)
|
|
2135 |
|
|
2136 |
Use one of the insertAction() or addAction() overloads instead.
|
|
2137 |
*/
|
|
2138 |
|
|
2139 |
/*!
|
|
2140 |
\fn int QMenuBar::insertItem(const QIcon& icon, const QString &text, int id, int index)
|
|
2141 |
|
|
2142 |
Use one of the insertAction(), addAction(), insertMenu(), or
|
|
2143 |
addMenu() overloads instead.
|
|
2144 |
*/
|
|
2145 |
|
|
2146 |
/*!
|
|
2147 |
\fn int QMenuBar::insertItem(const QString &text, QMenu *popup, int id, int index)
|
|
2148 |
|
|
2149 |
Use one of the insertMenu(), or addMenu() overloads instead.
|
|
2150 |
*/
|
|
2151 |
|
|
2152 |
/*!
|
|
2153 |
\fn int QMenuBar::insertItem(const QIcon& icon, const QString &text, QMenu *popup, int id, int index)
|
|
2154 |
|
|
2155 |
Use one of the insertMenu(), or addMenu() overloads instead.
|
|
2156 |
*/
|
|
2157 |
|
|
2158 |
/*!
|
|
2159 |
\fn int QMenuBar::insertItem(const QPixmap &pixmap, int id, int index)
|
|
2160 |
|
|
2161 |
Use one of the insertAction(), addAction(), insertMenu(), or
|
|
2162 |
addMenu() overloads instead.
|
|
2163 |
*/
|
|
2164 |
|
|
2165 |
/*!
|
|
2166 |
\fn int QMenuBar::insertItem(const QPixmap &pixmap, QMenu *popup, int id, int index)
|
|
2167 |
|
|
2168 |
Use one of the insertMenu(), or addMenu() overloads instead.
|
|
2169 |
*/
|
|
2170 |
|
|
2171 |
/*!
|
|
2172 |
\fn void QMenuBar::removeItem(int id)
|
|
2173 |
|
|
2174 |
Use removeAction() instead.
|
|
2175 |
*/
|
|
2176 |
|
|
2177 |
/*!
|
|
2178 |
\fn void QMenuBar::removeItemAt(int index)
|
|
2179 |
|
|
2180 |
Use removeAction() instead.
|
|
2181 |
*/
|
|
2182 |
|
|
2183 |
/*!
|
|
2184 |
\fn QKeySequence QMenuBar::accel(int id) const
|
|
2185 |
|
|
2186 |
Use shortcut() on the relevant QAction instead.
|
|
2187 |
*/
|
|
2188 |
|
|
2189 |
/*!
|
|
2190 |
\fn void QMenuBar::setAccel(const QKeySequence& key, int id)
|
|
2191 |
|
|
2192 |
Use setShortcut() on the relevant QAction instead.
|
|
2193 |
*/
|
|
2194 |
|
|
2195 |
/*!
|
|
2196 |
\fn QIcon QMenuBar::iconSet(int id) const
|
|
2197 |
|
|
2198 |
Use icon() on the relevant QAction instead.
|
|
2199 |
*/
|
|
2200 |
|
|
2201 |
/*!
|
|
2202 |
\fn QString QMenuBar::text(int id) const
|
|
2203 |
|
|
2204 |
Use text() on the relevant QAction instead.
|
|
2205 |
*/
|
|
2206 |
|
|
2207 |
/*!
|
|
2208 |
\fn QPixmap QMenuBar::pixmap(int id) const
|
|
2209 |
|
|
2210 |
Use QPixmap(icon()) on the relevant QAction instead.
|
|
2211 |
*/
|
|
2212 |
|
|
2213 |
/*!
|
|
2214 |
\fn void QMenuBar::setWhatsThis(int id, const QString &w)
|
|
2215 |
|
|
2216 |
Use setWhatsThis() on the relevant QAction instead.
|
|
2217 |
*/
|
|
2218 |
|
|
2219 |
/*!
|
|
2220 |
\fn QString QMenuBar::whatsThis(int id) const
|
|
2221 |
|
|
2222 |
Use whatsThis() on the relevant QAction instead.
|
|
2223 |
*/
|
|
2224 |
|
|
2225 |
/*!
|
|
2226 |
\fn void QMenuBar::changeItem(int id, const QString &text)
|
|
2227 |
|
|
2228 |
Use setText() on the relevant QAction instead.
|
|
2229 |
*/
|
|
2230 |
|
|
2231 |
/*!
|
|
2232 |
\fn void QMenuBar::changeItem(int id, const QPixmap &pixmap)
|
|
2233 |
|
|
2234 |
Use setText() on the relevant QAction instead.
|
|
2235 |
*/
|
|
2236 |
|
|
2237 |
/*!
|
|
2238 |
\fn void QMenuBar::changeItem(int id, const QIcon &icon, const QString &text)
|
|
2239 |
|
|
2240 |
Use setIcon() and setText() on the relevant QAction instead.
|
|
2241 |
*/
|
|
2242 |
|
|
2243 |
/*!
|
|
2244 |
\fn bool QMenuBar::isItemActive(int id) const
|
|
2245 |
|
|
2246 |
Use activeAction() instead.
|
|
2247 |
*/
|
|
2248 |
|
|
2249 |
/*!
|
|
2250 |
\fn bool QMenuBar::isItemEnabled(int id) const
|
|
2251 |
|
|
2252 |
Use isEnabled() on the relevant QAction instead.
|
|
2253 |
*/
|
|
2254 |
|
|
2255 |
/*!
|
|
2256 |
\fn void QMenuBar::setItemEnabled(int id, bool enable)
|
|
2257 |
|
|
2258 |
Use setEnabled() on the relevant QAction instead.
|
|
2259 |
*/
|
|
2260 |
|
|
2261 |
/*!
|
|
2262 |
\fn bool QMenuBar::isItemChecked(int id) const
|
|
2263 |
|
|
2264 |
Use isChecked() on the relevant QAction instead.
|
|
2265 |
*/
|
|
2266 |
|
|
2267 |
/*!
|
|
2268 |
\fn void QMenuBar::setItemChecked(int id, bool check)
|
|
2269 |
|
|
2270 |
Use setChecked() on the relevant QAction instead.
|
|
2271 |
*/
|
|
2272 |
|
|
2273 |
/*!
|
|
2274 |
\fn bool QMenuBar::isItemVisible(int id) const
|
|
2275 |
|
|
2276 |
Use isVisible() on the relevant QAction instead.
|
|
2277 |
*/
|
|
2278 |
|
|
2279 |
/*!
|
|
2280 |
\fn void QMenuBar::setItemVisible(int id, bool visible)
|
|
2281 |
|
|
2282 |
Use setVisible() on the relevant QAction instead.
|
|
2283 |
*/
|
|
2284 |
|
|
2285 |
/*!
|
|
2286 |
\fn int QMenuBar::indexOf(int id) const
|
|
2287 |
|
|
2288 |
Use actions().indexOf(action) on the relevant QAction instead.
|
|
2289 |
*/
|
|
2290 |
|
|
2291 |
/*!
|
|
2292 |
\fn int QMenuBar::idAt(int index) const
|
|
2293 |
|
|
2294 |
Use actions instead.
|
|
2295 |
*/
|
|
2296 |
|
|
2297 |
/*!
|
|
2298 |
\fn void QMenuBar::activateItemAt(int index)
|
|
2299 |
|
|
2300 |
Use activate() on the relevant QAction instead.
|
|
2301 |
*/
|
|
2302 |
|
|
2303 |
/*!
|
|
2304 |
\fn bool QMenuBar::connectItem(int id, const QObject *receiver, const char* member)
|
|
2305 |
|
|
2306 |
Use connect() on the relevant QAction instead.
|
|
2307 |
*/
|
|
2308 |
|
|
2309 |
/*!
|
|
2310 |
\fn bool QMenuBar::disconnectItem(int id,const QObject *receiver, const char* member)
|
|
2311 |
|
|
2312 |
Use disconnect() on the relevant QAction instead.
|
|
2313 |
*/
|
|
2314 |
|
|
2315 |
/*!
|
|
2316 |
\fn QMenuItem *QMenuBar::findItem(int id) const
|
|
2317 |
|
|
2318 |
Use actions instead.
|
|
2319 |
*/
|
|
2320 |
|
|
2321 |
/*!
|
|
2322 |
\fn Separator QMenuBar::separator() const
|
|
2323 |
|
|
2324 |
This function is provided only to make old code compile.
|
|
2325 |
*/
|
|
2326 |
|
|
2327 |
/*!
|
|
2328 |
\fn void QMenuBar::setSeparator(Separator sep)
|
|
2329 |
|
|
2330 |
This function is provided only to make old code compile.
|
|
2331 |
*/
|
|
2332 |
|
|
2333 |
/*!
|
|
2334 |
\fn QRect QMenuBar::itemRect(int index)
|
|
2335 |
|
|
2336 |
Use actionGeometry() on the relevant QAction instead.
|
|
2337 |
*/
|
|
2338 |
|
|
2339 |
/*!
|
|
2340 |
\fn int QMenuBar::itemAtPos(const QPoint &p)
|
|
2341 |
|
|
2342 |
There is no equivalent way to achieve this in Qt 4.
|
|
2343 |
*/
|
|
2344 |
|
|
2345 |
/*!
|
|
2346 |
\fn void QMenuBar::activated(int itemId);
|
|
2347 |
|
|
2348 |
Use triggered() instead.
|
|
2349 |
*/
|
|
2350 |
|
|
2351 |
/*!
|
|
2352 |
\fn void QMenuBar::highlighted(int itemId);
|
|
2353 |
|
|
2354 |
Use hovered() instead.
|
|
2355 |
*/
|
|
2356 |
|
|
2357 |
/*!
|
|
2358 |
\fn void QMenuBar::setFrameRect(QRect)
|
|
2359 |
\internal
|
|
2360 |
*/
|
|
2361 |
|
|
2362 |
/*!
|
|
2363 |
\fn QRect QMenuBar::frameRect() const
|
|
2364 |
\internal
|
|
2365 |
*/
|
|
2366 |
/*!
|
|
2367 |
\enum QMenuBar::DummyFrame
|
|
2368 |
\internal
|
|
2369 |
|
|
2370 |
\value Box
|
|
2371 |
\value Sunken
|
|
2372 |
\value Plain
|
|
2373 |
\value Raised
|
|
2374 |
\value MShadow
|
|
2375 |
\value NoFrame
|
|
2376 |
\value Panel
|
|
2377 |
\value StyledPanel
|
|
2378 |
\value HLine
|
|
2379 |
\value VLine
|
|
2380 |
\value GroupBoxPanel
|
|
2381 |
\value WinPanel
|
|
2382 |
\value ToolBarPanel
|
|
2383 |
\value MenuBarPanel
|
|
2384 |
\value PopupPanel
|
|
2385 |
\value LineEditPanel
|
|
2386 |
\value TabWidgetPanel
|
|
2387 |
\value MShape
|
|
2388 |
*/
|
|
2389 |
|
|
2390 |
/*!
|
|
2391 |
\fn void QMenuBar::setFrameShadow(DummyFrame)
|
|
2392 |
\internal
|
|
2393 |
*/
|
|
2394 |
|
|
2395 |
/*!
|
|
2396 |
\fn DummyFrame QMenuBar::frameShadow() const
|
|
2397 |
\internal
|
|
2398 |
*/
|
|
2399 |
|
|
2400 |
/*!
|
|
2401 |
\fn void QMenuBar::setFrameShape(DummyFrame)
|
|
2402 |
\internal
|
|
2403 |
*/
|
|
2404 |
|
|
2405 |
/*!
|
|
2406 |
\fn DummyFrame QMenuBar::frameShape() const
|
|
2407 |
\internal
|
|
2408 |
*/
|
|
2409 |
|
|
2410 |
/*!
|
|
2411 |
\fn void QMenuBar::setFrameStyle(int)
|
|
2412 |
\internal
|
|
2413 |
*/
|
|
2414 |
|
|
2415 |
/*!
|
|
2416 |
\fn int QMenuBar::frameStyle() const
|
|
2417 |
\internal
|
|
2418 |
*/
|
|
2419 |
|
|
2420 |
/*!
|
|
2421 |
\fn void QMenuBar::setLineWidth(int)
|
|
2422 |
\internal
|
|
2423 |
*/
|
|
2424 |
|
|
2425 |
/*!
|
|
2426 |
\fn int QMenuBar::lineWidth() const
|
|
2427 |
\internal
|
|
2428 |
*/
|
|
2429 |
|
|
2430 |
/*!
|
|
2431 |
\fn void QMenuBar::setMargin(int margin)
|
|
2432 |
Sets the width of the margin around the contents of the widget to \a margin.
|
|
2433 |
|
|
2434 |
Use QWidget::setContentsMargins() instead.
|
|
2435 |
\sa margin(), QWidget::setContentsMargins()
|
|
2436 |
*/
|
|
2437 |
|
|
2438 |
/*!
|
|
2439 |
\fn int QMenuBar::margin() const
|
|
2440 |
Returns the width of the margin around the contents of the widget.
|
|
2441 |
|
|
2442 |
Use QWidget::getContentsMargins() instead.
|
|
2443 |
\sa setMargin(), QWidget::getContentsMargins()
|
|
2444 |
*/
|
|
2445 |
|
|
2446 |
/*!
|
|
2447 |
\fn void QMenuBar::setMidLineWidth(int)
|
|
2448 |
\internal
|
|
2449 |
*/
|
|
2450 |
|
|
2451 |
/*!
|
|
2452 |
\fn int QMenuBar::midLineWidth() const
|
|
2453 |
\internal
|
|
2454 |
*/
|
|
2455 |
|
|
2456 |
// for private slots
|
|
2457 |
|
|
2458 |
|
|
2459 |
QT_END_NAMESPACE
|
|
2460 |
|
|
2461 |
#include <moc_qmenubar.cpp>
|
|
2462 |
|
|
2463 |
#endif // QT_NO_MENUBAR
|