|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Designer of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "tablewidgeteditor.h" |
|
43 #include <abstractformbuilder.h> |
|
44 #include <iconloader_p.h> |
|
45 #include <qdesigner_command_p.h> |
|
46 #include "formwindowbase_p.h" |
|
47 #include "qdesigner_utils_p.h" |
|
48 #include <designerpropertymanager.h> |
|
49 #include <qttreepropertybrowser.h> |
|
50 |
|
51 #include <QtDesigner/QDesignerFormWindowInterface> |
|
52 #include <QtDesigner/QDesignerFormEditorInterface> |
|
53 #include <QtDesigner/QDesignerIconCacheInterface> |
|
54 #include <QtCore/QDir> |
|
55 #include <QtCore/QQueue> |
|
56 #include <QtCore/QTextStream> |
|
57 |
|
58 QT_BEGIN_NAMESPACE |
|
59 |
|
60 using namespace qdesigner_internal; |
|
61 |
|
62 TableWidgetEditor::TableWidgetEditor(QDesignerFormWindowInterface *form, QWidget *parent) |
|
63 : AbstractItemEditor(form, parent), m_updatingBrowser(false) |
|
64 { |
|
65 m_columnEditor = new ItemListEditor(form, this); |
|
66 m_columnEditor->setObjectName(QLatin1String("columnEditor")); |
|
67 m_columnEditor->setNewItemText(tr("New Column")); |
|
68 m_rowEditor = new ItemListEditor(form, this); |
|
69 m_rowEditor->setObjectName(QLatin1String("rowEditor")); |
|
70 m_rowEditor->setNewItemText(tr("New Row")); |
|
71 ui.setupUi(this); |
|
72 |
|
73 injectPropertyBrowser(ui.itemsTab, ui.widget); |
|
74 connect(ui.showPropertiesButton, SIGNAL(clicked()), |
|
75 this, SLOT(togglePropertyBrowser())); |
|
76 togglePropertyBrowser(); |
|
77 |
|
78 ui.tabWidget->insertTab(0, m_columnEditor, tr("&Columns")); |
|
79 ui.tabWidget->insertTab(1, m_rowEditor, tr("&Rows")); |
|
80 ui.tabWidget->setCurrentIndex(0); |
|
81 setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); |
|
82 |
|
83 ui.tableWidget->setSelectionMode(QAbstractItemView::SingleSelection); |
|
84 |
|
85 connect(iconCache(), SIGNAL(reloaded()), this, SLOT(cacheReloaded())); |
|
86 } |
|
87 |
|
88 static AbstractItemEditor::PropertyDefinition tableHeaderPropList[] = { |
|
89 { Qt::DisplayPropertyRole, 0, DesignerPropertyManager::designerStringTypeId, "text" }, |
|
90 { Qt::DecorationPropertyRole, 0, DesignerPropertyManager::designerIconTypeId, "icon" }, |
|
91 { Qt::ToolTipPropertyRole, 0, DesignerPropertyManager::designerStringTypeId, "toolTip" }, |
|
92 // { Qt::StatusTipPropertyRole, 0, DesignerPropertyManager::designerStringTypeId, "statusTip" }, |
|
93 { Qt::WhatsThisPropertyRole, 0, DesignerPropertyManager::designerStringTypeId, "whatsThis" }, |
|
94 { Qt::FontRole, QVariant::Font, 0, "font" }, |
|
95 { Qt::TextAlignmentRole, 0, DesignerPropertyManager::designerAlignmentTypeId, "textAlignment" }, |
|
96 { Qt::BackgroundRole, QVariant::Color, 0, "background" }, |
|
97 { Qt::ForegroundRole, QVariant::Brush, 0, "foreground" }, |
|
98 { 0, 0, 0, 0 } |
|
99 }; |
|
100 |
|
101 static AbstractItemEditor::PropertyDefinition tableItemPropList[] = { |
|
102 { Qt::DisplayPropertyRole, 0, DesignerPropertyManager::designerStringTypeId, "text" }, |
|
103 { Qt::DecorationPropertyRole, 0, DesignerPropertyManager::designerIconTypeId, "icon" }, |
|
104 { Qt::ToolTipPropertyRole, 0, DesignerPropertyManager::designerStringTypeId, "toolTip" }, |
|
105 // { Qt::StatusTipPropertyRole, 0, DesignerPropertyManager::designerStringTypeId, "statusTip" }, |
|
106 { Qt::WhatsThisPropertyRole, 0, DesignerPropertyManager::designerStringTypeId, "whatsThis" }, |
|
107 { Qt::FontRole, QVariant::Font, 0, "font" }, |
|
108 { Qt::TextAlignmentRole, 0, DesignerPropertyManager::designerAlignmentTypeId, "textAlignment" }, |
|
109 { Qt::BackgroundRole, QVariant::Brush, 0, "background" }, |
|
110 { Qt::ForegroundRole, QVariant::Brush, 0, "foreground" }, |
|
111 { ItemFlagsShadowRole, 0, QtVariantPropertyManager::flagTypeId, "flags" }, |
|
112 { Qt::CheckStateRole, 0, QtVariantPropertyManager::enumTypeId, "checkState" }, |
|
113 { 0, 0, 0, 0 } |
|
114 }; |
|
115 |
|
116 TableWidgetContents TableWidgetEditor::fillContentsFromTableWidget(QTableWidget *tableWidget) |
|
117 { |
|
118 TableWidgetContents tblCont; |
|
119 tblCont.fromTableWidget(tableWidget, false); |
|
120 tblCont.applyToTableWidget(ui.tableWidget, iconCache(), true); |
|
121 |
|
122 tblCont.m_verticalHeader.applyToListWidget(m_rowEditor->listWidget(), iconCache(), true); |
|
123 m_rowEditor->setupEditor(tableWidget, tableHeaderPropList); |
|
124 |
|
125 tblCont.m_horizontalHeader.applyToListWidget(m_columnEditor->listWidget(), iconCache(), true); |
|
126 m_columnEditor->setupEditor(tableWidget, tableHeaderPropList); |
|
127 |
|
128 setupEditor(tableWidget, tableItemPropList); |
|
129 if (ui.tableWidget->columnCount() > 0 && ui.tableWidget->rowCount() > 0) |
|
130 ui.tableWidget->setCurrentCell(0, 0); |
|
131 |
|
132 updateEditor(); |
|
133 |
|
134 return tblCont; |
|
135 } |
|
136 |
|
137 TableWidgetContents TableWidgetEditor::contents() const |
|
138 { |
|
139 TableWidgetContents retVal; |
|
140 retVal.fromTableWidget(ui.tableWidget, true); |
|
141 return retVal; |
|
142 } |
|
143 |
|
144 void TableWidgetEditor::setItemData(int role, const QVariant &v) |
|
145 { |
|
146 QTableWidgetItem *item = ui.tableWidget->currentItem(); |
|
147 BoolBlocker block(m_updatingBrowser); |
|
148 if (!item) { |
|
149 item = new QTableWidgetItem; |
|
150 ui.tableWidget->setItem(ui.tableWidget->currentRow(), ui.tableWidget->currentColumn(), item); |
|
151 } |
|
152 QVariant newValue = v; |
|
153 if (role == Qt::FontRole && newValue.type() == QVariant::Font) { |
|
154 QFont oldFont = ui.tableWidget->font(); |
|
155 QFont newFont = qVariantValue<QFont>(newValue).resolve(oldFont); |
|
156 newValue = qVariantFromValue(newFont); |
|
157 item->setData(role, QVariant()); // force the right font with the current resolve mask is set (item view bug) |
|
158 } |
|
159 item->setData(role, newValue); |
|
160 } |
|
161 |
|
162 QVariant TableWidgetEditor::getItemData(int role) const |
|
163 { |
|
164 QTableWidgetItem *item = ui.tableWidget->currentItem(); |
|
165 if (!item) |
|
166 return QVariant(); |
|
167 return item->data(role); |
|
168 } |
|
169 |
|
170 void TableWidgetEditor::on_tableWidget_currentCellChanged(int currentRow, int currentCol, int, int /* XXX remove me */) |
|
171 { |
|
172 m_rowEditor->setCurrentIndex(currentRow); |
|
173 m_columnEditor->setCurrentIndex(currentCol); |
|
174 updateBrowser(); |
|
175 } |
|
176 |
|
177 void TableWidgetEditor::on_tableWidget_itemChanged(QTableWidgetItem *item) |
|
178 { |
|
179 if (m_updatingBrowser) |
|
180 return; |
|
181 |
|
182 PropertySheetStringValue val = qVariantValue<PropertySheetStringValue>(item->data(Qt::DisplayPropertyRole)); |
|
183 val.setValue(item->text()); |
|
184 BoolBlocker block(m_updatingBrowser); |
|
185 item->setData(Qt::DisplayPropertyRole, qVariantFromValue(val)); |
|
186 |
|
187 updateBrowser(); |
|
188 } |
|
189 |
|
190 void TableWidgetEditor::on_columnEditor_indexChanged(int col) |
|
191 { |
|
192 ui.tableWidget->setCurrentCell(ui.tableWidget->currentRow(), col); |
|
193 } |
|
194 |
|
195 void TableWidgetEditor::on_columnEditor_itemChanged(int idx, int role, const QVariant &v) |
|
196 { |
|
197 ui.tableWidget->horizontalHeaderItem(idx)->setData(role, v); |
|
198 } |
|
199 |
|
200 void TableWidgetEditor::on_rowEditor_indexChanged(int col) |
|
201 { |
|
202 ui.tableWidget->setCurrentCell(col, ui.tableWidget->currentColumn()); |
|
203 } |
|
204 |
|
205 void TableWidgetEditor::on_rowEditor_itemChanged(int idx, int role, const QVariant &v) |
|
206 { |
|
207 ui.tableWidget->verticalHeaderItem(idx)->setData(role, v); |
|
208 } |
|
209 |
|
210 void TableWidgetEditor::togglePropertyBrowser() |
|
211 { |
|
212 // Always hide in case parent widget is not visible -> on startup |
|
213 const bool isVisible = |
|
214 !this->isVisible() ? true : m_propertyBrowser->isVisible(); |
|
215 if (isVisible) |
|
216 ui.showPropertiesButton->setText(tr("Properties &<<")); |
|
217 else |
|
218 ui.showPropertiesButton->setText(tr("Properties &>>")); |
|
219 m_propertyBrowser->setVisible(!isVisible); |
|
220 } |
|
221 |
|
222 void TableWidgetEditor::updateEditor() |
|
223 { |
|
224 const bool wasEnabled = ui.tabWidget->isTabEnabled(2); |
|
225 const bool isEnabled = ui.tableWidget->columnCount() && ui.tableWidget->rowCount(); |
|
226 ui.tabWidget->setTabEnabled(2, isEnabled); |
|
227 if (!wasEnabled && isEnabled) |
|
228 ui.tableWidget->setCurrentCell(0, 0); |
|
229 |
|
230 QMetaObject::invokeMethod(ui.tableWidget, "updateGeometries"); |
|
231 ui.tableWidget->viewport()->update(); |
|
232 } |
|
233 |
|
234 void TableWidgetEditor::moveColumnsLeft(int fromColumn, int toColumn) |
|
235 { |
|
236 if (fromColumn >= toColumn) |
|
237 return; |
|
238 |
|
239 QTableWidgetItem *lastItem = ui.tableWidget->takeHorizontalHeaderItem(toColumn); |
|
240 for (int i = toColumn; i > fromColumn; i--) { |
|
241 ui.tableWidget->setHorizontalHeaderItem(i, |
|
242 ui.tableWidget->takeHorizontalHeaderItem(i - 1)); |
|
243 } |
|
244 ui.tableWidget->setHorizontalHeaderItem(fromColumn, lastItem); |
|
245 |
|
246 for (int i = 0; i < ui.tableWidget->rowCount(); i++) { |
|
247 QTableWidgetItem *lastItem = ui.tableWidget->takeItem(i, toColumn); |
|
248 for (int j = toColumn; j > fromColumn; j--) |
|
249 ui.tableWidget->setItem(i, j, ui.tableWidget->takeItem(i, j - 1)); |
|
250 ui.tableWidget->setItem(i, fromColumn, lastItem); |
|
251 } |
|
252 } |
|
253 |
|
254 void TableWidgetEditor::moveColumnsRight(int fromColumn, int toColumn) |
|
255 { |
|
256 if (fromColumn >= toColumn) |
|
257 return; |
|
258 |
|
259 QTableWidgetItem *lastItem = ui.tableWidget->takeHorizontalHeaderItem(fromColumn); |
|
260 for (int i = fromColumn; i < toColumn; i++) { |
|
261 ui.tableWidget->setHorizontalHeaderItem(i, |
|
262 ui.tableWidget->takeHorizontalHeaderItem(i + 1)); |
|
263 } |
|
264 ui.tableWidget->setHorizontalHeaderItem(toColumn, lastItem); |
|
265 |
|
266 for (int i = 0; i < ui.tableWidget->rowCount(); i++) { |
|
267 QTableWidgetItem *lastItem = ui.tableWidget->takeItem(i, fromColumn); |
|
268 for (int j = fromColumn; j < toColumn; j++) |
|
269 ui.tableWidget->setItem(i, j, ui.tableWidget->takeItem(i, j + 1)); |
|
270 ui.tableWidget->setItem(i, toColumn, lastItem); |
|
271 } |
|
272 } |
|
273 |
|
274 void TableWidgetEditor::moveRowsDown(int fromRow, int toRow) |
|
275 { |
|
276 if (fromRow >= toRow) |
|
277 return; |
|
278 |
|
279 QTableWidgetItem *lastItem = ui.tableWidget->takeVerticalHeaderItem(toRow); |
|
280 for (int i = toRow; i > fromRow; i--) { |
|
281 ui.tableWidget->setVerticalHeaderItem(i, |
|
282 ui.tableWidget->takeVerticalHeaderItem(i - 1)); |
|
283 } |
|
284 ui.tableWidget->setVerticalHeaderItem(fromRow, lastItem); |
|
285 |
|
286 for (int i = 0; i < ui.tableWidget->columnCount(); i++) { |
|
287 QTableWidgetItem *lastItem = ui.tableWidget->takeItem(toRow, i); |
|
288 for (int j = toRow; j > fromRow; j--) |
|
289 ui.tableWidget->setItem(j, i, ui.tableWidget->takeItem(j - 1, i)); |
|
290 ui.tableWidget->setItem(fromRow, i, lastItem); |
|
291 } |
|
292 } |
|
293 |
|
294 void TableWidgetEditor::moveRowsUp(int fromRow, int toRow) |
|
295 { |
|
296 if (fromRow >= toRow) |
|
297 return; |
|
298 |
|
299 QTableWidgetItem *lastItem = ui.tableWidget->takeVerticalHeaderItem(fromRow); |
|
300 for (int i = fromRow; i < toRow; i++) { |
|
301 ui.tableWidget->setVerticalHeaderItem(i, |
|
302 ui.tableWidget->takeVerticalHeaderItem(i + 1)); |
|
303 } |
|
304 ui.tableWidget->setVerticalHeaderItem(toRow, lastItem); |
|
305 |
|
306 for (int i = 0; i < ui.tableWidget->columnCount(); i++) { |
|
307 QTableWidgetItem *lastItem = ui.tableWidget->takeItem(fromRow, i); |
|
308 for (int j = fromRow; j < toRow; j++) |
|
309 ui.tableWidget->setItem(j, i, ui.tableWidget->takeItem(j + 1, i)); |
|
310 ui.tableWidget->setItem(toRow, i, lastItem); |
|
311 } |
|
312 } |
|
313 |
|
314 void TableWidgetEditor::on_columnEditor_itemInserted(int idx) |
|
315 { |
|
316 const int columnCount = ui.tableWidget->columnCount(); |
|
317 ui.tableWidget->setColumnCount(columnCount + 1); |
|
318 |
|
319 QTableWidgetItem *newItem = new QTableWidgetItem(m_columnEditor->newItemText()); |
|
320 newItem->setData(Qt::DisplayPropertyRole, qVariantFromValue(PropertySheetStringValue(m_columnEditor->newItemText()))); |
|
321 ui.tableWidget->setHorizontalHeaderItem(columnCount, newItem); |
|
322 |
|
323 moveColumnsLeft(idx, columnCount); |
|
324 |
|
325 int row = ui.tableWidget->currentRow(); |
|
326 if (row >= 0) |
|
327 ui.tableWidget->setCurrentCell(row, idx); |
|
328 |
|
329 updateEditor(); |
|
330 } |
|
331 |
|
332 void TableWidgetEditor::on_columnEditor_itemDeleted(int idx) |
|
333 { |
|
334 const int columnCount = ui.tableWidget->columnCount(); |
|
335 |
|
336 moveColumnsRight(idx, columnCount - 1); |
|
337 ui.tableWidget->setColumnCount(columnCount - 1); |
|
338 |
|
339 updateEditor(); |
|
340 } |
|
341 |
|
342 void TableWidgetEditor::on_columnEditor_itemMovedUp(int idx) |
|
343 { |
|
344 moveColumnsRight(idx - 1, idx); |
|
345 |
|
346 ui.tableWidget->setCurrentCell(ui.tableWidget->currentRow(), idx - 1); |
|
347 } |
|
348 |
|
349 void TableWidgetEditor::on_columnEditor_itemMovedDown(int idx) |
|
350 { |
|
351 moveColumnsLeft(idx, idx + 1); |
|
352 |
|
353 ui.tableWidget->setCurrentCell(ui.tableWidget->currentRow(), idx + 1); |
|
354 } |
|
355 |
|
356 void TableWidgetEditor::on_rowEditor_itemInserted(int idx) |
|
357 { |
|
358 const int rowCount = ui.tableWidget->rowCount(); |
|
359 ui.tableWidget->setRowCount(rowCount + 1); |
|
360 |
|
361 QTableWidgetItem *newItem = new QTableWidgetItem(m_rowEditor->newItemText()); |
|
362 newItem->setData(Qt::DisplayPropertyRole, qVariantFromValue(PropertySheetStringValue(m_rowEditor->newItemText()))); |
|
363 ui.tableWidget->setVerticalHeaderItem(rowCount, newItem); |
|
364 |
|
365 moveRowsDown(idx, rowCount); |
|
366 |
|
367 int col = ui.tableWidget->currentColumn(); |
|
368 if (col >= 0) |
|
369 ui.tableWidget->setCurrentCell(idx, col); |
|
370 |
|
371 updateEditor(); |
|
372 } |
|
373 |
|
374 void TableWidgetEditor::on_rowEditor_itemDeleted(int idx) |
|
375 { |
|
376 const int rowCount = ui.tableWidget->rowCount(); |
|
377 |
|
378 moveRowsUp(idx, rowCount - 1); |
|
379 ui.tableWidget->setRowCount(rowCount - 1); |
|
380 |
|
381 updateEditor(); |
|
382 } |
|
383 |
|
384 void TableWidgetEditor::on_rowEditor_itemMovedUp(int idx) |
|
385 { |
|
386 moveRowsUp(idx - 1, idx); |
|
387 |
|
388 ui.tableWidget->setCurrentCell(idx - 1, ui.tableWidget->currentColumn()); |
|
389 } |
|
390 |
|
391 void TableWidgetEditor::on_rowEditor_itemMovedDown(int idx) |
|
392 { |
|
393 moveRowsDown(idx, idx + 1); |
|
394 |
|
395 ui.tableWidget->setCurrentCell(idx + 1, ui.tableWidget->currentColumn()); |
|
396 } |
|
397 |
|
398 void TableWidgetEditor::cacheReloaded() |
|
399 { |
|
400 reloadIconResources(iconCache(), ui.tableWidget); |
|
401 } |
|
402 |
|
403 QT_END_NAMESPACE |