40 #include <QItemSelection>
60 int r = sourceIndex.row(),
c = sourceIndex.column();
61 int n = this->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 = this->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)
112 return this->sourceModel()->headerData(src_index.row(), Qt::Horizontal, Qt::DisplayRole);
114 if (role != Qt::DisplayRole)
116 return this->sourceModel()->headerData(src_index.column(), Qt::Vertical, Qt::DisplayRole);
118 if (role != Qt::CheckStateRole)
121 return this->sourceModel()->data(src_index, Qt::CheckStateRole);
123 if (role != Qt::DisplayRole)
126 return this->sourceModel()->data(src_index, Qt::ToolTipRole);
134 return qobject_cast<CollisionMatrixModel*>(sourceModel())->reason(src_index);
141 if (role == Qt::CheckStateRole)
143 sourceModel()->setData(src_index, value, role);
145 Q_EMIT dataChanged(this->
index(r, 2), this->
index(r, 3));
153 for (
const auto idx : selection.indexes())
155 if (idx.column() != 2)
157 setData(idx, value ? Qt::Checked : Qt::Unchecked, Qt::CheckStateRole);
163 if (
index.column() == 2)
164 return Qt::ItemIsUserCheckable | QAbstractItemModel::flags(
index);
166 return QAbstractItemModel::flags(
index);
171 if (role != Qt::DisplayRole)
174 if (orientation == Qt::Horizontal)
185 return "Reason to Disable";
188 else if (orientation == Qt::Vertical)
197 #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
198 connect(
this, SIGNAL(sourceModelChanged()),
this, SLOT(initSorting()));
202 sort_columns_ << 0 << 1;
203 sort_orders_ << Qt::AscendingOrder << Qt::AscendingOrder;
208 if (role == Qt::DisplayRole && orientation == Qt::Vertical)
211 return QSortFilterProxyModel::headerData(section, orientation, role);
219 void SortFilterProxyModel::initSorting()
221 int cols = sourceModel()->columnCount();
222 int prev_size = sort_columns_.size();
223 sort_columns_.resize(cols);
224 sort_orders_.resize(cols);
227 for (
int i = prev_size, end = sort_columns_.size(); i < end; ++i)
228 sort_columns_[i] = -1;
233 if (show_all_ == show_all)
236 show_all_ = show_all;
244 m->
data(m->
index(source_row, 2), Qt::CheckStateRole) == Qt::Checked))
247 const QRegExp regexp = this->filterRegExp();
248 if (regexp.isEmpty())
251 return m->
data(m->
index(source_row, 0, source_parent), Qt::DisplayRole).toString().contains(regexp) ||
252 m->
data(m->
index(source_row, 1, source_parent), Qt::DisplayRole).toString().contains(regexp);
258 if (left.userType() == QVariant::Type::Int)
259 return left.toInt() < right.toInt();
261 return left.toString() < right.toString();
266 int row_left = src_left.row();
267 int row_right = src_right.row();
268 QAbstractItemModel* m = sourceModel();
270 for (
int i = 0, end = sort_columns_.size(); i < end && sort_columns_[i] >= 0; ++i)
272 int sc = sort_columns_[i];
273 int role = sc == 2 ? Qt::CheckStateRole : Qt::DisplayRole;
274 QVariant value_left = m->data(m->index(row_left, sc), role);
275 QVariant value_right = m->data(m->index(row_right, sc), role);
277 if (value_left == value_right)
281 if (sort_orders_[i] == Qt::DescendingOrder)
296 int prev_idx = sort_columns_.indexOf(column);
298 prev_idx = sort_columns_.size() - 1;
300 sort_columns_.remove(prev_idx);
301 sort_orders_.remove(prev_idx);
303 sort_columns_.insert(0, column);
304 sort_orders_.insert(0, order);
306 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)