krita

kis_tool_freehand.cc

00001 /*
00002  *  kis_tool_brush.cc - part of Krita
00003  *
00004  *  Copyright (c) 2003-2004 Boudewijn Rempt <boud@valdyas.org>
00005  *  Copyright (c) 2004 Bart Coppens <kde@bartcoppens.be>
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; either version 2 of the License, or
00010  *  (at your option) any later version.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  */
00021 #include <qevent.h>
00022 #include <qlabel.h>
00023 #include <qlayout.h>
00024 #include <qwidget.h>
00025 #include <qrect.h>
00026 
00027 #include <kdebug.h>
00028 #include <kaction.h>
00029 #include <kcommand.h>
00030 #include <klocale.h>
00031 
00032 #include "kis_canvas_subject.h"
00033 #include "kis_undo_adapter.h"
00034 #include "kis_selection.h"
00035 #include "kis_painter.h"
00036 #include "kis_fill_painter.h"
00037 #include "kis_tool_freehand.h"
00038 #include "kis_cursor.h"
00039 #include "kis_button_press_event.h"
00040 #include "kis_button_release_event.h"
00041 #include "kis_move_event.h"
00042 #include "kis_layer.h"
00043 #include "kis_group_layer.h"
00044 #include "kis_paint_layer.h"
00045 #include "kis_canvas.h"
00046 #include "kis_canvas_painter.h"
00047 #include "kis_boundary_painter.h"
00048 #include "kis_brush.h"
00049 
00050 KisToolFreehand::KisToolFreehand(QString transactionText)
00051         : super(transactionText),
00052         m_dragDist ( 0 ),
00053         m_transactionText(transactionText),
00054         m_mode( HOVER )
00055 {
00056     m_painter = 0;
00057     m_currentImage = 0;
00058     m_tempLayer = 0;
00059     m_paintIncremental = true;
00060     m_paintOnSelection = false;
00061     m_paintedOutline = false;
00062 }
00063 
00064 KisToolFreehand::~KisToolFreehand()
00065 {
00066 }
00067 
00068 void KisToolFreehand::update(KisCanvasSubject *subject)
00069 {
00070     super::update(subject);
00071     m_currentImage = m_subject->currentImg();
00072 }
00073 
00074 void KisToolFreehand::buttonPress(KisButtonPressEvent *e)
00075 {
00076     if (!m_subject) return;
00077 
00078     if (!m_subject->currentBrush()) return;
00079 
00080     if (!m_currentImage || !m_currentImage->activeDevice()) return;
00081 
00082     if (e->button() == QMouseEvent::LeftButton) {
00083         
00084         // People complain that they can't start brush strokes outside of the image boundaries.
00085         // This makes sense, especially when combined with BUG:132759, so commenting out the
00086         // next line makes sense.
00087         //if (!m_currentImage->bounds().contains(e->pos().floorQPoint())) return;
00088         
00089         initPaint(e);
00090         paintAt(e->pos(), e->pressure(), e->xTilt(), e->yTilt());
00091 
00092         m_prevPos = e->pos();
00093         m_prevPressure = e->pressure();
00094         m_prevXTilt = e->xTilt();
00095         m_prevYTilt = e->yTilt();
00096 
00097         QRect r = m_painter->dirtyRect();
00098         if ( r.isValid() ) {
00099             m_dirtyRect = r;
00100 
00101             r = QRect(r.left()-1, r.top()-1, r.width()+2, r.height()+2); //needed to update selectionvisualization
00102             if (!m_paintOnSelection) {
00103                     m_currentImage->activeLayer()->setDirty(r);
00104             }
00105             else {
00106                 m_target->setDirty(r);
00107                 // Just update the canvas. XXX: After 1.5, find a better way to make sure tools don't set dirty what they didn't touch.
00108                 m_subject->canvasController()->updateCanvas( r );
00109             }
00110         }
00111     }
00112 }
00113 
00114 void KisToolFreehand::buttonRelease(KisButtonReleaseEvent* e)
00115 {
00116     if (e->button() == QMouseEvent::LeftButton && m_mode == PAINT) {
00117         endPaint();
00118     }
00119     KisToolPaint::buttonRelease(e);
00120 }
00121 
00122 void KisToolFreehand::move(KisMoveEvent *e)
00123 {
00124     if (m_mode == PAINT) {
00125 
00126         paintLine(m_prevPos, m_prevPressure, m_prevXTilt, m_prevYTilt, e->pos(), e->pressure(), e->xTilt(), e->yTilt());
00127 
00128         m_prevPos = e->pos();
00129         m_prevPressure = e->pressure();
00130         m_prevXTilt = e->xTilt();
00131         m_prevYTilt = e->yTilt();
00132 
00133         QRect r = m_painter->dirtyRect();
00134 
00135         if (r.isValid()) {
00136             m_dirtyRect |= r;
00137 
00138             if (!m_paintOnSelection) {
00139                     m_currentImage->activeLayer()->setDirty(r);
00140             }
00141             else {
00142                 // Just update the canvas
00143                 r = QRect(r.left()-1, r.top()-1, r.width()+2, r.height()+2); //needed to update selectionvisualization
00144                 m_target->setDirty(r);
00145                 m_subject->canvasController()->updateCanvas( r );
00146             }
00147         }
00148     }
00149 }
00150 
00151 void KisToolFreehand::initPaint(KisEvent *)
00152 {
00153     if (!m_currentImage || !m_currentImage->activeDevice()) return;
00154 
00155     m_mode = PAINT;
00156     m_dragDist = 0;
00157 
00158     // Create painter
00159     KisPaintDeviceSP device;
00160     if (m_currentImage && (device = m_currentImage->activeDevice())) {
00161 
00162         if (m_painter)
00163             delete m_painter;
00164 
00165         if (!m_paintIncremental) {
00166             if (m_currentImage->undo())
00167                 m_currentImage->undoAdapter()->beginMacro(m_transactionText);
00168 
00169             KisLayerSupportsIndirectPainting* layer;
00170             if ((layer = dynamic_cast<KisLayerSupportsIndirectPainting*>(
00171                     m_currentImage->activeLayer().data()))) {
00172 
00173                 // Hack for the painting of single-layered layers using indirect painting,
00174                 // because the group layer would not have a correctly synched cache (
00175                 // because of an optimization that would happen, having this layer as
00176                 // projection).
00177                 KisLayer* l = layer->layer();
00178                 KisPaintLayer* pl = dynamic_cast<KisPaintLayer*>(l);
00179                 if (l->parent() && (l->parent()->parent() == 0)
00180                     && (l->parent()->childCount() == 1)
00181                     && l->parent()->paintLayerInducesProjectionOptimization(pl)) {
00182                     // If there's a mask, device could've been the mask. The induce function
00183                     // should catch this, but better safe than sorry
00184                     l->parent()->resetProjection(pl->paintDevice());
00185                 }
00186 
00187                 m_target = new KisPaintDevice(m_currentImage->activeLayer(),
00188                                               device->colorSpace());
00189                 layer->setTemporaryTarget(m_target);
00190                 layer->setTemporaryCompositeOp(m_compositeOp);
00191                 layer->setTemporaryOpacity(m_opacity);
00192 
00193                 if (device->hasSelection())
00194                     m_target->setSelection(device->selection());
00195             }
00196         } else {
00197             m_target = device;
00198         }
00199         if(m_target->hasSelection()) m_target->selection()->startCachingExactRect();
00200         m_painter = new KisPainter( m_target );
00201         Q_CHECK_PTR(m_painter);
00202         m_source = device;
00203         if (currentImage()->undo()) m_painter->beginTransaction(m_transactionText);
00204     }
00205 
00206     m_painter->setPaintColor(m_subject->fgColor());
00207     m_painter->setBackgroundColor(m_subject->bgColor());
00208     m_painter->setBrush(m_subject->currentBrush());
00209 
00210 
00211     // if you're drawing on a temporary layer, the layer already sets this
00212     if (m_paintIncremental) {
00213         m_painter->setCompositeOp(m_compositeOp);
00214         m_painter->setOpacity(m_opacity);
00215     } else {
00216         m_painter->setCompositeOp(COMPOSITE_ALPHA_DARKEN);
00217         m_painter->setOpacity( OPACITY_OPAQUE );
00218 
00219     }
00220 
00221 /*    kdDebug() << "target: " << m_target << "( " << m_target->name() << " )"
00222             << " source: " << m_source << "( " << m_source->name() << " )"
00223             << ", incremental " << m_paintIncremental
00224             << ", paint on selection: " << m_paintOnSelection
00225             << ", active device has selection: " << device->hasSelection()
00226             << ", target has selection: " << m_target->hasSelection()
00227             << endl;
00228 */
00229 }
00230 
00231 void KisToolFreehand::endPaint()
00232 {
00233     m_mode = HOVER;
00234     if (m_currentImage) {
00235 
00236         if (m_painter) {
00237             // If painting in mouse release, make sure painter
00238             // is destructed or end()ed
00239             if (!m_paintIncremental) {
00240                 if (m_currentImage->undo())
00241                     m_painter->endTransaction();
00242                 KisPainter painter( m_source );
00243                 painter.setCompositeOp(m_compositeOp);
00244                 if (m_currentImage->undo())
00245                     painter.beginTransaction(m_transactionText);
00246                 painter.bitBlt(m_dirtyRect.x(), m_dirtyRect.y(), m_compositeOp, m_target,
00247                                m_opacity,
00248                                m_dirtyRect.x(), m_dirtyRect.y(),
00249                                m_dirtyRect.width(), m_dirtyRect.height());
00250 
00251                 KisLayerSupportsIndirectPainting* layer =
00252                     dynamic_cast<KisLayerSupportsIndirectPainting*>(m_source->parentLayer());
00253                 layer->setTemporaryTarget(0);
00254                 m_source->parentLayer()->setDirty(m_dirtyRect);
00255 
00256                 if (m_currentImage->undo()) {
00257                     m_currentImage->undoAdapter()->addCommand(painter.endTransaction());
00258                     m_currentImage->undoAdapter()->endMacro();
00259                 }
00260             } else {
00261                 if (m_currentImage->undo())
00262                     m_currentImage->undoAdapter()->addCommand(m_painter->endTransaction());
00263             }
00264         }
00265         delete m_painter;
00266         m_painter = 0;
00267         notifyModified();
00268         if(m_target->hasSelection()) m_target->selection()->stopCachingExactRect();
00269     }
00270 }
00271 
00272 void KisToolFreehand::paintAt(const KisPoint &pos,
00273                const double pressure,
00274                const double xTilt,
00275                const double yTilt)
00276 {
00277     painter()->paintAt(pos, pressure, xTilt, yTilt);
00278 }
00279 
00280 void KisToolFreehand::paintLine(const KisPoint & pos1,
00281                  const double pressure1,
00282                  const double xtilt1,
00283                  const double ytilt1,
00284                  const KisPoint & pos2,
00285                  const double pressure2,
00286                  const double xtilt2,
00287                  const double ytilt2)
00288 {
00289     m_dragDist = painter()->paintLine(pos1, pressure1, xtilt1, ytilt1, pos2, pressure2, xtilt2, ytilt2, m_dragDist);
00290 }
00291 
00292 
00293 KisImageSP KisToolFreehand::currentImage()
00294 {
00295     return m_currentImage;
00296 }
00297 
00298 
00299 void KisToolFreehand::paintOutline(const KisPoint& point) {
00300     if (!m_subject) {
00301         return;
00302     }
00303 
00304     KisCanvasController *controller = m_subject->canvasController();
00305 
00306     if (currentImage() && !currentImage()->bounds().contains(point.floorQPoint())) {
00307         if (m_paintedOutline) {
00308             controller->kiscanvas()->update();
00309             m_paintedOutline = false;
00310         }
00311         return;
00312     }
00313 
00314     KisCanvas *canvas = controller->kiscanvas();
00315     canvas->repaint();
00316 
00317     KisBrush *brush = m_subject->currentBrush();
00318     // There may not be a brush present, and we shouldn't crash in that case
00319     if (brush) {
00320         KisCanvasPainter gc(canvas);
00321         QPen pen(Qt::SolidLine);
00322 
00323         KisPoint hotSpot = brush->hotSpot();
00324 
00325         gc.setRasterOp(Qt::NotROP);
00326         gc.setPen(pen);
00327         gc.setViewport(0, 0, static_cast<Q_INT32>(canvas->width() * m_subject->zoomFactor()),
00328                        static_cast<Q_INT32>(canvas->height() * m_subject->zoomFactor()));
00329         gc.translate((- controller->horzValue()) / m_subject->zoomFactor(),
00330                         (- controller->vertValue()) / m_subject->zoomFactor());
00331 
00332         KisPoint topLeft = point - hotSpot;
00333 
00334         if (m_subject->currentPaintop().id() == "pen") {
00335             // Pen paints on whole pixels only.
00336             topLeft = topLeft.roundQPoint();
00337         }
00338 
00339         gc.translate(topLeft.x(), topLeft.y());
00340 
00341         KisBoundaryPainter::paint(brush->boundary(), gc);
00342         m_paintedOutline = true;
00343     }
00344 }
00345 
00346 
00347 #include "kis_tool_freehand.moc"
00348 
KDE Home | KDE Accessibility Home | Description of Access Keys