2011-02-18 11:47:15 +01:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2012-01-26 18:33:46 +01:00
|
|
|
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
|
2011-02-18 11:47:15 +01:00
|
|
|
**
|
2012-07-19 12:26:56 +02:00
|
|
|
** Contact: http://www.qt-project.org/
|
2011-02-18 11:47:15 +01:00
|
|
|
**
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** This file may be used under the terms of the GNU Lesser General Public
|
|
|
|
|
** License version 2.1 as published by the Free Software Foundation and
|
|
|
|
|
** appearing in the file LICENSE.LGPL included in the packaging of this file.
|
|
|
|
|
** Please review the following information to ensure the GNU Lesser General
|
|
|
|
|
** Public License version 2.1 requirements will be met:
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2011-02-18 11:47:15 +01:00
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2011-02-18 11:47:15 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Other Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
|
|
|
|
**
|
2011-02-18 11:47:15 +01:00
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
2010-07-08 11:32:45 +02:00
|
|
|
#include "qmljsoutline.h"
|
2010-07-12 09:44:42 +02:00
|
|
|
#include "qmloutlinemodel.h"
|
2010-11-02 11:10:27 +01:00
|
|
|
#include "qmljseditoreditable.h"
|
2010-08-10 11:59:02 +02:00
|
|
|
#include "qmljsoutlinetreeview.h"
|
2010-07-08 11:32:45 +02:00
|
|
|
|
2010-07-16 11:04:20 +02:00
|
|
|
#include <coreplugin/icore.h>
|
2012-02-14 16:43:51 +01:00
|
|
|
#include <coreplugin/idocument.h>
|
2010-07-14 12:21:23 +02:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2010-07-16 11:04:20 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QSettings>
|
|
|
|
|
#include <QAction>
|
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
#include <QTextBlock>
|
2010-07-08 11:32:45 +02:00
|
|
|
|
|
|
|
|
using namespace QmlJS;
|
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
|
debug = false
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
namespace QmlJSEditor {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
|
2010-07-14 16:46:54 +02:00
|
|
|
QmlJSOutlineFilterModel::QmlJSOutlineFilterModel(QObject *parent) :
|
|
|
|
|
QSortFilterProxyModel(parent)
|
|
|
|
|
{
|
2010-07-21 14:48:54 +02:00
|
|
|
setDynamicSortFilter(true);
|
2010-07-14 16:46:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool QmlJSOutlineFilterModel::filterAcceptsRow(int sourceRow,
|
|
|
|
|
const QModelIndex &sourceParent) const
|
|
|
|
|
{
|
|
|
|
|
if (m_filterBindings) {
|
|
|
|
|
QModelIndex sourceIndex = sourceModel()->index(sourceRow, 0, sourceParent);
|
2010-07-16 11:04:20 +02:00
|
|
|
QVariant itemType = sourceIndex.data(QmlOutlineModel::ItemTypeRole);
|
2010-08-12 12:47:25 +02:00
|
|
|
if (itemType == QmlOutlineModel::NonElementBindingType) {
|
2010-07-14 16:46:54 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-12 14:37:52 +02:00
|
|
|
QVariant QmlJSOutlineFilterModel::data(const QModelIndex &index, int role) const
|
|
|
|
|
{
|
|
|
|
|
if (role == QmlOutlineModel::AnnotationRole) {
|
|
|
|
|
// Don't show element id etc behind element if the property is also visible
|
|
|
|
|
if (!filterBindings()
|
|
|
|
|
&& index.data(QmlOutlineModel::ItemTypeRole) == QmlOutlineModel::ElementType) {
|
|
|
|
|
return QVariant();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return QSortFilterProxyModel::data(index, role);
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-14 16:46:54 +02:00
|
|
|
bool QmlJSOutlineFilterModel::filterBindings() const
|
|
|
|
|
{
|
|
|
|
|
return m_filterBindings;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QmlJSOutlineFilterModel::setFilterBindings(bool filterBindings)
|
|
|
|
|
{
|
2010-07-16 11:04:20 +02:00
|
|
|
m_filterBindings = filterBindings;
|
|
|
|
|
invalidateFilter();
|
2010-07-14 16:46:54 +02:00
|
|
|
}
|
|
|
|
|
|
2010-07-08 11:32:45 +02:00
|
|
|
QmlJSOutlineWidget::QmlJSOutlineWidget(QWidget *parent) :
|
|
|
|
|
TextEditor::IOutlineWidget(parent),
|
2010-07-12 14:45:22 +02:00
|
|
|
m_treeView(new QmlJSOutlineTreeView(this)),
|
2010-07-14 16:46:54 +02:00
|
|
|
m_filterModel(new QmlJSOutlineFilterModel(this)),
|
2010-08-13 13:50:35 +02:00
|
|
|
m_editor(0),
|
2010-07-08 11:32:45 +02:00
|
|
|
m_enableCursorSync(true),
|
|
|
|
|
m_blockCursorSync(false)
|
|
|
|
|
{
|
2010-07-16 11:04:20 +02:00
|
|
|
m_filterModel->setFilterBindings(false);
|
|
|
|
|
|
2010-07-14 16:46:54 +02:00
|
|
|
m_treeView->setModel(m_filterModel);
|
|
|
|
|
|
2010-07-08 11:32:45 +02:00
|
|
|
QVBoxLayout *layout = new QVBoxLayout;
|
|
|
|
|
|
|
|
|
|
layout->setMargin(0);
|
|
|
|
|
layout->setSpacing(0);
|
|
|
|
|
layout->addWidget(m_treeView);
|
|
|
|
|
|
2010-07-14 16:46:54 +02:00
|
|
|
m_showBindingsAction = new QAction(this);
|
2010-09-28 17:29:05 +02:00
|
|
|
m_showBindingsAction->setText(tr("Show All Bindings"));
|
2010-07-14 16:46:54 +02:00
|
|
|
m_showBindingsAction->setCheckable(true);
|
|
|
|
|
m_showBindingsAction->setChecked(true);
|
2010-07-16 11:04:20 +02:00
|
|
|
connect(m_showBindingsAction, SIGNAL(toggled(bool)), this, SLOT(setShowBindings(bool)));
|
2010-07-14 16:46:54 +02:00
|
|
|
|
2010-07-08 11:32:45 +02:00
|
|
|
setLayout(layout);
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-21 16:02:26 +01:00
|
|
|
void QmlJSOutlineWidget::setEditor(QmlJSTextEditorWidget *editor)
|
2010-07-08 11:32:45 +02:00
|
|
|
{
|
|
|
|
|
m_editor = editor;
|
|
|
|
|
|
2010-08-13 13:50:35 +02:00
|
|
|
m_filterModel->setSourceModel(m_editor->outlineModel());
|
2010-07-14 13:01:39 +02:00
|
|
|
modelUpdated();
|
|
|
|
|
|
2010-07-12 14:45:22 +02:00
|
|
|
connect(m_treeView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
|
|
|
|
|
this, SLOT(updateSelectionInText(QItemSelection)));
|
2010-07-08 11:32:45 +02:00
|
|
|
|
2010-12-20 09:44:54 +01:00
|
|
|
connect(m_treeView, SIGNAL(doubleClicked(QModelIndex)),
|
|
|
|
|
this, SLOT(updateTextCursor(QModelIndex)));
|
|
|
|
|
|
2010-08-13 13:50:35 +02:00
|
|
|
connect(m_editor, SIGNAL(outlineModelIndexChanged(QModelIndex)),
|
2010-07-12 14:45:22 +02:00
|
|
|
this, SLOT(updateSelectionInTree(QModelIndex)));
|
2010-08-13 13:50:35 +02:00
|
|
|
connect(m_editor->outlineModel(), SIGNAL(updated()),
|
2010-07-12 14:45:22 +02:00
|
|
|
this, SLOT(modelUpdated()));
|
2010-07-08 11:32:45 +02:00
|
|
|
}
|
|
|
|
|
|
2010-07-14 16:46:54 +02:00
|
|
|
QList<QAction*> QmlJSOutlineWidget::filterMenuActions() const
|
|
|
|
|
{
|
|
|
|
|
QList<QAction*> list;
|
|
|
|
|
list.append(m_showBindingsAction);
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-08 11:32:45 +02:00
|
|
|
void QmlJSOutlineWidget::setCursorSynchronization(bool syncWithCursor)
|
|
|
|
|
{
|
|
|
|
|
m_enableCursorSync = syncWithCursor;
|
|
|
|
|
if (m_enableCursorSync)
|
2010-08-13 13:50:35 +02:00
|
|
|
updateSelectionInTree(m_editor->outlineModelIndex());
|
2010-07-08 11:32:45 +02:00
|
|
|
}
|
|
|
|
|
|
2010-07-16 11:04:20 +02:00
|
|
|
void QmlJSOutlineWidget::restoreSettings(int position)
|
|
|
|
|
{
|
2012-01-24 15:36:40 +01:00
|
|
|
QSettings *settings = Core::ICore::settings();
|
2010-07-16 11:04:20 +02:00
|
|
|
bool showBindings = settings->value("QmlJSOutline."+QString::number(position)+".ShowBindings", true).toBool();
|
|
|
|
|
m_showBindingsAction->setChecked(showBindings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QmlJSOutlineWidget::saveSettings(int position)
|
|
|
|
|
{
|
2012-01-24 15:36:40 +01:00
|
|
|
QSettings *settings = Core::ICore::settings();
|
2010-07-16 11:04:20 +02:00
|
|
|
settings->setValue("QmlJSOutline."+QString::number(position)+".ShowBindings",
|
|
|
|
|
m_showBindingsAction->isChecked());
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-12 14:45:22 +02:00
|
|
|
void QmlJSOutlineWidget::modelUpdated()
|
2010-07-08 11:32:45 +02:00
|
|
|
{
|
|
|
|
|
m_treeView->expandAll();
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-12 14:45:22 +02:00
|
|
|
void QmlJSOutlineWidget::updateSelectionInTree(const QModelIndex &index)
|
2010-07-08 11:32:45 +02:00
|
|
|
{
|
|
|
|
|
if (!syncCursor())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_blockCursorSync = true;
|
2010-09-03 12:40:39 +02:00
|
|
|
|
|
|
|
|
QModelIndex baseIndex = index;
|
|
|
|
|
QModelIndex filterIndex = m_filterModel->mapFromSource(baseIndex);
|
|
|
|
|
while (baseIndex.isValid() && !filterIndex.isValid()) { // Search for ancestor index actually shown
|
|
|
|
|
baseIndex = baseIndex.parent();
|
|
|
|
|
filterIndex = m_filterModel->mapFromSource(baseIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_treeView->selectionModel()->select(filterIndex, QItemSelectionModel::ClearAndSelect);
|
|
|
|
|
m_treeView->scrollTo(filterIndex);
|
2010-07-08 11:32:45 +02:00
|
|
|
m_blockCursorSync = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QmlJSOutlineWidget::updateSelectionInText(const QItemSelection &selection)
|
|
|
|
|
{
|
|
|
|
|
if (!syncCursor())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!selection.indexes().isEmpty()) {
|
|
|
|
|
QModelIndex index = selection.indexes().first();
|
2010-08-25 13:21:38 +02:00
|
|
|
|
2010-12-20 09:44:54 +01:00
|
|
|
updateTextCursor(index);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QmlJSOutlineWidget::updateTextCursor(const QModelIndex &index)
|
|
|
|
|
{
|
|
|
|
|
QModelIndex sourceIndex = m_filterModel->mapToSource(index);
|
|
|
|
|
AST::SourceLocation location = m_editor->outlineModel()->sourceLocation(sourceIndex);
|
2010-08-25 13:21:38 +02:00
|
|
|
|
2010-12-20 09:44:54 +01:00
|
|
|
if (!location.isValid())
|
|
|
|
|
return;
|
2010-07-08 11:32:45 +02:00
|
|
|
|
2010-12-20 09:44:54 +01:00
|
|
|
const QTextBlock lastBlock = m_editor->document()->lastBlock();
|
|
|
|
|
const uint textLength = lastBlock.position() + lastBlock.length();
|
|
|
|
|
if (location.offset >= textLength)
|
|
|
|
|
return;
|
2010-09-14 11:58:06 +02:00
|
|
|
|
2010-12-20 09:44:54 +01:00
|
|
|
Core::EditorManager *editorManager = Core::EditorManager::instance();
|
|
|
|
|
editorManager->cutForwardNavigationHistory();
|
|
|
|
|
editorManager->addCurrentPositionToNavigationHistory();
|
2010-07-14 12:21:23 +02:00
|
|
|
|
2010-12-20 09:44:54 +01:00
|
|
|
QTextCursor textCursor = m_editor->textCursor();
|
|
|
|
|
m_blockCursorSync = true;
|
|
|
|
|
textCursor.setPosition(location.offset);
|
|
|
|
|
m_editor->setTextCursor(textCursor);
|
|
|
|
|
m_editor->centerCursor();
|
2012-01-18 11:54:12 +01:00
|
|
|
m_editor->setFocus();
|
2010-12-20 09:44:54 +01:00
|
|
|
m_blockCursorSync = false;
|
2010-07-08 11:32:45 +02:00
|
|
|
}
|
|
|
|
|
|
2010-07-14 16:46:54 +02:00
|
|
|
void QmlJSOutlineWidget::setShowBindings(bool showBindings)
|
|
|
|
|
{
|
|
|
|
|
m_filterModel->setFilterBindings(!showBindings);
|
|
|
|
|
modelUpdated();
|
2010-09-03 12:49:05 +02:00
|
|
|
updateSelectionInTree(m_editor->outlineModelIndex());
|
2010-07-14 16:46:54 +02:00
|
|
|
}
|
|
|
|
|
|
2010-07-08 11:32:45 +02:00
|
|
|
bool QmlJSOutlineWidget::syncCursor()
|
|
|
|
|
{
|
|
|
|
|
return m_enableCursorSync && !m_blockCursorSync;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool QmlJSOutlineWidgetFactory::supportsEditor(Core::IEditor *editor) const
|
|
|
|
|
{
|
|
|
|
|
if (qobject_cast<QmlJSEditorEditable*>(editor))
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextEditor::IOutlineWidget *QmlJSOutlineWidgetFactory::createWidget(Core::IEditor *editor)
|
|
|
|
|
{
|
|
|
|
|
QmlJSOutlineWidget *widget = new QmlJSOutlineWidget;
|
|
|
|
|
|
|
|
|
|
QmlJSEditorEditable *qmlJSEditable = qobject_cast<QmlJSEditorEditable*>(editor);
|
2011-02-21 16:02:26 +01:00
|
|
|
QmlJSTextEditorWidget *qmlJSEditor = qobject_cast<QmlJSTextEditorWidget*>(qmlJSEditable->widget());
|
2010-07-08 11:32:45 +02:00
|
|
|
Q_ASSERT(qmlJSEditor);
|
|
|
|
|
|
|
|
|
|
widget->setEditor(qmlJSEditor);
|
|
|
|
|
|
|
|
|
|
return widget;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace QmlJSEditor
|