kate Library API Documentation

kateprojecttreeview.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org>
00003    Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
00004    Copyright (C) 2001 Anders Lund <anders.lund@lund.tdcadsl.dk>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License version 2 as published by the Free Software Foundation.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.
00019 */
00020 
00021 //BEGIN Includes
00022 #include "kateprojecttreeview.h"
00023 #include "kateprojecttreeview.moc"
00024 
00025 #include "kateprojectdirview.h"
00026 #include "katemainwindow.h"
00027 
00028 #include <kapplication.h>
00029 #include <klocale.h>
00030 #include <kiconloader.h>
00031 #include <kmimetype.h>
00032 #include <klineedit.h>
00033 
00034 #include <kdebug.h>
00035 
00036 #include <qlabel.h>
00037 #include <qheader.h>
00038 #include <qpopupmenu.h>
00039 #include <qevent.h>
00040 //END
00041 
00042 //BEGIN KateProjectTreeViewItem
00043 KateProjectTreeViewItem::KateProjectTreeViewItem (QDict<KateProjectTreeViewItem> *dict, KateProjectTreeView * parent, Kate::Project *prj, const QString &name, const QString &fullname, bool dir)
00044  : KListViewItem (parent)
00045 {
00046   m_name = name;
00047   m_fullName = fullname;
00048   m_dir = dir;
00049   m_project = prj;
00050   m_dict = dict;
00051 
00052   init ();
00053 }
00054 
00055 KateProjectTreeViewItem::KateProjectTreeViewItem (QDict<KateProjectTreeViewItem> *dict, KateProjectTreeViewItem * parent, Kate::Project *prj, const QString &name, const QString &fullname, bool dir)
00056  : KListViewItem (parent)
00057 {
00058   m_name = name;
00059   m_fullName = fullname;
00060   m_dir = dir;
00061   m_project = prj;
00062   m_dict = dict;
00063 
00064   init ();
00065 }
00066 
00067 KateProjectTreeViewItem::~KateProjectTreeViewItem ()
00068 {
00069   if (m_dir)
00070     m_dict->remove(QString("/")+m_fullName);
00071 }
00072 
00073 void KateProjectTreeViewItem::init ()
00074 {
00075   if (m_dir)
00076     m_dict->insert(QString("/")+m_fullName, this);
00077 
00078   if (m_dir)
00079     setPixmap (0, KMimeType::mimeType("inode/directory")->pixmap( KIcon::Small ));
00080   else
00081     setPixmap (0, KMimeType::findByPath (m_project->dir() + QString ("/") + m_fullName)->pixmap (KIcon::Small, KIcon::SizeSmall));
00082 
00083   setText (0, m_name);
00084 }
00085 
00086 int KateProjectTreeViewItem::compare ( QListViewItem *i, int, bool ) const
00087 {
00088   KateProjectTreeViewItem *item = (KateProjectTreeViewItem *) i;
00089 
00090   if ((m_name == item->m_name) && (m_dir == item->m_dir))
00091     return 0;
00092 
00093   if (m_dir == 0)
00094   {
00095     if (item->m_dir == 1)
00096       return 1;
00097 
00098     if (m_name < item->m_name)
00099       return -1;
00100     else
00101       return 1;
00102   }
00103   else
00104   {
00105     if (item->m_dir == 0)
00106       return -1;
00107 
00108     if (m_name < item->m_name)
00109       return -1;
00110     else
00111       return 1;
00112   }
00113 }
00114 //END KateProjectTreeViewItem
00115 
00116 //BEGIN KateProjectTreeView
00117 KateProjectTreeView::KateProjectTreeView (Kate::Project *project, KateMainWindow *mainwin, QWidget *parent) : KListView (parent)
00118 {
00119   m_project = project;
00120   m_mainWin = mainwin;
00121 
00122   m_dirDict.setAutoDelete (false);
00123 
00124   setSelectionModeExt( KListView::Single );
00125   setRootIsDecorated (false);
00126   setAlternateBackground (viewport()->colorGroup().base());
00127 
00128   header()->setStretchEnabled (true);
00129   addColumn(i18n("Project: ") + m_project->name());
00130   header()->hide ();
00131 
00132   KateProjectTreeViewItem *item = new KateProjectTreeViewItem (&m_dirDict, this, m_project, i18n("Project Folder"), QString::null, true);
00133   addDir (item, QString::null);
00134 
00135   setOpen (item, true);
00136 
00137   connect(this,SIGNAL(executed(QListViewItem *, const QPoint &, int)),this,SLOT(slotDoubleClicked(QListViewItem *, const QPoint &, int)));
00138   connect( this, SIGNAL(returnPressed(QListViewItem*)), SLOT(execute(QListViewItem*)) );
00139   connect(this, SIGNAL( contextMenuRequested( QListViewItem *, const QPoint& , int ) ),
00140             this, SLOT( slotContextMenuRequested( QListViewItem *, const QPoint &, int ) ) );
00141 
00142   connect (m_project, SIGNAL (dirsAdded (const QString &, const QStringList &)), this, SLOT (dirsAdded (const QString &, const QStringList &)));
00143   connect (m_project, SIGNAL (filesAdded (const QString &, const QStringList &)), this, SLOT (filesAdded (const QString &, const QStringList &)));
00144   connect (m_project, SIGNAL (dirsRemoved (const QString &, const QStringList &)), this, SLOT (dirsRemoved (const QString &, const QStringList &)));
00145   connect (m_project, SIGNAL (filesRemoved (const QString &, const QStringList &)), this, SLOT (filesRemoved (const QString &, const QStringList &)));
00146 }
00147 
00148 KateProjectTreeView::~KateProjectTreeView ()
00149 {
00150 }
00151 
00152 void KateProjectTreeView::addDir (KateProjectTreeViewItem *parent, const QString &dir)
00153 {
00154   QString base = dir;
00155 
00156   if (!dir.isNull())
00157     base += QString ("/");
00158 
00159   QStringList dirs = m_project->dirs (dir);
00160 
00161   for (uint z=0; z < dirs.count(); z++)
00162   {
00163     KateProjectTreeViewItem *item = new KateProjectTreeViewItem (&m_dirDict, parent, m_project, dirs[z], base + dirs[z], true);
00164     addDir (item, base + dirs[z]);
00165   }
00166 
00167   QStringList files = m_project->files (dir);
00168 
00169   for (uint z=0; z < files.count(); z++)
00170   {
00171     new KateProjectTreeViewItem (&m_dirDict, parent, m_project, files[z], base + files[z], false);
00172   }
00173 }
00174 
00175 void KateProjectTreeView::slotDoubleClicked( QListViewItem *i, const QPoint &, int )
00176 {
00177   execute( i );
00178 }
00179 
00180 void KateProjectTreeView::execute( QListViewItem *i )
00181 {
00182   KateProjectTreeViewItem *item = (KateProjectTreeViewItem *) i;
00183 
00184   if (!item)
00185     return;
00186 
00187   if (item->isDir())
00188     item->setOpen (!item->isOpen());
00189   else
00190     m_mainWin->viewManager()->openURL (KURL (m_project->dir() + QString ("/") + item->fullName()));
00191 }
00192 
00193 void KateProjectTreeView::dirsAdded (const QString &dir, const QStringList &dirs)
00194 {
00195   KateProjectTreeViewItem *item = m_dirDict [QString("/")+dir];
00196 
00197   if (!item)
00198     return;
00199 
00200   QString fullname = dir;
00201   if (!fullname.isNull())
00202     fullname += QString ("/");
00203 
00204   for (uint z=0; z < dirs.size(); z++)
00205   {
00206     // add dir recursive
00207     KateProjectTreeViewItem *i = new KateProjectTreeViewItem (&m_dirDict, item, m_project, dirs[z], fullname + dirs[z], true);
00208     addDir (i, fullname+dirs[z]);
00209   }
00210 
00211   if (dir.isEmpty())
00212     item->setOpen (true);
00213 }
00214 
00215 void KateProjectTreeView::dirsRemoved (const QString &dir, const QStringList &dirs)
00216 {
00217   KateProjectTreeViewItem *item = m_dirDict [QString("/")+dir];
00218 
00219   if (!item)
00220     return;
00221 
00222   QPtrList<KateProjectTreeViewItem> l;
00223   l.setAutoDelete (true);
00224 
00225   KateProjectTreeViewItem *myChild = (KateProjectTreeViewItem *) item->firstChild();
00226   while( myChild )
00227   {
00228     if (dirs.findIndex (myChild->name()) != -1)
00229       l.append (myChild);
00230 
00231     myChild = (KateProjectTreeViewItem *) myChild->nextSibling();
00232   }
00233 }
00234 
00235 void KateProjectTreeView::filesAdded (const QString &dir, const QStringList &files)
00236 {
00237   KateProjectTreeViewItem *item = m_dirDict [QString("/")+dir];
00238 
00239   if (!item)
00240     return;
00241 
00242   QString fullname = dir;
00243   if (!fullname.isNull())
00244     fullname += QString ("/");
00245 
00246   for (uint z=0; z < files.size(); z++)
00247   {
00248     new KateProjectTreeViewItem (&m_dirDict, item, m_project, files[z], fullname + files[z], false);
00249   }
00250 
00251   if (dir.isEmpty())
00252     item->setOpen (true);
00253 }
00254 
00255 void KateProjectTreeView::filesRemoved (const QString &dir, const QStringList &files)
00256 {
00257   KateProjectTreeViewItem *item = m_dirDict [QString("/")+dir];
00258 
00259   if (!item)
00260     return;
00261 
00262   QPtrList<KateProjectTreeViewItem> l;
00263   l.setAutoDelete (true);
00264 
00265   KateProjectTreeViewItem *myChild = (KateProjectTreeViewItem *) item->firstChild();
00266   while( myChild )
00267   {
00268     if (files.findIndex (myChild->name()) != -1)
00269       l.append (myChild);
00270 
00271     myChild = (KateProjectTreeViewItem *) myChild->nextSibling();
00272   }
00273 }
00274 
00275 void KateProjectTreeView::slotContextMenuRequested ( QListViewItem * item, const QPoint & pos, int )
00276 {
00277   if (!item)
00278     return;
00279 
00280   KateProjectTreeViewItem *i = (KateProjectTreeViewItem *) item;
00281 
00282   QPopupMenu *menu = new QPopupMenu (this);
00283 
00284   if (i->isDir())
00285     menu->insertItem (i18n("Add Folders/Files..."), this, SLOT(addIt()));
00286 
00287   if (!i->fullName().isNull())
00288     menu->insertItem (i->isDir() ? i18n("Remove Folder") : i18n("Remove File"), this, SLOT(removeIt()));
00289 
00290   menu->exec(pos);
00291 }
00292 
00293 void KateProjectTreeView::removeIt ()
00294 {
00295   KateProjectTreeViewItem *item = (KateProjectTreeViewItem *) selectedItem();
00296 
00297   if (!item)
00298     return;
00299 
00300   if (item->fullName().isNull())
00301     return;
00302 
00303   QString dir = ((KateProjectTreeViewItem *) item->parent())->fullName();
00304   QStringList liste (item->name());
00305 
00306   if (item->isDir())
00307     m_project->removeDirs (dir, liste);
00308   else
00309     m_project->removeFiles (dir, liste);
00310 }
00311 
00312 void KateProjectTreeView::addIt ()
00313 {
00314   KateProjectTreeViewItem *item = (KateProjectTreeViewItem *) selectedItem();
00315 
00316   if (!item)
00317     return;
00318 
00319   if (item->isDir())
00320     KateProjectDirView::addDialog (m_project, item->fullName(), this);
00321 }
00322 //END KateProjectTreeView
00323 
00324 //BEGIN KateProjectTreeViewContainer
00325 KateProjectTreeViewContainer::KateProjectTreeViewContainer(
00326      Kate::Project *project, KateMainWindow *mainwin,
00327      QWidget *parent, const char *name )
00328      : QVBox( parent, name )
00329 {
00330   // quick find entry
00331   QHBox *b = new QHBox( this, "quickfind entry" );
00332   QLabel *l = new QLabel( i18n("F&ind:"), b );
00333   m_leQF = new KLineEdit( b );
00334   m_leQF->installEventFilter( this );
00335   l->setBuddy( m_leQF );
00336   connect( m_leQF, SIGNAL(textChanged(const QString &)),
00337                    SLOT(qfTextChanged(const QString &)) );
00338 
00339   // tree view
00340   m_tree = new KateProjectTreeView( project, mainwin, this );
00341 }
00342 
00343 KateProjectTreeViewContainer::~KateProjectTreeViewContainer()
00344 {
00345 }
00346 
00347 KateProjectTreeView *KateProjectTreeViewContainer::tree()
00348 {
00349   return m_tree;
00350 }
00351 
00352 void KateProjectTreeViewContainer::qfTextChanged( const QString &t )
00353 {
00354   QListViewItem *i ( m_tree->currentItem() );
00355   if ( ! i ) i = m_tree->firstChild();
00356 
00357   if (!i)
00358     return;
00359 
00360   bool found ( false );
00361   QListViewItemIterator it ( i );
00362 /*  if ( oldtext < t )
00363   {*/
00364     while ( it.current() )
00365     {
00366       if ( it.current()->text(0).startsWith( t ) )
00367       {
00368         found = true;
00369         break;
00370       }
00371       ++it;
00372     }
00373 //   }
00374   if ( ! found )
00375   {
00376     QListViewItemIterator it ( i );
00377     while ( it.current() )
00378     {
00379       if ( it.current()->text(0).startsWith( t ) )
00380       {
00381         found = true;
00382         break;
00383       }
00384       --it;
00385     }
00386   }
00387   if ( it.current() )
00388   {
00389     i = it.current();
00390     if ( i->parent() && ! i->parent()->isOpen() )
00391       i->parent()->setOpen( true );
00392     m_tree->ensureItemVisible( i );
00393 
00394     m_tree->setCurrentItem( i );
00395     m_tree->setSelected(i, true);
00396   }
00397   oldtext = t;
00398 }
00399 
00400 bool KateProjectTreeViewContainer::eventFilter( QObject *o, QEvent *e )
00401 {
00402   if ( o == m_leQF )
00403   {
00404     if ( e->type() == QEvent::KeyPress &&
00405          ( ((QKeyEvent*)e)->key() == Qt::Key_Return ||
00406            ((QKeyEvent*)e)->key() == Qt::Key_Enter ) )
00407     {
00408       return kapp->sendEvent( m_tree, e );
00409     }
00410     if ( e->type() == QEvent::KeyPress &&
00411          ((QKeyEvent*)e)->key() == Qt::Key_Tab )
00412     {
00413       m_tree->setFocus();
00414       return true;
00415     }
00416   }
00417   return QVBox::eventFilter( o, e );
00418 }
00419 
00420 //END KateProjectTreeViewContainer
KDE Logo
This file is part of the documentation for kate Library Version 3.3.90.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Apr 4 11:21:45 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003