QCMakeCacheView.cxx 19 KB

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