Commit 21347cd7 authored by Marco Bubke's avatar Marco Bubke
Browse files

QmlDesinger: Add combo box to add imports



Change-Id: Ic626f07d4a2bde32495b70b8c5726cb4b6821da3
Reviewed-by: default avatarThomas Hartmann <Thomas.Hartmann@digia.com>
parent 45b6fd25
......@@ -63,6 +63,11 @@ void ImportLabel::setImport(const Import &import)
m_import = import;
}
const Import ImportLabel::import() const
{
return m_import;
}
void ImportLabel::emitRemoveImport()
{
emit removeImport(m_import);
......
......@@ -44,6 +44,7 @@ public:
explicit ImportLabel(QWidget *parent = 0);
void setImport(const Import &import);
const Import import() const;
signals:
void removeImport(const Import &import);
......
......@@ -55,6 +55,8 @@ WidgetInfo ImportManagerView::widgetInfo()
if (m_importsWidget == 0) {
m_importsWidget = new ImportsWidget;
connect(m_importsWidget, SIGNAL(removeImport(Import)), this, SLOT(removeImport(Import)));
connect(m_importsWidget, SIGNAL(addImport(Import)), this, SLOT(addImport(Import)));
if (model())
m_importsWidget->setImports(model()->imports());
}
......@@ -66,26 +68,34 @@ void ImportManagerView::modelAttached(Model *model)
{
AbstractView::modelAttached(model);
if (m_importsWidget)
if (m_importsWidget) {
m_importsWidget->setImports(model->imports());
m_importsWidget->setPossibleImports(model->possibleImports());
m_importsWidget->setUsedImports(model->usedImports());
}
}
void ImportManagerView::modelAboutToBeDetached(Model *model)
{
if (m_importsWidget)
m_importsWidget->removeAllImports();
if (m_importsWidget) {
m_importsWidget->removeImports();
m_importsWidget->removePossibleImports();
m_importsWidget->removeUsedImports();
}
AbstractView::modelAboutToBeDetached(model);
}
void ImportManagerView::nodeCreated(const ModelNode &createdNode)
{
// handle case that the import is used
if (m_importsWidget)
m_importsWidget->setUsedImports(model()->usedImports());
}
void ImportManagerView::nodeAboutToBeRemoved(const ModelNode &removedNode)
{
// handle case that the import is unused
if (m_importsWidget)
m_importsWidget->setUsedImports(model()->usedImports());
}
void ImportManagerView::nodeRemoved(const ModelNode &/*removedNode*/, const NodeAbstractProperty &/*parentProperty*/, AbstractView::PropertyChangeFlags /*propertyChange*/)
......@@ -210,8 +220,11 @@ void ImportManagerView::nodeOrderChanged(const NodeListProperty &/*listProperty*
void ImportManagerView::importsChanged(const QList<Import> &addedImports, const QList<Import> &removedImports)
{
if (m_importsWidget)
if (m_importsWidget) {
m_importsWidget->setImports(model()->imports());
m_importsWidget->setPossibleImports(model()->possibleImports());
m_importsWidget->setUsedImports(model()->usedImports());
}
}
void ImportManagerView::auxiliaryDataChanged(const ModelNode &/*node*/, const PropertyName &/*name*/, const QVariant &/*data*/)
......@@ -235,4 +248,10 @@ void ImportManagerView::removeImport(const Import &import)
model()->changeImports(QList<Import>(), QList<Import>() << import);
}
void ImportManagerView::addImport(const Import &import)
{
if (model())
model()->changeImports(QList<Import>() << import, QList<Import>());
}
} // namespace QmlDesigner
......@@ -95,6 +95,7 @@ public:
private slots:
void removeImport(const Import &import);
void addImport(const Import &import);
private:
ImportsWidget *m_importsWidget;
......
......@@ -30,6 +30,7 @@
#include "importswidget.h"
#include <QVBoxLayout>
#include <QComboBox>
#include "importlabel.h"
......@@ -39,15 +40,54 @@ ImportsWidget::ImportsWidget(QWidget *parent) :
QWidget(parent)
{
setWindowTitle(tr("Import Manager"));
m_addImportComboBox = new QComboBox(this);
connect(m_addImportComboBox, SIGNAL(activated(int)), this, SLOT(addSelectedImport(int)));
}
void ImportsWidget::removeAllImports()
void ImportsWidget::removeImports()
{
qDeleteAll(m_importLabels);
m_importLabels.clear();
updateLayout();
}
static bool isImportAlreadyUsed(const Import &import, QList<ImportLabel*> importLabels)
{
foreach (ImportLabel *importLabel, importLabels) {
if (importLabel->import() == import)
return true;
}
return false;
}
void ImportsWidget::setPossibleImports(const QList<Import> &possibleImports)
{
m_addImportComboBox->clear();
foreach (const Import &possibleImport, possibleImports) {
if (!isImportAlreadyUsed(possibleImport, m_importLabels))
m_addImportComboBox->addItem(possibleImport.toString(true), QVariant::fromValue(possibleImport));
}
}
void ImportsWidget::removePossibleImports()
{
m_addImportComboBox->clear();
}
void ImportsWidget::setUsedImports(const QList<Import> &usedImports)
{
foreach (ImportLabel *importLabel, m_importLabels)
importLabel->setDisabled(usedImports.contains(importLabel->import()));
}
void ImportsWidget::removeUsedImports()
{
foreach (ImportLabel *importLabel, m_importLabels)
importLabel->setEnabled(true);
}
static bool importLess(const Import &firstImport, const Import &secondImport)
{
if (firstImport.url() == "QtQuick")
......@@ -90,6 +130,7 @@ void ImportsWidget::setImports(const QList<Import> &imports)
updateLayout();
}
void ImportsWidget::updateLayout()
{
delete layout();
......@@ -97,10 +138,19 @@ void ImportsWidget::updateLayout()
QVBoxLayout *layout = new QVBoxLayout(this);
layout->setSpacing(0);
layout->addWidget(m_addImportComboBox);
foreach (ImportLabel *importLabel, m_importLabels)
layout->addWidget(importLabel);
layout->addStretch();
}
void ImportsWidget::addSelectedImport(int addImportComboBoxIndex)
{
Import selectedImport = m_addImportComboBox->itemData(addImportComboBoxIndex).value<Import>();
emit addImport(selectedImport);
}
} // namespace QmlDesigner
......@@ -34,6 +34,10 @@
#include <QWidget>
QT_BEGIN_NAMESPACE
class QComboBox;
QT_END_NAMESPACE
namespace QmlDesigner {
class ImportLabel;
......@@ -45,16 +49,27 @@ public:
explicit ImportsWidget(QWidget *parent = 0);
void setImports(const QList<Import> &imports);
void removeAllImports();
void removeImports();
void setPossibleImports(const QList<Import> &possibleImports);
void removePossibleImports();
void setUsedImports(const QList<Import> &possibleImports);
void removeUsedImports();
signals:
void removeImport(const Import &import);
void addImport(const Import &import);
protected:
void updateLayout();
private slots:
void addSelectedImport(int addImportComboBoxIndex);
private:
QList<ImportLabel*> m_importLabels;
QComboBox *m_addImportComboBox;
};
} // namespace QmlDesigner
......
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