1 /**************************************************************************** 2 ** 3 ** DQt - D bindings for the Qt Toolkit 4 ** 5 ** GNU Lesser General Public License Usage 6 ** This file may be used under the terms of the GNU Lesser 7 ** General Public License version 3 as published by the Free Software 8 ** Foundation and appearing in the file LICENSE.LGPL3 included in the 9 ** packaging of this file. Please review the following information to 10 ** ensure the GNU Lesser General Public License version 3 requirements 11 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. 12 ** 13 ****************************************************************************/ 14 module qt.core.itemselectionmodel; 15 extern(C++): 16 17 import qt.config; 18 import qt.core.abstractitemmodel; 19 import qt.core.flags; 20 import qt.core.list; 21 import qt.core.object; 22 import qt.core.typeinfo; 23 import qt.helpers; 24 25 /+ QT_REQUIRE_CONFIG(itemmodel); +/ 26 27 28 @Q_MOVABLE_TYPE extern(C++, class) struct /+ Q_CORE_EXPORT +/ QItemSelectionRange 29 { 30 31 public: 32 @disable this(); 33 /+pragma(inline, true) this() 34 { 35 this.tl = typeof(this.tl)(); 36 this.br = typeof(this.br)(); 37 }+/ 38 /+ #if QT_VERSION < QT_VERSION_CHECK(6,0,0) +/ 39 // ### Qt 6: remove them all, the compiler-generated ones are fine 40 @disable this(this); 41 pragma(inline, true) this(ref const(QItemSelectionRange) other) 42 { 43 this.tl = other.tl; 44 this.br = other.br; 45 } 46 /+ QItemSelectionRange(QItemSelectionRange &&other) noexcept 47 : tl(std::move(other.tl)), br(std::move(other.br)) {} +/ 48 /+ QItemSelectionRange &operator=(QItemSelectionRange &&other) noexcept 49 { tl = std::move(other.tl); br = std::move(other.br); return *this; } +/ 50 /+ref QItemSelectionRange operator =(ref const(QItemSelectionRange) other) 51 { tl = other.tl; br = other.br; return this; }+/ 52 /+ #endif +/ // Qt < 6 53 this(ref const(QModelIndex) topL, ref const(QModelIndex) bottomR) 54 { 55 this.tl = topL; 56 this.br = bottomR; 57 } 58 /+ explicit +/this(ref const(QModelIndex) index) 59 { 60 this.tl = index; 61 this.br = tl; 62 } 63 64 /+ void swap(QItemSelectionRange &other) noexcept 65 { 66 qSwap(tl, other.tl); 67 qSwap(br, other.br); 68 } +/ 69 70 pragma(inline, true) int top() const { return tl.row(); } 71 pragma(inline, true) int left() const { return tl.column(); } 72 pragma(inline, true) int bottom() const { return br.row(); } 73 pragma(inline, true) int right() const { return br.column(); } 74 pragma(inline, true) int width() const { return br.column() - tl.column() + 1; } 75 pragma(inline, true) int height() const { return br.row() - tl.row() + 1; } 76 77 pragma(inline, true) ref const(QPersistentModelIndex) topLeft() const return { return tl; } 78 pragma(inline, true) ref const(QPersistentModelIndex) bottomRight() const return { return br; } 79 pragma(inline, true) QModelIndex parent() const { return tl.parent(); } 80 pragma(inline, true) const(QAbstractItemModel) model() const { return tl.model(); } 81 82 pragma(inline, true) bool contains(ref const(QModelIndex) index) const 83 { 84 return (parent() == index.parent() 85 && tl.row() <= index.row() && tl.column() <= index.column() 86 && br.row() >= index.row() && br.column() >= index.column()); 87 } 88 89 pragma(inline, true) bool contains(int row, int column, ref const(QModelIndex) parentIndex) const 90 { 91 return (parent() == parentIndex 92 && tl.row() <= row && tl.column() <= column 93 && br.row() >= row && br.column() >= column); 94 } 95 96 bool intersects(ref const(QItemSelectionRange) other) const; 97 /+ #if QT_DEPRECATED_SINCE(5, 0) 98 inline QItemSelectionRange intersect(const QItemSelectionRange &other) const 99 { return intersected(other); } 100 #endif +/ 101 QItemSelectionRange intersected(ref const(QItemSelectionRange) other) const; 102 103 104 /+pragma(inline, true) bool operator ==(ref const(QItemSelectionRange) other) const 105 { return (tl == other.tl && br == other.br); }+/ 106 /+pragma(inline, true) bool operator !=(ref const(QItemSelectionRange) other) const 107 { return !operator==(other); }+/ 108 /+ #if QT_DEPRECATED_SINCE(5, 15) +/ 109 /+/+ QT_DEPRECATED +/ bool operator <(ref const(QItemSelectionRange) other) const;+/ 110 /+ #endif +/ 111 112 pragma(inline, true) bool isValid() const 113 { 114 return (tl.isValid() && br.isValid() && tl.parent() == br.parent() 115 && top() <= bottom() && left() <= right()); 116 } 117 118 bool isEmpty() const; 119 120 QModelIndexList indexes() const; 121 122 private: 123 QPersistentModelIndex tl; QPersistentModelIndex br; 124 } 125 /+ Q_DECLARE_TYPEINFO(QItemSelectionRange, Q_MOVABLE_TYPE); 126 127 class QItemSelection; +/ 128 extern(C++, class) struct QItemSelectionModelPrivate; 129 130 class /+ Q_CORE_EXPORT +/ QItemSelectionModel : QObject 131 { 132 mixin(Q_OBJECT); 133 /+ Q_PROPERTY(QAbstractItemModel *model READ model WRITE setModel NOTIFY modelChanged) 134 Q_PROPERTY(bool hasSelection READ hasSelection NOTIFY selectionChanged STORED false DESIGNABLE false) 135 Q_PROPERTY(QModelIndex currentIndex READ currentIndex NOTIFY currentChanged STORED false DESIGNABLE false) 136 Q_PROPERTY(QItemSelection selection READ selection NOTIFY selectionChanged STORED false DESIGNABLE false) 137 Q_PROPERTY(QModelIndexList selectedIndexes READ selectedIndexes NOTIFY selectionChanged STORED false DESIGNABLE false) 138 139 Q_DECLARE_PRIVATE(QItemSelectionModel) +/ 140 141 public: 142 143 enum SelectionFlag { 144 NoUpdate = 0x0000, 145 Clear = 0x0001, 146 Select = 0x0002, 147 Deselect = 0x0004, 148 Toggle = 0x0008, 149 Current = 0x0010, 150 Rows = 0x0020, 151 Columns = 0x0040, 152 SelectCurrent = SelectionFlag.Select | SelectionFlag.Current, 153 ToggleCurrent = SelectionFlag.Toggle | SelectionFlag.Current, 154 ClearAndSelect = SelectionFlag.Clear | SelectionFlag.Select 155 } 156 157 /+ Q_DECLARE_FLAGS(SelectionFlags, SelectionFlag) +/ 158 alias SelectionFlags = QFlags!(SelectionFlag); /+ Q_FLAG(SelectionFlags) +/ 159 160 /+ explicit +/this(QAbstractItemModel model = null); 161 /+ explicit +/this(QAbstractItemModel model, QObject parent); 162 /+ virtual +/~this(); 163 164 final QModelIndex currentIndex() const; 165 166 @QInvokable final bool isSelected(ref const(QModelIndex) index) const; 167 @QInvokable final bool isRowSelected(int row, ref const(QModelIndex) parent = globalInitVar!QModelIndex) const; 168 @QInvokable final bool isColumnSelected(int column, ref const(QModelIndex) parent = globalInitVar!QModelIndex) const; 169 170 @QInvokable final bool rowIntersectsSelection(int row, ref const(QModelIndex) parent = globalInitVar!QModelIndex) const; 171 @QInvokable final bool columnIntersectsSelection(int column, ref const(QModelIndex) parent = globalInitVar!QModelIndex) const; 172 173 final bool hasSelection() const; 174 175 final QModelIndexList selectedIndexes() const; 176 @QInvokable final QModelIndexList selectedRows(int column = 0) const; 177 @QInvokable final QModelIndexList selectedColumns(int row = 0) const; 178 final const(QItemSelection) selection() const; 179 180 // ### Qt 6: Merge these two as "QAbstractItemModel *model() const" 181 final const(QAbstractItemModel) model() const; 182 final QAbstractItemModel model(); 183 184 final void setModel(QAbstractItemModel model); 185 186 public /+ Q_SLOTS +/: 187 /+ virtual +/ @QSlot void setCurrentIndex(ref const(QModelIndex) index, QItemSelectionModel.SelectionFlags command); 188 /+ virtual +/ @QSlot void select(ref const(QModelIndex) index, QItemSelectionModel.SelectionFlags command); 189 /+ virtual +/ @QSlot void select(ref const(QItemSelection) selection, QItemSelectionModel.SelectionFlags command); 190 /+ virtual +/ @QSlot void clear(); 191 /+ virtual +/ @QSlot void reset(); 192 193 @QSlot final void clearSelection(); 194 /+ virtual +/ @QSlot void clearCurrentIndex(); 195 196 /+ Q_SIGNALS +/public: 197 @QSignal final void selectionChanged(ref const(QItemSelection) selected, ref const(QItemSelection) deselected); 198 @QSignal final void currentChanged(ref const(QModelIndex) current, ref const(QModelIndex) previous); 199 @QSignal final void currentRowChanged(ref const(QModelIndex) current, ref const(QModelIndex) previous); 200 @QSignal final void currentColumnChanged(ref const(QModelIndex) current, ref const(QModelIndex) previous); 201 @QSignal final void modelChanged(QAbstractItemModel model); 202 203 protected: 204 this(ref QItemSelectionModelPrivate dd, QAbstractItemModel model); 205 final void emitSelectionChanged(ref const(QItemSelection) newSelection, ref const(QItemSelection) oldSelection); 206 207 private: 208 /+ Q_DISABLE_COPY(QItemSelectionModel) +/ 209 /+ Q_PRIVATE_SLOT(d_func(), void _q_columnsAboutToBeRemoved(const QModelIndex&, int, int)) 210 Q_PRIVATE_SLOT(d_func(), void _q_rowsAboutToBeRemoved(const QModelIndex&, int, int)) 211 Q_PRIVATE_SLOT(d_func(), void _q_columnsAboutToBeInserted(const QModelIndex&, int, int)) 212 Q_PRIVATE_SLOT(d_func(), void _q_rowsAboutToBeInserted(const QModelIndex&, int, int)) 213 Q_PRIVATE_SLOT(d_func(), void _q_layoutAboutToBeChanged(const QList<QPersistentModelIndex> &parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoHint)) 214 Q_PRIVATE_SLOT(d_func(), void _q_layoutChanged(const QList<QPersistentModelIndex> &parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoHint)) +/ 215 } 216 /+pragma(inline, true) QFlags!(QItemSelectionModel.SelectionFlags.enum_type) operator |(QItemSelectionModel.SelectionFlags.enum_type f1, QItemSelectionModel.SelectionFlags.enum_type f2)/+noexcept+/{return QFlags!(QItemSelectionModel.SelectionFlags.enum_type)(f1)|f2;}+/ 217 /+pragma(inline, true) QFlags!(QItemSelectionModel.SelectionFlags.enum_type) operator |(QItemSelectionModel.SelectionFlags.enum_type f1, QFlags!(QItemSelectionModel.SelectionFlags.enum_type) f2)/+noexcept+/{return f2|f1;}+/ 218 /+pragma(inline, true) QIncompatibleFlag operator |(QItemSelectionModel.SelectionFlags.enum_type f1, int f2)/+noexcept+/{return QIncompatibleFlag(int(f1)|f2);}+/ 219 220 /+ Q_DECLARE_OPERATORS_FOR_FLAGS(QItemSelectionModel::SelectionFlags) 221 // dummy implentation of qHash() necessary for instantiating QList<QItemSelectionRange>::toSet() with MSVC 222 inline uint qHash(const QItemSelectionRange &) { return 0; } 223 224 #ifdef Q_CC_MSVC 225 226 /* 227 ### Qt 6: 228 ### This needs to be removed for next releases of Qt. It is a workaround for vc++ because 229 ### Qt exports QItemSelection that inherits QList<QItemSelectionRange>. 230 */ 231 232 # ifndef Q_TEMPLATE_EXTERN 233 # if defined(QT_BUILD_CORE_LIB) 234 # define Q_TEMPLATE_EXTERN 235 # else 236 # define Q_TEMPLATE_EXTERN extern 237 # endif 238 # endif 239 Q_TEMPLATE_EXTERN template class Q_CORE_EXPORT QList<QItemSelectionRange>; 240 #endif +/ // Q_CC_MSVC 241 242 extern(C++, class) struct /+ Q_CORE_EXPORT +/ QItemSelection 243 { 244 public QList!(QItemSelectionRange) base0; 245 alias base0 this; 246 public: 247 @disable this(); 248 /+this()/+ noexcept+/ 249 { 250 this.QList!(QItemSelectionRange) = typeof(this.QList!(QItemSelectionRange))(); 251 }+/ 252 this(ref const(QModelIndex) topLeft, ref const(QModelIndex) bottomRight); 253 254 // reusing QList::swap() here is OK! 255 256 void select(ref const(QModelIndex) topLeft, ref const(QModelIndex) bottomRight); 257 bool contains(ref const(QModelIndex) index) const; 258 QModelIndexList indexes() const; 259 void merge(ref const(QItemSelection) other, QItemSelectionModel.SelectionFlags command); 260 static void split(ref const(QItemSelectionRange) range, 261 ref const(QItemSelectionRange) other, 262 QItemSelection* result); 263 } 264 /+ Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QItemSelection) 265 266 #ifndef QT_NO_DEBUG_STREAM 267 Q_CORE_EXPORT QDebug operator<<(QDebug, const QItemSelectionRange &); 268 #endif 269 270 271 Q_DECLARE_METATYPE(QItemSelectionRange) 272 Q_DECLARE_METATYPE(QItemSelection) +/ 273