/*************************************************************************** * Copyright (C) 2004 by Johan Maes - ON4QZ * * on4qz@telenet.be * * http://users.telenet.be/on4qz * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program 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 General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "editorview.h" #include "editorform.h" #include #include #include #include "fgbgbutton.h" #include "utils.h" #include #include #include "effects.h" #include "configdialog.h" #define BORDER 4 /** editorview */ editorView::editorView(QWidget *parent, const char *name): QCanvasView(parent, name) { ef=NULL; // create a default canvas mCanvas= new mcanvas(320,256); setCanvas(mCanvas); lineWidth=1; setMaximumSize(320+BORDER,256+BORDER); QPixmap pm(QPixmap::fromMimeSource("colorpicker.png")); crsr= new QCursor(pm,0,pm.height()-1); mCanvas->setBackgroundColor(Qt::darkGray); setAcceptDrops(TRUE); modified=FALSE; } editorView::~editorView() { } void editorView::slotChangeSize(QSize s) { mCanvas->resize(s.width(),s.height()); setMaximumSize(s.width()+BORDER,s.height()+BORDER); } //void editorView::clear() //{ // mCanvas->clearAll(); //} void editorView::setupAction(eactionType at) { if(at>COLORPICK) { sbp->message("Click on start point"); } else { sbp->message("Click to select color"); } actionType=at; } void editorView::slotArrow() { setupAction(SELECT); } void editorView::slotRectangle() { setupAction(RECTANGLE); } void editorView::slotCircle() { setupAction(ELLIPSE); } void editorView::slotColorpicker() { setCursor(*crsr); setupAction(COLORPICK); } void editorView::slotImage() { dirDialog d; QString fn(d.openFileName(txImagePath,"*")); if (fn.isEmpty()) return ; if(im.load(fn)) setupAction(IMAGE); } void editorView::slotCopy() { mCanvas->copy(); } void editorView::slotPaste() { mCanvas->paste(); } void editorView::slotExpand() { if(!mCanvas->checkSelected()) return; mCanvas->current()->setGeometry(mCanvas->rect().topLeft(),mCanvas->rect().bottomRight()); modified=TRUE; } void editorView::setImage(QImage *ima) { if(ima==NULL) return; mCanvas->setImage(ima); modified=TRUE; } // drag and drop support void editorView::dragEnterEvent(QDragEnterEvent* event) { // debug("dragEnter"); QString s; int i=0; do { s=event->format(i); //if(!s.isNull()) debug(s); i++; } while (!s.isNull()); if(QImageDrag::canDecode(event)) { // debug("image decode"); event->accept(); } if(QUriDrag::canDecode(event)) { event->accept(); } } void editorView::dropEvent(QDropEvent* event) { QStrList lst; QImage image; // debug("dropevent"); if (QImageDrag::decode( event, im )) { im.setAlphaBuffer(TRUE); setupAction(IMAGE); // debug("got image"); } else if(QUriDrag::decode( event, lst )) { QString filename = QUriDrag::uriToLocalFile(lst.at(0)); // debug(filename); if(im.load(filename)) setupAction(IMAGE); // debug("got file"); } } void editorView::slotLine() { setupAction(LINE); } void editorView::slotPenWidth(int pw) { lineWidth=pw; } void editorView::slotText() { if((mCanvas->current()->isSelected()) && (mCanvas->current()->rtti()==textRTTI)) { mCanvas->current()->setText(lineEditString); mCanvas->current()->updateActiveImage(); mCanvas->setChanged(mCanvas->current()->rect()); modified=TRUE; mCanvas->update(); debug("changed text"); repaint(FALSE); } else setupAction(TEXT); } void editorView::slotDeleteItem() { mCanvas->deleteObject(); modified=TRUE; } void editorView::slotApplyColors() { if(!mCanvas->checkSelected()) return; mCanvas->current()->applyColors(ef->fgbgFrame->getForegroundColor(),ef->fgbgFrame->getBackgroundColor(),lineWidth); modified=TRUE; } void editorView::slotChangeFont() { if(!mCanvas->checkSelected()) return; aFont=mCanvas->current()->getFont(); slotSelectFont(); mCanvas->current()->changeFont(aFont); modified=TRUE; } void editorView::slotLock() { if(!mCanvas->checkSelected()) return; mCanvas->current()->setLocked(TRUE); modified=TRUE; } void editorView::slotUnlock() { if(!mCanvas->checkSelected()) return; mCanvas->current()->setLocked(FALSE); modified=TRUE; } void editorView::slotBringToFront() { mCanvas->bringToFront(); modified=TRUE; } void editorView::slotSendToBack() { mCanvas->sendToBack(); modified=TRUE; } void editorView::slotSendBackward() { mCanvas->backward(); modified=TRUE; } void editorView::slotSendForward() { mCanvas->forward(); modified=TRUE; } void editorView::setupItem(int w,int h) { canvasItemBase *bp=mCanvas->current(); bp->create(clickPoint,clickPoint+QPoint(w,h)); mCanvas->setSelected(); slotApplyColors(); // bp->show(); mCanvas->update(); emit setShearRotate(0,0); sbp->message("Drag to size"); displaySize(); modified=TRUE; } void editorView::contentsMousePressEvent(QMouseEvent* e) { // we are either moving or creating QString tmp; mCanvas->resetSelected(); mCanvas->update(); // debug ("mouse pressed"); clickPoint = inverseWorldMatrix().map(e->pos()); switch (actionType) { case ELLIPSE: { mCanvas->createItem(ellipseRTTI); setupItem(); } break; case RECTANGLE: { mCanvas->createItem(rectangleRTTI); setupItem(); } break; case LINE: { mCanvas->createItem(lineRTTI); setupItem(); } break; case TEXT: { mCanvas->createItem(textRTTI); setupItem(); mCanvas->current()->setText(lineEditString); mCanvas->current()->setFont(aFont); } break; case IMAGE: { mCanvas->setImage(&im); im.reset(); setupItem(); } break; case SELECT: { if(mCanvas->makeSelection(clickPoint)==NULL) return; emit setShearRotate(mCanvas->current()->getShear(),mCanvas->current()->getRotate()); sizingText->setText(tmp.sprintf("x=%4d, y=%4d; w=%4d, h=%4d", mCanvas->current()->rect().x(), mCanvas->current()->rect().y(), mCanvas->current()->rect().width(), mCanvas->current()->rect().height())); //check for resizing if(mCanvas->current()->rtti()==textRTTI) { emit textLoaded( mCanvas->current()->getText()); } checkResizing(); if (e->button() == RightButton) { QPopupMenu *p=new QPopupMenu(this); QPopupMenu *arrange = new QPopupMenu( this ); QPopupMenu *effects = new QPopupMenu( this ); arrange->insertTearOffHandle(); effects->insertTearOffHandle(); arrange->insertItem("Forward",this,SLOT(slotSendForward())); arrange->insertItem("Backward",this,SLOT(slotSendBackward())); arrange->insertItem("Bring to front",this,SLOT(slotBringToFront())); arrange->insertItem("Send to back",this,SLOT(slotSendToBack())); effects->insertItem("Horizontal gradient",this,SLOT(slotHorGradient())); effects->insertItem("Vertical gradient",this,SLOT(slotVerGradient())); p->insertItem("Arrange",arrange); p->insertItem("Apply Colors",this,SLOT(slotApplyColors())); p->insertItem("Change Font",this,SLOT(slotChangeFont())); p->insertSeparator(); p->insertItem("Delete",this,SLOT(slotDeleteItem())); p->insertSeparator(); p->insertItem("Effects",effects); p->insertItem("Expand",this,SLOT(slotExpand())); p->insertItem("Lock",this,SLOT(slotLock())); p->insertItem("Unlock",this,SLOT(slotUnlock())); p->exec(QCursor::pos()); } mCanvas->update(); } break; default: break; } } void editorView::displaySize() { QString tmp; sizingText->setText(tmp.sprintf("x=%4d, y=%4d; w=%4d, h=%4d", mCanvas->current()->rect().x(), mCanvas->current()->rect().y(), mCanvas->current()->rect().width(), mCanvas->current()->rect().height())); } void editorView::contentsMouseMoveEvent(QMouseEvent* e) { QPoint p = inverseWorldMatrix().map(e->pos()); // debug("x=%d,y=%d",p.x(),p.y());void slotHorGradient(); switch (actionType) { case SELECT: { if ( mCanvas->checkSelected()) { if(!mCanvas->current()->isLocked()) { doSizing(p); displaySize(); } } } break; case COLORPICK: break; case LINE: case ELLIPSE: case RECTANGLE: case TEXT: case IMAGE: mCanvas->current()->setGeometry(clickPoint,QPoint(p.x(),p.y())); // debug("clickpoint=%d,%d",clickPoint.x(),clickPoint.y()); //mCanvas->current()->setEndPoint(p.x(),p.y()); displaySize(); break; default: break; } // if((sx<=0)|| (sy<=0)) return; mCanvas->update(); } void editorView::contentsMouseReleaseEvent(QMouseEvent*e) { QPoint p = inverseWorldMatrix().map(e->pos()); setCursor(Qt::ArrowCursor); switch (actionType) { case SELECT: { if ( mCanvas->checkSelected()) { if(!mCanvas->current()->isLocked()) { doSizing(p); displaySize(); } } } break; case COLORPICK: { if(mCanvas->makeSelection(clickPoint,FALSE)==NULL) return; QColor c=mCanvas->current()->getPixel(p.x(),p.y()); if(e->button()==Qt::LeftButton) { slotFgColorChanged(c); } else { slotBgColorChanged(c); } } break; // case TEXT: case IMAGE: mCanvas->current()->setFinal(TRUE); break; default: break; } // if(mCanvas->current()) (mCanvas->current()->setSelected(!mCanvas->current()->isSelected())); //debug("updating canvas"); if(mCanvas->current()) mCanvas->setChanged(mCanvas->current()->rect()); mCanvas->update(); actionType=SELECT; sbp->message("Select a tool,or click on object to select"); } void editorView::slotSetBg() { mCanvas->setBackgroundColor(ef->fgbgFrame->getBackgroundColor()); } void editorView::slotClearAll() { // slotSetBg(); mCanvas->clearAll(); modified=FALSE; } void editorView::slotFgColorChanged(QColor c) { // debug ("set fg color %x",c.rgb()); ef->fgbgFrame->setForegroundColor(c); } void editorView::slotBgColorChanged(QColor c) { // debug ("set bg color %x",c.rgb()); ef->fgbgFrame->setBackgroundColor(c); } void editorView::save(QFile &f,bool templ) { mCanvas->save(f,templ); if(templ) modified=FALSE; // save as template is needed to be able to edit the image later } void editorView::open(QFile &f) { if(mCanvas->count()!=0) modified=TRUE; // set modified if we combining images mCanvas->load(f); } QImage *editorView::getImage() { // im.reset(); // im.create(contentsWidth(),contentsHeight(),32); QPixmap pm(contentsWidth(),contentsHeight()); mCanvas->resetSelected(); pm.fill(Qt::darkGray); QPainter p(&pm); mCanvas->drawArea( mCanvas->rect(), &p ); im=pm.convertToImage(); return (&im); } void editorView::checkResizing() { sizing=mCanvas->current()->checkGrip(clickPoint); switch(sizing) { case SLT: case SRB: setCursor(Qt::SizeFDiagCursor); break; case SRT: case SLB: setCursor(Qt::SizeBDiagCursor); break; case SMT: case SMB: setCursor(Qt::SizeVerCursor ); break; case SLC: case SRC: setCursor(Qt::SizeHorCursor ); break; case SMOVE: setCursor(Qt::SizeAllCursor); break; } } void editorView::doSizing(QPoint p) { switch(sizing) { //debug( posx=%d,posy=%d, case SLT: execSizing(p,mCanvas->current()->endPoint()); break; case SRB: execSizing(mCanvas->current()->startPoint(),p); break; case SRT: execSizing(QPoint(mCanvas->current()->startPoint().x(),p.y()),QPoint(p.x(),mCanvas->current()->endPoint().y())); break; case SLB: execSizing(QPoint(p.x(),mCanvas->current()->startPoint().y()),QPoint(mCanvas->current()->endPoint().x(),p.y())); break; case SMT: execSizing(QPoint(mCanvas->current()->startPoint().x(),p.y()),mCanvas->current()->endPoint()); break; case SMB: execSizing(mCanvas->current()->startPoint(),QPoint(mCanvas->current()->endPoint().x(),p.y())); break; break; case SLC: execSizing(QPoint(p.x(),mCanvas->current()->startPoint().y()),mCanvas->current()->endPoint()); break; case SRC: execSizing(mCanvas->current()->startPoint(),QPoint(p.x(),mCanvas->current()->endPoint().y())); break; case SMOVE: mCanvas->current()->moveBy(p.x() - clickPoint.x(),p.y() - clickPoint.y()); clickPoint=p; break; } modified=TRUE; mCanvas->update(); } void editorView::execSizing(QPoint s,QPoint e) { if((s.x()>=e.x()) || (s.y()>=e.y())) return; mCanvas->current()->setGeometry(s,e); } void editorView::slotHorGradient() { if(!mCanvas->checkSelected()) return; mCanvas->current()->setGradient(1); mCanvas->setChanged(mCanvas->current()->rect()); mCanvas->update(); } void editorView::slotVerGradient() { if(!mCanvas->checkSelected()) return; mCanvas->current()->setGradient(2); mCanvas->setChanged(mCanvas->current()->rect()); mCanvas->update(); } void editorView::setRotate(int r) { rotateValue=r; if(!mCanvas->checkSelected()) return; mCanvas->current()->setRotate(rotateValue); mCanvas->setChanged(mCanvas->current()->rect()); mCanvas->update(); } void editorView::setShear(double s) { shearValue=s; if(!mCanvas->checkSelected()) return; mCanvas->current()->setShear(shearValue); mCanvas->setChanged(mCanvas->current()->rect()); mCanvas->update(); } void editorView::slotTextReturnPressed(const QString &t) { lineEditString=t; } void editorView::slotSelectFont() { bool ok; QFont f = QFontDialog::getFont( &ok, aFont ); if ( ok ) { aFont = f; } repaint(FALSE); }