2010-07-08 11:32:45 +02:00
|
|
|
#include "qmljsoutline.h"
|
2010-07-12 09:44:42 +02:00
|
|
|
#include "qmloutlinemodel.h"
|
2010-07-08 11:32:45 +02:00
|
|
|
|
2010-07-12 09:44:42 +02:00
|
|
|
#include <coreplugin/ifile.h>
|
2010-07-14 12:21:23 +02:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2010-07-14 16:46:54 +02:00
|
|
|
#include <QtGui/QAction>
|
2010-07-08 11:32:45 +02:00
|
|
|
#include <QtGui/QVBoxLayout>
|
|
|
|
|
|
2010-07-12 14:45:22 +02:00
|
|
|
#include <QDebug>
|
2010-07-08 11:32:45 +02:00
|
|
|
using namespace QmlJS;
|
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
|
debug = false
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
namespace QmlJSEditor {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
QmlJSOutlineTreeView::QmlJSOutlineTreeView(QWidget *parent) :
|
2010-07-16 11:18:30 +02:00
|
|
|
Utils::NavigationTreeView(parent)
|
2010-07-08 11:32:45 +02:00
|
|
|
{
|
|
|
|
|
// see also CppOutlineTreeView
|
|
|
|
|
setFocusPolicy(Qt::NoFocus);
|
|
|
|
|
setHeaderHidden(true);
|
|
|
|
|
setExpandsOnDoubleClick(false);
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-14 16:46:54 +02:00
|
|
|
QmlJSOutlineFilterModel::QmlJSOutlineFilterModel(QObject *parent) :
|
|
|
|
|
QSortFilterProxyModel(parent)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool QmlJSOutlineFilterModel::filterAcceptsRow(int sourceRow,
|
|
|
|
|
const QModelIndex &sourceParent) const
|
|
|
|
|
{
|
|
|
|
|
if (m_filterBindings) {
|
|
|
|
|
QModelIndex sourceIndex = sourceModel()->index(sourceRow, 0, sourceParent);
|
|
|
|
|
if (sourceIndex.data(QmlOutlineModel::ItemTypeRole) == QmlOutlineModel::PropertyType) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool QmlJSOutlineFilterModel::filterBindings() const
|
|
|
|
|
{
|
|
|
|
|
return m_filterBindings;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QmlJSOutlineFilterModel::setFilterBindings(bool filterBindings)
|
|
|
|
|
{
|
|
|
|
|
if (m_filterBindings != filterBindings) {
|
|
|
|
|
m_filterBindings = filterBindings;
|
|
|
|
|
invalidateFilter();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-07-08 11:32:45 +02:00
|
|
|
m_enableCursorSync(true),
|
|
|
|
|
m_blockCursorSync(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);
|
|
|
|
|
m_showBindingsAction->setText(tr("Show bindings"));
|
|
|
|
|
m_showBindingsAction->setCheckable(true);
|
|
|
|
|
m_showBindingsAction->setChecked(true);
|
|
|
|
|
connect(m_showBindingsAction, SIGNAL(triggered(bool)), this, SLOT(setShowBindings(bool)));
|
|
|
|
|
|
2010-07-08 11:32:45 +02:00
|
|
|
setLayout(layout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QmlJSOutlineWidget::setEditor(QmlJSTextEditor *editor)
|
|
|
|
|
{
|
|
|
|
|
m_editor = editor;
|
|
|
|
|
|
2010-07-14 16:46:54 +02:00
|
|
|
m_filterModel->setSourceModel(m_editor.data()->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-07-12 14:45:22 +02:00
|
|
|
connect(m_editor.data(), SIGNAL(outlineModelIndexChanged(QModelIndex)),
|
|
|
|
|
this, SLOT(updateSelectionInTree(QModelIndex)));
|
|
|
|
|
connect(m_editor.data()->outlineModel(), SIGNAL(updated()),
|
|
|
|
|
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-07-12 14:45:22 +02:00
|
|
|
updateSelectionInTree(m_editor.data()->outlineModelIndex());
|
2010-07-08 11:32:45 +02:00
|
|
|
}
|
|
|
|
|
|
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-07-14 16:46:54 +02:00
|
|
|
m_treeView->selectionModel()->select(m_filterModel->mapFromSource(index), QItemSelectionModel::ClearAndSelect);
|
2010-07-09 15:27:32 +02:00
|
|
|
m_treeView->scrollTo(index);
|
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();
|
|
|
|
|
AST::SourceLocation location = index.data(QmlOutlineModel::SourceLocationRole).value<AST::SourceLocation>();
|
|
|
|
|
|
2010-07-14 12:21:23 +02:00
|
|
|
Core::EditorManager *editorManager = Core::EditorManager::instance();
|
|
|
|
|
editorManager->cutForwardNavigationHistory();
|
|
|
|
|
editorManager->addCurrentPositionToNavigationHistory();
|
|
|
|
|
|
2010-07-08 11:32:45 +02:00
|
|
|
QTextCursor textCursor = m_editor.data()->textCursor();
|
|
|
|
|
m_blockCursorSync = true;
|
|
|
|
|
textCursor.setPosition(location.offset);
|
|
|
|
|
m_editor.data()->setTextCursor(textCursor);
|
|
|
|
|
m_blockCursorSync = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-14 16:46:54 +02:00
|
|
|
void QmlJSOutlineWidget::setShowBindings(bool showBindings)
|
|
|
|
|
{
|
|
|
|
|
m_filterModel->setFilterBindings(!showBindings);
|
|
|
|
|
modelUpdated();
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
QmlJSTextEditor *qmlJSEditor = qobject_cast<QmlJSTextEditor*>(qmlJSEditable->widget());
|
|
|
|
|
Q_ASSERT(qmlJSEditor);
|
|
|
|
|
|
|
|
|
|
widget->setEditor(qmlJSEditor);
|
|
|
|
|
|
|
|
|
|
return widget;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace QmlJSEditor
|