From bc7d0561c33b34f75c6ec6b68d27b956f9f90e8a Mon Sep 17 00:00:00 2001 From: Alessandro Portale Date: Thu, 25 Mar 2021 19:10:35 +0100 Subject: [PATCH] Android: Simplify pre-/post-run adb command input AdbCommandWidget is cumbersome to use and has issues regarding updating the enabled state of the buttons and the dragging and dropping of items. Cut the maintenance and replace AdbCommandWidget with a StringAspect, where one line means one entry, just like several other places in Qt Creator do. Some residue remains though: in order to keep project settings compatibility, we need to convert from QStringList (settings) to QString (StringAspect) and back. Change-Id: I3ebfff882358ba2e8c0ac6d5b309e89a84f0554c Reviewed-by: hjk --- src/plugins/android/CMakeLists.txt | 1 - src/plugins/android/adbcommandswidget.cpp | 189 ------------------ src/plugins/android/adbcommandswidget.h | 58 ------ src/plugins/android/adbcommandswidget.ui | 117 ----------- src/plugins/android/android.pro | 3 - src/plugins/android/android.qbs | 3 - .../android/androidrunconfiguration.cpp | 61 ++---- src/plugins/android/androidrunconfiguration.h | 21 -- src/plugins/android/androidrunnerworker.cpp | 8 +- 9 files changed, 26 insertions(+), 435 deletions(-) delete mode 100644 src/plugins/android/adbcommandswidget.cpp delete mode 100644 src/plugins/android/adbcommandswidget.h delete mode 100644 src/plugins/android/adbcommandswidget.ui diff --git a/src/plugins/android/CMakeLists.txt b/src/plugins/android/CMakeLists.txt index d2f57f253ef..72ebbdbebcf 100644 --- a/src/plugins/android/CMakeLists.txt +++ b/src/plugins/android/CMakeLists.txt @@ -2,7 +2,6 @@ add_qtc_plugin(Android DEPENDS QtcSsh QmlDebug Qt5::Xml LanguageServerProtocol PLUGIN_DEPENDS Core Debugger ProjectExplorer QtSupport LanguageClient SOURCES - adbcommandswidget.cpp adbcommandswidget.h adbcommandswidget.ui addnewavddialog.ui android.qrc android_global.h diff --git a/src/plugins/android/adbcommandswidget.cpp b/src/plugins/android/adbcommandswidget.cpp deleted file mode 100644 index 4fe5c76d8c3..00000000000 --- a/src/plugins/android/adbcommandswidget.cpp +++ /dev/null @@ -1,189 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of Qt Creator. -** -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -****************************************************************************/ -#include "adbcommandswidget.h" -#include "ui_adbcommandswidget.h" - -#include - -#include -#include -#include -#include - -#include - -namespace Android { -namespace Internal { - -using namespace std::placeholders; - -const char defaultCommand[] = "echo \"shell command\""; - -static void swapData(QStringListModel *model, const QModelIndex &srcIndex, - const QModelIndex &destIndex) -{ - if (model) { - QVariant data = model->data(destIndex, Qt::EditRole); // QTBUG-55078 - model->setData(destIndex, model->data(srcIndex, Qt::EditRole)); - model->setData(srcIndex, data); - } -} - -class AdbCommandsWidgetPrivate -{ -public: - AdbCommandsWidgetPrivate(AdbCommandsWidget *parent); - ~AdbCommandsWidgetPrivate(); - -private: - void addString(const QString &str); - void onAddButton(); - void onMoveUpButton(); - void onMoveDownButton(); - void onRemove(); - void onCurrentIndexChanged(const QModelIndex &newIndex, const QModelIndex &prevIndex); - - AdbCommandsWidget *q; - Ui::AdbCommandsWidget *m_ui = nullptr; - QStringListModel *m_stringModel = nullptr; - friend class AdbCommandsWidget; -}; - -AdbCommandsWidget::AdbCommandsWidget() - : d(new AdbCommandsWidgetPrivate(this)) -{ -} - -AdbCommandsWidget::~AdbCommandsWidget() = default; - -QStringList AdbCommandsWidget::commandsList() const -{ - return d->m_stringModel->stringList(); -} - -void AdbCommandsWidget::setTitleText(const QString &title) -{ - setTitle(title); -} - -void AdbCommandsWidget::setCommandList(const QStringList &commands) -{ - d->m_stringModel->setStringList(commands); -} - -AdbCommandsWidgetPrivate::AdbCommandsWidgetPrivate(AdbCommandsWidget *q): - q(q), - m_ui(new Ui::AdbCommandsWidget), - m_stringModel(new QStringListModel) -{ - m_ui->setupUi(q); - m_ui->addButton->setIcon(Utils::Icons::PLUS.icon()); - m_ui->removeButton->setIcon(Utils::Icons::MINUS.icon()); - m_ui->moveUpButton->setIcon(Utils::Icons::ARROW_UP.icon()); - m_ui->moveDownButton->setIcon(Utils::Icons::ARROW_DOWN.icon()); - - auto deleteShortcut = new QShortcut(QKeySequence(QKeySequence::Delete), m_ui->commandsView); - deleteShortcut->setContext(Qt::WidgetShortcut); - QObject::connect(deleteShortcut, &QShortcut::activated, - std::bind(&AdbCommandsWidgetPrivate::onRemove, this)); - - QObject::connect(m_ui->addButton, &QToolButton::clicked, - std::bind(&AdbCommandsWidgetPrivate::onAddButton, this)); - QObject::connect(m_ui->removeButton, &QToolButton::clicked, - std::bind(&AdbCommandsWidgetPrivate::onRemove, this)); - QObject::connect(m_ui->moveUpButton, &QToolButton::clicked, - std::bind(&AdbCommandsWidgetPrivate::onMoveUpButton, this)); - QObject::connect(m_ui->moveDownButton, &QToolButton::clicked, - std::bind(&AdbCommandsWidgetPrivate::onMoveDownButton, this)); - - m_ui->commandsView->setModel(m_stringModel); - QObject::connect(m_stringModel, &QStringListModel::dataChanged, - q, &AdbCommandsWidget::commandsChanged); - QObject::connect(m_stringModel, &QStringListModel::rowsRemoved, - q, &AdbCommandsWidget::commandsChanged); - QObject::connect(m_ui->commandsView->selectionModel(), &QItemSelectionModel::currentChanged, - std::bind(&AdbCommandsWidgetPrivate::onCurrentIndexChanged, this, _1, _2)); -} - -AdbCommandsWidgetPrivate::~AdbCommandsWidgetPrivate() -{ - delete m_ui; - delete m_stringModel; -} - -void AdbCommandsWidgetPrivate::addString(const QString &str) -{ - if (!str.isEmpty()) { - m_stringModel->insertRows(m_stringModel->rowCount(), 1); - const QModelIndex lastItemIndex = m_stringModel->index(m_stringModel->rowCount() - 1); - m_stringModel->setData(lastItemIndex, str); - } -} - -void AdbCommandsWidgetPrivate::onAddButton() -{ - addString(defaultCommand); - const QModelIndex index = m_stringModel->index(m_stringModel->rowCount() - 1); - m_ui->commandsView->setCurrentIndex(index); - m_ui->commandsView->edit(index); -} - -void AdbCommandsWidgetPrivate::onMoveUpButton() -{ - QModelIndex index = m_ui->commandsView->currentIndex(); - if (index.row() > 0) { - const QModelIndex newIndex = m_stringModel->index(index.row() - 1, 0); - swapData(m_stringModel, index, newIndex); - m_ui->commandsView->setCurrentIndex(newIndex); - } -} - -void AdbCommandsWidgetPrivate::onMoveDownButton() -{ - QModelIndex index = m_ui->commandsView->currentIndex(); - if (index.row() < m_stringModel->rowCount() - 1) { - const QModelIndex newIndex = m_stringModel->index(index.row() + 1, 0); - swapData(m_stringModel, index, newIndex); - m_ui->commandsView->setCurrentIndex(newIndex); - } -} - -void AdbCommandsWidgetPrivate::onRemove() -{ - const QModelIndex &index = m_ui->commandsView->currentIndex(); - if (index.isValid()) - m_stringModel->removeRow(index.row()); -} - -void AdbCommandsWidgetPrivate::onCurrentIndexChanged(const QModelIndex &newIndex, const QModelIndex &prevIndex) -{ - Q_UNUSED(prevIndex) - m_ui->moveUpButton->setEnabled(newIndex.row() != 0); - m_ui->moveDownButton->setEnabled(newIndex.row() < m_stringModel->rowCount() - 1); - m_ui->removeButton->setEnabled(newIndex.isValid()); -} - -} // Internal -} // Android diff --git a/src/plugins/android/adbcommandswidget.h b/src/plugins/android/adbcommandswidget.h deleted file mode 100644 index 4650639cc29..00000000000 --- a/src/plugins/android/adbcommandswidget.h +++ /dev/null @@ -1,58 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of Qt Creator. -** -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -****************************************************************************/ - -#pragma once - -#include -#include - -#include - -namespace Android { -namespace Internal { - -class AdbCommandsWidgetPrivate; - -class AdbCommandsWidget : public QGroupBox -{ - Q_OBJECT -public: - AdbCommandsWidget(); - ~AdbCommandsWidget() override; - - QStringList commandsList() const; - void setCommandList(const QStringList &commands); - - void setTitleText(const QString &title); - -signals: - void commandsChanged(); - -private: - std::unique_ptr d; -}; - -} // Internal -} // Android diff --git a/src/plugins/android/adbcommandswidget.ui b/src/plugins/android/adbcommandswidget.ui deleted file mode 100644 index 3aff5e4fea1..00000000000 --- a/src/plugins/android/adbcommandswidget.ui +++ /dev/null @@ -1,117 +0,0 @@ - - - AdbCommandsWidget - - - - 0 - 0 - 682 - 391 - - - - Widget - - - - - - - - - Qt::Vertical - - - - 20 - 40 - - - - - - - - - 0 - 0 - - - - Qt::MoveAction - - - QListView::Snap - - - - - - - false - - - - - - Qt::NoArrow - - - - - - - Qt::Vertical - - - - 20 - 40 - - - - - - - - - - - - - - - false - - - - - - Qt::NoArrow - - - - - - - false - - - - - - - - - - - commandsView - addButton - removeButton - moveUpButton - moveDownButton - - - - diff --git a/src/plugins/android/android.pro b/src/plugins/android/android.pro index 701194791c2..2dc02530767 100644 --- a/src/plugins/android/android.pro +++ b/src/plugins/android/android.pro @@ -44,7 +44,6 @@ HEADERS += \ androidbuildapkstep.h \ androidsdkmanager.h \ androidavdmanager.h \ - adbcommandswidget.h \ androidsdkpackage.h \ androidsdkmodel.h \ androidsdkmanagerwidget.h \ @@ -91,7 +90,6 @@ SOURCES += \ androidbuildapkstep.cpp \ androidsdkmanager.cpp \ androidavdmanager.cpp \ - adbcommandswidget.cpp \ androidsdkpackage.cpp \ androidsdkmodel.cpp \ androidsdkmanagerwidget.cpp \ @@ -108,7 +106,6 @@ FORMS += \ addnewavddialog.ui \ androidcreatekeystorecertificate.ui \ androiddevicedialog.ui \ - adbcommandswidget.ui \ androidsdkmanagerwidget.ui RESOURCES = android.qrc diff --git a/src/plugins/android/android.qbs b/src/plugins/android/android.qbs index 777be666f49..7040ee0fdfb 100644 --- a/src/plugins/android/android.qbs +++ b/src/plugins/android/android.qbs @@ -20,9 +20,6 @@ Project { files: [ "android_global.h", "android.qrc", - "adbcommandswidget.cpp", - "adbcommandswidget.h", - "adbcommandswidget.ui", "addnewavddialog.ui", "androidavdmanager.cpp", "androidavdmanager.h", diff --git a/src/plugins/android/androidrunconfiguration.cpp b/src/plugins/android/androidrunconfiguration.cpp index 5393bb0de20..5b834fa673a 100644 --- a/src/plugins/android/androidrunconfiguration.cpp +++ b/src/plugins/android/androidrunconfiguration.cpp @@ -29,66 +29,45 @@ #include "androidglobal.h" #include "androidtoolchain.h" #include "androidmanager.h" -#include "adbcommandswidget.h" #include #include #include #include +#include #include #include #include -#include #include #include #include -#include -#include -#include -#include - -using namespace Android::Internal; using namespace ProjectExplorer; using namespace Utils; namespace Android { -BaseStringListAspect::BaseStringListAspect(const QString &settingsKey, Utils::Id id) +class BaseStringListAspect final : public Utils::StringAspect { - setSettingsKey(settingsKey); - setId(id); -} +public: + explicit BaseStringListAspect() = default; + ~BaseStringListAspect() final = default; -BaseStringListAspect::~BaseStringListAspect() = default; + void fromMap(const QVariantMap &map) final + { + // Pre Qt Creator 5.0 hack: Reads QStringList as QString + setValue(map.value(settingsKey()).toStringList().join('\n')); + } -void BaseStringListAspect::addToLayout(LayoutBuilder &builder) -{ - QTC_CHECK(!m_widget); - m_widget = new AdbCommandsWidget; - m_widget->setCommandList(value()); - m_widget->setTitleText(labelText()); - builder.addItem(m_widget.data()); - connect(m_widget.data(), &AdbCommandsWidget::commandsChanged, this, [this] { - BaseAspect::setValue(m_widget->commandsList()); - emit changed(); - }); -} - -QStringList BaseStringListAspect::value() const -{ - return BaseAspect::value().toStringList(); -} - -void BaseStringListAspect::setValue(const QStringList &value) -{ - BaseAspect::setValue(value); - if (m_widget) - m_widget->setCommandList(value); -} + void toMap(QVariantMap &map) const final + { + // Pre Qt Creator 5.0 hack: Writes QString as QStringList + map.insert(settingsKey(), value().split('\n')); + } +}; AndroidRunConfiguration::AndroidRunConfiguration(Target *target, Utils::Id id) : RunConfiguration(target, id) @@ -123,16 +102,16 @@ AndroidRunConfiguration::AndroidRunConfiguration(Target *target, Utils::Id id) .arg(Core::Constants::IDE_DISPLAY_NAME)); auto preStartShellCmdAspect = addAspect(); + preStartShellCmdAspect->setDisplayStyle(StringAspect::TextEditDisplay); preStartShellCmdAspect->setId(Constants::ANDROID_PRESTARTSHELLCMDLIST); preStartShellCmdAspect->setSettingsKey("Android.PreStartShellCmdListKey"); - preStartShellCmdAspect->setLabelText( - tr("Shell commands to run on Android device before application launch.")); + preStartShellCmdAspect->setLabelText(tr("Pre-launch on-device shell commands:")); auto postStartShellCmdAspect = addAspect(); + postStartShellCmdAspect->setDisplayStyle(StringAspect::TextEditDisplay); postStartShellCmdAspect->setId(Constants::ANDROID_POSTFINISHSHELLCMDLIST); postStartShellCmdAspect->setSettingsKey("Android.PostStartShellCmdListKey"); - postStartShellCmdAspect->setLabelText( - tr("Shell commands to run on Android device after application quits.")); + postStartShellCmdAspect->setLabelText(tr("Post-quit on-device shell commands:")); setUpdater([this, target] { const BuildTargetInfo bti = buildTargetInfo(); diff --git a/src/plugins/android/androidrunconfiguration.h b/src/plugins/android/androidrunconfiguration.h index b2ad1cc5617..57894330f89 100644 --- a/src/plugins/android/androidrunconfiguration.h +++ b/src/plugins/android/androidrunconfiguration.h @@ -27,31 +27,10 @@ #include "android_global.h" -#include "adbcommandswidget.h" - #include -#include namespace Android { -class BaseStringListAspect : public Utils::BaseAspect -{ - Q_OBJECT - -public: - explicit BaseStringListAspect(const QString &settingsKey = QString(), - Utils::Id id = Utils::Id()); - ~BaseStringListAspect() override; - - void addToLayout(Utils::LayoutBuilder &builder) override; - - QStringList value() const; - void setValue(const QStringList &val); - -private: - QPointer m_widget; // Owned by RunConfigWidget -}; - class ANDROID_EXPORT AndroidRunConfiguration : public ProjectExplorer::RunConfiguration { Q_OBJECT diff --git a/src/plugins/android/androidrunnerworker.cpp b/src/plugins/android/androidrunnerworker.cpp index b07e6210599..f6f3f3b1587 100644 --- a/src/plugins/android/androidrunnerworker.cpp +++ b/src/plugins/android/androidrunnerworker.cpp @@ -297,14 +297,18 @@ AndroidRunnerWorker::AndroidRunnerWorker(RunWorker *runner, const QString &packa } if (auto aspect = runControl->aspect(Constants::ANDROID_PRESTARTSHELLCMDLIST)) { - for (const QString &shellCmd : static_cast(aspect)->value()) + const QStringList commands = + static_cast(aspect)->value().split('\n', Qt::SkipEmptyParts); + for (const QString &shellCmd : commands) m_beforeStartAdbCommands.append(QString("shell %1").arg(shellCmd)); } for (const QString &shellCmd : runner->recordedData(Constants::ANDROID_PRESTARTSHELLCMDLIST).toStringList()) m_beforeStartAdbCommands.append(QString("shell %1").arg(shellCmd)); if (auto aspect = runControl->aspect(Constants::ANDROID_POSTFINISHSHELLCMDLIST)) { - for (const QString &shellCmd : static_cast(aspect)->value()) + const QStringList commands = + static_cast(aspect)->value().split('\n', Qt::SkipEmptyParts); + for (const QString &shellCmd : commands) m_afterFinishAdbCommands.append(QString("shell %1").arg(shellCmd)); } for (const QString &shellCmd : runner->recordedData(Constants::ANDROID_POSTFINISHSHELLCMDLIST).toStringList())