search: add search Object Path

1. For internal file and folder, search the relative path;
2. For orphan file, search the complete path;
This commit is contained in:
Le Tan 2018-03-21 19:34:59 +08:00
parent a6c07a6dda
commit 28d359c4bb
5 changed files with 61 additions and 15 deletions

View File

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

View File

@ -1,7 +1,7 @@
#include "vsearch.h" #include "vsearch.h"
#include "utils/vutils.h" #include "utils/vutils.h"
#include "vfile.h" #include "vnotefile.h"
#include "vdirectory.h" #include "vdirectory.h"
#include "vnotebook.h" #include "vnotebook.h"
#include "veditarea.h" #include "veditarea.h"
@ -16,6 +16,7 @@ VSearch::VSearch(QObject *p_parent)
m_askedToStop(false), m_askedToStop(false),
m_engine(NULL) m_engine(NULL)
{ {
m_slashReg = QRegExp("[\\/]");
} }
QSharedPointer<VSearchResult> VSearch::search(const QVector<VFile *> &p_files) 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)) { if (testObject(VSearchConfig::Outline)) {
VSearchResultItem *item = searchForOutline(p_file); VSearchResultItem *item = searchForOutline(p_file);
if (item) { if (item) {
@ -190,19 +210,34 @@ void VSearch::searchFirstPhase(VDirectory *p_directory,
return; return;
} }
if (testTarget(VSearchConfig::Folder) if (testTarget(VSearchConfig::Folder)) {
&& testObject(VSearchConfig::Name)) { QString name = p_directory->getName();
QString text = p_directory->getName(); QString dirPath = p_directory->fetchPath();
if (matchNonContent(text)) { if (testObject(VSearchConfig::Name)) {
if (matchNonContent(name)) {
VSearchResultItem *item = new VSearchResultItem(VSearchResultItem::Folder, VSearchResultItem *item = new VSearchResultItem(VSearchResultItem::Folder,
VSearchResultItem::LineNumber, VSearchResultItem::LineNumber,
text, name,
p_directory->fetchPath()); dirPath);
QSharedPointer<VSearchResultItem> pitem(item); QSharedPointer<VSearchResultItem> pitem(item);
emit resultItemAdded(pitem); 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);
}
}
}
// Search files. // Search files.
if (testTarget(VSearchConfig::Note)) { if (testTarget(VSearchConfig::Note)) {
for (auto const & file : p_directory->getFiles()) { for (auto const & file : p_directory->getFiles()) {

View File

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

View File

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

View File

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