KDevelop API Documentation

filelist_widget.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2004 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 <qheader.h>
00013 #include <qpixmap.h>
00014 
00015 #include <kparts/part.h>
00016 #include <klibloader.h>
00017 #include <kurl.h>
00018 #include <kdebug.h>
00019 //#include <kiconloader.h>
00020 #include <klocale.h>
00021 #include <kpopupmenu.h>
00022 
00023 #include <kdevcore.h>
00024 #include <kdevpartcontroller.h>
00025 
00026 #include "filelist_part.h"
00027 #include "filelist_widget.h"
00028 #include "filelist_item.h"
00029 
00030 
00031 FileListWidget::FileListWidget(FileListPart *part)
00032  : KListView(0, "filelist widget"), QToolTip( viewport() ), _part( part )
00033 {
00034     addColumn( QString::null );
00035     header()->hide();
00036     setRootIsDecorated( false );
00037     setResizeMode( QListView::LastColumn );
00038 
00039     setSelectionMode( QListView::Extended );
00040 
00041     connect( _part->partController(), SIGNAL( partAdded(KParts::Part*) ), this, SLOT(partAdded(KParts::Part*)) );
00042     connect( _part->partController(), SIGNAL( partRemoved(KParts::Part*) ), this, SLOT(partRemoved()) );
00043 //  connect( _part->partController(), SIGNAL( partAdded(KParts::Part*) ), this, SLOT(refreshFileList()) );
00044 //  connect( _part->partController(), SIGNAL( partRemoved(KParts::Part*) ), this, SLOT(refreshFileList()) );
00045     connect( _part->partController(), SIGNAL( activePartChanged(KParts::Part*) ), this, SLOT( activePartChanged(KParts::Part* )) );
00046 
00047     connect( this, SIGNAL( executed( QListViewItem * ) ), this, SLOT( itemClicked( QListViewItem * ) ) );
00048     connect( this, SIGNAL( returnPressed( QListViewItem * ) ), this, SLOT( itemClicked( QListViewItem * ) ) );
00049 
00050     connect( this, SIGNAL( contextMenuRequested ( QListViewItem *, const QPoint & , int ) ),
00051         this, SLOT( popupMenu(QListViewItem *, const QPoint & , int ) ) );
00052 
00053     connect( _part->partController(), SIGNAL(documentChangedState(const KURL &, DocumentState)),
00054         this, SLOT(documentChangedState(const KURL&, DocumentState )) );
00055 
00056     connect( _part->partController(), SIGNAL(partURLChanged(KParts::ReadOnlyPart * )), this, SLOT(refreshFileList()) );
00057 
00058     refreshFileList();
00059 }
00060 
00061 
00062 FileListWidget::~FileListWidget()
00063 {}
00064 
00065 void FileListWidget::maybeTip( QPoint const & p )
00066 {
00067     FileListItem * item = static_cast<FileListItem*>( itemAt( p ) );
00068     QRect r = itemRect( item );
00069 
00070     if ( item && r.isValid() )
00071     {
00072         const QPixmap * pixmap = item->pixmap(0);
00073         if ( pixmap && ( p.x() <= pixmap->width() ) )
00074         {
00075             QString message;
00076             switch( item->state() )
00077             {
00078                 case Modified:
00079                     message = i18n("This file has unsaved changes.");
00080                     break;
00081                 case Dirty:
00082                     message = i18n("This file has changed on disk since it was last saved.");
00083                     break;
00084                 case DirtyAndModified:
00085                     message = i18n("Conflict: this file has changed on disk and has unsaved changes.");
00086                     break;
00087                 default:
00088                     message = item->url().prettyURL();
00089             }
00090 
00091             tip( r, message );
00092         }
00093         else
00094         {
00095             tip( r, item->url().prettyURL() );
00096         }
00097     }
00098 }
00099 
00100 FileListItem * FileListWidget::itemForURL( KURL const & url )
00101 {
00102     FileListItem * item = static_cast<FileListItem*>( firstChild() );
00103     while ( item )
00104     {
00105         if ( item->url() == url )
00106         {
00107             return item;
00108         }
00109         item = static_cast<FileListItem*>( item->nextSibling() );
00110     }
00111     return 0L;
00112 }
00113 
00114 void FileListWidget::refreshFileList( )
00115 {
00116     kdDebug() << k_funcinfo << endl;
00117 
00118     KListView::clear();
00119 
00120     KURL::List list( _part->openFiles() );
00121     QValueListIterator<KURL> it = list.begin();
00122     while ( it != list.end() )
00123     {
00124         FileListItem * item = new FileListItem( this, *it );
00125         item->setState( _part->partController()->documentState( *it ) );
00126         ++it;
00127     }
00128 
00129     activePartChanged( _part->partController()->activePart() );
00130 }
00131 
00132 
00133 void FileListWidget::partAdded( KParts::Part * part )
00134 {
00135     KParts::ReadOnlyPart * ro_part = dynamic_cast<KParts::ReadOnlyPart*>( part );
00136     if ( ro_part )
00137     {
00138         new FileListItem( this, ro_part->url() );
00139     }
00140 
00141     activePartChanged( _part->partController()->activePart() );
00142 }
00143 
00144 void FileListWidget::partRemoved()
00145 {
00146     FileListItem * item = static_cast<FileListItem*>( firstChild() );
00147     while ( item )
00148     {
00149         if ( ! _part->partController()->partForURL( item->url() ) )
00150         {
00151             delete item;
00152             break;
00153         }
00154         item = static_cast<FileListItem*>( item->nextSibling() );
00155     }
00156 
00157     activePartChanged( _part->partController()->activePart() );
00158 }
00159 
00160 
00161 void FileListWidget::itemClicked( QListViewItem * item )
00162 {
00163     if ( !item ) return;
00164 
00165     FileListItem * listItem = static_cast<FileListItem*>( item );
00166     _part->partController()->activatePart( _part->partController()->partForURL( listItem->url() ) );
00167 }
00168 
00169 void FileListWidget::activePartChanged( KParts::Part * part )
00170 {
00171     KParts::ReadOnlyPart * ro_part = dynamic_cast<KParts::ReadOnlyPart*>( part );
00172     if ( ro_part )
00173     {
00174         FileListItem * item = static_cast<FileListItem*>( firstChild() );
00175         while ( item )
00176         {
00177             if ( item->url() == ro_part->url() )
00178             {
00179                 FileListItem::setActive( item );
00180                 break;
00181             }
00182             item = static_cast<FileListItem*>( item->nextSibling() );
00183         }
00184     }
00185     repaintContents();
00186 }
00187 
00188 void FileListWidget::documentChangedState( const KURL & url, DocumentState state )
00189 {
00190     FileListItem * item = itemForURL( url );
00191     if ( item )
00192     {
00193         item->setState( state );
00194     }
00195 }
00196 
00197 void FileListWidget::popupMenu( QListViewItem * item, const QPoint & p, int )
00198 {
00199     if ( item )
00200     {
00201         KPopupMenu popup;
00202         popup.insertTitle( i18n("File List") );
00203         popup.insertItem( i18n("Close Selected"), this, SLOT(closeSelectedFiles()) );
00204         popup.insertItem( i18n("Save Selected"), this, SLOT(saveSelectedFiles()) );
00205         popup.insertItem( i18n("Reload Selected"), this, SLOT(reloadSelectedFiles()) );
00206 
00207         FileContext context( getSelectedURLs() );
00208         _part->core()->fillContextMenu( &popup, &context );
00209 
00210         popup.exec(p);
00211     }
00212 }
00213 
00214 KURL::List FileListWidget::getSelectedURLs( )
00215 {
00216     KURL::List list;
00217     FileListItem * item = static_cast<FileListItem*>( firstChild() );
00218     while ( item )
00219     {
00220         if ( item->isSelected() )
00221         {
00222             list << item->url();
00223         }
00224         item = static_cast<FileListItem*>( item->nextSibling() );
00225     }
00226     return list;
00227 }
00228 
00229 void FileListWidget::closeSelectedFiles( )
00230 {
00231     _part->partController()->closeFiles( getSelectedURLs() );
00232 }
00233 
00234 void FileListWidget::saveSelectedFiles( )
00235 {
00236     _part->partController()->saveFiles( getSelectedURLs() );
00237 }
00238 
00239 void FileListWidget::reloadSelectedFiles( )
00240 {
00241     _part->partController()->revertFiles( getSelectedURLs() );
00242 }
00243 
00244 #include "filelist_widget.moc"
00245 
00246 // 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.1.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Feb 22 09:22:40 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003