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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
|
/****************************************************************************
**
** 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 "widgetboxcategorylistview.h"
#include <QtDesigner/QDesignerFormEditorInterface>
#include <QtDesigner/QDesignerWidgetDataBaseInterface>
#include <QtXml/QDomDocument>
#include <QtGui/QIcon>
#include <QtGui/QListView>
#include <QtGui/QLineEdit>
#include <QtGui/QItemDelegate>
#include <QtGui/QSortFilterProxyModel>
#include <QtCore/QAbstractListModel>
#include <QtCore/QList>
#include <QtCore/QTextStream>
#include <QtCore/QRegExp>
static const char *widgetElementC = "widget";
static const char *nameAttributeC = "name";
static const char *uiOpeningTagC = "<ui>";
static const char *uiClosingTagC = "</ui>";
QT_BEGIN_NAMESPACE
enum { FilterRole = Qt::UserRole + 11 };
static QString domToString(const QDomElement &elt)
{
QString result;
QTextStream stream(&result, QIODevice::WriteOnly);
elt.save(stream, 2);
stream.flush();
return result;
}
static QDomDocument stringToDom(const QString &xml)
{
QDomDocument result;
result.setContent(xml);
return result;
}
namespace qdesigner_internal {
// Entry of the model list
struct WidgetBoxCategoryEntry {
WidgetBoxCategoryEntry();
explicit WidgetBoxCategoryEntry(const QDesignerWidgetBoxInterface::Widget &widget,
const QString &filter,
const QIcon &icon,
bool editable);
QDesignerWidgetBoxInterface::Widget widget;
QString toolTip;
QString whatsThis;
QString filter;
QIcon icon;
bool editable;
};
WidgetBoxCategoryEntry::WidgetBoxCategoryEntry() :
editable(false)
{
}
WidgetBoxCategoryEntry::WidgetBoxCategoryEntry(const QDesignerWidgetBoxInterface::Widget &w,
const QString &filterIn,
const QIcon &i, bool e) :
widget(w),
filter(filterIn),
icon(i),
editable(e)
{
}
/* WidgetBoxCategoryModel, representing a list of category entries. Uses a
* QAbstractListModel since the behaviour depends on the view mode of the list
* view, it does not return text in the case of IconMode. */
class WidgetBoxCategoryModel : public QAbstractListModel {
public:
explicit WidgetBoxCategoryModel(QDesignerFormEditorInterface *core, QObject *parent = 0);
// QAbstractListModel
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
virtual bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole);
virtual Qt::ItemFlags flags (const QModelIndex & index ) const;
virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
// The model returns no text in icon mode, so, it also needs to know it
QListView::ViewMode viewMode() const;
void setViewMode(QListView::ViewMode vm);
void addWidget(const QDesignerWidgetBoxInterface::Widget &widget, const QIcon &icon, bool editable);
QDesignerWidgetBoxInterface::Widget widgetAt(const QModelIndex & index) const;
QDesignerWidgetBoxInterface::Widget widgetAt(int row) const;
int indexOfWidget(const QString &name);
QDesignerWidgetBoxInterface::Category category() const;
bool removeCustomWidgets();
private:
typedef QList<WidgetBoxCategoryEntry> WidgetBoxCategoryEntrys;
QRegExp m_classNameRegExp;
QDesignerFormEditorInterface *m_core;
WidgetBoxCategoryEntrys m_items;
QListView::ViewMode m_viewMode;
};
WidgetBoxCategoryModel::WidgetBoxCategoryModel(QDesignerFormEditorInterface *core, QObject *parent) :
QAbstractListModel(parent),
m_classNameRegExp(QLatin1String("<widget +class *= *\"([^\"]+)\"")),
m_core(core),
m_viewMode(QListView::ListMode)
{
Q_ASSERT(m_classNameRegExp.isValid());
}
QListView::ViewMode WidgetBoxCategoryModel::viewMode() const
{
return m_viewMode;
}
void WidgetBoxCategoryModel::setViewMode(QListView::ViewMode vm)
{
if (m_viewMode == vm)
return;
m_viewMode = vm;
if (!m_items.empty())
reset();
}
int WidgetBoxCategoryModel::indexOfWidget(const QString &name)
{
const int count = m_items.size();
for (int i = 0; i < count; i++)
if (m_items.at(i).widget.name() == name)
return i;
return -1;
}
QDesignerWidgetBoxInterface::Category WidgetBoxCategoryModel::category() const
{
QDesignerWidgetBoxInterface::Category rc;
const WidgetBoxCategoryEntrys::const_iterator cend = m_items.constEnd();
for (WidgetBoxCategoryEntrys::const_iterator it = m_items.constBegin(); it != cend; ++it)
rc.addWidget(it->widget);
return rc;
}
bool WidgetBoxCategoryModel::removeCustomWidgets()
{
// Typically, we are a whole category of custom widgets, so, remove all
// and do reset.
bool changed = false;
for (WidgetBoxCategoryEntrys::iterator it = m_items.begin(); it != m_items.end(); )
if (it->widget.type() == QDesignerWidgetBoxInterface::Widget::Custom) {
it = m_items.erase(it);
changed = true;
} else {
++it;
}
if (changed)
reset();
return changed;
}
void WidgetBoxCategoryModel::addWidget(const QDesignerWidgetBoxInterface::Widget &widget, const QIcon &icon,bool editable)
{
// build item. Filter on name + class name if it is different and not a layout.
QString filter = widget.name();
if (!filter.contains(QLatin1String("Layout")) && m_classNameRegExp.indexIn(widget.domXml()) != -1) {
const QString className = m_classNameRegExp.cap(1);
if (!filter.contains(className))
filter += className;
}
WidgetBoxCategoryEntry item(widget, filter, icon, editable);
const QDesignerWidgetDataBaseInterface *db = m_core->widgetDataBase();
const int dbIndex = db->indexOfClassName(widget.name());
if (dbIndex != -1) {
const QDesignerWidgetDataBaseItemInterface *dbItem = db->item(dbIndex);
const QString toolTip = dbItem->toolTip();
if (!toolTip.isEmpty())
item.toolTip = toolTip;
const QString whatsThis = dbItem->whatsThis();
if (!whatsThis.isEmpty())
item.whatsThis = whatsThis;
}
// insert
const int row = m_items.size();
beginInsertRows(QModelIndex(), row, row);
m_items.push_back(item);
endInsertRows();
}
QVariant WidgetBoxCategoryModel::data(const QModelIndex &index, int role) const
{
const int row = index.row();
if (row < 0 || row >= m_items.size())
return QVariant();
const WidgetBoxCategoryEntry &item = m_items.at(row);
switch (role) {
case Qt::DisplayRole:
// No text in icon mode
return QVariant(m_viewMode == QListView::ListMode ? item.widget.name() : QString());
case Qt::DecorationRole:
return QVariant(item.icon);
case Qt::EditRole:
return QVariant(item.widget.name());
case Qt::ToolTipRole: {
if (m_viewMode == QListView::ListMode)
return QVariant(item.toolTip);
// Icon mode tooltip should contain the class name
QString tt = item.widget.name();
if (!item.toolTip.isEmpty()) {
tt += QLatin1Char('\n');
tt += item.toolTip;
}
return QVariant(tt);
}
case Qt::WhatsThisRole:
return QVariant(item.whatsThis);
case FilterRole:
return item.filter;
}
return QVariant();
}
bool WidgetBoxCategoryModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
const int row = index.row();
if (role != Qt::EditRole || row < 0 || row >= m_items.size() || value.type() != QVariant::String)
return false;
// Set name and adapt Xml
WidgetBoxCategoryEntry &item = m_items[row];
const QString newName = value.toString();
item.widget.setName(newName);
const QDomDocument doc = stringToDom(WidgetBoxCategoryListView::widgetDomXml(item.widget));
QDomElement widget_elt = doc.firstChildElement(QLatin1String(widgetElementC));
if (!widget_elt.isNull()) {
widget_elt.setAttribute(QLatin1String(nameAttributeC), newName);
item.widget.setDomXml(domToString(widget_elt));
}
emit dataChanged(index, index);
return true;
}
Qt::ItemFlags WidgetBoxCategoryModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags rc = Qt::ItemIsEnabled;
const int row = index.row();
if (row >= 0 && row < m_items.size())
if (m_items.at(row).editable) {
rc |= Qt::ItemIsSelectable;
// Can change name in list mode only
if (m_viewMode == QListView::ListMode)
rc |= Qt::ItemIsEditable;
}
return rc;
}
int WidgetBoxCategoryModel::rowCount(const QModelIndex & /*parent*/) const
{
return m_items.size();
}
bool WidgetBoxCategoryModel::removeRows(int row, int count, const QModelIndex & parent)
{
if (row < 0 || count < 1)
return false;
const int size = m_items.size();
const int last = row + count - 1;
if (row >= size || last >= size)
return false;
beginRemoveRows(parent, row, last);
for (int r = last; r >= row; r--)
m_items.removeAt(r);
endRemoveRows();
return true;
}
QDesignerWidgetBoxInterface::Widget WidgetBoxCategoryModel::widgetAt(const QModelIndex & index) const
{
return widgetAt(index.row());
}
QDesignerWidgetBoxInterface::Widget WidgetBoxCategoryModel::widgetAt(int row) const
{
if (row < 0 || row >= m_items.size())
return QDesignerWidgetBoxInterface::Widget();
return m_items.at(row).widget;
}
/* WidgetSubBoxItemDelegate, ensures a valid name using a regexp validator */
class WidgetBoxCategoryEntryDelegate : public QItemDelegate
{
public:
explicit WidgetBoxCategoryEntryDelegate(QWidget *parent = 0) : QItemDelegate(parent) {}
QWidget *createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const;
};
QWidget *WidgetBoxCategoryEntryDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QWidget *result = QItemDelegate::createEditor(parent, option, index);
if (QLineEdit *line_edit = qobject_cast<QLineEdit*>(result)) {
const QRegExp re = QRegExp(QLatin1String("[_a-zA-Z][_a-zA-Z0-9]*"));
Q_ASSERT(re.isValid());
line_edit->setValidator(new QRegExpValidator(re, line_edit));
}
return result;
}
// ---------------------- WidgetBoxCategoryListView
WidgetBoxCategoryListView::WidgetBoxCategoryListView(QDesignerFormEditorInterface *core, QWidget *parent) :
QListView(parent),
m_proxyModel(new QSortFilterProxyModel(this)),
m_model(new WidgetBoxCategoryModel(core, this))
{
setFocusPolicy(Qt::NoFocus);
setFrameShape(QFrame::NoFrame);
setIconSize(QSize(22, 22));
setSpacing(1);
setTextElideMode(Qt::ElideMiddle);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setResizeMode(QListView::Adjust);
setUniformItemSizes(true);
setItemDelegate(new WidgetBoxCategoryEntryDelegate(this));
connect(this, SIGNAL(pressed(QModelIndex)), this, SLOT(slotPressed(QModelIndex)));
setEditTriggers(QAbstractItemView::AnyKeyPressed);
m_proxyModel->setSourceModel(m_model);
m_proxyModel->setFilterRole(FilterRole);
setModel(m_proxyModel);
connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SIGNAL(scratchPadChanged()));
}
void WidgetBoxCategoryListView::setViewMode(ViewMode vm)
{
QListView::setViewMode(vm);
m_model->setViewMode(vm);
}
void WidgetBoxCategoryListView::setCurrentItem(AccessMode am, int row)
{
const QModelIndex index = am == FilteredAccess ?
m_proxyModel->index(row, 0) :
m_proxyModel->mapFromSource(m_model->index(row, 0));
if (index.isValid())
setCurrentIndex(index);
}
void WidgetBoxCategoryListView::slotPressed(const QModelIndex &index)
{
const QDesignerWidgetBoxInterface::Widget wgt = m_model->widgetAt(m_proxyModel->mapToSource(index));
if (wgt.isNull())
return;
emit pressed(wgt.name(), widgetDomXml(wgt), QCursor::pos());
}
void WidgetBoxCategoryListView::removeCurrentItem()
{
const QModelIndex index = currentIndex();
if (!index.isValid() || !m_proxyModel->removeRow(index.row()))
return;
// We check the unfiltered item count here, we don't want to get removed if the
// filtered view is empty
if (m_model->rowCount()) {
emit itemRemoved();
} else {
emit lastItemRemoved();
}
}
void WidgetBoxCategoryListView::editCurrentItem()
{
const QModelIndex index = currentIndex();
if (index.isValid())
edit(index);
}
int WidgetBoxCategoryListView::count(AccessMode am) const
{
return am == FilteredAccess ? m_proxyModel->rowCount() : m_model->rowCount();
}
int WidgetBoxCategoryListView::mapRowToSource(int filterRow) const
{
const QModelIndex filterIndex = m_proxyModel->index(filterRow, 0);
return m_proxyModel->mapToSource(filterIndex).row();
}
QDesignerWidgetBoxInterface::Widget WidgetBoxCategoryListView::widgetAt(AccessMode am, const QModelIndex & index) const
{
const QModelIndex unfilteredIndex = am == FilteredAccess ? m_proxyModel->mapToSource(index) : index;
return m_model->widgetAt(unfilteredIndex);
}
QDesignerWidgetBoxInterface::Widget WidgetBoxCategoryListView::widgetAt(AccessMode am, int row) const
{
return m_model->widgetAt(am == UnfilteredAccess ? row : mapRowToSource(row));
}
void WidgetBoxCategoryListView::removeRow(AccessMode am, int row)
{
m_model->removeRow(am == UnfilteredAccess ? row : mapRowToSource(row));
}
bool WidgetBoxCategoryListView::containsWidget(const QString &name)
{
return m_model->indexOfWidget(name) != -1;
}
void WidgetBoxCategoryListView::addWidget(const QDesignerWidgetBoxInterface::Widget &widget, const QIcon &icon, bool editable)
{
m_model->addWidget(widget, icon, editable);
}
QString WidgetBoxCategoryListView::widgetDomXml(const QDesignerWidgetBoxInterface::Widget &widget)
{
QString domXml = widget.domXml();
if (domXml.isEmpty()) {
domXml = QLatin1String(uiOpeningTagC);
domXml += QLatin1String("<widget class=\"");
domXml += widget.name();
domXml += QLatin1String("\"/>");
domXml += QLatin1String(uiClosingTagC);
}
return domXml;
}
void WidgetBoxCategoryListView::filter(const QRegExp &re)
{
m_proxyModel->setFilterRegExp(re);
}
QDesignerWidgetBoxInterface::Category WidgetBoxCategoryListView::category() const
{
return m_model->category();
}
bool WidgetBoxCategoryListView::removeCustomWidgets()
{
return m_model->removeCustomWidgets();
}
} // namespace qdesigner_internal
QT_END_NAMESPACE
|