Fix creating a project from a Recent preset with custom size

When creating a project from a Recent preset, everything worked well if
the user selected a predefined size. However, when typing a custom width
and custom height, the recent was saved with the selected/active item in
the screen size combobox.

This patch does the following to fix the issue:
* Save the recent info with the screen size written in the text fields
  (since they're updated when the user selects a different size from
  combobox but the user can type in any other value, the text fields
  always reflect the user's choice of width and height)
* When loading a preset for display in the UI, if the preset was saved
  with a custom size that does not exist in the predefined list (i.e. in
  the combobox), whether that is a custom size or a different
  orientation of a predefined size, then append an item into the screen
  size combobox so that this custom size will appear as one of the
  predefined sizes for the preset. This is both for visual reasons (so
  that the combobox would have a valid item active on display) and in
  order to be able to save in the backend model this choice (when the
  user decides to create the project)
* Update the ListField::selectRow function so that it does not attempt
  to select an item that does not exist. The reason is that, when the
  user clicks "Create" to create the project, the QWizard will be reset
  to the first page, and any modification we might have made to the
  model of the screen size combobox will be gone - and thus the
  selection would be invalid, which would normally cause the wizard to
  fail on page->isComplete(). By not allowing it to select an item in
  this case, we rely on the custom width and custom height fields to
  hold the real values.

Task-number: QDS-5691
Change-Id: I9e848c5f4957252eb414da7e7146f9f8e7be760c
Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
Samuel Ghinet
2022-01-17 13:24:05 +02:00
parent f708e0e70c
commit d023dfca1e
6 changed files with 39 additions and 18 deletions

View File

@@ -1157,12 +1157,16 @@ QStandardItemModel *ListField::itemModel()
return m_itemModel;
}
void ListField::selectRow(int row)
bool ListField::selectRow(int row)
{
auto index = itemModel()->index(row, 0);
QModelIndex index = itemModel()->index(row, 0);
if (!index.isValid())
return false;
selectionModel()->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect);
this->updateIndex();
return true;
}
QItemSelectionModel *ListField::selectionModel() const
@@ -1271,12 +1275,15 @@ QVariant ComboBoxField::toSettings() const
return {};
}
void ComboBoxField::selectRow(int row)
bool ComboBoxField::selectRow(int row)
{
ListField::selectRow(row);
if (!ListField::selectRow(row))
return false;
auto w = qobject_cast<QComboBox *>(widget());
w->setCurrentIndex(row);
return true;
}
int ComboBoxField::selectedRow() const

View File

@@ -282,7 +282,7 @@ public:
~ListField() override;
QStandardItemModel *model() { return m_itemModel; }
virtual void selectRow(int row);
virtual bool selectRow(int row);
protected:
bool parseData(const QVariant &data, QString *errorMessage) override;
@@ -351,7 +351,7 @@ private:
}
public:
void selectRow(int row) override;
bool selectRow(int row) override;
int selectedRow() const;
};

View File

@@ -68,6 +68,7 @@ void CreateProject::processFieldPage(ProjectExplorer::JsonFieldPage *page)
auto widthField = dynamic_cast<ProjectExplorer::LineEditField *>(page->jsonField("CustomScreenWidth"));
auto heightField = dynamic_cast<ProjectExplorer::LineEditField *>(page->jsonField("CustomScreenHeight"));
// TODO: use m_wizard to set these text items?
if (widthField && heightField) {
if (!m_customWidth.isEmpty() && !m_customHeight.isEmpty()) {
widthField->setText(m_customWidth);

View File

@@ -170,17 +170,26 @@ void QdsNewDialog::onProjectCanBeCreatedChanged(bool value)
emit fieldsValidChanged();
}
void QdsNewDialog::updateScreenSizes()
{
int index = m_wizard.screenSizeIndex(m_currentPreset->screenSizeName);
if (index > -1) {
setScreenSizeIndex(index);
} else {
index = m_screenSizeModel->appendItem(m_currentPreset->screenSizeName);
setScreenSizeIndex(index);
}
m_screenSizeModel->reset();
}
void QdsNewDialog::onWizardCreated(QStandardItemModel *screenSizeModel, QStandardItemModel *styleModel)
{
m_screenSizeModel->setBackendModel(screenSizeModel);
m_styleModel->setBackendModel(styleModel);
if (m_qmlDetailsLoaded) {
int index = m_wizard.screenSizeIndex(m_currentPreset->screenSizeName);
if (index > -1)
setScreenSizeIndex(index);
m_screenSizeModel->reset();
updateScreenSizes();
emit haveVirtualKeyboardChanged();
emit haveTargetQtVersionChanged();
@@ -288,11 +297,7 @@ void QdsNewDialog::setWizardFactories(QList<Core::IWizardFactory *> factories_,
emit projectLocationChanged(); // So that QML knows to update the field
if (m_qmlDetailsLoaded) {
int index = m_wizard.screenSizeIndex(m_currentPreset->screenSizeName);
if (index > -1)
setScreenSizeIndex(index);
m_screenSizeModel->reset();
updateScreenSizes();
}
if (m_qmlStylesLoaded)
@@ -334,9 +339,9 @@ void QdsNewDialog::accept()
.execute();
PresetItem item = m_wizard.preset();
QString screenSize = m_wizard.screenSizeName(m_qmlScreenSizeIndex);
QString customSizeName = m_qmlCustomWidth + " x " + m_qmlCustomHeight;
m_recentsStore.add(item.categoryId, item.name, screenSize);
m_recentsStore.add(item.categoryId, item.name, customSizeName);
m_dialog->close();
m_dialog->deleteLater();

View File

@@ -131,6 +131,8 @@ private:
QString projectDescription() const { return m_qmlProjectDescription; }
void updateScreenSizes();
private slots:
void onDeletingWizard();
void onWizardCreated(QStandardItemModel *screenSizeModel, QStandardItemModel *styleModel);

View File

@@ -87,6 +87,12 @@ public:
return {};
}
int appendItem(const QString &text)
{
m_backendModel->appendRow(new QStandardItem{text});
return rowCount(QModelIndex{}) - 1;
}
QHash<int, QByteArray> roleNames() const override
{
if (m_backendModel)