1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
|
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the Qt Designer of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "itemlisteditor.h"
#include <abstractformbuilder.h>
#include <iconloader_p.h>
#include <formwindowbase_p.h>
#include <designerpropertymanager.h>
#include <QtDesigner/QDesignerFormWindowInterface>
#include <qttreepropertybrowser.h>
#include <QtGui/QSplitter>
#include <QtCore/QCoreApplication>
QT_BEGIN_NAMESPACE
namespace qdesigner_internal {
class ItemPropertyBrowser : public QtTreePropertyBrowser
{
public:
ItemPropertyBrowser()
{
setResizeMode(Interactive);
//: Sample string to determinate the width for the first column of the list item property browser
const QString widthSampleString = QCoreApplication::translate("ItemPropertyBrowser", "XX Icon Selected off");
m_width = fontMetrics().width(widthSampleString);
setSplitterPosition(m_width);
m_width += fontMetrics().width(QLatin1String("/this/is/some/random/path"));
}
virtual QSize sizeHint() const
{
return QSize(m_width, 1);
}
private:
int m_width;
};
////////////////// Item editor ///////////////
AbstractItemEditor::AbstractItemEditor(QDesignerFormWindowInterface *form, QWidget *parent)
: QWidget(parent),
m_iconCache(qobject_cast<FormWindowBase *>(form)->iconCache()),
m_updatingBrowser(false)
{
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
m_propertyManager = new DesignerPropertyManager(form->core(), this);
m_editorFactory = new DesignerEditorFactory(form->core(), this);
m_editorFactory->setSpacing(0);
m_propertyBrowser = new ItemPropertyBrowser;
m_propertyBrowser->setFactoryForManager((QtVariantPropertyManager *)m_propertyManager,
m_editorFactory);
connect(m_editorFactory, SIGNAL(resetProperty(QtProperty*)),
SLOT(resetProperty(QtProperty*)));
connect(m_propertyManager, SIGNAL(valueChanged(QtProperty*,QVariant,bool)),
SLOT(propertyChanged(QtProperty*)));
connect(iconCache(), SIGNAL(reloaded()), this, SLOT(cacheReloaded()));
}
AbstractItemEditor::~AbstractItemEditor()
{
m_propertyBrowser->unsetFactoryForManager(m_propertyManager);
}
static const char * const itemFlagNames[] = {
QT_TRANSLATE_NOOP("AbstractItemEditor", "Selectable"),
QT_TRANSLATE_NOOP("AbstractItemEditor", "Editable"),
QT_TRANSLATE_NOOP("AbstractItemEditor", "DragEnabled"),
QT_TRANSLATE_NOOP("AbstractItemEditor", "DropEnabled"),
QT_TRANSLATE_NOOP("AbstractItemEditor", "UserCheckable"),
QT_TRANSLATE_NOOP("AbstractItemEditor", "Enabled"),
QT_TRANSLATE_NOOP("AbstractItemEditor", "Tristate"),
0
};
static const char * const checkStateNames[] = {
QT_TRANSLATE_NOOP("AbstractItemEditor", "Unchecked"),
QT_TRANSLATE_NOOP("AbstractItemEditor", "PartiallyChecked"),
QT_TRANSLATE_NOOP("AbstractItemEditor", "Checked"),
0
};
static QStringList c2qStringList(const char * const in[])
{
QStringList out;
for (int i = 0; in[i]; i++)
out << AbstractItemEditor::tr(in[i]);
return out;
}
void AbstractItemEditor::setupProperties(PropertyDefinition *propList)
{
for (int i = 0; propList[i].name; i++) {
int type = propList[i].typeFunc ? propList[i].typeFunc() : propList[i].type;
int role = propList[i].role;
QtVariantProperty *prop = m_propertyManager->addProperty(type, QLatin1String(propList[i].name));
Q_ASSERT(prop);
if (role == Qt::ToolTipPropertyRole || role == Qt::WhatsThisPropertyRole)
prop->setAttribute(QLatin1String("validationMode"), ValidationRichText);
else if (role == Qt::DisplayPropertyRole)
prop->setAttribute(QLatin1String("validationMode"), ValidationMultiLine);
else if (role == Qt::StatusTipPropertyRole)
prop->setAttribute(QLatin1String("validationMode"), ValidationSingleLine);
else if (role == ItemFlagsShadowRole)
prop->setAttribute(QLatin1String("flagNames"), c2qStringList(itemFlagNames));
else if (role == Qt::CheckStateRole)
prop->setAttribute(QLatin1String("enumNames"), c2qStringList(checkStateNames));
prop->setAttribute(QLatin1String("resettable"), true);
m_properties.append(prop);
m_rootProperties.append(prop);
m_propertyToRole.insert(prop, role);
}
}
void AbstractItemEditor::setupObject(QWidget *object)
{
m_propertyManager->setObject(object);
QDesignerFormWindowInterface *formWindow = QDesignerFormWindowInterface::findFormWindow(object);
FormWindowBase *fwb = qobject_cast<FormWindowBase *>(formWindow);
m_editorFactory->setFormWindowBase(fwb);
}
void AbstractItemEditor::setupEditor(QWidget *object, PropertyDefinition *propList)
{
setupProperties(propList);
setupObject(object);
}
void AbstractItemEditor::propertyChanged(QtProperty *property)
{
if (m_updatingBrowser)
return;
BoolBlocker block(m_updatingBrowser);
QtVariantProperty *prop = m_propertyManager->variantProperty(property);
int role;
if ((role = m_propertyToRole.value(prop, -1)) == -1)
// Subproperty
return;
if ((role == ItemFlagsShadowRole && prop->value().toInt() == (int)QListWidgetItem().flags())
|| (role == Qt::DecorationPropertyRole && !qvariant_cast<PropertySheetIconValue>(prop->value()).mask())
|| (role == Qt::FontRole && !qvariant_cast<QFont>(prop->value()).resolve())) {
prop->setModified(false);
setItemData(role, QVariant());
} else {
prop->setModified(true);
setItemData(role, prop->value());
}
switch (role) {
case Qt::DecorationPropertyRole:
setItemData(Qt::DecorationRole, QVariant::fromValue(iconCache()->icon(qvariant_cast<PropertySheetIconValue>(prop->value()))));
break;
case Qt::DisplayPropertyRole:
setItemData(Qt::EditRole, QVariant::fromValue(qvariant_cast<PropertySheetStringValue>(prop->value()).value()));
break;
case Qt::ToolTipPropertyRole:
setItemData(Qt::ToolTipRole, QVariant::fromValue(qvariant_cast<PropertySheetStringValue>(prop->value()).value()));
break;
case Qt::StatusTipPropertyRole:
setItemData(Qt::StatusTipRole, QVariant::fromValue(qvariant_cast<PropertySheetStringValue>(prop->value()).value()));
break;
case Qt::WhatsThisPropertyRole:
setItemData(Qt::WhatsThisRole, QVariant::fromValue(qvariant_cast<PropertySheetStringValue>(prop->value()).value()));
break;
default:
break;
}
prop->setValue(getItemData(role));
}
void AbstractItemEditor::resetProperty(QtProperty *property)
{
if (m_propertyManager->resetFontSubProperty(property))
return;
if (m_propertyManager->resetIconSubProperty(property))
return;
BoolBlocker block(m_updatingBrowser);
QtVariantProperty *prop = m_propertyManager->variantProperty(property);
int role = m_propertyToRole.value(prop);
if (role == ItemFlagsShadowRole)
prop->setValue(QVariant::fromValue((int)QListWidgetItem().flags()));
else
prop->setValue(QVariant(prop->valueType(), (void *)0));
prop->setModified(false);
setItemData(role, QVariant());
if (role == Qt::DecorationPropertyRole)
setItemData(Qt::DecorationRole, QVariant::fromValue(QIcon()));
if (role == Qt::DisplayPropertyRole)
setItemData(Qt::EditRole, QVariant::fromValue(QString()));
if (role == Qt::ToolTipPropertyRole)
setItemData(Qt::ToolTipRole, QVariant::fromValue(QString()));
if (role == Qt::StatusTipPropertyRole)
setItemData(Qt::StatusTipRole, QVariant::fromValue(QString()));
if (role == Qt::WhatsThisPropertyRole)
setItemData(Qt::WhatsThisRole, QVariant::fromValue(QString()));
}
void AbstractItemEditor::cacheReloaded()
{
BoolBlocker block(m_updatingBrowser);
m_propertyManager->reloadResourceProperties();
}
void AbstractItemEditor::updateBrowser()
{
BoolBlocker block(m_updatingBrowser);
foreach (QtVariantProperty *prop, m_properties) {
int role = m_propertyToRole.value(prop);
QVariant val = getItemData(role);
if (!val.isValid()) {
if (role == ItemFlagsShadowRole)
val = QVariant::fromValue((int)QListWidgetItem().flags());
else
val = QVariant((int)prop->value().userType(), (void *)0);
prop->setModified(false);
} else {
prop->setModified(true);
}
prop->setValue(val);
}
if (m_propertyBrowser->topLevelItems().isEmpty())
foreach (QtVariantProperty *prop, m_rootProperties)
m_propertyBrowser->addProperty(prop);
}
void AbstractItemEditor::injectPropertyBrowser(QWidget *parent, QWidget *widget)
{
// It is impossible to design a splitter with just one widget, so we do it by hand.
m_propertySplitter = new QSplitter;
m_propertySplitter->addWidget(widget);
m_propertySplitter->addWidget(m_propertyBrowser);
m_propertySplitter->setStretchFactor(0, 1);
m_propertySplitter->setStretchFactor(1, 0);
parent->layout()->addWidget(m_propertySplitter);
}
////////////////// List editor ///////////////
ItemListEditor::ItemListEditor(QDesignerFormWindowInterface *form, QWidget *parent)
: AbstractItemEditor(form, parent),
m_updating(false)
{
ui.setupUi(this);
injectPropertyBrowser(this, ui.widget);
connect(ui.showPropertiesButton, SIGNAL(clicked()),
this, SLOT(togglePropertyBrowser()));
setPropertyBrowserVisible(false);
QIcon upIcon = createIconSet(QString::fromUtf8("up.png"));
QIcon downIcon = createIconSet(QString::fromUtf8("down.png"));
QIcon minusIcon = createIconSet(QString::fromUtf8("minus.png"));
QIcon plusIcon = createIconSet(QString::fromUtf8("plus.png"));
ui.moveListItemUpButton->setIcon(upIcon);
ui.moveListItemDownButton->setIcon(downIcon);
ui.newListItemButton->setIcon(plusIcon);
ui.deleteListItemButton->setIcon(minusIcon);
connect(iconCache(), SIGNAL(reloaded()), this, SLOT(cacheReloaded()));
}
void ItemListEditor::setupEditor(QWidget *object, PropertyDefinition *propList)
{
AbstractItemEditor::setupEditor(object, propList);
if (ui.listWidget->count() > 0)
ui.listWidget->setCurrentRow(0);
else
updateEditor();
}
void ItemListEditor::setCurrentIndex(int idx)
{
m_updating = true;
ui.listWidget->setCurrentRow(idx);
m_updating = false;
}
void ItemListEditor::on_newListItemButton_clicked()
{
int row = ui.listWidget->currentRow() + 1;
QListWidgetItem *item = new QListWidgetItem(m_newItemText);
item->setData(Qt::DisplayPropertyRole, QVariant::fromValue(PropertySheetStringValue(m_newItemText)));
item->setFlags(item->flags() | Qt::ItemIsEditable);
if (row < ui.listWidget->count())
ui.listWidget->insertItem(row, item);
else
ui.listWidget->addItem(item);
emit itemInserted(row);
ui.listWidget->setCurrentItem(item);
ui.listWidget->editItem(item);
}
void ItemListEditor::on_deleteListItemButton_clicked()
{
int row = ui.listWidget->currentRow();
if (row != -1) {
delete ui.listWidget->takeItem(row);
emit itemDeleted(row);
}
if (row == ui.listWidget->count())
row--;
if (row < 0)
updateEditor();
else
ui.listWidget->setCurrentRow(row);
}
void ItemListEditor::on_moveListItemUpButton_clicked()
{
int row = ui.listWidget->currentRow();
if (row <= 0)
return; // nothing to do
ui.listWidget->insertItem(row - 1, ui.listWidget->takeItem(row));
ui.listWidget->setCurrentRow(row - 1);
emit itemMovedUp(row);
}
void ItemListEditor::on_moveListItemDownButton_clicked()
{
int row = ui.listWidget->currentRow();
if (row == -1 || row == ui.listWidget->count() - 1)
return; // nothing to do
ui.listWidget->insertItem(row + 1, ui.listWidget->takeItem(row));
ui.listWidget->setCurrentRow(row + 1);
emit itemMovedDown(row);
}
void ItemListEditor::on_listWidget_currentRowChanged()
{
updateEditor();
if (!m_updating)
emit indexChanged(ui.listWidget->currentRow());
}
void ItemListEditor::on_listWidget_itemChanged(QListWidgetItem *item)
{
if (m_updatingBrowser)
return;
PropertySheetStringValue val = qvariant_cast<PropertySheetStringValue>(item->data(Qt::DisplayPropertyRole));
val.setValue(item->text());
BoolBlocker block(m_updatingBrowser);
item->setData(Qt::DisplayPropertyRole, QVariant::fromValue(val));
// The checkState could change, too, but if this signal is connected,
// checkState is not in the list anyway, as we are editing a header item.
emit itemChanged(ui.listWidget->currentRow(), Qt::DisplayPropertyRole,
QVariant::fromValue(val));
updateBrowser();
}
void ItemListEditor::togglePropertyBrowser()
{
setPropertyBrowserVisible(!m_propertyBrowser->isVisible());
}
void ItemListEditor::setPropertyBrowserVisible(bool v)
{
ui.showPropertiesButton->setText(v ? tr("Properties &>>") : tr("Properties &<<"));
m_propertyBrowser->setVisible(v);
}
void ItemListEditor::setItemData(int role, const QVariant &v)
{
QListWidgetItem *item = ui.listWidget->currentItem();
bool reLayout = false;
if ((role == Qt::EditRole && (v.toString().count(QLatin1Char('\n')) != item->data(role).toString().count(QLatin1Char('\n'))))
|| role == Qt::FontRole)
reLayout = true;
QVariant newValue = v;
if (role == Qt::FontRole && newValue.type() == QVariant::Font) {
QFont oldFont = ui.listWidget->font();
QFont newFont = qvariant_cast<QFont>(newValue).resolve(oldFont);
newValue = QVariant::fromValue(newFont);
item->setData(role, QVariant()); // force the right font with the current resolve mask is set (item view bug)
}
item->setData(role, newValue);
if (reLayout)
ui.listWidget->doItemsLayout();
emit itemChanged(ui.listWidget->currentRow(), role, newValue);
}
QVariant ItemListEditor::getItemData(int role) const
{
return ui.listWidget->currentItem()->data(role);
}
void ItemListEditor::cacheReloaded()
{
reloadIconResources(iconCache(), ui.listWidget);
}
void ItemListEditor::updateEditor()
{
bool currentItemEnabled = false;
bool moveRowUpEnabled = false;
bool moveRowDownEnabled = false;
QListWidgetItem *item = ui.listWidget->currentItem();
if (item) {
currentItemEnabled = true;
int currentRow = ui.listWidget->currentRow();
if (currentRow > 0)
moveRowUpEnabled = true;
if (currentRow < ui.listWidget->count() - 1)
moveRowDownEnabled = true;
}
ui.moveListItemUpButton->setEnabled(moveRowUpEnabled);
ui.moveListItemDownButton->setEnabled(moveRowDownEnabled);
ui.deleteListItemButton->setEnabled(currentItemEnabled);
if (item)
updateBrowser();
else
m_propertyBrowser->clear();
}
} // namespace qdesigner_internal
QT_END_NAMESPACE
|