1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
// SPDX-FileCopyrightText: 2003 Scott Wheeler <wheeler@kde.org>
// SPDX-FileCopyrightText: 2004 Max Howell <max.howell@methylblue.com>
// SPDX-FileCopyrightText: 2004 Mark Kretschmann <markey@web.de>
// SPDX-FileCopyrightText: 2008 Seb Ruiz <ruiz@kde.org>
// SPDX-FileCopyrightText: 2008 Sebastian Trueg <trueg@kde.org>
// SPDX-FileCopyrightText: 2020 Simon Persson <simon.persson@mykolab.com>
//
// SPDX-License-Identifier: GPL-2.0-only
#include "folderselectionmodel.h"
#include "kuputils.h"
#include <QBrush>
#include <QColor>
#include <QDir>
#include <QFileInfo>
#include <QIcon>
#include <QPalette>
#include <KLocalizedString>
namespace
{
bool setContainsSubdir(const QSet<QString> &pSet, const QString &pParentDir)
{
// we need the trailing slash to be able to use the startsWith() function to check for parent dirs.
QString lPathWithSlash = pParentDir;
ensureTrailingSlash(lPathWithSlash);
foreach (QString lTestedPath, pSet) {
if (lTestedPath.startsWith(lPathWithSlash)) {
return true;
}
}
return false;
}
}
FolderSelectionModel::FolderSelectionModel(bool pHiddenFoldersVisible, QObject *pParent)
: QFileSystemModel(pParent)
{
setHiddenFoldersVisible(pHiddenFoldersVisible);
}
Qt::ItemFlags FolderSelectionModel::flags(const QModelIndex &pIndex) const
{
Qt::ItemFlags lFlags = QFileSystemModel::flags(pIndex);
lFlags |= Qt::ItemIsUserCheckable;
return lFlags;
}
QVariant FolderSelectionModel::data(const QModelIndex &pIndex, int pRole) const
{
if (!pIndex.isValid() || pIndex.column() != 0) {
return QFileSystemModel::data(pIndex, pRole);
}
const QString lPath = filePath(pIndex);
const InclusionState lState = inclusionState(lPath);
switch (pRole) {
case Qt::CheckStateRole: {
switch (lState) {
case StateIncluded:
case StateIncludeInherited:
if (setContainsSubdir(mExcludedPaths, lPath)) {
return Qt::PartiallyChecked;
}
return Qt::Checked;
default:
return Qt::Unchecked;
}
}
case IncludeStateRole:
return inclusionState(pIndex);
case Qt::ForegroundRole: {
switch (lState) {
case StateIncluded:
case StateIncludeInherited:
return QVariant::fromValue(QPalette().brush(QPalette::Active, QPalette::Text));
default:
if (setContainsSubdir(mIncludedPaths, lPath)) {
return QVariant::fromValue(QPalette().brush(QPalette::Active, QPalette::Text));
}
return QVariant::fromValue(QPalette().brush(QPalette::Disabled, QPalette::Text));
}
}
case Qt::ToolTipRole: {
switch (lState) {
case StateIncluded:
case StateIncludeInherited:
if (setContainsSubdir(mExcludedPaths, lPath)) {
return xi18nc("@info:tooltip %1 is the path of the folder in a listview",
"<filename>%1</filename><nl/>will be included in the backup, except "
"for unchecked subfolders",
filePath(pIndex));
}
return xi18nc("@info:tooltip %1 is the path of the folder in a listview",
"<filename>%1</filename><nl/>will be included in the backup",
filePath(pIndex));
default:
if (setContainsSubdir(mIncludedPaths, lPath)) {
return xi18nc("@info:tooltip %1 is the path of the folder in a listview",
"<filename>%1</filename><nl/> will <emphasis>not</emphasis> be included "
"in the backup but contains folders that will",
filePath(pIndex));
}
return xi18nc("@info:tooltip %1 is the path of the folder in a listview",
"<filename>%1</filename><nl/> will <emphasis>not</emphasis> be included "
"in the backup",
filePath(pIndex));
}
}
case Qt::DecorationRole:
if (lPath == QDir::homePath()) {
return QIcon::fromTheme(QStringLiteral("user-home"));
}
break;
}
return QFileSystemModel::data(pIndex, pRole);
}
bool FolderSelectionModel::setData(const QModelIndex &pIndex, const QVariant &pValue, int pRole)
{
if (!pIndex.isValid() || pIndex.column() != 0 || pRole != Qt::CheckStateRole) {
return QFileSystemModel::setData(pIndex, pValue, pRole);
}
// here we ignore the check value, we treat it as a toggle
// This is due to our using the Qt checking system in a virtual way
const QString lPath = filePath(pIndex);
const InclusionState lState = inclusionState(lPath);
switch (lState) {
case StateIncluded:
case StateIncludeInherited:
excludePath(lPath);
break;
default:
includePath(lPath);
}
QModelIndex lRecurseIndex = pIndex;
while (lRecurseIndex.isValid()) {
emit dataChanged(lRecurseIndex, lRecurseIndex);
lRecurseIndex = lRecurseIndex.parent();
}
return true;
}
void FolderSelectionModel::includePath(const QString &pPath)
{
const InclusionState lState = inclusionState(pPath);
if (lState == StateIncluded) {
return;
}
removeSubDirs(pPath);
if (lState == StateNone || lState == StateExcludeInherited) {
mIncludedPaths.insert(pPath);
emit includedPathAdded(pPath);
}
emit dataChanged(index(pPath), findLastLeaf(index(pPath)));
}
void FolderSelectionModel::excludePath(const QString &pPath)
{
const InclusionState lState = inclusionState(pPath);
if (lState == StateExcluded) {
return;
}
removeSubDirs(pPath);
if (lState == StateIncludeInherited) {
mExcludedPaths.insert(pPath);
emit excludedPathAdded(pPath);
}
emit dataChanged(index(pPath), findLastLeaf(index(pPath)));
}
void FolderSelectionModel::setIncludedPaths(const QSet<QString> &pIncludedPaths)
{
QSet<QString> lRemoved = mIncludedPaths - pIncludedPaths;
QSet<QString> lAdded = pIncludedPaths - mIncludedPaths;
if (lRemoved.count() + lAdded.count() == 0)
return;
beginResetModel();
mIncludedPaths = pIncludedPaths;
foreach (const QString &lRemovedPath, lRemoved) {
emit includedPathRemoved(lRemovedPath);
}
foreach (const QString &lAddedPath, lAdded) {
emit includedPathAdded(lAddedPath);
}
endResetModel();
}
void FolderSelectionModel::setExcludedPaths(const QSet<QString> &pExcludedPaths)
{
QSet<QString> lRemoved = mExcludedPaths - pExcludedPaths;
QSet<QString> lAdded = pExcludedPaths - mExcludedPaths;
if (lRemoved.count() + lAdded.count() == 0)
return;
beginResetModel();
mExcludedPaths = pExcludedPaths;
foreach (const QString &lRemovedPath, lRemoved) {
emit excludedPathRemoved(lRemovedPath);
}
foreach (const QString &lAddedPath, lAdded) {
emit excludedPathAdded(lAddedPath);
}
endResetModel();
}
QSet<QString> FolderSelectionModel::includedPaths() const
{
return mIncludedPaths;
}
QSet<QString> FolderSelectionModel::excludedPaths() const
{
return mExcludedPaths;
}
FolderSelectionModel::InclusionState FolderSelectionModel::inclusionState(const QModelIndex &pIndex) const
{
return inclusionState(filePath(pIndex));
}
FolderSelectionModel::InclusionState FolderSelectionModel::inclusionState(const QString &pPath) const
{
if (mIncludedPaths.contains(pPath)) {
return StateIncluded;
}
if (mExcludedPaths.contains(pPath)) {
return StateExcluded;
}
QString lParent = pPath.section(QDir::separator(), 0, -2, QString::SectionSkipEmpty | QString::SectionIncludeLeadingSep);
if (lParent.isEmpty()) {
return StateNone;
}
InclusionState state = inclusionState(lParent);
if (state == StateNone) {
return StateNone;
}
if (state == StateIncluded || state == StateIncludeInherited) {
return StateIncludeInherited;
}
return StateExcludeInherited;
}
bool FolderSelectionModel::hiddenFoldersVisible() const
{
return filter() & QDir::Hidden;
}
void FolderSelectionModel::setHiddenFoldersVisible(bool pVisible)
{
if (pVisible) {
setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Hidden);
} else {
setFilter(QDir::AllDirs | QDir::NoDotAndDotDot);
}
}
QModelIndex FolderSelectionModel::findLastLeaf(const QModelIndex &pIndex)
{
QModelIndex lIndex = pIndex;
forever {
int lRowCount = rowCount(lIndex);
if (lRowCount > 0) {
lIndex = index(lRowCount - 1, 0, lIndex);
} else {
return lIndex;
}
}
}
void FolderSelectionModel::removeSubDirs(const QString &pPath)
{
QSet<QString>::iterator it = mExcludedPaths.begin();
QString lPath = pPath + QStringLiteral("/");
while (it != mExcludedPaths.end()) {
if (*it == pPath || it->startsWith(lPath)) {
QString lPathCopy = *it;
it = mExcludedPaths.erase(it);
emit excludedPathRemoved(lPathCopy);
} else {
++it;
}
}
it = mIncludedPaths.begin();
while (it != mIncludedPaths.end()) {
if (*it == pPath || it->startsWith(lPath)) {
QString lPathCopy = *it;
it = mIncludedPaths.erase(it);
emit includedPathRemoved(lPathCopy);
} else {
++it;
}
}
}
|