diff --git a/computermodel.cpp b/computermodel.cpp index be37af31b..4e6272c2a 100644 --- a/computermodel.cpp +++ b/computermodel.cpp @@ -1,301 +1,301 @@ /*************************************************************************** * Copyright (C) 2007 Kevin Ottens * * Copyright (C) 2015 by Eike Hein * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * ***************************************************************************/ #include "computermodel.h" #include "actionlist.h" #include "simplefavoritesmodel.h" #include #include #include #include #include #include #include #include "krunner_interface.h" FilteredPlacesModel::FilteredPlacesModel(QObject *parent) : QSortFilterProxyModel(parent) , m_placesModel(new KFilePlacesModel(this)) { setSourceModel(m_placesModel); sort(0); } FilteredPlacesModel::~FilteredPlacesModel() { } QUrl FilteredPlacesModel::url(const QModelIndex &index) const { return KFilePlacesModel::convertedUrl(m_placesModel->url(mapToSource(index))); } bool FilteredPlacesModel::isDevice(const QModelIndex &index) const { return m_placesModel->isDevice(mapToSource(index)); } Solid::Device FilteredPlacesModel::deviceForIndex(const QModelIndex &index) const { return m_placesModel->deviceForIndex(mapToSource(index)); } bool FilteredPlacesModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const { const QModelIndex index = m_placesModel->index(sourceRow, 0, sourceParent); return !m_placesModel->isHidden(index) && !m_placesModel->data(index, KFilePlacesModel::FixedDeviceRole).toBool(); } bool FilteredPlacesModel::lessThan(const QModelIndex &left, const QModelIndex &right) const { bool lDevice = m_placesModel->isDevice(left); bool rDevice = m_placesModel->isDevice(right); if (lDevice && !rDevice) { return false; } else if (!lDevice && rDevice) { return true; } return (left.row() < right.row()); } RunCommandModel::RunCommandModel(QObject *parent) : AbstractModel(parent) { } RunCommandModel::~RunCommandModel() { } QString RunCommandModel::description() const { return QString(); } QVariant RunCommandModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) { return QVariant(); } if (role == Qt::DisplayRole) { return i18n("Run Command..."); } else if (role == Qt::DecorationRole) { return QIcon::fromTheme(QStringLiteral("system-run")); } else if (role == Kicker::DescriptionRole) { return i18n("Run a command or a search query"); } else if (role == Kicker::GroupRole) { return i18n("Applications"); } return QVariant(); } int RunCommandModel::rowCount(const QModelIndex &parent) const { return parent.isValid() ? 0 : (KAuthorized::authorize(QStringLiteral("run_command")) ? 1 : 0); } Q_INVOKABLE bool RunCommandModel::trigger(int row, const QString &actionId, const QVariant &argument) { Q_UNUSED(actionId) Q_UNUSED(argument) if (row == 0 && KAuthorized::authorize(QStringLiteral("run_command"))) { org::kde::krunner::App krunner(QStringLiteral("org.kde.krunner"), QStringLiteral("/App"), QDBusConnection::sessionBus()); krunner.display(); return true; } return false; } ComputerModel::ComputerModel(QObject *parent) : ForwardingModel(parent) , m_concatProxy(new KConcatenateRowsProxyModel(this)) , m_runCommandModel(new RunCommandModel(this)) , m_systemAppsModel(new SimpleFavoritesModel(this)) , m_filteredPlacesModel(new FilteredPlacesModel(this)) , m_appNameFormat(AppEntry::NameOnly) , m_appletInterface(nullptr) { connect(m_systemAppsModel, &SimpleFavoritesModel::favoritesChanged, this, &ComputerModel::systemApplicationsChanged); m_systemAppsModel->setFavorites(QStringList() << QStringLiteral("systemsettings.desktop")); m_concatProxy->addSourceModel(m_runCommandModel); m_concatProxy->addSourceModel(m_systemAppsModel); m_concatProxy->addSourceModel(m_filteredPlacesModel); setSourceModel(m_concatProxy); } ComputerModel::~ComputerModel() { } QString ComputerModel::description() const { return i18n("Computer"); } int ComputerModel::appNameFormat() const { return m_appNameFormat; } void ComputerModel::setAppNameFormat(int format) { if (m_appNameFormat != (AppEntry::NameFormat)format) { m_appNameFormat = (AppEntry::NameFormat)format; m_systemAppsModel->refresh(); emit appNameFormatChanged(); } } QObject *ComputerModel::appletInterface() const { return m_appletInterface; } void ComputerModel::setAppletInterface(QObject *appletInterface) { if (m_appletInterface != appletInterface) { m_appletInterface = appletInterface; emit appletInterfaceChanged(); } } QStringList ComputerModel::systemApplications() const { return m_systemAppsModel->favorites(); } void ComputerModel::setSystemApplications(const QStringList &apps) { m_systemAppsModel->setFavorites(apps); } QVariant ComputerModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) { return QVariant(); } const QModelIndex sourceIndex = m_concatProxy->mapToSource(m_concatProxy->index(index.row(), index.column())); bool isPlace = (sourceIndex.model() == m_filteredPlacesModel); if (isPlace) { if (role == Kicker::DescriptionRole) { if (m_filteredPlacesModel->isDevice(sourceIndex)) { Solid::Device device = m_filteredPlacesModel->deviceForIndex(sourceIndex); Solid::StorageAccess *access = device.as(); if (access) { return access->filePath(); } else { return QString(); } } else { const QUrl &url = m_filteredPlacesModel->url(sourceIndex); return url.toString(QUrl::PreferLocalFile); } } else if (role == Kicker::FavoriteIdRole) { if (!m_filteredPlacesModel->isDevice(sourceIndex)) { return m_filteredPlacesModel->url(sourceIndex); } } else if (role == Kicker::UrlRole) { return m_filteredPlacesModel->url(sourceIndex); } else if (role == Kicker::GroupRole) { return sourceIndex.data(KFilePlacesModel::GroupRole).toString(); } else if (role == Qt::DisplayRole || role == Qt::DecorationRole) { return sourceIndex.data(role); } } else if (role == Kicker::GroupRole) { return i18n("Applications"); } else { return sourceIndex.data(role); } return QVariant(); } bool ComputerModel::trigger(int row, const QString &actionId, const QVariant &argument) { const QModelIndex sourceIndex = m_concatProxy->mapToSource(m_concatProxy->index(row, 0)); if (sourceIndex.model() == m_filteredPlacesModel) { const QUrl &url = m_filteredPlacesModel->url(sourceIndex); if (url.isValid()) { - new KRun(url, 0); + new KRun(url, nullptr); return true; } Solid::Device device = m_filteredPlacesModel->deviceForIndex(sourceIndex); Solid::StorageAccess *access = device.as(); if (access && !access->isAccessible()) { connect(access, &Solid::StorageAccess::setupDone, this, &ComputerModel::onSetupDone); access->setup(); return true; } } else { AbstractModel *model = nullptr; if (sourceIndex.model() == m_systemAppsModel) { model = m_systemAppsModel; } else { model = m_runCommandModel; } return model->trigger(sourceIndex.row(), actionId, argument); } return false; } void ComputerModel::onSetupDone(Solid::ErrorType error, QVariant errorData, const QString &udi) { Q_UNUSED(errorData); if (error != Solid::NoError) { return; } Solid::Device device(udi); Solid::StorageAccess *access = device.as(); Q_ASSERT(access); - new KRun(QUrl::fromLocalFile(access->filePath()), 0); + new KRun(QUrl::fromLocalFile(access->filePath()), nullptr); } diff --git a/fileentry.cpp b/fileentry.cpp index 8913c4c86..c61dcf739 100644 --- a/fileentry.cpp +++ b/fileentry.cpp @@ -1,123 +1,123 @@ /*************************************************************************** * Copyright (C) 2015 by Eike Hein * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * ***************************************************************************/ #include "fileentry.h" #include "actionlist.h" #include #include FileEntry::FileEntry(AbstractModel *owner, const QUrl &url) : AbstractEntry(owner) , m_fileItem(nullptr) { if (url.isValid()) { m_fileItem = new KFileItem(url); m_fileItem->determineMimeType(); } } FileEntry::~FileEntry() { delete m_fileItem; } bool FileEntry::isValid() const { return m_fileItem && (m_fileItem->isFile() || m_fileItem->isDir()); } QIcon FileEntry::icon() const { if (m_fileItem) { return QIcon::fromTheme(m_fileItem->iconName(), QIcon::fromTheme(QStringLiteral("unknown"))); } return QIcon::fromTheme(QStringLiteral("unknown")); } QString FileEntry::name() const { if (m_fileItem) { return m_fileItem->text(); } return QString(); } QString FileEntry::description() const { if (m_fileItem) { return m_fileItem->url().toString(QUrl::PreferLocalFile); } return QString(); } QString FileEntry::id() const { if (m_fileItem) { return m_fileItem->url().toString(); } return QString(); } QUrl FileEntry::url() const { if (m_fileItem) { return m_fileItem->url(); } return QUrl(); } bool FileEntry::hasActions() const { return m_fileItem && m_fileItem->isFile(); } QVariantList FileEntry::actions() const { if (m_fileItem) { return Kicker::createActionListForFileItem(*m_fileItem); } return QVariantList(); } bool FileEntry::run(const QString& actionId, const QVariant &argument) { if (!m_fileItem) { return false; } if (actionId.isEmpty()) { - new KRun(m_fileItem->url(), 0); + new KRun(m_fileItem->url(), nullptr); return true; } else { bool close = false; if (Kicker::handleFileItemAction(*m_fileItem, actionId, argument, &close)) { return close; } } return false; } diff --git a/forwardingmodel.cpp b/forwardingmodel.cpp index 6313ed72c..a0cb86a8f 100644 --- a/forwardingmodel.cpp +++ b/forwardingmodel.cpp @@ -1,265 +1,265 @@ /*************************************************************************** * Copyright (C) 2015 by Eike Hein * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * ***************************************************************************/ #include "forwardingmodel.h" ForwardingModel::ForwardingModel(QObject *parent) : AbstractModel(parent) { } ForwardingModel::~ForwardingModel() { } QString ForwardingModel::description() const { if (!m_sourceModel) { return QString(); } AbstractModel *abstractModel = qobject_cast(m_sourceModel); if (!abstractModel) { return QString(); } return abstractModel->description(); } QAbstractItemModel *ForwardingModel::sourceModel() const { return m_sourceModel; } void ForwardingModel::setSourceModel(QAbstractItemModel *sourceModel) { disconnectSignals(); beginResetModel(); m_sourceModel = sourceModel; connectSignals(); endResetModel(); emit countChanged(); emit sourceModelChanged(); emit descriptionChanged(); } bool ForwardingModel::canFetchMore(const QModelIndex &parent) const { if (!m_sourceModel) { return false; } return m_sourceModel->canFetchMore(indexToSourceIndex(parent)); } void ForwardingModel::fetchMore(const QModelIndex &parent) { if (m_sourceModel) { m_sourceModel->fetchMore(indexToSourceIndex(parent)); } } QModelIndex ForwardingModel::index(int row, int column, const QModelIndex &parent) const { Q_UNUSED(parent) if (!m_sourceModel) { return QModelIndex(); } return createIndex(row, column); } QModelIndex ForwardingModel::parent(const QModelIndex &index) const { Q_UNUSED(index) return QModelIndex(); } QVariant ForwardingModel::data(const QModelIndex &index, int role) const { if (!m_sourceModel) { return QVariant(); } return m_sourceModel->data(indexToSourceIndex(index), role); } int ForwardingModel::rowCount(const QModelIndex &parent) const { if (!m_sourceModel) { return 0; } return m_sourceModel->rowCount(indexToSourceIndex(parent)); } QModelIndex ForwardingModel::indexToSourceIndex(const QModelIndex& index) const { if (!m_sourceModel || !index.isValid()) { return QModelIndex(); } return m_sourceModel->index(index.row(), index.column(), index.parent().isValid() ? indexToSourceIndex(index.parent()) : QModelIndex()); } bool ForwardingModel::trigger(int row, const QString &actionId, const QVariant &argument) { if (!m_sourceModel) { return false; } AbstractModel *abstractModel = qobject_cast(m_sourceModel); if (!abstractModel) { return false; } return abstractModel->trigger(row, actionId, argument); } QString ForwardingModel::labelForRow(int row) { if (!m_sourceModel) { return QString(); } AbstractModel *abstractModel = qobject_cast(m_sourceModel); if (!abstractModel) { return QString(); } return abstractModel->labelForRow(row); } AbstractModel* ForwardingModel::modelForRow(int row) { if (!m_sourceModel) { - return 0; + return nullptr; } AbstractModel *abstractModel = qobject_cast(m_sourceModel); if (!abstractModel) { - return 0; + return nullptr; } return abstractModel->modelForRow(row); } AbstractModel* ForwardingModel::favoritesModel() { AbstractModel *sourceModel = qobject_cast(m_sourceModel); if (sourceModel) { return sourceModel->favoritesModel(); } return AbstractModel::favoritesModel(); } int ForwardingModel::separatorCount() const { if (!m_sourceModel) { return 0; } AbstractModel *abstractModel = qobject_cast(m_sourceModel); if (!abstractModel) { return 0; } return abstractModel->separatorCount(); } void ForwardingModel::reset() { beginResetModel(); endResetModel(); emit countChanged(); emit separatorCountChanged(); } void ForwardingModel::connectSignals() { if (!m_sourceModel) { return; } connect(m_sourceModel, SIGNAL(destroyed()), this, SLOT(reset())); connect(m_sourceModel, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector)), this, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector)), Qt::UniqueConnection); connect(m_sourceModel, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)), this, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)), Qt::UniqueConnection); connect(m_sourceModel, SIGNAL(rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)), this, SIGNAL(rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)), Qt::UniqueConnection); connect(m_sourceModel, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), this, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), Qt::UniqueConnection); connect(m_sourceModel, SIGNAL(layoutAboutToBeChanged(QList,QAbstractItemModel::LayoutChangeHint)), this, SIGNAL(layoutAboutToBeChanged(QList,QAbstractItemModel::LayoutChangeHint)), Qt::UniqueConnection); connect(m_sourceModel, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SIGNAL(rowsInserted(QModelIndex,int,int)), Qt::UniqueConnection); connect(m_sourceModel, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SIGNAL(countChanged()), Qt::UniqueConnection); connect(m_sourceModel, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)), this, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)), Qt::UniqueConnection); connect(m_sourceModel, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SIGNAL(rowsRemoved(QModelIndex,int,int)), Qt::UniqueConnection); connect(m_sourceModel, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SIGNAL(countChanged()), Qt::UniqueConnection); connect(m_sourceModel, SIGNAL(modelAboutToBeReset()), this, SIGNAL(modelAboutToBeReset()), Qt::UniqueConnection); connect(m_sourceModel, SIGNAL(modelReset()), this, SIGNAL(modelReset()), Qt::UniqueConnection); connect(m_sourceModel, SIGNAL(modelReset()), this, SIGNAL(countChanged()), Qt::UniqueConnection); connect(m_sourceModel, SIGNAL(layoutChanged(QList,QAbstractItemModel::LayoutChangeHint)), this, SIGNAL(layoutChanged(QList,QAbstractItemModel::LayoutChangeHint)), Qt::UniqueConnection); } void ForwardingModel::disconnectSignals() { if (!m_sourceModel) { return; } - disconnect(m_sourceModel, 0, this, 0); + disconnect(m_sourceModel, nullptr, this, nullptr); } diff --git a/menuentryeditor.cpp b/menuentryeditor.cpp index 358ee4a37..4b4a759e5 100644 --- a/menuentryeditor.cpp +++ b/menuentryeditor.cpp @@ -1,68 +1,68 @@ /*************************************************************************** * Copyright (C) 2014 by Eike Hein * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * ***************************************************************************/ #include "menuentryeditor.h" #include #include #include #include MenuEntryEditor::MenuEntryEditor(QObject* parent) : QObject(parent) { } MenuEntryEditor::~MenuEntryEditor() { } bool MenuEntryEditor::canEdit(const QString& entryPath) const { KFileItemList itemList; itemList << KFileItem(QUrl::fromLocalFile(entryPath)); return KPropertiesDialog::canDisplay(itemList); } void MenuEntryEditor::edit(const QString& entryPath, const QString& menuId) { const QString &appsPath = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation); const QUrl &entryUrl = QUrl::fromLocalFile(entryPath); if (!appsPath.isEmpty() && entryUrl.isValid()) { const QDir appsDir(appsPath); const QString &fileName = entryUrl.fileName(); if (appsDir.exists(fileName)) { - KPropertiesDialog::showDialog(entryUrl, 0, false); + KPropertiesDialog::showDialog(entryUrl, nullptr, false); } else { if (!appsDir.exists()) { if (!QDir::root().mkpath(appsPath)) { return; } } KPropertiesDialog *dialog = new KPropertiesDialog(entryUrl, QUrl::fromLocalFile(appsPath), menuId); //KPropertiesDialog deletes itself dialog->show(); } } } diff --git a/placeholdermodel.cpp b/placeholdermodel.cpp index e0b5f763a..4168e20eb 100644 --- a/placeholdermodel.cpp +++ b/placeholdermodel.cpp @@ -1,407 +1,407 @@ /*************************************************************************** * Copyright (C) 2015 by Eike Hein * * Copyright (C) 2017 by Ivan Cukic * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * ***************************************************************************/ #include "placeholdermodel.h" #include "actionlist.h" #include "debug.h" #include PlaceholderModel::PlaceholderModel(QObject *parent) : AbstractModel(parent) , m_dropPlaceholderIndex(-1) , m_isTriggerInhibited(false) { connect(&m_triggerInhibitor, &QTimer::timeout, this, [&] { qCDebug(KICKER_DEBUG) << "%%% Inhibit stopped"; m_isTriggerInhibited = false; }); m_triggerInhibitor.setInterval(500); m_triggerInhibitor.setSingleShot(true); } void PlaceholderModel::inhibitTriggering() { qCDebug(KICKER_DEBUG) << "%%% Inhibit started"; m_isTriggerInhibited = true; m_triggerInhibitor.start(); } PlaceholderModel::~PlaceholderModel() { } QString PlaceholderModel::description() const { if (auto abstractModel = qobject_cast(m_sourceModel)) { return abstractModel->description(); } else { return QString(); } } QAbstractItemModel *PlaceholderModel::sourceModel() const { return m_sourceModel; } void PlaceholderModel::setSourceModel(QAbstractItemModel *sourceModel) { disconnectSignals(); beginResetModel(); m_sourceModel = sourceModel; connectSignals(); endResetModel(); emit countChanged(); emit sourceModelChanged(); emit descriptionChanged(); } bool PlaceholderModel::canFetchMore(const QModelIndex &parent) const { return m_sourceModel && m_sourceModel->canFetchMore(indexToSourceIndex(parent)); } void PlaceholderModel::fetchMore(const QModelIndex &parent) { if (m_sourceModel) { m_sourceModel->fetchMore(indexToSourceIndex(parent)); } } QModelIndex PlaceholderModel::index(int row, int column, const QModelIndex &parent) const { Q_UNUSED(parent) return m_sourceModel ? createIndex(row, column) : QModelIndex(); } QModelIndex PlaceholderModel::parent(const QModelIndex &index) const { Q_UNUSED(index) return QModelIndex(); } QVariant PlaceholderModel::data(const QModelIndex &index, int role) const { const auto row = index.row(); if (m_dropPlaceholderIndex == row) { switch (role) { case Kicker::IsDropPlaceholderRole: return true; // TODO: Maybe it would be nice to show something here? // case Qt::DisplayRole: // return "placeholder"; // // case Qt::DecorationRole: // return "select"; default: return QVariant(); } } return m_sourceModel ? m_sourceModel->data(indexToSourceIndex(index), role) : QVariant(); } int PlaceholderModel::rowCount(const QModelIndex &parent) const { if (!m_sourceModel || parent.isValid()) { return 0; } return m_sourceModel->rowCount() + (m_dropPlaceholderIndex != -1 ? 1 : 0); } QModelIndex PlaceholderModel::indexToSourceIndex(const QModelIndex& index) const { if (!m_sourceModel || !index.isValid()) { return QModelIndex(); } const auto row = index.row(); const auto column = index.column(); return index.parent().isValid() ? // We do not support tree models QModelIndex() : // If we are on top-level, lets add a placeholder m_sourceModel->index( row - (m_dropPlaceholderIndex != -1 && row > m_dropPlaceholderIndex ? 1 : 0), column, QModelIndex() ); } int PlaceholderModel::sourceRowToRow(int sourceRow) const { return sourceRow + (m_dropPlaceholderIndex != -1 && sourceRow >= m_dropPlaceholderIndex ? 1 : 0); } int PlaceholderModel::rowToSourceRow(int row) const { return row == m_dropPlaceholderIndex ? -1 : row - (m_dropPlaceholderIndex != -1 && row > m_dropPlaceholderIndex ? 1 : 0); } QModelIndex PlaceholderModel::sourceIndexToIndex(const QModelIndex& sourceIndex) const { if (!m_sourceModel || !sourceIndex.isValid()) { return QModelIndex(); } const auto sourceRow = sourceIndex.row(); const auto sourceColumn = sourceIndex.column(); return sourceIndex.parent().isValid() ? // We do not support tree-models QModelIndex() : // If we are on top-level, lets add a placeholder index( sourceRowToRow(sourceRow), sourceColumn, QModelIndex() ); } bool PlaceholderModel::trigger(int row, const QString &actionId, const QVariant &argument) { if (m_isTriggerInhibited) return false; if (auto abstractModel = qobject_cast(m_sourceModel)) { return abstractModel->trigger(rowToSourceRow(row), actionId, argument); } else { return false; } } QString PlaceholderModel::labelForRow(int row) { if (auto abstractModel = qobject_cast(m_sourceModel)) { return abstractModel->labelForRow(rowToSourceRow(row)); } else { return QString(); } } AbstractModel* PlaceholderModel::modelForRow(int row) { if (auto abstractModel = qobject_cast(m_sourceModel)) { return abstractModel->modelForRow(rowToSourceRow(row)); } else { - return 0; + return nullptr; } } AbstractModel* PlaceholderModel::favoritesModel() { if (auto abstractModel = qobject_cast(m_sourceModel)) { return abstractModel->favoritesModel(); } else { return AbstractModel::favoritesModel(); } } int PlaceholderModel::separatorCount() const { if (auto abstractModel = qobject_cast(m_sourceModel)) { return abstractModel->separatorCount(); } else { return 0; } } void PlaceholderModel::reset() { emit beginResetModel(); emit endResetModel(); emit countChanged(); emit separatorCountChanged(); } void PlaceholderModel::connectSignals() { if (!m_sourceModel) { return; } const auto sourceModelPtr = m_sourceModel.data(); connect(sourceModelPtr, SIGNAL(destroyed()), this, SLOT(reset())); connect(sourceModelPtr, &QAbstractItemModel::dataChanged, this, [this] (const QModelIndex &from, const QModelIndex &to, const QVector &roles) { emit dataChanged(sourceIndexToIndex(from), sourceIndexToIndex(to), roles); }); connect(sourceModelPtr, &QAbstractItemModel::rowsAboutToBeInserted, this, [this] (const QModelIndex &parent, int from, int to) { if (parent.isValid()) { qWarning() << "We do not support tree models"; } else { beginInsertRows(QModelIndex(), sourceRowToRow(from), sourceRowToRow(to)); } }); connect(sourceModelPtr, &QAbstractItemModel::rowsInserted, this, [this] { endInsertRows(); emit countChanged(); }); connect(sourceModelPtr, &QAbstractItemModel::rowsAboutToBeMoved, this, [this] (const QModelIndex &source, int from, int to, const QModelIndex &dest, int destRow) { if (source.isValid() || dest.isValid()) { qWarning() << "We do not support tree models"; } else { beginMoveRows(QModelIndex(), sourceRowToRow(from), sourceRowToRow(to), QModelIndex(), sourceRowToRow(destRow)); } }); connect(sourceModelPtr, &QAbstractItemModel::rowsMoved, this, [this] { endMoveRows(); }); connect(sourceModelPtr, &QAbstractItemModel::rowsAboutToBeRemoved, this, [this] (const QModelIndex &parent, int from, int to) { if (parent.isValid()) { qWarning() << "We do not support tree models"; } else { beginRemoveRows(QModelIndex(), sourceRowToRow(from), sourceRowToRow(to)); } }); connect(sourceModelPtr, &QAbstractItemModel::rowsRemoved, this, [this] { endRemoveRows(); emit countChanged(); }); connect(sourceModelPtr, &QAbstractItemModel::modelAboutToBeReset, this, [this] { beginResetModel(); }); connect(sourceModelPtr, &QAbstractItemModel::modelReset, this, [this] { endResetModel(); emit countChanged(); }); // We do not have persistant indices // connect(sourceModelPtr, &QAbstractItemModel::layoutAboutToBeChanged), // this, &PlaceholderModel::layoutAboutToBeChanged); // connect(sourceModelPtr, &QAbstractItemModel::layoutChanged), // this, SIGNAL(layoutChanged(QList,QAbstractItemModel::LayoutChangeHint)), // Qt::UniqueConnection); } void PlaceholderModel::disconnectSignals() { if (!m_sourceModel) { return; } - disconnect(m_sourceModel, 0, this, 0); + disconnect(m_sourceModel, nullptr, this, nullptr); } int PlaceholderModel::dropPlaceholderIndex() const { return m_dropPlaceholderIndex; } void PlaceholderModel::setDropPlaceholderIndex(int index) { if (index == m_dropPlaceholderIndex) return; inhibitTriggering(); if (index == -1 && m_dropPlaceholderIndex != -1) { // Removing the placeholder beginRemoveRows(QModelIndex(), m_dropPlaceholderIndex, m_dropPlaceholderIndex); m_dropPlaceholderIndex = index; endRemoveRows(); emit countChanged(); } else if (index != -1 && m_dropPlaceholderIndex == -1) { // Creating the placeholder beginInsertRows(QModelIndex(), index, index); m_dropPlaceholderIndex = index; endInsertRows(); emit countChanged(); } else if (m_dropPlaceholderIndex != index) { // Moving the placeholder int modelTo = index + (index > m_dropPlaceholderIndex ? 1 : 0); if (beginMoveRows( QModelIndex(), m_dropPlaceholderIndex, m_dropPlaceholderIndex, QModelIndex(), modelTo)) { m_dropPlaceholderIndex = index; endMoveRows(); } } emit dropPlaceholderIndexChanged(); }