kspread Library API Documentation

kspread_dlg_format.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002-2003 Norbert Andres <nandres@web.de>
00003              (C) 2002 Ariya Hidayat <ariya@kde.org>
00004              (C) 2002 John Dailey <dailey@vt.edu>
00005              (C) 2001-2002 Philipp Mueller <philipp.mueller@gmx.de>
00006              (C) 2000-2002 Laurent Montel <montel@kde.org>
00007              (C) 2000 Werner Trobin <trobin@kde.org>
00008              (C) 1998-2000 Torben Weis <weis@kde.org>
00009           
00010    This library is free software; you can redistribute it and/or
00011    modify it under the terms of the GNU Library General Public
00012    License as published by the Free Software Foundation; either
00013    version 2 of the License, or (at your option) any later version.
00014 
00015    This library is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018    Library General Public License for more details.
00019 
00020    You should have received a copy of the GNU Library General Public License
00021    along with this library; see the file COPYING.LIB.  If not, write to
00022    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00023    Boston, MA 02111-1307, USA.
00024 */
00025 
00026 #include "kspread_dlg_format.h"
00027 #include "kspread_doc.h"
00028 #include "kspread_locale.h"
00029 #include "kspread_selection.h"
00030 #include "kspread_sheet.h"
00031 #include "kspread_style.h"
00032 #include "kspread_style_manager.h"
00033 #include "kspread_undo.h"
00034 #include "kspread_view.h"
00035 
00036 #include <kstandarddirs.h>
00037 #include <ksimpleconfig.h>
00038 #include <kbuttonbox.h>
00039 #include <kmessagebox.h>
00040 
00041 #include <qlayout.h>
00042 #include <qlabel.h>
00043 #include <qcombobox.h>
00044 #include <qfile.h>
00045 #include <qpushbutton.h>
00046 
00047 
00048 KSpreadFormatDlg::KSpreadFormatDlg( KSpreadView* view, const char* name )
00049     : KDialogBase( view, name, TRUE,i18n("Sheet Style"),Ok|Cancel )
00050 {
00051     for( int i = 0; i < 16; ++i )
00052     m_cells[ i ] = 0;
00053 
00054     m_view = view;
00055     QWidget *page = new QWidget( this );
00056     setMainWidget(page);
00057     QVBoxLayout *vbox = new QVBoxLayout( page, 0, spacingHint() );
00058 
00059     QLabel *toplabel = new QLabel( i18n("Select the sheet style to apply:"), page );
00060     m_combo = new QComboBox( page );
00061     m_label = new QLabel( page );
00062 
00063     vbox->addWidget( toplabel );
00064     vbox->addWidget( m_combo );
00065     vbox->addWidget( m_label );
00066 
00067 
00068     QStringList lst = KSpreadFactory::global()->dirs()->findAllResources( "table-styles", "*.ksts", TRUE );
00069 
00070     QStringList::Iterator it = lst.begin();
00071     for( ; it != lst.end(); ++it )
00072     {
00073     KSimpleConfig cfg( *it, TRUE );
00074     cfg.setGroup( "Sheet-Style" );
00075 
00076     Entry e;
00077     e.config = *it;
00078     e.xml = cfg.readEntry( "XML" );
00079     e.image = cfg.readEntry( "Image" );
00080     e.name = cfg.readEntry( "Name" );
00081 
00082     m_entries.append( e );
00083 
00084     m_combo->insertItem( e.name );
00085     }
00086 
00087     slotActivated( 0 );
00088 
00089     connect( this, SIGNAL( okClicked() ), this, SLOT( slotOk() ) );
00090     connect( m_combo, SIGNAL( activated( int ) ), this, SLOT( slotActivated( int ) ) );
00091 }
00092 
00093 KSpreadFormatDlg::~KSpreadFormatDlg()
00094 {
00095     for( int i = 0; i < 16; ++i )
00096     delete m_cells[ i ];
00097 }
00098 
00099 void KSpreadFormatDlg::slotActivated( int index )
00100 {
00101     QString img = KSpreadFactory::global()->dirs()->findResource( "table-styles", m_entries[ index ].image );
00102     if ( img.isEmpty() )
00103     {
00104     QString str( i18n( "Could not find image %1." ) );
00105     str = str.arg( m_entries[ index ].image );
00106     KMessageBox::error( this, str );
00107     return;
00108     }
00109 
00110     QPixmap pix( img );
00111     if ( pix.isNull() )
00112     {
00113     QString str( i18n( "Could not load image %1." ) );
00114     str = str.arg( img );
00115     KMessageBox::error( this,str );
00116     return;
00117     }
00118 
00119     m_label->setPixmap( pix );
00120 }
00121 
00122 void KSpreadFormatDlg::slotOk()
00123 {
00124     m_view->doc()->emitBeginOperation( false );
00125 
00126     QString xml = KSpreadFactory::global()->dirs()->findResource( "table-styles", m_entries[ m_combo->currentItem() ].xml );
00127     if ( xml.isEmpty() )
00128     {
00129     QString str( i18n( "Could not find table-style XML file '%1'." ) );
00130     str = str.arg( m_entries[ m_combo->currentItem() ].xml );
00131     KMessageBox::error( this, str );
00132     return;
00133     }
00134 
00135     QFile file( xml );
00136     file.open( IO_ReadOnly );
00137     QDomDocument doc;
00138     doc.setContent( &file );
00139     file.close();
00140 
00141     if ( !parseXML( doc ) )
00142     {
00143     QString str( i18n( "Parsing error in table-style XML file %1." ) );
00144     str = str.arg( m_entries[ m_combo->currentItem() ].xml );
00145     KMessageBox::error( this, str );
00146     return;
00147     }
00148 
00149     QRect r = m_view->selection();
00150 
00151     if ( !m_view->doc()->undoLocked() )
00152     {
00153         QString title=i18n("Change Format");
00154         KSpreadUndoCellFormat *undo = new KSpreadUndoCellFormat( m_view->doc(), m_view->activeSheet(), r ,title);
00155         m_view->doc()->addCommand( undo );
00156     }
00157     //
00158     // Set colors, borders etc.
00159     //
00160 
00161     // Top left corner
00162     KSpreadCell* cell = m_view->activeSheet()->nonDefaultCell( r.left(), r.top() );
00163     cell->copy( *m_cells[0] );
00164 
00165     // Top column
00166     int x, y;
00167     for( x = r.left() + 1; x <= r.right(); ++x )
00168     {
00169     int pos = 1 + ( ( x - r.left() - 1 ) % 2 );
00170     KSpreadCell* cell = m_view->activeSheet()->nonDefaultCell( x, r.top() );
00171         if(!cell->isObscuringForced())
00172         {
00173         cell->copy( *m_cells[ pos ] );
00174 
00175     KSpreadFormat* c;
00176     if ( x == r.right() )
00177         c = m_cells[2];
00178     else
00179         c = m_cells[1];
00180 
00181     if ( c )
00182         cell->setTopBorderPen( c->topBorderPen( 0, 0 ) );
00183 
00184     if ( x == r.left() + 1 )
00185         c = m_cells[1];
00186     else
00187         c = m_cells[2];
00188 
00189     if ( c )
00190         cell->setLeftBorderPen( c->leftBorderPen( 0, 0 ) );
00191         }
00192     }
00193 
00194     cell = m_view->activeSheet()->nonDefaultCell( r.right(), r.top() );
00195     if ( m_cells[3] )
00196     cell->setRightBorderPen( m_cells[3]->leftBorderPen( 0, 0 ) );
00197 
00198     // Left row
00199     for( y = r.top() + 1; y <= r.bottom(); ++y )
00200     {
00201     int pos = 4 + ( ( y - r.top() - 1 ) % 2 ) * 4;
00202     KSpreadCell* cell = m_view->activeSheet()->nonDefaultCell( r.left(), y );
00203         if(!cell->isObscuringForced())
00204         {
00205         cell->copy( *m_cells[ pos ] );
00206 
00207     KSpreadFormat* c;
00208     if ( y == r.bottom() )
00209         c = m_cells[8];
00210     else
00211         c = m_cells[4];
00212 
00213     if ( c )
00214         cell->setLeftBorderPen( c->leftBorderPen( 0, 0 ) );
00215 
00216     if ( y == r.top() + 1 )
00217         c = m_cells[4];
00218     else
00219         c = m_cells[8];
00220 
00221     if ( c )
00222         cell->setTopBorderPen( c->topBorderPen( 0, 0 ) );
00223         }
00224     }
00225 
00226     // Body
00227     for( x = r.left() + 1; x <= r.right(); ++x )
00228     for( y = r.top() + 1; y <= r.bottom(); ++y )
00229         {
00230         int pos = 5 + ( ( y - r.top() - 1 ) % 2 ) * 4 + ( ( x - r.left() - 1 ) % 2 );
00231         KSpreadCell* cell = m_view->activeSheet()->nonDefaultCell( x, y );
00232             if(!cell->isObscuringForced())
00233             {
00234             cell->copy( *m_cells[ pos ] );
00235 
00236         KSpreadFormat* c;
00237         if ( x == r.left() + 1 )
00238         c = m_cells[ 5 + ( ( y - r.top() - 1 ) % 2 ) * 4 ];
00239         else
00240         c = m_cells[ 6 + ( ( y - r.top() - 1 ) % 2 ) * 4 ];
00241 
00242         if ( c )
00243         cell->setLeftBorderPen( c->leftBorderPen( 0, 0 ) );
00244 
00245         if ( y == r.top() + 1 )
00246         c = m_cells[ 5 + ( ( x - r.left() - 1 ) % 2 ) ];
00247         else
00248         c = m_cells[ 9 + ( ( x - r.left() - 1 ) % 2 ) ];
00249 
00250         if ( c )
00251         cell->setTopBorderPen( c->topBorderPen( 0, 0 ) );
00252             }
00253     }
00254 
00255     // Outer right border
00256     for( y = r.top(); y <= r.bottom(); ++y )
00257     {
00258     KSpreadCell* cell = m_view->activeSheet()->nonDefaultCell( r.right(), y );
00259         if(!cell->isObscuringForced())
00260         {
00261     if ( y == r.top() )
00262         {
00263         if ( m_cells[3] )
00264         cell->setRightBorderPen( m_cells[3]->leftBorderPen( 0, 0 ) );
00265     }
00266     else if ( y == r.right() )
00267         {
00268         if ( m_cells[11] )
00269         cell->setRightBorderPen( m_cells[11]->leftBorderPen( 0, 0 ) );
00270     }
00271     else
00272         {
00273         if ( m_cells[7] )
00274         cell->setRightBorderPen( m_cells[7]->leftBorderPen( 0, 0 ) );
00275     }
00276         }
00277     }
00278 
00279     // Outer bottom border
00280     for( x = r.left(); x <= r.right(); ++x )
00281     {
00282     KSpreadCell* cell = m_view->activeSheet()->nonDefaultCell( x, r.bottom() );
00283         if(!cell->isObscuringForced())
00284         {
00285         if ( x == r.left() )
00286         {
00287         if ( m_cells[12] )
00288         cell->setBottomBorderPen( m_cells[12]->topBorderPen( 0, 0 ) );
00289     }
00290     else if ( x == r.right() )
00291         {
00292         if ( m_cells[14] )
00293         cell->setBottomBorderPen( m_cells[14]->topBorderPen( 0, 0 ) );
00294     }
00295     else
00296         {
00297         if ( m_cells[13] )
00298         cell->setBottomBorderPen( m_cells[13]->topBorderPen( 0, 0 ) );
00299     }
00300         }
00301     }
00302 
00303     m_view->selectionInfo()->setSelection( r.topLeft(), r.bottomRight(),
00304                                            m_view->activeSheet() );
00305     m_view->doc()->setModified( true );
00306     m_view->slotUpdateView( m_view->activeSheet() );
00307     accept();
00308 }
00309 
00310 bool KSpreadFormatDlg::parseXML( const QDomDocument& doc )
00311 {
00312     for( int i = 0; i < 16; ++i )
00313     {
00314     delete m_cells[ i ];
00315     m_cells[ i ] = 0;
00316     }
00317 
00318     QDomElement e = doc.documentElement().firstChild().toElement();
00319     for( ; !e.isNull(); e = e.nextSibling().toElement() )
00320     {
00321     if ( e.tagName() == "cell" )
00322         {
00323         KSpreadSheet* sheet = m_view->activeSheet();
00324         KSpreadFormat* cell = new KSpreadFormat( sheet, sheet->doc()->styleManager()->defaultStyle() );
00325 
00326         if ( !cell->load( e.namedItem("format").toElement(), Normal ) )
00327         return false;
00328 
00329         int row = e.attribute("row").toInt();
00330         int column = e.attribute("column").toInt();
00331         int i = (row-1)*4 + (column-1);
00332         if ( i < 0 || i >= 16 )
00333         return false;
00334 
00335         m_cells[ i ] = cell;
00336     }
00337     }
00338 
00339     return TRUE;
00340 }
00341 
00342 #include "kspread_dlg_format.moc"
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:42:54 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003