QCMakeCacheView.cxx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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. bool QCMakeCacheView::setSearchFilter(const QString& s)
  147. {
  148. return QtCMake::setSearchFilter(this->SearchFilter, s);
  149. }
  150. QCMakeCacheModel::QCMakeCacheModel(QObject* p)
  151. : QStandardItemModel(p)
  152. , EditEnabled(true)
  153. , NewPropertyCount(0)
  154. , View(FlatView)
  155. {
  156. this->ShowNewProperties = true;
  157. QStringList labels;
  158. labels << tr("Name") << tr("Value");
  159. this->setHorizontalHeaderLabels(labels);
  160. }
  161. QCMakeCacheModel::~QCMakeCacheModel() = default;
  162. static uint qHash(const QCMakeProperty& p)
  163. {
  164. return qHash(p.Key);
  165. }
  166. void QCMakeCacheModel::setShowNewProperties(bool f)
  167. {
  168. this->ShowNewProperties = f;
  169. }
  170. void QCMakeCacheModel::clear()
  171. {
  172. this->QStandardItemModel::clear();
  173. this->NewPropertyCount = 0;
  174. QStringList labels;
  175. labels << tr("Name") << tr("Value");
  176. this->setHorizontalHeaderLabels(labels);
  177. }
  178. void QCMakeCacheModel::setProperties(const QCMakePropertyList& props)
  179. {
  180. this->beginResetModel();
  181. QSet<QCMakeProperty> newProps;
  182. QSet<QCMakeProperty> newProps2;
  183. if (this->ShowNewProperties) {
  184. #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
  185. newProps = props.toSet();
  186. #else
  187. newProps = QSet<QCMakeProperty>(props.begin(), props.end());
  188. #endif
  189. newProps2 = newProps;
  190. #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
  191. QSet<QCMakeProperty> oldProps = this->properties().toSet();
  192. #else
  193. QCMakePropertyList const& oldPropsList = this->properties();
  194. QSet<QCMakeProperty> oldProps =
  195. QSet<QCMakeProperty>(oldPropsList.begin(), oldPropsList.end());
  196. #endif
  197. oldProps.intersect(newProps);
  198. newProps.subtract(oldProps);
  199. newProps2.subtract(newProps);
  200. } else {
  201. #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
  202. newProps2 = props.toSet();
  203. #else
  204. newProps2 = QSet<QCMakeProperty>(props.begin(), props.end());
  205. #endif
  206. }
  207. bool b = this->blockSignals(true);
  208. this->clear();
  209. this->NewPropertyCount = newProps.size();
  210. if (View == FlatView) {
  211. QCMakePropertyList newP = newProps.values();
  212. QCMakePropertyList newP2 = newProps2.values();
  213. #if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)
  214. std::sort(newP.begin(), newP.end());
  215. std::sort(newP2.begin(), newP2.end());
  216. #else
  217. qSort(newP);
  218. qSort(newP2);
  219. #endif
  220. int row_count = 0;
  221. foreach (QCMakeProperty const& p, newP) {
  222. this->insertRow(row_count);
  223. this->setPropertyData(this->index(row_count, 0), p, true);
  224. row_count++;
  225. }
  226. foreach (QCMakeProperty const& p, newP2) {
  227. this->insertRow(row_count);
  228. this->setPropertyData(this->index(row_count, 0), p, false);
  229. row_count++;
  230. }
  231. } else if (this->View == GroupView) {
  232. QMap<QString, QCMakePropertyList> newPropsTree;
  233. QCMakeCacheModel::breakProperties(newProps, newPropsTree);
  234. QMap<QString, QCMakePropertyList> newPropsTree2;
  235. QCMakeCacheModel::breakProperties(newProps2, newPropsTree2);
  236. QStandardItem* root = this->invisibleRootItem();
  237. for (QMap<QString, QCMakePropertyList>::const_iterator iter =
  238. newPropsTree.begin();
  239. iter != newPropsTree.end(); ++iter) {
  240. QString const& key = iter.key();
  241. QCMakePropertyList const& props2 = iter.value();
  242. QList<QStandardItem*> parentItems;
  243. parentItems.append(
  244. new QStandardItem(key.isEmpty() ? tr("Ungrouped Entries") : key));
  245. parentItems.append(new QStandardItem());
  246. #if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)
  247. parentItems[0]->setData(QBrush(QColor(255, 100, 100)),
  248. Qt::BackgroundRole);
  249. parentItems[1]->setData(QBrush(QColor(255, 100, 100)),
  250. Qt::BackgroundRole);
  251. #else
  252. parentItems[0]->setData(QBrush(QColor(255, 100, 100)),
  253. Qt::BackgroundColorRole);
  254. parentItems[1]->setData(QBrush(QColor(255, 100, 100)),
  255. Qt::BackgroundColorRole);
  256. #endif
  257. parentItems[0]->setData(1, GroupRole);
  258. parentItems[1]->setData(1, GroupRole);
  259. root->appendRow(parentItems);
  260. int num = props2.size();
  261. for (int i = 0; i < num; i++) {
  262. QCMakeProperty const& prop = props2[i];
  263. QList<QStandardItem*> items;
  264. items.append(new QStandardItem());
  265. items.append(new QStandardItem());
  266. parentItems[0]->appendRow(items);
  267. this->setPropertyData(this->indexFromItem(items[0]), prop, true);
  268. }
  269. }
  270. for (QMap<QString, QCMakePropertyList>::const_iterator iter =
  271. newPropsTree2.begin();
  272. iter != newPropsTree2.end(); ++iter) {
  273. QString const& key = iter.key();
  274. QCMakePropertyList const& props2 = iter.value();
  275. QStandardItem* parentItem =
  276. new QStandardItem(key.isEmpty() ? tr("Ungrouped Entries") : key);
  277. root->appendRow(parentItem);
  278. parentItem->setData(1, GroupRole);
  279. int num = props2.size();
  280. for (int i = 0; i < num; i++) {
  281. QCMakeProperty const& prop = props2[i];
  282. QList<QStandardItem*> items;
  283. items.append(new QStandardItem());
  284. items.append(new QStandardItem());
  285. parentItem->appendRow(items);
  286. this->setPropertyData(this->indexFromItem(items[0]), prop, false);
  287. }
  288. }
  289. }
  290. this->blockSignals(b);
  291. this->endResetModel();
  292. }
  293. QCMakeCacheModel::ViewType QCMakeCacheModel::viewType() const
  294. {
  295. return this->View;
  296. }
  297. void QCMakeCacheModel::setViewType(QCMakeCacheModel::ViewType t)
  298. {
  299. this->beginResetModel();
  300. this->View = t;
  301. QCMakePropertyList props = this->properties();
  302. QCMakePropertyList oldProps;
  303. int numNew = this->NewPropertyCount;
  304. int numTotal = props.count();
  305. for (int i = numNew; i < numTotal; i++) {
  306. oldProps.append(props[i]);
  307. }
  308. bool b = this->blockSignals(true);
  309. this->clear();
  310. this->setProperties(oldProps);
  311. this->setProperties(props);
  312. this->blockSignals(b);
  313. this->endResetModel();
  314. }
  315. void QCMakeCacheModel::setPropertyData(const QModelIndex& idx1,
  316. const QCMakeProperty& prop, bool isNew)
  317. {
  318. QModelIndex idx2 = idx1.sibling(idx1.row(), 1);
  319. this->setData(idx1, prop.Key, Qt::DisplayRole);
  320. this->setData(idx1, prop.Help, QCMakeCacheModel::HelpRole);
  321. this->setData(idx1, prop.Type, QCMakeCacheModel::TypeRole);
  322. this->setData(idx1, prop.Advanced, QCMakeCacheModel::AdvancedRole);
  323. if (prop.Type == QCMakeProperty::BOOL) {
  324. int check = prop.Value.toBool() ? Qt::Checked : Qt::Unchecked;
  325. this->setData(idx2, check, Qt::CheckStateRole);
  326. } else {
  327. this->setData(idx2, prop.Value, Qt::DisplayRole);
  328. }
  329. this->setData(idx2, prop.Help, QCMakeCacheModel::HelpRole);
  330. if (!prop.Strings.isEmpty()) {
  331. this->setData(idx1, prop.Strings, QCMakeCacheModel::StringsRole);
  332. }
  333. if (isNew) {
  334. #if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)
  335. this->setData(idx1, QBrush(QColor(255, 100, 100)), Qt::BackgroundRole);
  336. this->setData(idx2, QBrush(QColor(255, 100, 100)), Qt::BackgroundRole);
  337. #else
  338. this->setData(idx1, QBrush(QColor(255, 100, 100)),
  339. Qt::BackgroundColorRole);
  340. this->setData(idx2, QBrush(QColor(255, 100, 100)),
  341. Qt::BackgroundColorRole);
  342. #endif
  343. }
  344. }
  345. void QCMakeCacheModel::getPropertyData(const QModelIndex& idx1,
  346. QCMakeProperty& prop) const
  347. {
  348. QModelIndex idx2 = idx1.sibling(idx1.row(), 1);
  349. prop.Key = this->data(idx1, Qt::DisplayRole).toString();
  350. prop.Help = this->data(idx1, HelpRole).toString();
  351. prop.Type = static_cast<QCMakeProperty::PropertyType>(
  352. this->data(idx1, TypeRole).toInt());
  353. prop.Advanced = this->data(idx1, AdvancedRole).toBool();
  354. prop.Strings =
  355. this->data(idx1, QCMakeCacheModel::StringsRole).toStringList();
  356. if (prop.Type == QCMakeProperty::BOOL) {
  357. int check = this->data(idx2, Qt::CheckStateRole).toInt();
  358. prop.Value = check == Qt::Checked;
  359. } else {
  360. prop.Value = this->data(idx2, Qt::DisplayRole).toString();
  361. }
  362. }
  363. QString QCMakeCacheModel::prefix(const QString& s)
  364. {
  365. QString prefix = s.section('_', 0, 0);
  366. if (prefix == s) {
  367. prefix = QString();
  368. }
  369. return prefix;
  370. }
  371. void QCMakeCacheModel::breakProperties(
  372. const QSet<QCMakeProperty>& props, QMap<QString, QCMakePropertyList>& result)
  373. {
  374. QMap<QString, QCMakePropertyList> tmp;
  375. // return a map of properties grouped by prefixes, and sorted
  376. foreach (QCMakeProperty const& p, props) {
  377. QString prefix = QCMakeCacheModel::prefix(p.Key);
  378. tmp[prefix].append(p);
  379. }
  380. // sort it and re-org any properties with only one sub item
  381. QCMakePropertyList reorgProps;
  382. QMap<QString, QCMakePropertyList>::iterator iter;
  383. for (iter = tmp.begin(); iter != tmp.end();) {
  384. if (iter->count() == 1) {
  385. reorgProps.append((*iter)[0]);
  386. iter = tmp.erase(iter);
  387. } else {
  388. #if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)
  389. std::sort(iter->begin(), iter->end());
  390. #else
  391. qSort(*iter);
  392. #endif
  393. ++iter;
  394. }
  395. }
  396. if (reorgProps.count()) {
  397. tmp[QString()] += reorgProps;
  398. }
  399. result = tmp;
  400. }
  401. QCMakePropertyList QCMakeCacheModel::properties() const
  402. {
  403. QCMakePropertyList props;
  404. if (!this->rowCount()) {
  405. return props;
  406. }
  407. QVector<QModelIndex> idxs;
  408. idxs.append(this->index(0, 0));
  409. // walk the entire model for property entries
  410. // this works regardless of a flat view or a tree view
  411. while (!idxs.isEmpty()) {
  412. QModelIndex idx = idxs.last();
  413. if (this->hasChildren(idx) && this->rowCount(idx)) {
  414. idxs.append(this->index(0, 0, idx));
  415. } else {
  416. if (!data(idx, GroupRole).toInt()) {
  417. // get data
  418. QCMakeProperty prop;
  419. this->getPropertyData(idx, prop);
  420. props.append(prop);
  421. }
  422. // go to the next in the tree
  423. while (!idxs.isEmpty() &&
  424. (
  425. #if QT_VERSION < QT_VERSION_CHECK(5, 1, 0)
  426. (idxs.last().row() + 1) >= rowCount(idxs.last().parent()) ||
  427. #endif
  428. !idxs.last().sibling(idxs.last().row() + 1, 0).isValid())) {
  429. idxs.remove(idxs.size() - 1);
  430. }
  431. if (!idxs.isEmpty()) {
  432. idxs.last() = idxs.last().sibling(idxs.last().row() + 1, 0);
  433. }
  434. }
  435. }
  436. return props;
  437. }
  438. bool QCMakeCacheModel::insertProperty(QCMakeProperty::PropertyType t,
  439. const QString& name,
  440. const QString& description,
  441. const QVariant& value, bool advanced)
  442. {
  443. QCMakeProperty prop;
  444. prop.Key = name;
  445. prop.Value = value;
  446. prop.Help = description;
  447. prop.Type = t;
  448. prop.Advanced = advanced;
  449. // insert at beginning
  450. this->insertRow(0);
  451. this->setPropertyData(this->index(0, 0), prop, true);
  452. this->NewPropertyCount++;
  453. return true;
  454. }
  455. void QCMakeCacheModel::setEditEnabled(bool e)
  456. {
  457. this->EditEnabled = e;
  458. }
  459. bool QCMakeCacheModel::editEnabled() const
  460. {
  461. return this->EditEnabled;
  462. }
  463. int QCMakeCacheModel::newPropertyCount() const
  464. {
  465. return this->NewPropertyCount;
  466. }
  467. Qt::ItemFlags QCMakeCacheModel::flags(const QModelIndex& idx) const
  468. {
  469. Qt::ItemFlags f = QStandardItemModel::flags(idx);
  470. if (!this->EditEnabled) {
  471. f &= ~Qt::ItemIsEditable;
  472. return f;
  473. }
  474. if (QCMakeProperty::BOOL == this->data(idx, TypeRole).toInt()) {
  475. f |= Qt::ItemIsUserCheckable;
  476. }
  477. return f;
  478. }
  479. QModelIndex QCMakeCacheModel::buddy(const QModelIndex& idx) const
  480. {
  481. if (!this->hasChildren(idx) &&
  482. this->data(idx, TypeRole).toInt() != QCMakeProperty::BOOL) {
  483. return this->index(idx.row(), 1, idx.parent());
  484. }
  485. return idx;
  486. }
  487. QCMakeCacheModelDelegate::QCMakeCacheModelDelegate(QObject* p)
  488. : QItemDelegate(p)
  489. , FileDialogFlag(false)
  490. {
  491. }
  492. void QCMakeCacheModelDelegate::setFileDialogFlag(bool f)
  493. {
  494. this->FileDialogFlag = f;
  495. }
  496. QWidget* QCMakeCacheModelDelegate::createEditor(
  497. QWidget* p, const QStyleOptionViewItem& /*option*/,
  498. const QModelIndex& idx) const
  499. {
  500. QModelIndex var = idx.sibling(idx.row(), 0);
  501. int type = var.data(QCMakeCacheModel::TypeRole).toInt();
  502. if (type == QCMakeProperty::BOOL) {
  503. return nullptr;
  504. }
  505. if (type == QCMakeProperty::PATH) {
  506. QCMakePathEditor* editor =
  507. new QCMakePathEditor(p, var.data(Qt::DisplayRole).toString());
  508. QObject::connect(editor, &QCMakePathEditor::fileDialogExists, this,
  509. &QCMakeCacheModelDelegate::setFileDialogFlag);
  510. return editor;
  511. }
  512. if (type == QCMakeProperty::FILEPATH) {
  513. QCMakeFilePathEditor* editor =
  514. new QCMakeFilePathEditor(p, var.data(Qt::DisplayRole).toString());
  515. QObject::connect(editor, &QCMakePathEditor::fileDialogExists, this,
  516. &QCMakeCacheModelDelegate::setFileDialogFlag);
  517. return editor;
  518. }
  519. if (type == QCMakeProperty::STRING &&
  520. var.data(QCMakeCacheModel::StringsRole).isValid()) {
  521. QCMakeComboBox* editor = new QCMakeComboBox(
  522. p, var.data(QCMakeCacheModel::StringsRole).toStringList());
  523. editor->setFrame(false);
  524. return editor;
  525. }
  526. QLineEdit* editor = new QLineEdit(p);
  527. editor->setFrame(false);
  528. return editor;
  529. }
  530. bool QCMakeCacheModelDelegate::editorEvent(QEvent* e,
  531. QAbstractItemModel* model,
  532. const QStyleOptionViewItem& option,
  533. const QModelIndex& index)
  534. {
  535. Qt::ItemFlags flags = model->flags(index);
  536. if (!(flags & Qt::ItemIsUserCheckable) ||
  537. !(option.state & QStyle::State_Enabled) ||
  538. !(flags & Qt::ItemIsEnabled)) {
  539. return false;
  540. }
  541. QVariant value = index.data(Qt::CheckStateRole);
  542. if (!value.isValid()) {
  543. return false;
  544. }
  545. if ((e->type() == QEvent::MouseButtonRelease) ||
  546. (e->type() == QEvent::MouseButtonDblClick)) {
  547. // eat the double click events inside the check rect
  548. if (e->type() == QEvent::MouseButtonDblClick) {
  549. return true;
  550. }
  551. } else if (e->type() == QEvent::KeyPress) {
  552. if (static_cast<QKeyEvent*>(e)->key() != Qt::Key_Space &&
  553. static_cast<QKeyEvent*>(e)->key() != Qt::Key_Select) {
  554. return false;
  555. }
  556. } else {
  557. return false;
  558. }
  559. Qt::CheckState state =
  560. (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked ? Qt::Unchecked
  561. : Qt::Checked);
  562. bool success = model->setData(index, state, Qt::CheckStateRole);
  563. if (success) {
  564. this->recordChange(model, index);
  565. }
  566. return success;
  567. }
  568. bool QCMakeCacheModelDelegate::eventFilter(QObject* object, QEvent* evt)
  569. {
  570. // FIXME: This filter avoids a crash when opening a file dialog
  571. // with the '...' button on a cache entry line in the GUI.
  572. // Previously this filter was commented as a workaround for Qt issue 205903,
  573. // but that was fixed in Qt 4.5.0 and the crash still occurs as of Qt 5.14
  574. // without this filter. This needs further investigation.
  575. if (evt->type() == QEvent::FocusOut && this->FileDialogFlag) {
  576. return false;
  577. }
  578. return QItemDelegate::eventFilter(object, evt);
  579. }
  580. void QCMakeCacheModelDelegate::setModelData(QWidget* editor,
  581. QAbstractItemModel* model,
  582. const QModelIndex& index) const
  583. {
  584. QItemDelegate::setModelData(editor, model, index);
  585. const_cast<QCMakeCacheModelDelegate*>(this)->recordChange(model, index);
  586. }
  587. QSize QCMakeCacheModelDelegate::sizeHint(const QStyleOptionViewItem& option,
  588. const QModelIndex& index) const
  589. {
  590. QSize sz = QItemDelegate::sizeHint(option, index);
  591. QStyle* style = QApplication::style();
  592. // increase to checkbox size
  593. QStyleOptionButton opt;
  594. opt.QStyleOption::operator=(option);
  595. #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
  596. sz = sz.expandedTo(
  597. style->subElementRect(QStyle::SE_ItemViewItemCheckIndicator, &opt, nullptr)
  598. .size());
  599. #else
  600. sz = sz.expandedTo(
  601. style->subElementRect(QStyle::SE_ViewItemCheckIndicator, &opt, nullptr)
  602. .size());
  603. #endif
  604. return sz;
  605. }
  606. QSet<QCMakeProperty> QCMakeCacheModelDelegate::changes() const
  607. {
  608. return mChanges;
  609. }
  610. void QCMakeCacheModelDelegate::clearChanges()
  611. {
  612. mChanges.clear();
  613. }
  614. void QCMakeCacheModelDelegate::recordChange(QAbstractItemModel* model,
  615. const QModelIndex& index)
  616. {
  617. QModelIndex idx = index;
  618. QAbstractItemModel* mymodel = model;
  619. while (qobject_cast<QAbstractProxyModel*>(mymodel)) {
  620. idx = static_cast<QAbstractProxyModel*>(mymodel)->mapToSource(idx);
  621. mymodel = static_cast<QAbstractProxyModel*>(mymodel)->sourceModel();
  622. }
  623. QCMakeCacheModel* cache_model = qobject_cast<QCMakeCacheModel*>(mymodel);
  624. if (cache_model && idx.isValid()) {
  625. QCMakeProperty prop;
  626. idx = idx.sibling(idx.row(), 0);
  627. cache_model->getPropertyData(idx, prop);
  628. // clean out an old one
  629. QSet<QCMakeProperty>::iterator iter = mChanges.find(prop);
  630. if (iter != mChanges.end()) {
  631. mChanges.erase(iter);
  632. }
  633. // now add the new item
  634. mChanges.insert(prop);
  635. }
  636. }