kword

KWTableDia.cpp
1/* This file is part of the KDE project
2 Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
3 Copyright (C) 2005 Thomas Zander <zander@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include "KWCanvas.h"
22#include "KWTableDia.h"
23#include "KWTableDia.moc"
24
25#include "KWTableTemplateSelector.h"
26#include "KWCommand.h"
27#include "KWDocument.h"
28#include "KWTableFrameSet.h"
29#include "KWTableTemplate.h"
30#include "KWPageManager.h"
31#include "KWPage.h"
32#include <kcommand.h>
33
34#include <tqlabel.h>
35#include <tqlayout.h>
36#include <tqspinbox.h>
37#include <tqcheckbox.h>
38#include <tqbuttongroup.h>
39#include <tqcombobox.h>
40
41#include <tdelocale.h>
42
43#include <stdlib.h>
44
45
46/******************************************************************/
47/* Class: KWTablePreview */
48/******************************************************************/
49
50void KWTablePreview::paintEvent( TQPaintEvent * )
51{
52 int wid = ( width() - 10 ) / cols;
53 int hei = ( height() - 10 ) / rows;
54
55 TQPainter p;
56 p.begin( this );
57
58 p.setPen( TQPen( black ) );
59
60 for ( int i = 0; i < rows; i++ )
61 {
62 for ( int j = 0; j < cols; j++ )
63 p.drawRect( j * wid + 5, i * hei + 5, wid + 1, hei + 1 );
64 }
65
66 p.end();
67}
68
69/******************************************************************/
70/* Class: KWTableDia */
71/******************************************************************/
72
73KWTableDia::KWTableDia( TQWidget* parent, const char* name, UseMode _useMode, KWCanvas *_canvas, KWDocument *_doc,
74 int rows, int cols, CellSize wid, CellSize hei, bool floating , const TQString & _templateName, int format)
75 : KDialogBase( Tabbed, i18n("Table Settings"), Ok | Cancel, Ok, parent, name, true)
76{
77 m_useMode = _useMode;
78 canvas = _canvas;
79 doc = _doc;
80
81 setupTab1( rows, cols, wid, hei, floating );
82 setupTab2( _templateName,format );
83
84 setInitialSize( TQSize(500, 480) );
85
86 oldRowCount = rows;
87 oldColCount = cols;
88 oldTemplateName = _templateName;
89#ifdef ALLOW_NON_INLINE_TABLES
90 if ( m_useMode==NEW )
91 {
92 slotInlineTable( cbIsFloating->isChecked());
93 }
94#endif
95}
96
97void KWTableDia::setupTab1( int rows, int cols, CellSize wid, CellSize hei, bool floating )
98{
99 tab1 = addPage( i18n( "Geometry" ) );
100
101 TQGridLayout *grid = new TQGridLayout( tab1, 9, 2, 0, KDialog::spacingHint() );
102
103 lRows = new TQLabel( i18n( "Number of rows:" ), tab1 );
104 grid->addWidget( lRows, 0, 0 );
105
106 nRows = new TQSpinBox( 1, 128, 1, tab1 );
107 nRows->setValue( rows );
108 grid->addWidget( nRows, 1, 0 );
109
110 lCols = new TQLabel( i18n( "Number of columns:" ), tab1 );
111 grid->addWidget( lCols, 2, 0 );
112
113 nCols = new TQSpinBox( 1, 128, 1, tab1 );
114 nCols->setValue( cols );
115 grid->addWidget( nCols, 3, 0 );
116
117#ifdef ALLOW_NON_INLINE_TABLES
118 if ( m_useMode==NEW )
119 {
120 lHei = new TQLabel( i18n( "Cell heights:" ), tab1 );
121 grid->addWidget( lHei, 4, 0 );
122
123 cHei = new TQComboBox( false, tab1 );
124 cHei->insertItem( i18n( "Automatic" ) );
125 cHei->insertItem( i18n( "Manual" ) );
126 cHei->setCurrentItem( (int)hei );
127 grid->addWidget( cHei, 5, 0 );
128
129 lWid = new TQLabel( i18n( "Cell widths:" ), tab1 );
130 grid->addWidget( lWid, 6, 0 );
131
132 cWid = new TQComboBox( false, tab1 );
133 cWid->insertItem( i18n( "Automatic" ) );
134 cWid->insertItem( i18n( "Manual" ) );
135 cWid->setCurrentItem( (int)wid );
136 grid->addWidget( cWid, 7, 0 );
137 }
138#else
139 Q_UNUSED( wid );
140 Q_UNUSED( hei );
141 Q_UNUSED( floating );
142#endif
143
144 preview = new KWTablePreview( tab1, rows, cols );
145 preview->setBackgroundColor( white );
146 grid->addMultiCellWidget( preview, 0, 8, 1, 1 );
147
148 if ( m_useMode==NEW )
149 {
150#ifdef ALLOW_NON_INLINE_TABLES
151 // Checkbox for floating/fixed location. The default is floating (aka inline).
152 cbIsFloating = new TQCheckBox( i18n( "The table is &inline" ), tab1 );
153 //cbIsFloating->setEnabled(false);
154 cbIsFloating->setChecked( floating );
155
156 grid->addMultiCellWidget( cbIsFloating, 9, 9, 0, 2 );
157 connect( cbIsFloating, TQ_SIGNAL( toggled ( bool ) ), this, TQ_SLOT( slotInlineTable( bool ) ) );
158#endif
159 }
160 else
161 if (m_useMode==EDIT)
162 {
163 cbReapplyTemplate1 = new TQCheckBox( i18n("Reapply template to table"), tab1 );
164 grid->addMultiCellWidget( cbReapplyTemplate1, 9, 9, 0, 2);
165
166 connect( cbReapplyTemplate1, TQ_SIGNAL( toggled ( bool ) ), this, TQ_SLOT( slotSetReapply( bool ) ) );
167 }
168
169 grid->addRowSpacing( 0, lRows->height() );
170 grid->addRowSpacing( 1, nRows->height() );
171 grid->addRowSpacing( 2, lCols->height() );
172 grid->addRowSpacing( 3, nCols->height() );
173#ifdef ALLOW_NON_INLINE_TABLES
174 if ( m_useMode==NEW )
175 {
176 grid->addRowSpacing( 4, lHei->height() );
177 grid->addRowSpacing( 5, cHei->height() );
178 grid->addRowSpacing( 6, lWid->height() );
179 grid->addRowSpacing( 7, cWid->height() );
180 }
181#endif
182 grid->addRowSpacing( 8, 150 - ( lRows->height() + nRows->height() + lCols->height() + nCols->height() ) );
183#ifdef ALLOW_NON_INLINE_TABLES
184 if ( m_useMode==NEW )
185 grid->addRowSpacing( 9, cbIsFloating->height() );
186#endif
187 grid->setRowStretch( 0, 0 );
188 grid->setRowStretch( 1, 0 );
189 grid->setRowStretch( 2, 0 );
190 grid->setRowStretch( 3, 0 );
191 grid->setRowStretch( 4, 0 );
192 grid->setRowStretch( 5, 0 );
193 grid->setRowStretch( 6, 0 );
194 grid->setRowStretch( 7, 0 );
195 grid->setRowStretch( 8, 1 );
196 grid->setRowStretch( 9, 0 );
197
198 grid->addColSpacing( 0, lRows->width() );
199 grid->addColSpacing( 0, nRows->width() );
200 grid->addColSpacing( 0, lCols->width() );
201 grid->addColSpacing( 0, nCols->width() );
202#ifdef ALLOW_NON_INLINE_TABLES
203 if ( m_useMode==NEW )
204 {
205 grid->addColSpacing( 0, lHei->width() );
206 grid->addColSpacing( 0, cHei->width() );
207 grid->addColSpacing( 0, lWid->width() );
208 grid->addColSpacing( 0, cWid->width() );
209 }
210#endif
211 grid->addColSpacing( 1, 150 );
212 grid->setColStretch( 0, 0 );
213 grid->setColStretch( 1, 1 );
214
215 grid->activate();
216
217 connect( nRows, TQ_SIGNAL( valueChanged( int ) ), this, TQ_SLOT( rowsChanged( int ) ) );
218 connect( nCols, TQ_SIGNAL( valueChanged( int ) ), this, TQ_SLOT( colsChanged( int ) ) );
219}
220
221void KWTableDia::setupTab2(const TQString & _templateName, int format )
222{
223 TQWidget *tab2 = addPage( i18n("Templates"));
224
225 TQGridLayout *grid = new TQGridLayout( tab2, 2, 1, 0, KDialog::spacingHint() );
226
227 tableTemplateSelector = new KWTableTemplateSelector( doc, tab2, _templateName,format );
228 grid->addWidget(tableTemplateSelector, 0, 0);
229
230 if (m_useMode==EDIT)
231 {
232 cbReapplyTemplate2 = new TQCheckBox( i18n("Reapply template to table"), tab2 );
233 grid->addWidget( cbReapplyTemplate2, 1, 0);
234
235 grid->setRowStretch( 0, 1);
236 grid->setRowStretch( 1, 0);
237
238 connect( cbReapplyTemplate2, TQ_SIGNAL( toggled ( bool ) ), this, TQ_SLOT( slotSetReapply( bool ) ) );
239 }
240 grid->activate();
241}
242
243void KWTableDia::slotOk()
244{
245 if ( m_useMode==NEW )
247 // Otherwise we have two modal dialogs fighting each other
248 canvas->createTable( nRows->value(), nCols->value(),
249#ifdef ALLOW_NON_INLINE_TABLES
250 cWid->currentItem(),
251 cHei->currentItem(),
252 cbIsFloating->isChecked(),
253#else
254 0,
255 0,
256 true,
257#endif
258 tableTemplateSelector->getTableTemplate(),
259 tableTemplateSelector->getFormatType());
260 else
261 {
262 KWTableFrameSet *table = canvas->getCurrentTable();
263 if ( table )
264 {
265 KMacroCommand *macroCmd = 0L;
266 KCommand *cmd = 0L;
267
268 // Add or delete rows
269 int rowsDiff = nRows->value()-oldRowCount;
270 if ( rowsDiff!=0 )
271 {
272 macroCmd = new KMacroCommand( (rowsDiff>0 ) ? i18n("Add New Rows to Table") : i18n("Remove Rows From Table") );
273 for ( int i = 0 ; i < abs( rowsDiff ) ; i++ )
274 {
275 if ( rowsDiff < 0 )
276 cmd = new KWRemoveRowCommand( i18n("Remove Row"), table, oldRowCount-i-1 );
277 else
278 {
279 cmd = new KWInsertRowCommand( i18n("Insert Row"), table, oldRowCount+i );
280 }
281
282 if (cmd)
283 macroCmd->addCommand( cmd );
284 }
285 canvas->setTableRows( nRows->value() );
286 }
287
288 // Add or delete cols
289 int colsDiff = nCols->value()-oldColCount;
290 if ( colsDiff!=0 )
291 {
292 double maxRightOffset;
293 if (table->isFloating())
294 // inline table: max offset of containing frame
295 maxRightOffset = table->anchorFrameset()->frame(0)->right();
296 else { // non inline table: max offset of the page
297 KWPage *page = doc->pageManager()->page(table->cell(0,0)->frame(0));
298 maxRightOffset = page->width() - page->rightMargin();
299 }
300 if ( !macroCmd )
301 macroCmd = new KMacroCommand( (colsDiff>0 ) ? i18n("Add New Columns to Table") : i18n("Remove Columns From Table") );
302 cmd = 0L;
303 for ( int i = 0 ; i < abs( colsDiff ) ; i++ )
304 {
305 if ( colsDiff < 0 )
306 cmd = new KWRemoveColumnCommand( i18n("Remove Column"), table, oldColCount-i-1 );
307 else
308 {
309 cmd = new KWInsertColumnCommand( i18n("Insert Column"), table, oldColCount+i, maxRightOffset);
310 }
311
312 if (cmd)
313 macroCmd->addCommand( cmd );
314 }
315 canvas->setTableCols( nCols->value() );
316 }
317
318 // Apply template
319 if ( tableTemplateSelector->getTableTemplate() && (( oldTemplateName!=tableTemplateSelector->getTableTemplate()->name() ) || (cbReapplyTemplate1->isChecked()) ))
320 {
321 if ( !macroCmd )
322 macroCmd = new KMacroCommand( i18n("Apply Template to Table") );
323 KWTableTemplateCommand *ttCmd=new KWTableTemplateCommand( i18n("Apply Template to Table"), table, tableTemplateSelector->getTableTemplate() );
324 macroCmd->addCommand( ttCmd );
325 canvas->setTableTemplateName( tableTemplateSelector->getTableTemplate()->name() );
326 }
327 if ( macroCmd )
328 {
329 macroCmd->execute();
330 doc->addCommand( macroCmd );
331 }
332 }
333 }
334 KDialogBase::slotOk();
335}
336
337void KWTableDia::rowsChanged( int _rows )
338{
339 preview->setRows( _rows );
340}
341
342void KWTableDia::colsChanged( int _cols )
343{
344 preview->setCols( _cols );
345}
346
347void KWTableDia::slotSetReapply( bool _reapply )
348{
349 if ( cbReapplyTemplate1->isChecked()!=_reapply ) cbReapplyTemplate1->setChecked( _reapply );
350 if ( cbReapplyTemplate2->isChecked()!=_reapply ) cbReapplyTemplate2->setChecked( _reapply );
351}
352
353void KWTableDia::slotInlineTable( bool state)
354{
355#ifdef ALLOW_NON_INLINE_TABLES
356 if ( m_useMode==NEW )
357 {
358 lWid->setEnabled( !state );
359 lHei->setEnabled( !state );
360 cHei->setEnabled( !state );
361 cWid->setEnabled( !state );
362 }
363#else
364 Q_UNUSED( state );
365#endif
366}
Class: KWCanvas This class is responsible for the rendering of the frames to the screen as well as th...
Definition KWCanvas.h:61
bool isFloating() const
Return true if this frameset is floating (inline), false if it's fixed.
Definition KWFrameSet.h:372
KWTextFrameSet * anchorFrameset() const
Return the frameset in which our anchor is - assuming isFloating().
Definition KWFrameSet.h:374
KWFrame * frame(unsigned int _num) const
get a frame by number
double width() const
return the width of this page (in pt)
Definition KWPage.cpp:38
double rightMargin() const
return the width of the margin at left (in pt);
Definition KWPage.cpp:108