KDevelop API Documentation

filetreewidget.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2001-2002 by Bernd Gehrmann                             *
00003  *   bernd@kdevelop.org                                                    *
00004  *   Copyright (C) 2003 by Mario Scalas (VCS Support)                      *
00005  *   mario.scalas@libero.it                                                *
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  ***************************************************************************/
00013 
00014 #include "filetreewidget.h"
00015 
00016 #include <qheader.h>
00017 #include <qpainter.h>
00018 #include <qregexp.h>
00019 #include <qstringlist.h>
00020 #include <qcolor.h>
00021 
00022 #include <kdebug.h>
00023 #include <klocale.h>
00024 #include <kpopupmenu.h>
00025 #include <kfileitem.h>
00026 #include <kurl.h>
00027 #include <kaction.h>
00028 
00029 #include "kdevcore.h"
00030 #include "kdevproject.h"
00031 #include "kdevpartcontroller.h"
00032 #include "kdevmainwindow.h"
00033 #include "kdevversioncontrol.h"
00034 #include "domutil.h"
00035 #include "urlutil.h"
00036 
00037 #include "fileviewpart.h"
00038 #include "fileitemfactory.h"
00039 #include "vcsfiletreewidgetimpl.h"
00040 #include "stdfiletreewidgetimpl.h"
00041 
00042 using namespace filetreeview;
00043 
00045 // class FileTreeViewItem
00047 
00048 #include <kdeversion.h>
00049 #if (KDE_VERSION_MINOR==0) && (KDE_VERSION_MAJOR==3)
00050 #include <kdevkurl.h>
00051 #define KURL KdevKURL
00052 #endif
00053 
00055 // class FileTreeWidget
00057 
00058 FileTreeWidget::FileTreeWidget( FileViewPart *part, QWidget *parent, KDevVCSFileInfoProvider *infoProvider )
00059     : KFileTreeView( parent, "filetreewidget" ), m_part( part ), m_rootBranch( 0 )
00060 {
00061     kdDebug(9017) << "Requested FileTree for: " << projectDirectory() << endl;
00062     if (versionControl() && infoProvider)
00063         kdDebug(9017) << "Valid VCS directory: " << versionControl()->isValidDirectory( projectDirectory() ) << endl;
00064 
00065     if (infoProvider && versionControl() && versionControl()->isValidDirectory( projectDirectory() ))
00066         m_impl = new VCSFileTreeWidgetImpl( this, infoProvider );
00067     else
00068         m_impl = new StdFileTreeWidgetImpl( this );
00069 
00070     //setResizeMode( QListView::LastColumn );
00071     setSorting( 0 );
00072     setAllColumnsShowFocus( true );
00073     setSelectionMode( QListView::Extended ); // Enable multiple items selection by use of Ctrl/Shift
00074     setDragEnabled( false );
00075 
00076     // Slot connections
00077     connect( this, SIGNAL(executed(QListViewItem*)), this, SLOT(slotItemExecuted(QListViewItem*)) );
00078     connect( this, SIGNAL(returnPressed(QListViewItem*)), this, SLOT(slotItemExecuted(QListViewItem*)) );
00079     connect( this, SIGNAL(contextMenu(KListView*, QListViewItem*, const QPoint&)),
00080              this, SLOT(slotContextMenu(KListView*, QListViewItem*, const QPoint&)) );
00081     // Intercepts KDevelop core signals and VCS notifications (if available)
00082     connect( m_part->project(), SIGNAL( addedFilesToProject( const QStringList & ) ),
00083              this, SLOT( addProjectFiles( const QStringList & ) ) );
00084     connect( m_part->project(), SIGNAL( removedFilesFromProject( const QStringList & ) ),
00085              this, SLOT( removeProjectFiles( const QStringList & ) ) );
00086     // Safeguard against VCS plug-in unloading at run-time
00087     connect( m_impl, SIGNAL(implementationInvalidated()), this, SLOT(slotImplementationInvalidated()) );
00088 
00089     // Hide pattern for files
00090     QDomDocument &dom = *m_part->projectDom();
00091     QString defaultHidePattern = "*.o,*.lo,CVS";
00092     QString hidePattern = DomUtil::readEntry( dom, "/kdevfileview/tree/hidepatterns", defaultHidePattern );
00093     m_hidePatterns = QStringList::split( ",", hidePattern );
00094 }
00095 
00097 
00098 FileTreeWidget::~FileTreeWidget()
00099 {
00100     kdDebug(9017) << "FileTreeWidget::~FileTreeWidget()" << endl;
00101 
00102     QDomDocument &dom = *m_part->projectDom();
00103     DomUtil::writeEntry( dom, "/kdevfileview/tree/hidepatterns", hidePatterns() );
00104 
00105 //    delete m_impl;
00106 }
00107 
00109 
00110 void FileTreeWidget::openDirectory( const QString& dirName )
00111 {
00112     kdDebug(9017) << "FileTreeWidget::openDirectory(): " + dirName << endl;
00113 
00114     // if we're reloading
00115     if (m_rootBranch)
00116     {
00117         removeBranch( m_rootBranch );
00118         m_projectFiles.clear();
00119     }
00120 
00121     addProjectFiles( m_part->project()->allFiles(), true );
00122 
00123     KURL url = KURL::fromPathOrURL( dirName );
00124     const QPixmap& pix = KMimeType::mimeType("inode/directory")->pixmap( KIcon::Small );
00125 
00126     // this is a bit odd, but the order of these calls seems to be important
00127     //FileTreeBranch *b = new FileTreeBranch( this, url, url.prettyURL(), pix );
00128     FileTreeBranchItem *b = m_impl->branchItemFactory()->makeBranchItem( this, url, url.prettyURL(), pix );
00129     b->setChildRecurse( false );
00130     m_rootBranch = addBranch( b );
00131     m_rootBranch->setOpen( true );
00132 }
00133 
00135 
00136 bool FileTreeWidget::shouldBeShown( KFileTreeViewItem* item )
00137 {
00138     FileTreeViewItem * i = static_cast<FileTreeViewItem *>( item );
00139     return( (m_impl->showNonProjectFiles() || i->isDir() || i->isProjectFile() )
00140              && !matchesHidePattern( i->url().fileName() ) );
00141 }
00142 
00144 
00145 bool FileTreeWidget::matchesHidePattern(const QString &fileName)
00146 {
00147     QStringList::ConstIterator it;
00148     for (it = m_hidePatterns.begin(); it != m_hidePatterns.end(); ++it) {
00149         QRegExp re(*it, true, true);
00150         if (re.search(fileName) == 0 && (uint)re.matchedLength() == fileName.length())
00151             return true;
00152     }
00153 
00154     return false;
00155 }
00156 
00158 
00159 void FileTreeWidget::hideOrShow()
00160 {
00161     // This is called the first time the tree branch is expanded
00162     FileTreeViewItem* item = static_cast<FileTreeViewItem*>(firstChild());
00163     if( !item )
00164       return;
00165 
00166     // Need to skip the root item (which is the sub-directory)
00167     // i.e. "/home/devmario/src/kdevelop/parts/cvsservice"
00168     item = static_cast<FileTreeViewItem*>( item->firstChild() );
00169     // Now fill the sub-tree
00170     while (item)
00171     {
00172         item->hideOrShow();
00173         item = static_cast<FileTreeViewItem*>(item->nextSibling());
00174     }
00175 }
00176 
00178 
00179 void FileTreeWidget::slotItemExecuted( QListViewItem* item )
00180 {
00181     if (!item)
00182         return;
00183 
00184     KFileTreeViewItem* ftitem = static_cast<KFileTreeViewItem*>(item);
00185 
00186     if (ftitem->isDir())
00187         return;
00188 
00189     m_part->partController()->editDocument( ftitem->url() );
00190 }
00191 
00193 
00194 void FileTreeWidget::slotContextMenu( KListView *, QListViewItem* item, const QPoint &p )
00195 {
00196     kdDebug(9017) << "FileTreeWidget::slotContextMenu(...)" << endl;
00197 
00198     KPopupMenu popup( i18n("File Tree"), this );
00199 
00200     // If an item is selected, fill the file context with selected files' list
00201     if (item)
00202     {
00203         m_impl->fillPopupMenu( &popup, item );
00204         FileContext context( m_impl->selectedPathUrls() );
00205         m_part->core()->fillContextMenu( &popup, &context );
00206     }
00207 
00208     popup.exec( p );
00209 }
00210 
00212 
00213 QString FileTreeWidget::projectDirectory()
00214 {
00215     return m_part->project()->projectDirectory();
00216 }
00217 
00219 
00220 QStringList FileTreeWidget::projectFiles()
00221 {
00222     return m_projectFiles;
00223 }
00224 
00226 
00227 void FileTreeWidget::addProjectFiles( QStringList const & fileList, bool constructing )
00228 {
00229     kdDebug(9017) << "files added to project: " << fileList.count() << endl;
00230 
00231     QStringList::ConstIterator it;
00232     for ( it = fileList.begin(); it != fileList.end(); ++it )
00233     {
00234         QString file = projectDirectory() + "/" + ( *it );
00235         if ( !m_projectFiles.contains( file ) )
00236         {
00237             m_projectFiles.append( file );
00238 //            kdDebug(9017) << "file added: " << file << endl;
00239         }
00240 
00241         if ( !constructing )
00242         {
00243             FileTreeViewItem* item = static_cast<FileTreeViewItem*>(firstChild());
00244             if( item )
00245             {
00246                 item->setProjectFile( file, true );
00247             }
00248         }
00249     }
00250 }
00251 
00253 
00254 void FileTreeWidget::removeProjectFiles( QStringList const & fileList )
00255 {
00256     kdDebug(9017) << "files removed from project: " << fileList.count() << endl;
00257 
00258     QStringList::ConstIterator it;
00259     for ( it = fileList.begin(); it != fileList.end(); ++it )
00260     {
00261         QString file = m_part->project()->projectDirectory() + "/" + ( *it );
00262         m_projectFiles.remove( file );
00263         kdDebug(9017) << "file removed: " << file << endl;
00264 
00265         FileTreeViewItem* item = static_cast<FileTreeViewItem*>(firstChild());
00266         if( item )
00267         {
00268             item->setProjectFile( file, false );
00269         }
00270     }
00271 }
00272 
00274 
00275 void FileTreeWidget::applyHidePatterns( const QString &hidePatterns )
00276 {
00277     m_hidePatterns = QStringList::split( ",", hidePatterns );
00278     hideOrShow();
00279 }
00280 
00282 
00283 QString FileTreeWidget::hidePatterns() const
00284 {
00285     return m_hidePatterns.join( "," );
00286 }
00287 
00289 
00290 KDevVersionControl *FileTreeWidget::versionControl() const
00291 {
00292     if (part() && part()->versionControl())
00293         return part()->versionControl();
00294     else
00295         return 0;
00296 }
00297 
00299 
00300 bool FileTreeWidget::showNonProjectFiles() const
00301 {
00302     return m_impl->showNonProjectFiles();
00303 }
00304 
00306 
00307 void FileTreeWidget::slotImplementationInvalidated()
00308 {
00309     kdDebug(9017) << "FileTreeWidget::slotImplementationInvalidated()" << endl;
00310 
00311     // Destroy old implementation, create the simpler default impl. and
00312     // reload list view
00313     // remove old branch
00314     removeBranch( m_rootBranch );
00315     m_rootBranch = 0; // avoid openDirectory() trying to release the branch
00316 
00317     // Restore a clean situation for an eventual new & different implementation
00321     for (int i=columns()-1; i>=0; --i)
00322     {
00323         kdDebug(9017) << "Removing column: " << i << endl;
00324         removeColumn( i );
00325     }
00326 
00327     delete m_impl;
00328     m_impl = new StdFileTreeWidgetImpl( this );
00329     openDirectory( projectDirectory() );
00330 }
00331 
00332 #include "filetreewidget.moc"
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:41 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003