| Line | Source | Count |
| 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 | #include "qdirmodel.h" | - |
| 41 | | - |
| 42 | #ifndef QT_NO_DIRMODEL | - |
| #include <qstack.h>#include <qfile.h> | |
| 44 | #include <qfilesystemmodel.h> | - |
| 45 | #include <qurl.h> | - |
| 46 | #include <qmimedata.h> | - |
| 47 | #include <qpair.h> | - |
| 48 | #include <qvector.h> | - |
| 49 | #include <qobject.h> | - |
| 50 | #include <qdatetime.h> | - |
| 51 | #include <qlocale.h> | - |
| 52 | #include <qstyle.h> | - |
| 53 | #include <qapplication.h> | - |
| 54 | #include <private/qabstractitemmodel_p.h> | - |
| 55 | #include <qdebug.h> | - |
| 56 | | - |
| 57 | #include <stack> | - |
| 58 | #include <vector> | - |
| 59 | | - |
| 60 | | - |
| 61 | | - |
| 62 | | - |
| 63 | | - |
| 64 | | - |
| 65 | | - |
| 66 | | - |
| 67 | QT_BEGIN_NAMESPACE | - |
| 68 | | - |
| 69 | class QDirModelPrivate : public QAbstractItemModelPrivate | - |
| 70 | { | - |
| 71 | Q_DECLARE_PUBLIC(QDirModel) | - |
| 72 | | - |
| 73 | public: | - |
| 74 | struct QDirNode | - |
| 75 | { | - |
| 76 | QDirNode() : parent(0), populated(false), stat(false) {} | - |
| 77 | QDirNode *parent; | - |
| 78 | QFileInfo info; | - |
| 79 | QIcon icon; | - |
| 80 | mutable QVector<QDirNode> children; | - |
| 81 | mutable bool populated; | - |
| 82 | mutable bool stat; | - |
| 83 | }; | - |
| 84 | | - |
| 85 | QDirModelPrivate() | - |
| 86 | : resolveSymlinks(true), | - |
| 87 | readOnly(true), | - |
| 88 | lazyChildCount(false), | - |
| 89 | allowAppendChild(true), | - |
| 90 | iconProvider(&defaultProvider), | - |
| 91 | shouldStat(true) | - |
| 92 | { } | - |
| 93 | | - |
| 94 | void init(); | - |
| 95 | QDirNode *node(int row, QDirNode *parent) const; | - |
| 96 | QVector<QDirNode> children(QDirNode *parent, bool stat) const; | - |
| 97 | | - |
| 98 | void _q_refresh(); | - |
| 99 | | - |
| 100 | void savePersistentIndexes(); | - |
| 101 | void restorePersistentIndexes(); | - |
| 102 | | - |
| 103 | QFileInfoList entryInfoList(const QString &path) const; | - |
| 104 | QStringList entryList(const QString &path) const; | - |
| 105 | | - |
| 106 | QString name(const QModelIndex &index) const; | - |
| 107 | QString size(const QModelIndex &index) const; | - |
| 108 | QString type(const QModelIndex &index) const; | - |
| 109 | QString time(const QModelIndex &index) const; | - |
| 110 | | - |
| 111 | void appendChild(QDirModelPrivate::QDirNode *parent, const QString &path) const; | - |
| 112 | static QFileInfo resolvedInfo(QFileInfo info); | - |
| 113 | | - |
| 114 | inline QDirNode *node(const QModelIndex &index) const; | - |
| 115 | inline void populate(QDirNode *parent) const; | - |
| 116 | inline void clear(QDirNode *parent) const; | - |
| 117 | | - |
| 118 | void invalidate(); | - |
| 119 | | - |
| 120 | mutable QDirNode root; | - |
| 121 | bool resolveSymlinks; | - |
| 122 | bool readOnly; | - |
| 123 | bool lazyChildCount; | - |
| 124 | bool allowAppendChild; | - |
| 125 | | - |
| 126 | QDir::Filters filters; | - |
| 127 | QDir::SortFlags sort; | - |
| 128 | QStringList nameFilters; | - |
| 129 | | - |
| 130 | QFileIconProvider *iconProvider; | - |
| 131 | QFileIconProvider defaultProvider; | - |
| 132 | | - |
| 133 | struct SavedPersistent { | - |
| 134 | QString path; | - |
| 135 | int column; | - |
| 136 | QPersistentModelIndexData *data; | - |
| 137 | QPersistentModelIndex index; | - |
| 138 | }; | - |
| 139 | QListQVector<SavedPersistent> savedPersistent; | - |
| 140 | QPersistentModelIndex toBeRefreshed; | - |
| 141 | | - |
| 142 | bool shouldStat; | - |
| 143 | }; | - |
| 144 | Q_DECLARE_TYPEINFO(QDirModelPrivate::SavedPersistent, Q_MOVABLE_TYPE); | - |
| 145 | | - |
| 146 | void qt_setDirModelShouldNotStat(QDirModelPrivate *modelPrivate) | - |
| 147 | { | - |
| 148 | modelPrivate->shouldStat = false; | - |
| 149 | } | - |
| 150 | | - |
| 151 | QDirModelPrivate::QDirNode *QDirModelPrivate::node(const QModelIndex &index) const | - |
| 152 | { | - |
| 153 | QDirModelPrivate::QDirNode *n = | - |
| 154 | static_cast<QDirModelPrivate::QDirNode*>(index.internalPointer()); | - |
| 155 | Q_ASSERT(n); | - |
| 156 | return n; | - |
| 157 | } | - |
| 158 | | - |
| 159 | void QDirModelPrivate::populate(QDirNode *parent) const | - |
| 160 | { | - |
| 161 | Q_ASSERT(parent); | - |
| 162 | parent->children = children(parent, parent->stat); | - |
| 163 | parent->populated = true; | - |
| 164 | } | - |
| 165 | | - |
| 166 | void QDirModelPrivate::clear(QDirNode *parent) const | - |
| 167 | { | - |
| 168 | Q_ASSERT(parent); | - |
| 169 | parent->children.clear(); | - |
| 170 | parent->populated = false; | - |
| 171 | } | - |
| 172 | | - |
| 173 | void QDirModelPrivate::invalidate() | - |
| 174 | { | - |
| 175 | QStackstd::stack<const QDirNode*, std::vector<const QDirNode*> > nodes; | - |
| 176 | nodes.push(&root); | - |
| 177 | while (!nodes.empty()) {| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 178 | const QDirNode *current = nodes.top(); | - |
| 179 | nodes.pop(); | - |
| 180 | current->stat = false; | - |
| 181 | const QVector<QDirNode> children&children = current->children; | - |
| 182 | for (int i = 0; i <const auto &child : children.count(); ++i) | - |
| 183 | nodes.push(&children.at(i));&child); never executed: nodes.push(&child); | 0 |
| 184 | } never executed: end of block | 0 |
| 185 | } never executed: end of block | 0 |
| 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 | QDirModel::QDirModel(const QStringList &nameFilters, | - |
| 229 | QDir::Filters filters, | - |
| 230 | QDir::SortFlags sort, | - |
| 231 | QObject *parent) | - |
| 232 | : QAbstractItemModel(*new QDirModelPrivate, parent) | - |
| 233 | { | - |
| 234 | Q_D(QDirModel); | - |
| 235 | | - |
| 236 | d->nameFilters = nameFilters.isEmpty() ? QStringList(QLatin1String("*")) : nameFilters; | - |
| 237 | d->filters = filters; | - |
| 238 | d->sort = sort; | - |
| 239 | d->root.parent = 0; | - |
| 240 | d->root.info = QFileInfo(); | - |
| 241 | d->clear(&d->root); | - |
| 242 | } | - |
| 243 | | - |
| 244 | | - |
| 245 | | - |
| 246 | | - |
| 247 | | - |
| 248 | QDirModel::QDirModel(QObject *parent) | - |
| 249 | : QAbstractItemModel(*new QDirModelPrivate, parent) | - |
| 250 | { | - |
| 251 | Q_D(QDirModel); | - |
| 252 | d->init(); | - |
| 253 | } | - |
| 254 | | - |
| 255 | | - |
| 256 | | - |
| 257 | | - |
| 258 | QDirModel::QDirModel(QDirModelPrivate &dd, QObject *parent) | - |
| 259 | : QAbstractItemModel(dd, parent) | - |
| 260 | { | - |
| 261 | Q_D(QDirModel); | - |
| 262 | d->init(); | - |
| 263 | } | - |
| 264 | | - |
| 265 | | - |
| 266 | | - |
| 267 | | - |
| 268 | | - |
| 269 | QDirModel::~QDirModel() | - |
| 270 | { | - |
| 271 | | - |
| 272 | } | - |
| 273 | | - |
| 274 | | - |
| 275 | | - |
| 276 | | - |
| 277 | | - |
| 278 | | - |
| 279 | | - |
| 280 | QModelIndex QDirModel::index(int row, int column, const QModelIndex &parent) const | - |
| 281 | { | - |
| 282 | Q_D(const QDirModel); | - |
| 283 | | - |
| 284 | if (column < 0 || column >= columnCount(parent) || row < 0 || parent.column() > 0) | - |
| 285 | return QModelIndex(); | - |
| 286 | | - |
| 287 | QDirModelPrivate::QDirNode *p = (d->indexValid(parent) ? d->node(parent) : &d->root); | - |
| 288 | Q_ASSERT(p); | - |
| 289 | if (!p->populated) | - |
| 290 | d->populate(p); | - |
| 291 | if (row >= p->children.count()) | - |
| 292 | return QModelIndex(); | - |
| 293 | | - |
| 294 | QDirModelPrivate::QDirNode *n = d->node(row, d->indexValid(parent) ? p : 0); | - |
| 295 | Q_ASSERT(n); | - |
| 296 | | - |
| 297 | return createIndex(row, column, n); | - |
| 298 | } | - |
| 299 | | - |
| 300 | | - |
| 301 | | - |
| 302 | | - |
| 303 | | - |
| 304 | QModelIndex QDirModel::parent(const QModelIndex &child) const | - |
| 305 | { | - |
| 306 | Q_D(const QDirModel); | - |
| 307 | | - |
| 308 | if (!d->indexValid(child)) | - |
| 309 | return QModelIndex(); | - |
| 310 | QDirModelPrivate::QDirNode *node = d->node(child); | - |
| 311 | QDirModelPrivate::QDirNode *par = (node ? node->parent : 0); | - |
| 312 | if (par == 0) | - |
| 313 | return QModelIndex(); | - |
| 314 | | - |
| 315 | | - |
| 316 | const QVector<QDirModelPrivate::QDirNode> children = | - |
| 317 | par->parent ? par->parent->children : d->root.children; | - |
| 318 | Q_ASSERT(children.count() > 0); | - |
| 319 | int row = (par - &(children.at(0))); | - |
| 320 | Q_ASSERT(row >= 0); | - |
| 321 | | - |
| 322 | return createIndex(row, 0, par); | - |
| 323 | } | - |
| 324 | | - |
| 325 | | - |
| 326 | | - |
| 327 | | - |
| 328 | | - |
| 329 | | - |
| 330 | int QDirModel::rowCount(const QModelIndex &parent) const | - |
| 331 | { | - |
| 332 | Q_D(const QDirModel); | - |
| 333 | if (parent.column() > 0) | - |
| 334 | return 0; | - |
| 335 | | - |
| 336 | if (!parent.isValid()) { | - |
| 337 | if (!d->root.populated) | - |
| 338 | d->populate(&d->root); | - |
| 339 | return d->root.children.count(); | - |
| 340 | } | - |
| 341 | if (parent.model() != this) | - |
| 342 | return 0; | - |
| 343 | QDirModelPrivate::QDirNode *p = d->node(parent); | - |
| 344 | if (p->info.isDir() && !p->populated) | - |
| 345 | d->populate(p); | - |
| 346 | return p->children.count(); | - |
| 347 | } | - |
| 348 | | - |
| 349 | | - |
| 350 | | - |
| 351 | | - |
| 352 | | - |
| 353 | | - |
| 354 | int QDirModel::columnCount(const QModelIndex &parent) const | - |
| 355 | { | - |
| 356 | if (parent.column() > 0) | - |
| 357 | return 0; | - |
| 358 | return 4; | - |
| 359 | } | - |
| 360 | | - |
| 361 | | - |
| 362 | | - |
| 363 | | - |
| 364 | QVariant QDirModel::data(const QModelIndex &index, int role) const | - |
| 365 | { | - |
| 366 | Q_D(const QDirModel); | - |
| 367 | if (!d->indexValid(index)) | - |
| 368 | return QVariant(); | - |
| 369 | | - |
| 370 | if (role == Qt::DisplayRole || role == Qt::EditRole) { | - |
| 371 | switch (index.column()) { | - |
| 372 | case 0: return d->name(index); | - |
| 373 | case 1: return d->size(index); | - |
| 374 | case 2: return d->type(index); | - |
| 375 | case 3: return d->time(index); | - |
| 376 | default: | - |
| 377 | qWarning("data: invalid display value column %d", index.column()); | - |
| 378 | return QVariant(); | - |
| 379 | } | - |
| 380 | } | - |
| 381 | | - |
| 382 | if (index.column() == 0) { | - |
| 383 | if (role == FileIconRole) | - |
| 384 | return fileIcon(index); | - |
| 385 | if (role == FilePathRole) | - |
| 386 | return filePath(index); | - |
| 387 | if (role == FileNameRole) | - |
| 388 | return fileName(index); | - |
| 389 | } | - |
| 390 | | - |
| 391 | if (index.column() == 1 && Qt::TextAlignmentRole == role) { | - |
| 392 | return Qt::AlignRight; | - |
| 393 | } | - |
| 394 | return QVariant(); | - |
| 395 | } | - |
| 396 | | - |
| 397 | | - |
| 398 | | - |
| 399 | | - |
| 400 | | - |
| 401 | | - |
| 402 | | - |
| 403 | | - |
| 404 | | - |
| 405 | bool QDirModel::setData(const QModelIndex &index, const QVariant &value, int role) | - |
| 406 | { | - |
| 407 | Q_D(QDirModel); | - |
| 408 | if (!d->indexValid(index) || index.column() != 0 | - |
| 409 | || (flags(index) & Qt::ItemIsEditable) == 0 || role != Qt::EditRole) | - |
| 410 | return false; | - |
| 411 | | - |
| 412 | QDirModelPrivate::QDirNode *node = d->node(index); | - |
| 413 | QDir dir = node->info.dir(); | - |
| 414 | QString name = value.toString(); | - |
| 415 | if (dir.rename(node->info.fileName(), name)) { | - |
| 416 | node->info = QFileInfo(dir, name); | - |
| 417 | QModelIndex sibling = index.sibling(index.row(), 3); | - |
| 418 | emit dataChanged(index, sibling); | - |
| 419 | | - |
| 420 | d->toBeRefreshed = index.parent(); | - |
| 421 | QMetaObject::invokeMethod(this, "_q_refresh", Qt::QueuedConnection); | - |
| 422 | | - |
| 423 | return true; | - |
| 424 | } | - |
| 425 | | - |
| 426 | return false; | - |
| 427 | } | - |
| 428 | | - |
| 429 | | - |
| 430 | | - |
| 431 | | - |
| 432 | | - |
| 433 | | - |
| 434 | QVariant QDirModel::headerData(int section, Qt::Orientation orientation, int role) const | - |
| 435 | { | - |
| 436 | if (orientation == Qt::Horizontal) { | - |
| 437 | if (role != Qt::DisplayRole) | - |
| 438 | return QVariant(); | - |
| 439 | switch (section) { | - |
| 440 | case 0: return tr("Name"); | - |
| 441 | case 1: return tr("Size"); | - |
| 442 | case 2: return | - |
| 443 | #ifdef Q_OS_MAC | - |
| 444 | tr("Kind", "Match OS X Finder"); | - |
| 445 | #else | - |
| 446 | tr("Type", "All other platforms"); | - |
| 447 | #endif | - |
| 448 | | - |
| 449 | | - |
| 450 | | - |
| 451 | | - |
| 452 | case 3: return tr("Date Modified"); | - |
| 453 | default: return QVariant(); | - |
| 454 | } | - |
| 455 | } | - |
| 456 | return QAbstractItemModel::headerData(section, orientation, role); | - |
| 457 | } | - |
| 458 | | - |
| 459 | | - |
| 460 | | - |
| 461 | | - |
| 462 | | - |
| 463 | | - |
| 464 | bool QDirModel::hasChildren(const QModelIndex &parent) const | - |
| 465 | { | - |
| 466 | Q_D(const QDirModel); | - |
| 467 | if (parent.column() > 0) | - |
| 468 | return false; | - |
| 469 | | - |
| 470 | if (!parent.isValid()) | - |
| 471 | return true; | - |
| 472 | QDirModelPrivate::QDirNode *p = d->node(parent); | - |
| 473 | Q_ASSERT(p); | - |
| 474 | | - |
| 475 | if (d->lazyChildCount) | - |
| 476 | return p->info.isDir(); | - |
| 477 | return p->info.isDir() && rowCount(parent) > 0; | - |
| 478 | } | - |
| 479 | | - |
| 480 | | - |
| 481 | | - |
| 482 | | - |
| 483 | | - |
| 484 | | - |
| 485 | Qt::ItemFlags QDirModel::flags(const QModelIndex &index) const | - |
| 486 | { | - |
| 487 | Q_D(const QDirModel); | - |
| 488 | Qt::ItemFlags flags = QAbstractItemModel::flags(index); | - |
| 489 | if (!d->indexValid(index)) | - |
| 490 | return flags; | - |
| 491 | flags |= Qt::ItemIsDragEnabled; | - |
| 492 | if (d->readOnly) | - |
| 493 | return flags; | - |
| 494 | QDirModelPrivate::QDirNode *node = d->node(index); | - |
| 495 | if ((index.column() == 0) && node->info.isWritable()) { | - |
| 496 | flags |= Qt::ItemIsEditable; | - |
| 497 | if (fileInfo(index).isDir()) | - |
| 498 | flags |= Qt::ItemIsDropEnabled; | - |
| 499 | } | - |
| 500 | return flags; | - |
| 501 | } | - |
| 502 | | - |
| 503 | | - |
| 504 | | - |
| 505 | | - |
| 506 | | - |
| 507 | | - |
| 508 | void QDirModel::sort(int column, Qt::SortOrder order) | - |
| 509 | { | - |
| 510 | QDir::SortFlags sort = QDir::DirsFirst | QDir::IgnoreCase; | - |
| 511 | if (order == Qt::DescendingOrder) | - |
| 512 | sort |= QDir::Reversed; | - |
| 513 | | - |
| 514 | switch (column) { | - |
| 515 | case 0: | - |
| 516 | sort |= QDir::Name; | - |
| 517 | break; | - |
| 518 | case 1: | - |
| 519 | sort |= QDir::Size; | - |
| 520 | break; | - |
| 521 | case 2: | - |
| 522 | sort |= QDir::Type; | - |
| 523 | break; | - |
| 524 | case 3: | - |
| 525 | sort |= QDir::Time; | - |
| 526 | break; | - |
| 527 | default: | - |
| 528 | break; | - |
| 529 | } | - |
| 530 | | - |
| 531 | setSorting(sort); | - |
| 532 | } | - |
| 533 | | - |
| 534 | | - |
| 535 | | - |
| 536 | | - |
| 537 | | - |
| 538 | | - |
| 539 | QStringList QDirModel::mimeTypes() const | - |
| 540 | { | - |
| 541 | return QStringList(QLatin1String("text/uri-list")); | - |
| 542 | } | - |
| 543 | | - |
| 544 | | - |
| 545 | | - |
| 546 | | - |
| 547 | | - |
| 548 | | - |
| 549 | | - |
| 550 | | - |
| 551 | | - |
| 552 | | - |
| 553 | QMimeData *QDirModel::mimeData(const QModelIndexList &indexes) const | - |
| 554 | { | - |
| 555 | QList<QUrl> urls; | - |
| 556 | QList<QModelIndex>::const_iterator it = indexes.begin(); | - |
| 557 | for (; it != indexes.end(); ++it) | - |
| 558 | if ((*it).column() == 0) | - |
| 559 | urls << QUrl::fromLocalFile(filePath(*it)); | - |
| 560 | QMimeData *data = new QMimeData(); | - |
| 561 | data->setUrls(urls); | - |
| 562 | return data; | - |
| 563 | } | - |
| 564 | | - |
| 565 | | - |
| 566 | | - |
| 567 | | - |
| 568 | | - |
| 569 | | - |
| 570 | | - |
| 571 | | - |
| 572 | | - |
| 573 | | - |
| 574 | | - |
| 575 | bool QDirModel::dropMimeData(const QMimeData *data, Qt::DropAction action, | - |
| 576 | int , int , const QModelIndex &parent) | - |
| 577 | { | - |
| 578 | Q_D(QDirModel); | - |
| 579 | if (!d->indexValid(parent) || isReadOnly()) | - |
| 580 | return false; | - |
| 581 | | - |
| 582 | bool success = true; | - |
| 583 | QString to = filePath(parent) + QDir::separator(); | - |
| 584 | QModelIndex _parent = parent; | - |
| 585 | | - |
| 586 | QList<QUrl> urls = data->urls(); | - |
| 587 | QList<QUrl>::const_iterator it = urls.constBegin(); | - |
| 588 | | - |
| 589 | switch (action) { | - |
| 590 | case Qt::CopyAction: | - |
| 591 | for (; it != urls.constEnd(); ++it) { | - |
| 592 | QString path = (*it).toLocalFile(); | - |
| 593 | success = QFile::copy(path, to + QFileInfo(path).fileName()) && success; | - |
| 594 | } | - |
| 595 | break; | - |
| 596 | case Qt::LinkAction: | - |
| 597 | for (; it != urls.constEnd(); ++it) { | - |
| 598 | QString path = (*it).toLocalFile(); | - |
| 599 | success = QFile::link(path, to + QFileInfo(path).fileName()) && success; | - |
| 600 | } | - |
| 601 | break; | - |
| 602 | case Qt::MoveAction: | - |
| 603 | for (; it != urls.constEnd(); ++it) { | - |
| 604 | QString path = (*it).toLocalFile(); | - |
| 605 | if (QFile::copy(path, to + QFileInfo(path).fileName()) | - |
| 606 | && QFile::remove(path)) { | - |
| 607 | QModelIndex idx=index(QFileInfo(path).path()); | - |
| 608 | if (idx.isValid()) { | - |
| 609 | refresh(idx); | - |
| 610 | | - |
| 611 | _parent = index(to); | - |
| 612 | } | - |
| 613 | } else { | - |
| 614 | success = false; | - |
| 615 | } | - |
| 616 | } | - |
| 617 | break; | - |
| 618 | default: | - |
| 619 | return false; | - |
| 620 | } | - |
| 621 | | - |
| 622 | if (success) | - |
| 623 | refresh(_parent); | - |
| 624 | | - |
| 625 | return success; | - |
| 626 | } | - |
| 627 | | - |
| 628 | | - |
| 629 | | - |
| 630 | | - |
| 631 | | - |
| 632 | | - |
| 633 | | - |
| 634 | Qt::DropActions QDirModel::supportedDropActions() const | - |
| 635 | { | - |
| 636 | return Qt::CopyAction | Qt::MoveAction; | - |
| 637 | } | - |
| 638 | | - |
| 639 | | - |
| 640 | | - |
| 641 | | - |
| 642 | | - |
| 643 | | - |
| 644 | void QDirModel::setIconProvider(QFileIconProvider *provider) | - |
| 645 | { | - |
| 646 | Q_D(QDirModel); | - |
| 647 | d->iconProvider = provider; | - |
| 648 | } | - |
| 649 | | - |
| 650 | | - |
| 651 | | - |
| 652 | | - |
| 653 | | - |
| 654 | QFileIconProvider *QDirModel::iconProvider() const | - |
| 655 | { | - |
| 656 | Q_D(const QDirModel); | - |
| 657 | return d->iconProvider; | - |
| 658 | } | - |
| 659 | | - |
| 660 | | - |
| 661 | | - |
| 662 | | - |
| 663 | | - |
| 664 | void QDirModel::setNameFilters(const QStringList &filters) | - |
| 665 | { | - |
| 666 | Q_D(QDirModel); | - |
| 667 | d->nameFilters = filters; | - |
| 668 | emit layoutAboutToBeChanged(); | - |
| 669 | if (d->shouldStat) | - |
| 670 | refresh(QModelIndex()); | - |
| 671 | else | - |
| 672 | d->invalidate(); | - |
| 673 | emit layoutChanged(); | - |
| 674 | } | - |
| 675 | | - |
| 676 | | - |
| 677 | | - |
| 678 | | - |
| 679 | | - |
| 680 | QStringList QDirModel::nameFilters() const | - |
| 681 | { | - |
| 682 | Q_D(const QDirModel); | - |
| 683 | return d->nameFilters; | - |
| 684 | } | - |
| 685 | | - |
| 686 | | - |
| 687 | | - |
| 688 | | - |
| 689 | | - |
| 690 | | - |
| 691 | | - |
| 692 | | - |
| 693 | | - |
| 694 | | - |
| 695 | void QDirModel::setFilter(QDir::Filters filters) | - |
| 696 | { | - |
| 697 | Q_D(QDirModel); | - |
| 698 | d->filters = filters; | - |
| 699 | emit layoutAboutToBeChanged(); | - |
| 700 | if (d->shouldStat) | - |
| 701 | refresh(QModelIndex()); | - |
| 702 | else | - |
| 703 | d->invalidate(); | - |
| 704 | emit layoutChanged(); | - |
| 705 | } | - |
| 706 | | - |
| 707 | | - |
| 708 | | - |
| 709 | | - |
| 710 | | - |
| 711 | | - |
| 712 | | - |
| 713 | QDir::Filters QDirModel::filter() const | - |
| 714 | { | - |
| 715 | Q_D(const QDirModel); | - |
| 716 | return d->filters; | - |
| 717 | } | - |
| 718 | | - |
| 719 | | - |
| 720 | | - |
| 721 | | - |
| 722 | | - |
| 723 | | - |
| 724 | | - |
| 725 | void QDirModel::setSorting(QDir::SortFlags sort) | - |
| 726 | { | - |
| 727 | Q_D(QDirModel); | - |
| 728 | d->sort = sort; | - |
| 729 | emit layoutAboutToBeChanged(); | - |
| 730 | if (d->shouldStat) | - |
| 731 | refresh(QModelIndex()); | - |
| 732 | else | - |
| 733 | d->invalidate(); | - |
| 734 | emit layoutChanged(); | - |
| 735 | } | - |
| 736 | | - |
| 737 | | - |
| 738 | | - |
| 739 | | - |
| 740 | | - |
| 741 | | - |
| 742 | | - |
| 743 | QDir::SortFlags QDirModel::sorting() const | - |
| 744 | { | - |
| 745 | Q_D(const QDirModel); | - |
| 746 | return d->sort; | - |
| 747 | } | - |
| 748 | | - |
| 749 | | - |
| 750 | | - |
| 751 | | - |
| 752 | | - |
| 753 | | - |
| 754 | | - |
| 755 | | - |
| 756 | void QDirModel::setResolveSymlinks(bool enable) | - |
| 757 | { | - |
| 758 | Q_D(QDirModel); | - |
| 759 | d->resolveSymlinks = enable; | - |
| 760 | } | - |
| 761 | | - |
| 762 | bool QDirModel::resolveSymlinks() const | - |
| 763 | { | - |
| 764 | Q_D(const QDirModel); | - |
| 765 | return d->resolveSymlinks; | - |
| 766 | } | - |
| 767 | | - |
| 768 | | - |
| 769 | | - |
| 770 | | - |
| 771 | | - |
| 772 | | - |
| 773 | | - |
| 774 | | - |
| 775 | | - |
| 776 | | - |
| 777 | | - |
| 778 | void QDirModel::setReadOnly(bool enable) | - |
| 779 | { | - |
| 780 | Q_D(QDirModel); | - |
| 781 | d->readOnly = enable; | - |
| 782 | } | - |
| 783 | | - |
| 784 | bool QDirModel::isReadOnly() const | - |
| 785 | { | - |
| 786 | Q_D(const QDirModel); | - |
| 787 | return d->readOnly; | - |
| 788 | } | - |
| 789 | | - |
| 790 | | - |
| 791 | | - |
| 792 | | - |
| 793 | | - |
| 794 | | - |
| 795 | | - |
| 796 | | - |
| 797 | | - |
| 798 | | - |
| 799 | | - |
| 800 | | - |
| 801 | | - |
| 802 | | - |
| 803 | void QDirModel::setLazyChildCount(bool enable) | - |
| 804 | { | - |
| 805 | Q_D(QDirModel); | - |
| 806 | d->lazyChildCount = enable; | - |
| 807 | } | - |
| 808 | | - |
| 809 | bool QDirModel::lazyChildCount() const | - |
| 810 | { | - |
| 811 | Q_D(const QDirModel); | - |
| 812 | return d->lazyChildCount; | - |
| 813 | } | - |
| 814 | | - |
| 815 | | - |
| 816 | | - |
| 817 | | - |
| 818 | | - |
| 819 | | - |
| 820 | | - |
| 821 | | - |
| 822 | void QDirModel::refresh(const QModelIndex &parent) | - |
| 823 | { | - |
| 824 | Q_D(QDirModel); | - |
| 825 | | - |
| 826 | QDirModelPrivate::QDirNode *n = d->indexValid(parent) ? d->node(parent) : &(d->root); | - |
| 827 | | - |
| 828 | int rows = n->children.count(); | - |
| 829 | if (rows == 0) { | - |
| 830 | emit layoutAboutToBeChanged(); | - |
| 831 | n->stat = true; | - |
| 832 | n->populated = false; | - |
| 833 | emit layoutChanged(); | - |
| 834 | return; | - |
| 835 | } | - |
| 836 | | - |
| 837 | emit layoutAboutToBeChanged(); | - |
| 838 | d->savePersistentIndexes(); | - |
| 839 | d->rowsAboutToBeRemoved(parent, 0, rows - 1); | - |
| 840 | n->stat = true; | - |
| 841 | d->clear(n); | - |
| 842 | d->rowsRemoved(parent, 0, rows - 1); | - |
| 843 | d->restorePersistentIndexes(); | - |
| 844 | emit layoutChanged(); | - |
| 845 | } | - |
| 846 | | - |
| 847 | | - |
| 848 | | - |
| 849 | | - |
| 850 | | - |
| 851 | | - |
| 852 | | - |
| 853 | QModelIndex QDirModel::index(const QString &path, int column) const | - |
| 854 | { | - |
| 855 | Q_D(const QDirModel); | - |
| 856 | | - |
| 857 | if (path.isEmpty() || path == QCoreApplication::translate("QFileDialog", "My Computer"))| TRUE | never evaluated | | FALSE | never evaluated |
| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 858 | return QModelIndex(); never executed: return QModelIndex(); | 0 |
| 859 | | - |
| 860 | QString absolutePath = QDir(path).absolutePath(); | - |
| 861 | #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) | - |
| 862 | absolutePath = absolutePath.toLower(); | - |
| 863 | | - |
| 864 | if (absolutePath.endsWith(QLatin1Char('.'))) { | - |
| 865 | int i; | - |
| 866 | for (i = absolutePath.count() - 1; i >= 0; --i) { | - |
| 867 | if (absolutePath.at(i) != QLatin1Char('.')) | - |
| 868 | break; | - |
| 869 | } | - |
| 870 | absolutePath = absolutePath.left(i+1); | - |
| 871 | } | - |
| 872 | #endif | - |
| 873 | | - |
| 874 | QStringList pathElements = absolutePath.split(QLatin1Char('/'), QString::SkipEmptyParts); | - |
| 875 | if ((pathElements.isEmpty() || !QFileInfo::exists(path))| TRUE | never evaluated | | FALSE | never evaluated |
| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 876 | #if !defined(Q_OS_WIN) || defined(Q_OS_WINCE) | - |
| 877 | && path != QLatin1String("/")| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 878 | #endif | - |
| 879 | ) | - |
| 880 | return QModelIndex(); never executed: return QModelIndex(); | 0 |
| 881 | | - |
| 882 | QModelIndex idx; | - |
| 883 | if (!d->root.populated) | TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 884 | d->populate(&d->root); never executed: d->populate(&d->root); | 0 |
| 885 | | - |
| 886 | #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) | - |
| 887 | if (absolutePath.startsWith(QLatin1String("//"))) { | - |
| 888 | QString host = pathElements.firstconstFirst(); | - |
| 889 | int r = 0; | - |
| 890 | for (; r < d->root.children.count(); ++r) | - |
| 891 | if (d->root.children.at(r).info.fileName() == host) | - |
| 892 | break; | - |
| 893 | bool childAppended = false; | - |
| 894 | if (r >= d->root.children.count() && d->allowAppendChild) { | - |
| 895 | d->appendChild(&d->root, QLatin1String("//") + host); | - |
| 896 | childAppended = true; | - |
| 897 | } | - |
| 898 | idx = index(r, 0, QModelIndex()); | - |
| 899 | pathElements.pop_front(); | - |
| 900 | if (childAppended) | - |
| 901 | emit const_cast<QDirModel*>(this)->layoutChanged(); | - |
| 902 | } else | - |
| 903 | #endif | - |
| 904 | #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) | - |
| 905 | if (pathElements.at(0).endsWith(QLatin1Char(':'))) { | - |
| 906 | pathElements[0] += QLatin1Char('/'); | - |
| 907 | } | - |
| 908 | #else | - |
| 909 | | - |
| 910 | pathElements.prepend(QLatin1String("/")); | - |
| 911 | #endif | - |
| 912 | | - |
| 913 | for (int i = 0; i < pathElements.count(); ++i) {| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 914 | Q_ASSERT(!pathElements.at(i).isEmpty()); | - |
| 915 | QString element = pathElements.at(i); | - |
| 916 | QDirModelPrivate::QDirNode *parent = (idx.isValid() ? d->node(idx) : &d->root);| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 917 | | - |
| 918 | Q_ASSERT(parent); | - |
| 919 | if (!parent->populated)| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 920 | d->populate(parent); never executed: d->populate(parent); | 0 |
| 921 | | - |
| 922 | | - |
| 923 | int row = -1; | - |
| 924 | for (int j = parent->children.count() - 1; j >= 0; --j) {| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 925 | const QFileInfo& fi = parent->children.at(j).info; | - |
| 926 | QString childFileName; | - |
| 927 | childFileName = idx.isValid() ? fi.fileName() : fi.absoluteFilePath();| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 928 | #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) | - |
| 929 | childFileName = childFileName.toLower(); | - |
| 930 | #endif | - |
| 931 | if (childFileName == element) {| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 932 | if (i == pathElements.count() - 1)| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 933 | parent->children[j].stat = true; never executed: parent->children[j].stat = true; | 0 |
| 934 | row = j; | - |
| 935 | break; never executed: break; | 0 |
| 936 | } | - |
| 937 | } never executed: end of block | 0 |
| 938 | | - |
| 939 | | - |
| 940 | if (row == -1) {| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 941 | #if defined(Q_OS_WINCE) | - |
| 942 | QString newPath; | - |
| 943 | if (parent->info.isRoot()) | - |
| 944 | newPath = parent->info.absoluteFilePath() + element; | - |
| 945 | else | - |
| 946 | newPath = parent->info.absoluteFilePath() + QLatin1Char('/') + element; | - |
| 947 | #else | - |
| 948 | QString newPath = parent->info.absoluteFilePath() + QLatin1Char('/') + element; | - |
| 949 | #endif | - |
| 950 | if (!d->allowAppendChild || !QFileInfo(newPath).isDir())| TRUE | never evaluated | | FALSE | never evaluated |
| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 951 | return QModelIndex(); never executed: return QModelIndex(); | 0 |
| 952 | d->appendChild(parent, newPath); | - |
| 953 | row = parent->children.count() - 1; | - |
| 954 | if (i == pathElements.count() - 1) | TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 955 | parent->children[row].stat = true; never executed: parent->children[row].stat = true; | 0 |
| 956 | emit const_cast<QDirModel*>(this)->layoutChanged(); | - |
| 957 | } never executed: end of block | 0 |
| 958 | | - |
| 959 | Q_ASSERT(row >= 0); | - |
| 960 | idx = createIndex(row, 0, static_cast<void*>(&parent->children[row])); | - |
| 961 | Q_ASSERT(idx.isValid()); | - |
| 962 | } never executed: end of block | 0 |
| 963 | | - |
| 964 | if (column != 0)| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 965 | return idx.sibling(idx.row(), column); never executed: return idx.sibling(idx.row(), column); | 0 |
| 966 | return idx; never executed: return idx; | 0 |
| 967 | } | - |
| 968 | | - |
| 969 | | - |
| 970 | | - |
| 971 | | - |
| 972 | | - |
| 973 | | - |
| 974 | bool QDirModel::isDir(const QModelIndex &index) const | - |
| 975 | { | - |
| 976 | Q_D(const QDirModel); | - |
| 977 | Q_ASSERT(d->indexValid(index)); | - |
| 978 | QDirModelPrivate::QDirNode *node = d->node(index); | - |
| 979 | return node->info.isDir(); | - |
| 980 | } | - |
| 981 | | - |
| 982 | | - |
| 983 | | - |
| 984 | | - |
| 985 | | - |
| 986 | QModelIndex QDirModel::mkdir(const QModelIndex &parent, const QString &name) | - |
| 987 | { | - |
| 988 | Q_D(QDirModel); | - |
| 989 | if (!d->indexValid(parent) || isReadOnly()) | - |
| 990 | return QModelIndex(); | - |
| 991 | | - |
| 992 | QDirModelPrivate::QDirNode *p = d->node(parent); | - |
| 993 | QString path = p->info.absoluteFilePath(); | - |
| 994 | | - |
| 995 | | - |
| 996 | | - |
| 997 | QDir newDir(name); | - |
| 998 | QDir dir(path); | - |
| 999 | if (newDir.isRelative()) | - |
| 1000 | newDir = QDir(path + QLatin1Char('/') + name); | - |
| 1001 | QString childName = newDir.dirName(); | - |
| 1002 | newDir.cdUp(); | - |
| 1003 | | - |
| 1004 | if (newDir.absolutePath() != dir.absolutePath() || !dir.mkdir(name)) | - |
| 1005 | return QModelIndex(); | - |
| 1006 | | - |
| 1007 | refresh(parent); | - |
| 1008 | | - |
| 1009 | QStringList entryList = d->entryList(path); | - |
| 1010 | int r = entryList.indexOf(childName); | - |
| 1011 | QModelIndex i = index(r, 0, parent); | - |
| 1012 | | - |
| 1013 | return i; | - |
| 1014 | } | - |
| 1015 | | - |
| 1016 | | - |
| 1017 | | - |
| 1018 | | - |
| 1019 | | - |
| 1020 | | - |
| 1021 | | - |
| 1022 | | - |
| 1023 | | - |
| 1024 | | - |
| 1025 | | - |
| 1026 | | - |
| 1027 | | - |
| 1028 | bool QDirModel::rmdir(const QModelIndex &index) | - |
| 1029 | { | - |
| 1030 | Q_D(QDirModel); | - |
| 1031 | if (!d->indexValid(index) || isReadOnly())| TRUE | never evaluated | | FALSE | never evaluated |
| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 1032 | return false; never executed: return false; | 0 |
| 1033 | | - |
| 1034 | QDirModelPrivate::QDirNode *n = d_func()->node(index); | - |
| 1035 | if (Q_UNLIKELY(!n->info.isDir())())) {| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 1036 | qWarning("rmdir: the node is not a directory"); | - |
| 1037 | return false; never executed: return false; | 0 |
| 1038 | } | - |
| 1039 | | - |
| 1040 | QModelIndex par = parent(index); | - |
| 1041 | QDirModelPrivate::QDirNode *p = d_func()->node(par); | - |
| 1042 | QDir dir = p->info.dir(); | - |
| 1043 | QString path = n->info.absoluteFilePath(); | - |
| 1044 | if (!dir.rmdir(path))| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 1045 | return false; never executed: return false; | 0 |
| 1046 | | - |
| 1047 | refresh(par); | - |
| 1048 | | - |
| 1049 | return true; never executed: return true; | 0 |
| 1050 | } | - |
| 1051 | | - |
| 1052 | | - |
| 1053 | | - |
| 1054 | | - |
| 1055 | | - |
| 1056 | | - |
| 1057 | | - |
| 1058 | | - |
| 1059 | | - |
| 1060 | | - |
| 1061 | | - |
| 1062 | | - |
| 1063 | bool QDirModel::remove(const QModelIndex &index) | - |
| 1064 | { | - |
| 1065 | Q_D(QDirModel); | - |
| 1066 | if (!d->indexValid(index) || isReadOnly()) | - |
| 1067 | return false; | - |
| 1068 | | - |
| 1069 | QDirModelPrivate::QDirNode *n = d_func()->node(index); | - |
| 1070 | if (n->info.isDir()) | - |
| 1071 | return false; | - |
| 1072 | | - |
| 1073 | QModelIndex par = parent(index); | - |
| 1074 | QDirModelPrivate::QDirNode *p = d_func()->node(par); | - |
| 1075 | QDir dir = p->info.dir(); | - |
| 1076 | QString path = n->info.absoluteFilePath(); | - |
| 1077 | if (!dir.remove(path)) | - |
| 1078 | return false; | - |
| 1079 | | - |
| 1080 | refresh(par); | - |
| 1081 | | - |
| 1082 | return true; | - |
| 1083 | } | - |
| 1084 | | - |
| 1085 | | - |
| 1086 | | - |
| 1087 | | - |
| 1088 | | - |
| 1089 | | - |
| 1090 | | - |
| 1091 | QString QDirModel::filePath(const QModelIndex &index) const | - |
| 1092 | { | - |
| 1093 | Q_D(const QDirModel); | - |
| 1094 | if (d->indexValid(index)) { | - |
| 1095 | QFileInfo fi = fileInfo(index); | - |
| 1096 | if (d->resolveSymlinks && fi.isSymLink()) | - |
| 1097 | fi = d->resolvedInfo(fi); | - |
| 1098 | return QDir::cleanPath(fi.absoluteFilePath()); | - |
| 1099 | } | - |
| 1100 | return QString(); | - |
| 1101 | } | - |
| 1102 | | - |
| 1103 | | - |
| 1104 | | - |
| 1105 | | - |
| 1106 | | - |
| 1107 | | - |
| 1108 | | - |
| 1109 | QString QDirModel::fileName(const QModelIndex &index) const | - |
| 1110 | { | - |
| 1111 | Q_D(const QDirModel); | - |
| 1112 | if (!d->indexValid(index)) | - |
| 1113 | return QString(); | - |
| 1114 | QFileInfo info = fileInfo(index); | - |
| 1115 | if (info.isRoot()) | - |
| 1116 | return info.absoluteFilePath(); | - |
| 1117 | if (d->resolveSymlinks && info.isSymLink()) | - |
| 1118 | info = d->resolvedInfo(info); | - |
| 1119 | return info.fileName(); | - |
| 1120 | } | - |
| 1121 | | - |
| 1122 | | - |
| 1123 | | - |
| 1124 | | - |
| 1125 | | - |
| 1126 | | - |
| 1127 | QIcon QDirModel::fileIcon(const QModelIndex &index) const | - |
| 1128 | { | - |
| 1129 | Q_D(const QDirModel); | - |
| 1130 | if (!d->indexValid(index)) | - |
| 1131 | return d->iconProvider->icon(QFileIconProvider::Computer); | - |
| 1132 | QDirModelPrivate::QDirNode *node = d->node(index); | - |
| 1133 | if (node->icon.isNull()) | - |
| 1134 | node->icon = d->iconProvider->icon(node->info); | - |
| 1135 | return node->icon; | - |
| 1136 | } | - |
| 1137 | | - |
| 1138 | | - |
| 1139 | | - |
| 1140 | | - |
| 1141 | | - |
| 1142 | | - |
| 1143 | | - |
| 1144 | | - |
| 1145 | | - |
| 1146 | | - |
| 1147 | | - |
| 1148 | | - |
| 1149 | QFileInfo QDirModel::fileInfo(const QModelIndex &index) const | - |
| 1150 | { | - |
| 1151 | Q_D(const QDirModel); | - |
| 1152 | Q_ASSERT(d->indexValid(index)); | - |
| 1153 | | - |
| 1154 | QDirModelPrivate::QDirNode *node = d->node(index); | - |
| 1155 | return node->info; | - |
| 1156 | } | - |
| 1157 | | - |
| 1158 | | - |
| 1159 | | - |
| 1160 | | - |
| 1161 | | - |
| 1162 | void QDirModelPrivate::init() | - |
| 1163 | { | - |
| 1164 | filters = QDir::AllEntries | QDir::NoDotAndDotDot; | - |
| 1165 | sort = QDir::Name; | - |
| 1166 | nameFilters << QLatin1String("*"); | - |
| 1167 | root.parent = 0; | - |
| 1168 | root.info = QFileInfo(); | - |
| 1169 | clear(&root); | - |
| 1170 | roleNames.insertMulti(QDirModel::FileIconRole, QByteArrayLiteral("fileIcon")); | - |
| 1171 | roleNames.insert(QDirModel::FilePathRole, QByteArrayLiteral("filePath")); | - |
| 1172 | roleNames.insert(QDirModel::FileNameRole, QByteArrayLiteral("fileName")); | - |
| 1173 | } | - |
| 1174 | | - |
| 1175 | QDirModelPrivate::QDirNode *QDirModelPrivate::node(int row, QDirNode *parent) const | - |
| 1176 | { | - |
| 1177 | if (row < 0)| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 1178 | return 0; never executed: return 0; | 0 |
| 1179 | | - |
| 1180 | bool isDir = !parent || parent->info.isDir();| TRUE | never evaluated | | FALSE | never evaluated |
| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 1181 | QDirNode *p = (parent ? parent : &root);| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 1182 | if (isDir && !p->populated)| TRUE | never evaluated | | FALSE | never evaluated |
| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 1183 | populate(p); never executed: populate(p); | 0 |
| 1184 | | - |
| 1185 | if (Q_UNLIKELY(row >= p->children.count())())) {| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 1186 | qWarning("node: the row does not exist"); | - |
| 1187 | return 0; never executed: return 0; | 0 |
| 1188 | } | - |
| 1189 | | - |
| 1190 | return const_cast<QDirNode*>(&p->children.at(row)); never executed: return const_cast<QDirNode*>(&p->children.at(row)); | 0 |
| 1191 | } | - |
| 1192 | | - |
| 1193 | QVector<QDirModelPrivate::QDirNode> QDirModelPrivate::children(QDirNode *parent, bool stat) const | - |
| 1194 | { | - |
| 1195 | Q_ASSERT(parent); | - |
| 1196 | QFileInfoList infoList; | - |
| 1197 | if (parent == &root) { | - |
| 1198 | parent = 0; | - |
| 1199 | infoList = QDir::drives(); | - |
| 1200 | } else if (parent->info.isDir()) { | - |
| 1201 | | - |
| 1202 | if (parent->info.isSymLink() && resolveSymlinks) { | - |
| 1203 | QString link = parent->info.symLinkTarget(); | - |
| 1204 | if (link.size() > 1 && link.at(link.size() - 1) == QDir::separator()) | - |
| 1205 | link.chop(1); | - |
| 1206 | if (stat) | - |
| 1207 | infoList = entryInfoList(link); | - |
| 1208 | else | - |
| 1209 | infoList = QDir(link).entryInfoList(nameFilters, QDir::AllEntries | QDir::System); | - |
| 1210 | } else { | - |
| 1211 | if (stat) | - |
| 1212 | infoList = entryInfoList(parent->info.absoluteFilePath()); | - |
| 1213 | else | - |
| 1214 | infoList = QDir(parent->info.absoluteFilePath()).entryInfoList(nameFilters, QDir::AllEntries | QDir::System); | - |
| 1215 | } | - |
| 1216 | } | - |
| 1217 | | - |
| 1218 | QVector<QDirNode> nodes(infoList.count()); | - |
| 1219 | for (int i = 0; i < infoList.count(); ++i) { | - |
| 1220 | QDirNode &node = nodes[i]; | - |
| 1221 | node.parent = parent; | - |
| 1222 | node.info = infoList.at(i); | - |
| 1223 | node.populated = false; | - |
| 1224 | node.stat = shouldStat; | - |
| 1225 | } | - |
| 1226 | | - |
| 1227 | return nodes; | - |
| 1228 | } | - |
| 1229 | | - |
| 1230 | void QDirModelPrivate::_q_refresh() | - |
| 1231 | { | - |
| 1232 | Q_Q(QDirModel); | - |
| 1233 | q->refresh(toBeRefreshed); | - |
| 1234 | toBeRefreshed = QModelIndex(); | - |
| 1235 | } | - |
| 1236 | | - |
| 1237 | void QDirModelPrivate::savePersistentIndexes() | - |
| 1238 | { | - |
| 1239 | Q_Q(QDirModel); | - |
| 1240 | savedPersistent.clear(); | - |
| 1241 | savedPersistent.reserve(persistent.indexes.size()); | - |
| 1242 | foreach (QPersistentModelIndexData *data, persistent.indexes) { | - |
| 1243 | SavedPersistent saved;QModelIndex index = data->index; | - |
| 1244 | SavedPersistent saved .path= { | - |
| 1245 | q->filePath(index); | - |
| saved.column =), | |
| 1246 | index.column(); | - |
| saved.data =(), | |
| 1247 | data; | - |
| saved.index =, | |
| 1248 | index;, | - |
| 1249 | }; | - |
| 1250 | savedPersistent.appendpush_back(std::move(saved);)); | - |
| 1251 | } never executed: end of block | 0 |
| 1252 | } never executed: end of block | 0 |
| 1253 | | - |
| 1254 | void QDirModelPrivate::restorePersistentIndexes() | - |
| 1255 | { | - |
| 1256 | Q_Q(QDirModel); | - |
| 1257 | bool allow = allowAppendChild; | - |
| 1258 | allowAppendChild = false; | - |
| 1259 | for (int i = 0; i <const SavedPersistent &sp : qAsConst(savedPersistent.count(); ++i))) { | - |
| 1260 | QPersistentModelIndexData *data = savedPersistentsp.at(i).data; | - |
| QString path = savedPersistent.at(i).path; | |
| int column = savedPersistent.at(i).column; | |
| 1261 | QModelIndex idx = q->index(sp.path, sp.column); | - |
| 1262 | if (idx != data->index || data->model == 0) {| TRUE | never evaluated | | FALSE | never evaluated |
| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 1263 | | - |
| 1264 | persistent.indexes.remove(data->index); | - |
| 1265 | data->index = idx; | - |
| 1266 | data->model = q; | - |
| 1267 | if (idx.isValid())| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 1268 | persistent.indexes.insert(idx, data); never executed: persistent.indexes.insert(idx, data); | 0 |
| 1269 | } never executed: end of block | 0 |
| 1270 | } never executed: end of block | 0 |
| 1271 | savedPersistent.clear(); | - |
| 1272 | allowAppendChild = allow; | - |
| 1273 | } never executed: end of block | 0 |
| 1274 | | - |
| 1275 | QFileInfoList QDirModelPrivate::entryInfoList(const QString &path) const | - |
| 1276 | { | - |
| 1277 | const QDir dir(path); | - |
| 1278 | return dir.entryInfoList(nameFilters, filters, sort); | - |
| 1279 | } | - |
| 1280 | | - |
| 1281 | QStringList QDirModelPrivate::entryList(const QString &path) const | - |
| 1282 | { | - |
| 1283 | const QDir dir(path); | - |
| 1284 | return dir.entryList(nameFilters, filters, sort); | - |
| 1285 | } | - |
| 1286 | | - |
| 1287 | QString QDirModelPrivate::name(const QModelIndex &index) const | - |
| 1288 | { | - |
| 1289 | const QDirNode *n = node(index); | - |
| 1290 | const QFileInfo info = n->info; | - |
| 1291 | if (info.isRoot()) { | - |
| 1292 | QString name = info.absoluteFilePath(); | - |
| 1293 | #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) | - |
| 1294 | if (name.startsWith(QLatin1Char('/'))) | - |
| 1295 | return info.fileName(); | - |
| 1296 | if (name.endsWith(QLatin1Char('/'))) | - |
| 1297 | name.chop(1); | - |
| 1298 | #endif | - |
| 1299 | return name; | - |
| 1300 | } | - |
| 1301 | return info.fileName(); | - |
| 1302 | } | - |
| 1303 | | - |
| 1304 | QString QDirModelPrivate::size(const QModelIndex &index) const | - |
| 1305 | { | - |
| 1306 | const QDirNode *n = node(index); | - |
| 1307 | if (n->info.isDir()) { | - |
| 1308 | #ifdef Q_OS_MAC | - |
| 1309 | return QLatin1String("--"); | - |
| 1310 | #else | - |
| 1311 | return QLatin1String(""); | - |
| 1312 | #endif | - |
| 1313 | | - |
| 1314 | | - |
| 1315 | | - |
| 1316 | | - |
| 1317 | } | - |
| 1318 | | - |
| 1319 | | - |
| 1320 | | - |
| 1321 | const quint64 kb = 1024; | - |
| 1322 | const quint64 mb = 1024 * kb; | - |
| 1323 | const quint64 gb = 1024 * mb; | - |
| 1324 | const quint64 tb = 1024 * gb; | - |
| 1325 | quint64 bytes = n->info.size(); | - |
| 1326 | if (bytes >= tb) | - |
| 1327 | return QFileSystemModel::tr("%1 TB").arg(QLocale().toString(qreal(bytes) / tb, 'f', 3)); | - |
| 1328 | if (bytes >= gb) | - |
| 1329 | return QFileSystemModel::tr("%1 GB").arg(QLocale().toString(qreal(bytes) / gb, 'f', 2)); | - |
| 1330 | if (bytes >= mb) | - |
| 1331 | return QFileSystemModel::tr("%1 MB").arg(QLocale().toString(qreal(bytes) / mb, 'f', 1)); | - |
| 1332 | if (bytes >= kb) | - |
| 1333 | return QFileSystemModel::tr("%1 KB").arg(QLocale().toString(bytes / kb)); | - |
| 1334 | return QFileSystemModel::tr("%1 byte(s)").arg(QLocale().toString(bytes)); | - |
| 1335 | } | - |
| 1336 | | - |
| 1337 | QString QDirModelPrivate::type(const QModelIndex &index) const | - |
| 1338 | { | - |
| 1339 | return iconProvider->type(node(index)->info); | - |
| 1340 | } | - |
| 1341 | | - |
| 1342 | QString QDirModelPrivate::time(const QModelIndex &index) const | - |
| 1343 | { | - |
| 1344 | #ifndef QT_NO_DATESTRING | - |
| 1345 | return node(index)->info.lastModified().toString(Qt::LocalDate); | - |
| 1346 | #else | - |
| 1347 | Q_UNUSED(index); | - |
| 1348 | return QString(); | - |
| 1349 | #endif | - |
| 1350 | } | - |
| 1351 | | - |
| 1352 | void QDirModelPrivate::appendChild(QDirModelPrivate::QDirNode *parent, const QString &path) const | - |
| 1353 | { | - |
| 1354 | QDirModelPrivate::QDirNode node; | - |
| 1355 | node.populated = false; | - |
| 1356 | node.stat = shouldStat; | - |
| 1357 | node.parent = (parent == &root ? 0 : parent); | - |
| 1358 | node.info = QFileInfo(path); | - |
| 1359 | node.info.setCaching(true); | - |
| 1360 | | - |
| 1361 | | - |
| 1362 | | - |
| 1363 | QDirModelPrivate *that = const_cast<QDirModelPrivate *>(this); | - |
| 1364 | that->savePersistentIndexes(); | - |
| 1365 | parent->children.append(node); | - |
| 1366 | for (int i = 0; i < parent->children.count(); ++i) { | - |
| 1367 | QDirNode *childNode = &parent->children[i]; | - |
| 1368 | for (int j = 0; j < childNode->children.count(); ++j) | - |
| 1369 | childNode->children[j].parent = childNode; | - |
| 1370 | } | - |
| 1371 | that->restorePersistentIndexes(); | - |
| 1372 | } | - |
| 1373 | | - |
| 1374 | QFileInfo QDirModelPrivate::resolvedInfo(QFileInfo info) | - |
| 1375 | { | - |
| 1376 | #ifdef Q_OS_WIN | - |
| 1377 | | - |
| 1378 | return QFileInfo(info.symLinkTarget()); | - |
| 1379 | #else | - |
| 1380 | QStringList paths; | - |
| 1381 | do { | - |
| 1382 | QFileInfo link(info.symLinkTarget()); | - |
| 1383 | if (link.isRelative()) | - |
| 1384 | info.setFile(info.absolutePath(), link.filePath()); | - |
| 1385 | else | - |
| 1386 | info = link; | - |
| 1387 | if (paths.contains(info.absoluteFilePath())) | - |
| 1388 | return QFileInfo(); | - |
| 1389 | paths.append(info.absoluteFilePath()); | - |
| 1390 | } while (info.isSymLink()); | - |
| 1391 | return info; | - |
| 1392 | #endif | - |
| 1393 | } | - |
| 1394 | | - |
| 1395 | QT_END_NAMESPACE | - |
| 1396 | | - |
| 1397 | #include "moc_qdirmodel.cpp" | - |
| 1398 | | - |
| 1399 | #endif // QT_NO_DIRMODEL | - |
| | |