00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "KWCanvas.h"
00022 #include "KWTableDia.h"
00023 #include "KWTableDia.moc"
00024
00025 #include "KWTableTemplateSelector.h"
00026 #include "KWCommand.h"
00027 #include "KWDocument.h"
00028 #include "KWTableFrameSet.h"
00029 #include "KWTableTemplate.h"
00030 #include "KWPageManager.h"
00031 #include "KWPage.h"
00032 #include <kcommand.h>
00033
00034 #include <qlabel.h>
00035 #include <qlayout.h>
00036 #include <qspinbox.h>
00037 #include <qcheckbox.h>
00038 #include <qbuttongroup.h>
00039 #include <qcombobox.h>
00040
00041 #include <klocale.h>
00042
00043 #include <stdlib.h>
00044
00045
00046
00047
00048
00049
00050 void KWTablePreview::paintEvent( QPaintEvent * )
00051 {
00052 int wid = ( width() - 10 ) / cols;
00053 int hei = ( height() - 10 ) / rows;
00054
00055 QPainter p;
00056 p.begin( this );
00057
00058 p.setPen( QPen( black ) );
00059
00060 for ( int i = 0; i < rows; i++ )
00061 {
00062 for ( int j = 0; j < cols; j++ )
00063 p.drawRect( j * wid + 5, i * hei + 5, wid + 1, hei + 1 );
00064 }
00065
00066 p.end();
00067 }
00068
00069
00070
00071
00072
00073 KWTableDia::KWTableDia( QWidget* parent, const char* name, UseMode _useMode, KWCanvas *_canvas, KWDocument *_doc,
00074 int rows, int cols, CellSize wid, CellSize hei, bool floating , const QString & _templateName, int format)
00075 : KDialogBase( Tabbed, i18n("Table Settings"), Ok | Cancel, Ok, parent, name, true)
00076 {
00077 m_useMode = _useMode;
00078 canvas = _canvas;
00079 doc = _doc;
00080
00081 setupTab1( rows, cols, wid, hei, floating );
00082 setupTab2( _templateName,format );
00083
00084 setInitialSize( QSize(500, 480) );
00085
00086 oldRowCount = rows;
00087 oldColCount = cols;
00088 oldTemplateName = _templateName;
00089 #ifdef ALLOW_NON_INLINE_TABLES
00090 if ( m_useMode==NEW )
00091 {
00092 slotInlineTable( cbIsFloating->isChecked());
00093 }
00094 #endif
00095 }
00096
00097 void KWTableDia::setupTab1( int rows, int cols, CellSize wid, CellSize hei, bool floating )
00098 {
00099 tab1 = addPage( i18n( "Geometry" ) );
00100
00101 QGridLayout *grid = new QGridLayout( tab1, 9, 2, 0, KDialog::spacingHint() );
00102
00103 lRows = new QLabel( i18n( "Number of rows:" ), tab1 );
00104 grid->addWidget( lRows, 0, 0 );
00105
00106 nRows = new QSpinBox( 1, 128, 1, tab1 );
00107 nRows->setValue( rows );
00108 grid->addWidget( nRows, 1, 0 );
00109
00110 lCols = new QLabel( i18n( "Number of columns:" ), tab1 );
00111 grid->addWidget( lCols, 2, 0 );
00112
00113 nCols = new QSpinBox( 1, 128, 1, tab1 );
00114 nCols->setValue( cols );
00115 grid->addWidget( nCols, 3, 0 );
00116
00117 #ifdef ALLOW_NON_INLINE_TABLES
00118 if ( m_useMode==NEW )
00119 {
00120 lHei = new QLabel( i18n( "Cell heights:" ), tab1 );
00121 grid->addWidget( lHei, 4, 0 );
00122
00123 cHei = new QComboBox( FALSE, tab1 );
00124 cHei->insertItem( i18n( "Automatic" ) );
00125 cHei->insertItem( i18n( "Manual" ) );
00126 cHei->setCurrentItem( (int)hei );
00127 grid->addWidget( cHei, 5, 0 );
00128
00129 lWid = new QLabel( i18n( "Cell widths:" ), tab1 );
00130 grid->addWidget( lWid, 6, 0 );
00131
00132 cWid = new QComboBox( FALSE, tab1 );
00133 cWid->insertItem( i18n( "Automatic" ) );
00134 cWid->insertItem( i18n( "Manual" ) );
00135 cWid->setCurrentItem( (int)wid );
00136 grid->addWidget( cWid, 7, 0 );
00137 }
00138 #else
00139 Q_UNUSED( wid );
00140 Q_UNUSED( hei );
00141 Q_UNUSED( floating );
00142 #endif
00143
00144 preview = new KWTablePreview( tab1, rows, cols );
00145 preview->setBackgroundColor( white );
00146 grid->addMultiCellWidget( preview, 0, 8, 1, 1 );
00147
00148 if ( m_useMode==NEW )
00149 {
00150 #ifdef ALLOW_NON_INLINE_TABLES
00151
00152 cbIsFloating = new QCheckBox( i18n( "The table is &inline" ), tab1 );
00153
00154 cbIsFloating->setChecked( floating );
00155
00156 grid->addMultiCellWidget( cbIsFloating, 9, 9, 0, 2 );
00157 connect( cbIsFloating, SIGNAL( toggled ( bool ) ), this, SLOT( slotInlineTable( bool ) ) );
00158 #endif
00159 }
00160 else
00161 if (m_useMode==EDIT)
00162 {
00163 cbReapplyTemplate1 = new QCheckBox( i18n("Reapply template to table"), tab1 );
00164 grid->addMultiCellWidget( cbReapplyTemplate1, 9, 9, 0, 2);
00165
00166 connect( cbReapplyTemplate1, SIGNAL( toggled ( bool ) ), this, SLOT( slotSetReapply( bool ) ) );
00167 }
00168
00169 grid->addRowSpacing( 0, lRows->height() );
00170 grid->addRowSpacing( 1, nRows->height() );
00171 grid->addRowSpacing( 2, lCols->height() );
00172 grid->addRowSpacing( 3, nCols->height() );
00173 #ifdef ALLOW_NON_INLINE_TABLES
00174 if ( m_useMode==NEW )
00175 {
00176 grid->addRowSpacing( 4, lHei->height() );
00177 grid->addRowSpacing( 5, cHei->height() );
00178 grid->addRowSpacing( 6, lWid->height() );
00179 grid->addRowSpacing( 7, cWid->height() );
00180 }
00181 #endif
00182 grid->addRowSpacing( 8, 150 - ( lRows->height() + nRows->height() + lCols->height() + nCols->height() ) );
00183 #ifdef ALLOW_NON_INLINE_TABLES
00184 if ( m_useMode==NEW )
00185 grid->addRowSpacing( 9, cbIsFloating->height() );
00186 #endif
00187 grid->setRowStretch( 0, 0 );
00188 grid->setRowStretch( 1, 0 );
00189 grid->setRowStretch( 2, 0 );
00190 grid->setRowStretch( 3, 0 );
00191 grid->setRowStretch( 4, 0 );
00192 grid->setRowStretch( 5, 0 );
00193 grid->setRowStretch( 6, 0 );
00194 grid->setRowStretch( 7, 0 );
00195 grid->setRowStretch( 8, 1 );
00196 grid->setRowStretch( 9, 0 );
00197
00198 grid->addColSpacing( 0, lRows->width() );
00199 grid->addColSpacing( 0, nRows->width() );
00200 grid->addColSpacing( 0, lCols->width() );
00201 grid->addColSpacing( 0, nCols->width() );
00202 #ifdef ALLOW_NON_INLINE_TABLES
00203 if ( m_useMode==NEW )
00204 {
00205 grid->addColSpacing( 0, lHei->width() );
00206 grid->addColSpacing( 0, cHei->width() );
00207 grid->addColSpacing( 0, lWid->width() );
00208 grid->addColSpacing( 0, cWid->width() );
00209 }
00210 #endif
00211 grid->addColSpacing( 1, 150 );
00212 grid->setColStretch( 0, 0 );
00213 grid->setColStretch( 1, 1 );
00214
00215 grid->activate();
00216
00217 connect( nRows, SIGNAL( valueChanged( int ) ), this, SLOT( rowsChanged( int ) ) );
00218 connect( nCols, SIGNAL( valueChanged( int ) ), this, SLOT( colsChanged( int ) ) );
00219 }
00220
00221 void KWTableDia::setupTab2(const QString & _templateName, int format )
00222 {
00223 QWidget *tab2 = addPage( i18n("Templates"));
00224
00225 QGridLayout *grid = new QGridLayout( tab2, 2, 1, 0, KDialog::spacingHint() );
00226
00227 tableTemplateSelector = new KWTableTemplateSelector( doc, tab2, _templateName,format );
00228 grid->addWidget(tableTemplateSelector, 0, 0);
00229
00230 if (m_useMode==EDIT)
00231 {
00232 cbReapplyTemplate2 = new QCheckBox( i18n("Reapply template to table"), tab2 );
00233 grid->addWidget( cbReapplyTemplate2, 1, 0);
00234
00235 grid->setRowStretch( 0, 1);
00236 grid->setRowStretch( 1, 0);
00237
00238 connect( cbReapplyTemplate2, SIGNAL( toggled ( bool ) ), this, SLOT( slotSetReapply( bool ) ) );
00239 }
00240 grid->activate();
00241 }
00242
00243 void KWTableDia::slotOk()
00244 {
00245 if ( m_useMode==NEW )
00247
00248 canvas->createTable( nRows->value(), nCols->value(),
00249 #ifdef ALLOW_NON_INLINE_TABLES
00250 cWid->currentItem(),
00251 cHei->currentItem(),
00252 cbIsFloating->isChecked(),
00253 #else
00254 0,
00255 0,
00256 true,
00257 #endif
00258 tableTemplateSelector->getTableTemplate(),
00259 tableTemplateSelector->getFormatType());
00260 else
00261 {
00262 KWTableFrameSet *table = canvas->getCurrentTable();
00263 if ( table )
00264 {
00265 KMacroCommand *macroCmd = 0L;
00266 KCommand *cmd = 0L;
00267
00268
00269 int rowsDiff = nRows->value()-oldRowCount;
00270 if ( rowsDiff!=0 )
00271 {
00272 macroCmd = new KMacroCommand( (rowsDiff>0 ) ? i18n("Add New Rows to Table") : i18n("Remove Rows From Table") );
00273 for ( int i = 0 ; i < abs( rowsDiff ) ; i++ )
00274 {
00275 if ( rowsDiff < 0 )
00276 cmd = new KWRemoveRowCommand( i18n("Remove Row"), table, oldRowCount-i-1 );
00277 else
00278 {
00279 cmd = new KWInsertRowCommand( i18n("Insert Row"), table, oldRowCount+i );
00280 }
00281
00282 if (cmd)
00283 macroCmd->addCommand( cmd );
00284 }
00285 canvas->setTableRows( nRows->value() );
00286 }
00287
00288
00289 int colsDiff = nCols->value()-oldColCount;
00290 if ( colsDiff!=0 )
00291 {
00292 double maxRightOffset;
00293 if (table->isFloating())
00294
00295 maxRightOffset = table->anchorFrameset()->frame(0)->right();
00296 else {
00297 KWPage *page = doc->pageManager()->page(table->cell(0,0)->frame(0));
00298 maxRightOffset = page->width() - page->rightMargin();
00299 }
00300 if ( !macroCmd )
00301 macroCmd = new KMacroCommand( (colsDiff>0 ) ? i18n("Add New Columns to Table") : i18n("Remove Columns From Table") );
00302 cmd = 0L;
00303 for ( int i = 0 ; i < abs( colsDiff ) ; i++ )
00304 {
00305 if ( colsDiff < 0 )
00306 cmd = new KWRemoveColumnCommand( i18n("Remove Column"), table, oldColCount-i-1 );
00307 else
00308 {
00309 cmd = new KWInsertColumnCommand( i18n("Insert Column"), table, oldColCount+i, maxRightOffset);
00310 }
00311
00312 if (cmd)
00313 macroCmd->addCommand( cmd );
00314 }
00315 canvas->setTableCols( nCols->value() );
00316 }
00317
00318
00319 if ( tableTemplateSelector->getTableTemplate() && (( oldTemplateName!=tableTemplateSelector->getTableTemplate()->name() ) || (cbReapplyTemplate1->isChecked()) ))
00320 {
00321 if ( !macroCmd )
00322 macroCmd = new KMacroCommand( i18n("Apply Template to Table") );
00323 KWTableTemplateCommand *ttCmd=new KWTableTemplateCommand( i18n("Apply Template to Table"), table, tableTemplateSelector->getTableTemplate() );
00324 macroCmd->addCommand( ttCmd );
00325 canvas->setTableTemplateName( tableTemplateSelector->getTableTemplate()->name() );
00326 }
00327 if ( macroCmd )
00328 {
00329 macroCmd->execute();
00330 doc->addCommand( macroCmd );
00331 }
00332 }
00333 }
00334 KDialogBase::slotOk();
00335 }
00336
00337 void KWTableDia::rowsChanged( int _rows )
00338 {
00339 preview->setRows( _rows );
00340 }
00341
00342 void KWTableDia::colsChanged( int _cols )
00343 {
00344 preview->setCols( _cols );
00345 }
00346
00347 void KWTableDia::slotSetReapply( bool _reapply )
00348 {
00349 if ( cbReapplyTemplate1->isChecked()!=_reapply ) cbReapplyTemplate1->setChecked( _reapply );
00350 if ( cbReapplyTemplate2->isChecked()!=_reapply ) cbReapplyTemplate2->setChecked( _reapply );
00351 }
00352
00353 void KWTableDia::slotInlineTable( bool state)
00354 {
00355 #ifdef ALLOW_NON_INLINE_TABLES
00356 if ( m_useMode==NEW )
00357 {
00358 lWid->setEnabled( !state );
00359 lHei->setEnabled( !state );
00360 cHei->setEnabled( !state );
00361 cWid->setEnabled( !state );
00362 }
00363 #else
00364 Q_UNUSED( state );
00365 #endif
00366 }