1 module examplewidgets.tabtablewidget; 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 TabTableWidget : 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 23 ui.tableWidget.setColumnCount(3); 24 ui.tableWidget.setRowCount(5); 25 } 26 ~this() 27 { 28 import core.stdcpp.new_; 29 30 cpp_delete(ui); 31 } 32 33 private /+ slots +/: 34 @QSlot final void on_tableWidget_customContextMenuRequested(ref const(QPoint) pos) 35 { 36 import core.stdcpp.new_; 37 import qt.gui.brush; 38 import qt.gui.color; 39 import qt.gui.font; 40 import qt.widgets.colordialog; 41 import qt.widgets.fontdialog; 42 import qt.widgets.inputdialog; 43 import qt.widgets.lineedit; 44 import qt.widgets.menu; 45 import qt.widgets.tablewidget; 46 import qt.widgets.action; 47 48 int currentRow = ui.tableWidget.rowAt(pos.y()); 49 int currentColumn = ui.tableWidget.columnAt(pos.x()); 50 QMenu menu = cpp_new!QMenu; 51 if(currentRow >= 0) 52 { 53 QAction action = menu.addAction("Current row = " ~ QString.number(currentRow)); 54 action.setEnabled(false); 55 56 action = menu.addAction(QString("Remove row")); 57 connect(action.signal!"triggered", menu, (){ 58 ui.tableWidget.removeRow(currentRow); 59 }); 60 61 action = menu.addAction(QString("Add row before")); 62 connect(action.signal!"triggered", menu, (){ 63 ui.tableWidget.insertRow(currentRow); 64 }); 65 66 action = menu.addAction(QString("Add row after")); 67 connect(action.signal!"triggered", menu, (){ 68 ui.tableWidget.insertRow(currentRow + 1); 69 }); 70 71 action = menu.addAction(QString("Change row header")); 72 connect(action.signal!"triggered", menu, (){ 73 QTableWidgetItem item = ui.tableWidget.verticalHeaderItem(currentRow); 74 bool ok; 75 QString text = QString.create; 76 if(item) 77 text = item.text(); 78 text = QInputDialog.getText(this, QString.create(), QString("Row header"), QLineEdit.EchoMode.Normal, text, &ok); 79 if(ok) 80 { 81 if(text.isEmpty()) 82 { 83 if(item) 84 cpp_delete(item); 85 } 86 else 87 { 88 if(!item) 89 { 90 item = cpp_new!QTableWidgetItem; 91 ui.tableWidget.setVerticalHeaderItem(currentRow, item); 92 } 93 item.setText(text); 94 } 95 } 96 }); 97 } 98 else 99 { 100 QAction action = menu.addAction(QString("No row selected")); 101 action.setEnabled(false); 102 103 action = menu.addAction(QString("Add row")); 104 connect(action.signal!"triggered", menu, (){ 105 ui.tableWidget.insertRow(ui.tableWidget.rowCount()); 106 }); 107 } 108 109 if(currentColumn >= 0) 110 { 111 QAction action = menu.addAction("Current column = " ~ QString.number(currentColumn)); 112 action.setEnabled(false); 113 114 action = menu.addAction(QString("Remove column")); 115 connect(action.signal!"triggered", menu, (){ 116 ui.tableWidget.removeColumn(currentColumn); 117 }); 118 119 action = menu.addAction(QString("Add column before")); 120 connect(action.signal!"triggered", menu, (){ 121 ui.tableWidget.insertColumn(currentColumn); 122 }); 123 124 action = menu.addAction(QString("Add column after")); 125 connect(action.signal!"triggered", menu, (){ 126 ui.tableWidget.insertColumn(currentColumn + 1); 127 }); 128 129 action = menu.addAction(QString("Change column header")); 130 connect(action.signal!"triggered", menu, (){ 131 QTableWidgetItem item = ui.tableWidget.horizontalHeaderItem(currentColumn); 132 bool ok; 133 QString text = QString.create; 134 if(item) 135 text = item.text(); 136 text = QInputDialog.getText(this, QString.create(), QString("Column header"), QLineEdit.EchoMode.Normal, text, &ok); 137 if(ok) 138 { 139 if(text.isEmpty()) 140 { 141 if(item) 142 cpp_delete(item); 143 } 144 else 145 { 146 if(!item) 147 { 148 item = cpp_new!QTableWidgetItem; 149 ui.tableWidget.setHorizontalHeaderItem(currentColumn, item); 150 } 151 item.setText(text); 152 } 153 } 154 }); 155 } 156 else 157 { 158 QAction action = menu.addAction(QString("No column selected")); 159 action.setEnabled(false); 160 161 action = menu.addAction(QString("Add column")); 162 connect(action.signal!"triggered", menu, (){ 163 ui.tableWidget.insertColumn(ui.tableWidget.columnCount()); 164 }); 165 } 166 167 if(currentColumn >= 0 && currentRow >= 0) 168 { 169 QTableWidgetItem item = ui.tableWidget.item(currentRow, currentColumn); 170 if(!item) 171 { 172 item = cpp_new!QTableWidgetItem(); 173 ui.tableWidget.setItem(currentRow, currentColumn, item); 174 } 175 176 QAction action = menu.addAction(QString("Item")); 177 action.setEnabled(false); 178 179 action = menu.addAction(QString("Change font")); 180 connect(action.signal!"triggered", menu, (){ 181 QFont font = item.font(); 182 bool ok; 183 font = QFontDialog.getFont(&ok, font, this); 184 if(ok) 185 item.setFont(font); 186 }); 187 188 action = menu.addAction(QString("Change background color")); 189 connect(action.signal!"triggered", menu, (){ 190 QBrush brush = item.background(); 191 QColor color = QColorDialog.getColor(brush.color(), this); 192 if(color.isValid()) 193 { 194 brush = QBrush(color); 195 item.setBackground(brush); 196 } 197 }); 198 } 199 200 menu.exec(ui.tableWidget.mapToGlobal(pos)); 201 cpp_delete(menu); 202 } 203 204 private: 205 UIStruct!"tabtablewidget.ui"* ui; 206 } 207