40#include <QItemSelection>
60 int r = sourceIndex.row(), c = sourceIndex.column();
61 int n = sourceModel()->columnCount();
67 int k = (n * (n - 1) / 2) - (n - r) * ((n - r) - 1) / 2 + c - r - 1;
75 int n = sourceModel()->columnCount();
76 int k = proxyIndex.row();
77 int r = n - 2 -
static_cast<int>(sqrt(-8 * k + 4 * n * (n - 1) - 7) / 2.0 - 0.5);
78 int c = k + r + 1 - n * (n - 1) / 2 + (n - r) * ((n - r) - 1) / 2;
79 return sourceModel()->index(r, c);
84 int n = sourceModel()->rowCount();
85 return (n * (n - 1) / 2);
95 return createIndex(row, column);
100 return QModelIndex();
106 switch (
index.column())
109 if (role != Qt::DisplayRole)
115 return sourceModel()->headerData(src_index.row(), Qt::Horizontal, Qt::DisplayRole);
118 if (role != Qt::DisplayRole)
120 return sourceModel()->headerData(src_index.column(), Qt::Vertical, Qt::DisplayRole);
122 if (role != Qt::CheckStateRole)
128 return sourceModel()->data(src_index, Qt::CheckStateRole);
131 if (role != Qt::DisplayRole)
137 return sourceModel()->data(src_index, Qt::ToolTipRole);
146 return qobject_cast<CollisionMatrixModel*>(sourceModel())->reason(src_index);
153 if (role == Qt::CheckStateRole)
155 sourceModel()->setData(src_index, value, role);
157 Q_EMIT dataChanged(this->
index(r, 2), this->
index(r, 3));
165 for (
const auto idx : selection.indexes())
167 if (idx.column() != 2)
169 setData(idx, value ? Qt::Checked : Qt::Unchecked, Qt::CheckStateRole);
175 if (
index.column() == 2)
177 return Qt::ItemIsUserCheckable | QAbstractItemModel::flags(
index);
181 return QAbstractItemModel::flags(
index);
187 if (role != Qt::DisplayRole)
190 if (orientation == Qt::Horizontal)
201 return "Reason to Disable";
204 else if (orientation == Qt::Vertical)
213#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
214 connect(
this, SIGNAL(sourceModelChanged()),
this, SLOT(initSorting()));
218 sort_columns_ << 0 << 1;
219 sort_orders_ << Qt::AscendingOrder << Qt::AscendingOrder;
224 if (role == Qt::DisplayRole && orientation == Qt::Vertical)
230 return QSortFilterProxyModel::headerData(section, orientation, role);
239void SortFilterProxyModel::initSorting()
242 int prev_size = sort_columns_.size();
243 sort_columns_.resize(cols);
244 sort_orders_.resize(cols);
247 for (
int i = prev_size, end = sort_columns_.size(); i < end; ++i)
248 sort_columns_[i] = -1;
253 if (show_all_ == show_all)
256 show_all_ = show_all;
264 m->
data(m->
index(source_row, 2), Qt::CheckStateRole) != Qt::Checked)
267 const QRegExp regexp = filterRegExp();
268 if (regexp.isEmpty())
271 return m->
data(m->
index(source_row, 0, source_parent), Qt::DisplayRole).toString().contains(regexp) ||
272 m->
data(m->
index(source_row, 1, source_parent), Qt::DisplayRole).toString().contains(regexp);
278 if (left.userType() == QVariant::Type::Int)
280 return left.toInt() < right.toInt();
284 return left.toString() < right.toString();
290 int row_left = src_left.row();
291 int row_right = src_right.row();
292 QAbstractItemModel* m = sourceModel();
294 for (
int i = 0, end = sort_columns_.size(); i < end && sort_columns_[i] >= 0; ++i)
296 int sc = sort_columns_[i];
297 int role = sc == 2 ? Qt::CheckStateRole : Qt::DisplayRole;
298 QVariant value_left = m->data(m->index(row_left, sc), role);
299 QVariant value_right = m->data(m->index(row_right, sc), role);
301 if (value_left == value_right)
305 if (sort_orders_[i] == Qt::DescendingOrder)
322 int prev_idx = sort_columns_.indexOf(column);
324 prev_idx = sort_columns_.size() - 1;
326 sort_columns_.remove(prev_idx);
327 sort_orders_.remove(prev_idx);
329 sort_columns_.insert(0, column);
330 sort_orders_.insert(0, order);
332 QSortFilterProxyModel::sort(column, Qt::AscendingOrder);
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
DisabledReason reason(int row) const
bool setData(const QModelIndex &index, const QVariant &value, int role) override
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override
CollisionLinearModel(CollisionMatrixModel *src, QObject *parent=nullptr)
QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
Qt::ItemFlags flags(const QModelIndex &index) const override
int columnCount(const QModelIndex &parent) const override
int rowCount(const QModelIndex &parent) const override
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
~CollisionLinearModel() override
void setEnabled(const QItemSelection &selection, bool value)
QVariant data(const QModelIndex &index, int role) const override
QModelIndex parent(const QModelIndex &child) const override
bool lessThan(const QModelIndex &src_left, const QModelIndex &src_right) const override
void setEnabled(const QItemSelection &selection, bool value)
SortFilterProxyModel(QObject *parent=nullptr)
void sort(int column, Qt::SortOrder order) override
void setShowAll(bool show_all)
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
DisabledReason
Reasons for disabling link pairs. Append "in collision" for understanding. NOT_DISABLED means the lin...
bool compareVariants(const QVariant &left, const QVariant &right)