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, 2005 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 "kateviewspacecontainer.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().left(), 0, e->rect().right(), 0 );
00071     p.setPen( ((KateViewSpace*)parentWidget())->isActiveSpace() ? colorGroup().light() : colorGroup().midlight() );
00072     p.drawLine( e->rect().left(), 1, e->rect().right(), 1 );
00073   }
00074 };
00075 //END KVSSBSep
00076 
00077 //BEGIN KateViewSpace
00078 KateViewSpace::KateViewSpace( KateViewSpaceContainer *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   disconnect( v->getDoc(), SIGNAL(modifiedChanged()),
00140               mStatusBar, SLOT(modifiedChanged()) );
00141 
00142   bool active = ( v == currentView() );
00143 
00144   mViewList.remove (v);
00145   stack->removeWidget (v);
00146 
00147   if ( ! active )
00148     return;
00149 
00150   if (currentView() != 0L)
00151     showView(mViewList.current());
00152   else if (mViewList.count() > 0)
00153     showView(mViewList.last());
00154 }
00155 
00156 bool KateViewSpace::showView(Kate::View* v)
00157 {
00158   return showView( v->getDoc()->documentNumber() );
00159 }
00160 
00161 bool KateViewSpace::showView(uint documentNumber)
00162 {
00163   QPtrListIterator<Kate::View> it (mViewList);
00164   it.toLast();
00165   for( ; it.current(); --it ) {
00166     if (((Kate::Document*)it.current()->getDoc())->documentNumber() == documentNumber) {
00167       if ( currentView() )
00168         disconnect( currentView()->getDoc(), SIGNAL(modifiedChanged()),
00169                     mStatusBar, SLOT(modifiedChanged()) );
00170 
00171       Kate::View* kv = it.current();
00172       connect( kv->getDoc(), SIGNAL(modifiedChanged()),
00173                mStatusBar, SLOT(modifiedChanged()) );
00174 
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 active, bool )
00200 {
00201   mIsActiveSpace = active;
00202 
00203   // change the statusbar palette and make sure it gets updated
00204   QPalette pal( palette() );
00205   if ( ! active )
00206   {
00207     pal.setColor( QColorGroup::Background, pal.active().mid() );
00208     pal.setColor( QColorGroup::Light, pal.active().midlight() );
00209   }
00210 
00211   mStatusBar->setPalette( pal );
00212   mStatusBar->update();
00213   sep->update();
00214 }
00215 
00216 bool KateViewSpace::event( QEvent *e )
00217 {
00218   if ( e->type() == QEvent::PaletteChange )
00219   {
00220     setActive( mIsActiveSpace );
00221     return true;
00222   }
00223   return QVBox::event( e );
00224 }
00225 
00226 void KateViewSpace::slotStatusChanged (Kate::View *view, int r, int c, int ovr, bool block, int mod, const QString &msg)
00227 {
00228   if ((QWidgetStack *)view->parentWidget() != stack)
00229     return;
00230   mStatusBar->setStatus( r, c, ovr, block, mod, msg );
00231 }
00232 
00233 void KateViewSpace::saveConfig ( KConfig* config, int myIndex ,const QString& viewConfGrp)
00234 {
00235 //   kdDebug()<<"KateViewSpace::saveConfig("<<myIndex<<", "<<viewConfGrp<<") - currentView: "<<currentView()<<")"<<endl;
00236   QString group = QString(viewConfGrp+"-ViewSpace %1").arg( myIndex );
00237 
00238   config->setGroup (group);
00239   config->writeEntry ("Count", mViewList.count());
00240 
00241   if (currentView())
00242     config->writeEntry( "Active View", currentView()->getDoc()->url().prettyURL() );
00243 
00244   // Save file list, includeing cursor position in this instance.
00245   QPtrListIterator<Kate::View> it(mViewList);
00246 
00247   int idx = 0;
00248   for (; it.current(); ++it)
00249   {
00250     if ( !it.current()->getDoc()->url().isEmpty() )
00251     {
00252       config->setGroup( group );
00253       config->writeEntry( QString("View %1").arg( idx ), it.current()->getDoc()->url().prettyURL() );
00254 
00255       // view config, group: "ViewSpace <n> url"
00256       QString vgroup = QString("%1 %2").arg(group).arg(it.current()->getDoc()->url().prettyURL());
00257       config->setGroup( vgroup );
00258       it.current()->writeSessionConfig( config );
00259     }
00260 
00261     idx++;
00262   }
00263 }
00264 
00265 void KateViewSpace::modifiedOnDisc(Kate::Document *, bool, unsigned char)
00266 {
00267   if ( currentView() )
00268     mStatusBar->updateMod( currentView()->getDoc()->isModified() );
00269 }
00270 
00271 void KateViewSpace::restoreConfig ( KateViewSpaceContainer *viewMan, KConfig* config, const QString &group )
00272 {
00273   config->setGroup (group);
00274   QString fn = config->readEntry( "Active View" );
00275 
00276   if ( !fn.isEmpty() )
00277   {
00278     Kate::Document *doc = KateDocManager::self()->findDocumentByUrl (KURL(fn));
00279 
00280     if (doc)
00281     {
00282       // view config, group: "ViewSpace <n> url"
00283       QString vgroup = QString("%1 %2").arg(group).arg(fn);
00284       config->setGroup( vgroup );
00285 
00286       viewMan->createView (doc);
00287 
00288       Kate::View *v = viewMan->activeView ();
00289 
00290       if (v)
00291         v->readSessionConfig( config );
00292     }
00293   }
00294 
00295   if (mViewList.isEmpty())
00296     viewMan->createView (KateDocManager::self()->document(0));
00297 
00298   m_group = group; // used for restroing view configs later
00299 }
00300 //END KateViewSpace
00301 
00302 //BEGIN KateVSStatusBar
00303 KateVSStatusBar::KateVSStatusBar ( KateViewSpace *parent, const char *name )
00304   : KStatusBar( parent, name ),
00305     m_viewSpace( parent )
00306 {
00307   m_lineColLabel = new QLabel( this );
00308   addWidget( m_lineColLabel, 0, false );
00309   m_lineColLabel->setAlignment( Qt::AlignCenter );
00310   m_lineColLabel->installEventFilter( this );
00311 
00312   m_modifiedLabel = new QLabel( QString("   "), this );
00313   addWidget( m_modifiedLabel, 0, false );
00314   m_modifiedLabel->setAlignment( Qt::AlignCenter );
00315   m_modifiedLabel->installEventFilter( this );
00316 
00317   m_insertModeLabel = new QLabel( i18n(" INS "), this );
00318   addWidget( m_insertModeLabel, 0, false );
00319   m_insertModeLabel->setAlignment( Qt::AlignCenter );
00320   m_insertModeLabel->installEventFilter( this );
00321 
00322   m_selectModeLabel = new QLabel( i18n(" NORM "), this );
00323   addWidget( m_selectModeLabel, 0, false );
00324   m_selectModeLabel->setAlignment( Qt::AlignCenter );
00325   m_selectModeLabel->installEventFilter( this );
00326 
00327   m_fileNameLabel=new KSqueezedTextLabel( this );
00328   addWidget( m_fileNameLabel, 1, true );
00329   m_fileNameLabel->setMinimumSize( 0, 0 );
00330   m_fileNameLabel->setSizePolicy(QSizePolicy( QSizePolicy::Ignored, QSizePolicy::Fixed ));
00331   m_fileNameLabel->setAlignment( /*Qt::AlignRight*/Qt::AlignLeft );
00332   m_fileNameLabel->installEventFilter( this );
00333 
00334   installEventFilter( this );
00335   m_modPm = SmallIcon("modified");
00336   m_modDiscPm = SmallIcon("modonhd");
00337   m_modmodPm = SmallIcon("modmod");
00338   m_noPm = SmallIcon("null");
00339 }
00340 
00341 KateVSStatusBar::~KateVSStatusBar ()
00342 {
00343 }
00344 
00345 void KateVSStatusBar::setStatus( int r, int c, int ovr, bool block, int mod, const QString &msg )
00346 {
00347   m_lineColLabel->setText(
00348     i18n(" Line: %1 Col: %2 ").arg(KGlobal::locale()->formatNumber(r+1, 0))
00349                               .arg(KGlobal::locale()->formatNumber(c+1, 0)) );
00350 
00351   if (ovr == 0)
00352     m_insertModeLabel->setText( i18n(" R/O ") );
00353   else if (ovr == 1)
00354     m_insertModeLabel->setText( i18n(" OVR ") );
00355   else if (ovr == 2)
00356     m_insertModeLabel->setText( i18n(" INS ") );
00357 
00358 //   updateMod( mod );
00359 
00360   m_selectModeLabel->setText( block ? i18n(" BLK ") : i18n(" NORM ") );
00361 
00362   m_fileNameLabel->setText( msg );
00363 }
00364 
00365 void KateVSStatusBar::updateMod( bool mod )
00366 {
00367   Kate::View *v = m_viewSpace->currentView();
00368   if ( v )
00369   {
00370     const KateDocumentInfo *info
00371       = KateDocManager::self()->documentInfo ( v->getDoc() );
00372 
00373     m_modifiedLabel->setPixmap(
00374         mod ?
00375           info && info->modifiedOnDisc ?
00376             m_modmodPm :
00377             m_modPm :
00378           info && info->modifiedOnDisc ?
00379             m_modDiscPm :
00380         m_noPm
00381         );
00382   }
00383 }
00384 
00385 void KateVSStatusBar::modifiedChanged()
00386 {
00387   Kate::View *v = m_viewSpace->currentView();
00388   if ( v )
00389     updateMod( v->getDoc()->isModified() );
00390 }
00391 
00392 void KateVSStatusBar::showMenu()
00393 {
00394    KMainWindow* mainWindow = static_cast<KMainWindow*>( topLevelWidget() );
00395    QPopupMenu* menu = static_cast<QPopupMenu*>( mainWindow->factory()->container("viewspace_popup", mainWindow ) );
00396 
00397    if (menu)
00398      menu->exec(QCursor::pos());
00399 }
00400 
00401 bool KateVSStatusBar::eventFilter(QObject*,QEvent *e)
00402 {
00403   if (e->type()==QEvent::MouseButtonPress)
00404   {
00405     if ( m_viewSpace->currentView() )
00406       m_viewSpace->currentView()->setFocus();
00407 
00408     if ( ((QMouseEvent*)e)->button()==RightButton)
00409       showMenu();
00410 
00411     return true;
00412   }
00413 
00414   return false;
00415 }
00416 //END KateVSStatusBar
00417 // kate: space-indent on; indent-width 2; replace-tabs on;
KDE Logo
This file is part of the documentation for kate Library Version 3.4.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Jun 14 01:19:51 2006 by doxygen 1.4.0 written by Dimitri van Heesch, © 1997-2003