Skip to content

Commit 8214d8a

Browse files
committed
dirmodel: Include empty directories in findFolders()
Directories which have just been created (and are still empty) should be considered for inclusion in the folder list. Update the logic to support this. Signed-off-by: Simon Glass <sjg@chromium.org>
1 parent 436bc71 commit 8214d8a

4 files changed

Lines changed: 50 additions & 15 deletions

File tree

dirmodel.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ void Dirmodel::addMatches(QStringList& matches, uint baseLen,
853853
for (int i = 0; i < parent->childCount(); i++) {
854854
const TreeItem *child = parent->childConst(i);
855855

856-
if (!child->childCount())
856+
if (!child->isDir())
857857
continue;
858858
QString leaf = child->dirName();
859859
QString fname = dirPath + leaf;
@@ -893,14 +893,15 @@ void Dirmodel::addFileMatches(QStringList& matches, const uint baseLen,
893893
const TreeItem *child = parent->childConst(i);
894894
const QString& fname = child->dirName();
895895

896-
if (child->childCount()) {
896+
if (child->isDir()) {
897897
addFileMatches(matches, baseLen, dirPath + fname + "/", child, text);
898898
} else {
899-
900899
if (fname.contains(text, Qt::CaseInsensitive))
901900
matches << dirPath.mid(baseLen) + fname;
902901
}
903902
}
903+
if (!parent->childCount())
904+
matches << dirPath.mid(baseLen);
904905
}
905906

906907
QStringList Dirmodel::findFiles(const QString& text, const QString& dirPath,

test/test_dirmodel.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void TestDirmodel::testAddDir()
5858
QModelIndex main = model->index(main_path);
5959
model->buildCache(main, 0);
6060

61-
QCOMPARE(getPaperTree(main_path), " one| a| b| two|");
61+
QCOMPARE(getPaperTree(main_path), " + one| + a| + b| + two|");
6262

6363
Q_ASSERT(main.isValid());
6464

@@ -72,15 +72,15 @@ void TestDirmodel::testAddDir()
7272
Q_ASSERT(dira.exists());
7373
QCOMPARE(model->data(new_a, Qt::DisplayRole).toString(), "new-dira");
7474

75-
QCOMPARE(getPaperTree(main_path), " one| a| b| new-dira| two|");
75+
QCOMPARE(getPaperTree(main_path), " + one| + a| + b| + new-dira| + two|");
7676

7777
QDir dirb(_tempDir->path() + "/main/one/new-dirb");
7878
QModelIndex new_b = model->mkdir(dir_one, "new-dirb", 0);
7979
Q_ASSERT(new_b.isValid());
8080
Q_ASSERT(dirb.exists());
8181

8282
QCOMPARE(getPaperTree(main_path),
83-
" one| a| b| new-dira| new-dirb| two|");
83+
" + one| + a| + b| + new-dira| + new-dirb| + two|");
8484

8585
// Since we added something to the model, the old index isn't valid, so get
8686
// a new one
@@ -100,7 +100,8 @@ void TestDirmodel::testCacheFiles()
100100
QModelIndex main = model->index(main_path);
101101
model->buildCache(main, 0);
102102

103-
QCOMPARE(getPaperTree(main_path), " one| a| b| ofile| ofile2| two|");
103+
QCOMPARE(getPaperTree(main_path),
104+
" + one| + a| + b| - ofile| - ofile2| + two|");
104105
}
105106

106107
void TestDirmodel::testAddFiles()
@@ -114,7 +115,8 @@ void TestDirmodel::testAddFiles()
114115
QModelIndex main = model->index(main_path);
115116
model->buildCache(main, 0);
116117

117-
QCOMPARE(getPaperTree(main_path), " one| a| b| ofile| ofile2| two|");
118+
QCOMPARE(getPaperTree(main_path),
119+
" + one| + a| + b| - ofile| - ofile2| + two|");
118120

119121
Q_ASSERT(main.isValid());
120122

@@ -126,26 +128,30 @@ void TestDirmodel::testAddFiles()
126128
QModelIndex dir_a = model->index(main_path + "/one/a");
127129
model->refreshCacheFrom(dir_a, nullptr);
128130

129-
QCOMPARE(getPaperTree(main_path), " one| a| b| ofile| ofile2| two|");
131+
QCOMPARE(getPaperTree(main_path),
132+
" + one| + a| + b| - ofile| - ofile2| + two|");
130133

131134
// Refreshing 'one' should update
132135
QModelIndex one = model->index(main_path + "/one");
133136
model->refreshCacheFrom(one, nullptr);
134137

135138
QCOMPARE(getPaperTree(main_path),
136-
" one| a| b| newfile| ofile| ofile2| two|");
139+
" + one| + a| + b| - newfile| - ofile| - ofile2| + two|");
137140

138141
QDir dir;
139142
Q_ASSERT(dir.remove(dst + "/main/one/newfile"));
140143

141144
// Refreshing 'two' should do nothing
142-
QModelIndex two = model->index(main_path + "/two/a");
145+
QModelIndex two = model->index(main_path + "/two");
146+
Q_ASSERT(two.isValid());
143147
model->refreshCacheFrom(two, nullptr);
144148

145149
// Refreshing 'one' should update
150+
Q_ASSERT(one.isValid());
146151
model->refreshCacheFrom(one, nullptr);
147152

148-
QCOMPARE(getPaperTree(main_path), " one| a| b| ofile| ofile2| two|");
153+
QCOMPARE(getPaperTree(main_path),
154+
" + one| + a| + b| - ofile| - ofile2| + two|");
149155
}
150156

151157
void TestDirmodel::checkModel(const QAbstractItemModel *model,

utils.cpp

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ bool utilDropSupported(QDropEvent *event, const QStringList& allowedTypes)
777777
}
778778

779779
TreeItem::TreeItem(const QVector<QVariant> &data, TreeItem *parent)
780-
: m_itemData(data), m_parentItem(parent)
780+
: m_itemData(data), m_parentItem(parent), m_isDir(false)
781781
{}
782782

783783
TreeItem::~TreeItem()
@@ -829,6 +829,16 @@ int TreeItem::childCount() const
829829
return m_childItems.count();
830830
}
831831

832+
bool TreeItem::isDir() const
833+
{
834+
return m_isDir;
835+
}
836+
837+
void TreeItem::setDir(bool isdir)
838+
{
839+
m_isDir = isdir;
840+
}
841+
832842
int TreeItem::columnCount() const
833843
{
834844
return m_itemData.count();
@@ -901,6 +911,10 @@ void TreeItem::write(QTextStream& stream, int level) const
901911
if (level) {
902912
for (int i = 0; i < level; i++)
903913
stream << QString(" ");
914+
if (isDir())
915+
stream << QString("+ ");
916+
else
917+
stream << QString("- ");
904918
stream << dirName() << '\n';
905919
}
906920
foreach (TreeItem *item, m_childItems)
@@ -925,9 +939,18 @@ bool TreeItem::read(QTextStream& stream, TreeItem *parent, int cur_level)
925939
qInfo() << "Invalid level < 1" << line;
926940
return false;
927941
}
928-
QString fname = line.mid(level);
942+
943+
QString type, fname;
944+
if (line.mid(level + 1, 1) == " ") {
945+
type = line.mid(level, 1);
946+
fname = line.mid(level + 2);
947+
} else {
948+
fname = line.mid(level);
949+
}
929950

930951
TreeItem *child = new TreeItem({fname}, nullptr);
952+
if (type == "+")
953+
child->setDir(true);
931954

932955
if (level == cur_level) {
933956
child->m_parentItem = parent;
@@ -979,8 +1002,10 @@ static void scanDir(const QString &dirPath, TreeItem *parent, Operation *op)
9791002
TreeItem *child = new TreeItem(columnData, parent);
9801003

9811004
parent->appendChild(child);
982-
if (fi.isDir ())
1005+
if (fi.isDir ()) {
1006+
child->setDir(true);
9831007
scanDir(dirPath + fi.fileName() + "/", child, 0);
1008+
}
9841009
}
9851010
if (op)
9861011
op->setProgress(i);

utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ class TreeItem
6767
TreeItem *child(const QString& name) const;
6868

6969
const TreeItem *childConst(int row) const;
70+
bool isDir() const;
71+
void setDir(bool isdir);
7072
int childCount() const;
7173
int columnCount() const;
7274
QVariant data(int column) const;
@@ -102,6 +104,7 @@ class TreeItem
102104
QVector<TreeItem*> m_childItems;
103105
QVector<QVariant> m_itemData;
104106
TreeItem *m_parentItem;
107+
bool m_isDir; // true if this is a directory
105108
};
106109

107110
/** retrieves a list of sizes from the Qt settings file.

0 commit comments

Comments
 (0)