1 module examplewidgets.tabtreeview; 2 3 import qt.config; 4 import qt.core.abstractitemmodel; 5 import qt.core.namespace; 6 import qt.core.string; 7 import qt.core.variant; 8 import qt.core.vector; 9 import qt.helpers; 10 import qt.widgets.ui; 11 import qt.widgets.widget; 12 13 extern(C++, class) struct RandomTreeItem 14 { 15 public: 16 ~this() 17 { 18 import qt.core.algorithms; 19 20 qDeleteAll(childs); 21 } 22 this(int dummy) 23 { 24 name = QString.create; 25 desc = QString.create; 26 childs = QVector!(RandomTreeItem*).create(); 27 } 28 QString name; 29 QString desc; 30 QVector!(RandomTreeItem*) childs; 31 RandomTreeItem* parent = null; 32 int row = 0; 33 } 34 35 class RandomTreeModel : QAbstractItemModel 36 { 37 public: 38 /+ explicit +/this() 39 { 40 root = createItem(null, 0); 41 } 42 ~this() 43 { 44 import core.stdcpp.new_; 45 46 cpp_delete(root); 47 } 48 49 final RandomTreeItem* getItem(ref const(QModelIndex) index) const 50 { 51 if (index.isValid()) { 52 RandomTreeItem* parentItem = static_cast!(RandomTreeItem*)(index.internalPointer()); 53 if (parentItem) { 54 int row = index.row(); 55 if(row >= 0 && row < parentItem.childs.length()) { 56 if(!parentItem.childs[row]) 57 parentItem.childs[row] = createItem(parentItem, row); 58 return parentItem.childs[row]; 59 } 60 } 61 } 62 return cast(RandomTreeItem*)root; 63 } 64 extern(C++) override int columnCount(ref const(QModelIndex) parent = globalInitVar!QModelIndex) const 65 { 66 return 2; 67 } 68 extern(C++) override int rowCount(ref const(QModelIndex) parent = globalInitVar!QModelIndex) const 69 { 70 RandomTreeItem* item = getItem(parent); 71 if(item) 72 return item.childs.length(); 73 return 0; 74 } 75 extern(C++) override QVariant data(ref const(QModelIndex) index, int role = /+ Qt:: +/qt.core.namespace.ItemDataRole.DisplayRole) const 76 { 77 if (!index.isValid()) 78 return QVariant(); 79 80 if (role != /+ Qt:: +/qt.core.namespace.ItemDataRole.DisplayRole && role != /+ Qt:: +/qt.core.namespace.ItemDataRole.EditRole) 81 return QVariant(); 82 83 RandomTreeItem* item = getItem(index); 84 if(index.column()) 85 return QVariant(item.desc); 86 else 87 return QVariant(item.name); 88 } 89 extern(C++) override /+ Qt:: +/qt.core.namespace.ItemFlags flags(ref const(QModelIndex) index) const 90 { 91 if (!index.isValid()) 92 return /+ Qt:: +/qt.core.namespace.ItemFlags.NoItemFlags; 93 94 /+ Qt:: +/qt.core.namespace.ItemFlags r = QAbstractItemModel.flags(index); 95 if (index.column() == 1) 96 r |= /+ Qt:: +/qt.core.namespace.ItemFlag.ItemIsEditable; 97 return r; 98 } 99 extern(C++) override bool setData(ref const(QModelIndex) index, ref const(QVariant) value, int role) 100 { 101 if (role != /+ Qt:: +/qt.core.namespace.ItemDataRole.EditRole) 102 return false; 103 104 RandomTreeItem* item = getItem(index); 105 if (item && index.column() == 1) { 106 item.desc = value.toString(); 107 QVector!(int) roles = QVector!(int).create(); 108 roles.append(qt.core.namespace.ItemDataRole.DisplayRole); 109 roles.append(qt.core.namespace.ItemDataRole.EditRole); 110 /+ emit +/ dataChanged(index, index, roles); 111 return true; 112 } 113 114 return false; 115 } 116 extern(C++) override QModelIndex index(int row, int column, ref const(QModelIndex) parent = globalInitVar!QModelIndex) const 117 { 118 if (parent.isValid() && parent.column() != 0) 119 return QModelIndex(); 120 121 RandomTreeItem* parentItem = getItem(parent); 122 if (!parentItem) 123 return QModelIndex(); 124 125 return createIndex(row, column, parentItem); 126 } 127 extern(C++) override QModelIndex parent(ref const(QModelIndex) index) const 128 { 129 if (!index.isValid()) 130 return QModelIndex(); 131 RandomTreeItem* parentItem = static_cast!(RandomTreeItem*)(index.internalPointer()); 132 if(parentItem) 133 return createIndex(parentItem.row, 0, parentItem.parent); 134 return QModelIndex(); 135 } 136 extern(C++) override QVariant headerData(int section, /+ Qt:: +/qt.core.namespace.Orientation orientation, int role) const 137 { 138 if (orientation == /+ Qt:: +/qt.core.namespace.Orientation.Horizontal && role == /+ Qt:: +/qt.core.namespace.ItemDataRole.DisplayRole) { 139 if(section == 0) 140 return cast(QVariant)(QString("Name")); 141 else if(section == 1) 142 return cast(QVariant)(QString("Description")); 143 } 144 145 return QVariant(); 146 } 147 148 private: 149 RandomTreeItem* root; 150 151 final RandomTreeItem* createItem(RandomTreeItem* parent, int row) const 152 { 153 import core.stdcpp.new_; 154 import core.stdc.stdlib; 155 156 RandomTreeItem* item = cpp_new!RandomTreeItem(0); 157 item.parent = parent; 158 item.row = row; 159 item.childs.resize(5); 160 item.name = QString.number(rand()); 161 return item; 162 } 163 } 164 165 class TabTreeView : QWidget 166 { 167 mixin(Q_OBJECT_D); 168 169 public: 170 /+ explicit +/this(QWidget parent = null) 171 { 172 import core.stdcpp.new_; 173 super(parent); 174 this.ui = cpp_new!(typeof(*ui)); 175 176 ui.setupUi(this); 177 model = cpp_new!RandomTreeModel(); 178 ui.treeView.setModel(model); 179 ui.treeView.setColumnWidth(0, 200); 180 } 181 ~this() 182 { 183 import core.stdcpp.new_; 184 185 cpp_delete(ui); 186 cpp_delete(model); 187 } 188 189 private: 190 UIStruct!"tabtreeview.ui"* ui; 191 RandomTreeModel model; 192 } 193