QCMakeCacheView.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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 <QDialog>
  24. #include <QLabel>
  25. #include <QDialogButtonBox>
  26. static QRegExp AdvancedRegExp[2] = { QRegExp("(false)"), QRegExp("(true|false)") };
  27. QCMakeCacheView::QCMakeCacheView(QWidget* p)
  28. : QTableView(p), Init(false)
  29. {
  30. // hook up our model and search/filter proxies
  31. this->CacheModel = new QCMakeCacheModel(this);
  32. this->AdvancedFilter = new QSortFilterProxyModel(this);
  33. this->AdvancedFilter->setSourceModel(this->CacheModel);
  34. this->AdvancedFilter->setFilterRole(QCMakeCacheModel::AdvancedRole);
  35. this->AdvancedFilter->setFilterRegExp(AdvancedRegExp[0]);
  36. this->SearchFilter = new QSortFilterProxyModel(this);
  37. this->SearchFilter->setSourceModel(this->AdvancedFilter);
  38. this->SearchFilter->setFilterCaseSensitivity(Qt::CaseInsensitive);
  39. this->SearchFilter->setFilterKeyColumn(-1); // all columns
  40. this->setModel(this->SearchFilter);
  41. // our delegate for creating our editors
  42. QCMakeCacheModelDelegate* delegate = new QCMakeCacheModelDelegate(this);
  43. this->setItemDelegate(delegate);
  44. // set up headers and sizes
  45. int h = 0;
  46. QFontMetrics met(this->font());
  47. h = qMax(met.height(), this->style()->pixelMetric(QStyle::PM_IndicatorHeight));
  48. this->verticalHeader()->setDefaultSectionSize(h + 4);
  49. this->horizontalHeader()->setStretchLastSection(true);
  50. this->verticalHeader()->hide();
  51. }
  52. void QCMakeCacheView::showEvent(QShowEvent* e)
  53. {
  54. if(!this->Init)
  55. {
  56. // initialize the table view column size
  57. int colWidth = this->columnWidth(0) + this->columnWidth(1);
  58. this->setColumnWidth(0, colWidth/2);
  59. this->setColumnWidth(1, colWidth/2);
  60. this->Init = true;
  61. }
  62. return QTableView::showEvent(e);
  63. }
  64. QCMakeCacheModel* QCMakeCacheView::cacheModel() const
  65. {
  66. return this->CacheModel;
  67. }
  68. void QCMakeCacheView::contextMenuEvent(QContextMenuEvent* /*e*/)
  69. {
  70. QList<QModelIndex> idxs = this->selectionModel()->selectedRows();
  71. if(idxs.count())
  72. {
  73. QMenu* menu = new QMenu(this);
  74. QAction* deleteAction = NULL;
  75. QAction* ignoreAction = NULL;
  76. if(this->cacheModel()->editEnabled())
  77. {
  78. QString t = idxs.count() > 1 ? tr("Delete Cache Entries") :
  79. tr("Delete Cache Entry");
  80. deleteAction = menu->addAction(t);
  81. t = idxs.count() > 1 ? tr("Ignore Cache Entries") :
  82. tr("Ignore Cache Entry");
  83. ignoreAction = menu->addAction(t);
  84. }
  85. QAction* helpAction = menu->addAction(tr("Help For Cache Entry"));
  86. QAction* which = menu->exec(QCursor::pos());
  87. if(!which)
  88. {
  89. return;
  90. }
  91. if(which == helpAction)
  92. {
  93. QModelIndex idx = this->selectionModel()->currentIndex();
  94. idx = this->SearchFilter->mapToSource(idx);
  95. idx = this->AdvancedFilter->mapToSource(idx);
  96. idx = this->cacheModel()->index(idx.row(), 0);
  97. QString msg = this->cacheModel()->data(idx, Qt::DisplayRole).toString() +
  98. "\n\n" +
  99. this->cacheModel()->data(idx, QCMakeCacheModel::HelpRole).toString();
  100. QDialog dialog;
  101. dialog.setWindowTitle(tr("CMakeSetup Help"));
  102. QVBoxLayout* l = new QVBoxLayout(&dialog);
  103. QLabel* lab = new QLabel(&dialog);
  104. l->addWidget(lab);
  105. lab->setText(msg);
  106. lab->setWordWrap(true);
  107. QDialogButtonBox* btns = new QDialogButtonBox(QDialogButtonBox::Ok,
  108. Qt::Horizontal, &dialog);
  109. QObject::connect(btns, SIGNAL(accepted()), &dialog, SLOT(accept()));
  110. l->addWidget(btns);
  111. dialog.exec();
  112. }
  113. else
  114. {
  115. QList<QPersistentModelIndex> pidxs;
  116. foreach(QModelIndex i, idxs)
  117. {
  118. i = this->SearchFilter->mapToSource(i);
  119. i = this->AdvancedFilter->mapToSource(i);
  120. pidxs.append(i);
  121. }
  122. if(which == deleteAction)
  123. {
  124. foreach(QPersistentModelIndex j, pidxs)
  125. {
  126. this->cacheModel()->removeRows(j.row(), 1);
  127. }
  128. }
  129. else if(which == ignoreAction)
  130. {
  131. foreach(QPersistentModelIndex j, pidxs)
  132. {
  133. j = this->cacheModel()->index(j.row(), 1);
  134. this->cacheModel()->setData(j, "IGNORE", Qt::DisplayRole);
  135. }
  136. }
  137. }
  138. }
  139. }
  140. QModelIndex QCMakeCacheView::moveCursor(CursorAction act,
  141. Qt::KeyboardModifiers mod)
  142. {
  143. // tab through values only (not names)
  144. QModelIndex current = this->currentIndex();
  145. if(act == MoveNext)
  146. {
  147. if(!current.isValid())
  148. {
  149. return this->model()->index(0, 1);
  150. }
  151. else if(current.column() == 0)
  152. {
  153. return this->model()->index(current.row(), 1);
  154. }
  155. else
  156. {
  157. return this->model()->index(current.row()+1, 1);
  158. }
  159. }
  160. else if(act == MovePrevious)
  161. {
  162. if(!current.isValid())
  163. {
  164. return this->model()->index(0, 1);
  165. }
  166. else
  167. {
  168. return this->model()->index(current.row()-1, 1);
  169. }
  170. }
  171. return QTableView::moveCursor(act, mod);
  172. }
  173. void QCMakeCacheView::setShowAdvanced(bool s)
  174. {
  175. this->AdvancedFilter->setFilterRegExp(
  176. s ? AdvancedRegExp[1] : AdvancedRegExp[0]);
  177. }
  178. bool QCMakeCacheView::showAdvanced() const
  179. {
  180. return this->AdvancedFilter->filterRegExp() == AdvancedRegExp[1];
  181. }
  182. void QCMakeCacheView::setSearchFilter(const QString& s)
  183. {
  184. this->SearchFilter->setFilterFixedString(s);
  185. }
  186. QCMakeCacheModel::QCMakeCacheModel(QObject* p)
  187. : QAbstractTableModel(p),
  188. NewCount(0), ModifiedValues(false), EditEnabled(true)
  189. {
  190. }
  191. QCMakeCacheModel::~QCMakeCacheModel()
  192. {
  193. }
  194. bool QCMakeCacheModel::modifiedValues() const
  195. {
  196. return this->ModifiedValues;
  197. }
  198. static uint qHash(const QCMakeCacheProperty& p)
  199. {
  200. return qHash(p.Key);
  201. }
  202. void QCMakeCacheModel::clear()
  203. {
  204. this->setProperties(QCMakeCachePropertyList());
  205. }
  206. void QCMakeCacheModel::setProperties(const QCMakeCachePropertyList& props)
  207. {
  208. QSet<QCMakeCacheProperty> newProps = props.toSet();
  209. QSet<QCMakeCacheProperty> oldProps = this->Properties.toSet();
  210. oldProps.intersect(newProps);
  211. newProps.subtract(oldProps);
  212. this->NewCount = newProps.count();
  213. this->Properties.clear();
  214. this->Properties = newProps.toList();
  215. qSort(this->Properties);
  216. QCMakeCachePropertyList tmp = oldProps.toList();
  217. qSort(tmp);
  218. this->Properties += tmp;
  219. this->ModifiedValues = NewCount != 0;
  220. this->reset();
  221. }
  222. QCMakeCachePropertyList QCMakeCacheModel::properties() const
  223. {
  224. return this->Properties;
  225. }
  226. void QCMakeCacheModel::setEditEnabled(bool e)
  227. {
  228. this->EditEnabled = e;
  229. }
  230. bool QCMakeCacheModel::editEnabled() const
  231. {
  232. return this->EditEnabled;
  233. }
  234. int QCMakeCacheModel::columnCount (const QModelIndex& /*p*/ ) const
  235. {
  236. return 2;
  237. }
  238. QVariant QCMakeCacheModel::data (const QModelIndex& idx, int role) const
  239. {
  240. if(idx.column() == 0 && (role == Qt::DisplayRole || role == Qt::EditRole))
  241. {
  242. return this->Properties[idx.row()].Key;
  243. }
  244. else if(idx.column() == 0 && role == Qt::ToolTipRole)
  245. {
  246. return this->data(idx, Qt::DisplayRole).toString() + "\n" +
  247. this->data(idx, QCMakeCacheModel::HelpRole).toString();
  248. }
  249. else if(idx.column() == 1 && (role == Qt::DisplayRole || role == Qt::EditRole))
  250. {
  251. if(this->Properties[idx.row()].Type != QCMakeCacheProperty::BOOL)
  252. {
  253. return this->Properties[idx.row()].Value;
  254. }
  255. }
  256. else if(idx.column() == 1 && role == Qt::CheckStateRole)
  257. {
  258. if(this->Properties[idx.row()].Type == QCMakeCacheProperty::BOOL)
  259. {
  260. return this->Properties[idx.row()].Value.toBool() ? Qt::Checked : Qt::Unchecked;
  261. }
  262. }
  263. else if(role == QCMakeCacheModel::HelpRole)
  264. {
  265. return this->Properties[idx.row()].Help;
  266. }
  267. else if(role == QCMakeCacheModel::TypeRole)
  268. {
  269. return this->Properties[idx.row()].Type;
  270. }
  271. else if(role == QCMakeCacheModel::AdvancedRole)
  272. {
  273. return this->Properties[idx.row()].Advanced;
  274. }
  275. else if(role == Qt::BackgroundRole && idx.row()+1 <= this->NewCount)
  276. {
  277. return QBrush(QColor(255,100,100));
  278. }
  279. return QVariant();
  280. }
  281. QModelIndex QCMakeCacheModel::parent (const QModelIndex& /*idx*/) const
  282. {
  283. return QModelIndex();
  284. }
  285. int QCMakeCacheModel::rowCount (const QModelIndex& p) const
  286. {
  287. if(p.isValid())
  288. {
  289. return 0;
  290. }
  291. return this->Properties.count();
  292. }
  293. QVariant QCMakeCacheModel::headerData (int section, Qt::Orientation orient, int role) const
  294. {
  295. // return header labels
  296. if(role == Qt::DisplayRole && orient == Qt::Horizontal)
  297. {
  298. return section == 0 ? "Name" : "Value";
  299. }
  300. return QVariant();
  301. }
  302. Qt::ItemFlags QCMakeCacheModel::flags (const QModelIndex& idx) const
  303. {
  304. Qt::ItemFlags f = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
  305. // all column 1's are editable
  306. if(idx.column() == 1 && this->EditEnabled)
  307. {
  308. f |= Qt::ItemIsEditable;
  309. // booleans are editable in place
  310. if(this->Properties[idx.row()].Type == QCMakeCacheProperty::BOOL)
  311. {
  312. f |= Qt::ItemIsUserCheckable;
  313. }
  314. }
  315. return f;
  316. }
  317. bool QCMakeCacheModel::setData (const QModelIndex& idx, const QVariant& value, int role)
  318. {
  319. if(idx.column() == 0 && (role == Qt::DisplayRole || role == Qt::EditRole))
  320. {
  321. this->Properties[idx.row()].Key = value.toString();
  322. this->ModifiedValues = true;
  323. emit this->dataChanged(idx, idx);
  324. }
  325. else if(idx.column() == 1 && (role == Qt::DisplayRole || role == Qt::EditRole))
  326. {
  327. this->Properties[idx.row()].Value = value.toString();
  328. this->ModifiedValues = true;
  329. emit this->dataChanged(idx, idx);
  330. }
  331. else if(idx.column() == 1 && (role == Qt::CheckStateRole))
  332. {
  333. this->Properties[idx.row()].Value = value.toInt() == Qt::Checked;
  334. this->ModifiedValues = true;
  335. emit this->dataChanged(idx, idx);
  336. }
  337. return false;
  338. }
  339. QModelIndex QCMakeCacheModel::buddy ( const QModelIndex& idx ) const
  340. {
  341. if(idx.column() == 0)
  342. {
  343. return this->index(idx.row(), 1);
  344. }
  345. return idx;
  346. }
  347. bool QCMakeCacheModel::removeRows(int row, int, const QModelIndex&)
  348. {
  349. if(row < 0 || row >= this->Properties.count())
  350. {
  351. return false;
  352. }
  353. this->beginRemoveRows(QModelIndex(), row, row);
  354. this->Properties.removeAt(row);
  355. if(this->NewCount >= row+1)
  356. {
  357. this->NewCount--;
  358. }
  359. this->endRemoveRows();
  360. return true;
  361. }
  362. QCMakeCacheModelDelegate::QCMakeCacheModelDelegate(QObject* p)
  363. : QItemDelegate(p)
  364. {
  365. }
  366. QWidget* QCMakeCacheModelDelegate::createEditor(QWidget* p,
  367. const QStyleOptionViewItem&, const QModelIndex& idx) const
  368. {
  369. QVariant type = idx.data(QCMakeCacheModel::TypeRole);
  370. if(type == QCMakeCacheProperty::BOOL)
  371. {
  372. return NULL;
  373. }
  374. else if(type == QCMakeCacheProperty::PATH)
  375. {
  376. return new QCMakeCachePathEditor(false, p);
  377. }
  378. else if(type == QCMakeCacheProperty::FILEPATH)
  379. {
  380. return new QCMakeCachePathEditor(true, p);
  381. }
  382. return new QLineEdit(p);
  383. }
  384. QCMakeCachePathEditor::QCMakeCachePathEditor(bool fp, QWidget* p)
  385. : QLineEdit(p), IsFilePath(fp)
  386. {
  387. // this *is* instead of has a line edit so QAbstractItemView
  388. // doesn't get confused with what the editor really is
  389. this->setContentsMargins(0, 0, 0, 0);
  390. this->ToolButton = new QToolButton(this);
  391. this->ToolButton->setText("...");
  392. this->ToolButton->setCursor(QCursor(Qt::ArrowCursor));
  393. QObject::connect(this->ToolButton, SIGNAL(clicked(bool)),
  394. this, SLOT(chooseFile()));
  395. }
  396. void QCMakeCachePathEditor::resizeEvent(QResizeEvent* e)
  397. {
  398. // make the tool button fit on the right side
  399. int h = e->size().height();
  400. this->ToolButton->resize(h, h);
  401. this->ToolButton->move(this->width() - h, 0);
  402. this->setContentsMargins(0, 0, h, 0);
  403. }
  404. void QCMakeCachePathEditor::chooseFile()
  405. {
  406. // choose a file and set it
  407. QString path;
  408. if(this->IsFilePath)
  409. {
  410. QFileInfo info(this->text());
  411. path = QFileDialog::getOpenFileName(this, tr("Select File"),
  412. info.absolutePath());
  413. }
  414. else
  415. {
  416. path = QFileDialog::getExistingDirectory(this, tr("Select Path"),
  417. this->text());
  418. }
  419. if(!path.isEmpty())
  420. {
  421. this->setText(path);
  422. }
  423. }