QCMakeCacheView.cxx 19 KB

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