1 module examplewidgets.tabtableview;
2 
3 import qt.config;
4 import qt.core.abstractitemmodel;
5 import qt.core.map;
6 import qt.core.namespace;
7 import qt.core.pair;
8 import qt.core.string;
9 import qt.core.variant;
10 import qt.core.vector;
11 import qt.helpers;
12 import qt.widgets.ui;
13 import qt.widgets.widget;
14 
15 class SimpleTableModel : QAbstractItemModel
16 {
17 public:
18     /+ explicit +/this()
19     {
20     }
21     ~this()
22     {
23     }
24 
25     extern(C++) override int columnCount(ref const(QModelIndex) parent = globalInitVar!QModelIndex) const
26     {
27         return 256;
28     }
29     extern(C++) override int rowCount(ref const(QModelIndex) parent = globalInitVar!QModelIndex) const
30     {
31         return 65536;
32     }
33     extern(C++) override QVariant data(ref const(QModelIndex) index, int role = /+ Qt:: +/qt.core.namespace.ItemDataRole.DisplayRole) const
34     {
35         if (!index.isValid())
36             return QVariant();
37 
38         if (role != /+ Qt:: +/qt.core.namespace.ItemDataRole.DisplayRole && role != /+ Qt:: +/qt.core.namespace.ItemDataRole.EditRole)
39             return QVariant();
40 
41         int[2] key = [index.column(), index.row()];
42         if(key in content)
43             return QVariant(content[key]);
44 
45         return QVariant(QString(""));
46     }
47     extern(C++) override /+ Qt:: +/qt.core.namespace.ItemFlags flags(ref const(QModelIndex) index) const
48     {
49         if (!index.isValid())
50             return /+ Qt:: +/qt.core.namespace.ItemFlags.NoItemFlags;
51 
52         return QAbstractItemModel.flags(index) | /+ Qt:: +/qt.core.namespace.ItemFlag.ItemIsEditable;
53     }
54     extern(C++) override bool setData(ref const(QModelIndex) index, ref const(QVariant) value, int role)
55     {
56         if (role != /+ Qt:: +/qt.core.namespace.ItemDataRole.EditRole)
57             return false;
58 
59         int[2] key = [index.column(), index.row()];
60         content[key] = value.toString();
61         QVector!(int) roles = QVector!(int).create();
62         roles.append(qt.core.namespace.ItemDataRole.DisplayRole);
63         roles.append(qt.core.namespace.ItemDataRole.EditRole);
64         /+ emit +/ dataChanged(index, index, roles);
65 
66         return true;
67     }
68     extern(C++) override QModelIndex index(int row, int column, ref const(QModelIndex) parent = globalInitVar!QModelIndex) const
69     {
70         if (parent.isValid())
71             return QModelIndex();
72 
73         return createIndex(row, column, null);
74     }
75     extern(C++) override QModelIndex parent(ref const(QModelIndex) index) const
76     {
77         return QModelIndex();
78     }
79     extern(C++) override QVariant headerData(int section, /+ Qt:: +/qt.core.namespace.Orientation orientation, int role) const
80     {
81         if (orientation == /+ Qt:: +/qt.core.namespace.Orientation.Horizontal && role == /+ Qt:: +/qt.core.namespace.ItemDataRole.DisplayRole) {
82             char[3] text;
83             if(section < 26)
84             {
85                 text[0] = cast(char)('A' + section);
86                 text[1] = 0;
87                 return QVariant(QString.fromUtf8(text.ptr));
88             }
89             if(section - 26 < 26 * 26)
90             {
91                 text[0] = cast(char)('A' + (section - 26) / 26);
92                 text[1] = cast(char)('A' + (section - 26) % 26);
93                 text[2] = 0;
94                 return QVariant(QString.fromUtf8(text.ptr));
95             }
96         }
97 
98         if (orientation == /+ Qt:: +/qt.core.namespace.Orientation.Vertical && role == /+ Qt:: +/qt.core.namespace.ItemDataRole.DisplayRole) {
99             return QVariant(QString.number(section + 1));
100         }
101 
102         return QVariant();
103     }
104 
105 private:
106     QString[int[2]] content;
107 }
108 
109 class TabTableView : QWidget
110 {
111     mixin(Q_OBJECT_D);
112 
113 public:
114     /+ explicit +/this(QWidget parent = null)
115     {
116         import core.stdcpp.new_;
117         super(parent);
118         this.ui = cpp_new!(typeof(*ui));
119 
120         ui.setupUi(this);
121         model = cpp_new!SimpleTableModel();
122         ui.tableView.setModel(model);
123     }
124     ~this()
125     {
126         import core.stdcpp.new_;
127 
128         cpp_delete(ui);
129         cpp_delete(model);
130     }
131 
132 private:
133     UIStruct!"tabtableview.ui"* ui;
134     SimpleTableModel model;
135 }
136