QCMakeCacheView.cxx 19 KB

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