QCMakeCacheView.cxx 16 KB

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