QCMakeCacheView.cxx 15 KB

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