File: eventtypeview.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 (338 lines) | stat: -rw-r--r-- 8,848 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* 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.
*/

/*
 * Event Type View
 */


#include "eventtypeview.h"

#include <QAction>
#include <QMenu>
#include <QHeaderView>

#include "eventtypeitem.h"
#include "toplevelbase.h"


//
// EventTypeView
//

EventTypeView::EventTypeView(TraceItemView* parentView,
			     QWidget* parent, const char* name)
  : QTreeWidget(parent), TraceItemView(parentView)
{
    setObjectName(name);
    // forbid scaling icon pixmaps to smaller size
    setIconSize(QSize(99,99));
    setColumnCount(6);
    QStringList labels;
    labels  << tr( "Event Type" )
	    << tr( "Incl." )
	    << tr( "Self" )
	    << tr( "Short" )
	    << QString()
	    << tr( "Formula" );
    setHeaderLabels(labels);
    // reduce minimum width for '=' column
    header()->setMinimumSectionSize(10);

    setRootIsDecorated(false);
    setSortingEnabled(false);
    setAllColumnsShowFocus(true);
    setMinimumHeight(50);


    setContextMenuPolicy(Qt::CustomContextMenu);
    connect( this,
	     SIGNAL(customContextMenuRequested(const QPoint &)),
	     SLOT(context(const QPoint &)));

    // FIXME: Endless jumping among 2 types possible!
    connect( this,
	     SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
	     SLOT(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)) );

    connect(this,
	    SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
	    SLOT(itemDoubleClicked(QTreeWidgetItem*,int)));

    connect(this,
	    SIGNAL(itemChanged(QTreeWidgetItem*,int)),
	    SLOT(itemChanged(QTreeWidgetItem*,int)));

    setWhatsThis( whatsThis() );
}

QString EventTypeView::whatsThis() const
{
    return tr( "<b>Cost Types List</b>"
		 "<p>This list shows all cost types available "
		 "and what the self/inclusive cost of the "
		 "current selected function is for that cost type.</p>"
		 "<p>By choosing a cost type from the list, "
		 "you change the cost type of costs shown "
		 "all over KCachegrind to be the selected one.</p>");
}


void EventTypeView::context(const QPoint & p)
{
  QMenu popup;

  QTreeWidgetItem* i = itemAt(p);
  EventType* ct = i ? ((EventTypeItem*) i)->eventType() : 0;

  QAction* selectType2Action = 0;
  QAction* hideType2Action = 0;
  if (ct)
    selectType2Action = popup.addAction(tr("Set as Secondary Event Type"));
  if (_eventType2)
    hideType2Action = popup.addAction(tr("Hide Secondary Event Type"));
  if (!popup.isEmpty())
    popup.addSeparator();

  QAction* editLongNameAction = 0;
  QAction* editShortNameAction = 0;
  QAction* editFormulaAction = 0;
  QAction* removeTypeAction = 0;
  if (ct && !ct->isReal()) {
      editLongNameAction = popup.addAction(tr("Edit Long Name"));
      editShortNameAction = popup.addAction(tr("Edit Short Name"));
      editFormulaAction = popup.addAction(tr("Edit Formula"));
      removeTypeAction = popup.addAction(tr("Remove"));
      popup.addSeparator();
  }

  addGoMenu(&popup);

  QAction* newTypeAction = 0;
  if( _data) {
      popup.addSeparator();
      newTypeAction = popup.addAction(tr("New Event Type..."));
  }

  QAction* a = popup.exec(viewport()->mapToGlobal(p));
  if (a == hideType2Action) selectedEventType2(0);
  else if (a == selectType2Action) selectedEventType2(ct);
  else if (a == editLongNameAction) editItem(i, 0);
  else if (a == editShortNameAction) editItem(i, 3);
  else if (a == editFormulaAction) editItem(i, 5);
  else if (a == removeTypeAction) {

    // search for a previous type 
    EventType* prev = 0, *ct = 0;
    EventTypeSet* m = _data->eventTypes();
    for (int i=0;i<m->realCount();i++) {
	ct = m->realType(i);
	if (ct) prev = ct;
    }
    for (int i=0;i<m->derivedCount();i++) {
	ct = m->derivedType(i);
	if (ct == _eventType) break;
	if (ct) prev = ct;
    }

    if (_data->eventTypes()->remove(ct)) {
      // select previous cost type
      selectedEventType(prev);
      if (_eventType2 == ct)
	selectedEventType2(prev);
      refresh();
    }
  }
  else if (a == newTypeAction) {
    int i = 1;
    while(1) {
	if (!EventType::knownDerivedType(tr("New%1").arg(i)))
	break;
      i++;
    }
    // add same new cost type to this set and to known types
    QString shortName = tr("New%1").arg(i);
    QString longName  = tr("New Event Type %1").arg(i);
    EventType* et;
    et = new EventType(shortName, longName);
    et->setFormula(QString()); // event is derived
    EventType::add(et);
    // EventType::add() took ownership, need new object
    et = new EventType(shortName, longName);
    et->setFormula(QString()); // event is derived
    _data->eventTypes()->add(et);
    refresh();
  }
}


void EventTypeView::currentItemChanged(QTreeWidgetItem* i, QTreeWidgetItem*)
{
    EventType* ct = i ? ((EventTypeItem*) i)->eventType() : 0;
    if (ct)
	selectedEventType(ct);
}

void EventTypeView::itemDoubleClicked(QTreeWidgetItem* i, int)
{
  EventType* ct = i ? ((EventTypeItem*) i)->eventType() : 0;
  if (ct)
      selectedEventType2(ct);
}

CostItem* EventTypeView::canShow(CostItem* i)
{
    if (!i) return 0;

    switch(i->type()) {
    case ProfileContext::Object:
    case ProfileContext::Class:
    case ProfileContext::File:
    case ProfileContext::Call:
    case ProfileContext::FunctionCycle:
    case ProfileContext::Function:
	break;
    default:
	return 0;
    }
    return i;
}

void EventTypeView::doUpdate(int changeType, bool)
{
    // Special case ?
    if (changeType == selectedItemChanged) return;

    if (changeType == eventType2Changed) return;

    if (changeType == groupTypeChanged) {
	for(int i = 0; i < topLevelItemCount(); i++)
	    ((EventTypeItem*)topLevelItem(i))->setGroupType(_groupType);

	return;
    }

    if (changeType == eventTypeChanged) {
	for(int i = 0; i < topLevelItemCount(); i++) {
	    EventTypeItem* item = (EventTypeItem*)topLevelItem(i);
	    if ( item->eventType() == _eventType) {
		setCurrentItem(item);
		scrollToItem(item);
		break;
	    }
	}

	return;
    }

    if (changeType == partsChanged) {
	for(int i = 0; i < topLevelItemCount(); i++)
	    ((EventTypeItem*)topLevelItem(i))->update();

	resizeColumnToContents(1);
	resizeColumnToContents(2);
	return;
    }


    refresh();
}

void EventTypeView::refresh()
{
    clear();
    setColumnWidth(1, 50);
    setColumnWidth(2, 50);

    if (!_data || !_activeItem) return;
    switch(_activeItem->type()) {
    case ProfileContext::Object:
    case ProfileContext::Class:
    case ProfileContext::File:
    case ProfileContext::FunctionCycle:
    case ProfileContext::Function:
	break;
    default:
	return;
    }
    TraceCostItem* c = (TraceCostItem*) _activeItem;

    EventType* ct =0;
    QTreeWidgetItem* item = 0;
    QTreeWidgetItem* selected = 0;
    QList<QTreeWidgetItem*> items;
    QString sumStr, pureStr;

    EventTypeSet* m = _data->eventTypes();
    for (int i=0; i<m->realCount();i++) {
	ct = m->realType(i);
	item = new EventTypeItem(c, ct, _groupType);
	if (ct == _eventType) selected = item;
	items.append(item);
    }
    for (int i=0; i<m->derivedCount();i++) {
	ct = m->derivedType(i);
	if (!ct) continue;
	item = new EventTypeItem(c, ct, _groupType);
	if (ct == _eventType) selected = item;
	items.append(item);
    }
    insertTopLevelItems(0,items);

    if (selected) {
	setCurrentItem(selected);
	scrollToItem(selected);
    }

    for(int c = 0; c<6; c++)
	resizeColumnToContents(c);
}

void EventTypeView::itemChanged(QTreeWidgetItem* item, int c)
{
  EventType* ct = item ? ((EventTypeItem*) item)->eventType() : 0;
  if (!ct || ct->isReal()) return;

  // search for matching known Type
  int knownCount = EventType::knownTypeCount();
  EventType* known = 0;
  for (int i=0; i<knownCount; i++) {
      known = EventType::knownType(i);
      if (known->name() == ct->name()) break;
  }

  QString t = item->text(c);
  if (c == 0) {
      ct->setLongName(t);
      if (known) known->setLongName(t);
  }
  else if (c == 3) {
      ct->setName(t);
      if (known) known->setName(t);
  }
  else if (c == 5) {
      ct->setFormula(t);
      if (known) known->setFormula(t);
  }
  else return;

  if (_topLevel) _topLevel->configChanged();
  refresh();
}

#include "eventtypeview.moc"