Browse Source

search: add search Object Path

1. For internal file and folder, search the relative path;
2. For orphan file, search the complete path;
Le Tan 7 years ago
parent
commit
28d359c4bb
5 changed files with 61 additions and 15 deletions
  1. 1 1
      src/resources/themes/v_pure/v_pure.palette
  2. 46 11
      src/vsearch.cpp
  3. 10 0
      src/vsearch.h
  4. 2 1
      src/vsearchconfig.h
  5. 2 2
      src/vsearcher.cpp

+ 1 - 1
src/resources/themes/v_pure/v_pure.palette

@@ -41,7 +41,7 @@ hover_fg=@base_fg
 hover_bg=#D0D0D0
 
 selected_fg=@base_fg
-selected_bg=#80CBC4
+selected_bg=#7BBAB9
 
 active_fg=@selected_fg
 active_bg=@selected_bg

+ 46 - 11
src/vsearch.cpp

@@ -1,7 +1,7 @@
 #include "vsearch.h"
 
 #include "utils/vutils.h"
-#include "vfile.h"
+#include "vnotefile.h"
 #include "vdirectory.h"
 #include "vnotebook.h"
 #include "veditarea.h"
@@ -16,6 +16,7 @@ VSearch::VSearch(QObject *p_parent)
       m_askedToStop(false),
       m_engine(NULL)
 {
+    m_slashReg = QRegExp("[\\/]");
 }
 
 QSharedPointer<VSearchResult> VSearch::search(const QVector<VFile *> &p_files)
@@ -152,6 +153,25 @@ void VSearch::searchFirstPhase(VFile *p_file,
         }
     }
 
+    if (testObject(VSearchConfig::Path)) {
+        QString normFilePath;
+        if (p_file->getType() == FileType::Note) {
+            normFilePath = static_cast<VNoteFile *>(p_file)->fetchRelativePath();
+        } else {
+            normFilePath = filePath;
+        }
+
+        removeSlashFromPath(normFilePath);
+        if (matchNonContent(normFilePath)) {
+            VSearchResultItem *item = new VSearchResultItem(VSearchResultItem::Note,
+                                                            VSearchResultItem::LineNumber,
+                                                            name,
+                                                            filePath);
+            QSharedPointer<VSearchResultItem> pitem(item);
+            emit resultItemAdded(pitem);
+        }
+    }
+
     if (testObject(VSearchConfig::Outline)) {
         VSearchResultItem *item = searchForOutline(p_file);
         if (item) {
@@ -190,16 +210,31 @@ void VSearch::searchFirstPhase(VDirectory *p_directory,
         return;
     }
 
-    if (testTarget(VSearchConfig::Folder)
-        && testObject(VSearchConfig::Name)) {
-        QString text = p_directory->getName();
-        if (matchNonContent(text)) {
-            VSearchResultItem *item = new VSearchResultItem(VSearchResultItem::Folder,
-                                                            VSearchResultItem::LineNumber,
-                                                            text,
-                                                            p_directory->fetchPath());
-            QSharedPointer<VSearchResultItem> pitem(item);
-            emit resultItemAdded(pitem);
+    if (testTarget(VSearchConfig::Folder)) {
+        QString name = p_directory->getName();
+        QString dirPath = p_directory->fetchPath();
+        if (testObject(VSearchConfig::Name)) {
+            if (matchNonContent(name)) {
+                VSearchResultItem *item = new VSearchResultItem(VSearchResultItem::Folder,
+                                                                VSearchResultItem::LineNumber,
+                                                                name,
+                                                                dirPath);
+                QSharedPointer<VSearchResultItem> pitem(item);
+                emit resultItemAdded(pitem);
+            }
+        }
+
+        if (testObject(VSearchConfig::Path)) {
+            QString normPath(p_directory->fetchRelativePath());
+            removeSlashFromPath(normPath);
+            if (matchNonContent(normPath)) {
+                VSearchResultItem *item = new VSearchResultItem(VSearchResultItem::Folder,
+                                                                VSearchResultItem::LineNumber,
+                                                                name,
+                                                                dirPath);
+                QSharedPointer<VSearchResultItem> pitem(item);
+                emit resultItemAdded(pitem);
+            }
         }
     }
 

+ 10 - 0
src/vsearch.h

@@ -74,6 +74,8 @@ private:
 
     void searchSecondPhase(const QSharedPointer<VSearchResult> &p_result);
 
+    void removeSlashFromPath(QString &p_path);
+
     bool m_askedToStop;
 
     QSharedPointer<VSearchConfig> m_config;
@@ -82,6 +84,9 @@ private:
 
     // Wildcard reg to for file name pattern.
     QRegExp m_patternReg;
+
+    // Remove slashes.
+    QRegExp m_slashReg;
 };
 
 inline bool VSearch::askedToStop() const
@@ -129,4 +134,9 @@ inline bool VSearch::matchPattern(const QString &p_name) const
 
     return p_name.contains(m_patternReg);
 }
+
+inline void VSearch::removeSlashFromPath(QString &p_path)
+{
+    p_path.remove(m_slashReg);
+}
 #endif // VSEARCH_H

+ 2 - 1
src/vsearchconfig.h

@@ -203,7 +203,8 @@ struct VSearchConfig
         Name = 0x1UL,
         Content = 0x2UL,
         Outline = 0x4UL,
-        Tag = 0x8UL
+        Tag = 0x8UL,
+        Path = 0x10UL
     };
 
     enum Target

+ 2 - 2
src/vsearcher.cpp

@@ -313,9 +313,9 @@ void VSearcher::handleInputChanged()
     readyToSearch = !keyword.isEmpty();
 
     if (readyToSearch) {
-        // Other targets are only available for Name.
+        // Other targets are only available for Name and Path.
         int obj = m_searchObjectCB->currentData().toInt();
-        if (obj != VSearchConfig::Name) {
+        if (obj != VSearchConfig::Name && obj != VSearchConfig::Path) {
             int target = m_searchTargetCB->currentData().toInt();
             if (!(target & VSearchConfig::Note)) {
                 readyToSearch = false;