PageRenderTime 59ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 1ms

/GUISupport/Qt/vtkQtDebugLeaksView.cxx

https://github.com/dgobbi/VTK
C++ | 242 lines | 176 code | 36 blank | 30 comment | 8 complexity | 09f288d7d79f08fd9a6247a7236d4af5 MD5 | raw file
  1. /*=========================================================================
  2. Program: Visualization Toolkit
  3. Module: vtkQtDebugLeaksView.cxx
  4. Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  5. All rights reserved.
  6. See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
  7. This software is distributed WITHOUT ANY WARRANTY; without even
  8. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  9. PURPOSE. See the above copyright notice for more information.
  10. =========================================================================*/
  11. #include "vtkQtDebugLeaksView.h"
  12. #include "vtkQtDebugLeaksModel.h"
  13. #include <QCheckBox>
  14. #include <QDesktopServices>
  15. #include <QHBoxLayout>
  16. #include <QHeaderView>
  17. #include <QItemSelectionModel>
  18. #include <QLineEdit>
  19. #include <QPushButton>
  20. #include <QSortFilterProxyModel>
  21. #include <QSplitter>
  22. #include <QTableView>
  23. #include <QVBoxLayout>
  24. #include <QUrl>
  25. //-----------------------------------------------------------------------------
  26. class vtkQtDebugLeaksView::qInternal
  27. {
  28. public:
  29. vtkQtDebugLeaksModel* Model;
  30. QSortFilterProxyModel* ProxyModel;
  31. QTableView* TableView;
  32. QTableView* ReferenceTableView;
  33. QCheckBox* FilterCheckBox;
  34. QLineEdit* FilterLineEdit;
  35. };
  36. //----------------------------------------------------------------------------
  37. vtkQtDebugLeaksView::vtkQtDebugLeaksView(QWidget *p) : QWidget(p)
  38. {
  39. this->Internal = new qInternal;
  40. this->Internal->Model = new vtkQtDebugLeaksModel(this);
  41. this->Internal->ProxyModel = new QSortFilterProxyModel(this->Internal->Model);
  42. this->Internal->ProxyModel->setSourceModel(this->Internal->Model);
  43. this->Internal->ProxyModel->setDynamicSortFilter(true);
  44. this->Internal->ProxyModel->setFilterKeyColumn(0);
  45. this->Internal->TableView = new QTableView;
  46. this->Internal->TableView->setSortingEnabled(true);
  47. this->Internal->TableView->setModel(this->Internal->ProxyModel);
  48. this->Internal->TableView->setObjectName("ClassTable");
  49. this->Internal->ReferenceTableView = new QTableView;
  50. this->Internal->ReferenceTableView->setObjectName("ReferenceTable");
  51. this->Internal->FilterCheckBox = new QCheckBox("Filter RegExp");
  52. this->Internal->FilterCheckBox->setChecked(true);
  53. this->Internal->FilterLineEdit = new QLineEdit();
  54. QVBoxLayout* mainLayout = new QVBoxLayout(this);
  55. QSplitter* splitter = new QSplitter();
  56. QPushButton* filterHelpButton = new QPushButton("RegExp Help");
  57. QHBoxLayout* filterLayout = new QHBoxLayout();
  58. filterLayout->addWidget(this->Internal->FilterCheckBox);
  59. filterLayout->addWidget(this->Internal->FilterLineEdit);
  60. filterLayout->addWidget(filterHelpButton);
  61. mainLayout->addLayout(filterLayout);
  62. mainLayout->addWidget(splitter);
  63. splitter->setOrientation(Qt::Vertical);
  64. splitter->addWidget(this->Internal->TableView);
  65. splitter->addWidget(this->Internal->ReferenceTableView);
  66. QList<int> sizes;
  67. sizes << 1 << 0;
  68. splitter->setSizes(sizes);
  69. this->connect(this->Internal->FilterLineEdit, SIGNAL(textChanged(const QString&)),
  70. SLOT(onFilterTextChanged(const QString&)));
  71. this->connect(this->Internal->FilterCheckBox, SIGNAL(stateChanged(int)),
  72. SLOT(onFilterToggled()));
  73. this->connect(filterHelpButton, SIGNAL(clicked()), SLOT(onFilterHelp()));
  74. this->connect(this->Internal->TableView->selectionModel(),
  75. SIGNAL(currentRowChanged(const QModelIndex&, const QModelIndex&)),
  76. this, SLOT(onCurrentRowChanged(const QModelIndex&)));
  77. this->connect(this->Internal->TableView,
  78. SIGNAL(doubleClicked(const QModelIndex&)),
  79. this, SLOT(onRowDoubleClicked(const QModelIndex&)));
  80. this->connect(this->Internal->ReferenceTableView,
  81. SIGNAL(doubleClicked(const QModelIndex&)),
  82. this, SLOT(onRowDoubleClicked(const QModelIndex&)));
  83. this->resize(400, 600);
  84. this->setWindowTitle("VTK Debug Leaks View");
  85. this->Internal->TableView->setColumnWidth(0, 200);
  86. this->Internal->TableView->horizontalHeader()->setStretchLastSection(true);
  87. this->Internal->TableView->verticalHeader()->setVisible(false);
  88. this->Internal->TableView->setSelectionMode(QAbstractItemView::SingleSelection);
  89. this->Internal->TableView->setSelectionBehavior(QAbstractItemView::SelectRows);
  90. this->Internal->ReferenceTableView->setSelectionMode(QAbstractItemView::SingleSelection);
  91. this->Internal->ReferenceTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
  92. this->setAttribute(Qt::WA_QuitOnClose, false);
  93. }
  94. //-----------------------------------------------------------------------------
  95. vtkQtDebugLeaksView::~vtkQtDebugLeaksView()
  96. {
  97. this->Internal->ReferenceTableView->setModel(0);
  98. this->Internal->TableView->setModel(0);
  99. delete this->Internal->Model;
  100. delete this->Internal;
  101. }
  102. //-----------------------------------------------------------------------------
  103. vtkQtDebugLeaksModel* vtkQtDebugLeaksView::model()
  104. {
  105. return this->Internal->Model;
  106. }
  107. //-----------------------------------------------------------------------------
  108. void vtkQtDebugLeaksView::onFilterHelp()
  109. {
  110. QDesktopServices::openUrl(QUrl("http://doc.trolltech.com/4.6/qregexp.html#introduction"));
  111. }
  112. //-----------------------------------------------------------------------------
  113. void vtkQtDebugLeaksView::onCurrentRowChanged(const QModelIndex& current)
  114. {
  115. QStandardItemModel* newModel = 0;
  116. QAbstractItemModel* previousModel = this->Internal->ReferenceTableView->model();
  117. QModelIndex index = this->Internal->ProxyModel->mapToSource(current);
  118. if (index.isValid())
  119. {
  120. QModelIndex classNameIndex = this->Internal->Model->index(index.row(), 0);
  121. QString className = this->Internal->Model->data(classNameIndex).toString();
  122. newModel = this->Internal->Model->referenceCountModel(className);
  123. }
  124. if (newModel != previousModel)
  125. {
  126. this->Internal->ReferenceTableView->setModel(newModel);
  127. this->Internal->ReferenceTableView->resizeColumnsToContents();
  128. this->Internal->ReferenceTableView->horizontalHeader()->setStretchLastSection(true);
  129. delete previousModel;
  130. }
  131. }
  132. //-----------------------------------------------------------------------------
  133. void vtkQtDebugLeaksView::onFilterTextChanged(const QString& text)
  134. {
  135. if (this->filterEnabled())
  136. {
  137. this->Internal->ProxyModel->setFilterRegExp(text);
  138. }
  139. }
  140. //-----------------------------------------------------------------------------
  141. void vtkQtDebugLeaksView::onFilterToggled()
  142. {
  143. QString text = this->filterText();
  144. if (!this->filterEnabled())
  145. {
  146. text = "";
  147. }
  148. this->Internal->ProxyModel->setFilterRegExp(text);
  149. }
  150. //-----------------------------------------------------------------------------
  151. bool vtkQtDebugLeaksView::filterEnabled() const
  152. {
  153. return this->Internal->FilterCheckBox->isChecked();
  154. }
  155. //-----------------------------------------------------------------------------
  156. void vtkQtDebugLeaksView::setFilterEnabled(bool value)
  157. {
  158. this->Internal->FilterCheckBox->setChecked(value);
  159. }
  160. //-----------------------------------------------------------------------------
  161. QString vtkQtDebugLeaksView::filterText() const
  162. {
  163. return this->Internal->FilterLineEdit->text();
  164. }
  165. //-----------------------------------------------------------------------------
  166. void vtkQtDebugLeaksView::setFilterText(const QString& text)
  167. {
  168. this->Internal->FilterLineEdit->setText(text);
  169. }
  170. //-----------------------------------------------------------------------------
  171. Q_DECLARE_METATYPE(vtkObjectBase*);
  172. //-----------------------------------------------------------------------------
  173. void vtkQtDebugLeaksView::onRowDoubleClicked(const QModelIndex& index)
  174. {
  175. if (index.model() == this->Internal->ReferenceTableView->model())
  176. {
  177. QModelIndex objectIndex = this->Internal->ReferenceTableView->model()->index(index.row(), 0);
  178. QVariant objectVariant = this->Internal->ReferenceTableView->model()->data(objectIndex, Qt::UserRole);
  179. vtkObjectBase* object = objectVariant.value<vtkObjectBase*>();
  180. this->onObjectDoubleClicked(object);
  181. }
  182. else
  183. {
  184. QModelIndex sourceIndex = this->Internal->ProxyModel->mapToSource(index);
  185. if (sourceIndex.isValid())
  186. {
  187. QString className = this->Internal->Model->data(
  188. this->Internal->Model->index(sourceIndex.row(), 0)).toString();
  189. this->onClassNameDoubleClicked(className);
  190. }
  191. }
  192. }
  193. //-----------------------------------------------------------------------------
  194. void vtkQtDebugLeaksView::onObjectDoubleClicked(vtkObjectBase* object)
  195. {
  196. Q_UNUSED(object);
  197. }
  198. //-----------------------------------------------------------------------------
  199. void vtkQtDebugLeaksView::onClassNameDoubleClicked(const QString& className)
  200. {
  201. Q_UNUSED(className);
  202. }