QCMakeCacheView.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. QCMakeCacheView::QCMakeCacheView(QWidget* p)
  27. : QTableView(p), Init(false)
  28. {
  29. // hook up our model and search/filter proxies
  30. this->CacheModel = new QCMakeCacheModel(this);
  31. this->AdvancedFilter = new QSortFilterProxyModel(this);
  32. this->AdvancedFilter->setSourceModel(this->CacheModel);
  33. this->AdvancedFilter->setFilterRole(QCMakeCacheModel::AdvancedRole);
  34. this->AdvancedFilter->setFilterRegExp(AdvancedRegExp[0]);
  35. this->AdvancedFilter->setDynamicSortFilter(true);
  36. this->SearchFilter = new QSortFilterProxyModel(this);
  37. this->SearchFilter->setSourceModel(this->AdvancedFilter);
  38. this->SearchFilter->setFilterCaseSensitivity(Qt::CaseInsensitive);
  39. #if QT_VERSION >= 0x040300 // breaks search in Qt 4.2
  40. this->SearchFilter->setFilterKeyColumn(-1); // all columns
  41. #endif
  42. this->SearchFilter->setDynamicSortFilter(true);
  43. this->setModel(this->SearchFilter);
  44. // our delegate for creating our editors
  45. QCMakeCacheModelDelegate* delegate = new QCMakeCacheModelDelegate(this);
  46. this->setItemDelegate(delegate);
  47. // set up headers and sizes
  48. int h = 0;
  49. QFontMetrics met(this->font());
  50. h = qMax(met.height(), this->style()->pixelMetric(QStyle::PM_IndicatorHeight));
  51. this->verticalHeader()->setDefaultSectionSize(h + 4);
  52. this->horizontalHeader()->setStretchLastSection(true);
  53. this->verticalHeader()->hide();
  54. }
  55. void QCMakeCacheView::showEvent(QShowEvent* e)
  56. {
  57. if(!this->Init)
  58. {
  59. // initialize the table view column size
  60. int colWidth = this->columnWidth(0) + this->columnWidth(1);
  61. this->setColumnWidth(0, colWidth/2);
  62. this->setColumnWidth(1, colWidth/2);
  63. this->Init = true;
  64. }
  65. return QTableView::showEvent(e);
  66. }
  67. QCMakeCacheModel* QCMakeCacheView::cacheModel() const
  68. {
  69. return this->CacheModel;
  70. }
  71. QModelIndex QCMakeCacheView::moveCursor(CursorAction act,
  72. Qt::KeyboardModifiers mod)
  73. {
  74. // tab through values only (not names)
  75. QModelIndex current = this->currentIndex();
  76. if(act == MoveNext)
  77. {
  78. if(!current.isValid())
  79. {
  80. return this->model()->index(0, 1);
  81. }
  82. else if(current.column() == 0)
  83. {
  84. return this->model()->index(current.row(), 1);
  85. }
  86. else
  87. {
  88. return this->model()->index(current.row()+1, 1);
  89. }
  90. }
  91. else if(act == MovePrevious)
  92. {
  93. if(!current.isValid())
  94. {
  95. return this->model()->index(0, 1);
  96. }
  97. else
  98. {
  99. return this->model()->index(current.row()-1, 1);
  100. }
  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)
  330. {
  331. }
  332. QWidget* QCMakeCacheModelDelegate::createEditor(QWidget* p,
  333. const QStyleOptionViewItem&, const QModelIndex& idx) const
  334. {
  335. QVariant type = idx.data(QCMakeCacheModel::TypeRole);
  336. if(type == QCMakeCacheProperty::BOOL)
  337. {
  338. return NULL;
  339. }
  340. else if(type == QCMakeCacheProperty::PATH)
  341. {
  342. return new QCMakeCachePathEditor(p);
  343. }
  344. else if(type == QCMakeCacheProperty::FILEPATH)
  345. {
  346. return new QCMakeCacheFilePathEditor(p);
  347. }
  348. return new QLineEdit(p);
  349. }
  350. bool QCMakeCacheModelDelegate::editorEvent(QEvent* event, QAbstractItemModel* model,
  351. const QStyleOptionViewItem& option, const QModelIndex& index)
  352. {
  353. Qt::ItemFlags flags = model->flags(index);
  354. if (!(flags & Qt::ItemIsUserCheckable) || !(option.state & QStyle::State_Enabled)
  355. || !(flags & Qt::ItemIsEnabled))
  356. {
  357. return false;
  358. }
  359. QVariant value = index.data(Qt::CheckStateRole);
  360. if (!value.isValid())
  361. {
  362. return false;
  363. }
  364. if ((event->type() == QEvent::MouseButtonRelease)
  365. || (event->type() == QEvent::MouseButtonDblClick))
  366. {
  367. // eat the double click events inside the check rect
  368. if (event->type() == QEvent::MouseButtonDblClick)
  369. {
  370. return true;
  371. }
  372. }
  373. else if (event->type() == QEvent::KeyPress)
  374. {
  375. if(static_cast<QKeyEvent*>(event)->key() != Qt::Key_Space &&
  376. static_cast<QKeyEvent*>(event)->key() != Qt::Key_Select)
  377. {
  378. return false;
  379. }
  380. }
  381. else
  382. {
  383. return false;
  384. }
  385. Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked
  386. ? Qt::Unchecked : Qt::Checked);
  387. return model->setData(index, state, Qt::CheckStateRole);
  388. }
  389. QCMakeCacheFileEditor::QCMakeCacheFileEditor(QWidget* p)
  390. : QLineEdit(p)
  391. {
  392. // this *is* instead of has a line edit so QAbstractItemView
  393. // doesn't get confused with what the editor really is
  394. this->setContentsMargins(0, 0, 0, 0);
  395. this->ToolButton = new QToolButton(this);
  396. this->ToolButton->setText("...");
  397. this->ToolButton->setCursor(QCursor(Qt::ArrowCursor));
  398. QObject::connect(this->ToolButton, SIGNAL(clicked(bool)),
  399. this, SLOT(chooseFile()));
  400. }
  401. QCMakeCacheFilePathEditor::QCMakeCacheFilePathEditor(QWidget* p)
  402. : QCMakeCacheFileEditor(p)
  403. {
  404. QCompleter* comp = new QCompleter(this);
  405. QDirModel* model = new QDirModel(comp);
  406. comp->setModel(model);
  407. this->setCompleter(comp);
  408. }
  409. QCMakeCachePathEditor::QCMakeCachePathEditor(QWidget* p)
  410. : QCMakeCacheFileEditor(p)
  411. {
  412. QCompleter* comp = new QCompleter(this);
  413. QDirModel* model = new QDirModel(comp);
  414. model->setFilter(QDir::AllDirs | QDir::Drives);
  415. comp->setModel(model);
  416. this->setCompleter(comp);
  417. }
  418. void QCMakeCacheFileEditor::resizeEvent(QResizeEvent* e)
  419. {
  420. // make the tool button fit on the right side
  421. int h = e->size().height();
  422. this->ToolButton->resize(h, h);
  423. this->ToolButton->move(this->width() - h, 0);
  424. this->setContentsMargins(0, 0, h, 0);
  425. }
  426. void QCMakeCacheFilePathEditor::chooseFile()
  427. {
  428. // choose a file and set it
  429. QString path;
  430. QFileInfo info(this->text());
  431. path = QFileDialog::getOpenFileName(this, tr("Select File"),
  432. info.absolutePath());
  433. if(!path.isEmpty())
  434. {
  435. this->setText(path);
  436. }
  437. }
  438. void QCMakeCachePathEditor::chooseFile()
  439. {
  440. // choose a file and set it
  441. QString path;
  442. path = QFileDialog::getExistingDirectory(this, tr("Select Path"),
  443. this->text());
  444. if(!path.isEmpty())
  445. {
  446. this->setText(path);
  447. }
  448. }