kspread

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., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include <qheader.h>
00022 #include <qlayout.h>
00023 #include <qmap.h>
00024 
00025 #include <kcombobox.h>
00026 #include <kdebug.h>
00027 #include <klistview.h>
00028 #include <klocale.h>
00029 
00030 #include "kspread_canvas.h"
00031 #include "kspread_cell.h"
00032 #include "kspread_dlg_layout.h"
00033 #include "kspread_sheet.h"
00034 #include "kspread_style.h"
00035 #include "kspread_style_manager.h"
00036 #include "kspread_view.h"
00037 
00038 #include "kspread_dlg_styles.h"
00039 
00040 using namespace KSpread;
00041 
00042 StyleWidget::StyleWidget( QWidget * parent, const char * name, WFlags fl )
00043   : QWidget( parent, name, fl )
00044 {
00045   QVBoxLayout * layout = new QVBoxLayout( this, 11, 6, "layout");
00046 
00047   m_styleList = new KListView( this, "m_styleList" );
00048   m_styleList->addColumn( i18n( "Styles" ) );
00049   m_styleList->setResizeMode( KListView::AllColumns );
00050   layout->addWidget( m_styleList );
00051 
00052   m_displayBox = new KComboBox( FALSE, this, "m_displayBox" );
00053   layout->addWidget( m_displayBox );
00054 
00055   m_styleList->header()->setLabel( 0, i18n( "Styles" ) );
00056   m_displayBox->clear();
00057   m_displayBox->insertItem( i18n( "All Styles" ) );
00058   m_displayBox->insertItem( i18n( "Applied Styles" ) );
00059   m_displayBox->insertItem( i18n( "Custom Styles" ) );
00060   m_displayBox->insertItem( i18n( "Hierarchical" ) );
00061   connect( m_styleList, SIGNAL(doubleClicked ( QListViewItem *)),this, SIGNAL( modifyStyle()));
00062   resize( QSize(446, 384).expandedTo(minimumSizeHint()) );
00063 }
00064 
00065 StyleWidget::~StyleWidget()
00066 {
00067 }
00068 
00069 
00070 
00071 StyleDlg::StyleDlg( View * parent, StyleManager * manager,
00072                                   const char * name )
00073   : KDialogBase( parent, name, true, "",
00074                  KDialogBase::Ok | KDialogBase::User1 | KDialogBase::User2 | KDialogBase::User3 | KDialogBase::Close,
00075                  KDialogBase::Ok, false, KGuiItem( i18n( "&New..." ) ), KGuiItem( i18n( "&Modify..." ) ), KGuiItem( i18n( "&Delete" ) ) ),
00076     m_view( parent ),
00077     m_styleManager( manager ),
00078     m_dlg( new StyleWidget( this ) )
00079 {
00080   setCaption( i18n( "Style Manager" ) );
00081   setButtonBoxOrientation( Vertical );
00082   setMainWidget( m_dlg );
00083 
00084   slotDisplayMode( 0 );
00085   enableButton( KDialogBase::User1, true );
00086   enableButton( KDialogBase::User2, true );
00087   enableButton( KDialogBase::User3, false );
00088 
00089   connect( m_dlg->m_styleList, SIGNAL( selectionChanged( QListViewItem * ) ),
00090            this, SLOT( slotSelectionChanged( QListViewItem * ) ) );
00091   connect( m_dlg->m_displayBox, SIGNAL( activated( int ) ), this, SLOT( slotDisplayMode( int ) ) );
00092   connect( this, SIGNAL( user3Clicked() ), this, SLOT( slotUser3() ) );
00093   connect( m_dlg, SIGNAL( modifyStyle() ), this, SLOT( slotUser2()));
00094 }
00095 
00096 StyleDlg::~StyleDlg()
00097 {
00098 }
00099 
00100 void StyleDlg::fillComboBox()
00101 {
00102   class Map : public QMap<CustomStyle *, KListViewItem *> {};
00103   Map entries;
00104 
00105   entries.clear();
00106   entries[m_styleManager->defaultStyle()] = new KListViewItem( m_dlg->m_styleList, i18n( "Default" ) );
00107 
00108   StyleManager::Styles::const_iterator iter = m_styleManager->m_styles.begin();
00109   StyleManager::Styles::const_iterator end  = m_styleManager->m_styles.end();
00110   uint count = m_styleManager->m_styles.count() + 1;
00111 
00112   while ( entries.count() != count )
00113   {
00114     if ( entries.find( iter.data() ) == entries.end() )
00115     {
00116       if ( iter.data()->parent() == 0 )
00117         entries[iter.data()] = new KListViewItem( m_dlg->m_styleList, iter.data()->name() );
00118       else
00119       {
00120         Map::const_iterator i = entries.find( iter.data()->parent() );
00121         if ( i != entries.end() )
00122           entries[iter.data()] = new KListViewItem( i.data(), iter.data()->name() );
00123       }
00124     }
00125 
00126     ++iter;
00127     if ( iter == end )
00128       iter = m_styleManager->m_styles.begin();
00129   }
00130   entries.clear();
00131 }
00132 
00133 void StyleDlg::slotDisplayMode( int mode )
00134 {
00135   m_dlg->m_styleList->clear();
00136 
00137   if ( mode != 3 )
00138     m_dlg->m_styleList->setRootIsDecorated( false );
00139   else
00140   {
00141     m_dlg->m_styleList->setRootIsDecorated( true );
00142     fillComboBox();
00143     return;
00144   }
00145 
00146   if ( mode != 2 )
00147     new KListViewItem( m_dlg->m_styleList, i18n( "Default" ) );
00148 
00149   StyleManager::Styles::iterator iter = m_styleManager->m_styles.begin();
00150   StyleManager::Styles::iterator end  = m_styleManager->m_styles.end();
00151 
00152   while ( iter != end )
00153   {
00154     CustomStyle * styleData = iter.data();
00155     if ( !styleData || styleData->name().isEmpty() )
00156     {
00157       ++iter;
00158       continue;
00159     }
00160 
00161     if ( mode == 2 )
00162     {
00163       if ( styleData->type() == Style::CUSTOM )
00164         new KListViewItem( m_dlg->m_styleList, styleData->name() );
00165     }
00166     else if ( mode == 1 )
00167     {
00168       if ( styleData->usage() > 0 )
00169         new KListViewItem( m_dlg->m_styleList, styleData->name() );
00170     }
00171     else
00172       new KListViewItem( m_dlg->m_styleList, styleData->name() );
00173 
00174     ++iter;
00175   }
00176 }
00177 
00178 void StyleDlg::slotOk()
00179 {
00180   KListViewItem * item = (KListViewItem *) m_dlg->m_styleList->currentItem();
00181 
00182   if ( !item )
00183   {
00184     accept();
00185     return;
00186   }
00187 
00188   CustomStyle * s = 0;
00189 
00190   QString name( item->text( 0 ) );
00191   if ( name == i18n( "Default" ) )
00192     s = m_styleManager->defaultStyle();
00193   else
00194     s = m_styleManager->style( name );
00195 
00196   if ( !s )
00197   {
00198     accept();
00199     return;
00200   }
00201 
00202   if ( m_view )
00203   {
00204     Sheet * sheet = m_view->activeSheet();
00205 
00206     if ( sheet )
00207     {
00208       m_view->doc()->emitBeginOperation( false );
00209       sheet->setSelectionStyle( m_view->selectionInfo(), s );
00210     }
00211   }
00212 
00213   m_view->slotUpdateView( m_view->activeSheet() );
00214   accept();
00215 }
00216 
00217 void StyleDlg::slotUser1()
00218 {
00219   CustomStyle * s = 0;
00220 
00221   KListViewItem * item = (KListViewItem *) m_dlg->m_styleList->currentItem();
00222 
00223   if ( item )
00224   {
00225     QString name( item->text( 0 ) );
00226     if ( name == i18n( "Default" ) )
00227       s = m_styleManager->defaultStyle();
00228     else
00229       s = m_styleManager->style( name );
00230   }
00231   else
00232     s = m_styleManager->defaultStyle();
00233 
00234   int i = 1;
00235   QString newName( i18n( "style%1" ).arg( m_styleManager->count() + i ) );
00236   while ( m_styleManager->style( newName ) != 0 )
00237   {
00238     ++i;
00239     newName = i18n( "style%1" ).arg( m_styleManager->count() + i );
00240   }
00241 
00242   CustomStyle * style = new CustomStyle( newName, s );
00243   style->setType( Style::TENTATIVE );
00244 
00245   CellFormatDialog dlg( m_view, style, m_styleManager, m_view->doc() );
00246 
00247   if ( style->type() == Style::TENTATIVE )
00248   {
00249     delete style;
00250     return;
00251   }
00252 
00253   m_styleManager->m_styles[ style->name() ] = style;
00254 
00255   slotDisplayMode( m_dlg->m_displayBox->currentItem() );
00256 }
00257 
00258 void StyleDlg::slotUser2()
00259 {
00260   KListViewItem * item = (KListViewItem *) m_dlg->m_styleList->currentItem();
00261 
00262   if ( !item )
00263     return;
00264 
00265   CustomStyle * s = 0;
00266 
00267   QString name( item->text( 0 ) );
00268   if ( name == i18n( "Default" ) )
00269     s = m_styleManager->defaultStyle();
00270   else
00271     s = m_styleManager->style( name );
00272 
00273   if ( !s )
00274     return;
00275 
00276   CellFormatDialog dlg( m_view, s, m_styleManager, m_view->doc() );
00277   slotDisplayMode( m_dlg->m_displayBox->currentItem() );
00278 }
00279 
00280 void StyleDlg::slotUser3()
00281 {
00282   KListViewItem * item = (KListViewItem *) m_dlg->m_styleList->currentItem();
00283 
00284   if ( !item )
00285     return;
00286 
00287   CustomStyle * s = 0;
00288 
00289   QString name( item->text( 0 ) );
00290   if ( name == i18n( "Default" ) )
00291     s = m_styleManager->defaultStyle();
00292   else
00293     s = m_styleManager->style( name );
00294 
00295   if ( !s )
00296     return;
00297 
00298   if ( s->type() != Style::CUSTOM )
00299     return;
00300 
00301   s->setType( Style::AUTO );
00302   m_styleManager->takeStyle( s );
00303 
00304   slotDisplayMode( m_dlg->m_displayBox->currentItem() );
00305 }
00306 
00307 void StyleDlg::slotSelectionChanged( QListViewItem * item )
00308 {
00309   if ( !item )
00310     return;
00311 
00312   CustomStyle* style = 0;
00313   QString name( item->text( 0 ) );
00314   if ( name == i18n( "Default" ) )
00315     style = m_styleManager->defaultStyle();
00316   else
00317     style = m_styleManager->style( name );
00318   if ( !style )
00319   {
00320     enableButton( KDialogBase::User3, false );
00321     return;
00322   }
00323 
00324   if ( style->type() == Style::BUILTIN )
00325     enableButton( KDialogBase::User3, false );
00326   else
00327     enableButton( KDialogBase::User3, true );
00328 }
00329 
00330 
00331 #include "kspread_dlg_styles.moc"
00332 
KDE Home | KDE Accessibility Home | Description of Access Keys