Skip to content

Commit

Permalink
fix(cpn): Selection of sound packs in update process (EdgeTX#3848)
Browse files Browse the repository at this point in the history
* chore(cpn): increase github REST api page size for sound packs

* chore(cpn): add selection mode parameter to chooser dialog

* chore(cpn): remove cli debug messages when progress dialog available

* chore(cpn): carve out filterAssets from setFilteredAssets and call new function

* fix(cpn): refactor update sounds asset filtering

* chore(cpn): move code block

* fix(cpn): remove trailing hyphen from default sounds asset filter
  • Loading branch information
Neil Horne authored Sep 7, 2023
1 parent 73a4a23 commit fec64a9
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 95 deletions.
3 changes: 2 additions & 1 deletion companion/src/updates/chooserdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "chooserdialog.h"
#include "ui_chooserdialog.h"

ChooserDialog::ChooserDialog(QWidget * parent, QString title, QStandardItemModel * itemModel) :
ChooserDialog::ChooserDialog(QWidget * parent, QString title, QStandardItemModel * itemModel, QAbstractItemView::SelectionMode mode) :
QDialog(parent),
ui(new Ui::ChooserDialog)
{
Expand All @@ -32,6 +32,7 @@ ChooserDialog::ChooserDialog(QWidget * parent, QString title, QStandardItemModel

ui->listView->setModel(itemModel);
ui->listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
ui->listView->setSelectionMode(mode);

connect(ui->buttonBox, &QDialogButtonBox::accepted, [=]() {
QDialog::accept();
Expand Down
4 changes: 3 additions & 1 deletion companion/src/updates/chooserdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <QString>
#include <QStandardItemModel>
#include <QItemSelectionModel>
#include <QAbstractItemView>

namespace Ui {
class ChooserDialog;
Expand All @@ -35,7 +36,8 @@ class ChooserDialog : public QDialog
Q_OBJECT

public:
ChooserDialog(QWidget * parent, QString title, QStandardItemModel * itemModel);
ChooserDialog(QWidget * parent, QString title, QStandardItemModel * itemModel,
QAbstractItemView::SelectionMode selMode = QAbstractItemView::SingleSelection);
virtual ~ChooserDialog();

QItemSelectionModel* selectedItems();
Expand Down
2 changes: 1 addition & 1 deletion companion/src/updates/chooserdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<item>
<widget class="QListView" name="listView">
<property name="selectionMode">
<enum>QAbstractItemView::MultiSelection</enum>
<enum>QAbstractItemView::SingleSelection</enum>
</property>
</widget>
</item>
Expand Down
32 changes: 20 additions & 12 deletions companion/src/updates/updateinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,25 @@ bool UpdateInterface::decompress()
return true;
}

bool UpdateInterface::filterAssets(const UpdateParameters::AssetParams & ap)
{
QString pattern(m_params->buildFilterPattern(ap.filterType, ap.filter));
m_repo->assets()->setFilterPattern(pattern);
m_status->reportProgress(tr("Asset filter applied: %1 - %2 found").arg(pattern).arg(m_repo->assets()->count()), QtDebugMsg);

if (m_repo->assets()->count() < 1) {
m_status->reportProgress(tr("No assets found in release '%1' using filter '%2'").arg(m_repo->releases()->name()).arg(pattern), QtCriticalMsg);
return false;
}
else if (ap.maxExpected > 0 && m_repo->assets()->count() > ap.maxExpected) {
m_status->reportProgress(tr("%1 assets found when %2 expected in release '%3' using filter '%4'")
.arg(m_repo->assets()->count() + 1).arg(ap.maxExpected).arg(m_repo->releases()->name().arg(pattern)), QtCriticalMsg);
return false;
}

return true;
}

bool UpdateInterface::flagAssets()
{
m_status->progressMessage(tr("Flagging assets"));
Expand Down Expand Up @@ -743,19 +762,8 @@ bool UpdateInterface::retrieveRepoJsonFile(const QString & filename, QJsonDocume

bool UpdateInterface::setFilteredAssets(const UpdateParameters::AssetParams & ap)
{
QString pattern(m_params->buildFilterPattern(ap.filterType, ap.filter));
m_repo->assets()->setFilterPattern(pattern);
m_status->reportProgress(tr("Asset filter applied: %1 - %2 found").arg(pattern).arg(m_repo->assets()->count()), QtDebugMsg);

if (m_repo->assets()->count() < 1) {
m_status->reportProgress(tr("No assets found in release '%1' using filter '%2'").arg(m_repo->releases()->name()).arg(pattern), QtCriticalMsg);
if (!filterAssets(ap))
return false;
}
else if (ap.maxExpected > 0 && m_repo->assets()->count() > ap.maxExpected) {
m_status->reportProgress(tr("%1 assets found when %2 expected in release '%3' using filter '%4'")
.arg(m_repo->assets()->count() + 1).arg(ap.maxExpected).arg(m_repo->releases()->name().arg(pattern)), QtCriticalMsg);
return false;
}

for (int i = 0; i < m_repo->assets()->count(); i++) {
m_repo->assets()->getSetId(i);
Expand Down
1 change: 1 addition & 0 deletions companion/src/updates/updateinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class UpdateInterface : public QWidget
bool decompressFlaggedAssets();
const QString downloadDir() const;
bool downloadFlaggedAssets();
bool filterAssets(const UpdateParameters::AssetParams & ap);
void init(QString repo, QString nightly = "", int resultsPerPage = -1);
const bool isSettingsIndexValid() const;
UpdateNetwork* const network() const;
Expand Down
87 changes: 18 additions & 69 deletions companion/src/updates/updatesounds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
#include <QMessageBox>
#include <QStandardItem>
#include <QItemSelectionModel>
#include <QAbstractItemView>

UpdateSounds::UpdateSounds(QWidget * parent) :
UpdateInterface(parent, CID_Sounds, tr("Sounds"))
{
init(QString(GH_API_REPOS_EDGETX).append("/edgetx-sdcard-sounds"));
init(QString(GH_API_REPOS_EDGETX).append("/edgetx-sdcard-sounds"), "", 50);
langPacks = new QStandardItemModel();
}

Expand All @@ -50,7 +51,7 @@ void UpdateSounds::assetSettingsInit()
cad.processes(UPDFLG_Common_Asset);
cad.flags(cad.processes() | UPDFLG_CopyStructure);
cad.filterType(UpdateParameters::UFT_Startswith);
cad.filter("edgetx-sdcard-sounds-%LANGUAGE%-");
cad.filter("edgetx-sdcard-sounds-%LANGUAGE%");

qDebug() << "Asset settings initialised";
}
Expand All @@ -59,69 +60,18 @@ bool UpdateSounds::flagAssets()
{
status()->progressMessage(tr("Processing available sounds"));

QJsonDocument *json = new QJsonDocument();

const QString jsonFile("sounds.json");

if (!retrieveAssetsJsonFile(jsonFile, json)) {
status()->reportProgress(tr("Unable to retrieve asset '%1' from release '%2'").arg(jsonFile).arg(repo()->releases()->name()), QtDebugMsg);
// assume older release where file not an asset
if (!retrieveRepoJsonFile(jsonFile, json)) {
status()->reportProgress(tr("Unable to retrieve file '%1' from repo '%2'").arg(jsonFile).arg(repo()->path()), QtCriticalMsg);
delete json;
return false;
}
}

/*
{
"language": "en-GB",
"name": "British English Female",
"description": "British English Female Voice (en-GB-Libby)",
"directory": "en_gb-libby"
},
*/

// always refresh to allow for language change in radio profile
// always refresh to allow for language change in radio profile
langPacks->clear();

if (json->isArray()) {
const QJsonArray &arr = json->array();

foreach (const QJsonValue &v, arr) {
if (v.isObject()) {
const QJsonObject obj = v.toObject();
QString langVariant;
QString lang;

if (!obj.value("language").isUndefined()) {
langVariant = obj.value("language").toString();
lang = langVariant.split("-").at(0);
}

if (lang == params()->language) {
QStandardItem * item = new QStandardItem();

if (!obj.value("language").isUndefined())
item->setData(obj.value("language").toString(), IMDR_Language);
if (!obj.value("name").isUndefined())
item->setData(obj.value("name").toString(), IMDR_Name);
if (!obj.value("description").isUndefined())
item->setData(obj.value("description").toString(), Qt::DisplayRole);
if (!obj.value("directory").isUndefined())
item->setData(obj.value("directory").toString(), IMDR_Directory);

langPacks->appendRow(item);
}
}
}
}

delete json;

if (langPacks->rowCount() < 1) {
status()->reportProgress(tr("Language '%1' not listed in '%2'").arg(params()->language).arg(jsonFile), QtCriticalMsg);
UpdateParameters::AssetParams ap = params()->assets[0];
if (!filterAssets(ap))
return false;

for (int i = 0; i < repo()->assets()->count(); i++) {
repo()->assets()->getSetId(i);
QStandardItem * item = new QStandardItem();
item->setText(repo()->assets()->name());
langPacks->appendRow(item);
}

if (langPacks->rowCount() > 1) {
Expand All @@ -139,26 +89,25 @@ bool UpdateSounds::flagAssets()

QModelIndexList selIndexes = selItems->selectedIndexes();

for (int i = 0; i < selIndexes.size(); i++) {
if (!flagLanguageAsset(langPacks->data(selIndexes.at(i), IMDR_Directory).toString()))
return false;
}
if (!flagLanguageAsset(langPacks->data(selIndexes.at(0)).toString()))
return false;

dlg->deleteLater();
}
else if (langPacks->rowCount() == 1) {
if (!flagLanguageAsset(langPacks->data(langPacks->index(0, 0), IMDR_Directory).toString()))
if (!flagLanguageAsset(langPacks->data(langPacks->index(0, 0)).toString()))
return false;
}

return true;
}

bool UpdateSounds::flagLanguageAsset(QString lang)
bool UpdateSounds::flagLanguageAsset(QString asset)
{
status()->progressMessage(tr("Flagging assets"));

params()->language = lang;
UpdateParameters::AssetParams ap = params()->assets[0];
ap.filter = asset;
ap.filterType = UpdateParameters::UFT_Exact;
return setFilteredAssets(ap);
}
7 changes: 0 additions & 7 deletions companion/src/updates/updatesounds.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ class UpdateSounds : public UpdateInterface
virtual void assetSettingsInit() override;

private:
enum ItemModelDataRoles {
IMDR_Language = Qt::UserRole,
IMDR_Name,
IMDR_Directory,
};
Q_ENUM(ItemModelDataRoles)

QStandardItemModel *langPacks;

bool flagLanguageAsset(QString lang);
Expand Down
5 changes: 1 addition & 4 deletions companion/src/updates/updatestatus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ void UpdateStatus::reportProgress(QString text, QtMsgType type)
if (m_logLevel == QtDebugMsg ||
(m_logLevel == QtInfoMsg && type > QtDebugMsg) ||
(type < QtInfoMsg && type >= m_logLevel)) {
if (m_progress) {
if (m_progress)
m_progress->addMessage(text, type);
if (m_logLevel == QtDebugMsg)
qDebug() << text;
}
else
qDebug() << text;
}
Expand Down

0 comments on commit fec64a9

Please sign in to comment.