Changeset View
Changeset View
Standalone View
Standalone View
containments/desktop/plugins/folder/positioner.cpp
Show All 23 Lines | |||||
24 | #include <QTimer> | 24 | #include <QTimer> | ||
25 | 25 | | |||
26 | #include <cstdlib> | 26 | #include <cstdlib> | ||
27 | 27 | | |||
28 | Positioner::Positioner(QObject *parent): QAbstractItemModel(parent) | 28 | Positioner::Positioner(QObject *parent): QAbstractItemModel(parent) | ||
29 | , m_enabled(false) | 29 | , m_enabled(false) | ||
30 | , m_folderModel(nullptr) | 30 | , m_folderModel(nullptr) | ||
31 | , m_perStripe(0) | 31 | , m_perStripe(0) | ||
32 | , m_lastRow(-1) | | |||
33 | , m_ignoreNextTransaction(false) | 32 | , m_ignoreNextTransaction(false) | ||
34 | , m_pendingPositions(false) | 33 | , m_pendingPositions(false) | ||
35 | , m_updatePositionsTimer(new QTimer(this)) | 34 | , m_updatePositionsTimer(new QTimer(this)) | ||
36 | { | 35 | { | ||
37 | m_updatePositionsTimer->setSingleShot(true); | 36 | m_updatePositionsTimer->setSingleShot(true); | ||
38 | m_updatePositionsTimer->setInterval(0); | 37 | m_updatePositionsTimer->setInterval(0); | ||
39 | connect(m_updatePositionsTimer, &QTimer::timeout, this, &Positioner::updatePositions); | 38 | connect(m_updatePositionsTimer, &QTimer::timeout, this, &Positioner::updatePositions); | ||
40 | } | 39 | } | ||
▲ Show 20 Lines • Show All 333 Lines • ▼ Show 20 Line(s) | 372 | } else { | |||
374 | sourceRows.append(-1); | 373 | sourceRows.append(-1); | ||
375 | } | 374 | } | ||
376 | } | 375 | } | ||
377 | 376 | | |||
378 | (isFrom ? fromIndices : toIndices).append(v); | 377 | (isFrom ? fromIndices : toIndices).append(v); | ||
379 | } | 378 | } | ||
380 | 379 | | |||
381 | const int oldCount = rowCount(); | 380 | const int oldCount = rowCount(); | ||
381 | int newLast = 0; | ||||
382 | 382 | | |||
383 | for (int i = 0; i < fromIndices.count(); ++i) | 383 | for (int i = 0; i < fromIndices.count(); ++i) | ||
384 | { | 384 | { | ||
385 | const int from = fromIndices[i]; | 385 | const int from = fromIndices[i]; | ||
386 | int to = toIndices[i]; | 386 | int to = toIndices[i]; | ||
387 | const int sourceRow = sourceRows[i]; | 387 | const int sourceRow = sourceRows[i]; | ||
388 | 388 | | |||
389 | if (sourceRow == -1 || from == to) { | 389 | if (sourceRow == -1 || from == to) { | ||
Show All 12 Lines | 401 | if (!fromIndices.contains(to) && !isBlank(to)) { | |||
402 | // find the next blank space | 402 | // find the next blank space | ||
403 | while (!isBlank(to) && from != to) { | 403 | while (!isBlank(to) && from != to) { | ||
404 | to++; | 404 | to++; | ||
405 | } | 405 | } | ||
406 | } | 406 | } | ||
407 | 407 | | |||
408 | toIndices[i] = to; | 408 | toIndices[i] = to; | ||
409 | 409 | | |||
410 | if (to > newLast) { | ||||
411 | newLast = to; | ||||
412 | } | ||||
413 | } | ||||
414 | | ||||
415 | const int newCount = newLast + 1; | ||||
416 | QModelIndexList changed; | ||||
417 | | ||||
418 | auto doUpdateMaps = [&]() { | ||||
419 | for (int i = 0; i < fromIndices.count(); ++i) | ||||
420 | { | ||||
421 | const int from = fromIndices[i]; | ||||
422 | const int to = toIndices[i]; | ||||
423 | const int sourceRow = sourceRows[i]; | ||||
424 | | ||||
410 | if (!toIndices.contains(from)) { | 425 | if (!toIndices.contains(from)) { | ||
411 | m_proxyToSource.remove(from); | 426 | m_proxyToSource.remove(from); | ||
412 | } | 427 | } | ||
413 | 428 | | |||
414 | updateMaps(to, sourceRow); | 429 | updateMaps(to, sourceRow); | ||
415 | 430 | changed.append(index(from, 0)); | |||
416 | const QModelIndex &fromIdx = index(from, 0); | | |||
417 | emit dataChanged(fromIdx, fromIdx); | | |||
418 | 431 | | |||
419 | if (to < oldCount) { | 432 | if (to < oldCount) { | ||
420 | const QModelIndex &toIdx = index(to, 0); | 433 | changed.append(index(to, 0)); | ||
421 | emit dataChanged(toIdx, toIdx); | | |||
422 | } | 434 | } | ||
423 | } | 435 | } | ||
424 | 436 | }; | |||
425 | const int newCount = rowCount(); | | |||
426 | 437 | | |||
427 | if (newCount > oldCount) { | 438 | if (newCount > oldCount) { | ||
428 | if (m_beginInsertRowsCalled) { | 439 | if (m_beginInsertRowsCalled) { | ||
429 | endInsertRows(); | 440 | endInsertRows(); | ||
430 | m_beginInsertRowsCalled = false; | 441 | m_beginInsertRowsCalled = false; | ||
431 | } | 442 | } | ||
432 | beginInsertRows(QModelIndex(), oldCount, newCount - 1); | 443 | beginInsertRows(QModelIndex(), oldCount, newCount - 1); | ||
444 | doUpdateMaps(); | ||||
433 | endInsertRows(); | 445 | endInsertRows(); | ||
434 | } | 446 | } | ||
435 | 447 | | |||
436 | if (newCount < oldCount) { | 448 | if (newCount < oldCount) { | ||
437 | beginRemoveRows(QModelIndex(), newCount, oldCount - 1); | 449 | beginRemoveRows(QModelIndex(), newCount, oldCount - 1); | ||
450 | doUpdateMaps(); | ||||
438 | endRemoveRows(); | 451 | endRemoveRows(); | ||
439 | } | 452 | } | ||
440 | 453 | | |||
454 | for (auto idx : changed) { | ||||
455 | emit dataChanged(idx, idx); | ||||
456 | } | ||||
457 | | ||||
441 | m_updatePositionsTimer->start(); | 458 | m_updatePositionsTimer->start(); | ||
442 | } | 459 | } | ||
443 | 460 | | |||
444 | void Positioner::updatePositions() | 461 | void Positioner::updatePositions() | ||
445 | { | 462 | { | ||
446 | QStringList positions; | 463 | QStringList positions; | ||
447 | 464 | | |||
448 | if (m_enabled && !m_proxyToSource.isEmpty() && m_perStripe > 0) { | 465 | if (m_enabled && !m_proxyToSource.isEmpty() && m_perStripe > 0) { | ||
▲ Show 20 Lines • Show All 154 Lines • ▼ Show 20 Line(s) | 619 | } else { | |||
603 | newProxyToSource.insert(it.value(), it.key()); | 620 | newProxyToSource.insert(it.value(), it.key()); | ||
604 | newSourceToProxy.insert(it.key(), it.value()); | 621 | newSourceToProxy.insert(it.key(), it.value()); | ||
605 | } | 622 | } | ||
606 | } | 623 | } | ||
607 | 624 | | |||
608 | m_proxyToSource = newProxyToSource; | 625 | m_proxyToSource = newProxyToSource; | ||
609 | m_sourceToProxy = newSourceToProxy; | 626 | m_sourceToProxy = newSourceToProxy; | ||
610 | 627 | | |||
611 | m_lastRow = -1; | | |||
612 | int newLast = lastRow(); | 628 | int newLast = lastRow(); | ||
613 | 629 | | |||
614 | if (oldLast > newLast) { | 630 | if (oldLast > newLast) { | ||
615 | int diff = oldLast - newLast; | 631 | int diff = oldLast - newLast; | ||
616 | beginRemoveRows(QModelIndex(), ((oldLast - diff) + 1), oldLast); | 632 | beginRemoveRows(QModelIndex(), ((oldLast - diff) + 1), oldLast); | ||
617 | } else { | 633 | } else { | ||
618 | m_ignoreNextTransaction = true; | 634 | m_ignoreNextTransaction = true; | ||
619 | } | 635 | } | ||
▲ Show 20 Lines • Show All 92 Lines • ▼ Show 20 Line(s) | 727 | for (int i = 0; i < size; ++i) { | |||
712 | updateMaps(i, i); | 728 | updateMaps(i, i); | ||
713 | } | 729 | } | ||
714 | } | 730 | } | ||
715 | 731 | | |||
716 | void Positioner::updateMaps(int proxyIndex, int sourceIndex) | 732 | void Positioner::updateMaps(int proxyIndex, int sourceIndex) | ||
717 | { | 733 | { | ||
718 | m_proxyToSource.insert(proxyIndex, sourceIndex); | 734 | m_proxyToSource.insert(proxyIndex, sourceIndex); | ||
719 | m_sourceToProxy.insert(sourceIndex, proxyIndex); | 735 | m_sourceToProxy.insert(sourceIndex, proxyIndex); | ||
720 | m_lastRow = -1; | | |||
721 | } | 736 | } | ||
722 | 737 | | |||
723 | int Positioner::firstRow() const | 738 | int Positioner::firstRow() const | ||
724 | { | 739 | { | ||
725 | if (!m_proxyToSource.isEmpty()) { | 740 | if (!m_proxyToSource.isEmpty()) { | ||
726 | QList<int> keys(m_proxyToSource.keys()); | 741 | QList<int> keys(m_proxyToSource.keys()); | ||
727 | qSort(keys); | 742 | qSort(keys); | ||
728 | 743 | | |||
729 | return keys.first(); | 744 | return keys.first(); | ||
730 | } | 745 | } | ||
731 | 746 | | |||
732 | return -1; | 747 | return -1; | ||
733 | } | 748 | } | ||
734 | 749 | | |||
735 | int Positioner::lastRow() const | 750 | int Positioner::lastRow() const | ||
736 | { | 751 | { | ||
737 | if (!m_proxyToSource.isEmpty()) { | 752 | if (!m_proxyToSource.isEmpty()) { | ||
738 | if (m_lastRow != -1) { | | |||
739 | return m_lastRow; | | |||
740 | } else { | | |||
741 | QList<int> keys(m_proxyToSource.keys()); | 753 | QList<int> keys(m_proxyToSource.keys()); | ||
742 | qSort(keys); | 754 | qSort(keys); | ||
743 | return keys.last(); | 755 | return keys.last(); | ||
744 | } | 756 | } | ||
745 | } | | |||
746 | 757 | | |||
747 | return 0; | 758 | return 0; | ||
748 | } | 759 | } | ||
749 | 760 | | |||
750 | int Positioner::firstFreeRow() const | 761 | int Positioner::firstFreeRow() const | ||
751 | { | 762 | { | ||
752 | if (!m_proxyToSource.isEmpty()) { | 763 | if (!m_proxyToSource.isEmpty()) { | ||
753 | int last = lastRow(); | 764 | int last = lastRow(); | ||
▲ Show 20 Lines • Show All 187 Lines • Show Last 20 Lines |