summaryrefslogtreecommitdiffstats
path: root/kword/KWTableDia.cpp
blob: 711c76c503d33cbcb20745bf6033ed88f9dc602e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* This file is part of the KDE project
   Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
   Copyright (C) 2005 Thomas Zander <zander@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "KWCanvas.h"
#include "KWTableDia.h"
#include "KWTableDia.moc"

#include "KWTableTemplateSelector.h"
#include "KWCommand.h"
#include "KWDocument.h"
#include "KWTableFrameSet.h"
#include "KWTableTemplate.h"
#include "KWPageManager.h"
#include "KWPage.h"
#include <kcommand.h>

#include <tqlabel.h>
#include <tqlayout.h>
#include <tqspinbox.h>
#include <tqcheckbox.h>
#include <tqbuttongroup.h>
#include <tqcombobox.h>

#include <klocale.h>

#include <stdlib.h>


/******************************************************************/
/* Class: KWTablePreview                                          */
/******************************************************************/

void KWTablePreview::paintEvent( TQPaintEvent * )
{
    int wid = ( width() - 10 ) / cols;
    int hei = ( height() - 10 ) / rows;

    TQPainter p;
    p.begin( this );

    p.setPen( TQPen( black ) );

    for ( int i = 0; i < rows; i++ )
    {
        for ( int j = 0; j < cols; j++ )
            p.drawRect( j * wid + 5, i * hei + 5, wid + 1, hei + 1 );
    }

    p.end();
}

/******************************************************************/
/* Class: KWTableDia                                              */
/******************************************************************/

KWTableDia::KWTableDia( TQWidget* tqparent, const char* name, UseMode _useMode, KWCanvas *_canvas, KWDocument *_doc,
        int rows, int cols, CellSize wid, CellSize hei, bool floating , const TQString & _templateName, int format)
    : KDialogBase( Tabbed, i18n("Table Settings"), Ok | Cancel, Ok, tqparent, name, true)
{
    m_useMode = _useMode;
    canvas = _canvas;
    doc = _doc;

    setupTab1( rows, cols, wid, hei, floating );
    setupTab2( _templateName,format );

    setInitialSize( TQSize(500, 480) );

    oldRowCount = rows;
    oldColCount = cols;
    oldTemplateName = _templateName;
#ifdef ALLOW_NON_INLINE_TABLES
    if ( m_useMode==NEW )
    {
        slotInlineTable( cbIsFloating->isChecked());
    }
#endif
}

void KWTableDia::setupTab1( int rows, int cols, CellSize wid, CellSize hei, bool floating )
{
    tab1 = addPage( i18n( "Geometry" ) );

    TQGridLayout *grid = new TQGridLayout( tab1, 9, 2, 0, KDialog::spacingHint() );

    lRows = new TQLabel( i18n( "Number of rows:" ), tab1 );
    grid->addWidget( lRows, 0, 0 );

    nRows = new TQSpinBox( 1, 128, 1, tab1 );
    nRows->setValue( rows );
    grid->addWidget( nRows, 1, 0 );

    lCols = new TQLabel( i18n( "Number of columns:" ), tab1 );
    grid->addWidget( lCols, 2, 0 );

    nCols = new TQSpinBox( 1, 128, 1, tab1 );
    nCols->setValue( cols );
    grid->addWidget( nCols, 3, 0 );

#ifdef ALLOW_NON_INLINE_TABLES
    if ( m_useMode==NEW )
    {
        lHei = new TQLabel( i18n( "Cell heights:" ), tab1 );
        grid->addWidget( lHei, 4, 0 );

        cHei = new TQComboBox( FALSE, tab1 );
        cHei->insertItem( i18n( "Automatic" ) );
        cHei->insertItem( i18n( "Manual" ) );
        cHei->setCurrentItem( (int)hei );
        grid->addWidget( cHei, 5, 0 );

        lWid = new TQLabel( i18n( "Cell widths:" ), tab1 );
        grid->addWidget( lWid, 6, 0 );

        cWid = new TQComboBox( FALSE, tab1 );
        cWid->insertItem( i18n( "Automatic" ) );
        cWid->insertItem( i18n( "Manual" ) );
        cWid->setCurrentItem( (int)wid );
        grid->addWidget( cWid, 7, 0 );
    }
#else
    Q_UNUSED( wid );
    Q_UNUSED( hei );
    Q_UNUSED( floating );
#endif

    preview = new KWTablePreview( tab1, rows, cols );
    preview->setBackgroundColor( white );
    grid->addMultiCellWidget( preview, 0, 8, 1, 1 );

    if ( m_useMode==NEW )
    {
#ifdef ALLOW_NON_INLINE_TABLES
        // Checkbox for floating/fixed location. The default is floating (aka inline).
        cbIsFloating = new TQCheckBox( i18n( "The table is &inline" ), tab1 );
        //cbIsFloating->setEnabled(false);
        cbIsFloating->setChecked( floating );

        grid->addMultiCellWidget( cbIsFloating, 9, 9, 0, 2 );
        connect( cbIsFloating, TQT_SIGNAL( toggled ( bool )  ), this, TQT_SLOT( slotInlineTable( bool ) ) );
#endif
    }
    else
    if (m_useMode==EDIT)
    {
        cbReapplyTemplate1 = new TQCheckBox( i18n("Reapply template to table"), tab1 );
        grid->addMultiCellWidget( cbReapplyTemplate1, 9, 9, 0, 2);

        connect( cbReapplyTemplate1, TQT_SIGNAL( toggled ( bool )  ), this, TQT_SLOT( slotSetReapply( bool ) ) );
    }

    grid->addRowSpacing( 0, lRows->height() );
    grid->addRowSpacing( 1, nRows->height() );
    grid->addRowSpacing( 2, lCols->height() );
    grid->addRowSpacing( 3, nCols->height() );
#ifdef ALLOW_NON_INLINE_TABLES
    if ( m_useMode==NEW )
    {
        grid->addRowSpacing( 4, lHei->height() );
        grid->addRowSpacing( 5, cHei->height() );
        grid->addRowSpacing( 6, lWid->height() );
        grid->addRowSpacing( 7, cWid->height() );
    }
#endif
    grid->addRowSpacing( 8, 150 - ( lRows->height() + nRows->height() + lCols->height() + nCols->height() ) );
#ifdef ALLOW_NON_INLINE_TABLES
    if ( m_useMode==NEW )
        grid->addRowSpacing( 9, cbIsFloating->height() );
#endif
    grid->setRowStretch( 0, 0 );
    grid->setRowStretch( 1, 0 );
    grid->setRowStretch( 2, 0 );
    grid->setRowStretch( 3, 0 );
    grid->setRowStretch( 4, 0 );
    grid->setRowStretch( 5, 0 );
    grid->setRowStretch( 6, 0 );
    grid->setRowStretch( 7, 0 );
    grid->setRowStretch( 8, 1 );
    grid->setRowStretch( 9, 0 );

    grid->addColSpacing( 0, lRows->width() );
    grid->addColSpacing( 0, nRows->width() );
    grid->addColSpacing( 0, lCols->width() );
    grid->addColSpacing( 0, nCols->width() );
#ifdef ALLOW_NON_INLINE_TABLES
    if ( m_useMode==NEW )
    {
        grid->addColSpacing( 0, lHei->width() );
        grid->addColSpacing( 0, cHei->width() );
        grid->addColSpacing( 0, lWid->width() );
        grid->addColSpacing( 0, cWid->width() );
    }
#endif
    grid->addColSpacing( 1, 150 );
    grid->setColStretch( 0, 0 );
    grid->setColStretch( 1, 1 );

    grid->activate();

    connect( nRows, TQT_SIGNAL( valueChanged( int ) ), this, TQT_SLOT( rowsChanged( int ) ) );
    connect( nCols, TQT_SIGNAL( valueChanged( int ) ), this, TQT_SLOT( colsChanged( int ) ) );
}

void KWTableDia::setupTab2(const TQString & _templateName, int format )
{
    TQWidget *tab2 = addPage( i18n("Templates"));

    TQGridLayout *grid = new TQGridLayout( tab2, 2, 1, 0, KDialog::spacingHint() );

    tableTemplateSelector = new KWTableTemplateSelector( doc, tab2, _templateName,format );
    grid->addWidget(tableTemplateSelector, 0, 0);

    if (m_useMode==EDIT)
    {
        cbReapplyTemplate2 = new TQCheckBox( i18n("Reapply template to table"), tab2 );
        grid->addWidget( cbReapplyTemplate2, 1, 0);

        grid->setRowStretch( 0, 1);
        grid->setRowStretch( 1, 0);

        connect( cbReapplyTemplate2, TQT_SIGNAL( toggled ( bool )  ), this, TQT_SLOT( slotSetReapply( bool ) ) );
    }
    grid->activate();
}

void KWTableDia::slotOk()
{
    if ( m_useMode==NEW )
        /// ###### This should be done AFTER this dialog is closed.
        // Otherwise we have two modal dialogs fighting each other
        canvas->createTable( nRows->value(), nCols->value(),
#ifdef ALLOW_NON_INLINE_TABLES
                             cWid->currentItem(),
                             cHei->currentItem(),
                             cbIsFloating->isChecked(),
#else
                             0,
                             0,
                             true,
#endif
                             tableTemplateSelector->getTableTemplate(),
                             tableTemplateSelector->getFormatType());
    else
    {
        KWTableFrameSet *table = canvas->getCurrentTable();
        if ( table )
        {
            KMacroCommand *macroCmd = 0L;
            KCommand *cmd = 0L;

            // Add or delete rows
            int rowsDiff = nRows->value()-oldRowCount;
            if ( rowsDiff!=0 )
            {
                macroCmd = new KMacroCommand( (rowsDiff>0 ) ? i18n("Add New Rows to Table") : i18n("Remove Rows From Table") );
                for ( int i = 0 ; i < abs( rowsDiff ) ; i++ )
                {
                    if ( rowsDiff < 0 )
                        cmd = new KWRemoveRowCommand( i18n("Remove Row"), table, oldRowCount-i-1 );
                    else
                    {
                        cmd = new KWInsertRowCommand( i18n("Insert Row"), table, oldRowCount+i );
                    }

                    if (cmd)
                        macroCmd->addCommand( cmd );
                }
                canvas->setTableRows( nRows->value() );
            }

            // Add or delete cols
            int colsDiff = nCols->value()-oldColCount;
            if ( colsDiff!=0 )
            {
                double maxRightOffset;
                if (table->isFloating())
                    // inline table: max offset of containing frame
                    maxRightOffset = table->anchorFrameset()->frame(0)->right();
                else { // non inline table: max offset of the page
                    KWPage *page = doc->pageManager()->page(table->cell(0,0)->frame(0));
                    maxRightOffset = page->width() - page->rightMargin();
                }
                if ( !macroCmd )
                    macroCmd = new KMacroCommand( (colsDiff>0 ) ? i18n("Add New Columns to Table") : i18n("Remove Columns From Table") );
                cmd = 0L;
                for ( int i = 0 ; i < abs( colsDiff ) ; i++ )
                {
                    if ( colsDiff < 0 )
                        cmd = new KWRemoveColumnCommand( i18n("Remove Column"), table, oldColCount-i-1 );
                    else
                    {
                        cmd = new KWInsertColumnCommand( i18n("Insert Column"), table, oldColCount+i,  maxRightOffset);
                    }

                    if (cmd)
                        macroCmd->addCommand( cmd );
                }
                canvas->setTableCols( nCols->value() );
            }

            // Apply template
            if ( tableTemplateSelector->getTableTemplate() && (( oldTemplateName!=tableTemplateSelector->getTableTemplate()->name() ) || (cbReapplyTemplate1->isChecked()) ))
            {
                if ( !macroCmd )
                    macroCmd = new KMacroCommand( i18n("Apply Template to Table") );
                KWTableTemplateCommand *ttCmd=new KWTableTemplateCommand( i18n("Apply Template to Table"), table, tableTemplateSelector->getTableTemplate() );
                macroCmd->addCommand( ttCmd );
                canvas->setTableTemplateName( tableTemplateSelector->getTableTemplate()->name() );
            }
            if ( macroCmd )
            {
                macroCmd->execute();
                doc->addCommand( macroCmd );
            }
        }
    }
    KDialogBase::slotOk();
}

void KWTableDia::rowsChanged( int _rows )
{
    preview->setRows( _rows );
}

void KWTableDia::colsChanged( int _cols )
{
    preview->setCols( _cols );
}

void KWTableDia::slotSetReapply( bool _reapply )
{
    if ( cbReapplyTemplate1->isChecked()!=_reapply ) cbReapplyTemplate1->setChecked( _reapply );
    if ( cbReapplyTemplate2->isChecked()!=_reapply ) cbReapplyTemplate2->setChecked( _reapply );
}

void KWTableDia::slotInlineTable( bool state)
{
#ifdef ALLOW_NON_INLINE_TABLES
    if ( m_useMode==NEW )
    {
        lWid->setEnabled( !state );
        lHei->setEnabled( !state );
        cHei->setEnabled( !state );
        cWid->setEnabled( !state );
    }
#else
    Q_UNUSED( state );
#endif
}