From 94264617bfdda8fbfc31921dc9ba49ffff84eaeb Mon Sep 17 00:00:00 2001 From: Kai Koehne Date: Mon, 12 Jul 2010 14:45:22 +0200 Subject: QmlJSEditor: Use QmlOutline model also in combo box The drop down combo box and the Outline in the sidebar now share the same model. --- src/plugins/qmljseditor/qmljseditor.cpp | 99 +++++++++++++++++++++-------- src/plugins/qmljseditor/qmljseditor.h | 10 ++- src/plugins/qmljseditor/qmljsoutline.cpp | 83 ++++-------------------- src/plugins/qmljseditor/qmljsoutline.h | 7 +- src/plugins/qmljseditor/qmloutlinemodel.cpp | 2 + src/plugins/qmljseditor/qmloutlinemodel.h | 3 + 6 files changed, 102 insertions(+), 102 deletions(-) diff --git a/src/plugins/qmljseditor/qmljseditor.cpp b/src/plugins/qmljseditor/qmljseditor.cpp index 1d51d1be33..9142231c5c 100644 --- a/src/plugins/qmljseditor/qmljseditor.cpp +++ b/src/plugins/qmljseditor/qmljseditor.cpp @@ -32,6 +32,7 @@ #include "qmljshighlighter.h" #include "qmljseditorplugin.h" #include "qmljsmodelmanager.h" +#include "qmloutlinemodel.h" #include #include @@ -70,9 +71,11 @@ #include #include +#include #include #include #include +#include enum { UPDATE_DOCUMENT_DEFAULT_INTERVAL = 50, @@ -612,6 +615,7 @@ QString QmlJSEditorEditable::preferredMode() const QmlJSTextEditor::QmlJSTextEditor(QWidget *parent) : TextEditor::BaseTextEditor(parent), m_methodCombo(0), + m_outlineModel(new QmlOutlineModel(this)), m_modelManager(0), m_contextPane(0) { @@ -688,6 +692,16 @@ bool QmlJSTextEditor::isOutdated() const return false; } +QmlOutlineModel *QmlJSTextEditor::outlineModel() const +{ + return m_outlineModel; +} + +QModelIndex QmlJSTextEditor::outlineModelIndex() const +{ + return m_outlineModelIndex; +} + Core::IEditor *QmlJSEditorEditable::duplicate(QWidget *parent) { QmlJSTextEditor *newEditor = new QmlJSTextEditor(parent); @@ -794,33 +808,37 @@ void QmlJSTextEditor::modificationChanged(bool changed) m_modelManager->fileChangedOnDisk(file()->fileName()); } -void QmlJSTextEditor::jumpToMethod(int index) +void QmlJSTextEditor::jumpToMethod(int /*index*/) { - if (index > 0 && index <= m_semanticInfo.declarations.size()) { // indexes are 1-based - Declaration d = m_semanticInfo.declarations.at(index - 1); - gotoLine(d.startLine, d.startColumn - 1); - setFocus(); - } + QModelIndex index = m_methodCombo->view()->currentIndex(); + AST::SourceLocation location = index.data(QmlOutlineModel::SourceLocationRole).value(); + + QTextCursor cursor = textCursor(); + cursor.setPosition(location.offset); + setTextCursor(cursor); + + setFocus(); } void QmlJSTextEditor::updateMethodBoxIndex() { - int line = 0, column = 0; - convertPosition(position(), &line, &column); + m_outlineModelIndex = indexForPosition(position()); + emit outlineModelIndexChanged(m_outlineModelIndex); - int currentSymbolIndex = 0; + QModelIndex comboIndex = m_outlineModelIndex; - int index = 0; - while (index < m_semanticInfo.declarations.size()) { - const Declaration &d = m_semanticInfo.declarations.at(index++); + if (comboIndex.isValid()) { + bool blocked = m_methodCombo->blockSignals(true); - if (line < d.startLine) - break; - else - currentSymbolIndex = index; + // There is no direct way to select a non-root item + m_methodCombo->setRootModelIndex(comboIndex.parent()); + m_methodCombo->setCurrentIndex(comboIndex.row()); + m_methodCombo->setRootModelIndex(QModelIndex()); + + updateMethodBoxToolTip(); + m_methodCombo->blockSignals(blocked); } - m_methodCombo->setCurrentIndex(currentSymbolIndex); updateUses(); } @@ -983,6 +1001,14 @@ void QmlJSTextEditor::createToolBar(QmlJSEditorEditable *editable) { m_methodCombo = new QComboBox; m_methodCombo->setMinimumContentsLength(22); + m_methodCombo->setModel(m_outlineModel); + + QTreeView *treeView = new QTreeView; + treeView->header()->hide(); + treeView->setItemsExpandable(false); + m_methodCombo->setView(treeView); + treeView->expandAll(); + //m_methodCombo->setSizeAdjustPolicy(QComboBox::AdjustToContents); // Make the combo box prefer to expand @@ -1332,15 +1358,14 @@ void QmlJSTextEditor::updateSemanticInfo(const SemanticInfo &semanticInfo) FindDeclarations findDeclarations; m_semanticInfo.declarations = findDeclarations(doc->ast()); - QStringList items; - items.append(tr("