File indexing completed on 2024-12-15 04:03:31

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_WIDGET_ERASER_SIZE 0
0030 
0031 
0032 #include "kpToolWidgetEraserSize.h"
0033 
0034 #include "imagelib/kpPainter.h"
0035 #include "pixmapfx/kpPixmapFX.h"
0036 #include "tools/kpTool.h"
0037 
0038 #include "kpLogCategories.h"
0039 #include <KLocalizedString>
0040 
0041 #include <QPainter>
0042 
0043 
0044 static int EraserSizes [] = {2, 3, 5, 9, 17, 29};
0045 static const int NumEraserSizes =
0046     int (sizeof (::EraserSizes) / sizeof (::EraserSizes [0]));
0047 
0048 
0049 static void DrawImage (kpImage *destImage, const QPoint &topLeft, void *userData)
0050 {
0051     auto *pack = static_cast <kpToolWidgetEraserSize::DrawPackage *> (userData);
0052 
0053     const int size = ::EraserSizes [pack->selected];
0054 
0055     kpPainter::fillRect (destImage,
0056         topLeft.x (), topLeft.y (), size, size,
0057         pack->color);
0058 }
0059 
0060 static void DrawCursor (kpImage *destImage, const QPoint &topLeft, void *userData)
0061 {
0062     ::DrawImage (destImage, topLeft, userData);
0063 
0064 
0065     auto *pack = static_cast <kpToolWidgetEraserSize::DrawPackage *> (userData);
0066 
0067     const int size = ::EraserSizes [pack->selected];
0068 
0069     // Would 1-pixel border on all sides completely cover the color of the
0070     // eraser?
0071     if (size <= 2) {
0072         return;
0073     }
0074 
0075     // Draw 1-pixel border on all sides.
0076     QPainter painter(destImage);
0077     painter.drawRect(topLeft.x(), topLeft.y(), size - 1, size - 1);
0078 }
0079 
0080 //---------------------------------------------------------------------
0081 
0082 kpToolWidgetEraserSize::kpToolWidgetEraserSize (QWidget *parent, const QString &name)
0083     : kpToolWidgetBase (parent, name)
0084 {
0085     for (int i = 0; i < ::NumEraserSizes; i++)
0086     {
0087         if (i == 3 || i == 5) {
0088             startNewOptionRow ();
0089         }
0090 
0091         const int s = ::EraserSizes [i];
0092 
0093         QImage previewPixmap (s, s, QImage::Format_ARGB32_Premultiplied);
0094         if (i < 3)
0095         {
0096             // HACK: kpToolWidgetBase's layout code sucks and gives uneven spacing
0097             previewPixmap = QImage ((width () - 4) / 3, 9, QImage::Format_ARGB32_Premultiplied);
0098             Q_ASSERT (previewPixmap.width () >= s &&
0099                 previewPixmap.height () >= s);
0100         }
0101 
0102         previewPixmap.fill(0);
0103 
0104         DrawPackage pack = drawFunctionDataForSelected (kpColor::Black, i);
0105         ::DrawImage (&previewPixmap,
0106             QPoint ((previewPixmap.width () - s) / 2,
0107                     (previewPixmap.height () - s) / 2),
0108             &pack);
0109 
0110         addOption(QPixmap::fromImage(std::move(previewPixmap)),
0111                   i18n("%1x%2", s, s) /*tooltip*/);
0112     }
0113 
0114     finishConstruction (1, 0);
0115 }
0116 
0117 //---------------------------------------------------------------------
0118 
0119 kpToolWidgetEraserSize::~kpToolWidgetEraserSize () = default;
0120 
0121 //---------------------------------------------------------------------
0122 
0123 
0124 // public
0125 int kpToolWidgetEraserSize::eraserSize () const
0126 {
0127     return ::EraserSizes[selected() < 0 ? 0 : selected()];
0128 }
0129 
0130 
0131 // public
0132 kpTempImage::UserFunctionType kpToolWidgetEraserSize::drawFunction () const
0133 {
0134     return &::DrawImage;
0135 }
0136 
0137 // public
0138 kpTempImage::UserFunctionType kpToolWidgetEraserSize::drawCursorFunction () const
0139 {
0140     return &::DrawCursor;
0141 }
0142 
0143 //---------------------------------------------------------------------
0144 
0145 
0146 // public static
0147 kpToolWidgetEraserSize::DrawPackage kpToolWidgetEraserSize::drawFunctionDataForSelected (
0148         const kpColor &color, int selectedIndex)
0149 {
0150     DrawPackage pack;
0151 
0152     pack.selected = selectedIndex;
0153     pack.color = color;
0154 
0155     return pack;
0156 }
0157 
0158 //---------------------------------------------------------------------
0159 
0160 // public
0161 kpToolWidgetEraserSize::DrawPackage kpToolWidgetEraserSize::drawFunctionData (
0162         const kpColor &color) const
0163 {
0164     return drawFunctionDataForSelected (color, selected ());
0165 }
0166 
0167 //---------------------------------------------------------------------
0168 
0169 // protected slot virtual [base kpToolWidgetBase]
0170 bool kpToolWidgetEraserSize::setSelected (int row, int col, bool saveAsDefault)
0171 {
0172     const bool ret = kpToolWidgetBase::setSelected (row, col, saveAsDefault);
0173     if (ret) {
0174         Q_EMIT eraserSizeChanged (eraserSize ());
0175     }
0176     return ret;
0177 }
0178 
0179 //---------------------------------------------------------------------
0180 
0181 #include "moc_kpToolWidgetEraserSize.cpp"