From af44be42cfacb85ef783cdd051f5d106d25c6b50 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Thu, 6 Aug 2009 16:19:18 +0200 Subject: Designer: Cannot edit menus in an RTL form when Designer uses LTR. Use widget->layoutDirection() instead of QApplication::layoutDirection() where applicable, which caused it to break for the case that Designer's layoutDirection does not match that of a widget [property]. In addition, position the submenu correctly on the left side in the case of RTL. Reviewed-by: Jarek Kobus Task-number: 259238 --- .../src/components/buddyeditor/buddyeditor.cpp | 2 +- .../formeditor/default_actionprovider.cpp | 15 ++-- .../components/formeditor/default_actionprovider.h | 4 +- tools/designer/src/lib/shared/actionprovider_p.h | 2 +- tools/designer/src/lib/shared/qdesigner_menu.cpp | 86 +++++++++++++++------- tools/designer/src/lib/shared/qdesigner_menu_p.h | 3 + .../designer/src/lib/shared/qdesigner_menubar.cpp | 58 +++++++++------ .../designer/src/lib/shared/qdesigner_menubar_p.h | 4 +- .../designer/src/lib/shared/qdesigner_toolbar.cpp | 2 +- 9 files changed, 114 insertions(+), 62 deletions(-) diff --git a/tools/designer/src/components/buddyeditor/buddyeditor.cpp b/tools/designer/src/components/buddyeditor/buddyeditor.cpp index 9984b0d..d5c8670 100644 --- a/tools/designer/src/components/buddyeditor/buddyeditor.cpp +++ b/tools/designer/src/components/buddyeditor/buddyeditor.cpp @@ -404,7 +404,7 @@ QWidget *BuddyEditor::findBuddy(QLabel *l, const QWidgetList &existingBuddies) c const QRect geom = l->geometry(); const int y = geom.center().y(); QWidget *neighbour = 0; - switch (QApplication::layoutDirection()) { + switch (l->layoutDirection()) { case Qt::LeftToRight: { // Walk right to find next managed neighbour const int xEnd = parent->size().width(); for (int x = geom.right() + 1; x < xEnd; x += DeltaX) diff --git a/tools/designer/src/components/formeditor/default_actionprovider.cpp b/tools/designer/src/components/formeditor/default_actionprovider.cpp index 42d1f81..41333f8 100644 --- a/tools/designer/src/components/formeditor/default_actionprovider.cpp +++ b/tools/designer/src/components/formeditor/default_actionprovider.cpp @@ -73,9 +73,8 @@ enum { indicatorSize = 2 }; // Position an indicator horizontally over the rectangle, indicating // 'Insert before' (left or right according to layout direction) -static inline QRect horizontalIndicatorRect(const QRect &rect) +static inline QRect horizontalIndicatorRect(const QRect &rect, Qt::LayoutDirection layoutDirection) { - const Qt::LayoutDirection layoutDirection = QApplication::layoutDirection(); // Position right? QRect rc = QRect(rect.x(), 0, indicatorSize, rect.height() - 1); if (layoutDirection == Qt::RightToLeft) @@ -91,13 +90,13 @@ static inline QRect verticalIndicatorRect(const QRect &rect) // Determine the geometry of the indicator by retrieving // the action under mouse and positioning the bar within its geometry. -QRect ActionProviderBase::indicatorGeometry(const QPoint &pos) const +QRect ActionProviderBase::indicatorGeometry(const QPoint &pos, Qt::LayoutDirection layoutDirection) const { QAction *action = actionAt(pos); if (!action) return QRect(); QRect rc = actionGeometry(action); - return orientation() == Qt::Horizontal ? horizontalIndicatorRect(rc) : verticalIndicatorRect(rc); + return orientation() == Qt::Horizontal ? horizontalIndicatorRect(rc, layoutDirection) : verticalIndicatorRect(rc); } // Adjust the indicator while dragging. (-1,1) is called to finish a DND operation @@ -107,7 +106,7 @@ void ActionProviderBase::adjustIndicator(const QPoint &pos) m_indicator->hide(); return; } - const QRect ig = indicatorGeometry(pos); + const QRect ig = indicatorGeometry(pos, m_indicator->layoutDirection()); if (ig.isValid()) { m_indicator->setGeometry(ig); QPalette p = m_indicator->palette(); @@ -145,9 +144,9 @@ Qt::Orientation QToolBarActionProvider::orientation() const return m_widget->orientation(); } -QRect QToolBarActionProvider::indicatorGeometry(const QPoint &pos) const +QRect QToolBarActionProvider::indicatorGeometry(const QPoint &pos, Qt::LayoutDirection layoutDirection) const { - const QRect actionRect = ActionProviderBase::indicatorGeometry(pos); + const QRect actionRect = ActionProviderBase::indicatorGeometry(pos, layoutDirection); if (actionRect.isValid()) return actionRect; // Toolbar differs in that is has no dummy placeholder to 'insert before' @@ -155,7 +154,7 @@ QRect QToolBarActionProvider::indicatorGeometry(const QPoint &pos) const const QRect freeArea = ToolBarEventFilter::freeArea(m_widget); if (!freeArea.contains(pos)) return QRect(); - return orientation() == Qt::Horizontal ? horizontalIndicatorRect(freeArea) : verticalIndicatorRect(freeArea); + return orientation() == Qt::Horizontal ? horizontalIndicatorRect(freeArea, layoutDirection) : verticalIndicatorRect(freeArea); } // ------------- QMenuBarActionProvider diff --git a/tools/designer/src/components/formeditor/default_actionprovider.h b/tools/designer/src/components/formeditor/default_actionprovider.h index 3660f11..270ea36 100644 --- a/tools/designer/src/components/formeditor/default_actionprovider.h +++ b/tools/designer/src/components/formeditor/default_actionprovider.h @@ -66,7 +66,7 @@ public: virtual Qt::Orientation orientation() const = 0; protected: - virtual QRect indicatorGeometry(const QPoint &pos) const; + virtual QRect indicatorGeometry(const QPoint &pos, Qt::LayoutDirection layoutDirection) const; private: QWidget *m_indicator; @@ -84,7 +84,7 @@ public: Qt::Orientation orientation() const; protected: - virtual QRect indicatorGeometry(const QPoint &pos) const; + virtual QRect indicatorGeometry(const QPoint &pos, Qt::LayoutDirection layoutDirection) const; private: QToolBar *m_widget; diff --git a/tools/designer/src/lib/shared/actionprovider_p.h b/tools/designer/src/lib/shared/actionprovider_p.h index b43dee9..5f9b7a0 100644 --- a/tools/designer/src/lib/shared/actionprovider_p.h +++ b/tools/designer/src/lib/shared/actionprovider_p.h @@ -86,7 +86,7 @@ template // actionGeometry() can be wrong sometimes; it returns a geometry that // stretches to the end of the toolbar/menu bar. So, check from the beginning // in the case of a horizontal right-to-left orientation. - const bool checkTopRight = orientation == Qt::Horizontal && QApplication::layoutDirection() == Qt::RightToLeft; + const bool checkTopRight = orientation == Qt::Horizontal && w->layoutDirection() == Qt::RightToLeft; const QPoint topRight = QPoint(w->rect().width(), 0); for (int index = 0; index < actionCount; ++index) { QRect g = w->actionGeometry(actions.at(index)); diff --git a/tools/designer/src/lib/shared/qdesigner_menu.cpp b/tools/designer/src/lib/shared/qdesigner_menu.cpp index c727d8e..6aba65b 100644 --- a/tools/designer/src/lib/shared/qdesigner_menu.cpp +++ b/tools/designer/src/lib/shared/qdesigner_menu.cpp @@ -73,6 +73,19 @@ QT_BEGIN_NAMESPACE using namespace qdesigner_internal; +// give the user a little more space to click on the sub menu rectangle +static inline void extendClickableArea(QRect *subMenuRect, Qt::LayoutDirection dir) +{ + switch (dir) { + case Qt::LeftToRight: + subMenuRect->setLeft(subMenuRect->left() - 20); + break; + case Qt::RightToLeft: + subMenuRect->setRight(subMenuRect->right() + 20); + break; + } +} + QDesignerMenu::QDesignerMenu(QWidget *parent) : QMenu(parent), m_currentIndex(0), @@ -325,8 +338,7 @@ bool QDesignerMenu::handleMouseDoubleClickEvent(QWidget *, QMouseEvent *event) QRect pm_rect; if (action->menu() || hasSubMenuPixmap(action)) { pm_rect = subMenuPixmapRect(action); - pm_rect.setLeft(pm_rect.left() - 20); // give the user a little more - // space to click + extendClickableArea(&pm_rect, layoutDirection()); } if (!pm_rect.contains(event->pos()) && m_currentIndex != -1) @@ -381,7 +393,7 @@ bool QDesignerMenu::handleMousePressEvent(QWidget * /*widget*/, QMouseEvent *eve QAction *action = safeActionAt(index); QRect pm_rect = subMenuPixmapRect(action); - pm_rect.setLeft(pm_rect.left() - 20); // give the user a little more space to click + extendClickableArea(&pm_rect, layoutDirection()); const int old_index = m_currentIndex; m_currentIndex = index; @@ -540,7 +552,7 @@ QRect QDesignerMenu::subMenuPixmapRect(QAction *action) const { static const QPixmap pm(QLatin1String(":/trolltech/formeditor/images/submenu.png")); const QRect g = actionGeometry(action); - const int x = g.right() - pm.width() - 2; + const int x = layoutDirection() == Qt::LeftToRight ? (g.right() - pm.width() - 2) : 2; const int y = g.top() + (g.height() - pm.height())/2 + 1; return QRect(x, y, pm.width(), pm.height()); } @@ -863,38 +875,52 @@ void QDesignerMenu::closeMenuChain() m_lastSubMenuIndex = -1; } -void QDesignerMenu::moveLeft() +// Close submenu using the left/right keys according to layoutDirection(). +// Return false to indicate the event must be propagated to the menu bar. +bool QDesignerMenu::hideSubMenuOnCursorKey() { if (parentMenu()) { hide(); - } else { - closeMenuChain(); - if (QDesignerMenuBar *mb = parentMenuBar()) { - if (QApplication::layoutDirection() == Qt::LeftToRight) - mb->moveLeft(); - else - mb->moveRight(); - } + return true; } + closeMenuChain(); update(); + if (parentMenuBar()) + return false; + return true; } -void QDesignerMenu::moveRight() +// Open a submenu using the left/right keys according to layoutDirection(). +// Return false to indicate the event must be propagated to the menu bar. +bool QDesignerMenu::showSubMenuOnCursorKey() { - QAction *action = currentAction(); + const QAction *action = currentAction(); - if (qobject_cast(action) || action->isSeparator()) { + if (qobject_cast(action) || action->isSeparator()) { closeMenuChain(); - if (QDesignerMenuBar *mb = parentMenuBar()) { - if (QApplication::layoutDirection() == Qt::LeftToRight) - mb->moveRight(); - else - mb->moveLeft(); - } - } else { - m_lastSubMenuIndex = -1; // force a refresh - slotShowSubMenuNow(); + if (parentMenuBar()) + return false; + return true; } + m_lastSubMenuIndex = -1; // force a refresh + slotShowSubMenuNow(); + return true; +} + +void QDesignerMenu::moveLeft() +{ + const bool handled = layoutDirection() == Qt::LeftToRight ? + hideSubMenuOnCursorKey() : showSubMenuOnCursorKey(); + if (!handled) + parentMenuBar()->moveLeft(); +} + +void QDesignerMenu::moveRight() +{ + const bool handled = layoutDirection() == Qt::LeftToRight ? + showSubMenuOnCursorKey() : hideSubMenuOnCursorKey(); + if (!handled) + parentMenuBar()->moveRight(); } void QDesignerMenu::moveUp(bool ctrl) @@ -1053,7 +1079,15 @@ void QDesignerMenu::slotShowSubMenuNow() if ((menu->windowFlags() & Qt::Popup) != Qt::Popup) menu->setWindowFlags(Qt::Popup); const QRect g = actionGeometry(action); - menu->move(mapToGlobal(g.topRight())); + if (layoutDirection() == Qt::LeftToRight) { + menu->move(mapToGlobal(g.topRight())); + } else { + // The position is not initially correct due to the unknown width, + // causing it to overlap a bit the first time it is invoked. + const QSize menuSize = menu->size(); + QPoint point = g.topLeft() - QPoint(menu->width() + 10, 0); + menu->move(mapToGlobal(point)); + } menu->show(); menu->setFocus(); } else { diff --git a/tools/designer/src/lib/shared/qdesigner_menu_p.h b/tools/designer/src/lib/shared/qdesigner_menu_p.h index 55d8bcd..93735e6 100644 --- a/tools/designer/src/lib/shared/qdesigner_menu_p.h +++ b/tools/designer/src/lib/shared/qdesigner_menu_p.h @@ -181,6 +181,9 @@ protected: void selectCurrentAction(); private: + bool hideSubMenuOnCursorKey(); + bool showSubMenuOnCursorKey(); + QPoint m_startPosition; int m_currentIndex; QAction *m_addItem; diff --git a/tools/designer/src/lib/shared/qdesigner_menubar.cpp b/tools/designer/src/lib/shared/qdesigner_menubar.cpp index b53bb8d..2b19142 100644 --- a/tools/designer/src/lib/shared/qdesigner_menubar.cpp +++ b/tools/designer/src/lib/shared/qdesigner_menubar.cpp @@ -219,18 +219,12 @@ bool QDesignerMenuBar::handleKeyPressEvent(QWidget *, QKeyEvent *e) case Qt::Key_Left: e->accept(); - if (QApplication::layoutDirection() == Qt::LeftToRight) - moveLeft(e->modifiers() & Qt::ControlModifier); - else - moveRight(e->modifiers() & Qt::ControlModifier); + moveLeft(e->modifiers() & Qt::ControlModifier); return true; case Qt::Key_Right: e->accept(); - if (QApplication::layoutDirection() == Qt::LeftToRight) - moveRight(e->modifiers() & Qt::ControlModifier); - else - moveLeft(e->modifiers() & Qt::ControlModifier); + moveRight(e->modifiers() & Qt::ControlModifier); return true; // no update case Qt::Key_Up: @@ -741,28 +735,48 @@ int QDesignerMenuBar::realActionCount() const return actions().count() - 1; // 1 fake actions } -void QDesignerMenuBar::moveLeft(bool ctrl) +bool QDesignerMenuBar::dragging() const { - if (ctrl) - (void) swap(m_currentIndex, m_currentIndex - 1); - - m_currentIndex = qMax(0, --m_currentIndex); - // Always re-select, swapping destroys order - updateCurrentAction(true); + return m_dragging; } -bool QDesignerMenuBar::dragging() const +void QDesignerMenuBar::moveLeft(bool ctrl) { - return m_dragging; + if (layoutDirection() == Qt::LeftToRight) { + movePrevious(ctrl); + } else { + moveNext(ctrl); + } } void QDesignerMenuBar::moveRight(bool ctrl) { - if (ctrl) - (void) swap(m_currentIndex + 1, m_currentIndex); + if (layoutDirection() == Qt::LeftToRight) { + moveNext(ctrl); + } else { + movePrevious(ctrl); + } +} + +void QDesignerMenuBar::movePrevious(bool ctrl) +{ + const bool swapped = ctrl && swapActions(m_currentIndex, m_currentIndex - 1); + const int newIndex = qMax(0, m_currentIndex - 1); + // Always re-select, swapping destroys order + if (swapped || newIndex != m_currentIndex) { + m_currentIndex = newIndex; + updateCurrentAction(true); + } +} - m_currentIndex = qMin(actions().count() - 1, ++m_currentIndex); - updateCurrentAction(!ctrl); +void QDesignerMenuBar::moveNext(bool ctrl) +{ + const bool swapped = ctrl && swapActions(m_currentIndex + 1, m_currentIndex); + const int newIndex = qMin(actions().count() - 1, m_currentIndex + 1); + if (swapped || newIndex != m_currentIndex) { + m_currentIndex = newIndex; + updateCurrentAction(!ctrl); + } } void QDesignerMenuBar::moveUp() @@ -869,7 +883,7 @@ QAction *QDesignerMenuBar::safeActionAt(int index) const return actions().at(index); } -bool QDesignerMenuBar::swap(int a, int b) +bool QDesignerMenuBar::swapActions(int a, int b) { const int left = qMin(a, b); int right = qMax(a, b); diff --git a/tools/designer/src/lib/shared/qdesigner_menubar_p.h b/tools/designer/src/lib/shared/qdesigner_menubar_p.h index 97a655b..fb820e1 100644 --- a/tools/designer/src/lib/shared/qdesigner_menubar_p.h +++ b/tools/designer/src/lib/shared/qdesigner_menubar_p.h @@ -155,10 +155,12 @@ protected: QAction *safeActionAt(int index) const; - bool swap(int a, int b); + bool swapActions(int a, int b); private: void updateCurrentAction(bool selectAction); + void movePrevious(bool ctrl); + void moveNext(bool ctrl); QAction *m_addMenu; QPointer m_activeMenu; diff --git a/tools/designer/src/lib/shared/qdesigner_toolbar.cpp b/tools/designer/src/lib/shared/qdesigner_toolbar.cpp index 2693452..8c0c61d 100644 --- a/tools/designer/src/lib/shared/qdesigner_toolbar.cpp +++ b/tools/designer/src/lib/shared/qdesigner_toolbar.cpp @@ -466,7 +466,7 @@ QRect ToolBarEventFilter::freeArea(const QToolBar *tb) QRect exclusionRectangle = actionList.empty() ? handleArea(tb) : tb->actionGeometry(actionList.back()); switch (tb->orientation()) { case Qt::Horizontal: - switch (QApplication::layoutDirection()) { + switch (tb->layoutDirection()) { case Qt::LeftToRight: rc.setX(exclusionRectangle.right() + 1); break; -- cgit v0.12