kate Library API Documentation

katefilelist.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 #include "katefilelist.h"
00022 #include "katefilelist.moc"
00023 
00024 #include "katedocmanager.h"
00025 #include "kateviewmanager.h"
00026 #include "katemainwindow.h"
00027 
00028 #include <qapplication.h>
00029 #include <qpainter.h>
00030 #include <qpopupmenu.h>
00031 
00032 #include <kiconloader.h>
00033 #include <klocale.h>
00034 #include <kglobalsettings.h>
00035 #include <kpassivepopup.h>
00036 // #include <knotifyclient.h>
00037 #include <kdebug.h>
00038 #include <kapplication.h>
00039 
00040 KateFileList::KateFileList (KateDocManager *_docManager,
00041                             KateViewManager *_viewManager,
00042                             QWidget * parent, const char * name )
00043     :  KListBox (parent, name)
00044     , m_sort( KateFileList::sortByID )
00045 {
00046   setFocusPolicy ((QWidget::FocusPolicy)0);
00047 
00048   docManager = _docManager;
00049   viewManager = _viewManager;
00050   tooltip = new KFLToolTip( this );
00051 
00052   for (uint i = 0; i < docManager->documents(); i++)
00053   {
00054     slotDocumentCreated (docManager->document(i));
00055     slotModChanged (docManager->document(i));
00056   }
00057 
00058   connect(docManager,SIGNAL(documentCreated(Kate::Document *)),this,SLOT(slotDocumentCreated(Kate::Document *)));
00059   connect(docManager,SIGNAL(documentDeleted(uint)),this,SLOT(slotDocumentDeleted(uint)));
00060 
00061   // do not Honour KDE single/double click setting
00062 //  connect(this,SIGNAL(executed(QListBoxItem *)),this,SLOT(slotActivateView(QListBoxItem *)));
00063   connect(this,SIGNAL(highlighted(QListBoxItem *)),this,SLOT(slotActivateView(QListBoxItem *)));
00064 
00065   connect(viewManager,SIGNAL(viewChanged()), this,SLOT(slotViewChanged()));
00066 
00067   connect(this,SIGNAL(contextMenuRequested ( QListBoxItem *, const QPoint & )), this,SLOT(slotMenu ( QListBoxItem *, const QPoint & )));
00068 }
00069 
00070 KateFileList::~KateFileList ()
00071 {
00072   delete tooltip;
00073 }
00074 
00075 void KateFileList::keyPressEvent(QKeyEvent *e) {
00076   if ( ( e->key() == Key_Return ) || ( e->key() == Key_Enter ) )
00077   {
00078     e->accept();
00079     slotActivateView(item(currentItem()));
00080   }
00081   else
00082   {
00083     KListBox::keyPressEvent(e);
00084   }
00085 }
00086 
00087 void KateFileList::slotNextDocument()
00088 {
00089   int c = currentItem ();
00090 
00091   if ((c == -1) || (count() == 0))
00092     return;
00093   
00094   if (uint(c+1) < count())
00095     viewManager->activateView( ((KateFileListItem *)item(c+1))->documentNumber() );
00096   else
00097     viewManager->activateView( ((KateFileListItem *)item(0))->documentNumber() );
00098 }
00099     
00100 void KateFileList::slotPrevDocument()
00101 {
00102   int c = currentItem ();
00103 
00104   if ((c == -1) || (count() == 0))
00105     return;
00106   
00107   if ((c-1) >= 0)
00108     viewManager->activateView( ((KateFileListItem *)item(c-1))->documentNumber() );
00109   else
00110     viewManager->activateView( ((KateFileListItem *)item(count()-1))->documentNumber() );
00111 
00112 }
00113 
00114 void KateFileList::slotDocumentCreated (Kate::Document *doc)
00115 {
00116   insertItem (new KateFileListItem (docManager, doc, doc->documentNumber(), doc->docName()) );
00117   connect(doc,SIGNAL(modStateChanged(Kate::Document *)),this,SLOT(slotModChanged(Kate::Document *)));
00118   connect(doc,SIGNAL(nameChanged(Kate::Document *)),this,SLOT(slotNameChanged(Kate::Document *)));
00119   connect(doc,SIGNAL(modifiedOnDisc(Kate::Document *, bool, unsigned char)),this,SLOT(slotModifiedOnDisc(Kate::Document *, bool, unsigned char)));
00120 
00121   updateSort ();
00122 }
00123 
00124 void KateFileList::slotDocumentDeleted (uint documentNumber)
00125 {
00126   for (uint i = 0; i < count(); i++)
00127   {
00128     if (((KateFileListItem *) item (i)) ->documentNumber() == documentNumber)
00129     {
00130       if (count() > 1)
00131         removeItem( i );
00132       else
00133         clear();
00134     }
00135   }
00136 }
00137 
00138 void KateFileList::slotActivateView( QListBoxItem *item )
00139 {
00140   viewManager->activateView( ((KateFileListItem *)item)->documentNumber() );
00141 }
00142 
00143 void KateFileList::slotModChanged (Kate::Document *doc)
00144 {
00145   if (!doc) return;
00146 
00147   for (uint i = 0; i < count(); i++)
00148   {
00149     if (((KateFileListItem *) item (i)) ->documentNumber() == doc->documentNumber())
00150     {
00151       triggerUpdate(false);
00152       break;
00153     }
00154   }
00155 }
00156 
00157 void KateFileList::slotModifiedOnDisc (Kate::Document *doc, bool, unsigned char r)
00158 {
00159   for (uint i = 0; i < count(); i++)
00160   {
00161     if (((KateFileListItem *) item (i)) ->documentNumber() == doc->documentNumber())
00162     {
00163       triggerUpdate(false);
00164       break;
00165     }
00166   }
00167 
00168   if ( r != 0 )
00169   {
00170     QPixmap w( BarIcon("messagebox_warning", 32) );
00171     QString a;
00172     if ( r == 1 )
00173       a = i18n("The document<br><code>%1</code><br>was changed on disk by another process.");
00174     else if ( r == 2 )
00175       a = i18n("The document<br><code>%1</code><br>was created on disk by another process.");
00176     else if ( r == 3 )
00177       a = i18n("The document<br><code>%1</code><br>was deleted from disk by another process");
00178 
00179 //     KNotifyClient::instance();
00180 //     int n = KNotifyClient::event( "file_modified_on_disc",
00181 //           i18n("The document<br><code>%1</code><br>%2").arg( doc->url().prettyURL() ).arg( a ) );
00182 //     kdDebug(13001)<<"The BASTARD returned "<<n<<endl;
00183     if ( ((KateMainWindow*)topLevelWidget())->notifyMod() )
00184       KPassivePopup::message( i18n("Warning"),
00185                               a.arg( doc->url().prettyURL() ),
00186                               w, topLevelWidget() );
00187   }
00188 }
00189 
00190 void KateFileList::slotNameChanged (Kate::Document *doc)
00191 {
00192   if (!doc) return;
00193 
00194   for (uint i = 0; i < count(); i++)
00195   {
00196     if (((KateFileListItem *) item (i)) ->documentNumber() == doc->documentNumber())
00197     {
00198       //File name shouldn't be too long - Maciek
00199      QString c = doc -> docName();
00200      if (c.length() > 200)
00201        c = "..." + c.right(197);
00202 
00203      ((KateFileListItem *)item(i))->setText(c);
00204 
00205       triggerUpdate(false);
00206       break;
00207     }
00208   }
00209 
00210   updateSort ();
00211 }
00212 
00213 void KateFileList::slotViewChanged ()
00214 {
00215   if (!viewManager->activeView()) return;
00216 
00217   Kate::View *view = viewManager->activeView();
00218 
00219   for (uint i = 0; i < count(); i++)
00220   {
00221     if (((KateFileListItem *) item (i)) ->documentNumber() == ((Kate::Document *)view->getDoc())->documentNumber())
00222     {
00223       setCurrentItem (i);
00224       if ( !isSelected( item(i) ) )
00225         setSelected( i, true );
00226       break;
00227     }
00228   }
00229 }
00230 
00231 void KateFileList::slotMenu ( QListBoxItem *item, const QPoint &p )
00232 {
00233   if (!item)
00234     return;
00235   
00236   QPopupMenu *menu = (QPopupMenu*) ((viewManager->mainWindow())->factory()->container("filelist_popup", viewManager->mainWindow()));
00237 
00238   if (menu)
00239     menu->exec(p);
00240 }
00241 
00242 void KateFileList::tip( const QPoint &p, QRect &r, QString &str )
00243 {
00244   KateFileListItem *i = (KateFileListItem*)itemAt( p );
00245   r = itemRect( i );
00246   str = "";
00247 
00248   if ( !i || !r.isValid() )
00249     return;
00250 
00251   Kate::Document *doc = docManager->documentWithID(i->documentNumber());
00252 
00253   if (!doc)
00254     return;
00255 
00256   const KateDocumentInfo *info = docManager->documentInfo(doc);
00257 
00258   if (info && info->modifiedOnDisc)
00259   {
00260     if (info->modifiedOnDiscReason == 1)
00261       str += i18n("<b>This file was changed (modified) on disc by another program.</b><br />");
00262     else if (info->modifiedOnDiscReason == 2)
00263       str += i18n("<b>This file was changed (created) on disc by another program.</b><br />");
00264     else if (info->modifiedOnDiscReason == 3)
00265       str += i18n("<b>This file was changed (deleted) on disc by another program.</b><br />");
00266   }
00267 
00268   str += doc->url().prettyURL();
00269 }
00270 
00271 KateFileListItem::KateFileListItem( KateDocManager *_docManager, Kate::Document *doc, uint documentNumber, const QString& text): QListBoxItem()
00272 {
00273   this->doc = doc;
00274   myDocID = documentNumber;
00275   docManager = _docManager;
00276   setText( text );
00277 }
00278 
00279 KateFileListItem::~KateFileListItem()
00280 {
00281 }
00282 
00283 uint KateFileListItem::documentNumber ()
00284 {
00285   return myDocID;
00286 }
00287 
00288 void KateFileListItem::setText(const QString &text)
00289 {
00290   QListBoxItem::setText(text);
00291 }
00292 
00293 int KateFileListItem::height( const QListBox* lb ) const
00294 {
00295   int h;
00296 
00297   if ( text().isEmpty() )
00298     h = 16;
00299   else
00300     h = QMAX( 16, lb->fontMetrics().lineSpacing() + 1 );
00301 
00302   return QMAX( h, QApplication::globalStrut().height() );
00303 }
00304 
00305 int KateFileListItem::width( const QListBox* lb ) const
00306 {
00307   if ( text().isEmpty() )
00308     return QMAX( 16 + 6, QApplication::globalStrut().width() );
00309 
00310   return QMAX( 16 + lb->fontMetrics().width( text() ) + 6, QApplication::globalStrut().width() );
00311 }
00312 
00313 void KateFileListItem::paint( QPainter *painter )
00314 {
00315   static QPixmap noPm = SmallIcon ("null");
00316   static QPixmap modPm = SmallIcon("modified");
00317   static QPixmap discPm = SmallIcon("modonhd");
00318   static QPixmap modmodPm = SmallIcon("modmod");
00319 
00320   const KateDocumentInfo *info = docManager->documentInfo (doc);
00321 
00322   if (info && info->modifiedOnDisc)
00323     painter->drawPixmap( 3, 0, doc->isModified() ? modmodPm : discPm );
00324   else
00325     painter->drawPixmap( 3, 0, doc->isModified() ? modPm : noPm );
00326 
00327   if ( !text().isEmpty() )
00328   {
00329     QFontMetrics fm = painter->fontMetrics();
00330 
00331     int yPos;                       // vertical text position
00332 
00333      if ( 16 < fm.height() )
00334       yPos = fm.ascent() + fm.leading()/2;
00335     else
00336       yPos = 16/2 - fm.height()/2 + fm.ascent();
00337 
00338     painter->drawText( 16 + 4, yPos, text() );
00339   }
00340 }
00341 
00343 // KateFileList::KFLToolTip implementation
00344 
00345 KateFileList::KFLToolTip::KFLToolTip( QWidget *parent )
00346   : QToolTip( parent )
00347 {
00348 }
00349 
00350 void KateFileList::KFLToolTip::maybeTip( const QPoint &p )
00351 {
00352   QString str;
00353   QRect r;
00354 
00355   ((KateFileList*)parentWidget())->tip( p, r, str );
00356 
00357   if( !str.isEmpty() && r.isValid() )
00358     tip( r, str );
00359 }
00360 
00361 void KateFileList::setSortType (int s)
00362 {
00363   m_sort = s;
00364   updateSort ();
00365 }
00366 
00367 void KateFileList::updateSort ()
00368 {
00369   if (m_sort == KateFileList::sortByName)
00370     sort ();
00371 }
KDE Logo
This file is part of the documentation for kate Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Aug 20 13:39:05 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003