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::sortByName ) 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 // 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::slotNextDocument() 00076 { 00077 int c = currentItem (); 00078 00079 if ((c == -1) || (count() == 0)) 00080 return; 00081 00082 if (uint(c+1) < count()) 00083 viewManager->activateView( ((KateFileListItem *)item(c+1))->documentNumber() ); 00084 else 00085 viewManager->activateView( ((KateFileListItem *)item(0))->documentNumber() ); 00086 } 00087 00088 void KateFileList::slotPrevDocument() 00089 { 00090 int c = currentItem (); 00091 00092 if ((c == -1) || (count() == 0)) 00093 return; 00094 00095 if ((c-1) >= 0) 00096 viewManager->activateView( ((KateFileListItem *)item(c-1))->documentNumber() ); 00097 else 00098 viewManager->activateView( ((KateFileListItem *)item(count()-1))->documentNumber() ); 00099 00100 } 00101 00102 void KateFileList::slotDocumentCreated (Kate::Document *doc) 00103 { 00104 insertItem (new KateFileListItem (docManager, doc, doc->documentNumber(), doc->docName()) ); 00105 connect(doc,SIGNAL(modStateChanged(Kate::Document *)),this,SLOT(slotModChanged(Kate::Document *))); 00106 connect(doc,SIGNAL(nameChanged(Kate::Document *)),this,SLOT(slotNameChanged(Kate::Document *))); 00107 connect(doc,SIGNAL(modifiedOnDisc(Kate::Document *, bool, unsigned char)),this,SLOT(slotModifiedOnDisc(Kate::Document *, bool, unsigned char))); 00108 00109 updateSort (); 00110 } 00111 00112 void KateFileList::slotDocumentDeleted (uint documentNumber) 00113 { 00114 for (uint i = 0; i < count(); i++) 00115 { 00116 if (((KateFileListItem *) item (i)) ->documentNumber() == documentNumber) 00117 { 00118 if (count() > 1) 00119 removeItem( i ); 00120 else 00121 clear(); 00122 } 00123 } 00124 } 00125 00126 void KateFileList::slotActivateView( QListBoxItem *item ) 00127 { 00128 viewManager->activateView( ((KateFileListItem *)item)->documentNumber() ); 00129 } 00130 00131 void KateFileList::slotModChanged (Kate::Document *doc) 00132 { 00133 if (!doc) return; 00134 00135 for (uint i = 0; i < count(); i++) 00136 { 00137 if (((KateFileListItem *) item (i)) ->documentNumber() == doc->documentNumber()) 00138 { 00139 triggerUpdate(false); 00140 break; 00141 } 00142 } 00143 } 00144 00145 void KateFileList::slotModifiedOnDisc (Kate::Document *doc, bool, unsigned char r) 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 if ( r != 0 ) 00157 { 00158 QPixmap w( BarIcon("messagebox_warning", 32) ); 00159 QString a; 00160 if ( r == 1 ) 00161 a = i18n("The document<br><code>%1</code><br>was changed on disk by another process."); 00162 else if ( r == 2 ) 00163 a = i18n("The document<br><code>%1</code><br>was created on disk by another process."); 00164 else if ( r == 3 ) 00165 a = i18n("The document<br><code>%1</code><br>was deleted from disk by another process"); 00166 00167 // KNotifyClient::instance(); 00168 // int n = KNotifyClient::event( "file_modified_on_disc", 00169 // i18n("The document<br><code>%1</code><br>%2").arg( doc->url().prettyURL() ).arg( a ) ); 00170 // kdDebug(13001)<<"The BASTARD returned "<<n<<endl; 00171 if ( ((KateMainWindow*)topLevelWidget())->notifyMod() ) 00172 KPassivePopup::message( i18n("Warning"), 00173 a.arg( doc->url().prettyURL() ), 00174 w, topLevelWidget() ); 00175 } 00176 } 00177 00178 void KateFileList::slotNameChanged (Kate::Document *doc) 00179 { 00180 if (!doc) return; 00181 00182 for (uint i = 0; i < count(); i++) 00183 { 00184 if (((KateFileListItem *) item (i)) ->documentNumber() == doc->documentNumber()) 00185 { 00186 //File name shouldn't be too long - Maciek 00187 QString c = doc -> docName(); 00188 if (c.length() > 200) 00189 c = "..." + c.right(197); 00190 00191 ((KateFileListItem *)item(i))->setText(c); 00192 00193 triggerUpdate(false); 00194 break; 00195 } 00196 } 00197 00198 updateSort (); 00199 } 00200 00201 void KateFileList::slotViewChanged () 00202 { 00203 if (!viewManager->activeView()) return; 00204 00205 Kate::View *view = viewManager->activeView(); 00206 00207 for (uint i = 0; i < count(); i++) 00208 { 00209 if (((KateFileListItem *) item (i)) ->documentNumber() == ((Kate::Document *)view->getDoc())->documentNumber()) 00210 { 00211 setCurrentItem (i); 00212 if ( !isSelected( item(i) ) ) 00213 setSelected( i, true ); 00214 break; 00215 } 00216 } 00217 } 00218 00219 void KateFileList::slotMenu ( QListBoxItem *item, const QPoint &p ) 00220 { 00221 if (!item) 00222 return; 00223 00224 QPopupMenu *menu = (QPopupMenu*) ((viewManager->mainWindow())->factory()->container("filelist_popup", viewManager->mainWindow())); 00225 00226 if (menu) 00227 menu->exec(p); 00228 } 00229 00230 void KateFileList::tip( const QPoint &p, QRect &r, QString &str ) 00231 { 00232 KateFileListItem *i = (KateFileListItem*)itemAt( p ); 00233 r = itemRect( i ); 00234 str = ""; 00235 00236 if ( !i || !r.isValid() ) 00237 return; 00238 00239 Kate::Document *doc = docManager->documentWithID(i->documentNumber()); 00240 00241 if (!doc) 00242 return; 00243 00244 const KateDocumentInfo *info = docManager->documentInfo(doc); 00245 00246 if (info && info->modifiedOnDisc) 00247 { 00248 if (info->modifiedOnDiscReason == 1) 00249 str += i18n("<b>This file was changed (modified) on disc by another program!</b><br />"); 00250 else if (info->modifiedOnDiscReason == 2) 00251 str += i18n("<b>This file was changed (created) on disc by another program!</b><br />"); 00252 else if (info->modifiedOnDiscReason == 3) 00253 str += i18n("<b>This file was changed (deleted) on disc by another program!</b><br />"); 00254 } 00255 00256 str += doc->url().prettyURL(); 00257 } 00258 00259 KateFileListItem::KateFileListItem( KateDocManager *_docManager, Kate::Document *doc, uint documentNumber, const QString& text): QListBoxItem() 00260 { 00261 this->doc = doc; 00262 myDocID = documentNumber; 00263 docManager = _docManager; 00264 setText( text ); 00265 } 00266 00267 KateFileListItem::~KateFileListItem() 00268 { 00269 } 00270 00271 uint KateFileListItem::documentNumber () 00272 { 00273 return myDocID; 00274 } 00275 00276 void KateFileListItem::setText(const QString &text) 00277 { 00278 QListBoxItem::setText(text); 00279 } 00280 00281 int KateFileListItem::height( const QListBox* lb ) const 00282 { 00283 int h; 00284 00285 if ( text().isEmpty() ) 00286 h = 16; 00287 else 00288 h = QMAX( 16, lb->fontMetrics().lineSpacing() + 1 ); 00289 00290 return QMAX( h, QApplication::globalStrut().height() ); 00291 } 00292 00293 int KateFileListItem::width( const QListBox* lb ) const 00294 { 00295 if ( text().isEmpty() ) 00296 return QMAX( 16 + 6, QApplication::globalStrut().width() ); 00297 00298 return QMAX( 16 + lb->fontMetrics().width( text() ) + 6, QApplication::globalStrut().width() ); 00299 } 00300 00301 void KateFileListItem::paint( QPainter *painter ) 00302 { 00303 static QPixmap noPm = SmallIcon ("null"); 00304 static QPixmap modPm = SmallIcon("modified"); 00305 static QPixmap discPm = SmallIcon("modonhd"); 00306 static QPixmap modmodPm = SmallIcon("modmod"); 00307 00308 const KateDocumentInfo *info = docManager->documentInfo (doc); 00309 00310 if (info && info->modifiedOnDisc) 00311 painter->drawPixmap( 3, 0, doc->isModified() ? modmodPm : discPm ); 00312 else 00313 painter->drawPixmap( 3, 0, doc->isModified() ? modPm : noPm ); 00314 00315 if ( !text().isEmpty() ) 00316 { 00317 QFontMetrics fm = painter->fontMetrics(); 00318 00319 int yPos; // vertical text position 00320 00321 if ( 16 < fm.height() ) 00322 yPos = fm.ascent() + fm.leading()/2; 00323 else 00324 yPos = 16/2 - fm.height()/2 + fm.ascent(); 00325 00326 painter->drawText( 16 + 4, yPos, text() ); 00327 } 00328 } 00329 00331 // KateFileList::KFLToolTip implementation 00332 00333 KateFileList::KFLToolTip::KFLToolTip( QWidget *parent ) 00334 : QToolTip( parent ) 00335 { 00336 } 00337 00338 void KateFileList::KFLToolTip::maybeTip( const QPoint &p ) 00339 { 00340 QString str; 00341 QRect r; 00342 00343 ((KateFileList*)parentWidget())->tip( p, r, str ); 00344 00345 if( !str.isEmpty() && r.isValid() ) 00346 tip( r, str ); 00347 } 00348 00349 void KateFileList::setSortType (int s) 00350 { 00351 m_sort = s; 00352 updateSort (); 00353 } 00354 00355 void KateFileList::updateSort () 00356 { 00357 if (m_sort == KateFileList::sortByName) 00358 sort (); 00359 }
KDE Logo
This file is part of the documentation for kate Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Sep 16 15:59:27 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003