swift
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 
11 using namespace swift::misc;
12 
13 namespace swift::gui::models
14 {
15  int CListModelBaseNonTemplate::columnCount(const QModelIndex &modelIndex) const
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) { return QVariant(); }
25  const bool handled = (role == Qt::DisplayRole || role == Qt::ToolTipRole || role == Qt::InitialSortOrderRole);
26  if (!handled) { return QVariant(); }
27  if (section < 0 || section >= m_columns.size()) { return QVariant(); }
28 
29  if (role == Qt::DisplayRole) { return QVariant(m_columns.at(section).getColumnName()); }
30  if (role == Qt::ToolTipRole) { return QVariant(m_columns.at(section).getColumnToolTip()); }
31  return QVariant();
32  }
33 
34  QModelIndex CListModelBaseNonTemplate::index(int row, int column, const QModelIndex &parent) const
35  {
36  Q_UNUSED(parent)
37  return QStandardItemModel::createIndex(row, column);
38  }
39 
40  QModelIndex CListModelBaseNonTemplate::parent(const QModelIndex &child) const
41  {
42  Q_UNUSED(child)
43  return QModelIndex();
44  }
45 
46  CPropertyIndex CListModelBaseNonTemplate::columnToPropertyIndex(int column) const
47  {
48  return m_columns.columnToPropertyIndex(column);
49  }
50 
51  int CListModelBaseNonTemplate::propertyIndexToColumn(const CPropertyIndex &propertyIndex) const
52  {
53  return m_columns.propertyIndexToColumn(propertyIndex);
54  }
55 
56  CPropertyIndex CListModelBaseNonTemplate::modelIndexToPropertyIndex(const QModelIndex &index) const
57  {
58  return this->columnToPropertyIndex(index.column());
59  }
60 
61  void CListModelBaseNonTemplate::sortByPropertyIndex(const CPropertyIndex &propertyIndex, Qt::SortOrder order)
62  {
63  const int column = this->propertyIndexToColumn(propertyIndex);
64  this->sort(column, order);
65  }
66 
67  bool CListModelBaseNonTemplate::setSortColumnByPropertyIndex(const CPropertyIndex &propertyIndex)
68  {
69  const int column = m_columns.propertyIndexToColumn(propertyIndex);
70  if (m_sortColumn == column) { return false; } // not changed
71  m_sortColumn = column;
72  return true; // changed
73  }
74 
75  bool CListModelBaseNonTemplate::setSorting(const CPropertyIndex &propertyIndex, Qt::SortOrder order)
76  {
77  if (propertyIndex.isEmpty())
78  {
79  this->setNoSorting();
80  return false;
81  }
82 
83  const bool changedColumn = this->setSortColumnByPropertyIndex(propertyIndex);
84  const bool changedOrder = (m_sortOrder == order);
85  m_sortOrder = order;
86  return changedColumn || changedOrder;
87  }
88 
89  CPropertyIndex CListModelBaseNonTemplate::getSortProperty() const
90  {
91  if (!this->hasValidSortColumn()) { return CPropertyIndex::empty(); }
92  return m_columns.at(m_sortColumn).getPropertyIndex();
93  }
94 
95  bool CListModelBaseNonTemplate::hasValidSortColumn() const
96  {
97  if (!(m_sortColumn >= 0 && m_sortColumn < m_columns.size())) { return false; }
98  return m_columns.isSortable(m_sortColumn);
99  }
100 
101  Qt::ItemFlags CListModelBaseNonTemplate::flags(const QModelIndex &index) const
102  {
103  Qt::ItemFlags f = QStandardItemModel::flags(index);
104  if (!index.isValid()) { return f; }
105  const bool editable = m_columns.isEditable(index);
106  f = editable ? (f | Qt::ItemIsEditable) : (f & ~Qt::ItemIsEditable);
107 
108  // flags from formatter
109  const CDefaultFormatter *formatter = m_columns.getFormatter(index);
110  if (formatter) { f = formatter->flags(f, editable); }
111 
112  // drag and drop
113  f = f | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
114  return f;
115  }
116 
117  Qt::DropActions CListModelBaseNonTemplate::supportedDragActions() const
118  {
119  return isOrderable() ? Qt::CopyAction | Qt::MoveAction : Qt::CopyAction;
120  }
121 
122  Qt::DropActions CListModelBaseNonTemplate::supportedDropActions() const { return m_dropActions; }
123 
124  QStringList CListModelBaseNonTemplate::mimeTypes() const
125  {
126  static const QStringList mimes({ "application/swift.container.json" });
127  return mimes;
128  }
129 
130  void CListModelBaseNonTemplate::markDestroyed() { m_modelDestroyed = true; }
131 
132  bool CListModelBaseNonTemplate::isModelDestroyed() { return m_modelDestroyed; }
133 
134  void CListModelBaseNonTemplate::clearHighlighting()
135  {
136  // can be overridden to delete highlighting
137  }
138 
139  bool CListModelBaseNonTemplate::hasHighlightedRows() const
140  {
141  return false;
142  // can be overridden to enable highlighting based operations
143  }
144 
145  void CListModelBaseNonTemplate::emitDataChanged(int startRowIndex, int endRowIndex)
146  {
147  SWIFT_VERIFY_X(startRowIndex <= endRowIndex, Q_FUNC_INFO, "check rows");
148  SWIFT_VERIFY_X(startRowIndex >= 0 && endRowIndex >= 0, Q_FUNC_INFO, "check rows");
149 
150  const int columns = columnCount();
151  const int rows = rowCount();
152  if (columns < 1) { return; }
153  if (startRowIndex < 0) { startRowIndex = 0; }
154  if (endRowIndex >= rows) { endRowIndex = rows - 1; }
155  const QModelIndex topLeft(createIndex(startRowIndex, 0));
156  const QModelIndex bottomRight(createIndex(endRowIndex, columns - 1));
157  emit this->dataChanged(topLeft, bottomRight);
158  }
159 
160  CListModelBaseNonTemplate::CListModelBaseNonTemplate(const QString &translationContext, QObject *parent)
161  : QStandardItemModel(parent), m_columns(translationContext), m_sortColumn(-1), m_sortOrder(Qt::AscendingOrder)
162  {
163  // non unique default name, set translation context as default
164  this->setObjectName(translationContext);
165 
166  // connect
167  connect(this, &CListModelBaseNonTemplate::dataChanged, this, &CListModelBaseNonTemplate::onDataChanged);
168  }
169 
170 } // namespace swift::gui::models
Column formatter default implementation, also serving as interface.
virtual Qt::ItemFlags flags(Qt::ItemFlags flags, bool editable) const
Flags.
virtual void onDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector< int > &roles)=0
Feedback when QStandardItemModel::dataChanged was called.
bool isEmpty() const
Empty?
static const CPropertyIndex & empty()
an empty property index
Models to be used with views, mainly QTableView.
Free functions in swift::misc.
#define SWIFT_VERIFY_X(COND, WHERE, WHAT)
A weaker kind of assert.
Definition: verify.h:26