QCMakeCacheView.cxx 21 KB

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