1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "qmljsoutline.h"
34 #include "qmloutlinemodel.h"
35 #include "qmljseditoreditable.h"
36 #include "qmljsoutlinetreeview.h"
38 #include <coreplugin/icore.h>
39 #include <coreplugin/ifile.h>
40 #include <coreplugin/editormanager/editormanager.h>
42 #include <QtCore/QDebug>
43 #include <QtCore/QSettings>
44 #include <QtGui/QAction>
45 #include <QtGui/QVBoxLayout>
46 #include <QtGui/QTextBlock>
48 using namespace QmlJS;
54 namespace QmlJSEditor {
58 QmlJSOutlineFilterModel::QmlJSOutlineFilterModel(QObject *parent) :
59 QSortFilterProxyModel(parent)
61 setDynamicSortFilter(true);
64 bool QmlJSOutlineFilterModel::filterAcceptsRow(int sourceRow,
65 const QModelIndex &sourceParent) const
67 if (m_filterBindings) {
68 QModelIndex sourceIndex = sourceModel()->index(sourceRow, 0, sourceParent);
69 QVariant itemType = sourceIndex.data(QmlOutlineModel::ItemTypeRole);
70 if (itemType == QmlOutlineModel::NonElementBindingType) {
74 return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
77 QVariant QmlJSOutlineFilterModel::data(const QModelIndex &index, int role) const
79 if (role == QmlOutlineModel::AnnotationRole) {
80 // Don't show element id etc behind element if the property is also visible
82 && index.data(QmlOutlineModel::ItemTypeRole) == QmlOutlineModel::ElementType) {
86 return QSortFilterProxyModel::data(index, role);
89 bool QmlJSOutlineFilterModel::filterBindings() const
91 return m_filterBindings;
94 void QmlJSOutlineFilterModel::setFilterBindings(bool filterBindings)
96 m_filterBindings = filterBindings;
100 QmlJSOutlineWidget::QmlJSOutlineWidget(QWidget *parent) :
101 TextEditor::IOutlineWidget(parent),
102 m_treeView(new QmlJSOutlineTreeView(this)),
103 m_filterModel(new QmlJSOutlineFilterModel(this)),
105 m_enableCursorSync(true),
106 m_blockCursorSync(false)
108 m_filterModel->setFilterBindings(false);
110 m_treeView->setModel(m_filterModel);
112 QVBoxLayout *layout = new QVBoxLayout;
114 layout->setMargin(0);
115 layout->setSpacing(0);
116 layout->addWidget(m_treeView);
118 m_showBindingsAction = new QAction(this);
119 m_showBindingsAction->setText(tr("Show All Bindings"));
120 m_showBindingsAction->setCheckable(true);
121 m_showBindingsAction->setChecked(true);
122 connect(m_showBindingsAction, SIGNAL(toggled(bool)), this, SLOT(setShowBindings(bool)));
127 void QmlJSOutlineWidget::setEditor(QmlJSTextEditorWidget *editor)
131 m_filterModel->setSourceModel(m_editor->outlineModel());
134 connect(m_treeView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
135 this, SLOT(updateSelectionInText(QItemSelection)));
137 connect(m_treeView, SIGNAL(doubleClicked(QModelIndex)),
138 this, SLOT(updateTextCursor(QModelIndex)));
140 connect(m_editor, SIGNAL(outlineModelIndexChanged(QModelIndex)),
141 this, SLOT(updateSelectionInTree(QModelIndex)));
142 connect(m_editor->outlineModel(), SIGNAL(updated()),
143 this, SLOT(modelUpdated()));
146 QList<QAction*> QmlJSOutlineWidget::filterMenuActions() const
148 QList<QAction*> list;
149 list.append(m_showBindingsAction);
153 void QmlJSOutlineWidget::setCursorSynchronization(bool syncWithCursor)
155 m_enableCursorSync = syncWithCursor;
156 if (m_enableCursorSync)
157 updateSelectionInTree(m_editor->outlineModelIndex());
160 void QmlJSOutlineWidget::restoreSettings(int position)
162 QSettings *settings = Core::ICore::instance()->settings();
163 bool showBindings = settings->value("QmlJSOutline."+QString::number(position)+".ShowBindings", true).toBool();
164 m_showBindingsAction->setChecked(showBindings);
167 void QmlJSOutlineWidget::saveSettings(int position)
169 QSettings *settings = Core::ICore::instance()->settings();
170 settings->setValue("QmlJSOutline."+QString::number(position)+".ShowBindings",
171 m_showBindingsAction->isChecked());
174 void QmlJSOutlineWidget::modelUpdated()
176 m_treeView->expandAll();
179 void QmlJSOutlineWidget::updateSelectionInTree(const QModelIndex &index)
184 m_blockCursorSync = true;
186 QModelIndex baseIndex = index;
187 QModelIndex filterIndex = m_filterModel->mapFromSource(baseIndex);
188 while (baseIndex.isValid() && !filterIndex.isValid()) { // Search for ancestor index actually shown
189 baseIndex = baseIndex.parent();
190 filterIndex = m_filterModel->mapFromSource(baseIndex);
193 m_treeView->selectionModel()->select(filterIndex, QItemSelectionModel::ClearAndSelect);
194 m_treeView->scrollTo(filterIndex);
195 m_blockCursorSync = false;
198 void QmlJSOutlineWidget::updateSelectionInText(const QItemSelection &selection)
203 if (!selection.indexes().isEmpty()) {
204 QModelIndex index = selection.indexes().first();
206 updateTextCursor(index);
210 void QmlJSOutlineWidget::updateTextCursor(const QModelIndex &index)
212 QModelIndex sourceIndex = m_filterModel->mapToSource(index);
213 AST::SourceLocation location = m_editor->outlineModel()->sourceLocation(sourceIndex);
215 if (!location.isValid())
218 const QTextBlock lastBlock = m_editor->document()->lastBlock();
219 const uint textLength = lastBlock.position() + lastBlock.length();
220 if (location.offset >= textLength)
223 Core::EditorManager *editorManager = Core::EditorManager::instance();
224 editorManager->cutForwardNavigationHistory();
225 editorManager->addCurrentPositionToNavigationHistory();
227 QTextCursor textCursor = m_editor->textCursor();
228 m_blockCursorSync = true;
229 textCursor.setPosition(location.offset);
230 m_editor->setTextCursor(textCursor);
231 m_editor->centerCursor();
232 m_blockCursorSync = false;
235 void QmlJSOutlineWidget::setShowBindings(bool showBindings)
237 m_filterModel->setFilterBindings(!showBindings);
239 updateSelectionInTree(m_editor->outlineModelIndex());
242 bool QmlJSOutlineWidget::syncCursor()
244 return m_enableCursorSync && !m_blockCursorSync;
247 bool QmlJSOutlineWidgetFactory::supportsEditor(Core::IEditor *editor) const
249 if (qobject_cast<QmlJSEditorEditable*>(editor))
254 TextEditor::IOutlineWidget *QmlJSOutlineWidgetFactory::createWidget(Core::IEditor *editor)
256 QmlJSOutlineWidget *widget = new QmlJSOutlineWidget;
258 QmlJSEditorEditable *qmlJSEditable = qobject_cast<QmlJSEditorEditable*>(editor);
259 QmlJSTextEditorWidget *qmlJSEditor = qobject_cast<QmlJSTextEditorWidget*>(qmlJSEditable->widget());
260 Q_ASSERT(qmlJSEditor);
262 widget->setEditor(qmlJSEditor);
267 } // namespace Internal
268 } // namespace QmlJSEditor