1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
3 #include "QCMakeCacheView.h"
4
5 #include "QCMakeWidgets.h"
6 #include <QApplication>
7 #include <QEvent>
8 #include <QHBoxLayout>
9 #include <QHeaderView>
10 #include <QKeyEvent>
11 #include <QMetaProperty>
12 #include <QSortFilterProxyModel>
13 #include <QStyle>
14
15 // filter for searches
16 class QCMakeSearchFilter : public QSortFilterProxyModel
17 {
18 public:
QCMakeSearchFilter(QObject * o)19 QCMakeSearchFilter(QObject* o)
20 : QSortFilterProxyModel(o)
21 {
22 }
23
24 protected:
filterAcceptsRow(int row,const QModelIndex & p) const25 bool filterAcceptsRow(int row, const QModelIndex& p) const override
26 {
27 QStringList strs;
28 const QAbstractItemModel* m = this->sourceModel();
29 QModelIndex idx = m->index(row, 0, p);
30
31 // if there are no children, get strings for column 0 and 1
32 if (!m->hasChildren(idx)) {
33 strs.append(m->data(idx).toString());
34 idx = m->index(row, 1, p);
35 strs.append(m->data(idx).toString());
36 } else {
37 // get strings for children entries to compare with
38 // instead of comparing with the parent
39 int num = m->rowCount(idx);
40 for (int i = 0; i < num; i++) {
41 QModelIndex tmpidx = m->index(i, 0, idx);
42 strs.append(m->data(tmpidx).toString());
43 tmpidx = m->index(i, 1, idx);
44 strs.append(m->data(tmpidx).toString());
45 }
46 }
47
48 // check all strings for a match
49 foreach (QString const& str, strs) {
50 #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
51 if (str.contains(this->filterRegularExpression())) {
52 #else
53 if (str.contains(this->filterRegExp())) {
54 #endif
55 return true;
56 }
57 }
58
59 return false;
60 }
61 };
62
63 // filter for searches
64 class QCMakeAdvancedFilter : public QSortFilterProxyModel
65 {
66 public:
QCMakeAdvancedFilter(QObject * o)67 QCMakeAdvancedFilter(QObject* o)
68 : QSortFilterProxyModel(o)
69 , ShowAdvanced(false)
70 {
71 }
72
setShowAdvanced(bool f)73 void setShowAdvanced(bool f)
74 {
75 this->ShowAdvanced = f;
76 this->invalidate();
77 }
showAdvanced() const78 bool showAdvanced() const { return this->ShowAdvanced; }
79
80 protected:
81 bool ShowAdvanced;
82
filterAcceptsRow(int row,const QModelIndex & p) const83 bool filterAcceptsRow(int row, const QModelIndex& p) const override
84 {
85 const QAbstractItemModel* m = this->sourceModel();
86 QModelIndex idx = m->index(row, 0, p);
87
88 // if there are no children
89 if (!m->hasChildren(idx)) {
90 bool adv = m->data(idx, QCMakeCacheModel::AdvancedRole).toBool();
91 return !adv || this->ShowAdvanced;
92 }
93
94 // check children
95 int num = m->rowCount(idx);
96 for (int i = 0; i < num; i++) {
97 bool accept = this->filterAcceptsRow(i, idx);
98 if (accept) {
99 return true;
100 }
101 }
102 return false;
103 }
104 };
105
QCMakeCacheView(QWidget * p)106 QCMakeCacheView::QCMakeCacheView(QWidget* p)
107 : QTreeView(p)
108 {
109 // hook up our model and search/filter proxies
110 this->CacheModel = new QCMakeCacheModel(this);
111 this->AdvancedFilter = new QCMakeAdvancedFilter(this);
112 this->AdvancedFilter->setSourceModel(this->CacheModel);
113 this->AdvancedFilter->setDynamicSortFilter(true);
114 this->SearchFilter = new QCMakeSearchFilter(this);
115 this->SearchFilter->setSourceModel(this->AdvancedFilter);
116 this->SearchFilter->setFilterCaseSensitivity(Qt::CaseInsensitive);
117 this->SearchFilter->setDynamicSortFilter(true);
118 this->setModel(this->SearchFilter);
119
120 // our delegate for creating our editors
121 QCMakeCacheModelDelegate* delegate = new QCMakeCacheModelDelegate(this);
122 this->setItemDelegate(delegate);
123
124 this->setUniformRowHeights(true);
125
126 this->setEditTriggers(QAbstractItemView::AllEditTriggers);
127
128 // tab, backtab doesn't step through items
129 this->setTabKeyNavigation(false);
130
131 this->setRootIsDecorated(false);
132 }
133
event(QEvent * e)134 bool QCMakeCacheView::event(QEvent* e)
135 {
136 if (e->type() == QEvent::Show) {
137 this->header()->setDefaultSectionSize(this->viewport()->width() / 2);
138 }
139 return QTreeView::event(e);
140 }
141
cacheModel() const142 QCMakeCacheModel* QCMakeCacheView::cacheModel() const
143 {
144 return this->CacheModel;
145 }
146
moveCursor(CursorAction act,Qt::KeyboardModifiers mod)147 QModelIndex QCMakeCacheView::moveCursor(CursorAction act,
148 Qt::KeyboardModifiers mod)
149 {
150 // want home/end to go to begin/end of rows, not columns
151 if (act == MoveHome) {
152 return this->model()->index(0, 1);
153 }
154 if (act == MoveEnd) {
155 return this->model()->index(this->model()->rowCount() - 1, 1);
156 }
157 return QTreeView::moveCursor(act, mod);
158 }
159
setShowAdvanced(bool s)160 void QCMakeCacheView::setShowAdvanced(bool s)
161 {
162 this->SearchFilter->invalidate();
163 this->AdvancedFilter->setShowAdvanced(s);
164 }
165
showAdvanced() const166 bool QCMakeCacheView::showAdvanced() const
167 {
168 return this->AdvancedFilter->showAdvanced();
169 }
170
setSearchFilter(const QString & s)171 void QCMakeCacheView::setSearchFilter(const QString& s)
172 {
173 #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
174 this->SearchFilter->setFilterRegularExpression(s);
175 #else
176 this->SearchFilter->setFilterFixedString(s);
177 #endif
178 }
179
QCMakeCacheModel(QObject * p)180 QCMakeCacheModel::QCMakeCacheModel(QObject* p)
181 : QStandardItemModel(p)
182 , EditEnabled(true)
183 , NewPropertyCount(0)
184 , View(FlatView)
185 {
186 this->ShowNewProperties = true;
187 QStringList labels;
188 labels << tr("Name") << tr("Value");
189 this->setHorizontalHeaderLabels(labels);
190 }
191
192 QCMakeCacheModel::~QCMakeCacheModel() = default;
193
qHash(const QCMakeProperty & p)194 static uint qHash(const QCMakeProperty& p)
195 {
196 return qHash(p.Key);
197 }
198
setShowNewProperties(bool f)199 void QCMakeCacheModel::setShowNewProperties(bool f)
200 {
201 this->ShowNewProperties = f;
202 }
203
clear()204 void QCMakeCacheModel::clear()
205 {
206 this->QStandardItemModel::clear();
207 this->NewPropertyCount = 0;
208
209 QStringList labels;
210 labels << tr("Name") << tr("Value");
211 this->setHorizontalHeaderLabels(labels);
212 }
213
setProperties(const QCMakePropertyList & props)214 void QCMakeCacheModel::setProperties(const QCMakePropertyList& props)
215 {
216 this->beginResetModel();
217
218 QSet<QCMakeProperty> newProps;
219 QSet<QCMakeProperty> newProps2;
220
221 if (this->ShowNewProperties) {
222 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
223 newProps = props.toSet();
224 #else
225 newProps = QSet<QCMakeProperty>(props.begin(), props.end());
226 #endif
227 newProps2 = newProps;
228 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
229 QSet<QCMakeProperty> oldProps = this->properties().toSet();
230 #else
231 QCMakePropertyList const& oldPropsList = this->properties();
232 QSet<QCMakeProperty> oldProps =
233 QSet<QCMakeProperty>(oldPropsList.begin(), oldPropsList.end());
234 #endif
235 oldProps.intersect(newProps);
236 newProps.subtract(oldProps);
237 newProps2.subtract(newProps);
238 } else {
239 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
240 newProps2 = props.toSet();
241 #else
242 newProps2 = QSet<QCMakeProperty>(props.begin(), props.end());
243 #endif
244 }
245
246 bool b = this->blockSignals(true);
247
248 this->clear();
249 this->NewPropertyCount = newProps.size();
250
251 if (View == FlatView) {
252 QCMakePropertyList newP = newProps.values();
253 QCMakePropertyList newP2 = newProps2.values();
254 #if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)
255 std::sort(newP.begin(), newP.end());
256 std::sort(newP2.begin(), newP2.end());
257 #else
258 qSort(newP);
259 qSort(newP2);
260 #endif
261 int row_count = 0;
262 foreach (QCMakeProperty const& p, newP) {
263 this->insertRow(row_count);
264 this->setPropertyData(this->index(row_count, 0), p, true);
265 row_count++;
266 }
267 foreach (QCMakeProperty const& p, newP2) {
268 this->insertRow(row_count);
269 this->setPropertyData(this->index(row_count, 0), p, false);
270 row_count++;
271 }
272 } else if (this->View == GroupView) {
273 QMap<QString, QCMakePropertyList> newPropsTree;
274 QCMakeCacheModel::breakProperties(newProps, newPropsTree);
275 QMap<QString, QCMakePropertyList> newPropsTree2;
276 QCMakeCacheModel::breakProperties(newProps2, newPropsTree2);
277
278 QStandardItem* root = this->invisibleRootItem();
279
280 for (QMap<QString, QCMakePropertyList>::const_iterator iter =
281 newPropsTree.begin();
282 iter != newPropsTree.end(); ++iter) {
283 QString const& key = iter.key();
284 QCMakePropertyList const& props2 = iter.value();
285
286 QList<QStandardItem*> parentItems;
287 parentItems.append(
288 new QStandardItem(key.isEmpty() ? tr("Ungrouped Entries") : key));
289 parentItems.append(new QStandardItem());
290 #if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)
291 parentItems[0]->setData(QBrush(QColor(255, 100, 100)),
292 Qt::BackgroundRole);
293 parentItems[1]->setData(QBrush(QColor(255, 100, 100)),
294 Qt::BackgroundRole);
295 #else
296 parentItems[0]->setData(QBrush(QColor(255, 100, 100)),
297 Qt::BackgroundColorRole);
298 parentItems[1]->setData(QBrush(QColor(255, 100, 100)),
299 Qt::BackgroundColorRole);
300 #endif
301 parentItems[0]->setData(1, GroupRole);
302 parentItems[1]->setData(1, GroupRole);
303 root->appendRow(parentItems);
304
305 int num = props2.size();
306 for (int i = 0; i < num; i++) {
307 QCMakeProperty prop = props2[i];
308 QList<QStandardItem*> items;
309 items.append(new QStandardItem());
310 items.append(new QStandardItem());
311 parentItems[0]->appendRow(items);
312 this->setPropertyData(this->indexFromItem(items[0]), prop, true);
313 }
314 }
315
316 for (QMap<QString, QCMakePropertyList>::const_iterator iter =
317 newPropsTree2.begin();
318 iter != newPropsTree2.end(); ++iter) {
319 QString const& key = iter.key();
320 QCMakePropertyList const& props2 = iter.value();
321
322 QStandardItem* parentItem =
323 new QStandardItem(key.isEmpty() ? tr("Ungrouped Entries") : key);
324 root->appendRow(parentItem);
325 parentItem->setData(1, GroupRole);
326
327 int num = props2.size();
328 for (int i = 0; i < num; i++) {
329 QCMakeProperty prop = props2[i];
330 QList<QStandardItem*> items;
331 items.append(new QStandardItem());
332 items.append(new QStandardItem());
333 parentItem->appendRow(items);
334 this->setPropertyData(this->indexFromItem(items[0]), prop, false);
335 }
336 }
337 }
338
339 this->blockSignals(b);
340 this->endResetModel();
341 }
342
viewType() const343 QCMakeCacheModel::ViewType QCMakeCacheModel::viewType() const
344 {
345 return this->View;
346 }
347
setViewType(QCMakeCacheModel::ViewType t)348 void QCMakeCacheModel::setViewType(QCMakeCacheModel::ViewType t)
349 {
350 this->beginResetModel();
351
352 this->View = t;
353
354 QCMakePropertyList props = this->properties();
355 QCMakePropertyList oldProps;
356 int numNew = this->NewPropertyCount;
357 int numTotal = props.count();
358 for (int i = numNew; i < numTotal; i++) {
359 oldProps.append(props[i]);
360 }
361
362 bool b = this->blockSignals(true);
363 this->clear();
364 this->setProperties(oldProps);
365 this->setProperties(props);
366 this->blockSignals(b);
367 this->endResetModel();
368 }
369
setPropertyData(const QModelIndex & idx1,const QCMakeProperty & prop,bool isNew)370 void QCMakeCacheModel::setPropertyData(const QModelIndex& idx1,
371 const QCMakeProperty& prop, bool isNew)
372 {
373 QModelIndex idx2 = idx1.sibling(idx1.row(), 1);
374
375 this->setData(idx1, prop.Key, Qt::DisplayRole);
376 this->setData(idx1, prop.Help, QCMakeCacheModel::HelpRole);
377 this->setData(idx1, prop.Type, QCMakeCacheModel::TypeRole);
378 this->setData(idx1, prop.Advanced, QCMakeCacheModel::AdvancedRole);
379
380 if (prop.Type == QCMakeProperty::BOOL) {
381 int check = prop.Value.toBool() ? Qt::Checked : Qt::Unchecked;
382 this->setData(idx2, check, Qt::CheckStateRole);
383 } else {
384 this->setData(idx2, prop.Value, Qt::DisplayRole);
385 }
386 this->setData(idx2, prop.Help, QCMakeCacheModel::HelpRole);
387
388 if (!prop.Strings.isEmpty()) {
389 this->setData(idx1, prop.Strings, QCMakeCacheModel::StringsRole);
390 }
391
392 if (isNew) {
393 #if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)
394 this->setData(idx1, QBrush(QColor(255, 100, 100)), Qt::BackgroundRole);
395 this->setData(idx2, QBrush(QColor(255, 100, 100)), Qt::BackgroundRole);
396 #else
397 this->setData(idx1, QBrush(QColor(255, 100, 100)),
398 Qt::BackgroundColorRole);
399 this->setData(idx2, QBrush(QColor(255, 100, 100)),
400 Qt::BackgroundColorRole);
401 #endif
402 }
403 }
404
getPropertyData(const QModelIndex & idx1,QCMakeProperty & prop) const405 void QCMakeCacheModel::getPropertyData(const QModelIndex& idx1,
406 QCMakeProperty& prop) const
407 {
408 QModelIndex idx2 = idx1.sibling(idx1.row(), 1);
409
410 prop.Key = this->data(idx1, Qt::DisplayRole).toString();
411 prop.Help = this->data(idx1, HelpRole).toString();
412 prop.Type = static_cast<QCMakeProperty::PropertyType>(
413 this->data(idx1, TypeRole).toInt());
414 prop.Advanced = this->data(idx1, AdvancedRole).toBool();
415 prop.Strings =
416 this->data(idx1, QCMakeCacheModel::StringsRole).toStringList();
417 if (prop.Type == QCMakeProperty::BOOL) {
418 int check = this->data(idx2, Qt::CheckStateRole).toInt();
419 prop.Value = check == Qt::Checked;
420 } else {
421 prop.Value = this->data(idx2, Qt::DisplayRole).toString();
422 }
423 }
424
prefix(const QString & s)425 QString QCMakeCacheModel::prefix(const QString& s)
426 {
427 QString prefix = s.section('_', 0, 0);
428 if (prefix == s) {
429 prefix = QString();
430 }
431 return prefix;
432 }
433
breakProperties(const QSet<QCMakeProperty> & props,QMap<QString,QCMakePropertyList> & result)434 void QCMakeCacheModel::breakProperties(
435 const QSet<QCMakeProperty>& props, QMap<QString, QCMakePropertyList>& result)
436 {
437 QMap<QString, QCMakePropertyList> tmp;
438 // return a map of properties grouped by prefixes, and sorted
439 foreach (QCMakeProperty const& p, props) {
440 QString prefix = QCMakeCacheModel::prefix(p.Key);
441 tmp[prefix].append(p);
442 }
443 // sort it and re-org any properties with only one sub item
444 QCMakePropertyList reorgProps;
445 QMap<QString, QCMakePropertyList>::iterator iter;
446 for (iter = tmp.begin(); iter != tmp.end();) {
447 if (iter->count() == 1) {
448 reorgProps.append((*iter)[0]);
449 iter = tmp.erase(iter);
450 } else {
451 #if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)
452 std::sort(iter->begin(), iter->end());
453 #else
454 qSort(*iter);
455 #endif
456 ++iter;
457 }
458 }
459 if (reorgProps.count()) {
460 tmp[QString()] += reorgProps;
461 }
462 result = tmp;
463 }
464
properties() const465 QCMakePropertyList QCMakeCacheModel::properties() const
466 {
467 QCMakePropertyList props;
468
469 if (!this->rowCount()) {
470 return props;
471 }
472
473 QVector<QModelIndex> idxs;
474 idxs.append(this->index(0, 0));
475
476 // walk the entire model for property entries
477 // this works regardless of a flat view or a tree view
478 while (!idxs.isEmpty()) {
479 QModelIndex idx = idxs.last();
480 if (this->hasChildren(idx) && this->rowCount(idx)) {
481 idxs.append(this->index(0, 0, idx));
482 } else {
483 if (!data(idx, GroupRole).toInt()) {
484 // get data
485 QCMakeProperty prop;
486 this->getPropertyData(idx, prop);
487 props.append(prop);
488 }
489
490 // go to the next in the tree
491 while (!idxs.isEmpty() &&
492 (
493 #if QT_VERSION < QT_VERSION_CHECK(5, 1, 0)
494 (idxs.last().row() + 1) >= rowCount(idxs.last().parent()) ||
495 #endif
496 !idxs.last().sibling(idxs.last().row() + 1, 0).isValid())) {
497 idxs.remove(idxs.size() - 1);
498 }
499 if (!idxs.isEmpty()) {
500 idxs.last() = idxs.last().sibling(idxs.last().row() + 1, 0);
501 }
502 }
503 }
504
505 return props;
506 }
507
insertProperty(QCMakeProperty::PropertyType t,const QString & name,const QString & description,const QVariant & value,bool advanced)508 bool QCMakeCacheModel::insertProperty(QCMakeProperty::PropertyType t,
509 const QString& name,
510 const QString& description,
511 const QVariant& value, bool advanced)
512 {
513 QCMakeProperty prop;
514 prop.Key = name;
515 prop.Value = value;
516 prop.Help = description;
517 prop.Type = t;
518 prop.Advanced = advanced;
519
520 // insert at beginning
521 this->insertRow(0);
522 this->setPropertyData(this->index(0, 0), prop, true);
523 this->NewPropertyCount++;
524 return true;
525 }
526
setEditEnabled(bool e)527 void QCMakeCacheModel::setEditEnabled(bool e)
528 {
529 this->EditEnabled = e;
530 }
531
editEnabled() const532 bool QCMakeCacheModel::editEnabled() const
533 {
534 return this->EditEnabled;
535 }
536
newPropertyCount() const537 int QCMakeCacheModel::newPropertyCount() const
538 {
539 return this->NewPropertyCount;
540 }
541
flags(const QModelIndex & idx) const542 Qt::ItemFlags QCMakeCacheModel::flags(const QModelIndex& idx) const
543 {
544 Qt::ItemFlags f = QStandardItemModel::flags(idx);
545 if (!this->EditEnabled) {
546 f &= ~Qt::ItemIsEditable;
547 return f;
548 }
549 if (QCMakeProperty::BOOL == this->data(idx, TypeRole).toInt()) {
550 f |= Qt::ItemIsUserCheckable;
551 }
552 return f;
553 }
554
buddy(const QModelIndex & idx) const555 QModelIndex QCMakeCacheModel::buddy(const QModelIndex& idx) const
556 {
557 if (!this->hasChildren(idx) &&
558 this->data(idx, TypeRole).toInt() != QCMakeProperty::BOOL) {
559 return this->index(idx.row(), 1, idx.parent());
560 }
561 return idx;
562 }
563
QCMakeCacheModelDelegate(QObject * p)564 QCMakeCacheModelDelegate::QCMakeCacheModelDelegate(QObject* p)
565 : QItemDelegate(p)
566 , FileDialogFlag(false)
567 {
568 }
569
setFileDialogFlag(bool f)570 void QCMakeCacheModelDelegate::setFileDialogFlag(bool f)
571 {
572 this->FileDialogFlag = f;
573 }
574
createEditor(QWidget * p,const QStyleOptionViewItem &,const QModelIndex & idx) const575 QWidget* QCMakeCacheModelDelegate::createEditor(
576 QWidget* p, const QStyleOptionViewItem& /*option*/,
577 const QModelIndex& idx) const
578 {
579 QModelIndex var = idx.sibling(idx.row(), 0);
580 int type = var.data(QCMakeCacheModel::TypeRole).toInt();
581 if (type == QCMakeProperty::BOOL) {
582 return nullptr;
583 }
584 if (type == QCMakeProperty::PATH) {
585 QCMakePathEditor* editor =
586 new QCMakePathEditor(p, var.data(Qt::DisplayRole).toString());
587 QObject::connect(editor, &QCMakePathEditor::fileDialogExists, this,
588 &QCMakeCacheModelDelegate::setFileDialogFlag);
589 return editor;
590 }
591 if (type == QCMakeProperty::FILEPATH) {
592 QCMakeFilePathEditor* editor =
593 new QCMakeFilePathEditor(p, var.data(Qt::DisplayRole).toString());
594 QObject::connect(editor, &QCMakePathEditor::fileDialogExists, this,
595 &QCMakeCacheModelDelegate::setFileDialogFlag);
596 return editor;
597 }
598 if (type == QCMakeProperty::STRING &&
599 var.data(QCMakeCacheModel::StringsRole).isValid()) {
600 QCMakeComboBox* editor = new QCMakeComboBox(
601 p, var.data(QCMakeCacheModel::StringsRole).toStringList());
602 editor->setFrame(false);
603 return editor;
604 }
605
606 QLineEdit* editor = new QLineEdit(p);
607 editor->setFrame(false);
608 return editor;
609 }
610
editorEvent(QEvent * e,QAbstractItemModel * model,const QStyleOptionViewItem & option,const QModelIndex & index)611 bool QCMakeCacheModelDelegate::editorEvent(QEvent* e,
612 QAbstractItemModel* model,
613 const QStyleOptionViewItem& option,
614 const QModelIndex& index)
615 {
616 Qt::ItemFlags flags = model->flags(index);
617 if (!(flags & Qt::ItemIsUserCheckable) ||
618 !(option.state & QStyle::State_Enabled) ||
619 !(flags & Qt::ItemIsEnabled)) {
620 return false;
621 }
622
623 QVariant value = index.data(Qt::CheckStateRole);
624 if (!value.isValid()) {
625 return false;
626 }
627
628 if ((e->type() == QEvent::MouseButtonRelease) ||
629 (e->type() == QEvent::MouseButtonDblClick)) {
630 // eat the double click events inside the check rect
631 if (e->type() == QEvent::MouseButtonDblClick) {
632 return true;
633 }
634 } else if (e->type() == QEvent::KeyPress) {
635 if (static_cast<QKeyEvent*>(e)->key() != Qt::Key_Space &&
636 static_cast<QKeyEvent*>(e)->key() != Qt::Key_Select) {
637 return false;
638 }
639 } else {
640 return false;
641 }
642
643 Qt::CheckState state =
644 (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked ? Qt::Unchecked
645 : Qt::Checked);
646 bool success = model->setData(index, state, Qt::CheckStateRole);
647 if (success) {
648 this->recordChange(model, index);
649 }
650 return success;
651 }
652
eventFilter(QObject * object,QEvent * evt)653 bool QCMakeCacheModelDelegate::eventFilter(QObject* object, QEvent* evt)
654 {
655 // FIXME: This filter avoids a crash when opening a file dialog
656 // with the '...' button on a cache entry line in the GUI.
657 // Previously this filter was commented as a workaround for Qt issue 205903,
658 // but that was fixed in Qt 4.5.0 and the crash still occurs as of Qt 5.14
659 // without this filter. This needs further investigation.
660 if (evt->type() == QEvent::FocusOut && this->FileDialogFlag) {
661 return false;
662 }
663 return QItemDelegate::eventFilter(object, evt);
664 }
665
setModelData(QWidget * editor,QAbstractItemModel * model,const QModelIndex & index) const666 void QCMakeCacheModelDelegate::setModelData(QWidget* editor,
667 QAbstractItemModel* model,
668 const QModelIndex& index) const
669 {
670 QItemDelegate::setModelData(editor, model, index);
671 const_cast<QCMakeCacheModelDelegate*>(this)->recordChange(model, index);
672 }
673
sizeHint(const QStyleOptionViewItem & option,const QModelIndex & index) const674 QSize QCMakeCacheModelDelegate::sizeHint(const QStyleOptionViewItem& option,
675 const QModelIndex& index) const
676 {
677 QSize sz = QItemDelegate::sizeHint(option, index);
678 QStyle* style = QApplication::style();
679
680 // increase to checkbox size
681 QStyleOptionButton opt;
682 opt.QStyleOption::operator=(option);
683 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
684 sz = sz.expandedTo(
685 style->subElementRect(QStyle::SE_ItemViewItemCheckIndicator, &opt, nullptr)
686 .size());
687 #else
688 sz = sz.expandedTo(
689 style->subElementRect(QStyle::SE_ViewItemCheckIndicator, &opt, nullptr)
690 .size());
691 #endif
692
693 return sz;
694 }
695
changes() const696 QSet<QCMakeProperty> QCMakeCacheModelDelegate::changes() const
697 {
698 return mChanges;
699 }
700
clearChanges()701 void QCMakeCacheModelDelegate::clearChanges()
702 {
703 mChanges.clear();
704 }
705
recordChange(QAbstractItemModel * model,const QModelIndex & index)706 void QCMakeCacheModelDelegate::recordChange(QAbstractItemModel* model,
707 const QModelIndex& index)
708 {
709 QModelIndex idx = index;
710 QAbstractItemModel* mymodel = model;
711 while (qobject_cast<QAbstractProxyModel*>(mymodel)) {
712 idx = static_cast<QAbstractProxyModel*>(mymodel)->mapToSource(idx);
713 mymodel = static_cast<QAbstractProxyModel*>(mymodel)->sourceModel();
714 }
715 QCMakeCacheModel* cache_model = qobject_cast<QCMakeCacheModel*>(mymodel);
716 if (cache_model && idx.isValid()) {
717 QCMakeProperty prop;
718 idx = idx.sibling(idx.row(), 0);
719 cache_model->getPropertyData(idx, prop);
720
721 // clean out an old one
722 QSet<QCMakeProperty>::iterator iter = mChanges.find(prop);
723 if (iter != mChanges.end()) {
724 mChanges.erase(iter);
725 }
726 // now add the new item
727 mChanges.insert(prop);
728 }
729 }
730