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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
/*
selectionmodelmodel.cpp
This file is part of GammaRay, the Qt application inspection and manipulation tool.
SPDX-FileCopyrightText: 2016 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
Author: Volker Krause <volker.krause@kdab.com>
SPDX-License-Identifier: GPL-2.0-or-later
Contact KDAB at <info@kdab.com> for commercial licensing options.
*/
#include "selectionmodelmodel.h"
#include <core/objectdataprovider.h>
#include <QItemSelectionModel>
#include <algorithm>
using namespace GammaRay;
SelectionModelModel::SelectionModelModel(QObject *parent)
: ObjectModelBase<QAbstractTableModel>(parent)
, m_model(nullptr)
{
}
SelectionModelModel::~SelectionModelModel() = default;
void SelectionModelModel::objectCreated(QObject *obj)
{
Q_ASSERT(obj);
auto model = qobject_cast<QItemSelectionModel *>(obj);
if (!model)
return;
auto it = std::lower_bound(m_selectionModels.begin(), m_selectionModels.end(), model);
if (it != m_selectionModels.end() && *it == model)
return;
m_selectionModels.insert(it, model);
connect(model, &QItemSelectionModel::selectionChanged, this, &SelectionModelModel::selectionChanged);
connect(model, &QItemSelectionModel::modelChanged, this, &SelectionModelModel::sourceModelChanged);
if (!m_model || model->model() != m_model)
return;
it = std::lower_bound(m_currentSelectionModels.begin(), m_currentSelectionModels.end(), model);
const auto row = std::distance(m_currentSelectionModels.begin(), it);
beginInsertRows(QModelIndex(), row, row);
m_currentSelectionModels.insert(it, model);
endInsertRows();
}
void SelectionModelModel::objectDestroyed(QObject *obj)
{
Q_ASSERT(obj);
// do not dereference!
QItemSelectionModel *unsafeModelPtr = nullptr;
memcpy(&unsafeModelPtr, &obj, sizeof(unsafeModelPtr));
auto it = std::lower_bound(m_selectionModels.begin(), m_selectionModels.end(), unsafeModelPtr);
if (it == m_selectionModels.end() || *it != unsafeModelPtr)
return;
m_selectionModels.erase(it);
it = std::lower_bound(m_currentSelectionModels.begin(), m_currentSelectionModels.end(), unsafeModelPtr);
if (it == m_currentSelectionModels.end() || *it != unsafeModelPtr)
return;
const auto row = std::distance(m_currentSelectionModels.begin(), it);
beginRemoveRows(QModelIndex(), row, row);
m_currentSelectionModels.erase(it);
endRemoveRows();
}
void SelectionModelModel::sourceModelChanged()
{
auto model = qobject_cast<QItemSelectionModel *>(sender());
Q_ASSERT(model);
auto it = std::lower_bound(m_currentSelectionModels.begin(), m_currentSelectionModels.end(), model);
if (it != m_currentSelectionModels.end() && *it == model && model->model() != m_model && m_model) {
const auto row = std::distance(m_currentSelectionModels.begin(), it);
beginRemoveRows(QModelIndex(), row, row);
m_currentSelectionModels.erase(it);
endRemoveRows();
}
if (model->model() == m_model && m_model) {
auto it = std::lower_bound(m_currentSelectionModels.begin(), m_currentSelectionModels.end(), model);
if (it != m_currentSelectionModels.end() && *it == model)
return;
const auto row = std::distance(m_currentSelectionModels.begin(), it);
beginInsertRows(QModelIndex(), row, row);
m_currentSelectionModels.insert(it, model);
endInsertRows();
}
}
void SelectionModelModel::selectionChanged()
{
auto model = qobject_cast<QItemSelectionModel *>(sender());
Q_ASSERT(model);
if (model->model() != m_model)
return;
const auto it = std::lower_bound(m_currentSelectionModels.constBegin(), m_currentSelectionModels.constEnd(), model);
Q_ASSERT(it != m_currentSelectionModels.constEnd() && *it == model);
const auto row = std::distance(m_currentSelectionModels.constBegin(), it);
emit dataChanged(index(row, 1), index(row, 3));
}
void SelectionModelModel::setModel(QAbstractItemModel *model)
{
if (model == m_model)
return;
if (!m_currentSelectionModels.isEmpty()) {
beginRemoveRows(QModelIndex(), 0, m_currentSelectionModels.size() - 1);
m_currentSelectionModels.clear();
endRemoveRows();
}
m_model = model;
QVector<QItemSelectionModel *> models;
std::copy_if(m_selectionModels.constBegin(), m_selectionModels.constEnd(), std::back_inserter(models), [this](QItemSelectionModel *model) {
return model->model() == m_model;
});
if (models.isEmpty())
return;
beginInsertRows(QModelIndex(), 0, models.size() - 1);
m_currentSelectionModels = std::move(models);
endInsertRows();
}
int SelectionModelModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 5;
}
int SelectionModelModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return m_currentSelectionModels.size();
}
QVariant SelectionModelModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
auto model = m_currentSelectionModels.at(index.row());
if (role == Qt::DisplayRole) {
switch (index.column()) {
case 1:
return model->selectedIndexes().size();
case 2:
return model->selectedRows().size();
case 3:
return model->selectedColumns().size();
case 4:
return ObjectDataProvider::typeName(model);
}
}
return dataForObject(model, index, role);
}
QVariant SelectionModelModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
switch (section) {
case 0:
return tr("Object");
case 1:
return tr("#Items");
case 2:
return tr("#Rows");
case 3:
return tr("#Columns");
case 4:
return tr("Type");
}
}
return QAbstractTableModel::headerData(section, orientation, role);
}
|