eventtypeview.cpp 8.39 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* This file is part of KCachegrind.
   Copyright (C) 2003 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
15
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Dirk Mueller's avatar
Dirk Mueller committed
16
   Boston, MA 02110-1301, USA.
17
18
19
*/

/*
20
 * Event Type View
21
22
 */

Laurent Montel's avatar
Laurent Montel committed
23

24
25
#include "eventtypeview.h"

26
27
#include <QAction>
#include <QMenu>
28
#include <QHeaderView>
29

30
#include "eventtypeitem.h"
31
#include "toplevelbase.h"
32
33
34


//
35
// EventTypeView
36
37
//

38
EventTypeView::EventTypeView(TraceItemView* parentView,
39
40
			     QWidget* parent, const char* name)
  : QTreeWidget(parent), TraceItemView(parentView)
41
{
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    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);
59
60
61
62
    setAllColumnsShowFocus(true);
    setMinimumHeight(50);


63
    setContextMenuPolicy(Qt::CustomContextMenu);
64
    connect( this,
65
66
	     SIGNAL(customContextMenuRequested(const QPoint &)),
	     SLOT(context(const QPoint &)));
67

68
69
70
71
    // FIXME: Endless jumping among 2 types possible!
    connect( this,
	     SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
	     SLOT(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)) );
72
73

    connect(this,
74
75
	    SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
	    SLOT(itemDoubleClicked(QTreeWidgetItem*,int)));
76
77

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

81
    setWhatsThis( whatsThis() );
82
83
}

84
QString EventTypeView::whatsThis() const
85
{
86
    return tr( "<b>Cost Types List</b>"
87
		 "<p>This list shows all cost types available "
Andrew Coles's avatar
   
Andrew Coles committed
88
89
		 "and what the self/inclusive cost of the "
		 "current selected function is for that cost type.</p>"
90
91
		 "<p>By choosing a cost type from the list, "
		 "you change the cost type of costs shown "
Jonathan Esk-Riddell's avatar
Jonathan Esk-Riddell committed
92
		 "all over KCachegrind to be the selected one.</p>");
93
94
}

95
96

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

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

103
104
  QAction* selectType2Action = 0;
  QAction* hideType2Action = 0;
Josef Weidendorfer's avatar
Josef Weidendorfer committed
105
  if (ct)
106
    selectType2Action = popup.addAction(tr("Set as Secondary Event Type"));
107
  if (_eventType2)
108
109
110
111
112
113
114
115
    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;
116
  if (ct && !ct->isReal()) {
117
118
119
120
121
      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();
122
123
  }

Josef Weidendorfer's avatar
Josef Weidendorfer committed
124
  addGoMenu(&popup);
125

126
127
128
  QAction* newTypeAction = 0;
  if( _data) {
      popup.addSeparator();
129
      newTypeAction = popup.addAction(tr("New Event Type..."));
Laurent Montel's avatar
Laurent Montel committed
130
  }
131

132
  QAction* a = popup.exec(viewport()->mapToGlobal(p));
133
134
  if (a == hideType2Action) selectedEventType2(0);
  else if (a == selectType2Action) selectedEventType2(ct);
135
136
137
  else if (a == editLongNameAction) editItem(i, 0);
  else if (a == editShortNameAction) editItem(i, 3);
  else if (a == editFormulaAction) editItem(i, 5);
138
  else if (a == removeTypeAction) {
Josef Weidendorfer's avatar
Josef Weidendorfer committed
139
140

    // search for a previous type 
141
142
    EventType* prev = 0, *ct = 0;
    EventTypeSet* m = _data->eventTypes();
Josef Weidendorfer's avatar
Josef Weidendorfer committed
143
144
145
146
    for (int i=0;i<m->realCount();i++) {
	ct = m->realType(i);
	if (ct) prev = ct;
    }
147
148
149
    for (int i=0;i<m->derivedCount();i++) {
	ct = m->derivedType(i);
	if (ct == _eventType) break;
Josef Weidendorfer's avatar
Josef Weidendorfer committed
150
151
152
	if (ct) prev = ct;
    }

153
    if (_data->eventTypes()->remove(ct)) {
Josef Weidendorfer's avatar
Josef Weidendorfer committed
154
      // select previous cost type
155
156
157
      selectedEventType(prev);
      if (_eventType2 == ct)
	selectedEventType2(prev);
158
      refresh();
Josef Weidendorfer's avatar
Josef Weidendorfer committed
159
    }
160
  }
161
  else if (a == newTypeAction) {
Josef Weidendorfer's avatar
Josef Weidendorfer committed
162
163
    int i = 1;
    while(1) {
164
	if (!EventType::knownDerivedType(tr("New%1").arg(i)))
Josef Weidendorfer's avatar
Josef Weidendorfer committed
165
166
167
	break;
      i++;
    }
168
    // add same new cost type to this set and to known types
169
170
    QString shortName = tr("New%1").arg(i);
    QString longName  = tr("New Event Type %1").arg(i);
171
172
    EventType::add(new EventType(shortName, longName, "0"));
    _data->eventTypes()->add(new EventType(shortName, longName, "0"));
Josef Weidendorfer's avatar
Josef Weidendorfer committed
173
    refresh();
174
175
176
  }
}

177
178

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

185
void EventTypeView::itemDoubleClicked(QTreeWidgetItem* i, int)
186
{
187
  EventType* ct = i ? ((EventTypeItem*) i)->eventType() : 0;
188
  if (ct)
189
      selectedEventType2(ct);
190
191
}

192
CostItem* EventTypeView::canShow(CostItem* i)
193
194
195
196
{
    if (!i) return 0;

    switch(i->type()) {
197
198
199
200
201
202
    case ProfileContext::Object:
    case ProfileContext::Class:
    case ProfileContext::File:
    case ProfileContext::Call:
    case ProfileContext::FunctionCycle:
    case ProfileContext::Function:
203
204
205
206
207
208
209
	break;
    default:
	return 0;
    }
    return i;
}

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

215
    if (changeType == eventType2Changed) return;
Josef Weidendorfer's avatar
Josef Weidendorfer committed
216

217
    if (changeType == groupTypeChanged) {
218
219
	for(int i = 0; i < topLevelItemCount(); i++)
	    ((EventTypeItem*)topLevelItem(i))->setGroupType(_groupType);
220
221
222
223

	return;
    }

224
    if (changeType == eventTypeChanged) {
225
226
227
228
229
	for(int i = 0; i < topLevelItemCount(); i++) {
	    EventTypeItem* item = (EventTypeItem*)topLevelItem(i);
	    if ( item->eventType() == _eventType) {
		setCurrentItem(item);
		scrollToItem(item);
230
231
		break;
	    }
232
	}
233
234
235
236
237

	return;
    }

    if (changeType == partsChanged) {
238
239
	for(int i = 0; i < topLevelItemCount(); i++)
	    ((EventTypeItem*)topLevelItem(i))->update();
240
241
242
243
244
245
246
247

	return;
    }


    refresh();
}

248
void EventTypeView::refresh()
249
250
251
252
253
254
255
{
    clear();
    setColumnWidth(1, 50);
    setColumnWidth(2, 50);

    if (!_data || !_activeItem) return;
    switch(_activeItem->type()) {
256
257
258
259
260
    case ProfileContext::Object:
    case ProfileContext::Class:
    case ProfileContext::File:
    case ProfileContext::FunctionCycle:
    case ProfileContext::Function:
261
262
263
264
265
266
	break;
    default:
	return;
    }
    TraceCostItem* c = (TraceCostItem*) _activeItem;

267
268
269
270
    EventType* ct =0;
    QTreeWidgetItem* item = 0;
    QTreeWidgetItem* selected = 0;
    QList<QTreeWidgetItem*> items;
271
272
    QString sumStr, pureStr;

273
    EventTypeSet* m = _data->eventTypes();
274
275
276
277
278
279
280
    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++) {
281
	ct = m->derivedType(i);
Josef Weidendorfer's avatar
Josef Weidendorfer committed
282
	if (!ct) continue;
283
284
285
	item = new EventTypeItem(c, ct, _groupType);
	if (ct == _eventType) selected = item;
	items.append(item);
286
    }
287
    insertTopLevelItems(0,items);
288

289
290
291
    if (selected) {
	setCurrentItem(selected);
	scrollToItem(selected);
292
293
    }

294
295
    for(int c = 0; c<6; c++)
	resizeColumnToContents(c);
296
297
}

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

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

311
  QString t = item->text(c);
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
  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();
}

330
#include "eventtypeview.moc"