PageRenderTime 71ms CodeModel.GetById 41ms RepoModel.GetById 1ms app.codeStats 0ms

/ui/qt/widgets/profile_tree_view.cpp

https://gitlab.com/jvelando/wireshark
C++ | 118 lines | 84 code | 21 blank | 13 comment | 17 complexity | 7f668994162533d74b3a5bbd2ab21b2b MD5 | raw file
  1. /* profile_tree_view.cpp
  2. *
  3. * Wireshark - Network traffic analyzer
  4. * By Gerald Combs <gerald@wireshark.org>
  5. * Copyright 1998 Gerald Combs
  6. *
  7. * SPDX-License-Identifier: GPL-2.0-or-later
  8. */
  9. #include <ui/qt/models/url_link_delegate.h>
  10. #include <ui/qt/models/profile_model.h>
  11. #include <ui/qt/widgets/profile_tree_view.h>
  12. #include <QDesktopServices>
  13. #include <QDir>
  14. #include <QItemDelegate>
  15. #include <QLineEdit>
  16. #include <QUrl>
  17. ProfileUrlLinkDelegate::ProfileUrlLinkDelegate(QObject *parent) : UrlLinkDelegate (parent) {}
  18. void ProfileUrlLinkDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
  19. {
  20. /* Only paint links for valid paths */
  21. if (index.data(ProfileModel::DATA_PATH_IS_NOT_DESCRIPTION).toBool())
  22. UrlLinkDelegate::paint(painter, option, index);
  23. else
  24. QStyledItemDelegate::paint(painter, option, index);
  25. }
  26. ProfileTreeEditDelegate::ProfileTreeEditDelegate(QWidget *parent) : QItemDelegate(parent), editor_(Q_NULLPTR) {}
  27. void ProfileTreeEditDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  28. {
  29. if (qobject_cast<QLineEdit *>(editor))
  30. {
  31. QLineEdit * ql = qobject_cast<QLineEdit *>(editor);
  32. ql->setText(index.data().toString());
  33. }
  34. }
  35. ProfileTreeView::ProfileTreeView(QWidget *parent) :
  36. QTreeView (parent)
  37. {
  38. delegate_ = new ProfileTreeEditDelegate();
  39. setItemDelegateForColumn(ProfileModel::COL_NAME, delegate_);
  40. connect(this, &QAbstractItemView::clicked, this, &ProfileTreeView::clicked);
  41. connect(delegate_, SIGNAL(commitData(QWidget *)), this, SIGNAL(itemUpdated()));
  42. }
  43. ProfileTreeView::~ProfileTreeView()
  44. {
  45. delete delegate_;
  46. }
  47. void ProfileTreeView::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
  48. {
  49. QTreeView::selectionChanged(selected, deselected);
  50. if (model())
  51. {
  52. int offColumn = model()->columnCount();
  53. int idxCount = selectedIndexes().count() / offColumn;
  54. int dselCount = deselected.count() > 0 ? deselected.at(0).indexes().count() / offColumn : 0;
  55. /* Ensure, that the last selected row cannot be deselected */
  56. if (idxCount == 0 && dselCount == 1)
  57. {
  58. QModelIndex idx = deselected.at(0).indexes().at(0);
  59. /* If the last item is no longer valid or the row is out of bounds, select default */
  60. if (! idx.isValid() || idx.row() >= model()->rowCount())
  61. idx = model()->index(0, ProfileModel::COL_NAME);
  62. selectRow(idx.row());
  63. }
  64. else if (selectedIndexes().count() == 0)
  65. selectRow(0);
  66. }
  67. }
  68. void ProfileTreeView::clicked(const QModelIndex &index)
  69. {
  70. if (!index.isValid())
  71. return;
  72. /* Only paint links for valid paths */
  73. if (index.data(ProfileModel::DATA_INDEX_VALUE_IS_URL).toBool())
  74. {
  75. QString path = QDir::toNativeSeparators(index.data().toString());
  76. QDesktopServices::openUrl(QUrl::fromLocalFile(path));
  77. }
  78. }
  79. void ProfileTreeView::selectRow(int row)
  80. {
  81. if (row < 0)
  82. return;
  83. setCurrentIndex(model()->index(row, 0));
  84. selectionModel()->select(
  85. QItemSelection(model()->index(row, 0), model()->index(row, model()->columnCount() -1)),
  86. QItemSelectionModel::ClearAndSelect);
  87. }
  88. void ProfileTreeView::mouseDoubleClickEvent(QMouseEvent *ev)
  89. {
  90. /* due to the fact, that we allow only row selection, selected rows are always added with all columns */
  91. if (selectedIndexes().count() <= model()->columnCount())
  92. QTreeView::mouseDoubleClickEvent(ev);
  93. }
  94. bool ProfileTreeView::activeEdit()
  95. {
  96. return (state() == QAbstractItemView::EditingState);
  97. }