1 module examplewidgets.tabtreewidget;
2 
3 import qt.config;
4 import qt.core.point;
5 import qt.core.string;
6 import qt.helpers;
7 import qt.widgets.ui;
8 import qt.widgets.widget;
9 
10 class TabTreeWidget : QWidget
11 {
12     mixin(Q_OBJECT_D);
13 
14 public:
15     /+ explicit +/this(QWidget parent = null)
16     {
17         import core.stdcpp.new_;
18         super(parent);
19         this.ui = cpp_new!(typeof(*ui));
20 
21         ui.setupUi(this);
22         ui.treeWidget.setColumnWidth(0, 200);
23     }
24     ~this()
25     {
26         import core.stdcpp.new_;
27 
28         cpp_delete(ui);
29     }
30 
31 private /+ slots +/:
32     @QSlot final void on_treeWidget_customContextMenuRequested(ref const(QPoint) pos)
33     {
34         import core.stdcpp.new_;
35         import qt.core.namespace;
36         import qt.gui.brush;
37         import qt.gui.color;
38         import qt.gui.font;
39         import qt.widgets.colordialog;
40         import qt.widgets.fontdialog;
41         import qt.widgets.menu;
42         import qt.widgets.treewidget;
43         import qt.widgets.action;
44 
45         QTreeWidgetItem item = ui.treeWidget.itemAt(pos);
46         QMenu menu = cpp_new!QMenu;
47         if(item)
48         {
49             QAction action = menu.addAction("Selected: " ~ item.text(0));
50             action.setEnabled(false);
51 
52             action = menu.addAction(QString("Remove"));
53             connect(action.signal!"triggered", menu, (){
54                 cpp_delete(item);
55             });
56 
57             action = menu.addAction(QString("Add child"));
58             connect(action.signal!"triggered", menu, (){
59                 QTreeWidgetItem child = cpp_new!QTreeWidgetItem();
60                 child.setText(0, QString("New child"));
61                 child.setFlags(child.flags() | /+ Qt:: +/qt.core.namespace.ItemFlag.ItemIsEditable);
62                 item.addChild(child);
63                 ui.treeWidget.setCurrentItem(child);
64                 ui.treeWidget.editItem(child, 0);
65             });
66 
67             action = menu.addAction(QString("Add sibling before"));
68             connect(action.signal!"triggered", menu, (){
69                 QTreeWidgetItem sibling = cpp_new!QTreeWidgetItem();
70                 sibling.setText(0, QString("New sibling"));
71                 sibling.setFlags(sibling.flags() | /+ Qt:: +/qt.core.namespace.ItemFlag.ItemIsEditable);
72                 if(item.parent())
73                     item.parent().insertChild(item.parent().indexOfChild(item), sibling);
74                 else
75                     ui.treeWidget.insertTopLevelItem(ui.treeWidget.indexOfTopLevelItem(item), sibling);
76                 ui.treeWidget.setCurrentItem(sibling);
77                 ui.treeWidget.editItem(sibling, 0);
78             });
79 
80             action = menu.addAction(QString("Add sibling after"));
81             connect(action.signal!"triggered", menu, (){
82                 QTreeWidgetItem sibling = cpp_new!QTreeWidgetItem();
83                 sibling.setText(0, QString("New sibling"));
84                 sibling.setFlags(sibling.flags() | /+ Qt:: +/qt.core.namespace.ItemFlag.ItemIsEditable);
85                 if(item.parent())
86                     item.parent().insertChild(item.parent().indexOfChild(item) + 1, sibling);
87                 else
88                     ui.treeWidget.insertTopLevelItem(ui.treeWidget.indexOfTopLevelItem(item) + 1, sibling);
89                 ui.treeWidget.setCurrentItem(sibling);
90                 ui.treeWidget.editItem(sibling, 0);
91             });
92 
93             action = menu.addAction(QString("Change font"));
94             connect(action.signal!"triggered", menu, (){
95                 QFont font = item.font(0);
96                 bool ok;
97                 font = QFontDialog.getFont(&ok, font, this);
98                 if(ok)
99                 {
100                     item.setFont(0, font);
101                     item.setFont(1, font);
102                 }
103             });
104 
105             action = menu.addAction(QString("Change background color"));
106             connect(action.signal!"triggered", menu, (){
107                 QBrush brush = item.background(0);
108                 QColor color = QColorDialog.getColor(brush.color(), this);
109                 if(color.isValid())
110                 {
111                     brush.setColor(color);
112                     brush = QBrush(color);
113                     item.setBackground(0, brush);
114                     item.setBackground(1, brush);
115                 }
116             });
117         }
118         else
119         {
120             QAction action = menu.addAction(QString("No item selected"));
121             action.setEnabled(false);
122 
123             action = menu.addAction(QString("Add top level item"));
124             connect(action.signal!"triggered", menu, (){
125                 QTreeWidgetItem item2 = cpp_new!QTreeWidgetItem();
126                 item2.setText(0, QString("New item"));
127                 item2.setFlags(item2.flags() | /+ Qt:: +/qt.core.namespace.ItemFlag.ItemIsEditable);
128                     ui.treeWidget.addTopLevelItem(item2);
129                 ui.treeWidget.setCurrentItem(item2);
130                 ui.treeWidget.editItem(item2, 0);
131             });
132         }
133 
134         menu.exec(ui.treeWidget.mapToGlobal(pos));
135         cpp_delete(menu);
136     }
137 
138 private:
139     UIStruct!"tabtreewidget.ui"* ui;
140 }
141