File: stackselection.cpp

package info (click to toggle)
kcachegrind 4%3A16.08.3-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,844 kB
  • ctags: 3,250
  • sloc: cpp: 28,541; perl: 325; python: 235; makefile: 7; sh: 5
file content (268 lines) | stat: -rw-r--r-- 7,108 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* This file is part of KCachegrind.
   Copyright (c) 2003-2015 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>

   KCachegrind is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation, version 2.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

/*
 * StackSelection for KCachegrind
 * For function selection of a most expected stack,
 *  to be put into a QDockWindow
 */

#include "stackselection.h"

#include <QPushButton>
#include <QVBoxLayout>
#include <QTreeWidget>
#include <QHeaderView>

#include "stackbrowser.h"
#include "stackitem.h"


StackSelection::StackSelection(QWidget* parent)
  : QWidget(parent)
{
  _data = 0;
  _browser = new StackBrowser();
  _function = 0;
  _eventType = 0;
  _eventType2 = 0;
  _groupType = ProfileContext::Function;

  setWindowTitle(tr("Stack Selection"));

  QVBoxLayout* vboxLayout = new QVBoxLayout(this);
  vboxLayout->setSpacing(6);
  vboxLayout->setMargin(3);

  _stackList = new QTreeWidget(this);
  QStringList headerLabels;
  headerLabels << tr("Cost")
               << tr("Cost2")
               << tr("Calls")
               << tr("Function");
  _stackList->setHeaderLabels(headerLabels);
  _stackList->setRootIsDecorated(false);
  _stackList->setAllColumnsShowFocus(true);
  _stackList->setUniformRowHeights(true);
  _stackList->setSortingEnabled(false);
  _stackList->setColumnWidth(0, 50);
  // 2nd cost column hidden at first (_eventType2 == 0)
  _stackList->setColumnWidth(1, 0);
  _stackList->setColumnWidth(2, 50);
  vboxLayout->addWidget(_stackList);

  connect(_stackList,
          SIGNAL( currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
          this, SLOT( stackSelected(QTreeWidgetItem*,QTreeWidgetItem*) ) );
}

StackSelection::~StackSelection()
{
  delete _browser;
}

void StackSelection::setData(TraceData* data)
{
  if (_data == data) return;

  _data = data;

  _stackList->clear();
   delete _browser;
  _browser = new StackBrowser();
  _function = 0;
}


void StackSelection::setFunction(TraceFunction* f)
{
  if (_function == f) return;
  _function = f;

  if (!_data || !_function) return;

  //qDebug() << "StackSelection::setFunction " << f->name();

  HistoryItem* item = _browser->current();
  if (!item || item->function() != f) {
    _browser->select(f);
    rebuildStackList();
  }
}


void StackSelection::rebuildStackList()
{
  HistoryItem* item = _browser->current();
  _stackList->clear();
  _stackList->setColumnWidth(0, 50);
  _stackList->setColumnWidth(1, _eventType2 ? 50:0);
  _stackList->setColumnWidth(2, 50);
  if (!item || !item->stack()) return;

  TraceFunction* top = item->stack()->top();
  if (!top) return;


  QList<QTreeWidgetItem*> items;
  QTreeWidgetItem* activeItem = 0;
  TraceCallList l = item->stack()->calls();
  for(int i=l.count()-1; i>=0; i--) {
      StackItem* si = new StackItem(this, 0, l.at(i));
      if (si->function() == item->function())
          activeItem = si;
      items.prepend(si);
  }
  StackItem* si = new StackItem(this, 0, top);
  if (si->function() == item->function())
      activeItem = si;
  items.prepend(si);

#if QT_VERSION >= 0x050000
  _stackList->header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
  _stackList->header()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
  _stackList->header()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
#else
  _stackList->header()->setResizeMode(0, QHeaderView::ResizeToContents);
  _stackList->header()->setResizeMode(1, QHeaderView::ResizeToContents);
  _stackList->header()->setResizeMode(2, QHeaderView::ResizeToContents);
#endif

  _stackList->addTopLevelItems(items);
  if (activeItem) {
    // this calls stackFunctionSelected()
    _stackList->setCurrentItem(activeItem);
    _stackList->scrollToItem(activeItem);
  }

#if QT_VERSION >= 0x050000
  _stackList->header()->setSectionResizeMode(0, QHeaderView::Interactive);
  _stackList->header()->setSectionResizeMode(1, QHeaderView::Interactive);
  _stackList->header()->setSectionResizeMode(2, QHeaderView::Interactive);
#else
  _stackList->header()->setResizeMode(0, QHeaderView::Interactive);
  _stackList->header()->setResizeMode(1, QHeaderView::Interactive);
  _stackList->header()->setResizeMode(2, QHeaderView::Interactive);
#endif

  if (!_eventType2) {
    _stackList->setColumnWidth(1, 0);
  }
}

void StackSelection::stackSelected(QTreeWidgetItem* i, QTreeWidgetItem*)
{
  if (!i) return;

  TraceFunction* f = ((StackItem*)i)->function();
  emit functionSelected(f);
}


void StackSelection::browserBack()
{
  if (_browser && _browser->canGoBack()) {
    _browser->goBack();
    rebuildStackList();
  }
}

void StackSelection::browserForward()
{
  if (_browser && _browser->canGoForward()) {
    _browser->goForward();
    rebuildStackList();
  }
}

void StackSelection::browserUp()
{
  if (_browser) {
    _browser->goUp();
    rebuildStackList();
  }
}

void StackSelection::browserDown()
{
  if (_browser) {
    _browser->goDown();
    rebuildStackList();
  }
}

void StackSelection::refresh()
{
#if QT_VERSION >= 0x050000
    _stackList->header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
    _stackList->header()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
#else
    _stackList->header()->setResizeMode(0, QHeaderView::ResizeToContents);
    _stackList->header()->setResizeMode(1, QHeaderView::ResizeToContents);
#endif

    // there is no resorting allowed, so this is save
    for(int i = 0; i < _stackList->topLevelItemCount(); i++) {
        QTreeWidgetItem* item = _stackList->topLevelItem(i);
        ((StackItem*)item)->updateCost();
    }

    if (!_eventType2) {
#if QT_VERSION >= 0x050000
        _stackList->header()->setSectionResizeMode(1, QHeaderView::Interactive);
#else
        _stackList->header()->setResizeMode(1, QHeaderView::Interactive);
#endif
        _stackList->setColumnWidth(1, 0);
    }
}

void StackSelection::setEventType(EventType* ct)
{
  if (ct == _eventType) return;
  _eventType = ct;

  if (_eventType)
    _stackList->headerItem()->setText(0, _eventType->name());

  refresh();
}

void StackSelection::setEventType2(EventType* ct)
{
  if (ct == _eventType2) return;
  _eventType2 = ct;

  if (_eventType2)
    _stackList->headerItem()->setText(1, _eventType2->name());

  refresh();
}

void StackSelection::setGroupType(ProfileContext::Type gt)
{
  if (_groupType == gt) return;
  _groupType = gt;

  for(int i = 0; i < _stackList->topLevelItemCount(); i++) {
      QTreeWidgetItem* item = _stackList->topLevelItem(i);
      ((StackItem*)item)->updateGroup();
  }
}

#include "stackselection.moc"