Commit 30d123e2 authored by Tim Jenssen's avatar Tim Jenssen

Wizards: add IconListField

 - reuse some code from ComboBoxField throw an abstract ListField class
 - ListField can handle more data like: icon, trToolTip
 - fix disabledIndex in ComboBoxField
 - adjust documentation

Change-Id: I00b6ab787fb2fad97dafff32786cf73c636c772d
Reviewed-by: Tobias Hunger's avatarTobias Hunger <tobias.hunger@qt.io>
parent 5c5bac67
......@@ -730,7 +730,10 @@
\endlist
\section2 Combo Box
\section2 List
\note The Combo Box and Icon List types are both variations of the List type,
and therefore they can have the same properties.
\code
{
......@@ -744,19 +747,36 @@
}
},
\endcode
or
\code
{
"name": "ChosenBuildSystem",
"trDisplayName": "Choose your build system:",
"type": "IconList",
"data":
{
"items": [
{ "trKey": "Qbs", "value": "qbs", "icon": "qbs_icon.png", "trToolTip": "Building with Qbs." },
{ "trKey": "QMake", "value": "qmake", "icon": "qmake_icon.png", "trToolTip": "Building with QMake." }
]
}
},
\endcode
\list
\li \c items specifies a list of items to put into the combo box. The
list can contain both JSON objects and plain strings. For JSON
objects, define \c trKey and \c value pairs, where the \c trKey is
the list item visible to users and \c value contains the data
associated with the item.
\li \c items specifies a list of items to put into the list type. The
list can contain both JSON objects and plain strings.
For JSON objects, define \c trKey and \c value pairs, where the
\c trKey is the list item visible to users and \c value contains
the data associated with the item.
In addition, you can use \c icon to specify an icon for the list
item and \c trToolTip to specify a tooltip for it.
\li \c index specifies the index to select when the combo box is
\li \c index specifies the index to select when the list type is
enabled. By default, it is set to \c 0.
\li \c disabledIndex specifies the index to show if the combo box is
\li \c disabledIndex specifies the index to show if the list type is
disabled.
\endlist
......
......@@ -42,7 +42,6 @@ QT_END_NAMESPACE
namespace Utils {
class MacroExpander;
class TextFieldComboBox;
} // namespace Utils
namespace ProjectExplorer {
......
......@@ -32,6 +32,16 @@
#include <QWidget>
#include <QString>
#include <QVariant>
#include <QDir>
#include <memory>
#include <vector>
QT_BEGIN_NAMESPACE
class QStandardItem;
class QStandardItemModel;
class QItemSelectionModel;
QT_END_NAMESPACE
namespace ProjectExplorer {
......@@ -170,25 +180,58 @@ private:
bool m_isModified = false;
};
class ComboBoxField : public JsonFieldPage::Field
class ListField : public JsonFieldPage::Field
{
private:
public:
enum SpecialRoles {
ValueRole = Qt::UserRole,
ConditionRole = Qt::UserRole + 1,
IconStringRole = Qt::UserRole + 2
};
ListField();
virtual ~ListField() override;
protected:
bool parseData(const QVariant &data, QString *errorMessage) override;
QWidget *createWidget(const QString &displayName, JsonFieldPage *page) override;
void setup(JsonFieldPage *page, const QString &name) override;
QWidget *createWidget(const QString &displayName, JsonFieldPage *page) override = 0;
void setup(JsonFieldPage *page, const QString &name) override = 0;
bool validate(Utils::MacroExpander *expander, QString *message) override;
void initializeData(Utils::MacroExpander *expander) override;
QStandardItemModel *itemModel();
QItemSelectionModel *selectionModel();
void setSelectionModel(QItemSelectionModel *selectionModel);
QSize maxIconSize();
private:
void addPossibleIconSize(const QIcon &icon);
void updateIndex();
QStringList m_itemList;
QStringList m_itemDataList;
QVariantList m_itemConditionList;
std::vector<std::unique_ptr<QStandardItem>> m_itemList;
QStandardItemModel *m_itemModel = nullptr;
QItemSelectionModel *m_selectionModel = nullptr;
int m_index = -1;
int m_disabledIndex = -1;
QSize m_maxIconSize;
mutable int m_savedIndex = -1;
};
class ComboBoxField : public ListField
{
public:
void setup(JsonFieldPage *page, const QString &name) override;
QWidget *createWidget(const QString &displayName, JsonFieldPage *page) override;
void initializeData(Utils::MacroExpander *expander) override;
};
class IconListField : public ListField
{
public:
void setup(JsonFieldPage *page, const QString &name) override;
QWidget *createWidget(const QString &displayName, JsonFieldPage *page) override;
void initializeData(Utils::MacroExpander *expander) override;
};
} // namespace ProjectExplorer
......@@ -34,6 +34,7 @@
#include <QCheckBox>
#include <QLineEdit>
#include <QComboBox>
#include <QListView>
#include <functional>
namespace {
......@@ -209,8 +210,8 @@ void ProjectExplorer::ProjectExplorerPlugin::testJsonWizardsLineEdit()
void ProjectExplorer::ProjectExplorerPlugin::testJsonWizardsComboBox()
{
QString errorMessage;
QWidget parent;
const QJsonArray items({"abc", "cde", "fgh"});
QJsonObject disabledComboBoxObject = createWidget("ComboBox", "Disabled", QJsonObject({ {{"disabledIndex", 2}, {"items", items}} }));
disabledComboBoxObject.insert("enabled", false);
......@@ -228,6 +229,8 @@ void ProjectExplorer::ProjectExplorerPlugin::testJsonWizardsComboBox()
QComboBox *defaultComboBox = findComboBox(wizard, "Default");
QVERIFY(defaultComboBox);
QCOMPARE(defaultComboBox->count(), items.count());
QCOMPARE(qPrintable(defaultComboBox->currentText()), "abc");
defaultComboBox->setCurrentIndex(2);
QCOMPARE(qPrintable(defaultComboBox->currentText()), "fgh");
......@@ -238,7 +241,49 @@ void ProjectExplorer::ProjectExplorerPlugin::testJsonWizardsComboBox()
QComboBox *disabledComboBox = findComboBox(wizard, "Disabled");
QVERIFY(disabledComboBox);
QEXPECT_FAIL("", "This is wrong, since ComboBox got condition items", Continue);
QCOMPARE(qPrintable(disabledComboBox->currentText()), "fgh");
}
void ProjectExplorer::ProjectExplorerPlugin::testJsonWizardsIconList()
{
QString errorMessage;
QWidget parent;
const QJsonArray items({
QJsonObject{
{"trKey", "item no1"},
{"condition", true},
{"icon", "../share/qtcreator/templates/wizards/qtquickstyleicons/default.png"}
},
QJsonObject{
{"trKey", "item no2"},
{"condition", false},
{"icon", "not_existing_path"}
},
QJsonObject{
{"trKey", "item no3"},
{"condition", true},
{"trToolTip", "MyToolTip"},
{"icon", "../share/qtcreator/templates/wizards/qtquickstyleicons/default.png"}
}
});
const QJsonArray widgets({
createWidget("IconList", "Fancy", QJsonObject{{"index", -1}, {"items", items}})
});
const QJsonObject pages = createFieldPageJsonObject(widgets);
const QJsonObject wizardObject = createGeneralWizard(pages);
JsonWizardFactory *factory = ProjectExplorer::JsonWizardFactory::createWizardFactory(wizardObject.toVariantMap(), QDir(), &errorMessage);
QVERIFY2(factory, qPrintable(errorMessage));
Utils::Wizard *wizard = factory->runWizard(QString(), &parent, Core::Id(), QVariantMap());
auto view = wizard->findChild<QListView *>("FancyIconList");
QCOMPARE(view->model()->rowCount(), 2);
QVERIFY(view->model()->index(0,0).data(Qt::DecorationRole).canConvert<QIcon>());
QIcon icon = view->model()->index(0,0).data(Qt::DecorationRole).value<QIcon>();
QVERIFY(!icon.isNull());
QVERIFY(!wizard->page(0)->isComplete());
}
......@@ -57,6 +57,7 @@ FieldPageFactory::FieldPageFactory()
JsonFieldPage::registerFieldFactory(QLatin1String("PathChooser"), []() { return new PathChooserField; });
JsonFieldPage::registerFieldFactory(QLatin1String("CheckBox"), []() { return new CheckBoxField; });
JsonFieldPage::registerFieldFactory(QLatin1String("ComboBox"), []() { return new ComboBoxField; });
JsonFieldPage::registerFieldFactory(QLatin1String("IconList"), []() { return new IconListField; });
}
Utils::WizardPage *FieldPageFactory::create(JsonWizard *wizard, Core::Id typeId, const QVariant &data)
......
......@@ -189,6 +189,7 @@ private slots:
void testJsonWizardsCheckBox();
void testJsonWizardsLineEdit();
void testJsonWizardsComboBox();
void testJsonWizardsIconList();
void testAnsiFilterOutputParser_data();
void testAnsiFilterOutputParser();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment