diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2022-07-19 17:50:22 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2022-07-21 15:44:16 +0000 |
commit | b0b2b56996affa7121a9e19eb188067fff012034 (patch) | |
tree | 95491637105f9cc07035b626c48cf5ce1c782cf0 | |
parent | 3b151e462c7adf4d5d3b0bf577fbc045a165c5f5 (diff) | |
download | qttools-b0b2b56996affa7121a9e19eb188067fff012034.tar.gz |
Designer: port away from the deprecated QAction APIs
The deprecation of QAction::associatedWidgets() results in a bit more
code, because the list of associated objects can also contain
QGraphicsWidget instances. So we need to explicitly qobject_cast them
to QWidget* before using.
Apart from that the changes are rather straightforward.
Task-number: QTBUG-104968
Change-Id: I2e7c94de069cd5c9cde5e6d41ac86004a8bae6c9
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
(cherry picked from commit ca8a71e1997650a0ecf497d667cfab5a72858c18)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | src/designer/src/designer/qdesigner_workbench.cpp | 2 | ||||
-rw-r--r-- | src/designer/src/lib/shared/actioneditor.cpp | 8 | ||||
-rw-r--r-- | src/designer/src/lib/shared/actionrepository.cpp | 22 | ||||
-rw-r--r-- | src/designer/src/lib/shared/qdesigner_command.cpp | 27 | ||||
-rw-r--r-- | src/designer/src/lib/shared/qdesigner_menu.cpp | 15 | ||||
-rw-r--r-- | src/designer/src/lib/shared/qdesigner_propertycommand.cpp | 10 | ||||
-rw-r--r-- | src/designer/src/lib/uilib/abstractformbuilder.cpp | 2 | ||||
-rw-r--r-- | src/linguist/linguist/formpreviewview.cpp | 6 |
8 files changed, 54 insertions, 38 deletions
diff --git a/src/designer/src/designer/qdesigner_workbench.cpp b/src/designer/src/designer/qdesigner_workbench.cpp index 73e03b339..693d14f97 100644 --- a/src/designer/src/designer/qdesigner_workbench.cpp +++ b/src/designer/src/designer/qdesigner_workbench.cpp @@ -755,7 +755,7 @@ void QDesignerWorkbench::updateWindowMenu(QDesignerFormWindowInterface *fwi) void QDesignerWorkbench::formWindowActionTriggered(QAction *a) { - QDesignerFormWindow *fw = qobject_cast<QDesignerFormWindow *>(a->parentWidget()); + QDesignerFormWindow *fw = qobject_cast<QDesignerFormWindow *>(a->parent()); Q_ASSERT(fw); if (isFormWindowMinimized(fw)) diff --git a/src/designer/src/lib/shared/actioneditor.cpp b/src/designer/src/lib/shared/actioneditor.cpp index 85962a31c..c8fdc43ff 100644 --- a/src/designer/src/lib/shared/actioneditor.cpp +++ b/src/designer/src/lib/shared/actioneditor.cpp @@ -326,7 +326,13 @@ void ActionEditor::slotCurrentItemChanged(QAction *action) QDesignerObjectInspector *oi = qobject_cast<QDesignerObjectInspector *>(core()->objectInspector()); - if (action->associatedWidgets().isEmpty()) { + // Check if we have at least one associated QWidget: + const auto associatedObjects = action->associatedObjects(); + auto it = std::find_if(associatedObjects.cbegin(), associatedObjects.cend(), + [](QObject *obj) { + return qobject_cast<QWidget *>(obj) != nullptr; + }); + if (it == associatedObjects.cend()) { // Special case: action not in object tree. Deselect all and set in property editor fw->clearSelection(false); if (oi) diff --git a/src/designer/src/lib/shared/actionrepository.cpp b/src/designer/src/lib/shared/actionrepository.cpp index 96e6334a7..40afd50b5 100644 --- a/src/designer/src/lib/shared/actionrepository.cpp +++ b/src/designer/src/lib/shared/actionrepository.cpp @@ -114,14 +114,16 @@ QModelIndex ActionModel::addAction(QAction *action) // Find the associated menus and toolbars, ignore toolbuttons QWidgetList ActionModel::associatedWidgets(const QAction *action) { - QWidgetList rc = action->associatedWidgets(); - for (QWidgetList::iterator it = rc.begin(); it != rc.end(); ) - if (qobject_cast<const QMenu *>(*it) || qobject_cast<const QToolBar *>(*it)) { - ++it; - } else { - it = rc.erase(it); + const QObjectList rc = action->associatedObjects(); + QWidgetList result; + result.reserve(rc.size()); + for (QObject *obj : rc) { + if (QWidget *w = qobject_cast<QWidget *>(obj)) { + if (qobject_cast<const QMenu *>(w) || qobject_cast<const QToolBar *>(w)) + result.push_back(w); } - return rc; + } + return result; } // shortcut is a fake property, need to retrieve it via property sheet. @@ -618,9 +620,9 @@ QPixmap ActionRepositoryMimeData::actionDragPixmap(const QAction *action) if (!icon.isNull()) return icon.pixmap(QSize(22, 22)); - const QWidgetList &associatedWidgets = action->associatedWidgets(); - for (QWidget *w : associatedWidgets) { - if (QToolButton *tb = qobject_cast<QToolButton *>(w)) + const QObjectList associatedObjects = action->associatedObjects(); + for (QObject *o : associatedObjects) { + if (QToolButton *tb = qobject_cast<QToolButton *>(o)) return tb->grab(QRect(0, 0, -1, -1)); } diff --git a/src/designer/src/lib/shared/qdesigner_command.cpp b/src/designer/src/lib/shared/qdesigner_command.cpp index c1bd00e21..9d7d0da9a 100644 --- a/src/designer/src/lib/shared/qdesigner_command.cpp +++ b/src/designer/src/lib/shared/qdesigner_command.cpp @@ -2628,19 +2628,20 @@ static RemoveActionCommand::ActionData findActionIn(QAction *action) { RemoveActionCommand::ActionData result; // We only want menus and toolbars, no toolbuttons. - const QWidgetList &associatedWidgets = action->associatedWidgets(); - for (QWidget *widget : associatedWidgets) { - if (qobject_cast<const QMenu *>(widget) || qobject_cast<const QToolBar *>(widget)) { - const auto actionList = widget->actions(); - const int size = actionList.size(); - for (int i = 0; i < size; ++i) { - if (actionList.at(i) == action) { - QAction *before = nullptr; - if (i + 1 < size) - before = actionList.at(i + 1); - result.append(RemoveActionCommand::ActionDataItem(before, widget)); - break; - } + const QObjectList associatedObjects = action->associatedObjects(); + for (QObject *obj : associatedObjects) { + if (!qobject_cast<const QMenu *>(obj) && !qobject_cast<const QToolBar *>(obj)) + continue; + QWidget *widget = static_cast<QWidget *>(obj); + const auto actionList = widget->actions(); + const int size = actionList.size(); + for (int i = 0; i < size; ++i) { + if (actionList.at(i) == action) { + QAction *before = nullptr; + if (i + 1 < size) + before = actionList.at(i + 1); + result.append(RemoveActionCommand::ActionDataItem(before, widget)); + break; } } } diff --git a/src/designer/src/lib/shared/qdesigner_menu.cpp b/src/designer/src/lib/shared/qdesigner_menu.cpp index ad17d8c06..36e988e23 100644 --- a/src/designer/src/lib/shared/qdesigner_menu.cpp +++ b/src/designer/src/lib/shared/qdesigner_menu.cpp @@ -1010,16 +1010,15 @@ QDesignerMenu *QDesignerMenu::findOrCreateSubMenu(QAction *action) bool QDesignerMenu::canCreateSubMenu(QAction *action) const // ### improve it's a bit too slow { - const QWidgetList &associatedWidgets = action->associatedWidgets(); - for (const QWidget *aw : associatedWidgets) { - if (aw != this) { - if (const QMenu *m = qobject_cast<const QMenu *>(aw)) { + const QObjectList associatedObjects = action->associatedObjects(); + for (const QObject *ao : associatedObjects) { + if (ao != this) { + if (const QMenu *m = qobject_cast<const QMenu *>(ao)) { if (m->actions().contains(action)) return false; // sorry - } else { - if (const QToolBar *tb = qobject_cast<const QToolBar *>(aw)) - if (tb->actions().contains(action)) - return false; // sorry + } else if (const QToolBar *tb = qobject_cast<const QToolBar *>(ao)) { + if (tb->actions().contains(action)) + return false; // sorry } } } diff --git a/src/designer/src/lib/shared/qdesigner_propertycommand.cpp b/src/designer/src/lib/shared/qdesigner_propertycommand.cpp index 57037a85d..d3ad56c0f 100644 --- a/src/designer/src/lib/shared/qdesigner_propertycommand.cpp +++ b/src/designer/src/lib/shared/qdesigner_propertycommand.cpp @@ -626,8 +626,14 @@ PropertyHelper::PropertyHelper(QObject* object, m_parentWidget = (qobject_cast<QWidget*>(object))->parentWidget(); m_objectType = OT_Widget; } else { - if (const QAction *action = qobject_cast<const QAction *>(m_object)) - m_objectType = action->associatedWidgets().isEmpty() ? OT_FreeAction : OT_AssociatedAction; + if (const QAction *action = qobject_cast<const QAction *>(m_object)) { + const QObjectList associatedObjects = action->associatedObjects(); + auto it = std::find_if(associatedObjects.cbegin(), associatedObjects.cend(), + [](QObject *obj) { + return qobject_cast<QWidget *>(obj) != nullptr; + }); + m_objectType = (it == associatedObjects.cend()) ? OT_FreeAction : OT_AssociatedAction; + } } if(debugPropertyCommands) diff --git a/src/designer/src/lib/uilib/abstractformbuilder.cpp b/src/designer/src/lib/uilib/abstractformbuilder.cpp index 2845d7341..604a7f9d1 100644 --- a/src/designer/src/lib/uilib/abstractformbuilder.cpp +++ b/src/designer/src/lib/uilib/abstractformbuilder.cpp @@ -2406,7 +2406,7 @@ void QAbstractFormBuilder::setWorkingDirectory(const QDir &directory) */ DomAction *QAbstractFormBuilder::createDom(QAction *action) { - if (action->parentWidget() == action->menu() || action->isSeparator()) + if (action->parent() == action->menu() || action->isSeparator()) return nullptr; DomAction *ui_action = new DomAction; diff --git a/src/linguist/linguist/formpreviewview.cpp b/src/linguist/linguist/formpreviewview.cpp index 18dfd1711..dcd14843a 100644 --- a/src/linguist/linguist/formpreviewview.cpp +++ b/src/linguist/linguist/formpreviewview.cpp @@ -352,8 +352,10 @@ static void highlightAction(QAction *a, bool on) a->setProperty(FONT_BACKUP_PROP, QVariant()); } } - for (QWidget *w : a->associatedWidgets()) - highlightWidget(w, on); + for (QObject *o : a->associatedObjects()) { + if (QWidget *w = qobject_cast<QWidget *>(o)) + highlightWidget(w, on); + } } static void highlightWidget(QWidget *w, bool on) |