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