QCMakeCacheView.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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 <QToolButton>
  15. #include <QFileDialog>
  16. #include <QHBoxLayout>
  17. #include <QHeaderView>
  18. #include <QEvent>
  19. #include <QFileInfo>
  20. #include <QStyle>
  21. #include <QKeyEvent>
  22. #include <QMenu>
  23. #include <QDirModel>
  24. #include <QCompleter>
  25. static QRegExp AdvancedRegExp[2] = { QRegExp("(false)"), QRegExp("(true|false)") };
  26. // filter for searches
  27. class QCMakeSearchFilter : public QSortFilterProxyModel
  28. {
  29. public:
  30. QCMakeSearchFilter(QObject* o) : QSortFilterProxyModel(o) {}
  31. protected:
  32. bool filterAcceptsRow(int row, const QModelIndex& p) const
  33. {
  34. // accept row if either column matches
  35. QModelIndex idx0 = this->sourceModel()->index(row, 0, p);
  36. QModelIndex idx1 = this->sourceModel()->index(row, 1, p);
  37. QString str0 = this->sourceModel()->data(idx0).toString();
  38. QString str1 = this->sourceModel()->data(idx1).toString();
  39. return str0.contains(this->filterRegExp()) ||
  40. str1.contains(this->filterRegExp());
  41. }
  42. };
  43. QCMakeCacheView::QCMakeCacheView(QWidget* p)
  44. : QTableView(p), Init(false)
  45. {
  46. // hook up our model and search/filter proxies
  47. this->CacheModel = new QCMakeCacheModel(this);
  48. this->AdvancedFilter = new QSortFilterProxyModel(this);
  49. this->AdvancedFilter->setSourceModel(this->CacheModel);
  50. this->AdvancedFilter->setFilterRole(QCMakeCacheModel::AdvancedRole);
  51. this->AdvancedFilter->setFilterRegExp(AdvancedRegExp[0]);
  52. this->AdvancedFilter->setDynamicSortFilter(true);
  53. this->SearchFilter = new QCMakeSearchFilter(this);
  54. this->SearchFilter->setSourceModel(this->AdvancedFilter);
  55. this->SearchFilter->setFilterCaseSensitivity(Qt::CaseInsensitive);
  56. this->SearchFilter->setDynamicSortFilter(true);
  57. this->setModel(this->SearchFilter);
  58. // our delegate for creating our editors
  59. QCMakeCacheModelDelegate* delegate = new QCMakeCacheModelDelegate(this);
  60. this->setItemDelegate(delegate);
  61. this->setEditTriggers(QAbstractItemView::AllEditTriggers);
  62. // set up headers and sizes
  63. int h = 0;
  64. QFontMetrics met(this->font());
  65. h = qMax(met.height(), this->style()->pixelMetric(QStyle::PM_IndicatorHeight));
  66. this->verticalHeader()->setDefaultSectionSize(h + 4);
  67. this->horizontalHeader()->setStretchLastSection(true);
  68. this->verticalHeader()->hide();
  69. }
  70. void QCMakeCacheView::showEvent(QShowEvent* e)
  71. {
  72. if(!this->Init)
  73. {
  74. // initialize the table view column size
  75. int colWidth = this->columnWidth(0) + this->columnWidth(1);
  76. this->setColumnWidth(0, colWidth/2);
  77. this->setColumnWidth(1, colWidth/2);
  78. this->Init = true;
  79. }
  80. return QTableView::showEvent(e);
  81. }
  82. QCMakeCacheModel* QCMakeCacheView::cacheModel() const
  83. {
  84. return this->CacheModel;
  85. }
  86. QModelIndex QCMakeCacheView::moveCursor(CursorAction act,
  87. Qt::KeyboardModifiers mod)
  88. {
  89. // tab through values only (not names)
  90. QModelIndex current = this->currentIndex();
  91. if(act == MoveNext)
  92. {
  93. if(!current.isValid())
  94. {
  95. return this->model()->index(0, 1);
  96. }
  97. else if(current.column() == 0)
  98. {
  99. return this->model()->index(current.row(), 1);
  100. }
  101. else
  102. {
  103. return this->model()->index(current.row()+1, 1);
  104. }
  105. }
  106. else if(act == MovePrevious)
  107. {
  108. if(!current.isValid())
  109. {
  110. return this->model()->index(0, 1);
  111. }
  112. else
  113. {
  114. return this->model()->index(current.row()-1, 1);
  115. }
  116. }
  117. return QTableView::moveCursor(act, mod);
  118. }
  119. void QCMakeCacheView::setShowAdvanced(bool s)
  120. {
  121. this->AdvancedFilter->setFilterRegExp(
  122. s ? AdvancedRegExp[1] : AdvancedRegExp[0]);
  123. }
  124. bool QCMakeCacheView::showAdvanced() const
  125. {
  126. return this->AdvancedFilter->filterRegExp() == AdvancedRegExp[1];
  127. }
  128. void QCMakeCacheView::setSearchFilter(const QString& s)
  129. {
  130. this->selectionModel()->clear();
  131. this->SearchFilter->setFilterFixedString(s);
  132. }
  133. QCMakeCacheModel::QCMakeCacheModel(QObject* p)
  134. : QAbstractTableModel(p),
  135. NewCount(0), EditEnabled(true)
  136. {
  137. }
  138. QCMakeCacheModel::~QCMakeCacheModel()
  139. {
  140. }
  141. static uint qHash(const QCMakeCacheProperty& p)
  142. {
  143. return qHash(p.Key);
  144. }
  145. void QCMakeCacheModel::clear()
  146. {
  147. this->setProperties(QCMakeCachePropertyList());
  148. }
  149. void QCMakeCacheModel::setProperties(const QCMakeCachePropertyList& props)
  150. {
  151. QSet<QCMakeCacheProperty> newProps = props.toSet();
  152. QSet<QCMakeCacheProperty> newProps2 = props.toSet();
  153. QSet<QCMakeCacheProperty> oldProps = this->Properties.toSet();
  154. oldProps.intersect(newProps);
  155. newProps.subtract(oldProps);
  156. newProps2.subtract(newProps);
  157. this->NewCount = newProps.count();
  158. this->Properties.clear();
  159. this->Properties = newProps.toList();
  160. qSort(this->Properties);
  161. QCMakeCachePropertyList tmp = newProps2.toList();
  162. qSort(tmp);
  163. this->Properties += tmp;
  164. this->reset();
  165. }
  166. QCMakeCachePropertyList QCMakeCacheModel::properties() const
  167. {
  168. return this->Properties;
  169. }
  170. void QCMakeCacheModel::setEditEnabled(bool e)
  171. {
  172. this->EditEnabled = e;
  173. }
  174. bool QCMakeCacheModel::editEnabled() const
  175. {
  176. return this->EditEnabled;
  177. }
  178. int QCMakeCacheModel::newCount() const
  179. {
  180. return this->NewCount;
  181. }
  182. int QCMakeCacheModel::columnCount (const QModelIndex& /*p*/ ) const
  183. {
  184. return 2;
  185. }
  186. QVariant QCMakeCacheModel::data (const QModelIndex& idx, int role) const
  187. {
  188. if(idx.column() == 0 && (role == Qt::DisplayRole || role == Qt::EditRole))
  189. {
  190. return this->Properties[idx.row()].Key;
  191. }
  192. else if(idx.column() == 0 && role == Qt::ToolTipRole)
  193. {
  194. return this->data(idx, Qt::DisplayRole).toString() + "\n" +
  195. this->data(idx, QCMakeCacheModel::HelpRole).toString();
  196. }
  197. else if(idx.column() == 1 && (role == Qt::DisplayRole || role == Qt::EditRole))
  198. {
  199. if(this->Properties[idx.row()].Type != QCMakeCacheProperty::BOOL)
  200. {
  201. return this->Properties[idx.row()].Value;
  202. }
  203. }
  204. else if(idx.column() == 1 && role == Qt::CheckStateRole)
  205. {
  206. if(this->Properties[idx.row()].Type == QCMakeCacheProperty::BOOL)
  207. {
  208. return this->Properties[idx.row()].Value.toBool() ? Qt::Checked : Qt::Unchecked;
  209. }
  210. }
  211. else if(role == QCMakeCacheModel::HelpRole)
  212. {
  213. return this->Properties[idx.row()].Help;
  214. }
  215. else if(role == QCMakeCacheModel::TypeRole)
  216. {
  217. return this->Properties[idx.row()].Type;
  218. }
  219. else if(role == QCMakeCacheModel::AdvancedRole)
  220. {
  221. return this->Properties[idx.row()].Advanced;
  222. }
  223. else if(role == Qt::BackgroundRole && idx.row()+1 <= this->NewCount)
  224. {
  225. return QBrush(QColor(255,100,100));
  226. }
  227. return QVariant();
  228. }
  229. QModelIndex QCMakeCacheModel::parent (const QModelIndex& /*idx*/) const
  230. {
  231. return QModelIndex();
  232. }
  233. int QCMakeCacheModel::rowCount (const QModelIndex& p) const
  234. {
  235. if(p.isValid())
  236. {
  237. return 0;
  238. }
  239. return this->Properties.count();
  240. }
  241. QVariant QCMakeCacheModel::headerData (int section, Qt::Orientation orient, int role) const
  242. {
  243. // return header labels
  244. if(role == Qt::DisplayRole && orient == Qt::Horizontal)
  245. {
  246. return section == 0 ? "Name" : "Value";
  247. }
  248. return QVariant();
  249. }
  250. Qt::ItemFlags QCMakeCacheModel::flags (const QModelIndex& idx) const
  251. {
  252. Qt::ItemFlags f = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
  253. // all column 1's are editable
  254. if(idx.column() == 1 && this->EditEnabled)
  255. {
  256. f |= Qt::ItemIsEditable;
  257. // booleans are editable in place
  258. if(this->Properties[idx.row()].Type == QCMakeCacheProperty::BOOL)
  259. {
  260. f |= Qt::ItemIsUserCheckable;
  261. }
  262. }
  263. return f;
  264. }
  265. bool QCMakeCacheModel::setData (const QModelIndex& idx, const QVariant& value, int role)
  266. {
  267. if(idx.column() == 0 && (role == Qt::DisplayRole || role == Qt::EditRole))
  268. {
  269. this->Properties[idx.row()].Key = value.toString();
  270. emit this->dataChanged(idx, idx);
  271. }
  272. else if(idx.column() == 1 && (role == Qt::DisplayRole || role == Qt::EditRole))
  273. {
  274. this->Properties[idx.row()].Value = value.toString();
  275. emit this->dataChanged(idx, idx);
  276. }
  277. else if(idx.column() == 1 && (role == Qt::CheckStateRole))
  278. {
  279. this->Properties[idx.row()].Value = value.toInt() == Qt::Checked;
  280. emit this->dataChanged(idx, idx);
  281. }
  282. else if(role == QCMakeCacheModel::HelpRole)
  283. {
  284. this->Properties[idx.row()].Help = value.toString();
  285. emit this->dataChanged(idx, idx);
  286. }
  287. else if(role == QCMakeCacheModel::TypeRole)
  288. {
  289. this->Properties[idx.row()].Type = static_cast<QCMakeCacheProperty::PropertyType>(value.toInt());
  290. }
  291. else if(role == QCMakeCacheModel::AdvancedRole)
  292. {
  293. this->Properties[idx.row()].Advanced = value.toBool();
  294. }
  295. return false;
  296. }
  297. QModelIndex QCMakeCacheModel::buddy(const QModelIndex& idx) const
  298. {
  299. if(idx.column() == 0)
  300. {
  301. if(this->Properties[idx.row()].Type != QCMakeCacheProperty::BOOL)
  302. {
  303. return this->index(idx.row(), 1);
  304. }
  305. }
  306. return idx;
  307. }
  308. bool QCMakeCacheModel::removeRows(int row, int num, const QModelIndex&)
  309. {
  310. if(row < 0 || row+num > this->Properties.count())
  311. {
  312. return false;
  313. }
  314. this->beginRemoveRows(QModelIndex(), row, row+num-1);
  315. for(int i=0; i<num; i++)
  316. {
  317. this->Properties.removeAt(row);
  318. if(this->NewCount >= row+1)
  319. {
  320. this->NewCount--;
  321. }
  322. }
  323. this->endRemoveRows();
  324. return true;
  325. }
  326. bool QCMakeCacheModel::insertRows(int row, int num, const QModelIndex&)
  327. {
  328. if(row < 0)
  329. row = 0;
  330. if(row > this->rowCount())
  331. row = this->rowCount();
  332. this->beginInsertRows(QModelIndex(), row, row+num-1);
  333. for(int i=0; i<num; i++)
  334. {
  335. this->Properties.insert(row+i, QCMakeCacheProperty());
  336. if(this->NewCount >= row)
  337. {
  338. this->NewCount++;
  339. }
  340. }
  341. this->endInsertRows();
  342. return true;
  343. }
  344. QCMakeCacheModelDelegate::QCMakeCacheModelDelegate(QObject* p)
  345. : QItemDelegate(p)
  346. {
  347. }
  348. QWidget* QCMakeCacheModelDelegate::createEditor(QWidget* p,
  349. const QStyleOptionViewItem&, const QModelIndex& idx) const
  350. {
  351. const QAbstractItemModel* model = idx.model();
  352. QModelIndex var = model->index(idx.row(), 0);
  353. QVariant type = idx.data(QCMakeCacheModel::TypeRole);
  354. if(type == QCMakeCacheProperty::BOOL)
  355. {
  356. return NULL;
  357. }
  358. else if(type == QCMakeCacheProperty::PATH)
  359. {
  360. return new QCMakeCachePathEditor(p,
  361. var.data(Qt::DisplayRole).toString());
  362. }
  363. else if(type == QCMakeCacheProperty::FILEPATH)
  364. {
  365. return new QCMakeCacheFilePathEditor(p,
  366. var.data(Qt::DisplayRole).toString());
  367. }
  368. return new QLineEdit(p);
  369. }
  370. bool QCMakeCacheModelDelegate::editorEvent(QEvent* e, QAbstractItemModel* model,
  371. const QStyleOptionViewItem& option, const QModelIndex& index)
  372. {
  373. Qt::ItemFlags flags = model->flags(index);
  374. if (!(flags & Qt::ItemIsUserCheckable) || !(option.state & QStyle::State_Enabled)
  375. || !(flags & Qt::ItemIsEnabled))
  376. {
  377. return false;
  378. }
  379. QVariant value = index.data(Qt::CheckStateRole);
  380. if (!value.isValid())
  381. {
  382. return false;
  383. }
  384. if ((e->type() == QEvent::MouseButtonRelease)
  385. || (e->type() == QEvent::MouseButtonDblClick))
  386. {
  387. // eat the double click events inside the check rect
  388. if (e->type() == QEvent::MouseButtonDblClick)
  389. {
  390. return true;
  391. }
  392. }
  393. else if (e->type() == QEvent::KeyPress)
  394. {
  395. if(static_cast<QKeyEvent*>(e)->key() != Qt::Key_Space &&
  396. static_cast<QKeyEvent*>(e)->key() != Qt::Key_Select)
  397. {
  398. return false;
  399. }
  400. }
  401. else
  402. {
  403. return false;
  404. }
  405. Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked
  406. ? Qt::Unchecked : Qt::Checked);
  407. return model->setData(index, state, Qt::CheckStateRole);
  408. }
  409. QCMakeCacheFileEditor::QCMakeCacheFileEditor(QWidget* p, const QString& var)
  410. : QLineEdit(p), Variable(var)
  411. {
  412. // this *is* instead of has a line edit so QAbstractItemView
  413. // doesn't get confused with what the editor really is
  414. this->setContentsMargins(0, 0, 0, 0);
  415. this->ToolButton = new QToolButton(this);
  416. this->ToolButton->setText("...");
  417. this->ToolButton->setCursor(QCursor(Qt::ArrowCursor));
  418. QObject::connect(this->ToolButton, SIGNAL(clicked(bool)),
  419. this, SLOT(chooseFile()));
  420. }
  421. QCMakeCacheFilePathEditor::QCMakeCacheFilePathEditor(QWidget* p, const QString& var)
  422. : QCMakeCacheFileEditor(p, var)
  423. {
  424. QCompleter* comp = new QCompleter(this);
  425. QDirModel* model = new QDirModel(comp);
  426. comp->setModel(model);
  427. this->setCompleter(comp);
  428. }
  429. QCMakeCachePathEditor::QCMakeCachePathEditor(QWidget* p, const QString& var)
  430. : QCMakeCacheFileEditor(p, var)
  431. {
  432. QCompleter* comp = new QCompleter(this);
  433. QDirModel* model = new QDirModel(comp);
  434. model->setFilter(QDir::AllDirs | QDir::Drives);
  435. comp->setModel(model);
  436. this->setCompleter(comp);
  437. }
  438. void QCMakeCacheFileEditor::resizeEvent(QResizeEvent* e)
  439. {
  440. // make the tool button fit on the right side
  441. int h = e->size().height();
  442. this->ToolButton->resize(h, h);
  443. this->ToolButton->move(this->width() - h, 0);
  444. this->setContentsMargins(0, 0, h, 0);
  445. }
  446. void QCMakeCacheFilePathEditor::chooseFile()
  447. {
  448. // choose a file and set it
  449. QString path;
  450. QFileInfo info(this->text());
  451. QString title;
  452. if(this->Variable.isEmpty())
  453. {
  454. title = tr("Select File");
  455. }
  456. else
  457. {
  458. title = tr("Select File for %1");
  459. title = title.arg(this->Variable);
  460. }
  461. path = QFileDialog::getOpenFileName(this, title, info.absolutePath());
  462. if(!path.isEmpty())
  463. {
  464. this->setText(path);
  465. }
  466. }
  467. void QCMakeCachePathEditor::chooseFile()
  468. {
  469. // choose a file and set it
  470. QString path;
  471. QString title;
  472. if(this->Variable.isEmpty())
  473. {
  474. title = tr("Select Path");
  475. }
  476. else
  477. {
  478. title = tr("Select Path for %1");
  479. title = title.arg(this->Variable);
  480. }
  481. path = QFileDialog::getExistingDirectory(this, title, this->text());
  482. if(!path.isEmpty())
  483. {
  484. this->setText(path);
  485. }
  486. }