diff --git a/panels/dock/taskmanager/dockglobalelementmodel.cpp b/panels/dock/taskmanager/dockglobalelementmodel.cpp index 0e5ab0494..9ef221be8 100644 --- a/panels/dock/taskmanager/dockglobalelementmodel.cpp +++ b/panels/dock/taskmanager/dockglobalelementmodel.cpp @@ -29,122 +29,141 @@ DockGlobalElementModel::DockGlobalElementModel(QAbstractItemModel *appsModel, Do , m_activeAppModel(activeAppModel) { connect(TaskManagerSettings::instance(), &TaskManagerSettings::dockedElementsChanged, this, &DockGlobalElementModel::loadDockedElements); - connect(m_appsModel, &QAbstractItemModel::rowsRemoved, this, [this](const QModelIndex &parent, int first, int last) { - Q_UNUSED(parent) - for (int i = first; i <= last; ++i) { - auto it = std::find_if(m_data.begin(), m_data.end(), [this, &i](auto data) { - return std::get<1>(data) == m_appsModel && std::get<2>(data) == i; - }); - if (it != m_data.end()) { - auto pos = it - m_data.begin(); - beginRemoveRows(QModelIndex(), pos, pos); - m_data.remove(pos); - endRemoveRows(); - } - } - std::for_each(m_data.begin(), m_data.end(), [this, first, last](auto &data) { - if (std::get<1>(data) == m_appsModel && std::get<2>(data) >= first) { - data = std::make_tuple(std::get<0>(data), std::get<1>(data), std::get<2>(data) - ((last - first) + 1)); - } - }); - }); - - connect(m_activeAppModel, &QAbstractItemModel::rowsInserted, this, [this](const QModelIndex &parent, int first, int last) { - Q_UNUSED(parent) - for (int i = first; i <= last; ++i) { - auto index = m_activeAppModel->index(i, 0); - auto desktopId = index.data(TaskManager::DesktopIdRole).toString(); - - auto it = std::find_if(m_data.begin(), m_data.end(), [this, &desktopId](auto &data) { - return m_appsModel == std::get<1>(data) && desktopId == std::get<0>(data); - }); - - if (it != m_data.end()) { - *it = std::make_tuple(desktopId, m_activeAppModel, i); - auto pIndex = this->index(it - m_data.begin(), 0); - Q_EMIT dataChanged(pIndex, pIndex, {TaskManager::ActiveRole, TaskManager::AttentionRole, TaskManager::WindowsRole, TaskManager::MenusRole}); - - } else { - beginInsertRows(QModelIndex(), m_data.size(), m_data.size()); - m_data.append(std::make_tuple(desktopId, m_activeAppModel, i)); - endInsertRows(); - } - } - - std::for_each(m_data.begin(), m_data.end(), [this, first, last](auto &data) { - if (std::get<1>(data) == m_activeAppModel && std::get<2>(data) > first) { - data = std::make_tuple(std::get<0>(data), std::get<1>(data), std::get<2>(data) + ((last - first) + 1)); + connect( + m_appsModel, + &QAbstractItemModel::rowsRemoved, + this, + [this](const QModelIndex &parent, int first, int last) { + Q_UNUSED(parent) + for (int i = first; i <= last; ++i) { + auto it = std::find_if(m_data.begin(), m_data.end(), [this, &i](auto data) { + return std::get<1>(data) == m_appsModel && std::get<2>(data) == i; + }); + if (it != m_data.end()) { + auto pos = it - m_data.begin(); + beginRemoveRows(QModelIndex(), pos, pos); + m_data.remove(pos); + endRemoveRows(); + } } - }); - }); - - connect(m_activeAppModel, &QAbstractItemModel::rowsRemoved, this, [this](const QModelIndex &parent, int first, int last) { - Q_UNUSED(parent) - for (int i = first; i <= last; ++i) { - auto it = std::find_if(m_data.begin(), m_data.end(), [this, i](auto data) { - return std::get<1>(data) == m_activeAppModel && std::get<2>(data) == i; + std::for_each(m_data.begin(), m_data.end(), [this, first, last](auto &data) { + if (std::get<1>(data) == m_appsModel && std::get<2>(data) >= first) { + data = std::make_tuple(std::get<0>(data), std::get<1>(data), std::get<2>(data) - ((last - first) + 1)); + } }); + }, + Qt::QueuedConnection); + + connect( + m_activeAppModel, + &QAbstractItemModel::rowsInserted, + this, + [this](const QModelIndex &parent, int first, int last) { + Q_UNUSED(parent) + for (int i = first; i <= last; ++i) { + auto index = m_activeAppModel->index(i, 0); + auto desktopId = index.data(TaskManager::DesktopIdRole).toString(); + + auto it = std::find_if(m_data.begin(), m_data.end(), [this, &desktopId](auto &data) { + return m_appsModel == std::get<1>(data) && desktopId == std::get<0>(data); + }); + + if (it != m_data.end()) { + *it = std::make_tuple(desktopId, m_activeAppModel, i); + auto pIndex = this->index(it - m_data.begin(), 0); + Q_EMIT dataChanged(pIndex, pIndex, {TaskManager::ActiveRole, TaskManager::AttentionRole, TaskManager::WindowsRole, TaskManager::MenusRole}); - if (it == m_data.end()) { - qWarning() << "failed to find a running apps on dock" << i; - continue; + } else { + beginInsertRows(QModelIndex(), m_data.size(), m_data.size()); + m_data.append(std::make_tuple(desktopId, m_activeAppModel, i)); + endInsertRows(); + } } - auto pos = it - m_data.begin(); - auto id = std::get<0>(*it); - - auto oit = std::find_if(m_data.constBegin(), m_data.constEnd(), [this, &id, i](auto &data) { - return std::get<0>(data) == id && std::get<1>(data) == m_activeAppModel && std::get<2>(data) != i; + std::for_each(m_data.begin(), m_data.end(), [this, first, last](auto &data) { + if (std::get<1>(data) == m_activeAppModel && std::get<2>(data) > first) { + data = std::make_tuple(std::get<0>(data), std::get<1>(data), std::get<2>(data) + ((last - first) + 1)); + } }); + }, + Qt::QueuedConnection); + + connect( + m_activeAppModel, + &QAbstractItemModel::rowsRemoved, + this, + [this](const QModelIndex &parent, int first, int last) { + Q_UNUSED(parent) + for (int i = first; i <= last; ++i) { + auto it = std::find_if(m_data.begin(), m_data.end(), [this, i](auto data) { + return std::get<1>(data) == m_activeAppModel && std::get<2>(data) == i; + }); + + if (it == m_data.end()) { + qWarning() << "failed to find a running apps on dock" << i; + continue; + } - if (oit == m_data.constEnd() && m_dockedElements.contains(std::make_tuple("desktop", id))) { - auto pIndex = this->index(pos, 0); - auto res = m_appsModel->match(m_appsModel->index(0, 0), TaskManager::DesktopIdRole, id, 1, Qt::MatchExactly); - if (res.isEmpty()) { + auto pos = it - m_data.begin(); + auto id = std::get<0>(*it); + + auto oit = std::find_if(m_data.constBegin(), m_data.constEnd(), [this, &id, i](auto &data) { + return std::get<0>(data) == id && std::get<1>(data) == m_activeAppModel && std::get<2>(data) != i; + }); + + if (oit == m_data.constEnd() && m_dockedElements.contains(std::make_tuple("desktop", id))) { + auto pIndex = this->index(pos, 0); + auto res = m_appsModel->match(m_appsModel->index(0, 0), TaskManager::DesktopIdRole, id, 1, Qt::MatchExactly); + if (res.isEmpty()) { + beginRemoveRows(QModelIndex(), pos, pos); + m_data.remove(pos); + endRemoveRows(); + } else { + auto row = res.first().row(); + *it = std::make_tuple(id, m_appsModel, row); + Q_EMIT dataChanged(pIndex, + pIndex, + {TaskManager::ActiveRole, TaskManager::AttentionRole, TaskManager::WindowsRole, TaskManager::MenusRole}); + } + } else { beginRemoveRows(QModelIndex(), pos, pos); m_data.remove(pos); endRemoveRows(); - } else { - auto row = res.first().row(); - *it = std::make_tuple(id, m_appsModel, row); - Q_EMIT dataChanged(pIndex, pIndex, {TaskManager::ActiveRole, TaskManager::AttentionRole, TaskManager::WindowsRole, TaskManager::MenusRole}); } - } else { - beginRemoveRows(QModelIndex(), pos, pos); - m_data.remove(pos); - endRemoveRows(); - } - } - std::for_each(m_data.begin(), m_data.end(), [this, first, last](auto &data) { - if (std::get<1>(data) == m_activeAppModel && std::get<2>(data) >= first) { - data = std::make_tuple(std::get<0>(data), std::get<1>(data), std::get<2>(data) - ((last - first) + 1)); } - }); - }); - - connect(m_activeAppModel, - &QAbstractItemModel::dataChanged, - this, - [this](const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList &roles) { - int first = topLeft.row(), last = bottomRight.row(); - for (int i = first; i <= last; i++) { - auto it = std::find_if(m_data.constBegin(), m_data.constEnd(), [this, i](auto data) { - return std::get<1>(data) == m_activeAppModel && std::get<2>(data) == i; - }); - - if (it == m_data.end()) - return; - auto pos = it - m_data.constBegin(); - - auto oldRoles = roles; - auto desktopId = roles.indexOf(TaskManager::DesktopIdRole); - auto identifyId = roles.indexOf(TaskManager::IdentityRole); - if (desktopId != -1 || identifyId != -1) { - oldRoles.append(TaskManager::ItemIdRole); - } - Q_EMIT dataChanged(index(pos, 0), index(pos, 0), oldRoles); + std::for_each(m_data.begin(), m_data.end(), [this, first, last](auto &data) { + if (std::get<1>(data) == m_activeAppModel && std::get<2>(data) >= first) { + data = std::make_tuple(std::get<0>(data), std::get<1>(data), std::get<2>(data) - ((last - first) + 1)); } }); + }, + Qt::QueuedConnection); + + connect( + m_activeAppModel, + &QAbstractItemModel::dataChanged, + this, + [this](const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList &roles) { + int first = topLeft.row(), last = bottomRight.row(); + for (int i = first; i <= last; i++) { + auto it = std::find_if(m_data.constBegin(), m_data.constEnd(), [this, i](auto data) { + return std::get<1>(data) == m_activeAppModel && std::get<2>(data) == i; + }); + + if (it == m_data.end()) + return; + auto pos = it - m_data.constBegin(); + + auto oldRoles = roles; + auto desktopId = roles.indexOf(TaskManager::DesktopIdRole); + auto identifyId = roles.indexOf(TaskManager::IdentityRole); + if (desktopId != -1 || identifyId != -1) { + oldRoles.append(TaskManager::ItemIdRole); + } + Q_EMIT dataChanged(index(pos, 0), index(pos, 0), oldRoles); + } + }, + Qt::QueuedConnection); QMetaObject::invokeMethod(this, &DockGlobalElementModel::loadDockedElements, Qt::QueuedConnection); }