File indexing completed on 2024-06-16 04:10:04

0001 
0002 /*
0003    Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
0004    All rights reserved.
0005 
0006    Redistribution and use in source and binary forms, with or without
0007    modification, are permitted provided that the following conditions
0008    are met:
0009 
0010    1. Redistributions of source code must retain the above copyright
0011       notice, this list of conditions and the following disclaimer.
0012    2. Redistributions in binary form must reproduce the above copyright
0013       notice, this list of conditions and the following disclaimer in the
0014       documentation and/or other materials provided with the distribution.
0015 
0016    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0017    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0018    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0019    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0020    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0021    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0022    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0023    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0024    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0025    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0026 */
0027 
0028 
0029 #define DEBUG_KP_TOOL_FREE_FROM_SELECTION 0
0030 
0031 
0032 #include "kpToolFreeFormSelection.h"
0033 
0034 #include "kpLogCategories.h"
0035 #include <KLocalizedString>
0036 
0037 #include "document/kpDocument.h"
0038 #include "layers/selections/image/kpFreeFormImageSelection.h"
0039 #include "environments/tools/selection/kpToolSelectionEnvironment.h"
0040 
0041 //---------------------------------------------------------------------
0042 
0043 kpToolFreeFormSelection::kpToolFreeFormSelection (kpToolSelectionEnvironment *environ,
0044         QObject *parent)
0045     : kpAbstractImageSelectionTool (i18n ("Selection (Free-Form)"),
0046                        i18n ("Makes a free-form selection"),
0047                        Qt::Key_M,
0048                        environ, parent,
0049                        QStringLiteral("tool_free_form_selection"))
0050 {
0051 }
0052 
0053 //---------------------------------------------------------------------
0054 
0055 kpToolFreeFormSelection::~kpToolFreeFormSelection () = default;
0056 
0057 //---------------------------------------------------------------------
0058 
0059 // protected virtual [base kpAbstractSelectionTool]
0060 bool kpToolFreeFormSelection::drawCreateMoreSelectionAndUpdateStatusBar (
0061         bool dragAccepted,
0062         const QPoint &accidentalDragAdjustedPoint,
0063         const QRect &/*normalizedRect*/)
0064 {
0065 #if DEBUG_KP_TOOL_FREE_FROM_SELECTION
0066     qCDebug(kpLogTools) << "kpToolFreeFormSelection::createMoreSelectionAndUpdateStatusBar("
0067                << "dragAccepted=" << dragAccepted
0068                << ",accidentalDragAdjustedPoint=" << accidentalDragAdjustedPoint
0069                << ")";
0070 #endif
0071 
0072     // Prevent unintentional creation of 1-pixel selections.
0073     if (!dragAccepted && accidentalDragAdjustedPoint == startPoint ())
0074     {
0075     #if DEBUG_KP_TOOL_FREE_FROM_SELECTION && 1
0076         qCDebug(kpLogTools) << "\tnon-text NOP - return";
0077     #endif
0078         setUserShapePoints (accidentalDragAdjustedPoint);
0079         return false;
0080     }
0081 
0082     Q_ASSERT (accidentalDragAdjustedPoint == currentPoint ());
0083     Q_ASSERT (dragAccepted == static_cast<bool> (document ()->selection ()));
0084 
0085     const kpFreeFormImageSelection *oldPointsSel = nullptr;
0086     if (document ()->selection ())
0087     {
0088         kpAbstractSelection *sel = document ()->selection ();
0089         Q_ASSERT (dynamic_cast <kpFreeFormImageSelection *> (sel));
0090         oldPointsSel = dynamic_cast <kpFreeFormImageSelection *> (sel);
0091     }
0092 
0093 
0094     QPolygon points;
0095 
0096     // First point in drag?
0097     if (!dragAccepted)
0098     {
0099       points.append (startPoint ());
0100     }
0101     // Not first point in drag.
0102     else
0103     {
0104         if ( !oldPointsSel ) {  // assert above says we never reach this, but let's make coverity happy
0105             return false;
0106         }
0107 
0108       // Get existing points in selection.
0109       points = oldPointsSel->cardinallyAdjacentPoints ();
0110     }
0111 
0112 
0113 #if DEBUG_KP_TOOL_FREE_FROM_SELECTION
0114     qCDebug(kpLogTools) << "\tlast old point=" << points.last ();
0115 #endif
0116 
0117     // TODO: There should be an upper limit on this before drawing the
0118     //       polygon becomes too slow.
0119     points.append (accidentalDragAdjustedPoint);
0120 
0121 
0122     document ()->setSelection (
0123         kpFreeFormImageSelection (points, environ ()->imageSelectionTransparency ()));
0124 
0125     // Prevent accidental usage of dangling pointer to old selection
0126     // (deleted by kpDocument::setSelection()).
0127     oldPointsSel = nullptr;
0128 
0129 #if DEBUG_KP_TOOL_FREE_FROM_SELECTION && 1
0130     qCDebug(kpLogTools) << "\t\tfreeform; #points="
0131               << document ()->selection ()->calculatePoints ().count ();
0132 #endif
0133 
0134     setUserShapePoints (accidentalDragAdjustedPoint);
0135 
0136     return true;
0137 }
0138 
0139 //---------------------------------------------------------------------