kspread Library API Documentation

kspread_dlg_styles.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003 Laurent Montel <montel@kde.org>
00003              (C) 2003 Norbert Andres <nandres@web.de>  
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
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 <kcombobox.h>
00022 #include <kdebug.h>
00023 #include <klistview.h>
00024 #include <klocale.h>
00025 
00026 #include "kspread_canvas.h"
00027 #include "kspread_cell.h"
00028 #include "kspread_dlg_layout.h"
00029 #include "kspread_dlg_styles.h"
00030 #include "kspread_sheet.h"
00031 #include "kspread_style.h"
00032 #include "kspread_style_manager.h"
00033 #include "kspread_view.h"
00034 
00035 #include <qheader.h>
00036 #include <qlayout.h>
00037 #include <qmap.h>
00038 
00039 StyleWidget::StyleWidget( QWidget * parent, const char * name, WFlags fl )
00040   : QWidget( parent, name, fl )
00041 {
00042   QVBoxLayout * layout = new QVBoxLayout( this, 11, 6, "layout");
00043 
00044   m_styleList = new KListView( this, "m_styleList" );
00045   m_styleList->addColumn( i18n( "Styles" ) );
00046   m_styleList->setResizeMode( KListView::AllColumns );
00047   layout->addWidget( m_styleList );
00048 
00049   m_displayBox = new KComboBox( FALSE, this, "m_displayBox" );
00050   layout->addWidget( m_displayBox );
00051 
00052   m_styleList->header()->setLabel( 0, i18n( "Styles" ) );
00053   m_displayBox->clear();
00054   m_displayBox->insertItem( i18n( "All Styles" ) );
00055   m_displayBox->insertItem( i18n( "Applied Styles" ) );
00056   m_displayBox->insertItem( i18n( "Custom Styles" ) );
00057   m_displayBox->insertItem( i18n( "Hierarchical" ) );
00058   connect( m_styleList, SIGNAL(doubleClicked ( QListViewItem *)),this, SIGNAL( modifyStyle()));
00059   resize( QSize(446, 384).expandedTo(minimumSizeHint()) );
00060 }
00061 
00062 StyleWidget::~StyleWidget()
00063 {
00064 }
00065 
00066 
00067 
00068 KSpreadStyleDlg::KSpreadStyleDlg( KSpreadView * parent, KSpreadStyleManager * manager,
00069                                   const char * name )
00070   : KDialogBase( parent, name, true, "",
00071                  KDialogBase::Ok | KDialogBase::User1 | KDialogBase::User2 | KDialogBase::User3 | KDialogBase::Close,
00072                  KDialogBase::Ok, false, KGuiItem( i18n( "&New..." ) ), KGuiItem( i18n( "&Modify..." ) ), KGuiItem( i18n( "&Delete" ) ) ),
00073     m_view( parent ),
00074     m_styleManager( manager ),
00075     m_dlg( new StyleWidget( this ) )
00076 {
00077   setCaption( i18n( "Style Manager" ) );
00078   setButtonBoxOrientation( Vertical );
00079   setMainWidget( m_dlg );
00080 
00081   slotDisplayMode( 0 );
00082   enableButton( KDialogBase::User1, true );
00083   enableButton( KDialogBase::User2, true );
00084   enableButton( KDialogBase::User3, false );
00085 
00086   connect( m_dlg->m_styleList, SIGNAL( selectionChanged( QListViewItem * ) ),
00087            this, SLOT( slotSelectionChanged( QListViewItem * ) ) );
00088   connect( m_dlg->m_displayBox, SIGNAL( activated( int ) ), this, SLOT( slotDisplayMode( int ) ) );
00089   connect( this, SIGNAL( user3Clicked() ), this, SLOT( slotUser3() ) );
00090   connect( m_dlg, SIGNAL( modifyStyle() ), this, SLOT( slotUser2()));
00091 }
00092 
00093 KSpreadStyleDlg::~KSpreadStyleDlg()
00094 {
00095 }
00096 
00097 void KSpreadStyleDlg::fillComboBox()
00098 {
00099   class Map : public QMap<KSpreadCustomStyle *, KListViewItem *> {};
00100   Map entries;
00101 
00102   entries.clear();
00103   entries[m_styleManager->defaultStyle()] = new KListViewItem( m_dlg->m_styleList, i18n( "Default" ) );
00104 
00105   KSpreadStyleManager::Styles::const_iterator iter = m_styleManager->m_styles.begin();
00106   KSpreadStyleManager::Styles::const_iterator end  = m_styleManager->m_styles.end();
00107   uint count = m_styleManager->m_styles.count() + 1;
00108 
00109   while ( entries.count() != count )
00110   {
00111     if ( entries.find( iter.data() ) == entries.end() )
00112     {
00113       if ( iter.data()->parent() == 0 )
00114         entries[iter.data()] = new KListViewItem( m_dlg->m_styleList, iter.data()->name() );
00115       else
00116       {
00117         Map::const_iterator i = entries.find( iter.data()->parent() );
00118         if ( i != entries.end() )
00119           entries[iter.data()] = new KListViewItem( i.data(), iter.data()->name() );
00120       }
00121     }
00122 
00123     ++iter;
00124     if ( iter == end )
00125       iter = m_styleManager->m_styles.begin();
00126   }
00127   entries.clear();
00128 }
00129 
00130 void KSpreadStyleDlg::slotDisplayMode( int mode )
00131 {
00132   m_dlg->m_styleList->clear();
00133 
00134   if ( mode != 3 )
00135     m_dlg->m_styleList->setRootIsDecorated( false );
00136   else
00137   {
00138     m_dlg->m_styleList->setRootIsDecorated( true );
00139     fillComboBox();
00140     return;
00141   }
00142 
00143   if ( mode != 2 )
00144     new KListViewItem( m_dlg->m_styleList, i18n( "Default" ) );
00145 
00146   KSpreadStyleManager::Styles::iterator iter = m_styleManager->m_styles.begin();
00147   KSpreadStyleManager::Styles::iterator end  = m_styleManager->m_styles.end();
00148 
00149   while ( iter != end )
00150   {
00151     KSpreadCustomStyle * styleData = iter.data();
00152     if ( !styleData || styleData->name().isEmpty() )
00153     {
00154       ++iter;
00155       continue;
00156     }
00157 
00158     if ( mode == 2 )
00159     {
00160       if ( styleData->type() == KSpreadStyle::CUSTOM )
00161         new KListViewItem( m_dlg->m_styleList, styleData->name() );
00162     }
00163     else if ( mode == 1 )
00164     {
00165       if ( styleData->usage() > 0 )
00166         new KListViewItem( m_dlg->m_styleList, styleData->name() );
00167     }
00168     else
00169       new KListViewItem( m_dlg->m_styleList, styleData->name() );
00170 
00171     ++iter;
00172   }
00173 }
00174 
00175 void KSpreadStyleDlg::slotOk()
00176 {
00177   KListViewItem * item = (KListViewItem *) m_dlg->m_styleList->currentItem();
00178 
00179   if ( !item )
00180   {
00181     accept();
00182     return;
00183   }
00184 
00185   KSpreadCustomStyle * s = 0;
00186 
00187   QString name( item->text( 0 ) );
00188   if ( name == i18n( "Default" ) )
00189     s = m_styleManager->defaultStyle();
00190   else
00191     s = m_styleManager->style( name );
00192 
00193   if ( !s )
00194   {
00195     accept();
00196     return;
00197   }
00198 
00199   if ( m_view )
00200   {
00201     KSpreadSheet * sheet = m_view->activeSheet();
00202 
00203     if ( sheet )
00204     {
00205       m_view->doc()->emitBeginOperation( false );
00206       sheet->setSelectionStyle( m_view->selectionInfo(), s );
00207     }
00208   }
00209 
00210   m_view->slotUpdateView( m_view->activeSheet() );
00211   accept();
00212 }
00213 
00214 void KSpreadStyleDlg::slotUser1()
00215 {
00216   KSpreadCustomStyle * s = 0;
00217 
00218   KListViewItem * item = (KListViewItem *) m_dlg->m_styleList->currentItem();
00219 
00220   if ( item )
00221   {
00222     QString name( item->text( 0 ) );
00223     if ( name == i18n( "Default" ) )
00224       s = m_styleManager->defaultStyle();
00225     else
00226       s = m_styleManager->style( name );
00227   }
00228   else
00229     s = m_styleManager->defaultStyle();
00230 
00231   int i = 1;
00232   QString newName( i18n( "style%1" ).arg( m_styleManager->count() + i ) );
00233   while ( m_styleManager->style( newName ) != 0 )
00234   {
00235     ++i;
00236     newName = i18n( "style%1" ).arg( m_styleManager->count() + i );
00237   }
00238 
00239   KSpreadCustomStyle * style = new KSpreadCustomStyle( newName, s );
00240   style->setType( KSpreadStyle::TENTATIVE );
00241 
00242   CellFormatDlg dlg( m_view, style, m_styleManager, m_view->doc() );
00243 
00244   if ( style->type() == KSpreadStyle::TENTATIVE )
00245   {
00246     delete style;
00247     return;
00248   }
00249 
00250   m_styleManager->m_styles[ style->name() ] = style;
00251 
00252   slotDisplayMode( m_dlg->m_displayBox->currentItem() );
00253 }
00254 
00255 void KSpreadStyleDlg::slotUser2()
00256 {
00257   KListViewItem * item = (KListViewItem *) m_dlg->m_styleList->currentItem();
00258 
00259   if ( !item )
00260     return;
00261 
00262   KSpreadCustomStyle * s = 0;
00263 
00264   QString name( item->text( 0 ) );
00265   if ( name == i18n( "Default" ) )
00266     s = m_styleManager->defaultStyle();
00267   else
00268     s = m_styleManager->style( name );
00269 
00270   if ( !s )
00271     return;
00272 
00273   CellFormatDlg dlg( m_view, s, m_styleManager, m_view->doc() );
00274 
00275   slotDisplayMode( m_dlg->m_displayBox->currentItem() );
00276 }
00277 
00278 void KSpreadStyleDlg::slotUser3()
00279 {
00280   KListViewItem * item = (KListViewItem *) m_dlg->m_styleList->currentItem();
00281 
00282   if ( !item )
00283     return;
00284 
00285   KSpreadCustomStyle * s = 0;
00286 
00287   QString name( item->text( 0 ) );
00288   if ( name == i18n( "Default" ) )
00289     s = m_styleManager->defaultStyle();
00290   else
00291     s = m_styleManager->style( name );
00292 
00293   if ( !s )
00294     return;
00295 
00296   if ( s->type() != KSpreadStyle::CUSTOM )
00297     return;
00298 
00299   s->setType( KSpreadStyle::AUTO );
00300   m_styleManager->takeStyle( s );
00301 
00302   slotDisplayMode( m_dlg->m_displayBox->currentItem() );
00303 }
00304 
00305 void KSpreadStyleDlg::slotSelectionChanged( QListViewItem * item )
00306 {
00307   if ( !item )
00308     return;
00309 
00310   KSpreadCustomStyle * style = m_styleManager->style( item->text( 0 ) );
00311   if ( !style )
00312   {
00313     enableButton( KDialogBase::User3, false );
00314     return;
00315   }
00316 
00317   if ( style->type() == KSpreadStyle::BUILTIN )
00318     enableButton( KDialogBase::User3, false );
00319   else
00320     enableButton( KDialogBase::User3, true );
00321 }
00322 
00323 
00324 #include "kspread_dlg_styles.moc"
00325 
KDE Logo
This file is part of the documentation for kspread Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:43:02 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003