changeset 555:a1d210c767ab find

Implement basic search function in both work and history tabs
author Chris Cannam
date Wed, 22 Feb 2012 14:54:30 +0000
parents 930462068dcc
children 04f18b2a32e8
files src/changesetitem.cpp src/changesetitem.h src/filestatuswidget.cpp src/filestatuswidget.h src/hgtabwidget.cpp src/hgtabwidget.h src/historywidget.cpp src/historywidget.h src/mainwindow.cpp src/mainwindow.h
diffstat 10 files changed, 108 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/changesetitem.cpp	Mon Feb 20 18:15:53 2012 +0000
+++ b/src/changesetitem.cpp	Wed Feb 22 14:54:30 2012 +0000
@@ -38,7 +38,7 @@
 ChangesetItem::ChangesetItem(Changeset *cs) :
     m_changeset(cs), m_detail(0),
     m_showBranch(false), m_column(0), m_row(0), m_wide(false),
-    m_current(false), m_closing(false), m_new(false)
+    m_current(false), m_closing(false), m_new(false), m_searchMatches(false)
 {
     m_font = QFont();
     m_font.setPixelSize(11);
@@ -91,6 +91,15 @@
     emit detailHidden();
 }    
 
+bool
+ChangesetItem::setSearchText(QString text)
+{
+    m_searchText = text;
+    m_searchMatches = (m_changeset->comment().contains
+                       (text, Qt::CaseInsensitive));
+    return m_searchMatches;
+}
+
 void
 ChangesetItem::mousePressEvent(QGraphicsSceneMouseEvent *e)
 {
@@ -296,6 +305,17 @@
     bool showText = (scale >= 0.2);
     bool showProperLines = (scale >= 0.1);
 
+    if (m_searchText != "") {
+        if (m_searchMatches) {
+            userColour = QColor("#008400");
+            showProperLines = true;
+            showText = true;
+        } else {
+            branchColour = Qt::gray;
+            userColour = Qt::gray;
+        }
+    }
+
     if (!showProperLines) {
 	paint->setPen(QPen(branchColour, 0));
     } else {
@@ -452,6 +472,8 @@
 
     paint->setFont(f);
 
+    if (m_searchMatches) paint->setPen(userColour);
+
     for (int i = 0; i < lines.size(); ++i) {
 	paint->drawText(x0 + 3, i * fh + fh + fm.ascent(), lines[i].trimmed());
     }
--- a/src/changesetitem.h	Mon Feb 20 18:15:53 2012 +0000
+++ b/src/changesetitem.h	Wed Feb 22 14:54:30 2012 +0000
@@ -65,6 +65,8 @@
     bool showBranch() const { return m_showBranch; }
     void setShowBranch(bool s) { m_showBranch = s; }
 
+    bool setSearchText(QString text); // return true if it matches
+
 signals:
     void detailShown();
     void detailHidden();
@@ -110,6 +112,8 @@
     bool m_current;
     bool m_closing;
     bool m_new;
+    QString m_searchText;
+    bool m_searchMatches;
 
     QMap<QAction *, QString> m_parentDiffActions;
     QMap<QAction *, QString> m_summaryActions;
--- a/src/filestatuswidget.cpp	Mon Feb 20 18:15:53 2012 +0000
+++ b/src/filestatuswidget.cpp	Wed Feb 22 14:54:30 2012 +0000
@@ -413,6 +413,14 @@
 }
 
 void
+FileStatusWidget::setSearchText(QString text)
+{
+    if (m_searchText == text) return;
+    m_searchText = text;
+    updateWidgets();
+}
+
+void
 FileStatusWidget::updateWidgets()
 {
     QDateTime lastInteractionTime;
@@ -425,6 +433,7 @@
     foreach (QString f, m_selectedFiles) selectedFiles.insert(f);
 
     int visibleCount = 0;
+    bool finding = (m_searchText != "");
 
     foreach (FileStates::State s, m_stateListMap.keys()) {
 
@@ -442,6 +451,13 @@
 
         foreach (QString file, files) {
 
+            if (finding) {
+                if (file.contains(m_searchText, Qt::CaseInsensitive)) {
+                    highPriority.push_back(file);
+                }
+                continue;
+            }
+
             bool highlighted = false;
 
             if (s == FileStates::Unknown) {
@@ -467,7 +483,11 @@
         foreach (QString file, highPriority) {
             QListWidgetItem *item = new QListWidgetItem(file);
             w->addItem(item);
-            item->setForeground(QColor("#d40000"));
+            if (finding) {
+                item->setForeground(QColor("#008400"));
+            } else {
+                item->setForeground(QColor("#d40000"));
+            }                
             item->setSelected(selectedFiles.contains(file));
         }
 
--- a/src/filestatuswidget.h	Mon Feb 20 18:15:53 2012 +0000
+++ b/src/filestatuswidget.h	Wed Feb 22 14:54:30 2012 +0000
@@ -78,6 +78,8 @@
     void clearSelections();
     void updateWidgets();
 
+    void setSearchText(QString text);
+
 private slots:
     void menuActionActivated();
     void itemSelectionChanged();
@@ -104,6 +106,8 @@
     QList<QWidget *> m_boxes;
     QWidget *m_boxesParent;
 
+    QString m_searchText;
+
     void layoutBoxesGridly(int count);
     void layoutBoxesLinearly();
     void setNoModificationsLabelText();
--- a/src/hgtabwidget.cpp	Mon Feb 20 18:15:53 2012 +0000
+++ b/src/hgtabwidget.cpp	Wed Feb 22 14:54:30 2012 +0000
@@ -154,6 +154,12 @@
     m_historyWidget->update();
 }
 
+void HgTabWidget::setSearchText(QString text)
+{
+    m_fileStatusWidget->setSearchText(text);
+    m_historyWidget->setSearchText(text);
+}
+
 bool HgTabWidget::canDiff() const
 {
     return canRevert();
--- a/src/hgtabwidget.h	Mon Feb 20 18:15:53 2012 +0000
+++ b/src/hgtabwidget.h	Wed Feb 22 14:54:30 2012 +0000
@@ -113,6 +113,8 @@
     void showWorkTab();
     void showHistoryTab();
 
+    void setSearchText(QString text);
+
 private:
     FileStatusWidget *m_fileStatusWidget;
     HistoryWidget *m_historyWidget;
--- a/src/historywidget.cpp	Mon Feb 20 18:15:53 2012 +0000
+++ b/src/historywidget.cpp	Wed Feb 22 14:54:30 2012 +0000
@@ -241,6 +241,7 @@
         toFocus->ensureVisible();
     }
 
+    updateSearchStatus();
     connectSceneSignals();
 }
 
@@ -294,6 +295,38 @@
     }
 }
 
+void HistoryWidget::setSearchText(QString text)
+{
+    if (m_searchText == text) return;
+    m_searchText = text;
+    updateSearchStatus();
+}
+
+void HistoryWidget::updateSearchStatus()
+{
+    QGraphicsScene *scene = m_panned->scene();
+    if (!scene) return;
+
+    ChangesetItem *toFocus = 0;
+
+    QList<QGraphicsItem *> items = scene->items();
+    foreach (QGraphicsItem *it, items) {
+
+        ChangesetItem *csit = dynamic_cast<ChangesetItem *>(it);
+        if (!csit) continue;
+        
+        bool matched = csit->setSearchText(m_searchText);
+        if (matched && (!toFocus || csit->row() < toFocus->row())) {
+            toFocus = csit;
+        }
+        csit->update();
+    }
+
+    if (toFocus) {
+        toFocus->ensureVisible();
+    }
+}
+
 void HistoryWidget::connectSceneSignals()
 {
     ChangesetScene *scene = qobject_cast<ChangesetScene *>(m_panned->scene());
--- a/src/historywidget.h	Mon Feb 20 18:15:53 2012 +0000
+++ b/src/historywidget.h	Wed Feb 22 14:54:30 2012 +0000
@@ -68,6 +68,9 @@
 
 private slots:
     void showClosedChanged(bool);
+
+public slots:
+    void setSearchText(QString);
     
 private:
     Changesets m_changesets;
@@ -82,6 +85,8 @@
     Panner *m_panner;
     QCheckBox *m_showClosedBranches;
 
+    QString m_searchText;
+
     QGraphicsScene *scene();
     void clearChangesets();
     void replaceChangesets(Changesets);
@@ -90,6 +95,7 @@
     void setChangesetParents();
     void updateNewAndCurrentItems();
     void connectSceneSignals();
+    void updateSearchStatus();
 };
 
 #endif
--- a/src/mainwindow.cpp	Mon Feb 20 18:15:53 2012 +0000
+++ b/src/mainwindow.cpp	Wed Feb 22 14:54:30 2012 +0000
@@ -112,6 +112,8 @@
 
     m_findWidget = new FindWidget(this);
     cl->addWidget(m_findWidget, row++, 1, Qt::AlignRight | Qt::AlignTop);
+    connect(m_findWidget, SIGNAL(findTextChanged(QString)),
+            this, SLOT(findTextChanged(QString)));
 
     m_hgTabs = new HgTabWidget(central, m_workFolderPath);
     connectTabsSignals();
@@ -230,6 +232,12 @@
     hgQueryPaths();
 }
 
+void MainWindow::findTextChanged(QString text)
+{
+    std::cerr << "find: " << text << std::endl;
+    m_hgTabs->setSearchText(text);
+}
+
 void MainWindow::hgRefresh()
 {
     clearState();
--- a/src/mainwindow.h	Mon Feb 20 18:15:53 2012 +0000
+++ b/src/mainwindow.h	Wed Feb 22 14:54:30 2012 +0000
@@ -69,6 +69,7 @@
     void startupDialog();
     void clearSelections();
     void showAllChanged();
+    void findTextChanged(QString);
 
     void hgTest();
     void hgTestExtension();