| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "sortfiltertreeproxymodel.h" |
| 7 |
|
| 8 |
namespace Avogadro::QtGui { |
| 9 |
|
| 10 |
// Custom class for Avogadro to handle filtering files |
| 11 |
// Directories are at most 2 levels deep until we get to files |
| 12 |
bool SortFilterTreeProxyModel::filterAcceptsRow( |
| 13 |
int sourceRow, const QModelIndex& sourceParent) const |
| 14 |
{ |
| 15 |
// First we see if we're the source root node |
| 16 |
QModelIndex sourceIndex = sourceModel()->index(sourceRow, 0, sourceParent); |
| 17 |
if (!sourceIndex.isValid() || !sourceParent.isValid()) |
| 18 |
return true; // viewer will handle filtering |
| 19 |
// Make sure the root is always accepted, or we become rootless |
| 20 |
// See |
| 21 |
// http://stackoverflow.com/questions/3212392/qtreeview-qfilesystemmodel-setrootpath-and-qsortfilterproxymodel-with-regexp-fo |
| 22 |
if (m_sourceRoot.isValid() && sourceIndex == m_sourceRoot) { |
| 23 |
return true; // true root, always accept |
| 24 |
} |
| 25 |
|
| 26 |
QString data = sourceModel()->data(sourceIndex).toString(); |
| 27 |
// ignore any image files |
| 28 |
if (data.endsWith(".png") || data.endsWith(".svg")) |
| 29 |
return false; |
| 30 |
// ignore any .smi files |
| 31 |
if (data.endsWith(".smi")) |
| 32 |
return false; |
| 33 |
// other development files like .json, .md, and .py |
| 34 |
if (data.endsWith(".json") || data.endsWith(".md") || data.endsWith(".py") || |
| 35 |
data.endsWith(".toml")) |
| 36 |
return false; |
| 37 |
|
| 38 |
// Now we see if we're a child of the root |
| 39 |
// If not, we accept -- only filter under *our* tree |
| 40 |
// Along the way, we'll see if a parent matches the filter |
| 41 |
if (sourceParent != m_sourceRoot) { |
| 42 |
bool childOfRoot = false; |
| 43 |
QModelIndex parent = sourceParent; |
| 44 |
for (int depth = 3; depth > 0; depth--) { |
| 45 |
if (sourceModel()->data(parent).toString().contains( |
| 46 |
filterRegularExpression())) |
| 47 |
return true; // a parent matches the pattern |
| 48 |
|
| 49 |
parent = parent.parent(); |
| 50 |
if (!parent.isValid()) |
| 51 |
return true; // tree view handles filtering, and we ascended too far |
| 52 |
if (parent == m_sourceRoot) { |
| 53 |
childOfRoot = true; |
| 54 |
break; |
| 55 |
} |
| 56 |
} |
| 57 |
// OK, we've gone up the tree, did we find our root? |
| 58 |
if (!childOfRoot) |
| 59 |
return true; |
| 60 |
} |
| 61 |
// else, sourceParent is a root, so we're good to filter |
| 62 |
|
| 63 |
// Check if the data for this row matches. If so, the default is to accept |
| 64 |
if (data.contains(filterRegularExpression())) |
| 65 |
return true; |
| 66 |
|
| 67 |
// Now we have to check the child nodes |
| 68 |
// We'll show the row if any child is accepted |
| 69 |
// (i.e., if a file matches, we'll show the directory path to it) |
| 70 |
// Try to fetchMore() first |
| 71 |
sourceModel()->fetchMore(sourceIndex); |
| 72 |
for (int i = 0; i < sourceModel()->rowCount(sourceIndex); ++i) { |
| 73 |
QModelIndex subRow = sourceModel()->index(i, 0, sourceIndex); |
| 74 |
if (!subRow.isValid()) |
| 75 |
continue; |
| 76 |
|
| 77 |
QString rowData = sourceModel()->data(subRow).toString(); |
| 78 |
if (rowData.contains(filterRegularExpression())) |
| 79 |
return true; |
| 80 |
} |
| 81 |
return false; // nothing matched |
| 82 |
} |
| 83 |
|
| 84 |
} // namespace Avogadro::QtGui |