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_FILL_STYLE 0 0030 0031 0032 #include "kpToolWidgetFillStyle.h" 0033 0034 #include "imagelib/kpColor.h" 0035 #include "kpDefs.h" 0036 #include "pixmapfx/kpPixmapFX.h" 0037 #include "tools/kpTool.h" 0038 0039 #include "kpLogCategories.h" 0040 0041 #include <QPainter> 0042 #include <QPixmap> 0043 0044 //--------------------------------------------------------------------- 0045 0046 kpToolWidgetFillStyle::kpToolWidgetFillStyle (QWidget *parent, const QString &name) 0047 : kpToolWidgetBase (parent, name) 0048 { 0049 for (int i = 0; i < FillStyleNum; i++) 0050 { 0051 QPixmap pixmap; 0052 0053 pixmap = fillStylePixmap (static_cast<FillStyle> (i), 0054 (width () - 2/*margin*/) * 3 / 4, 0055 (height () - 2/*margin*/ - 2/*spacing*/) * 3 / (3 * 4)); 0056 addOption (pixmap, fillStyleName (static_cast<FillStyle> (i))/*tooltip*/); 0057 0058 startNewOptionRow (); 0059 } 0060 0061 finishConstruction (0, 0); 0062 } 0063 0064 //--------------------------------------------------------------------- 0065 0066 kpToolWidgetFillStyle::~kpToolWidgetFillStyle () = default; 0067 0068 //--------------------------------------------------------------------- 0069 0070 // private 0071 QPixmap kpToolWidgetFillStyle::fillStylePixmap (FillStyle fs, int w, int h) 0072 { 0073 QPixmap pixmap ((w <= 0 ? width () : w), (h <= 0 ? height () : h)); 0074 pixmap.fill(palette().color(QPalette::Window)); 0075 0076 const int penWidth = 2; 0077 0078 const QRect rectRect(1, 1, w - 2, h - 2); 0079 0080 QPainter painter(&pixmap); 0081 painter.setPen(kpPixmapFX::QPainterDrawRectPen(Qt::black, penWidth)); 0082 0083 switch ( fs ) 0084 { 0085 case NoFill: 0086 { 0087 painter.setBrush(Qt::NoBrush); 0088 break; 0089 } 0090 case FillWithBackground: 0091 { 0092 painter.setBrush(Qt::gray); 0093 break; 0094 } 0095 case FillWithForeground: 0096 { 0097 painter.setBrush(Qt::black); 0098 break; 0099 } 0100 default: ; 0101 } 0102 0103 painter.drawRect(rectRect); 0104 painter.end(); 0105 0106 return pixmap; 0107 } 0108 0109 //--------------------------------------------------------------------- 0110 0111 // private 0112 QString kpToolWidgetFillStyle::fillStyleName (FillStyle fs) const 0113 { 0114 switch (fs) 0115 { 0116 case NoFill: 0117 return i18n ("No Fill"); 0118 0119 case FillWithBackground: 0120 return i18n ("Fill with Background Color"); 0121 0122 case FillWithForeground: 0123 return i18n ("Fill with Foreground Color"); 0124 0125 default: 0126 return {}; 0127 } 0128 } 0129 0130 //--------------------------------------------------------------------- 0131 0132 // public 0133 kpToolWidgetFillStyle::FillStyle kpToolWidgetFillStyle::fillStyle () const 0134 { 0135 #if DEBUG_KP_TOOL_WIDGET_FILL_STYLE 0136 qCDebug(kpLogWidgets) << "kpToolWidgetFillStyle::fillStyle() selected=" 0137 << selectedRow (); 0138 #endif 0139 return static_cast<FillStyle> (selectedRow ()); 0140 } 0141 0142 //--------------------------------------------------------------------- 0143 0144 kpColor kpToolWidgetFillStyle::drawingBackgroundColor ( 0145 const kpColor &foregroundColor, const kpColor &backgroundColor) const 0146 { 0147 switch (fillStyle ()) 0148 { 0149 default: 0150 case NoFill: 0151 return kpColor::Invalid; 0152 0153 case FillWithBackground: 0154 return backgroundColor; 0155 0156 case FillWithForeground: 0157 return foregroundColor; 0158 } 0159 } 0160 0161 //--------------------------------------------------------------------- 0162 0163 // virtual protected slot [base kpToolWidgetBase] 0164 bool kpToolWidgetFillStyle::setSelected (int row, int col, bool saveAsDefault) 0165 { 0166 const bool ret = kpToolWidgetBase::setSelected (row, col, saveAsDefault); 0167 if (ret) { 0168 Q_EMIT fillStyleChanged (fillStyle ()); 0169 } 0170 return ret; 0171 } 0172 0173 //--------------------------------------------------------------------- 0174 0175 #include "moc_kpToolWidgetFillStyle.cpp"