00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <qcombobox.h>
00027 #include <qfile.h>
00028 #include <qlayout.h>
00029 #include <qlabel.h>
00030 #include <qpushbutton.h>
00031
00032 #include <kbuttonbox.h>
00033 #include <kmessagebox.h>
00034 #include <kstandarddirs.h>
00035 #include <ksimpleconfig.h>
00036
00037 #include "kspread_dlg_format.h"
00038 #include "kspread_doc.h"
00039 #include "kspread_locale.h"
00040 #include "kspread_sheet.h"
00041 #include "kspread_style.h"
00042 #include "kspread_style_manager.h"
00043 #include "kspread_undo.h"
00044 #include "kspread_view.h"
00045 #include "selection.h"
00046
00047
00048 using namespace KSpread;
00049
00050 FormatDialog::FormatDialog( View* view, const char* name )
00051 : KDialogBase( view, name, TRUE,i18n("Sheet Style"),Ok|Cancel )
00052 {
00053 for( int i = 0; i < 16; ++i )
00054 m_cells[ i ] = 0;
00055
00056 m_view = view;
00057 QWidget *page = new QWidget( this );
00058 setMainWidget(page);
00059 QVBoxLayout *vbox = new QVBoxLayout( page, 0, spacingHint() );
00060
00061 QLabel *toplabel = new QLabel( i18n("Select the sheet style to apply:"), page );
00062 m_combo = new QComboBox( page );
00063 m_label = new QLabel( page );
00064
00065 vbox->addWidget( toplabel );
00066 vbox->addWidget( m_combo );
00067 vbox->addWidget( m_label );
00068
00069
00070 QStringList lst = Factory::global()->dirs()->findAllResources( "sheet-styles", "*.ksts", TRUE );
00071
00072 QStringList::Iterator it = lst.begin();
00073 for( ; it != lst.end(); ++it )
00074 {
00075 KSimpleConfig cfg( *it, TRUE );
00076 cfg.setGroup( "Sheet-Style" );
00077
00078 Entry e;
00079 e.config = *it;
00080 e.xml = cfg.readEntry( "XML" );
00081 e.image = cfg.readEntry( "Image" );
00082 e.name = cfg.readEntry( "Name" );
00083
00084 m_entries.append( e );
00085
00086 m_combo->insertItem( e.name );
00087 }
00088
00089 slotActivated( 0 );
00090
00091 connect( this, SIGNAL( okClicked() ), this, SLOT( slotOk() ) );
00092 connect( m_combo, SIGNAL( activated( int ) ), this, SLOT( slotActivated( int ) ) );
00093 }
00094
00095 FormatDialog::~FormatDialog()
00096 {
00097 for( int i = 0; i < 16; ++i )
00098 delete m_cells[ i ];
00099 }
00100
00101 void FormatDialog::slotActivated( int index )
00102 {
00103 enableButtonOK(true);
00104
00105 QString img = Factory::global()->dirs()->findResource( "sheet-styles", m_entries[ index ].image );
00106 if ( img.isEmpty() )
00107 {
00108 QString str( i18n( "Could not find image %1." ) );
00109 str = str.arg( m_entries[ index ].image );
00110 KMessageBox::error( this, str );
00111
00112 enableButtonOK(false);
00113
00114 return;
00115 }
00116
00117 QPixmap pix( img );
00118 if ( pix.isNull() )
00119 {
00120 QString str( i18n( "Could not load image %1." ) );
00121 str = str.arg( img );
00122 KMessageBox::error( this,str );
00123
00124 enableButtonOK(false);
00125
00126 return;
00127 }
00128
00129 m_label->setPixmap( pix );
00130 }
00131
00132 void FormatDialog::slotOk()
00133 {
00134
00135 m_view->doc()->emitBeginOperation( false );
00136
00137 QString xml = Factory::global()->dirs()->findResource( "sheet-styles", m_entries[ m_combo->currentItem() ].xml );
00138 if ( xml.isEmpty() )
00139 {
00140 QString str( i18n( "Could not find sheet-style XML file '%1'." ) );
00141 str = str.arg( m_entries[ m_combo->currentItem() ].xml );
00142 KMessageBox::error( this, str );
00143 return;
00144 }
00145
00146 QFile file( xml );
00147 file.open( IO_ReadOnly );
00148 QDomDocument doc;
00149 doc.setContent( &file );
00150 file.close();
00151
00152 if ( !parseXML( doc ) )
00153 {
00154 QString str( i18n( "Parsing error in sheet-style XML file %1." ) );
00155 str = str.arg( m_entries[ m_combo->currentItem() ].xml );
00156 KMessageBox::error( this, str );
00157 return;
00158 }
00159
00160 QRect r = m_view->selectionInfo()->selection();
00161
00162 if ( !m_view->doc()->undoLocked() )
00163 {
00164 QString title=i18n("Change Format");
00165 UndoCellFormat *undo = new UndoCellFormat( m_view->doc(), m_view->activeSheet(), r, title);
00166 m_view->doc()->addCommand( undo );
00167 }
00168
00169
00170
00171
00172
00173 Cell* cell = m_view->activeSheet()->nonDefaultCell( r.left(), r.top() );
00174 cell->format()->copy( *m_cells[0] );
00175
00176
00177 int x, y;
00178 for( x = r.left() + 1; x <= r.right(); ++x )
00179 {
00180 int pos = 1 + ( ( x - r.left() - 1 ) % 2 );
00181 Cell* cell = m_view->activeSheet()->nonDefaultCell( x, r.top() );
00182 if(!cell->isPartOfMerged())
00183 {
00184 cell->format()->copy( *m_cells[ pos ] );
00185
00186 Format* c;
00187 if ( x == r.right() )
00188 c = m_cells[2];
00189 else
00190 c = m_cells[1];
00191
00192 if ( c )
00193 cell->setTopBorderPen( c->topBorderPen( 0, 0 ) );
00194
00195 if ( x == r.left() + 1 )
00196 c = m_cells[1];
00197 else
00198 c = m_cells[2];
00199
00200 if ( c )
00201 cell->setLeftBorderPen( c->leftBorderPen( 0, 0 ) );
00202 }
00203 }
00204
00205 cell = m_view->activeSheet()->nonDefaultCell( r.right(), r.top() );
00206 if ( m_cells[3] )
00207 cell->setRightBorderPen( m_cells[3]->leftBorderPen( 0, 0 ) );
00208
00209
00210 for( y = r.top() + 1; y <= r.bottom(); ++y )
00211 {
00212 int pos = 4 + ( ( y - r.top() - 1 ) % 2 ) * 4;
00213 Cell* cell = m_view->activeSheet()->nonDefaultCell( r.left(), y );
00214 if(!cell->isPartOfMerged())
00215 {
00216 cell->format()->copy( *m_cells[ pos ] );
00217
00218 Format* c;
00219 if ( y == r.bottom() )
00220 c = m_cells[8];
00221 else
00222 c = m_cells[4];
00223
00224 if ( c )
00225 cell->setLeftBorderPen( c->leftBorderPen( 0, 0 ) );
00226
00227 if ( y == r.top() + 1 )
00228 c = m_cells[4];
00229 else
00230 c = m_cells[8];
00231
00232 if ( c )
00233 cell->setTopBorderPen( c->topBorderPen( 0, 0 ) );
00234 }
00235 }
00236
00237
00238 for( x = r.left() + 1; x <= r.right(); ++x )
00239 for( y = r.top() + 1; y <= r.bottom(); ++y )
00240 {
00241 int pos = 5 + ( ( y - r.top() - 1 ) % 2 ) * 4 + ( ( x - r.left() - 1 ) % 2 );
00242 Cell* cell = m_view->activeSheet()->nonDefaultCell( x, y );
00243 if(!cell->isPartOfMerged())
00244 {
00245 cell->format()->copy( *m_cells[ pos ] );
00246
00247 Format* c;
00248 if ( x == r.left() + 1 )
00249 c = m_cells[ 5 + ( ( y - r.top() - 1 ) % 2 ) * 4 ];
00250 else
00251 c = m_cells[ 6 + ( ( y - r.top() - 1 ) % 2 ) * 4 ];
00252
00253 if ( c )
00254 cell->setLeftBorderPen( c->leftBorderPen( 0, 0 ) );
00255
00256 if ( y == r.top() + 1 )
00257 c = m_cells[ 5 + ( ( x - r.left() - 1 ) % 2 ) ];
00258 else
00259 c = m_cells[ 9 + ( ( x - r.left() - 1 ) % 2 ) ];
00260
00261 if ( c )
00262 cell->setTopBorderPen( c->topBorderPen( 0, 0 ) );
00263 }
00264 }
00265
00266
00267 for( y = r.top(); y <= r.bottom(); ++y )
00268 {
00269 Cell* cell = m_view->activeSheet()->nonDefaultCell( r.right(), y );
00270 if(!cell->isPartOfMerged())
00271 {
00272 if ( y == r.top() )
00273 {
00274 if ( m_cells[3] )
00275 cell->setRightBorderPen( m_cells[3]->leftBorderPen( 0, 0 ) );
00276 }
00277 else if ( y == r.right() )
00278 {
00279 if ( m_cells[11] )
00280 cell->setRightBorderPen( m_cells[11]->leftBorderPen( 0, 0 ) );
00281 }
00282 else
00283 {
00284 if ( m_cells[7] )
00285 cell->setRightBorderPen( m_cells[7]->leftBorderPen( 0, 0 ) );
00286 }
00287 }
00288 }
00289
00290
00291 for( x = r.left(); x <= r.right(); ++x )
00292 {
00293 Cell* cell = m_view->activeSheet()->nonDefaultCell( x, r.bottom() );
00294 if(!cell->isPartOfMerged())
00295 {
00296 if ( x == r.left() )
00297 {
00298 if ( m_cells[12] )
00299 cell->setBottomBorderPen( m_cells[12]->topBorderPen( 0, 0 ) );
00300 }
00301 else if ( x == r.right() )
00302 {
00303 if ( m_cells[14] )
00304 cell->setBottomBorderPen( m_cells[14]->topBorderPen( 0, 0 ) );
00305 }
00306 else
00307 {
00308 if ( m_cells[13] )
00309 cell->setBottomBorderPen( m_cells[13]->topBorderPen( 0, 0 ) );
00310 }
00311 }
00312 }
00313
00314 m_view->selectionInfo()->initialize(r);
00315 m_view->doc()->setModified( true );
00316 m_view->slotUpdateView( m_view->activeSheet() );
00317 accept();
00318 }
00319
00320 bool FormatDialog::parseXML( const QDomDocument& doc )
00321 {
00322 for( int i = 0; i < 16; ++i )
00323 {
00324 delete m_cells[ i ];
00325 m_cells[ i ] = 0;
00326 }
00327
00328 QDomElement e = doc.documentElement().firstChild().toElement();
00329 for( ; !e.isNull(); e = e.nextSibling().toElement() )
00330 {
00331 if ( e.tagName() == "cell" )
00332 {
00333 Sheet* sheet = m_view->activeSheet();
00334 Format* cell = new Format( sheet, sheet->doc()->styleManager()->defaultStyle() );
00335
00336 if ( !cell->load( e.namedItem("format").toElement(), Paste::Normal ) )
00337 return false;
00338
00339 int row = e.attribute("row").toInt();
00340 int column = e.attribute("column").toInt();
00341 int i = (row-1)*4 + (column-1);
00342 if ( i < 0 || i >= 16 )
00343 return false;
00344
00345 m_cells[ i ] = cell;
00346 }
00347 }
00348
00349 return TRUE;
00350 }
00351
00352 #include "kspread_dlg_format.moc"