QCMakeCacheView.cxx 19 KB

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