QCMakeCacheView.cxx 21 KB

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