• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdevelop API Reference
  • KDE Home
  • Contact Us
 

kdevplatform/debugger

  • sources
  • kfour-appscomplete
  • kdevelop
  • kdevplatform
  • debugger
  • variable
variabletooltip.cpp
Go to the documentation of this file.
1 /*
2  * KDevelop Debugger Support
3  *
4  * Copyright 2008 Vladimir Prus <[email protected]>
5  * Copyright 2009 Niko Sams <[email protected]>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public
18  * License along with this program; if not, write to the
19  * Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  */
22 
23 #include "variabletooltip.h"
24 
25 #include <QWidget>
26 #include <QHBoxLayout>
27 #include <QApplication>
28 #include <QMouseEvent>
29 #include <QHeaderView>
30 #include <QPushButton>
31 #include <QScrollBar>
32 #include <QDesktopWidget>
33 #include <KLocalizedString>
34 #include <QPainter>
35 #include <QSortFilterProxyModel>
36 
37 #include "variablecollection.h"
38 #include "../util/treeview.h"
39 #include "../interfaces/ivariablecontroller.h"
40 #include "../../util/activetooltip.h"
41 #include "../../interfaces/icore.h"
42 #include "../../interfaces/idebugcontroller.h"
43 
44 namespace KDevelop {
45 
46 class SizeGrip : public QWidget
47 {
48  Q_OBJECT
49 public:
50  explicit SizeGrip(QWidget* parent) : QWidget(parent) {
51  m_parent = parent;
52  }
53 protected:
54  void paintEvent(QPaintEvent *) override
55  {
56  QPainter painter(this);
57  QStyleOptionSizeGrip opt;
58  opt.init(this);
59  opt.corner = Qt::BottomRightCorner;
60  style()->drawControl(QStyle::CE_SizeGrip, &opt, &painter, this);
61  }
62 
63  void mousePressEvent(QMouseEvent* e) override
64  {
65  if (e->button() == Qt::LeftButton) {
66  m_pos = e->globalPos();
67  m_startSize = m_parent->size();
68  e->ignore();
69  }
70  }
71  void mouseReleaseEvent(QMouseEvent*) override
72  {
73  m_pos = QPoint();
74  }
75  void mouseMoveEvent(QMouseEvent* e) override
76  {
77  if (!m_pos.isNull()) {
78  m_parent->resize(
79  m_startSize.width() + (e->globalPos().x() - m_pos.x()),
80  m_startSize.height() + (e->globalPos().y() - m_pos.y())
81  );
82  }
83  }
84 private:
85  QWidget *m_parent;
86  QSize m_startSize;
87  QPoint m_pos;
88 };
89 
90 VariableToolTip::VariableToolTip(QWidget* parent, const QPoint& position,
91  const QString& identifier)
92 : ActiveToolTip(parent, position)
93 {
94  setPalette( QApplication::palette() );
95 
96  m_model = new TreeModel(QVector<QString>{i18n("Name"), i18n("Value"), i18n("Type")}, this);
97 
98  auto* tr = new TooltipRoot(m_model);
99  m_model->setRootItem(tr);
100  m_var = ICore::self()->debugController()->currentSession()->
101  variableController()->createVariable(
102  m_model, tr, identifier);
103  tr->init(m_var);
104  m_var->attachMaybe(this, "variableCreated");
105 
106  auto* l = new QVBoxLayout(this);
107  l->setContentsMargins(0, 0, 0, 0);
108  // setup proxy model
109  m_proxy = new QSortFilterProxyModel;
110  m_view = new AsyncTreeView(m_model, m_proxy, this);
111  m_proxy->setSourceModel(m_model);
112  m_view->setModel(m_proxy);
113  m_view->header()->resizeSection(0, 150);
114  m_view->header()->resizeSection(1, 90);
115  m_view->setSelectionBehavior(QAbstractItemView::SelectRows);
116  m_view->setSelectionMode(QAbstractItemView::SingleSelection);
117  m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
118  m_view->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding);
119  l->addWidget(m_view);
120 
121  QModelIndex varIndex = m_proxy->mapFromSource(m_model->indexForItem(m_var, 0));
122  m_itemHeight = m_view->indexRowSizeHint(varIndex);
123  connect(m_view->verticalScrollBar(),
124  &QScrollBar::rangeChanged,
125  this,
126  &VariableToolTip::slotRangeChanged);
127 
128  m_selection = m_view->selectionModel();
129  m_selection->select(varIndex,
130  QItemSelectionModel::Rows | QItemSelectionModel::ClearAndSelect);
131 
132  auto* buttonBox = new QHBoxLayout();
133  buttonBox->setContentsMargins(11, 0, 11, 6);
134  auto* watchThisButton = new QPushButton(i18n("Watch This"));
135  buttonBox->addWidget(watchThisButton);
136  auto* stopOnChangeButton = new QPushButton(i18n("Stop on Change"));
137  buttonBox->addWidget(stopOnChangeButton);
138 
139  connect(watchThisButton, &QPushButton::clicked,
140  this, [this](){ slotLinkActivated(QStringLiteral("add_watch")); });
141  connect(stopOnChangeButton, &QPushButton::clicked,
142  this, [this](){ slotLinkActivated(QStringLiteral("add_watchpoint")); });
143 
144  auto* inner = new QHBoxLayout();
145  l->addLayout(inner);
146  inner->setContentsMargins(0, 0, 0, 0);
147  inner->addLayout(buttonBox);
148  inner->addStretch();
149 
150  auto* g = new SizeGrip(this);
151  g->setFixedSize(16, 16);
152  inner->addWidget(g, 0, (Qt::Alignment)(Qt::AlignRight | Qt::AlignBottom));
153 
154  move(position);
155 }
156 
157 void VariableToolTip::variableCreated(bool hasValue)
158 {
159  m_view->resizeColumns();
160  if (hasValue) {
161  ActiveToolTip::showToolTip(this, 0.0);
162  } else {
163  close();
164  }
165 }
166 
167 void VariableToolTip::slotLinkActivated(const QString& link)
168 {
169  Variable* v = m_var;
170  QItemSelection s = m_selection->selection();
171  if (!s.empty())
172  {
173  QModelIndex index = s.front().topLeft();
174  const auto sourceIndex = m_proxy->mapToSource(index);
175  TreeItem *item = m_model->itemForIndex(sourceIndex);
176  if (item)
177  {
178  auto* v2 = qobject_cast<Variable*>(item);
179  if (v2)
180  v = v2;
181  }
182  }
183 
184  IDebugSession *session = ICore::self()->debugController()->currentSession();
185  if (session && session->state() != IDebugSession::NotStartedState && session->state() != IDebugSession::EndedState) {
186  if (link == QLatin1String("add_watch")) {
187  session->variableController()->addWatch(v);
188  } else if (link == QLatin1String("add_watchpoint")) {
189  session->variableController()->addWatchpoint(v);
190  }
191  }
192  close();
193 }
194 
195 void VariableToolTip::slotRangeChanged(int min, int max)
196 {
197  Q_ASSERT(min == 0);
198  Q_UNUSED(min);
199  QRect rect = QApplication::desktop()->screenGeometry(this);
200  if (pos().y() + height() + max*m_itemHeight < rect.bottom())
201  resize(width(), height() + max*m_itemHeight);
202  else
203  {
204  // Oh, well, I'm sorry, but here's the scrollbar you was
205  // longing to see
206  m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
207  }
208 }
209 
210 }
211 
212 #include "variabletooltip.moc"
KDevelop::AsyncTreeView::indexRowSizeHint
int indexRowSizeHint(const QModelIndex &index) const
QAbstractScrollArea::verticalScrollBar
QScrollBar * verticalScrollBar() const
QVBoxLayout
QStyle::drawControl
virtual void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const=0
QApplication::desktop
QDesktopWidget * desktop()
QItemSelectionModel::selection
const QItemSelection selection() const
QHeaderView::resizeSection
void resizeSection(int logicalIndex, int size)
QTreeView::setModel
virtual void setModel(QAbstractItemModel *model)
QWidget::setSizePolicy
void setSizePolicy(QSizePolicy)
QRect
QMouseEvent::button
Qt::MouseButton button() const
variablecollection.h
QAbstractButton::clicked
void clicked(bool checked)
QWidget
QAbstractItemView::selectionModel
QItemSelectionModel * selectionModel() const
KDevelop::AsyncTreeView
Definition: treeview.h:34
QAbstractItemView::setSelectionMode
void setSelectionMode(QAbstractItemView::SelectionMode mode)
QSize
KDevelop::Variable::attachMaybe
virtual void attachMaybe(QObject *callback=nullptr, const char *callbackMethod=nullptr)=0
QPoint::x
int x() const
QPoint::y
int y() const
QSortFilterProxyModel::mapToSource
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const
QSortFilterProxyModel::setSourceModel
virtual void setSourceModel(QAbstractItemModel *sourceModel)
QPainter
QMouseEvent
KDevelop::TreeModel
Definition: treemodel.h:38
QTreeView::header
QHeaderView * header() const
QList::empty
bool empty() const
ActiveToolTip
QRect::bottom
int bottom() const
QSortFilterProxyModel
KDevelop::TreeModel::itemForIndex
TreeItem * itemForIndex(const QModelIndex &index) const
Definition: treemodel.cpp:160
QPushButton
QWidget::style
QStyle * style() const
Qt::Alignment
typedef Alignment
KDevelop::VariableToolTip::VariableToolTip
VariableToolTip(QWidget *parent, const QPoint &position, const QString &identifier)
Definition: variabletooltip.cpp:90
variabletooltip.h
QString
KDevelop::AsyncTreeView::resizeColumns
void resizeColumns()
Definition: treeview.cpp:77
KDevelop::TreeModel::indexForItem
QModelIndex indexForItem(TreeItem *item, int column) const
Definition: treemodel.cpp:170
QLatin1String
KDevelop::TreeModel::setRootItem
void setRootItem(TreeItem *item)
Definition: treemodel.cpp:49
KDevelop::IDebugSession::EndedState
Definition: idebugsession.h:68
QItemSelection
QItemSelectionModel::select
virtual void select(const QModelIndex &index, QFlags< QItemSelectionModel::SelectionFlag > command)
QDesktopWidget::screenGeometry
const QRect screenGeometry(int screen) const
KDevelop::IDebugSession::NotStartedState
Definition: idebugsession.h:62
QStyleOptionSizeGrip
QSortFilterProxyModel::mapFromSource
virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const
QHBoxLayout
QPaintEvent
KDevelop::TooltipRoot
Definition: variablecollection.h:124
QMouseEvent::globalPos
const QPoint & globalPos() const
QEvent::ignore
void ignore()
QStyleOption::init
void init(const QWidget *widget)
KDevelop
The variables widget is passive, and is invoked by the rest of the code via two main Q_SLOTS:
Definition: breakpoint.h:34
QAbstractItemView::setSelectionBehavior
void setSelectionBehavior(QAbstractItemView::SelectionBehavior behavior)
QModelIndex
QVector< QString >
QList::front
T & front()
QAbstractSlider::rangeChanged
void rangeChanged(int min, int max)
QAbstractScrollArea::setVerticalScrollBarPolicy
void setVerticalScrollBarPolicy(Qt::ScrollBarPolicy)
QApplication::palette
QPalette palette()
QPoint
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Apr 16 2021 23:29:37 by doxygen 1.8.16 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kdevplatform/debugger

Skip menu "kdevplatform/debugger"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdevelop API Reference

Skip menu "kdevelop API Reference"
  • kdevplatform
  •   debugger
  •   documentation
  •   interfaces
  •   language
  •     assistant
  •     backgroundparser
  •     checks
  •     classmodel
  •     codecompletion
  •     codegen
  •     duchain
  •     editor
  •     highlighting
  •     interfaces
  •     util
  •   outputview
  •   project
  •   serialization
  •   shell
  •   sublime
  •   tests
  •   util
  •   vcs

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal