1
/***************************************************************************
5
copyright : (C) 2002 by Alexander Dymo
6
email : cloudtemple@mksat.net
7
***************************************************************************/
9
/***************************************************************************
11
* This program is free software; you can redistribute it and/or modify *
12
* it under the terms of the GNU Library General Public License as *
13
* published by the Free Software Foundation; either version 2 of the *
14
* License, or (at your option) any later version. *
16
***************************************************************************/
37
#include "creportitem.h"
40
#include "cspecialfield.h"
41
#include "ccalcfield.h"
43
#include "canvkutemplate.h"
44
#include "canvdetailbase.h"
48
//#include "kudesigner_view.h"
49
#include "kudesigner_command.h"
52
void SelectionRect::draw(QPainter & painter)
54
// painter.setPen(Qt::NoPen);
59
canvas()->drawArea( canvas()->rect(), &p );
62
/* qDebug("creating pixmap");
63
QPixmap mp(rect().size());
64
qDebug("creating painter");
66
qDebug("filling pixmap");
67
canvas()->drawArea(canvas()->rect(), &p);
68
qDebug("converting to image");
69
QImage im = mp.convertToImage();
73
mp.convertFromImage(im, Qt::OrderedAlphaDither);
75
qDebug("creating brush");
76
QBrush br(KGlobalSettings::highlightColor(),Qt::CustomPattern);
81
// painter.drawRect(rect());
82
QPen pen(QColor(0,0,0), 0, Qt::DotLine);
84
painter.setBrush(QBrush(NoBrush));
85
painter.drawRect(rect());
86
// QCanvasRectangle::draw(painter);
91
ReportCanvas::ReportCanvas(QCanvas * canvas, QWidget * parent, const char * name, WFlags f):
92
QCanvasView(canvas, parent, name, f),m_plugin(0)/*,m_view(parent)*/
94
m_canvas=(MyCanvas*)canvas;
99
request = RequestNone;
100
selectionRect = new SelectionRect(0, 0, 0, 0, canvas);
102
connect(m_canvas, SIGNAL(itemSelected()), this, SLOT(selectItem()));
105
void ReportCanvas::setPlugin(KuDesignerPlugin *plugin)
110
void ReportCanvas::deleteItem(QCanvasItemList &l)
112
for (QCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it)
114
m_canvas->unselectItem((CanvasBox*)*it);
115
if ( ((MyCanvas*)(canvas()))->templ->removeReportItem(*it) )
120
void ReportCanvas::editItem(QCanvasItemList &/* l */)
122
//display editor for report items or sections
123
/* for (QCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it)
125
if ((*it)->rtti() >= 1800) //for my own report items
127
CanvasBox *l = (CanvasBox*)(*it);
128
dlgItemOptions *dlgOpts = new dlgItemOptions(&(l->props), this);
131
if ((*it)->rtti() == KuDesignerRttiKugarTemplate)
132
((CanvasKugarTemplate*)(*it))->updatePaperProps();
135
if ((*it)->rtti() < 2000)
136
((MyCanvas *)(canvas()))->templ->arrangeSections();
138
emit modificationPerformed();
144
void ReportCanvas::selectItemFromList(QCanvasItemList &l)
146
//display editor for report items or sections
147
for (QCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it)
149
if ((*it)->rtti() >= 1800 ) //include bands and the template itself
151
CanvasBox *b = (CanvasBox*)(*it);
152
if (!m_canvas->selected.containsRef(b))
154
m_canvas->unselectAll();
155
m_canvas->selectItem(b, false);
157
// qWarning("selected item set");
158
// selected->drawHolders();
161
if (m_canvas->selected.containsRef(b))
163
if (m_canvas->selected.count() > 1)
165
m_canvas->unselectAll();
166
m_canvas->selectItem(b, false);
173
m_canvas->unselectAll();
174
// qWarning("unselect");
178
void ReportCanvas::placeItem(QCanvasItemList &l, QMouseEvent *e)
180
for (QCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it)
182
if ( ((*it)->rtti() > 1800) && (((*it)->rtti() < 2000)) )
184
int band = (*it)->rtti();
186
if ( (band == KuDesignerRttiDetailHeader) ||
187
(band == KuDesignerRttiDetail ) ||
188
(band == KuDesignerRttiDetailFooter) )
189
bandLevel = ((CanvasDetailBase *)(*it))->level();
190
emit itemPlaced(e->x(), e->y(), band, bandLevel);
192
// emit modificationPerformed();
196
emit selectedActionProcessed();
200
bool ReportCanvas::startResizing(QMouseEvent * /*e*/, QPoint &p)
202
if (m_canvas->selected.count()==0) return false;
203
for (CanvasBox *cbx=m_canvas->selected.first();
204
cbx;cbx=m_canvas->selected.next())
206
resizing_type=cbx->isInHolder(p);
209
m_canvas->selectItem(cbx,false);
210
//kdDebug()<<"A Widget should be resized"<<endl;
217
if (cbx->rtti()>2001)
219
CanvasReportItem *item = (CanvasReportItem *)(cbx);
220
resizing_constraint.setX((int)item->section()->x());
221
resizing_constraint.setY((int)item->section()->y());
222
resizing_constraint.setWidth(item->section()->width());
223
resizing_constraint.setHeight(
224
item->section()->height());
225
if (cbx->rtti()!=KuDesignerRttiCanvasLine)
227
resizing_minSize.setWidth(10);
228
resizing_minSize.setHeight(10);
232
resizing_minSize.setWidth(0);
233
resizing_minSize.setHeight(0);
237
if (cbx->rtti()>=KuDesignerRttiReportHeader)
239
resizing_constraint=QRect(0,0,1000,1000);
240
resizing_minSize.setWidth(0);
241
resizing_minSize.setHeight(
242
((CanvasBand*)cbx)->minHeight());
246
resizing_constraint=QRect(0,0,1000,1000);
247
resizing_minSize.setWidth(0);
248
resizing_minSize.setHeight(10);
256
void ReportCanvas::startMoveOrResizeOrSelectItem(QCanvasItemList &l,
257
QMouseEvent * /*e*/, QPoint &p)
259
//allow user to move any item except for page rectangle
260
for (QCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it)
262
CanvasBox *cb=(CanvasBox*)(*it);
263
if (cb->rtti() >= 1700) //> 2001)
268
resizing_type=cb->isInHolder(p);
269
if ((*it)->rtti() >2001)
271
CanvasReportItem *item = (CanvasReportItem *)(*it);
280
selectionStarted = 1;
281
selectionRect->setX(p.x());
282
selectionRect->setY(p.y());
283
selectionRect->setZ(50);
284
selectionRect->show();
287
void ReportCanvas::contentsMousePressEvent(QMouseEvent* e)
289
QPoint p = inverseWorldMatrix().QWMatrix::map(e->pos());
290
QCanvasItemList l=canvas()->collisions(p);
292
//if there is a request for properties or for delete operation
293
//perform that and do not take care about mouse buttons
295
// qWarning("mouse press");
313
selectionStarted = 0;
318
qWarning("Selected items:");
319
for (b = selected.first(); b; b = selected.next())
320
qWarning("%s", b->props["Text"].first.latin1());
328
// qWarning("placing item");
329
m_canvas->unselectAll();
334
// qWarning("starting move or resize");
335
if (!startResizing(e,p))
337
selectItemFromList(l);
338
startMoveOrResizeOrSelectItem(l, e, p);
347
void ReportCanvas::contentsMouseReleaseEvent(QMouseEvent* e)
349
selectionRect->setSize(0, 0);
350
selectionRect->setX(0);
351
selectionRect->setY(0);
352
selectionRect->hide();
354
QPoint p = inverseWorldMatrix().QWMatrix::map(e->pos());
355
QCanvasItemList l=canvas()->collisions(p);
360
if (selectionStarted)
375
void ReportCanvas::fixMinValues(double &pos,double minv,double &offset)
379
offset=offset+pos-minv;
386
offset=offset+pos-minv;
398
void ReportCanvas::fixMaxValues(double &pos,double size,double maxv,double &offset)
400
double tmpMax=pos+size;
403
offset=offset+tmpMax-maxv;
410
offset=offset+tmpMax-maxv;
415
pos=offset+maxv-size;
422
void ReportCanvas::contentsMouseMoveEvent(QMouseEvent* e)
424
QPoint p = inverseWorldMatrix().map(e->pos());
426
/* QCanvasItemList l=canvas()->collisions(p);
427
setCursor(QCursor(Qt::ArrowCursor));
429
for (QCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it)
431
if ((*it)->rtti() > 2000)
433
CanvasReportItem *item = (CanvasReportItem*)(*it);
434
if (item->bottomRightResizableRect().contains(e->pos()))
435
setCursor(QCursor(Qt::SizeFDiagCursor));
441
double newXPos=moving->x()+p.x()-moving_start.x();
442
double newYPos=moving->y()+p.y()-moving_start.y();
443
fixMinValues(newYPos,moving->parentSection->y(),moving_offsetY);
444
fixMinValues(newXPos,moving->parentSection->x(),moving_offsetX);
445
fixMaxValues(newYPos,moving->height(),moving->parentSection->y()+moving->parentSection->height(),moving_offsetY);
446
fixMaxValues(newXPos,moving->width(),moving->parentSection->x()+moving->parentSection->width(),moving_offsetX);
448
moving->move(newXPos,newYPos);
449
/* attempt to prevent item collisions
450
QCanvasItemList l=canvas()->collisions(moving->rect());
453
moving->moveBy(-(p.x() - moving_start.x()),
454
-(p.y() - moving_start.y()));
459
moving->updateGeomProps();
461
emit modificationPerformed();
465
QCanvasRectangle *r = (QCanvasRectangle *)resizing;
466
double newXPos=r->x();
467
double newYPos=r->y();
468
double h=r->height();
470
// kdDebug()<<"resizing"<<endl;
473
if (resizing_type & CanvasBox::ResizeBottom)
475
// kdDebug()<<"Resize bottom"<<endl;
476
h = h + p.y() - moving_start.y();
477
fixMaxValues(h,r->y(),resizing_constraint.bottom(),moving_offsetY);
478
if(resizing->rtti() != KuDesignerRttiCanvasLine)
479
fixMinValues(h,resizing_minSize.height(),moving_offsetY);
482
if (resizing_type & CanvasBox::ResizeTop)
484
// kdDebug()<<"Resize top"<<endl;
485
newYPos=r->y()+p.y()-moving_start.y();
486
fixMinValues(newYPos,resizing_constraint.top(),moving_offsetY);
487
if(resizing->rtti() != KuDesignerRttiCanvasLine)
488
fixMaxValues(newYPos, resizing_minSize.height(),r->y()+r->height(),moving_offsetY);
489
h=h+(r->y()-newYPos);
493
//horizontal resizing
494
if (resizing_type & CanvasBox::ResizeRight)
496
// kdDebug()<<"Resize right"<<endl;
497
w = w + p.x() - moving_start.x();
498
fixMaxValues(w,r->x(),resizing_constraint.right(),moving_offsetX);
499
if(resizing->rtti() != KuDesignerRttiCanvasLine)
500
fixMinValues(w,resizing_minSize.width(),moving_offsetX);
504
if (resizing_type & CanvasBox::ResizeLeft)
506
// kdDebug()<<"Resize left"<<endl;
507
newXPos=r->x()+p.x()-moving_start.x();
508
fixMinValues(newXPos,resizing_constraint.left(),moving_offsetX);
509
if(resizing->rtti() != KuDesignerRttiCanvasLine)
510
fixMaxValues(newXPos, resizing_minSize.width(),r->x()+r->width(),moving_offsetX);
511
w=w+(r->x()-newXPos);
514
r->move(newXPos,newYPos);
515
r->setSize((int)w, (int)h);
517
resizing->updateGeomProps();
519
emit modificationPerformed();
521
if (selectionStarted)
523
/* qDebug("x_start = %d, y_start = %d, x_end = %d, y_end = %d", moving_start.x(),
524
moving_start.y(), e->pos().x(), e->pos().y());*/
525
selectionRect->setSize((int) (e->pos().x() - selectionRect->x()),
526
(int) (e->pos().y() - selectionRect->y() ));
527
m_canvas->unselectAll();
528
QCanvasItemList l = canvas()->collisions(selectionRect->rect());
529
for (QCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it)
532
int left = selectionRect->rect().left();
533
int right = selectionRect->rect().right();
534
int top = selectionRect->rect().top();
535
int bottom = selectionRect->rect().bottom();
536
r.setLeft(left<right ? left : right);
537
r.setRight(left<right ? right : left);
538
r.setTop(top<bottom ? top : bottom);
539
r.setBottom(top<bottom ? bottom : top);
541
if ( ((*it)->rtti() > 2001) &&
542
(r.contains(((CanvasBox*)(*it))->rect())) )
544
m_canvas->selectItem((CanvasBox*)(*it));
550
/* selectionRect->setSize(e->pos().x() - selectionRect->x(),
551
e->pos().y() - selectionRect->y());
553
QCanvasItemList l = canvas()->collisions(selectionRect->rect());
554
for (QCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it)
556
if ( ((*it)->rtti() > 2001) &&
557
(selectionRect->rect().contains(((CanvasBox*)(*it))->rect())) )
559
selectItem((CanvasBox*)(*it));
567
void ReportCanvas::contentsMouseDoubleClickEvent( QMouseEvent *e )
569
CanvasReportItem *item = 0L;
570
if ( e->button() == Qt::LeftButton && m_canvas->selected.count() == 1 )
571
item = dynamic_cast<CanvasReportItem *>( m_canvas->selected.first() );
574
item->fastProperty();
580
void ReportCanvas::setRequest(RequestType r)
585
QApplication::restoreOverrideCursor();
586
QApplication::setOverrideCursor(Qt::PointingHandCursor);
589
QApplication::restoreOverrideCursor();
590
QApplication::setOverrideCursor(Qt::ForbiddenCursor);
593
QApplication::restoreOverrideCursor();
599
void ReportCanvas::clearRequest()
601
QApplication::restoreOverrideCursor();
602
request = RequestNone;
603
emit selectedEditActionProcessed();
606
bool ReportCanvas::requested()
608
if (request == RequestNone)
614
void ReportCanvas::updateProperty(QString name, QString value)
617
for (b = m_canvas->selected.first(); b; b = m_canvas->selected.next())
619
b->props[name]->setValue(value);
622
if ((b->rtti()>=1800) && (b->rtti()<2000))
623
((MyCanvas*)(canvas()))->templ->arrangeSections();
628
void ReportCanvas::finishSelection()
630
selectionStarted = false;
632
if (!m_canvas->selected.isEmpty())
633
if (m_canvas->selected.count() > 1)
635
// handling multiple selection
636
std::map<QString, PropPtr > curr = m_canvas->selected.first()->props;
638
CanvasBox *b = m_canvas->selected.first();
639
b = m_canvas->selected.next();
642
std::map<QString, PropPtr > selProps;
643
std::insert_iterator< std::map<QString, PropPtr > > it(selProps, selProps.begin());
644
std::set_intersection(curr.begin(), curr.end(), b->props.begin(), b->props.end(), it);
648
/* qWarning("Multiple selection property list:");
649
for (std::map<QString, PropPtr>::const_iterator i = curr->begin(); i != curr->end(); ++i )
651
qWarning(" %s = %s", i->first.latin1(), i->second->value().latin1());
654
b = m_canvas->selected.next();
657
/* qWarning("Multiple selection property list:");
658
for (std::map<QString, PropPtr>::const_iterator i = curr->begin(); i != curr->end(); ++i )
660
qWarning(" %s = %s", i->first.latin1(), i->second->value().latin1());
663
std::map<QString, PropPtr > *x = new std::map<QString, PropPtr >(curr);
664
emit selectionMade(x,0);
667
emit selectionMade(&(m_canvas->selected.first()->props),
668
m_canvas->selected.first());
671
for (b = selected.first(); b; b = selected.next())
673
qWarning("item with rtti = %d is selected", b->rtti());
678
void ReportCanvas::contentsDragMoveEvent ( QDragMoveEvent * event) {
679
//perhaps this could be optimized a littlebit
680
if (!m_plugin) return;
681
QCanvasItemList l=canvas()->collisions(event->pos());
682
// kdDebug()<<l.count()<<endl;
688
CanvasBox *b=(CanvasBox*) (*(l.begin()));
689
if (m_plugin->dragMove(event,b))
695
void ReportCanvas::contentsDragEnterEvent ( QDragEnterEvent * /*event*/) {
701
void ReportCanvas::keyPressEvent( QKeyEvent *e )
703
qDebug("keyPress (selection : %d)", m_canvas->selected.count());
705
if ( m_canvas->selected.count() == 1 ) {
706
CanvasReportItem *item = static_cast<CanvasReportItem *>( m_canvas->selected.first() );
708
switch ( e->key() ) {
710
qDebug("Deleting selection");
711
/* unselectItem(item);
712
( (MyCanvas*) canvas() )->templ->removeReportItem( item );
715
//FIXME: this disregards undo/redo
716
if (((MyCanvas*)canvas())->selected.count() > 0)
718
DeleteReportItemsCommand *cmd = new DeleteReportItemsCommand(((MyCanvas*)canvas()), ((MyCanvas*)canvas())->selected);
724
/* Adjust height with - and + */
728
int size = item->props["FontSize"]->value().toInt();
730
if ( e->key() == Qt::Key_Minus )
741
updateProperty( "FontSize", QString::number( size ) );
752
void ReportCanvas::selectItem( )
754
if (!selectionStarted)
758
/*void ReportCanvas::deleteSelected( )
762
QPtrList<CanvasBox> list = m_canvas->selected;
765
for (b = list.first(); b; b = list.next())
767
( (MyCanvas*) canvas() )->templ->removeReportItem( b );