KDevelop API Documentation

parts/fileview/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 m_part->mainWindow()->lowerView( this ); 00191 } 00192 00194 00195 void FileTreeWidget::slotContextMenu( KListView *, QListViewItem* item, const QPoint &p ) 00196 { 00197 kdDebug(9017) << "FileTreeWidget::slotContextMenu(...)" << endl; 00198 00199 KPopupMenu popup( i18n("File Tree"), this ); 00200 00201 // If an item is selected, fill the file context with selected files' list 00202 if (item) 00203 { 00204 m_impl->fillPopupMenu( &popup, item ); 00205 FileContext context( m_impl->selectedPathUrls() ); 00206 m_part->core()->fillContextMenu( &popup, &context ); 00207 } 00208 00209 popup.exec( p ); 00210 } 00211 00213 00214 QString FileTreeWidget::projectDirectory() 00215 { 00216 return m_part->project()->projectDirectory(); 00217 } 00218 00220 00221 QStringList FileTreeWidget::projectFiles() 00222 { 00223 return m_projectFiles; 00224 } 00225 00227 00228 void FileTreeWidget::addProjectFiles( QStringList const & fileList, bool constructing ) 00229 { 00230 kdDebug(9017) << "files added to project: " << fileList.count() << endl; 00231 00232 QStringList::ConstIterator it; 00233 for ( it = fileList.begin(); it != fileList.end(); ++it ) 00234 { 00235 QString file = projectDirectory() + "/" + ( *it ); 00236 if ( !m_projectFiles.contains( file ) ) 00237 { 00238 m_projectFiles.append( file ); 00239 // kdDebug(9017) << "file added: " << file << endl; 00240 } 00241 00242 if ( !constructing ) 00243 { 00244 FileTreeViewItem* item = static_cast<FileTreeViewItem*>(firstChild()); 00245 if( item ) 00246 { 00247 item->setProjectFile( file, true ); 00248 } 00249 } 00250 } 00251 } 00252 00254 00255 void FileTreeWidget::removeProjectFiles( QStringList const & fileList ) 00256 { 00257 kdDebug(9017) << "files removed from project: " << fileList.count() << endl; 00258 00259 QStringList::ConstIterator it; 00260 for ( it = fileList.begin(); it != fileList.end(); ++it ) 00261 { 00262 QString file = m_part->project()->projectDirectory() + "/" + ( *it ); 00263 m_projectFiles.remove( file ); 00264 kdDebug(9017) << "file removed: " << file << endl; 00265 00266 FileTreeViewItem* item = static_cast<FileTreeViewItem*>(firstChild()); 00267 if( item ) 00268 { 00269 item->setProjectFile( file, false ); 00270 } 00271 } 00272 } 00273 00275 00276 void FileTreeWidget::applyHidePatterns( const QString &hidePatterns ) 00277 { 00278 m_hidePatterns = QStringList::split( ",", hidePatterns ); 00279 hideOrShow(); 00280 } 00281 00283 00284 QString FileTreeWidget::hidePatterns() const 00285 { 00286 return m_hidePatterns.join( "," ); 00287 } 00288 00290 00291 KDevVersionControl *FileTreeWidget::versionControl() const 00292 { 00293 if (part() && part()->versionControl()) 00294 return part()->versionControl(); 00295 else 00296 return 0; 00297 } 00298 00300 00301 bool FileTreeWidget::showNonProjectFiles() const 00302 { 00303 return m_impl->showNonProjectFiles(); 00304 } 00305 00307 00308 void FileTreeWidget::slotImplementationInvalidated() 00309 { 00310 kdDebug(9017) << "FileTreeWidget::slotImplementationInvalidated()" << endl; 00311 00312 // Destroy old implementation, create the simpler default impl. and 00313 // reload list view 00314 // remove old branch 00315 removeBranch( m_rootBranch ); 00316 m_rootBranch = 0; // avoid openDirectory() trying to release the branch 00317 00318 // Restore a clean situation for an eventual new & different implementation 00322 for (int i=columns()-1; i>=0; --i) 00323 { 00324 kdDebug(9017) << "Removing column: " << i << endl; 00325 removeColumn( i ); 00326 } 00327 00328 delete m_impl; 00329 m_impl = new StdFileTreeWidgetImpl( this ); 00330 openDirectory( projectDirectory() ); 00331 } 00332 00333 #include "filetreewidget.moc"
KDE Logo
This file is part of the documentation for KDevelop Version 3.0.4.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Oct 19 08:01:51 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003