00001 #include <qlayout.h>
00002 #include <qpainter.h>
00003 #include <qpopupmenu.h>
00004
00005 #include <kparts/part.h>
00006 #include <klibloader.h>
00007 #include <kurl.h>
00008 #include <kdebug.h>
00009 #include <klistview.h>
00010 #include <klocale.h>
00011 #include <kstatusbar.h>
00012
00013 #include <kdevcore.h>
00014 #include <kdevpartcontroller.h>
00015 #include <kdevmainwindow.h>
00016 #include <kdevproject.h>
00017
00018 #include "valgrind_part.h"
00019 #include "valgrind_widget.h"
00020
00021 #define VALLISTVIEWITEMRTTI 130977
00022
00023
00024 class ValListViewItem: public QListViewItem
00025 {
00026 public:
00027 ValListViewItem( QListView* parent, int key, int pid, const QString& message ):
00028 QListViewItem( parent, QString::number( key ), QString::number( pid ), message ),
00029 _key( key ), _pid ( pid ), backtrace( false ), _line( -1 ), _active( false ) {}
00030
00031 ValListViewItem( ValListViewItem* parent, int key, int pid, const QString& message, const QString& filename, int line, bool active ):
00032 QListViewItem( parent, QString::number( key ), QString::null, message ),
00033 _key( key ), _pid( pid ), backtrace( true ), _filename( filename ), _line( line ), _active( active )
00034 {
00035 if ( parent->_pid != _pid && _pid > 0 )
00036 setText( 1, QString::number( _pid ) );
00037 }
00038
00039 virtual ~ValListViewItem();
00040
00041 static int intCompare( int i1, int i2 )
00042 {
00043 if ( i1 > i2 )
00044 return 1;
00045 else if ( i1 < i2 )
00046 return -1;
00047 else
00048 return 0;
00049 }
00050
00051 int compare( QListViewItem* i, int col, bool ascending ) const
00052 {
00053 if ( !i || i->rtti() != VALLISTVIEWITEMRTTI )
00054 return QListViewItem::compare( i, col, ascending );
00055 switch ( col ) {
00056 case 0 : return intCompare( ((ValListViewItem*)i)->_key, _key );
00057 case 1 : return intCompare( ((ValListViewItem*)i)->_pid, _pid );
00058 default: return QListViewItem::compare( i, col, ascending );
00059 }
00060 }
00061
00062 void paintCell( QPainter* p, const QColorGroup& cg, int column, int width, int align )
00063 {
00064 if ( _active ) {
00065 QFont fnt = p->font();
00066 fnt.setBold( true );
00067 p->setFont( fnt );
00068 }
00069 QListViewItem::paintCell( p, cg, column, width, align );
00070 }
00071
00072 int rtti() const { return VALLISTVIEWITEMRTTI; }
00073
00074 QString fileName() const { return _filename; }
00075 int line() const { return _line; }
00076 QString message() const { return text( 2 ); }
00077 bool isHighlighted() const { return _active; }
00078
00079 private:
00080 int _key;
00081 int _pid;
00082 bool backtrace;
00083 QString _filename;
00084 int _line;
00085 bool _active;
00086 };
00087
00088 ValListViewItem::~ValListViewItem() {}
00089
00090 ValgrindWidget::ValgrindWidget( ValgrindPart *part )
00091 : QWidget(0, "valgrind widget"), _part( part )
00092 {
00093 QVBoxLayout* vbl = new QVBoxLayout( this );
00094 lv = new KListView( this );
00095 lv->addColumn( i18n( "No." ) );
00096 lv->addColumn( i18n( "Thread" ) );
00097 lv->addColumn( i18n( "Message" ) );
00098 lv->setSorting( 0, false );
00099 lv->setRootIsDecorated( true );
00100 lv->setAllColumnsShowFocus( true );
00101 vbl->addWidget( lv );
00102
00103 popup = new QPopupMenu( lv, "valPopup" );
00104 popup->insertItem( i18n( "&Open Valgrind Output..." ), _part, SLOT(loadOutput()), 0, 0 );
00105 popup->insertSeparator();
00106 popup->insertItem( i18n( "Expand All Items" ), this, SLOT(expandAll()), 0, 2 );
00107 popup->insertItem( i18n( "Collapse All Items" ), this, SLOT(collapseAll()), 0, 3 );
00108
00109 connect( popup, SIGNAL(aboutToShow()),
00110 this, SLOT(aboutToShowPopup()) );
00111 connect( lv, SIGNAL(executed(QListViewItem*)),
00112 this, SLOT(executed(QListViewItem*)) );
00113 connect( lv, SIGNAL(contextMenu(KListView*, QListViewItem*, const QPoint&)),
00114 this, SLOT(slotContextMenu(KListView*, QListViewItem*, const QPoint&)) );
00115 }
00116
00117
00118 ValgrindWidget::~ValgrindWidget()
00119 {
00120 }
00121
00122 void ValgrindWidget::clear()
00123 {
00124 lv->clear();
00125 msgNumber = 1;
00126 }
00127
00128 void ValgrindWidget::addMessage( const ValgrindItem& vi )
00129 {
00130 QStringList projectFiles;
00131 QString projectDirectory;
00132
00133 ValListViewItem* lvi = new ValListViewItem( lv, msgNumber++, vi.pid(), vi.message() );
00134 lvi->setMultiLinesEnabled( true );
00135 const ValgrindItem::BacktraceList backtrace = vi.backtrace();
00136 if ( !backtrace.isEmpty() )
00137 lvi->setExpandable( true );
00138
00139 int i = 0;
00140 for ( ValgrindItem::BacktraceList::ConstIterator it = backtrace.begin(); it != backtrace.end(); ++it ) {
00141 new ValListViewItem( lvi, ++i, (*it).pid(), (*it).message(), (*it).url(), (*it).line(), (*it).isHighlighted() );
00142 }
00143 }
00144
00145 void ValgrindWidget::executed( QListViewItem* lvi )
00146 {
00147 Q_ASSERT( _part );
00148 Q_ASSERT( _part->partController() );
00149 Q_ASSERT( _part->mainWindow() );
00150
00151 if ( !lvi || lvi->rtti() != VALLISTVIEWITEMRTTI )
00152 return;
00153 ValListViewItem* vli = 0;
00154 if ( !((ValListViewItem*)lvi)->fileName().isEmpty() ) {
00155 vli = (ValListViewItem*)lvi;
00156 } else if ( lvi->isExpandable() ) {
00157
00158 QListViewItemIterator it( lv );
00159 while ( vli == 0 && it.current() ) {
00160 if ( it.current()->rtti() == VALLISTVIEWITEMRTTI && ((ValListViewItem*)it.current())->isHighlighted() )
00161 vli = (ValListViewItem*)it.current();
00162 ++it;
00163 }
00164 }
00165 if ( vli ) {
00166
00167 _part->partController()->editDocument( KURL( vli->fileName() ), vli->line() - 1 );
00168 _part->mainWindow()->statusBar()->message( vli->message(), 10000 );
00169 _part->mainWindow()->lowerView( this );
00170 }
00171 }
00172
00173 void ValgrindWidget::expandAll()
00174 {
00175 QListViewItem* child = lv->firstChild();
00176 while ( child ) {
00177 child->setOpen( true );
00178 child = child->nextSibling();
00179 }
00180 }
00181
00182 void ValgrindWidget::collapseAll()
00183 {
00184 QListViewItem* child = lv->firstChild();
00185 while ( child ) {
00186 child->setOpen( false );
00187 child = child->nextSibling();
00188 }
00189 }
00190
00191 void ValgrindWidget::aboutToShowPopup()
00192 {
00193 bool en = (lv->firstChild() != 0);
00194 popup->setItemEnabled( 2, en );
00195 popup->setItemEnabled( 3, en );
00196 }
00197
00198 void ValgrindWidget::slotContextMenu( KListView* l, QListViewItem* , const QPoint& p )
00199 {
00200 if ( l != lv )
00201 return;
00202
00203 popup->exec( p );
00204 }
00205
00206 #include "valgrind_widget.moc"