QCMakeCacheView.cxx 21 KB

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