QCMakeCacheView.cxx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. QCMakeCacheView::QCMakeCacheView(QWidget* p)
  20. : QTableView(p), Init(false)
  21. {
  22. QCMakeCacheModel* m = new QCMakeCacheModel(this);
  23. this->setModel(m);
  24. this->horizontalHeader()->setStretchLastSection(true);
  25. this->verticalHeader()->hide();
  26. QCMakeCacheModelDelegate* delegate = new QCMakeCacheModelDelegate(this);
  27. this->setItemDelegate(delegate);
  28. }
  29. void QCMakeCacheView::showEvent(QShowEvent* e)
  30. {
  31. if(!this->Init)
  32. {
  33. // initialize the table view column size
  34. int colWidth = this->columnWidth(0) + this->columnWidth(1);
  35. this->setColumnWidth(0, colWidth/2);
  36. this->setColumnWidth(1, colWidth/2);
  37. this->Init = true;
  38. }
  39. return QTableView::showEvent(e);
  40. }
  41. QCMakeCacheModel* QCMakeCacheView::cacheModel() const
  42. {
  43. return qobject_cast<QCMakeCacheModel*>(this->model());
  44. }
  45. QModelIndex QCMakeCacheView::moveCursor(CursorAction act,
  46. Qt::KeyboardModifiers mod)
  47. {
  48. // tab through values only (not names)
  49. QModelIndex current = this->currentIndex();
  50. if(act == MoveNext)
  51. {
  52. if(!current.isValid())
  53. {
  54. return this->model()->index(0, 1);
  55. }
  56. else if(current.column() == 0)
  57. {
  58. return this->model()->index(current.row(), 1);
  59. }
  60. else
  61. {
  62. return this->model()->index(current.row()+1, 1);
  63. }
  64. }
  65. else if(act == MovePrevious)
  66. {
  67. if(!current.isValid())
  68. {
  69. return this->model()->index(0, 1);
  70. }
  71. else
  72. {
  73. return this->model()->index(current.row()-1, 1);
  74. }
  75. }
  76. return QTableView::moveCursor(act, mod);
  77. }
  78. QCMakeCacheModel::QCMakeCacheModel(QObject* p)
  79. : QAbstractTableModel(p), IsDirty(false)
  80. {
  81. }
  82. QCMakeCacheModel::~QCMakeCacheModel()
  83. {
  84. }
  85. bool QCMakeCacheModel::isDirty() const
  86. {
  87. return this->IsDirty;
  88. }
  89. void QCMakeCacheModel::setProperties(const QCMakeCachePropertyList& props)
  90. {
  91. this->Properties = props;
  92. this->reset();
  93. this->IsDirty = false;
  94. }
  95. QCMakeCachePropertyList QCMakeCacheModel::properties() const
  96. {
  97. return this->Properties;
  98. }
  99. int QCMakeCacheModel::columnCount ( const QModelIndex & parent ) const
  100. {
  101. return 2;
  102. }
  103. QVariant QCMakeCacheModel::data ( const QModelIndex & index, int role ) const
  104. {
  105. if(index.column() == 0 && (role == Qt::DisplayRole || role == Qt::EditRole))
  106. {
  107. return this->Properties[index.row()].Key;
  108. }
  109. else if(index.column() == 0 && role == Qt::ToolTipRole)
  110. {
  111. return this->data(index, Qt::DisplayRole).toString() + "\n" +
  112. this->data(index, QCMakeCacheModel::HelpRole).toString();
  113. }
  114. else if(index.column() == 1 && (role == Qt::DisplayRole || role == Qt::EditRole))
  115. {
  116. if(this->Properties[index.row()].Type != QCMakeCacheProperty::BOOL)
  117. {
  118. return this->Properties[index.row()].Value;
  119. }
  120. }
  121. else if(index.column() == 1 && role == Qt::CheckStateRole)
  122. {
  123. if(this->Properties[index.row()].Type == QCMakeCacheProperty::BOOL)
  124. {
  125. return this->Properties[index.row()].Value.toBool() ? Qt::Checked : Qt::Unchecked;
  126. }
  127. }
  128. else if(role == QCMakeCacheModel::HelpRole)
  129. {
  130. return this->Properties[index.row()].Help;
  131. }
  132. else if(role == QCMakeCacheModel::TypeRole)
  133. {
  134. return this->Properties[index.row()].Type;
  135. }
  136. else if(role == QCMakeCacheModel::AdvancedRole)
  137. {
  138. return this->Properties[index.row()].Advanced;
  139. }
  140. return QVariant();
  141. }
  142. QModelIndex QCMakeCacheModel::parent ( const QModelIndex & index ) const
  143. {
  144. return QModelIndex();
  145. }
  146. int QCMakeCacheModel::rowCount ( const QModelIndex & parent ) const
  147. {
  148. if(parent.isValid())
  149. {
  150. return 0;
  151. }
  152. return this->Properties.count();
  153. }
  154. QVariant QCMakeCacheModel::headerData ( int section, Qt::Orientation orient, int role ) const
  155. {
  156. // return header labels
  157. if(role == Qt::DisplayRole && orient == Qt::Horizontal)
  158. {
  159. return section == 0 ? "Name" : "Value";
  160. }
  161. return QVariant();
  162. }
  163. Qt::ItemFlags QCMakeCacheModel::flags ( const QModelIndex& index ) const
  164. {
  165. Qt::ItemFlags f = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
  166. // all column 1's are editable
  167. if(index.column() == 1)
  168. {
  169. f |= Qt::ItemIsEditable;
  170. // booleans are editable in place
  171. if(this->Properties[index.row()].Type == QCMakeCacheProperty::BOOL)
  172. {
  173. f |= Qt::ItemIsUserCheckable;
  174. }
  175. }
  176. return f;
  177. }
  178. bool QCMakeCacheModel::setData ( const QModelIndex & index, const QVariant& value, int role )
  179. {
  180. if(index.column() == 0 && (role == Qt::DisplayRole || role == Qt::EditRole))
  181. {
  182. this->Properties[index.row()].Key = value.toString();
  183. this->IsDirty = true;
  184. emit this->dataChanged(index, index);
  185. }
  186. else if(index.column() == 1 && (role == Qt::DisplayRole || role == Qt::EditRole))
  187. {
  188. this->Properties[index.row()].Value = value.toString();
  189. this->IsDirty = true;
  190. emit this->dataChanged(index, index);
  191. }
  192. else if(index.column() == 1 && (role == Qt::CheckStateRole))
  193. {
  194. this->Properties[index.row()].Value = value.toInt() == Qt::Checked;
  195. this->IsDirty = true;
  196. emit this->dataChanged(index, index);
  197. }
  198. return false;
  199. }
  200. QCMakeCacheModelDelegate::QCMakeCacheModelDelegate(QObject* p)
  201. : QItemDelegate(p)
  202. {
  203. }
  204. QWidget* QCMakeCacheModelDelegate::createEditor(QWidget* parent,
  205. const QStyleOptionViewItem&, const QModelIndex& index) const
  206. {
  207. QVariant type = index.data(QCMakeCacheModel::TypeRole);
  208. if(type == QCMakeCacheProperty::BOOL)
  209. {
  210. return NULL;
  211. }
  212. else if(type == QCMakeCacheProperty::PATH)
  213. {
  214. return new QCMakeCachePathEditor(index.data().toString(), false, parent);
  215. }
  216. else if(type == QCMakeCacheProperty::FILEPATH)
  217. {
  218. return new QCMakeCachePathEditor(index.data().toString(), true, parent);
  219. }
  220. return new QLineEdit(parent);
  221. }
  222. QCMakeCachePathEditor::QCMakeCachePathEditor(const QString& file, bool fp, QWidget* p)
  223. : QWidget(p), LineEdit(this), IsFilePath(fp)
  224. {
  225. QHBoxLayout* l = new QHBoxLayout(this);
  226. l->setMargin(0);
  227. l->setSpacing(0);
  228. l->addWidget(&this->LineEdit);
  229. QToolButton* tb = new QToolButton(this);
  230. tb->setText("...");
  231. l->addWidget(tb);
  232. QObject::connect(tb, SIGNAL(clicked(bool)),
  233. this, SLOT(chooseFile()));
  234. this->LineEdit.setText(file);
  235. tb->setFocusProxy(&this->LineEdit);
  236. this->setFocusProxy(&this->LineEdit);
  237. }
  238. void QCMakeCachePathEditor::chooseFile()
  239. {
  240. QString path;
  241. if(this->IsFilePath)
  242. {
  243. path = QFileDialog::getOpenFileName(this, "TODO");
  244. }
  245. else
  246. {
  247. path = QFileDialog::getExistingDirectory(this, "TODO", this->value());
  248. }
  249. if(!path.isEmpty())
  250. {
  251. this->LineEdit.setText(path);
  252. }
  253. }