From 331effc2fd5e25eae4243c517e0047d864fd94a5 Mon Sep 17 00:00:00 2001
From: Roberto Raggi <roberto.raggi@nokia.com>
Date: Tue, 29 Sep 2009 18:16:13 +0200
Subject: [PATCH] Introduced checkable results.

---
 src/plugins/cpptools/cppfindreferences.cpp    |  2 +
 .../find/searchresulttreeitemdelegate.cpp     | 13 +++++
 src/plugins/find/searchresulttreeitems.cpp    | 28 ++++++++++-
 src/plugins/find/searchresulttreeitems.h      | 10 +++-
 src/plugins/find/searchresulttreemodel.cpp    | 49 +++++++++++++++++++
 src/plugins/find/searchresulttreemodel.h      |  4 ++
 src/plugins/find/searchresultwindow.cpp       |  1 +
 7 files changed, 104 insertions(+), 3 deletions(-)

diff --git a/src/plugins/cpptools/cppfindreferences.cpp b/src/plugins/cpptools/cppfindreferences.cpp
index 2f6964cbe35..fce5d3451d9 100644
--- a/src/plugins/cpptools/cppfindreferences.cpp
+++ b/src/plugins/cpptools/cppfindreferences.cpp
@@ -439,7 +439,9 @@ static void find_helper(QFutureInterface<Core::Utils::FileSearchResult> &future,
 
 void CppFindReferences::findAll(Symbol *symbol)
 {
+    const bool wasInReplaceMode = _resultWindow->isShowingReplaceUI();
     _resultWindow->clearContents();
+    _resultWindow->setShowReplaceUI(true);
     _resultWindow->popup(true);
 
     const Snapshot snapshot = _modelManager->snapshot();
diff --git a/src/plugins/find/searchresulttreeitemdelegate.cpp b/src/plugins/find/searchresulttreeitemdelegate.cpp
index 35eab26cadc..84d230d4b35 100644
--- a/src/plugins/find/searchresulttreeitemdelegate.cpp
+++ b/src/plugins/find/searchresulttreeitemdelegate.cpp
@@ -35,6 +35,7 @@
 #include <QPainter>
 #include <QAbstractTextDocumentLayout>
 #include <QApplication>
+#include <QDebug>
 
 #include <math.h>
 
@@ -67,6 +68,18 @@ void SearchResultTreeItemDelegate::paint(QPainter *painter, const QStyleOptionVi
         QItemDelegate::drawDisplay(painter, opt, resultRowRect, displayString);
         QItemDelegate::drawFocus(painter, opt, opt.rect);
 
+        QVariant value = index.data(Qt::CheckStateRole);
+        if (value.isValid()) {
+            Qt::CheckState checkState = Qt::Unchecked;
+            checkState = static_cast<Qt::CheckState>(value.toInt());
+            QRect checkRect = check(opt, opt.rect, value);
+
+            QRect emptyRect;
+            doLayout(opt, &checkRect, &emptyRect, &emptyRect, false);
+
+            QItemDelegate::drawCheck(painter, opt, opt.rect, checkState);
+        }
+
         painter->restore();
     }
 }
diff --git a/src/plugins/find/searchresulttreeitems.cpp b/src/plugins/find/searchresulttreeitems.cpp
index e4ea18539f1..b2c19ec9ca4 100644
--- a/src/plugins/find/searchresulttreeitems.cpp
+++ b/src/plugins/find/searchresulttreeitems.cpp
@@ -32,7 +32,7 @@
 using namespace Find::Internal;
 
 SearchResultTreeItem::SearchResultTreeItem(SearchResultTreeItem::ItemType type, const SearchResultTreeItem *parent)
-  : m_type(type), m_parent(parent)
+  : m_type(type), m_parent(parent), m_isUserCheckable(false), m_checkState(Qt::Unchecked)
 {
 }
 
@@ -41,6 +41,26 @@ SearchResultTreeItem::~SearchResultTreeItem()
     clearChildren();
 }
 
+bool SearchResultTreeItem::isUserCheckable() const
+{
+    return m_isUserCheckable;
+}
+
+void SearchResultTreeItem::setIsUserCheckable(bool isUserCheckable)
+{
+    m_isUserCheckable = isUserCheckable;
+}
+
+Qt::CheckState SearchResultTreeItem::checkState() const
+{
+    return m_checkState;
+}
+
+void SearchResultTreeItem::setCheckState(Qt::CheckState checkState)
+{
+    m_checkState = checkState;
+}
+
 void SearchResultTreeItem::clearChildren()
 {
     qDeleteAll(m_children);
@@ -62,7 +82,7 @@ int SearchResultTreeItem::rowOfItem() const
     return (m_parent ? m_parent->m_children.indexOf(const_cast<SearchResultTreeItem*>(this)):0);
 }
 
-const SearchResultTreeItem* SearchResultTreeItem::childAt(int index) const
+SearchResultTreeItem* SearchResultTreeItem::childAt(int index) const
 {
     return m_children.at(index);
 }
@@ -131,5 +151,9 @@ void SearchResultFile::appendResultLine(int index, int lineNumber, const QString
 {
     SearchResultTreeItem *child = new SearchResultTextRow(index, lineNumber, rowText,
                                                           searchTermStart, searchTermLength, this);
+    if (isUserCheckable()) {
+        child->setIsUserCheckable(true);
+        child->setCheckState(Qt::Checked);
+    }
     appendChild(child);
 }
diff --git a/src/plugins/find/searchresulttreeitems.h b/src/plugins/find/searchresulttreeitems.h
index ccad626b8ad..6db2efbe0fc 100644
--- a/src/plugins/find/searchresulttreeitems.h
+++ b/src/plugins/find/searchresulttreeitems.h
@@ -54,16 +54,24 @@ public:
 
     ItemType itemType() const;
     const SearchResultTreeItem *parent() const;
-    const SearchResultTreeItem *childAt(int index) const;
+    SearchResultTreeItem *childAt(int index) const;
     void appendChild(SearchResultTreeItem *child);
     int childrenCount() const;
     int rowOfItem() const;
     void clearChildren();
 
+    bool isUserCheckable() const;
+    void setIsUserCheckable(bool isUserCheckable);
+
+    Qt::CheckState checkState() const;
+    void setCheckState(Qt::CheckState checkState);
+
 private:
     ItemType m_type;
     const SearchResultTreeItem *m_parent;
     QList<SearchResultTreeItem *> m_children;
+    bool m_isUserCheckable;
+    Qt::CheckState m_checkState;
 };
 
 class SearchResultTextRow : public SearchResultTreeItem
diff --git a/src/plugins/find/searchresulttreemodel.cpp b/src/plugins/find/searchresulttreemodel.cpp
index 5c3d5eb36ab..b711e8c1105 100644
--- a/src/plugins/find/searchresulttreemodel.cpp
+++ b/src/plugins/find/searchresulttreemodel.cpp
@@ -36,12 +36,14 @@
 #include <QtGui/QColor>
 #include <QtGui/QPalette>
 #include <QtCore/QDir>
+#include <QtCore/QDebug>
 
 using namespace Find::Internal;
 
 SearchResultTreeModel::SearchResultTreeModel(QObject *parent)
     : QAbstractItemModel(parent)
     , m_lastAppendedResultFile(0)
+    , m_showReplaceUI(false)
 {
     m_rootItem = new SearchResultTreeItem();
     m_textEditorFont = QFont("Courier");
@@ -52,11 +54,31 @@ SearchResultTreeModel::~SearchResultTreeModel()
     delete m_rootItem;
 }
 
+void SearchResultTreeModel::setShowReplaceUI(bool show)
+{
+    m_showReplaceUI = show;
+}
+
 void SearchResultTreeModel::setTextEditorFont(const QFont &font)
 {
     m_textEditorFont = font;
 }
 
+Qt::ItemFlags SearchResultTreeModel::flags(const QModelIndex &index) const
+{
+    Qt::ItemFlags flags = QAbstractItemModel::flags(index);
+
+    if (index.isValid()) {
+        if (const SearchResultTreeItem *item = static_cast<const SearchResultTreeItem*>(index.internalPointer())) {
+            if (item->itemType() == SearchResultTreeItem::ResultRow && item->isUserCheckable()) {
+                flags |= Qt::ItemIsUserCheckable;
+            }
+        }
+    }
+
+    return flags;
+}
+
 QModelIndex SearchResultTreeModel::index(int row, int column,
                                          const QModelIndex &parent) const
 {
@@ -135,12 +157,28 @@ QVariant SearchResultTreeModel::data(const QModelIndex &index, int role) const
     return result;
 }
 
+bool SearchResultTreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
+{
+    if (role == Qt::CheckStateRole) {
+        SearchResultTreeItem *item = static_cast<SearchResultTreeItem*>(index.internalPointer());
+        SearchResultTextRow *row = static_cast<SearchResultTextRow *>(item);
+        Qt::CheckState checkState = static_cast<Qt::CheckState>(value.toInt());
+        row->setCheckState(checkState);
+        return true;
+    }
+    return QAbstractItemModel::setData(index, value, role);
+}
+
 QVariant SearchResultTreeModel::data(const SearchResultTextRow *row, int role) const
 {
     QVariant result;
 
     switch (role)
     {
+    case Qt::CheckStateRole:
+        if (row->isUserCheckable())
+            result = row->checkState();
+        break;
     case Qt::ToolTipRole:
         result = row->rowText().trimmed();
         break;
@@ -188,6 +226,12 @@ QVariant SearchResultTreeModel::data(const SearchResultFile *file, int role) con
 
     switch (role)
     {
+#if 0
+    case Qt::CheckStateRole:
+        if (file->isUserCheckable())
+            result = file->checkState();
+        break;
+#endif
     case Qt::BackgroundRole: {
         const QColor baseColor = QApplication::palette().base().color();
         result = baseColor.darker(105);
@@ -228,6 +272,11 @@ void SearchResultTreeModel::appendResultFile(const QString &fileName)
 {
     m_lastAppendedResultFile = new SearchResultFile(fileName, m_rootItem);
 
+    if (m_showReplaceUI) {
+        m_lastAppendedResultFile->setIsUserCheckable(true);
+        m_lastAppendedResultFile->setCheckState(Qt::Checked);
+    }
+
     const int childrenCount = m_rootItem->childrenCount();
     beginInsertRows(QModelIndex(), childrenCount, childrenCount);
     m_rootItem->appendChild(m_lastAppendedResultFile);
diff --git a/src/plugins/find/searchresulttreemodel.h b/src/plugins/find/searchresulttreemodel.h
index 544ad01a5a1..f5b0fea20d7 100644
--- a/src/plugins/find/searchresulttreemodel.h
+++ b/src/plugins/find/searchresulttreemodel.h
@@ -48,13 +48,16 @@ public:
     SearchResultTreeModel(QObject *parent = 0);
     ~SearchResultTreeModel();
 
+    void setShowReplaceUI(bool show);
     void setTextEditorFont(const QFont &font);
 
+    Qt::ItemFlags flags(const QModelIndex &index) const;
     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
     QModelIndex parent(const QModelIndex &child) const;
     int rowCount(const QModelIndex &parent = QModelIndex()) const;
     int columnCount(const QModelIndex &parent = QModelIndex()) const;
     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
+    bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
     QVariant headerData(int section, Qt::Orientation orientation, int role) const;
 
     QModelIndex next(const QModelIndex &idx) const;
@@ -81,6 +84,7 @@ private:
     SearchResultTreeItem *m_rootItem;
     SearchResultFile *m_lastAppendedResultFile;
     QFont m_textEditorFont;
+    bool m_showReplaceUI;
 };
 
 } // namespace Internal
diff --git a/src/plugins/find/searchresultwindow.cpp b/src/plugins/find/searchresultwindow.cpp
index 728687ba1a7..1fe0b1c7e3e 100644
--- a/src/plugins/find/searchresultwindow.cpp
+++ b/src/plugins/find/searchresultwindow.cpp
@@ -109,6 +109,7 @@ SearchResultWindow::~SearchResultWindow()
 
 void SearchResultWindow::setShowReplaceUI(bool show)
 {
+    m_searchResultTreeView->model()->setShowReplaceUI(show);
     m_replaceLabel->setVisible(show);
     m_replaceTextEdit->setVisible(show);
     m_replaceButton->setVisible(show);
-- 
GitLab