swift
Loading...
Searching...
No Matches
listmodelbasenontemplate.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2018 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
4// Drag and drop docu:
5// http://doc.qt.io/qt-5/model-view-programming.html#using-drag-and-drop-with-item-views
6
8
9#include "misc/verify.h"
10
11using namespace swift::misc;
12
13namespace swift::gui::models
14{
16 {
17 Q_UNUSED(modelIndex)
18 int c = m_columns.size();
19 return c;
20 }
21
22 QVariant CListModelBaseNonTemplate::headerData(int section, Qt::Orientation orientation, int role) const
23 {
24 if (orientation != Qt::Horizontal)
25 {
26 return {};
27 }
28 const bool handled = (role == Qt::DisplayRole || role == Qt::ToolTipRole || role == Qt::InitialSortOrderRole);
29 if (!handled)
30 {
31 return {};
32 }
33 if (section < 0 || section >= m_columns.size())
34 {
35 return {};
36 }
37
38 if (role == Qt::DisplayRole)
39 {
40 return { m_columns.at(section).getColumnName() };
41 }
42 if (role == Qt::ToolTipRole)
43 {
44 return { m_columns.at(section).getColumnToolTip() };
45 }
46 return {};
47 }
48
50 {
51 Q_UNUSED(parent)
52 return QStandardItemModel::createIndex(row, column);
53 }
54
56 {
57 Q_UNUSED(child)
58 return {};
59 }
60
62 {
63 return m_columns.columnToPropertyIndex(column);
64 }
65
67 {
68 return m_columns.propertyIndexToColumn(propertyIndex);
69 }
70
75
77 {
78 const int column = this->propertyIndexToColumn(propertyIndex);
79 this->sort(column, order);
80 }
81
83 {
84 const int column = m_columns.propertyIndexToColumn(propertyIndex);
85 if (m_sortColumn == column)
86 {
87 return false;
88 } // not changed
89 m_sortColumn = column;
90 return true; // changed
91 }
92
94 {
95 if (propertyIndex.isEmpty())
96 {
97 this->setNoSorting();
98 return false;
99 }
100
101 const bool changedColumn = this->setSortColumnByPropertyIndex(propertyIndex);
102 const bool changedOrder = (m_sortOrder == order);
103 m_sortOrder = order;
104 return changedColumn || changedOrder;
105 }
106
108 {
109 if (!this->hasValidSortColumn())
110 {
111 return CPropertyIndex::empty();
112 }
113 return m_columns.at(m_sortColumn).getPropertyIndex();
114 }
115
117 {
119 {
120 return false;
121 }
122 return m_columns.isSortable(m_sortColumn);
123 }
124
126 {
128 if (!index.isValid())
129 {
130 return f;
131 }
132 const bool editable = m_columns.isEditable(index);
133 f = editable ? (f | Qt::ItemIsEditable) : (f & ~Qt::ItemIsEditable);
134
135 // flags from formatter
136 const CDefaultFormatter *formatter = m_columns.getFormatter(index);
137 if (formatter)
138 {
139 f = formatter->flags(f, editable);
140 }
141
142 // drag and drop
144 return f;
145 }
146
151
153
155 {
156 static const QStringList mimes({ "application/swift.container.json" });
157 return mimes;
158 }
159
161
163
165 {
166 // can be overridden to delete highlighting
167 }
168
170 {
171 return false;
172 // can be overridden to enable highlighting based operations
173 }
174
175 void CListModelBaseNonTemplate::emitDataChanged(int startRowIndex, int endRowIndex)
176 {
177 SWIFT_VERIFY_X(startRowIndex <= endRowIndex, Q_FUNC_INFO, "check rows");
178 SWIFT_VERIFY_X(startRowIndex >= 0 && endRowIndex >= 0, Q_FUNC_INFO, "check rows");
179
180 const int columns = columnCount();
181 const int rows = rowCount();
182 if (columns < 1)
183 {
184 return;
185 }
186 startRowIndex = std::max(startRowIndex, 0);
187 if (endRowIndex >= rows)
188 {
189 endRowIndex = rows - 1;
190 }
191 const QModelIndex topLeft(createIndex(startRowIndex, 0));
192 const QModelIndex bottomRight(createIndex(endRowIndex, columns - 1));
193 emit this->dataChanged(topLeft, bottomRight);
194 }
195
197 : QStandardItemModel(parent), m_columns(translationContext)
198 {
199 // non unique default name, set translation context as default
200 this->setObjectName(translationContext);
201
202 // connect
204 }
205
206} // namespace swift::gui::models
Column formatter default implementation, also serving as interface.
virtual Qt::ItemFlags flags(Qt::ItemFlags flags, bool editable) const
Flags.
virtual bool hasHighlightedRows() const
Has highlighted rows?
Qt::ItemFlags flags(const QModelIndex &index) const final
virtual void clearHighlighting()
Remove all highlighting.
virtual void onDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector< int > &roles)=0
Feedback when QStandardItemModel::dataChanged was called.
virtual bool setSorting(const swift::misc::CPropertyIndex &propertyIndex, Qt::SortOrder order=Qt::AscendingOrder)
Sorting.
virtual bool setSortColumnByPropertyIndex(const swift::misc::CPropertyIndex &propertyIndex)
Set column for sorting.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const final
QModelIndex parent(const QModelIndex &child) const final
virtual swift::misc::CPropertyIndex columnToPropertyIndex(int column) const
Column to property index.
void markDestroyed()
Mark as about to be destroyed, normally marked from view.
CListModelBaseNonTemplate(const QString &translationContext, QObject *parent=nullptr)
Constructor.
QVariant headerData(int section, Qt::Orientation orientation, int role) const final
swift::misc::CPropertyIndex getSortProperty() const
Sorted by property.
virtual bool isOrderable() const =0
Orderable, normally use a container swift::misc::IOrderableList.
void sortByPropertyIndex(const swift::misc::CPropertyIndex &propertyIndex, Qt::SortOrder order=Qt::AscendingOrder)
Sort by index.
void emitDataChanged(int startRowIndex, int endRowIndex)
Send signal that data have been changed.
virtual swift::misc::CPropertyIndex modelIndexToPropertyIndex(const QModelIndex &index) const
Index to property index.
virtual int propertyIndexToColumn(const swift::misc::CPropertyIndex &propertyIndex) const
Property index to column number.
virtual bool hasValidSortColumn() const
Has valid sort column?
int columnCount(const QModelIndex &modelIndex=QModelIndex()) const final
static const CPropertyIndex & empty()
an empty property index
Models to be used with views, mainly QTableView.
Free functions in swift::misc.
QModelIndex createIndex(int row, int column, const void *ptr) const const
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
int column() const const
QObject(QObject *parent)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
void setObjectName(QAnyStringView name)
QStandardItemModel(QObject *parent)
virtual Qt::ItemFlags flags(const QModelIndex &index) const const override
virtual int rowCount(const QModelIndex &parent) const const override
virtual void sort(int column, Qt::SortOrder order) override
typedef DropActions
DisplayRole
typedef ItemFlags
Orientation
SortOrder
#define SWIFT_VERIFY_X(COND, WHERE, WHAT)
A weaker kind of assert.
Definition verify.h:26