diff options
Diffstat (limited to 'src/widgets/dialogs/qsidebar.cpp')
-rw-r--r-- | src/widgets/dialogs/qsidebar.cpp | 97 |
1 files changed, 51 insertions, 46 deletions
diff --git a/src/widgets/dialogs/qsidebar.cpp b/src/widgets/dialogs/qsidebar.cpp index bbbcb83630..581e659a44 100644 --- a/src/widgets/dialogs/qsidebar.cpp +++ b/src/widgets/dialogs/qsidebar.cpp @@ -43,6 +43,12 @@ QUrlModel::QUrlModel(QObject *parent) : QStandardItemModel(parent), showFullPath { } +QUrlModel::~QUrlModel() +{ + for (const auto &conn : std::as_const(modelConnections)) + disconnect(conn); +} + /*! \reimp */ @@ -177,11 +183,14 @@ void QUrlModel::setUrl(const QModelIndex &index, const QUrl &url, const QModelIn setData(index, true, EnabledRole); } + // newIcon could be null if fileSystemModel->iconProvider() returns null + if (!newIcon.isNull()) { // Make sure that we have at least 32x32 images - const QSize size = newIcon.actualSize(QSize(32,32)); - if (size.width() < 32) { - QPixmap smallPixmap = newIcon.pixmap(QSize(32, 32)); - newIcon.addPixmap(smallPixmap.scaledToWidth(32, Qt::SmoothTransformation)); + const QSize size = newIcon.actualSize(QSize(32,32)); + if (size.width() < 32) { + QPixmap smallPixmap = newIcon.pixmap(QSize(32, 32)); + newIcon.addPixmap(smallPixmap.scaledToWidth(32, Qt::SmoothTransformation)); + } } if (index.data().toString() != newName) @@ -211,8 +220,9 @@ void QUrlModel::addUrls(const QList<QUrl> &list, int row, bool move) if (row == -1) row = rowCount(); row = qMin(row, rowCount()); - for (int i = list.count() - 1; i >= 0; --i) { - QUrl url = list.at(i); + const auto rend = list.crend(); + for (auto it = list.crbegin(); it != rend; ++it) { + QUrl url = *it; if (!url.isValid() || url.scheme() != "file"_L1) continue; //this makes sure the url is clean @@ -265,21 +275,19 @@ void QUrlModel::setFileSystemModel(QFileSystemModel *model) if (model == fileSystemModel) return; if (fileSystemModel != nullptr) { - disconnect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), - this, SLOT(dataChanged(QModelIndex,QModelIndex))); - disconnect(model, SIGNAL(layoutChanged()), - this, SLOT(layoutChanged())); - disconnect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)), - this, SLOT(layoutChanged())); + for (const auto &conn : std::as_const(modelConnections)) + disconnect(conn); } fileSystemModel = model; if (fileSystemModel != nullptr) { - connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), - this, SLOT(dataChanged(QModelIndex,QModelIndex))); - connect(model, SIGNAL(layoutChanged()), - this, SLOT(layoutChanged())); - connect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)), - this, SLOT(layoutChanged())); + modelConnections = { + connect(model, &QFileSystemModel::dataChanged, + this, &QUrlModel::dataChanged), + connect(model, &QFileSystemModel::layoutChanged, + this, &QUrlModel::layoutChanged), + connect(model, &QFileSystemModel::rowsRemoved, + this, &QUrlModel::layoutChanged), + }; } clear(); insertColumns(0, 1); @@ -291,7 +299,7 @@ void QUrlModel::setFileSystemModel(QFileSystemModel *model) void QUrlModel::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) { QModelIndex parent = topLeft.parent(); - for (int i = 0; i < watching.count(); ++i) { + for (int i = 0; i < watching.size(); ++i) { QModelIndex index = watching.at(i).index; if (index.model() && topLeft.model()) { Q_ASSERT(index.model() == topLeft.model()); @@ -312,13 +320,11 @@ void QUrlModel::dataChanged(const QModelIndex &topLeft, const QModelIndex &botto void QUrlModel::layoutChanged() { QStringList paths; - const int numPaths = watching.count(); - paths.reserve(numPaths); - for (int i = 0; i < numPaths; ++i) - paths.append(watching.at(i).path); + paths.reserve(watching.size()); + for (const WatchItem &item : std::as_const(watching)) + paths.append(item.path); watching.clear(); - for (int i = 0; i < numPaths; ++i) { - QString path = paths.at(i); + for (const auto &path : paths) { QModelIndex newIndex = fileSystemModel->index(path); watching.append({newIndex, path}); if (newIndex.isValid()) @@ -353,14 +359,16 @@ void QSidebar::setModelAndUrls(QFileSystemModel *model, const QList<QUrl> &newUr setModel(urlModel); setItemDelegate(new QSideBarDelegate(this)); - connect(selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), - this, SLOT(clicked(QModelIndex))); + connect(selectionModel(), &QItemSelectionModel::currentChanged, + this, &QSidebar::clicked); #if QT_CONFIG(draganddrop) setDragDropMode(QAbstractItemView::DragDrop); #endif +#if QT_CONFIG(menu) setContextMenuPolicy(Qt::CustomContextMenu); - connect(this, SIGNAL(customContextMenuRequested(QPoint)), - this, SLOT(showContextMenu(QPoint))); + connect(this, &QSidebar::customContextMenuRequested, + this, &QSidebar::showContextMenu); +#endif urlModel->setUrls(newUrls); setCurrentIndex(this->model()->index(0,0)); } @@ -386,8 +394,8 @@ QSize QSidebar::sizeHint() const void QSidebar::selectUrl(const QUrl &url) { - disconnect(selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), - this, SLOT(clicked(QModelIndex))); + disconnect(selectionModel(), &QItemSelectionModel::currentChanged, + this, &QSidebar::clicked); selectionModel()->clear(); for (int i = 0; i < model()->rowCount(); ++i) { @@ -397,8 +405,8 @@ void QSidebar::selectUrl(const QUrl &url) } } - connect(selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), - this, SLOT(clicked(QModelIndex))); + connect(selectionModel(), &QItemSelectionModel::currentChanged, + this, &QSidebar::clicked); } #if QT_CONFIG(menu) @@ -414,10 +422,10 @@ void QSidebar::showContextMenu(const QPoint &position) QAction *action = new QAction(QFileDialog::tr("Remove"), this); if (indexAt(position).data(QUrlModel::UrlRole).toUrl().path().isEmpty()) action->setEnabled(false); - connect(action, SIGNAL(triggered()), this, SLOT(removeEntry())); + connect(action, &QAction::triggered, this, &QSidebar::removeEntry); actions.append(action); } - if (actions.count() > 0) + if (actions.size() > 0) QMenu::exec(actions, mapToGlobal(position)); } #endif // QT_CONFIG(menu) @@ -429,16 +437,13 @@ void QSidebar::showContextMenu(const QPoint &position) */ void QSidebar::removeEntry() { - QList<QModelIndex> idxs = selectionModel()->selectedIndexes(); - QList<QPersistentModelIndex> indexes; - const int numIndexes = idxs.count(); - indexes.reserve(numIndexes); - for (int i = 0; i < numIndexes; i++) - indexes.append(idxs.at(i)); - - for (int i = 0; i < numIndexes; ++i) { - if (!indexes.at(i).data(QUrlModel::UrlRole).toUrl().path().isEmpty()) - model()->removeRow(indexes.at(i).row()); + const QList<QModelIndex> idxs = selectionModel()->selectedIndexes(); + // Create a list of QPersistentModelIndex as the removeRow() calls below could + // invalidate the indexes in "idxs" + const QList<QPersistentModelIndex> persIndexes(idxs.cbegin(), idxs.cend()); + for (const QPersistentModelIndex &persistent : persIndexes) { + if (!persistent.data(QUrlModel::UrlRole).toUrl().path().isEmpty()) + model()->removeRow(persistent.row()); } } @@ -470,7 +475,7 @@ void QSidebar::focusInEvent(QFocusEvent *event) bool QSidebar::event(QEvent * event) { if (event->type() == QEvent::KeyRelease) { - QKeyEvent* ke = (QKeyEvent*) event; + QKeyEvent *ke = static_cast<QKeyEvent *>(event); if (ke->key() == Qt::Key_Delete) { removeEntry(); return true; |