Files
qt-creator/src/plugins/qmljseditor/qmljsoutline.cpp

133 lines
3.6 KiB
C++
Raw Normal View History

2010-07-08 11:32:45 +02:00
#include "qmljsoutline.h"
#include "qmloutlinemodel.h"
2010-07-08 11:32:45 +02:00
#include <coreplugin/ifile.h>
#include <coreplugin/editormanager/editormanager.h>
2010-07-08 11:32:45 +02:00
#include <QtGui/QVBoxLayout>
#include <QDebug>
2010-07-08 11:32:45 +02:00
using namespace QmlJS;
enum {
debug = false
};
namespace QmlJSEditor {
namespace Internal {
QmlJSOutlineTreeView::QmlJSOutlineTreeView(QWidget *parent) :
QTreeView(parent)
{
// see also CppOutlineTreeView
setFocusPolicy(Qt::NoFocus);
setFrameStyle(QFrame::NoFrame);
setAttribute(Qt::WA_MacShowFocusRect, false);
setUniformRowHeights(true);
setHeaderHidden(true);
setTextElideMode(Qt::ElideNone);
setIndentation(20);
setExpandsOnDoubleClick(false);
}
QmlJSOutlineWidget::QmlJSOutlineWidget(QWidget *parent) :
TextEditor::IOutlineWidget(parent),
m_treeView(new QmlJSOutlineTreeView(this)),
2010-07-08 11:32:45 +02:00
m_enableCursorSync(true),
m_blockCursorSync(false)
{
QVBoxLayout *layout = new QVBoxLayout;
layout->setMargin(0);
layout->setSpacing(0);
layout->addWidget(m_treeView);
setLayout(layout);
}
void QmlJSOutlineWidget::setEditor(QmlJSTextEditor *editor)
{
m_editor = editor;
m_treeView->setModel(m_editor.data()->outlineModel());
connect(m_treeView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
this, SLOT(updateSelectionInText(QItemSelection)));
2010-07-08 11:32:45 +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
}
void QmlJSOutlineWidget::setCursorSynchronization(bool syncWithCursor)
{
m_enableCursorSync = syncWithCursor;
if (m_enableCursorSync)
updateSelectionInTree(m_editor.data()->outlineModelIndex());
2010-07-08 11:32:45 +02:00
}
void QmlJSOutlineWidget::modelUpdated()
2010-07-08 11:32:45 +02:00
{
m_treeView->expandAll();
}
void QmlJSOutlineWidget::updateSelectionInTree(const QModelIndex &index)
2010-07-08 11:32:45 +02:00
{
if (!syncCursor())
return;
m_blockCursorSync = true;
m_treeView->selectionModel()->select(index, QItemSelectionModel::ClearAndSelect);
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>();
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;
}
}
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