kate Library API Documentation

kateviewspace.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 "kateviewspace.h" 00022 #include "kateviewspace.moc" 00023 00024 #include "katemainwindow.h" 00025 #include "kateviewmanager.h" 00026 #include "katedocmanager.h" 00027 #include "kateapp.h" 00028 00029 #include <kiconloader.h> 00030 #include <klocale.h> 00031 #include <ksqueezedtextlabel.h> 00032 #include <kconfig.h> 00033 #include <kdebug.h> 00034 00035 #include <qwidgetstack.h> 00036 #include <qpainter.h> 00037 #include <qlabel.h> 00038 #include <qcursor.h> 00039 #include <qpopupmenu.h> 00040 #include <qpixmap.h> 00041 00042 //BEGIN KVSSBSep 00043 /* 00044 "KateViewSpaceStatusBarSeparator" 00045 A 2 px line to separate the statusbar from the view. 00046 It is here to compensate for the lack of a frame in the view, 00047 I think Kate looks very nice this way, as QScrollView with frame 00048 looks slightly clumsy... 00049 Slight 3D effect. I looked for suitable QStyle props or methods, 00050 but found none, though maybe it should use QStyle::PM_DefaultFrameWidth 00051 for height (TRY!). 00052 It does look a bit funny with flat styles (Light, .Net) as is, 00053 but there are on methods to paint panel lines separately. And, 00054 those styles tends to look funny on their own, as a light line 00055 in a 3D frame next to a light contents widget is not functional. 00056 Also, QStatusBar is up to now completely ignorant to style. 00057 -anders 00058 */ 00059 class KVSSBSep : public QWidget { 00060 public: 00061 KVSSBSep( KateViewSpace *parent=0) : QWidget(parent) 00062 { 00063 setFixedHeight( 2 ); 00064 } 00065 protected: 00066 void paintEvent( QPaintEvent *e ) 00067 { 00068 QPainter p( this ); 00069 p.setPen( colorGroup().shadow() ); 00070 p.drawLine( e->rect().topLeft(), e->rect().topRight() ); 00071 p.setPen( ((KateViewSpace*)parentWidget())->isActiveSpace() ? colorGroup().light() : colorGroup().midlight() ); 00072 p.drawLine( e->rect().bottomLeft(), e->rect().bottomRight() ); 00073 } 00074 }; 00075 //END KVSSBSep 00076 00077 //BEGIN KateViewSpace 00078 KateViewSpace::KateViewSpace( KateViewManager *viewManager, 00079 QWidget* parent, const char* name ) 00080 : QVBox(parent, name), 00081 m_viewManager( viewManager ) 00082 { 00083 mViewList.setAutoDelete(false); 00084 00085 stack = new QWidgetStack( this ); 00086 setStretchFactor(stack, 1); 00087 stack->setFocus(); 00088 sep = new KVSSBSep( this ); 00089 mStatusBar = new KateVSStatusBar(this); 00090 mIsActiveSpace = false; 00091 mViewCount = 0; 00092 00093 setMinimumWidth (mStatusBar->minimumWidth()); 00094 m_group = QString::null; 00095 } 00096 00097 KateViewSpace::~KateViewSpace() 00098 { 00099 } 00100 00101 void KateViewSpace::polish() 00102 { 00103 mStatusBar->show(); 00104 } 00105 00106 void KateViewSpace::addView(Kate::View* v, bool show) 00107 { 00108 // restore the config of this view if possible 00109 if ( !m_group.isEmpty() ) 00110 { 00111 QString fn = v->getDoc()->url().prettyURL(); 00112 if ( ! fn.isEmpty() ) 00113 { 00114 QString vgroup = QString("%1 %2").arg(m_group).arg(fn); 00115 if ( KateApp::kateSessionConfig()->hasGroup( vgroup ) ) 00116 { 00117 KateApp::kateSessionConfig()->setGroup( vgroup ); 00118 v->readSessionConfig( KateApp::kateSessionConfig() ); 00119 } 00120 00121 } 00122 } 00123 00124 uint id = mViewList.count(); 00125 stack->addWidget(v, id); 00126 if (show) { 00127 mViewList.append(v); 00128 showView( v ); 00129 } 00130 else { 00131 Kate::View* c = mViewList.current(); 00132 mViewList.prepend( v ); 00133 showView( c ); 00134 } 00135 } 00136 00137 void KateViewSpace::removeView(Kate::View* v) 00138 { 00139 // mStatusBar->slotClear (); 00140 mViewList.remove (v); 00141 stack->removeWidget (v); 00142 // FIXME only if active - focus stack->visibleWidget() or back out 00143 if (currentView() != 0L) 00144 stack->raiseWidget(mViewList.current()); 00145 else if (mViewList.count() > 0) 00146 stack->raiseWidget(mViewList.last()); 00147 } 00148 00149 bool KateViewSpace::showView(Kate::View* v) 00150 { 00151 Kate::Document* d = v->getDoc(); 00152 QPtrListIterator<Kate::View> it (mViewList); 00153 00154 it.toLast(); 00155 for( ; it.current(); --it ) { 00156 if (it.current()->getDoc() == d) { 00157 Kate::View* kv = it.current(); 00158 mViewList.removeRef( kv ); 00159 mViewList.append( kv ); 00160 stack->raiseWidget( kv ); 00161 kv->show(); 00162 return true; 00163 } 00164 } 00165 return false; 00166 } 00167 00168 bool KateViewSpace::showView(uint documentNumber) 00169 { 00170 QPtrListIterator<Kate::View> it (mViewList); 00171 it.toLast(); 00172 for( ; it.current(); --it ) { 00173 if (((Kate::Document*)it.current()->getDoc())->documentNumber() == documentNumber) { 00174 Kate::View* kv = it.current(); 00175 mViewList.removeRef( kv ); 00176 mViewList.append( kv ); 00177 stack->raiseWidget( kv ); 00178 kv->show(); 00179 return true; 00180 } 00181 } 00182 return false; 00183 } 00184 00185 00186 Kate::View* KateViewSpace::currentView() 00187 { 00188 if (mViewList.count() > 0) 00189 return (Kate::View*)stack->visibleWidget(); 00190 00191 return 0L; 00192 } 00193 00194 bool KateViewSpace::isActiveSpace() 00195 { 00196 return mIsActiveSpace; 00197 } 00198 00199 void KateViewSpace::setActive( bool b, bool ) 00200 { 00201 mIsActiveSpace = b; 00202 00203 // change the statusbar palette and make sure it gets updated 00204 QPalette pal( palette() ); 00205 if ( ! b ) 00206 { 00207 pal.setColor( QColorGroup::Background, pal.active().mid() ); 00208 pal.setColor( QColorGroup::Light, pal.active().midlight() ); 00209 } 00210 mStatusBar->setPalette( pal ); 00211 mStatusBar->update(); 00212 sep->update(); 00213 } 00214 00215 bool KateViewSpace::event( QEvent *e ) 00216 { 00217 if ( e->type() == QEvent::PaletteChange ) 00218 { 00219 setActive( mIsActiveSpace ); 00220 return true; 00221 } 00222 return QVBox::event( e ); 00223 } 00224 00225 void KateViewSpace::slotStatusChanged (Kate::View *view, int r, int c, int ovr, bool block, int mod, const QString &msg) 00226 { 00227 if ((QWidgetStack *)view->parentWidget() != stack) 00228 return; 00229 mStatusBar->setStatus( r, c, ovr, block, mod, msg ); 00230 } 00231 00232 void KateViewSpace::saveConfig ( KConfig* config, int myIndex ,const QString& viewConfGrp) 00233 { 00234 QString group = QString(viewConfGrp+"-ViewSpace %1").arg( myIndex ); 00235 00236 config->setGroup (group); 00237 config->writeEntry ("Count", mViewList.count()); 00238 00239 if (currentView()) 00240 config->writeEntry( "Active View", currentView()->getDoc()->url().prettyURL() ); 00241 00242 // Save file list, includeing cursor position in this instance. 00243 QPtrListIterator<Kate::View> it(mViewList); 00244 00245 int idx = 0; 00246 for (; it.current(); ++it) 00247 { 00248 if ( !it.current()->getDoc()->url().isEmpty() ) 00249 { 00250 config->setGroup( group ); 00251 config->writeEntry( QString("View %1").arg( idx ), it.current()->getDoc()->url().prettyURL() ); 00252 00253 // view config, group: "ViewSpace <n> url" 00254 QString vgroup = QString("%1 %2").arg(group).arg(it.current()->getDoc()->url().prettyURL()); 00255 config->setGroup( vgroup ); 00256 it.current()->writeSessionConfig( config ); 00257 } 00258 00259 idx++; 00260 } 00261 } 00262 00263 void KateViewSpace::modifiedOnDisc(Kate::Document *, bool, unsigned char) 00264 { 00265 mStatusBar->updateMod( currentView()->getDoc()->isModified() ); 00266 } 00267 00268 void KateViewSpace::restoreConfig ( KateViewManager *viewMan, KConfig* config, const QString &group ) 00269 { 00270 config->setGroup (group); 00271 00272 QString fn = config->readEntry( "Active View" ); 00273 00274 if ( !fn.isEmpty() ) 00275 { 00276 Kate::Document *doc = viewMan->m_docManager->findDocumentByUrl (KURL(fn)); 00277 00278 if (doc) 00279 { 00280 // view config, group: "ViewSpace <n> url" 00281 QString vgroup = QString("%1 %2").arg(group).arg(fn); 00282 config->setGroup( vgroup ); 00283 00284 viewMan->createView (doc); 00285 00286 Kate::View *v = viewMan->activeView (); 00287 00288 if (v) 00289 v->readSessionConfig( config ); 00290 } 00291 } 00292 00293 if (mViewList.isEmpty()) 00294 viewMan->createView (viewMan->m_docManager->document(0)); 00295 00296 m_group = group; // used for restroing view configs later 00297 } 00298 //END KateViewSpace 00299 00300 //BEGIN KateVSStatusBar 00301 KateVSStatusBar::KateVSStatusBar ( KateViewSpace *parent, const char *name ) 00302 : KStatusBar( parent, name ), 00303 m_viewSpace( parent ) 00304 { 00305 m_lineColLabel = new QLabel( i18n(" Line: 1 Col: 0 "), this ); 00306 addWidget( m_lineColLabel, 0, false ); 00307 m_lineColLabel->setAlignment( Qt::AlignCenter ); 00308 m_lineColLabel->installEventFilter( this ); 00309 00310 m_modifiedLabel = new QLabel( QString(" "), this ); 00311 addWidget( m_modifiedLabel, 0, false ); 00312 m_modifiedLabel->setAlignment( Qt::AlignCenter ); 00313 m_modifiedLabel->installEventFilter( this ); 00314 00315 m_insertModeLabel = new QLabel( i18n(" INS "), this ); 00316 addWidget( m_insertModeLabel, 0, false ); 00317 m_insertModeLabel->setAlignment( Qt::AlignCenter ); 00318 m_insertModeLabel->installEventFilter( this ); 00319 00320 m_selectModeLabel = new QLabel( i18n(" NORM "), this ); 00321 addWidget( m_selectModeLabel, 0, false ); 00322 m_selectModeLabel->setAlignment( Qt::AlignCenter ); 00323 m_selectModeLabel->installEventFilter( this ); 00324 00325 m_fileNameLabel=new KSqueezedTextLabel( this ); 00326 addWidget( m_fileNameLabel, 1, true ); 00327 m_fileNameLabel->setMinimumSize( 0, 0 ); 00328 m_fileNameLabel->setSizePolicy(QSizePolicy( QSizePolicy::Ignored, QSizePolicy::Fixed )); 00329 m_fileNameLabel->setAlignment( /*Qt::AlignRight*/Qt::AlignLeft ); 00330 m_fileNameLabel->installEventFilter( this ); 00331 00332 installEventFilter( this ); 00333 m_modPm = SmallIcon("modified"); 00334 m_modDiscPm = SmallIcon("modonhd"); 00335 m_modmodPm = SmallIcon("modmod"); 00336 m_noPm = SmallIcon("null"); 00337 } 00338 00339 KateVSStatusBar::~KateVSStatusBar () 00340 { 00341 } 00342 00343 void KateVSStatusBar::setStatus( int r, int c, int ovr, bool block, int mod, const QString &msg ) 00344 { 00345 m_lineColLabel->setText( 00346 i18n(" Line: %1 Col: %2 ").arg(KGlobal::locale()->formatNumber(r+1, 0)) 00347 .arg(KGlobal::locale()->formatNumber(c+1, 0)) ); 00348 00349 if (ovr == 0) 00350 m_insertModeLabel->setText( i18n(" R/O ") ); 00351 else if (ovr == 1) 00352 m_insertModeLabel->setText( i18n(" OVR ") ); 00353 else if (ovr == 2) 00354 m_insertModeLabel->setText( i18n(" INS ") ); 00355 00356 updateMod( mod ); 00357 00358 m_selectModeLabel->setText( block ? i18n(" BLK ") : i18n(" NORM ") ); 00359 00360 m_fileNameLabel->setText( msg ); 00361 } 00362 00363 void KateVSStatusBar::updateMod( bool mod ) 00364 { 00365 const KateDocumentInfo *info = m_viewSpace->m_viewManager->m_docManager-> 00366 documentInfo ( m_viewSpace->currentView()->getDoc() ); 00367 00368 m_modifiedLabel->setPixmap( 00369 mod ? 00370 info && info->modifiedOnDisc ? 00371 m_modmodPm : 00372 m_modPm : 00373 info && info->modifiedOnDisc ? 00374 m_modDiscPm : 00375 m_noPm 00376 ); 00377 } 00378 00379 void KateVSStatusBar::showMenu() 00380 { 00381 KMainWindow* mainWindow = static_cast<KMainWindow*>( topLevelWidget() ); 00382 QPopupMenu* menu = static_cast<QPopupMenu*>( mainWindow->factory()->container("viewspace_popup", mainWindow ) ); 00383 00384 if (menu) 00385 menu->exec(QCursor::pos()); 00386 } 00387 00388 bool KateVSStatusBar::eventFilter(QObject*,QEvent *e) 00389 { 00390 if (e->type()==QEvent::MouseButtonPress) 00391 { 00392 if ( ((KateViewSpace*)parentWidget())->currentView() ) 00393 ((KateViewSpace*)parentWidget())->currentView()->setFocus(); 00394 00395 if ( ((QMouseEvent*)e)->button()==RightButton) 00396 showMenu(); 00397 00398 return true; 00399 } 00400 return false; 00401 } 00402 //END KateVSStatusBar 00403 // kate: space-indent on; indent-width 2; replace-tabs on;
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:28 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003