QmlOutline: Show icons for known types

This commit is contained in:
Kai Koehne
2010-07-13 17:05:47 +02:00
parent 874a185f53
commit ce32f25ba3
24 changed files with 141 additions and 22 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 416 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 406 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 512 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 367 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 320 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 579 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 303 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 339 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 459 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 334 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 236 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 316 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 224 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 249 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 327 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 247 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 544 B

View File

@@ -28,17 +28,29 @@
**************************************************************************/
#include "qmljsicons.h"
#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QHash>
#include <QtCore/QPair>
using namespace QmlJS;
using namespace QmlJS::AST;
enum {
debug = false
};
namespace QmlJS {
Icons *Icons::m_instance = 0;
class IconsPrivate
{
public:
QIcon elementIcon;
QIcon propertyIcon;
QHash<QPair<QString,QString>,QIcon> iconHash;
QString resourcePath;
};
} // namespace QmlJS
@@ -52,9 +64,55 @@ Icons::Icons()
Icons::~Icons()
{
m_instance = 0;
delete m_d;
}
Icons *Icons::instance()
{
if (!m_instance)
m_instance = new Icons();
return m_instance;
}
void Icons::setIconFilesPath(const QString &iconPath)
{
if (iconPath == m_d->resourcePath)
return;
m_d->resourcePath = iconPath;
if (debug)
qDebug() << "QmlJSIcons -" << "parsing" << iconPath;
QDir topDir(iconPath);
foreach (const QFileInfo &subDirInfo, topDir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot)) {
if (debug)
qDebug() << "QmlJSIcons - parsing" << subDirInfo.absoluteFilePath();
const QString packageName = subDirInfo.fileName();
QDir subDir(subDirInfo.absoluteFilePath() + QLatin1String("/16x16"));
foreach (const QFileInfo &iconFile, subDir.entryInfoList(QDir::Files)) {
QIcon icon(iconFile.absoluteFilePath());
if (icon.isNull()) {
if (debug)
qDebug() << "QmlJSIcons - skipping" << iconFile.absoluteFilePath();
continue;
}
if (debug)
qDebug() << "QmlJSIcons - adding" << packageName << iconFile.baseName() << "icon to database";
QPair<QString,QString> element(packageName, iconFile.baseName());
m_d->iconHash.insert(element, icon);
}
}
}
QIcon Icons::icon(const QString &packageName, const QString typeName) const
{
QPair<QString,QString> element(packageName, typeName);
if (debug)
qDebug() << "QmlJSIcons - icon for" << packageName << typeName << "requested" << m_d->iconHash.contains(element);
return m_d->iconHash.value(element);
}
QIcon Icons::icon(Node *node) const
{
if (dynamic_cast<AST::UiObjectDefinition*>(node)) {

View File

@@ -41,14 +41,21 @@ class IconsPrivate;
class QMLJS_EXPORT Icons
{
public:
Icons();
~Icons();
static Icons *instance();
void setIconFilesPath(const QString &iconPath);
QIcon icon(const QString &packageName, const QString typeName) const;
QIcon icon(AST::Node *node) const;
QIcon objectDefinitionIcon() const;
QIcon scriptBindingIcon() const;
private:
Icons();
static Icons *m_instance;
IconsPrivate *m_d;
};

View File

@@ -850,7 +850,7 @@ void QmlJSTextEditor::updateOutlineNow()
return;
}
m_outlineModel->update(document);
m_outlineModel->update(document, snapshot);
updateOutlineIndexNow();
}

View File

@@ -39,6 +39,7 @@
#include "qmljsoutline.h"
#include "qmljspreviewrunner.h"
#include "qmljsquickfix.h"
#include "qmljs/qmljsicons.h"
#include <qmldesigner/qmldesignerconstants.h>
@@ -186,6 +187,13 @@ void QmlJSEditorPlugin::extensionsInitialized()
{
}
ExtensionSystem::IPlugin::ShutdownFlag QmlJSEditorPlugin::aboutToShutdown()
{
delete QmlJS::Icons::instance(); // delete object held by singleton
return IPlugin::aboutToShutdown();
}
void QmlJSEditorPlugin::openPreview()
{
Core::EditorManager *em = Core::EditorManager::instance();

View File

@@ -77,6 +77,7 @@ public:
// IPlugin
bool initialize(const QStringList &arguments, QString *errorMessage = 0);
void extensionsInitialized();
ShutdownFlag aboutToShutdown();
static QmlJSEditorPlugin *instance()
{ return m_instance; }

View File

@@ -1,7 +1,11 @@
#include "qmloutlinemodel.h"
#include <qmljs/parser/qmljsastvisitor_p.h>
#include <qmljs/qmljsinterpreter.h>
#include <qmljs/qmljslookupcontext.h>
#include <coreplugin/icore.h>
#include <QtCore/QDebug>
#include <QtCore/QTime>
#include <typeinfo>
using namespace QmlJS;
@@ -11,7 +15,8 @@ enum {
debug = false
};
namespace {
namespace QmlJSEditor {
namespace Internal {
class QmlOutlineModelSync : protected AST::Visitor
{
@@ -22,14 +27,23 @@ public:
{
}
void operator()(Document::Ptr doc)
void operator()(Document::Ptr doc, const Snapshot &snapshot)
{
m_nodeToIndex.clear();
// Set up lookup context once to do the element type lookup
//
// We're simplifying here by using the root context everywhere
// (empty node list). However, creating the LookupContext is quite expensive (about 3ms),
// and there is AFAIK no way to introduce new type names in a sub-context.
m_context = LookupContext::create(doc, snapshot, QList<AST::Node*>());
if (debug)
qDebug() << "QmlOutlineModel ------";
if (doc && doc->ast())
doc->ast()->accept(this);
m_context.clear();
}
private:
@@ -64,7 +78,7 @@ private:
}
typedef QPair<QString,QString> ElementType;
bool visit(AST::UiObjectDefinition *objDef)
{
if (!validElement(objDef)) {
@@ -78,8 +92,14 @@ private:
+ objDef->lastSourceLocation().length;
const QString typeName = asString(objDef->qualifiedTypeNameId);
const QString id = getId(objDef);
QModelIndex index = m_model->enterElement(typeName, id, location);
if (!m_typeToIcon.contains(typeName)) {
m_typeToIcon.insert(typeName, getIcon(objDef));
}
const QIcon icon = m_typeToIcon.value(typeName);
QString id = getId(objDef);
QModelIndex index = m_model->enterElement(typeName, id, icon, location);
m_nodeToIndex.insert(objDef, index);
return true;
}
@@ -112,10 +132,33 @@ private:
}
bool validElement(AST::UiObjectDefinition *objDef) {
// For 'Rectangle { id }', id is parsed as UiObjectDefinition ... Filter this out.ctan
// For 'Rectangle { id }', id is parsed as UiObjectDefinition ... Filter this out.
return objDef->qualifiedTypeNameId->name->asString().at(0).isUpper();
}
QIcon getIcon(AST::UiObjectDefinition *objDef) {
const QmlJS::Interpreter::Value *value = m_context->evaluate(objDef->qualifiedTypeNameId);
if (const Interpreter::ObjectValue *objectValue = value->asObjectValue()) {
do {
QString module;
QString typeName;
if (const Interpreter::QmlObjectValue *qmlObjectValue =
dynamic_cast<const Interpreter::QmlObjectValue*>(objectValue)) {
module = qmlObjectValue->packageName();
}
typeName = objectValue->className();
QIcon icon = m_model->m_icons->icon(module, typeName);
if (! icon.isNull())
return icon;
objectValue = objectValue->prototype(m_context->context());
} while (objectValue);
}
return QIcon();
}
QString getId(AST::UiObjectDefinition *objDef) {
QString id;
for (AST::UiObjectMemberList *it = objDef->initializer->members; it; it = it->next) {
@@ -137,19 +180,19 @@ private:
QmlOutlineModel *m_model;
LookupContext::Ptr m_context;
QHash<AST::Node*, QModelIndex> m_nodeToIndex;
QHash<QString, QIcon> m_typeToIcon;
int indent;
};
} // namespace
namespace QmlJSEditor {
namespace Internal {
QmlOutlineModel::QmlOutlineModel(QObject *parent) :
QStandardItemModel(parent)
{
m_icons = Icons::instance();
const QString resourcePath = Core::ICore::instance()->resourcePath();
QmlJS::Icons::instance()->setIconFilesPath(resourcePath + "/qmlicons");
}
QmlJS::Document::Ptr QmlOutlineModel::document() const
@@ -157,7 +200,7 @@ QmlJS::Document::Ptr QmlOutlineModel::document() const
return m_document;
}
void QmlOutlineModel::update(QmlJS::Document::Ptr doc)
void QmlOutlineModel::update(QmlJS::Document::Ptr doc, const QmlJS::Snapshot &snapshot)
{
m_document = doc;
@@ -166,12 +209,12 @@ void QmlOutlineModel::update(QmlJS::Document::Ptr doc)
m_currentItem = invisibleRootItem();
QmlOutlineModelSync syncModel(this);
syncModel(doc);
syncModel(doc, snapshot);
emit updated();
}
QModelIndex QmlOutlineModel::enterElement(const QString &type, const QString &id, const AST::SourceLocation &sourceLocation)
QModelIndex QmlOutlineModel::enterElement(const QString &type, const QString &id, const QIcon &icon, const AST::SourceLocation &sourceLocation)
{
QStandardItem *item = enterNode(sourceLocation);
if (!id.isEmpty()) {
@@ -179,8 +222,8 @@ QModelIndex QmlOutlineModel::enterElement(const QString &type, const QString &id
} else {
item->setText(type);
}
item->setIcon(icon);
item->setToolTip(type);
item->setIcon(m_icons.objectDefinitionIcon());
return item->index();
}
@@ -193,7 +236,7 @@ QModelIndex QmlOutlineModel::enterProperty(const QString &name, const AST::Sourc
{
QStandardItem *item = enterNode(sourceLocation);
item->setText(name);
item->setIcon(m_icons.scriptBindingIcon());
item->setIcon(m_icons->scriptBindingIcon());
return item->index();
}

View File

@@ -20,9 +20,9 @@ public:
QmlOutlineModel(QObject *parent = 0);
QmlJS::Document::Ptr document() const;
void update(QmlJS::Document::Ptr doc);
void update(QmlJS::Document::Ptr doc, const QmlJS::Snapshot &snapshot);
QModelIndex enterElement(const QString &typeName, const QString &id, const QmlJS::AST::SourceLocation &location);
QModelIndex enterElement(const QString &typeName, const QString &id, const QIcon &icon, const QmlJS::AST::SourceLocation &location);
void leaveElement();
QModelIndex enterProperty(const QString &name, const QmlJS::AST::SourceLocation &location);
@@ -40,7 +40,9 @@ private:
QmlJS::Document::Ptr m_document;
QList<int> m_treePos;
QStandardItem *m_currentItem;
QmlJS::Icons m_icons;
QmlJS::Icons *m_icons;
friend class QmlOutlineModelSync;
};
} // namespace Internal