QCMakeCacheView.cxx 21 KB

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