~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to parts/valgrind/valgrind_widget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2010-05-05 07:21:55 UTC
  • mfrom: (1.2.3 upstream) (5.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100505072155-h78lx19pu04sbhtn
Tags: 4:4.0.0-2
* Upload to unstable (Closes: #579947, #481832).
* Acknowledge obsolete NMU fixes (Closes: #562410, #546961).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <qlayout.h>
2
 
#include <qpainter.h>
3
 
#include <qpopupmenu.h>
4
 
 
5
 
#include <kparts/part.h>
6
 
#include <klibloader.h>
7
 
#include <kurl.h>
8
 
#include <kdebug.h>
9
 
#include <klistview.h>
10
 
#include <klocale.h>
11
 
#include <kstatusbar.h>
12
 
 
13
 
#include <kdevcore.h>
14
 
#include <kdevpartcontroller.h>
15
 
#include <kdevmainwindow.h>
16
 
#include <kdevproject.h>
17
 
 
18
 
#include "valgrind_part.h"
19
 
#include "valgrind_widget.h"
20
 
 
21
 
#define VALLISTVIEWITEMRTTI 130977
22
 
 
23
 
// helper class to sort the ListView by item number instead of the string representation of the item number
24
 
class ValListViewItem: public QListViewItem
25
 
{
26
 
public:
27
 
  ValListViewItem( QListView* parent, int key, int pid, const QString& message ):
28
 
    QListViewItem( parent, QString::number( key ), QString::number( pid ), message ),
29
 
    _key( key ), _pid ( pid ), backtrace( false ), _line( -1 ), _active( false ) {}
30
 
 
31
 
  ValListViewItem( ValListViewItem* parent, int key, int pid, const QString& message, const QString& filename, int line, bool active ):
32
 
    QListViewItem( parent, QString::number( key ), QString::null, message ),
33
 
    _key( key ), _pid( pid ), backtrace( true ), _filename( filename ), _line( line ), _active( active )
34
 
  {
35
 
    if ( parent->_pid != _pid && _pid > 0 )
36
 
      setText( 1, QString::number( _pid ) );
37
 
  }
38
 
 
39
 
  virtual ~ValListViewItem();
40
 
 
41
 
  static int intCompare( int i1, int i2 )
42
 
  {
43
 
    if ( i1 > i2 )
44
 
      return 1;
45
 
    else if ( i1 < i2 )
46
 
      return -1;
47
 
    else
48
 
      return 0;
49
 
  }
50
 
 
51
 
  int compare( QListViewItem* i, int col, bool ascending ) const
52
 
  {
53
 
    if ( !i || i->rtti() != VALLISTVIEWITEMRTTI )
54
 
      return QListViewItem::compare( i, col, ascending );
55
 
    switch ( col ) {
56
 
      case 0 : return intCompare( ((ValListViewItem*)i)->_key, _key );
57
 
      case 1 : return intCompare( ((ValListViewItem*)i)->_pid, _pid );
58
 
      default: return QListViewItem::compare( i, col, ascending );
59
 
    }
60
 
  }
61
 
 
62
 
  void paintCell( QPainter* p, const QColorGroup& cg, int column, int width, int align )
63
 
  {
64
 
    if ( _active ) {
65
 
      QFont fnt = p->font();
66
 
      fnt.setBold( true );
67
 
      p->setFont( fnt );
68
 
    }
69
 
    QListViewItem::paintCell( p, cg, column, width, align );
70
 
  }
71
 
 
72
 
  int rtti() const { return VALLISTVIEWITEMRTTI; }
73
 
 
74
 
  QString fileName() const { return _filename; }
75
 
  int line() const { return _line; }
76
 
  QString message() const { return text( 2 ); }
77
 
  bool isHighlighted() const { return _active; }
78
 
 
79
 
private:
80
 
  int _key;
81
 
  int _pid;
82
 
  bool backtrace;
83
 
  QString _filename;
84
 
  int _line;
85
 
  bool _active;
86
 
};
87
 
 
88
 
ValListViewItem::~ValListViewItem() {}
89
 
 
90
 
ValgrindWidget::ValgrindWidget( ValgrindPart *part )
91
 
 : QWidget(0, "valgrind widget"), _part( part )
92
 
{
93
 
  QVBoxLayout* vbl = new QVBoxLayout( this );
94
 
  lv = new KListView( this );
95
 
  lv->addColumn( i18n( "No." ) );
96
 
  lv->addColumn( i18n( "Thread" ) );
97
 
  lv->addColumn( i18n( "Message" ) );
98
 
  lv->setSorting( 0, false );
99
 
  lv->setRootIsDecorated( true );
100
 
  lv->setAllColumnsShowFocus( true );
101
 
  vbl->addWidget( lv );
102
 
 
103
 
  popup = new QPopupMenu( lv, "valPopup" );
104
 
  popup->insertItem( i18n( "&Open Valgrind Output..." ), _part, SLOT(loadOutput()), 0, 0 );
105
 
  popup->insertSeparator();
106
 
  popup->insertItem( i18n( "Expand All Items" ), this, SLOT(expandAll()), 0, 2 );
107
 
  popup->insertItem( i18n( "Collapse All Items" ), this, SLOT(collapseAll()), 0, 3 );
108
 
 
109
 
  connect( popup, SIGNAL(aboutToShow()),
110
 
           this, SLOT(aboutToShowPopup()) );
111
 
  connect( lv, SIGNAL(executed(QListViewItem*)),
112
 
           this, SLOT(executed(QListViewItem*)) );
113
 
  connect( lv, SIGNAL(contextMenu(KListView*, QListViewItem*, const QPoint&)),
114
 
           this, SLOT(slotContextMenu(KListView*, QListViewItem*, const QPoint&)) );
115
 
}
116
 
 
117
 
 
118
 
ValgrindWidget::~ValgrindWidget()
119
 
{
120
 
}
121
 
 
122
 
void ValgrindWidget::clear()
123
 
{
124
 
  lv->clear();
125
 
  msgNumber = 1;
126
 
}
127
 
 
128
 
void ValgrindWidget::addMessage( const ValgrindItem& vi )
129
 
{
130
 
  QStringList projectFiles;
131
 
  QString projectDirectory;
132
 
 
133
 
  ValListViewItem* lvi = new ValListViewItem( lv, msgNumber++, vi.pid(), vi.message() );
134
 
  lvi->setMultiLinesEnabled( true );
135
 
  const ValgrindItem::BacktraceList backtrace = vi.backtrace();
136
 
  if ( !backtrace.isEmpty() )
137
 
    lvi->setExpandable( true );
138
 
 
139
 
  int i = 0;
140
 
  for ( ValgrindItem::BacktraceList::ConstIterator it = backtrace.begin(); it != backtrace.end(); ++it ) {
141
 
    new ValListViewItem( lvi, ++i, (*it).pid(), (*it).message(), (*it).url(), (*it).line(), (*it).isHighlighted() );
142
 
  }
143
 
}
144
 
 
145
 
void ValgrindWidget::executed( QListViewItem* lvi )
146
 
{
147
 
  Q_ASSERT( _part );
148
 
  Q_ASSERT( _part->partController() );
149
 
  Q_ASSERT( _part->mainWindow() );
150
 
 
151
 
  if ( !lvi || lvi->rtti() != VALLISTVIEWITEMRTTI )
152
 
    return;
153
 
  ValListViewItem* vli = 0;
154
 
  if ( !((ValListViewItem*)lvi)->fileName().isEmpty() ) {
155
 
    vli = (ValListViewItem*)lvi;
156
 
  } else if ( lvi->isExpandable() ) {
157
 
    // find the memleak position
158
 
    QListViewItemIterator it( lv );
159
 
    while ( vli == 0 && it.current() ) {
160
 
      if ( it.current()->rtti() == VALLISTVIEWITEMRTTI && ((ValListViewItem*)it.current())->isHighlighted() )
161
 
          vli = (ValListViewItem*)it.current();
162
 
      ++it;
163
 
    }
164
 
  }
165
 
  if ( vli ) {
166
 
    // display the file
167
 
    _part->partController()->editDocument( KURL( vli->fileName() ), vli->line() - 1 );
168
 
    _part->mainWindow()->statusBar()->message( vli->message(), 10000 );
169
 
  }
170
 
}
171
 
 
172
 
void ValgrindWidget::expandAll()
173
 
{
174
 
  QListViewItem* child = lv->firstChild();
175
 
  while ( child ) {
176
 
    child->setOpen( true );
177
 
    child = child->nextSibling();
178
 
  }
179
 
}
180
 
 
181
 
void ValgrindWidget::collapseAll()
182
 
{
183
 
  QListViewItem* child = lv->firstChild();
184
 
  while ( child ) {
185
 
    child->setOpen( false );
186
 
    child = child->nextSibling();
187
 
  }
188
 
}
189
 
 
190
 
void ValgrindWidget::aboutToShowPopup()
191
 
{
192
 
  bool en = (lv->firstChild() != 0);
193
 
  popup->setItemEnabled( 2, en );
194
 
  popup->setItemEnabled( 3, en );
195
 
}
196
 
 
197
 
void ValgrindWidget::slotContextMenu( KListView* l, QListViewItem* /*i*/, const QPoint& p )
198
 
{
199
 
  if ( l != lv )
200
 
    return;
201
 
 
202
 
  popup->exec( p );
203
 
}
204
 
 
205
 
#include "valgrind_widget.moc"