diff --git a/src/plugins/qmljseditor/qmljsoutline.cpp b/src/plugins/qmljseditor/qmljsoutline.cpp index 0fcac6e60a4..29352c39f7c 100644 --- a/src/plugins/qmljseditor/qmljsoutline.cpp +++ b/src/plugins/qmljseditor/qmljsoutline.cpp @@ -53,6 +53,14 @@ QmlJSOutlineFilterModel::QmlJSOutlineFilterModel(QObject *parent) : setDynamicSortFilter(true); } +Qt::ItemFlags QmlJSOutlineFilterModel::flags(const QModelIndex &index) const +{ + Qt::ItemFlags f = sourceModel()->flags(index); + if (m_sorted) + f.setFlag(Qt::ItemIsDropEnabled, false); + return f; +} + bool QmlJSOutlineFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const { @@ -65,6 +73,15 @@ bool QmlJSOutlineFilterModel::filterAcceptsRow(int sourceRow, return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent); } +bool QmlJSOutlineFilterModel::lessThan(const QModelIndex &sourceLeft, + const QModelIndex &sourceRight) const +{ + if (!m_sorted) + return sourceLeft.row() > sourceRight.row(); + + return sourceLeft.data().toString() > sourceRight.data().toString(); +} + QVariant QmlJSOutlineFilterModel::data(const QModelIndex &index, int role) const { if (role == QmlOutlineModel::AnnotationRole) { @@ -93,6 +110,12 @@ void QmlJSOutlineFilterModel::setFilterBindings(bool filterBindings) invalidateFilter(); } +void QmlJSOutlineFilterModel::setSorted(bool sorted) +{ + m_sorted = sorted; + invalidate(); +} + QmlJSOutlineWidget::QmlJSOutlineWidget(QWidget *parent) : TextEditor::IOutlineWidget(parent) , m_treeView(new QmlJSOutlineTreeView(this)) @@ -101,6 +124,8 @@ QmlJSOutlineWidget::QmlJSOutlineWidget(QWidget *parent) m_filterModel->setFilterBindings(false); m_treeView->setModel(m_filterModel); + m_treeView->setSortingEnabled(true); + setFocusProxy(m_treeView); auto layout = new QVBoxLayout; @@ -158,15 +183,25 @@ void QmlJSOutlineWidget::setCursorSynchronization(bool syncWithCursor) m_editor->updateOutlineIndexNow(); } +void QmlJSOutlineWidget::setSorted(bool sorted) +{ + m_sorted = sorted; + m_filterModel->setSorted(m_sorted); +} + void QmlJSOutlineWidget::restoreSettings(const QVariantMap &map) { bool showBindings = map.value(QString::fromLatin1("QmlJSOutline.ShowBindings"), true).toBool(); m_showBindingsAction->setChecked(showBindings); + setSorted(map.value(QString("QmlJSOutline.Sort"), false).toBool()); } QVariantMap QmlJSOutlineWidget::settings() const { - return {{QLatin1String("QmlJSOutline.ShowBindings"), m_showBindingsAction->isChecked()}}; + return { + {QString("QmlJSOutline.ShowBindings"), m_showBindingsAction->isChecked()}, + {QString("QmlJSOutline.Sort"), m_sorted} + }; } void QmlJSOutlineWidget::updateSelectionInTree(const QModelIndex &index) diff --git a/src/plugins/qmljseditor/qmljsoutline.h b/src/plugins/qmljseditor/qmljsoutline.h index b81e9f39ce0..4e083530023 100644 --- a/src/plugins/qmljseditor/qmljsoutline.h +++ b/src/plugins/qmljseditor/qmljsoutline.h @@ -51,15 +51,19 @@ class QmlJSOutlineFilterModel : public QSortFilterProxyModel public: QmlJSOutlineFilterModel(QObject *parent); // QSortFilterProxyModel + Qt::ItemFlags flags(const QModelIndex &index) const override; bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override; + bool lessThan(const QModelIndex &sourceLeft, const QModelIndex &sourceRight) const override; QVariant data(const QModelIndex &index, int role) const override; Qt::DropActions supportedDragActions() const override; bool filterBindings() const; void setFilterBindings(bool filterBindings); + void setSorted(bool sorted); private: bool m_filterBindings = false; + bool m_sorted = false; }; class QmlJSOutlineWidget : public TextEditor::IOutlineWidget @@ -73,6 +77,8 @@ public: // IOutlineWidget QList filterMenuActions() const override; void setCursorSynchronization(bool syncWithCursor) override; + bool isSorted() const override { return m_sorted; }; + void setSorted(bool sorted) override; void restoreSettings(const QVariantMap &map) override; QVariantMap settings() const override; @@ -93,6 +99,7 @@ private: bool m_enableCursorSync = true; bool m_blockCursorSync = false; + bool m_sorted = false; }; class QmlJSOutlineWidgetFactory : public TextEditor::IOutlineWidgetFactory @@ -100,6 +107,7 @@ class QmlJSOutlineWidgetFactory : public TextEditor::IOutlineWidgetFactory Q_OBJECT public: bool supportsEditor(Core::IEditor *editor) const override; + bool supportsSorting() const override { return true; } TextEditor::IOutlineWidget *createWidget(Core::IEditor *editor) override; };