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 #define DEBUG_KP_TOOL_RECT_SELECTION 0 0029 0030 #include "kpToolRectSelection.h" 0031 #include "kpLogCategories.h" 0032 #include "document/kpDocument.h" 0033 #include "layers/selections/image/kpRectangularImageSelection.h" 0034 #include "environments/tools/selection/kpToolSelectionEnvironment.h" 0035 0036 #include <KLocalizedString> 0037 0038 kpToolRectSelection::kpToolRectSelection (kpToolSelectionEnvironment *environ, 0039 QObject *parent) 0040 : kpAbstractImageSelectionTool (i18n ("Selection (Rectangular)"), 0041 i18n ("Makes a rectangular selection"), 0042 Qt::Key_S, 0043 environ, parent, 0044 QStringLiteral("tool_rect_selection")) 0045 { 0046 } 0047 0048 kpToolRectSelection::~kpToolRectSelection () = default; 0049 0050 0051 // protected virtual [base kpAbstractSelectionTool] 0052 bool kpToolRectSelection::drawCreateMoreSelectionAndUpdateStatusBar ( 0053 bool dragAccepted, 0054 const QPoint &accidentalDragAdjustedPoint, 0055 const QRect &normalizedRect) 0056 { 0057 // Prevent unintentional creation of 1-pixel selections. 0058 // REFACTOR: This line is duplicated code with other tools. 0059 if (!dragAccepted && accidentalDragAdjustedPoint == startPoint ()) 0060 { 0061 #if DEBUG_KP_TOOL_RECT_SELECTION && 1 0062 qCDebug(kpLogTools) << "\tnon-text NOP - return"; 0063 #endif 0064 setUserShapePoints (accidentalDragAdjustedPoint); 0065 return false; 0066 } 0067 0068 Q_ASSERT (accidentalDragAdjustedPoint == currentPoint ()); 0069 0070 const QRect usefulRect = normalizedRect.intersected (document ()->rect ()); 0071 document ()->setSelection ( 0072 kpRectangularImageSelection ( 0073 usefulRect, 0074 environ ()->imageSelectionTransparency ())); 0075 0076 setUserShapePoints (startPoint (), 0077 QPoint (qMax (0, qMin (currentPoint ().x (), document ()->width () - 1)), 0078 qMax (0, qMin (currentPoint ().y (), document ()->height () - 1)))); 0079 0080 return true; 0081 }