KDevelop API Documentation

parts/bookmarks/bookmarks_widget.cpp

Go to the documentation of this file.
00001 /*************************************************************************** 00002 * Copyright (C) 2003 by Jens Dagerbo * 00003 * jens.dagerbo@swipnet.se * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU General Public License as published by * 00007 * the Free Software Foundation; either version 2 of the License, or * 00008 * (at your option) any later version. * 00009 * * 00010 ***************************************************************************/ 00011 00012 #include <qdict.h> 00013 #include <qheader.h> 00014 #include <qtooltip.h> 00015 #include <qpair.h> 00016 #include <qstylesheet.h> 00017 00018 #include <kparts/part.h> 00019 #include <klibloader.h> 00020 #include <kurl.h> 00021 #include <kdebug.h> 00022 #include <kiconloader.h> 00023 #include <kpopupmenu.h> 00024 #include <klocale.h> 00025 00026 #include <kdevpartcontroller.h> 00027 #include <kdevcore.h> 00028 00029 00030 #include "bookmarks_part.h" 00031 #include "bookmarks_widget.h" 00032 #include "bookmarks_config.h" 00033 00034 namespace 00035 { 00036 00037 // shamelessly lifted from kdelibs/kate/part/kateviewhelpers.cpp 00038 static const char* const bookmark_xpm[]={ 00039 "12 12 4 1", 00040 "b c #808080", 00041 "a c #000080", 00042 "# c #0000ff", 00043 ". c None", 00044 "........###.", 00045 ".......#...a", 00046 "......#.##.a", 00047 ".....#.#..aa", 00048 "....#.#...a.", 00049 "...#.#.a.a..", 00050 "..#.#.a.a...", 00051 ".#.#.a.a....", 00052 "#.#.a.a.....", 00053 "#.#a.a......", 00054 "#...a.......", 00055 ".aaa........"}; 00056 00057 } 00058 00059 class BookmarkItem : public QListViewItem 00060 { 00061 public: 00062 BookmarkItem( QListView * parent, KURL const & url ) 00063 : QListViewItem( parent, url.fileName() ), 00064 _url( url ), _line( -1 ), _isBookmark( false ) 00065 {} 00066 00067 BookmarkItem( QListViewItem * parent, KURL const & url, QPair<int,QString> mark ) 00068 : QListViewItem( parent, QString::number( mark.first +1 ).rightJustify( 5 ) ), 00069 _url( url ), _line( mark.first ), _isBookmark( true ) 00070 { 00071 BookmarksWidget * lv = static_cast<BookmarksWidget*>( listView() ); 00072 BookmarksConfig::CodeLineType codeline = lv->config()->codeline(); 00073 00074 if ( codeline == BookmarksConfig::Never ) 00075 { 00076 return; 00077 } 00078 00079 if ( codeline == BookmarksConfig::Token ) 00080 { 00081 if ( mark.second.startsWith( lv->config()->token() ) ) 00082 { 00083 setText( 0, text( 0 ) + " " + mark.second ); 00084 } 00085 return; 00086 } 00087 00088 setText( 0, text( 0 ) + " " + mark.second ); 00089 } 00090 00091 KURL url() 00092 { 00093 return _url; 00094 } 00095 int line() 00096 { 00097 return _line; 00098 } 00099 00100 QString tipText() 00101 { 00102 if ( _isBookmark ) 00103 { 00104 BookmarksWidget * w = static_cast<BookmarksWidget*> ( listView() ); 00105 QStringList list = w->getContext( _url, _line ); 00106 00107 QString code = "<qt><table><tr><td><pre>"; 00108 for ( uint i = 0; i < list.count(); i++) 00109 { 00110 QString temp = QStyleSheet::escape( list[i] ); 00111 00112 if ( i == (list.count() / 2) ) // count() is always odd 00113 { 00114 temp = "<b>" + temp + "</b>"; 00115 } 00116 code += temp + "\n"; 00117 } 00118 code += "</pre></td></tr></table></qt>"; 00119 00120 return code; 00121 } 00122 else 00123 { 00124 return _url.path(); 00125 } 00126 } 00127 00128 bool isBookmark() 00129 { 00130 return _isBookmark; 00131 } 00132 00133 private: 00134 KURL _url; 00135 int _line; 00136 bool _isBookmark; 00137 QString _code; 00138 00139 }; 00140 00141 BookmarksWidget::BookmarksWidget(BookmarksPart *part) 00142 : KListView(0, "bookmarks widget"), QToolTip( viewport() ), 00143 _part( part ) 00144 00145 { 00146 addColumn( QString::null ); 00147 header()->hide(); 00148 setRootIsDecorated( true ); 00149 setResizeMode( QListView::LastColumn ); 00150 00151 connect( this, SIGNAL( executed( QListViewItem * ) ), this, SLOT( itemClicked( QListViewItem * ) ) ); 00152 connect( this, SIGNAL( returnPressed( QListViewItem * ) ), this, SLOT( itemClicked( QListViewItem * ) ) ); 00153 connect( this, SIGNAL( contextMenuRequested ( QListViewItem *, const QPoint & , int ) ), 00154 this, SLOT( popupMenu(QListViewItem *, const QPoint & , int ) ) ); 00155 } 00156 00157 00158 BookmarksWidget::~BookmarksWidget() 00159 {} 00160 00161 void BookmarksWidget::maybeTip(const QPoint &p) 00162 { 00163 // kdDebug(0) << "ToolTip::maybeTip()" << endl; 00164 00165 if ( ! _part->config()->toolTip() ) return; 00166 00167 BookmarkItem * item = dynamic_cast<BookmarkItem*>( itemAt( p ) ); 00168 QRect r = itemRect( item ); 00169 00170 if ( item && r.isValid() ) 00171 { 00172 tip( r, item->tipText() ); 00173 } 00174 } 00175 00176 void BookmarksWidget::update( QDict<EditorData> const & map ) 00177 { 00178 // kdDebug(0) << "BookmarksWidget::update()" << endl; 00179 00180 QListView::clear(); 00181 00182 QDictIterator<EditorData> it( map ); 00183 while ( it.current() ) 00184 { 00185 if ( ! it.current()->marks.isEmpty() ) 00186 { 00187 createURL( it.current() ); 00188 } 00189 ++it; 00190 } 00191 } 00192 00193 void BookmarksWidget::updateURL( EditorData * data ) 00194 { 00195 // kdDebug(0) << "BookmarksWidget::updateURL()" << endl; 00196 00197 // remove the node that contains 'data' 00198 removeURL( data->url ); 00199 00200 // create it again with new data 00201 createURL( data ); 00202 } 00203 00204 void BookmarksWidget::createURL( EditorData * data ) 00205 { 00206 // kdDebug(0) << "BookmarksWidget::createURL()" << endl; 00207 00208 if ( data ) 00209 { 00210 QListViewItem * file = new BookmarkItem( this, data->url ); 00211 file->setOpen( true ); 00212 file->setPixmap( 0, SmallIcon( "document" ) ); 00213 00214 QValueListIterator< QPair<int,QString> > it = data->marks.begin(); 00215 while ( it != data->marks.end() ) 00216 { 00217 QListViewItem * item = new BookmarkItem( file, data->url, *it ); 00218 item->setPixmap( 0, QPixmap((const char**)bookmark_xpm) ); 00219 ++it; 00220 } 00221 } 00222 } 00223 00224 bool BookmarksWidget::removeURL( KURL const & url ) 00225 { 00226 // kdDebug(0) << "BookmarksWidget::removeURL()" << endl; 00227 00228 QListViewItem * item = firstChild(); 00229 while ( item ) 00230 { 00231 BookmarkItem * bm = static_cast<BookmarkItem*>(item); 00232 if ( bm->url() == url ) 00233 { 00234 delete item; 00235 return true; 00236 } 00237 item = item->nextSibling(); 00238 } 00239 return false; 00240 } 00241 00242 void BookmarksWidget::doEmitRemoveBookMark() 00243 { 00244 // kdDebug(0) << "BookmarksWidget::doEmitRemoveBookMark()" << endl; 00245 00246 if ( _selectedItem->isBookmark() ) 00247 { 00248 emit removeBookmarkForURL( _selectedItem->url(), _selectedItem->line() ); 00249 } 00250 else 00251 { 00252 emit removeAllBookmarksForURL( _selectedItem->url() ); 00253 } 00254 } 00255 00256 void BookmarksWidget::popupMenu( QListViewItem * item, const QPoint & p, int ) 00257 { 00258 // kdDebug(0) << "BookmarksWidget::contextMenuRequested()" << endl; 00259 00260 if ( item ) 00261 { 00262 _selectedItem = static_cast<BookmarkItem *>(item); 00263 00264 KPopupMenu popup; 00265 00266 if ( _selectedItem->isBookmark() ) 00267 { 00268 popup.insertTitle( _selectedItem->url().fileName() + i18n(", line ") 00269 + QString::number( _selectedItem->line() +1 ) ); 00270 00271 popup.insertItem( i18n("Remove This Bookmark"), this, SLOT( doEmitRemoveBookMark() ) ); 00272 } 00273 else 00274 { 00275 popup.insertTitle( _selectedItem->url().fileName() + i18n( ", All" ) ); 00276 popup.insertItem( i18n("Remove These Bookmarks"), this, SLOT( doEmitRemoveBookMark() ) ); 00277 } 00278 popup.exec(p); 00279 } 00280 00281 } 00282 00283 void BookmarksWidget::itemClicked( QListViewItem * clickedItem ) 00284 { 00285 // kdDebug(0) << "BookmarksWidget::itemClicked()" << endl; 00286 00287 if ( ! clickedItem ) 00288 return; 00289 00290 BookmarkItem * item = static_cast<BookmarkItem*>( clickedItem ); 00291 _part->partController()->editDocument( item->url(), item->line() ); 00292 00293 } 00294 00295 BookmarksConfig * BookmarksWidget::config( ) 00296 { 00297 return _part->config(); 00298 } 00299 00300 QStringList BookmarksWidget::getContext( KURL const & url, unsigned int line ) 00301 { 00302 return _part->getContext( url, line, config()->context() ); 00303 } 00304 00305 00306 #include "bookmarks_widget.moc" 00307 00308 // kate: space-indent off; indent-width 4; tab-width 4; show-tabs off;
KDE Logo
This file is part of the documentation for KDevelop Version 3.0.4.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Oct 6 17:39:09 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003