summaryrefslogtreecommitdiffstats
path: root/kugar/kudesigner/kudesigner_view.cpp
blob: 713b852116422986947a0f9acd21c20a7c27c2d2 (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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/* This file is part of the KDE project
  Copyright (C) 2002-2004 Alexander Dymo <cloudtemple@mksat.net>

  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
  MEm_viewHANTABILITY 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 "kudesigner_view.h"
#include "kudesigner_factory.h"
#include "kudesigner_doc.h"

#include <map>

#include <qpainter.h>
#include <qiconset.h>
#include <qinputdialog.h>
#include <qevent.h>
#include <qmainwindow.h>
#include <qaction.h>
#include <qlayout.h>
#include <qdockwindow.h>
#include <qmenubar.h>
#include <qmessagebox.h>
#include <qspinbox.h>
#include <qlabel.h>

#include <kaction.h>
#include <kstdaction.h>
#include <klocale.h>
#include <kdebug.h>

#include <KoMainWindow.h>

#include <commdefs.h>
#include <view.h>
#include <structurewidget.h>
#include <canvas.h>
#include <command.h>

#include <field.h>
#include <calcfield.h>
#include <label.h>
#include <line.h>
#include <specialfield.h>

#include <kugartemplate.h>
#include <reportheader.h>
#include <reportfooter.h>
#include <pageheader.h>
#include <pagefooter.h>
#include <detailheader.h>
#include <detailfooter.h>
#include <detail.h>

#include <koproperty/editor.h>
#include <koproperty/property.h>

using namespace Kudesigner;

KudesignerView::KudesignerView( KudesignerDoc* part, QWidget* parent, const char* name )
        : KoView( part, parent, name ), m_propertyEditor( 0 ), m_doc( part )
{
    setInstance( KudesignerFactory::global() );
    if ( !part->isReadWrite() )  // readonly case, e.g. when embedded into konqueror
        setXMLFile( "kudesigner_readonly.rc" ); // simplified GUI
    else
        setXMLFile( "kudesignerui.rc" );

    QVBoxLayout *l = new QVBoxLayout( this, 0, 0 );
    m_view = new Kudesigner::View( part->canvas(), this );
    if ( part->plugin() )
    {
        m_view->setAcceptDrops( part->plugin() ->acceptsDrops() );
        m_view->viewport() ->setAcceptDrops( part->plugin() ->acceptsDrops() );
        m_view->setPlugin( part->plugin() );
    }
    l->addWidget( m_view );

    m_view->viewport() ->setFocusProxy( m_view );
    m_view->viewport() ->setFocusPolicy( WheelFocus );
    m_view->setFocus();

    m_view->itemToInsert = 0;

    QDockWindow *dw1 = new QDockWindow( QDockWindow::OutsideDock, shell() );
    QDockWindow *dw2 = new QDockWindow( QDockWindow::OutsideDock, shell() );
    m_structure = new Kudesigner::StructureWidget( dw1 );
    m_propertyEditor = new Editor( dw2 );
    dw1->boxLayout() ->addWidget( m_structure, 1 );
    dw2->boxLayout() ->addWidget( m_propertyEditor, 1 );
    dw1->setFixedExtentWidth( 400 );
    dw1->setResizeEnabled( true );
    dw2->setFixedExtentWidth( 400 );
    dw2->setResizeEnabled( true );

    if ( m_doc->plugin() )
    {
        //                 connect( m_propertyEditor, SIGNAL(createPluggedInEditor(QWidget*&, Editor *, Property*, Box *)),
        //                          m_doc->plugin(), SLOT(createPluggedInEditor(QWidget*&, Editor *, Property*, Box *)));

        kdDebug() << "*************Property and plugin have been connected" << endl;
    }

    shell() ->addDockWindow( dw1, m_doc->propertyPosition() );
    shell() ->addDockWindow( dw2, m_doc->propertyPosition() );

    m_structure->setDocument( m_doc->canvas() );

    connect( m_doc, SIGNAL( canvasChanged( Kudesigner::Canvas * ) ),
             m_structure, SLOT( setDocument( Kudesigner::Canvas * ) ) );
    connect( m_doc->canvas(), SIGNAL( structureModified() ),
             m_structure, SLOT( refresh() ) );

    connect( m_view, SIGNAL( selectionMade( Buffer* ) ),
             this, SLOT( populateProperties( Buffer* ) ) );

    connect( m_view, SIGNAL( selectionClear() ),
             m_propertyEditor, SLOT( clear() ) );

    connect( m_view, SIGNAL( changed() ),
             m_doc, SLOT( setModified() ) );

    connect( m_view, SIGNAL( selectionMade( Buffer* ) ),
             m_structure, SLOT( selectionMade() ) );
    connect( m_view, SIGNAL( selectionClear() ),
             m_structure, SLOT( selectionClear() ) );

    connect( m_view, SIGNAL( selectedActionProcessed() ), this, SLOT( unselectItemAction() ) );
    connect( m_view, SIGNAL( modificationPerformed() ), part, SLOT( setModified() ) );
    connect( m_view, SIGNAL( itemPlaced( int, int, int, int ) ), this, SLOT( placeItem( int, int, int, int ) ) );

    gridLabel = new QLabel( i18n( "Grid size:" ), shell() );
    gridBox = new QSpinBox( 1, 100, 1, shell() );
    gridBox->setValue( 10 );
    connect( gridBox, SIGNAL( valueChanged( int ) ), m_view, SLOT( setGridSize( int ) ) );

    initActions();

    show();
    m_view->show();
    m_structure->refresh();
}

KudesignerView::~KudesignerView()
{
    delete gridLabel;
    delete gridBox;
}

void KudesignerView::paintEvent( QPaintEvent* ev )
{
    QPainter painter;
    painter.begin( this );

    // ### TODO: Scaling

    // Let the document do the drawing
    koDocument() ->paintEverything( painter, ev->rect(), FALSE, this );

    painter.end();
}

void KudesignerView::resizeEvent( QResizeEvent* /*_ev*/ )
{
    m_view->setGeometry( 0, 0, width(), height() );
}

void KudesignerView::initActions()
{
    cutAction = KStdAction::cut( this, SLOT( cut() ), actionCollection() );
    copyAction = KStdAction::copy( this, SLOT( copy() ), actionCollection() );
    pasteAction = KStdAction::paste( this, SLOT( paste() ), actionCollection() );
    selectAllAction = KStdAction::selectAll( this, SLOT( selectAll() ), actionCollection() );
    deleteAction = new KAction( i18n( "Delete" ), "editdelete", 0, this,
                                SLOT( deleteItems() ), actionCollection(), "edit_delete" );
    cutAction->setEnabled( false );
    copyAction->setEnabled( false );
    pasteAction->setEnabled( false );
    //    deleteAction->setEnabled(false);

    sectionsReportHeader = new KAction( i18n( "Report Header" ), "irh", 0, this,
                                        SLOT( slotAddReportHeader() ), actionCollection(), "rheader" );
    sectionsReportFooter = new KAction( i18n( "Report Footer" ), "irf", 0, this,
                                        SLOT( slotAddReportFooter() ), actionCollection(), "rfooter" );
    sectionsPageHeader = new KAction( i18n( "Page Header" ), "iph", 0, this,
                                      SLOT( slotAddPageHeader() ), actionCollection(), "pheader" );
    sectionsPageFooter = new KAction( i18n( "Page Footer" ), "ipf", 0, this,
                                      SLOT( slotAddPageFooter() ), actionCollection(), "pfooter" );
    sectionsDetailHeader = new KAction( i18n( "Detail Header" ), "idh", 0, this,
                                        SLOT( slotAddDetailHeader() ), actionCollection(), "dheader" );
    sectionsDetail = new KAction( i18n( "Detail" ), "id", 0, this,
                                  SLOT( slotAddDetail() ), actionCollection(), "detail" );
    sectionsDetailFooter = new KAction( i18n( "Detail Footer" ), "idf", 0, this,
                                        SLOT( slotAddDetailFooter() ), actionCollection(), "dfooter" );

    itemsNothing = new KRadioAction( i18n( "Clear Selection" ), "frame_edit", 0, this,
                                     SLOT( slotAddItemNothing() ), actionCollection(), "nothing" );
    itemsNothing->setExclusiveGroup( "itemsToolBar" );
    itemsNothing->setChecked( true );
    itemsLabel = new KRadioAction( i18n( "Label" ), "frame_text", 0, this,
                                   SLOT( slotAddItemLabel() ), actionCollection(), "label" );
    itemsLabel->setExclusiveGroup( "itemsToolBar" );
    itemsField = new KRadioAction( i18n( "Field" ), "frame_field", 0, this,
                                   SLOT( slotAddItemField() ), actionCollection(), "field" );
    itemsField->setExclusiveGroup( "itemsToolBar" );
    itemsSpecial = new KRadioAction( i18n( "Special Field" ), "frame_query", 0, this,
                                     SLOT( slotAddItemSpecial() ), actionCollection(), "special" );
    itemsSpecial->setExclusiveGroup( "itemsToolBar" );
    itemsCalculated = new KRadioAction( i18n( "Calculated Field" ), "frame_formula", 0, this,
                                        SLOT( slotAddItemCalculated() ), actionCollection(), "calcfield" );
    itemsCalculated->setExclusiveGroup( "itemsToolBar" );
    itemsLine = new KRadioAction( i18n( "Line" ), "frame_chart", 0, this,
                                  SLOT( slotAddItemLine() ), actionCollection(), "line" );
    itemsLine->setExclusiveGroup( "itemsToolBar" );

    gridActionLabel = new KWidgetAction( gridLabel, i18n( "Grid Label" ), 0, this,
                                         0, actionCollection(), "gridlabel" );

    gridAction = new KWidgetAction( gridBox, i18n( "Grid Size" ), 0, this,
                                    0, actionCollection(), "gridaction" );
}

void KudesignerView::updateReadWrite( bool /*readwrite*/ )
{
}

void KudesignerView::guiActivateEvent( KParts::GUIActivateEvent *ev )
{
    if ( ev->activated() )
        m_propertyEditor->show();
    else
        m_propertyEditor->hide();
    KoView::guiActivateEvent( ev );
}

void KudesignerView::populateProperties( Buffer *buf )
{
    connect( buf, SIGNAL( propertyChanged() ), m_doc->canvas(), SLOT( changed() ) );
    m_propertyEditor->changeSet( buf );
}

void KudesignerView::cut()
{
    //    kdDebug(31000) << "KudesignerView::cut(): CUT called" << endl;
}

void KudesignerView::copy()
{
    //    kdDebug(31000) << "KudesignerView::copy(): COPY called" << endl;
}

void KudesignerView::paste( )
{}

void KudesignerView::deleteItems( )
{
    if ( m_doc->canvas() ->selected.count() > 0 )
        m_doc->addCommand( new DeleteReportItemsCommand( m_doc->canvas(), m_doc->canvas() ->selected ) );
}

void KudesignerView::selectAll( )
{
    m_doc->canvas() ->selectAll();
}

void KudesignerView::slotAddReportHeader()
{
    if ( !( ( ( KudesignerDoc * ) ( koDocument() ) ) ) ->canvas() ->kugarTemplate() ->reportHeader )
    {
        m_doc->addCommand( new AddReportHeaderCommand( m_doc->canvas() ) );
    }
}

void KudesignerView::slotAddReportFooter()
{
    if ( !( ( ( KudesignerDoc * ) ( koDocument() ) ) ) ->canvas() ->kugarTemplate() ->reportFooter )
    {
        m_doc->addCommand( new AddReportFooterCommand( m_doc->canvas() ) );
    }
}

void KudesignerView::slotAddPageHeader()
{
    if ( !( ( ( KudesignerDoc * ) ( koDocument() ) ) ) ->canvas() ->kugarTemplate() ->pageHeader )
    {
        m_doc->addCommand( new AddPageHeaderCommand( m_doc->canvas() ) );
    }
}

void KudesignerView::slotAddPageFooter()
{
    if ( !( ( ( KudesignerDoc * ) ( koDocument() ) ) ) ->canvas() ->kugarTemplate() ->pageFooter )
    {
        m_doc->addCommand( new AddPageFooterCommand( m_doc->canvas() ) );
    }
}

void KudesignerView::slotAddDetailHeader()
{
    bool Ok = false;
    unsigned int level = QInputDialog::getInteger( tr( "Add Detail Header" ), tr( "Enter detail level:" ),
                         0, 0, 100, 1, &Ok, this );
    if ( !Ok )
        return ;
    if ( m_doc->canvas() ->kugarTemplate() ->detailsCount >= level )
    {
        m_doc->addCommand( new AddDetailHeaderCommand( level, m_doc->canvas() ) );
    }
}

void KudesignerView::slotAddDetail()
{
    bool Ok = false;
    unsigned int level = QInputDialog::getInteger( tr( "Add Detail" ), tr( "Enter detail level:" ),
                         0, 0, 100, 1, &Ok, this );
    if ( !Ok )
        return ;
    if ( ( ( level == 0 ) && ( m_doc->canvas() ->kugarTemplate() ->detailsCount == 0 ) )
            || ( m_doc->canvas() ->kugarTemplate() ->detailsCount == level ) )
    {
        m_doc->addCommand( new AddDetailCommand( level, m_doc->canvas() ) );
    }
}

void KudesignerView::slotAddDetailFooter()
{
    bool Ok = false;
    unsigned int level = QInputDialog::getInteger( tr( "Add Detail Footer" ), tr( "Enter detail level:" ),
                         0, 0, 100, 1, &Ok, this );
    if ( !Ok )
        return ;

    if ( m_doc->canvas() ->kugarTemplate() ->detailsCount >= level )
    {
        m_doc->addCommand( new AddDetailFooterCommand( level, m_doc->canvas() ) );
    }
}

void KudesignerView::slotAddItemNothing()
{
    if ( m_doc->canvas() )
    {
        if ( m_view->itemToInsert )
        {
            m_view->itemToInsert = 0;
        }
    }
}

void KudesignerView::slotAddItemLabel()
{
    if ( m_doc->canvas() )
    {
        m_view->itemToInsert = Rtti_Label;
    }
}

void KudesignerView::slotAddItemField()
{
    if ( m_doc->canvas() )
    {
        m_view->itemToInsert = Rtti_Field;
    }
}

void KudesignerView::slotAddItemSpecial()
{
    if ( m_doc->canvas() )
    {
        m_view->itemToInsert = Rtti_Special;
    }
}

void KudesignerView::slotAddItemCalculated()
{
    if ( m_doc->canvas() )
    {
        m_view->itemToInsert = Rtti_Calculated;
    }
}

void KudesignerView::slotAddItemLine()
{
    if ( m_doc->canvas() )
    {
        m_view->itemToInsert = Rtti_Line;
    }
}

void KudesignerView::unselectItemAction()
{
    /*    itemsNothing->setOn(true);*/
}

void KudesignerView::placeItem( int x, int y, int band, int bandLevel )
{
    m_doc->addCommand( new AddReportItemCommand( m_doc->canvas(), m_view, x, y, ( Kudesigner::RttiValues ) band, bandLevel ) );
}

#include "kudesigner_view.moc"