File indexing completed on 2024-05-19 04:23:09
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_COLOR_SIMILARITY_TOOL_BAR_ITEM 0 0030 0031 0032 #include "kpColorSimilarityToolBarItem.h" 0033 0034 #include <QTimer> 0035 #include <QPixmap> 0036 0037 #include <KConfigGroup> 0038 #include <KLocalizedString> 0039 #include <KSharedConfig> 0040 0041 #include "imagelib/kpColor.h" 0042 #include "dialogs/kpColorSimilarityDialog.h" 0043 #include "kpColorSimilarityCubeRenderer.h" 0044 #include "kpDefs.h" 0045 0046 //--------------------------------------------------------------------- 0047 0048 kpColorSimilarityToolBarItem::kpColorSimilarityToolBarItem (QWidget *parent) 0049 : QToolButton (parent), 0050 kpColorSimilarityHolder (), 0051 0052 m_oldColorSimilarity (0), 0053 m_processedColorSimilarity (kpColor::Exact), 0054 m_flashTimer (new QTimer (this)), 0055 m_flashHighlight (0), 0056 m_suppressingFlashCounter (0) 0057 { 0058 setAutoRaise (true); 0059 setFixedSize (52, 52); 0060 0061 setWhatsThis (WhatsThisWithClickInstructions ()); 0062 0063 connect (this, &kpColorSimilarityToolBarItem::clicked, 0064 this, &kpColorSimilarityToolBarItem::openDialog); 0065 0066 KConfigGroup cfg (KSharedConfig::openConfig (), QStringLiteral(kpSettingsGroupGeneral)); 0067 setColorSimilarityInternal (cfg.readEntry (kpSettingColorSimilarity, 0.0), 0068 false/*don't write config*/); 0069 0070 m_flashTimer->setInterval (100/*ms*/); 0071 connect (m_flashTimer, &QTimer::timeout, 0072 this, &kpColorSimilarityToolBarItem::slotFlashTimerTimeout); 0073 } 0074 0075 //--------------------------------------------------------------------- 0076 0077 // public 0078 int kpColorSimilarityToolBarItem::processedColorSimilarity () const 0079 { 0080 return m_processedColorSimilarity; 0081 } 0082 0083 //--------------------------------------------------------------------- 0084 0085 // private 0086 void kpColorSimilarityToolBarItem::setColorSimilarityInternal (double similarity, 0087 bool writeConfig) 0088 { 0089 #if DEBUG_KP_COLOR_SIMILARITY_TOOL_BAR_ITEM 0090 qCDebug(kpLogWidgets) << "kpColorSimilarityToolBarItem::setColorSimilarityInternal(" 0091 << "similarity=" << similarity << ",writeConfig=" << writeConfig 0092 << ")"; 0093 #endif 0094 0095 m_oldColorSimilarity = colorSimilarity (); 0096 0097 kpColorSimilarityHolder::setColorSimilarity (similarity); 0098 m_processedColorSimilarity = kpColor::processSimilarity (colorSimilarity ()); 0099 0100 updateIcon (); 0101 updateToolTip (); 0102 0103 if (writeConfig) 0104 { 0105 KConfigGroup cfg (KSharedConfig::openConfig (), QStringLiteral(kpSettingsGroupGeneral)); 0106 cfg.writeEntry (kpSettingColorSimilarity, colorSimilarity ()); 0107 cfg.sync (); 0108 } 0109 0110 Q_EMIT colorSimilarityChanged (colorSimilarity (), m_processedColorSimilarity); 0111 } 0112 0113 //--------------------------------------------------------------------- 0114 0115 // public virtual [base kopColorSimilarityHolder] 0116 void kpColorSimilarityToolBarItem::setColorSimilarity (double similarity) 0117 { 0118 // (this calls the base setColorSimilarity() as required by base) 0119 setColorSimilarityInternal (similarity, true/*write config*/); 0120 } 0121 0122 //--------------------------------------------------------------------- 0123 0124 // public 0125 double kpColorSimilarityToolBarItem::oldColorSimilarity () const 0126 { 0127 return m_oldColorSimilarity; 0128 } 0129 0130 //--------------------------------------------------------------------- 0131 0132 // public 0133 void kpColorSimilarityToolBarItem::openDialog () 0134 { 0135 kpColorSimilarityDialog dialog (this); 0136 dialog.setColorSimilarity (colorSimilarity ()); 0137 if (dialog.exec ()) 0138 { 0139 setColorSimilarity (dialog.colorSimilarity ()); 0140 } 0141 } 0142 0143 //--------------------------------------------------------------------- 0144 0145 // private slot: 0146 void kpColorSimilarityToolBarItem::slotFlashTimerTimeout () 0147 { 0148 #if DEBUG_KP_COLOR_SIMILARITY_TOOL_BAR_ITEM 0149 qCDebug(kpLogWidgets) << "kpColorSimilarityToolBarItem::slotFlashTimerTimeout()" 0150 << " highlight=" << m_flashHighlight << endl; 0151 #endif 0152 int newHigh = m_flashHighlight - 20; 0153 if (newHigh < 0) { 0154 newHigh = 0; 0155 } 0156 0157 m_flashHighlight = newHigh; 0158 0159 updateIcon (); 0160 0161 if (newHigh == 0) { 0162 m_flashTimer->stop (); 0163 } 0164 } 0165 0166 //--------------------------------------------------------------------- 0167 0168 // public 0169 void kpColorSimilarityToolBarItem::flash () 0170 { 0171 #if DEBUG_KP_COLOR_SIMILARITY_TOOL_BAR_ITEM 0172 qCDebug(kpLogWidgets) << "kpColorSimilarityToolBarItem::flash()"; 0173 #endif 0174 if (isSuppressingFlash ()) { 0175 return; 0176 } 0177 0178 if (m_flashHighlight == 255) 0179 { 0180 #if DEBUG_KP_COLOR_SIMILARITY_TOOL_BAR_ITEM 0181 qCDebug(kpLogWidgets) << "\tNOP"; 0182 #endif 0183 } 0184 else 0185 { 0186 m_flashHighlight = 255; 0187 0188 updateIcon (); 0189 } 0190 0191 m_flashTimer->start (); 0192 } 0193 0194 //--------------------------------------------------------------------- 0195 0196 // public 0197 bool kpColorSimilarityToolBarItem::isSuppressingFlash () const 0198 { 0199 return (m_suppressingFlashCounter > 0); 0200 } 0201 0202 //--------------------------------------------------------------------- 0203 0204 // public 0205 void kpColorSimilarityToolBarItem::suppressFlash () 0206 { 0207 m_suppressingFlashCounter++; 0208 } 0209 0210 //--------------------------------------------------------------------- 0211 0212 // public 0213 void kpColorSimilarityToolBarItem::unsupressFlash () 0214 { 0215 m_suppressingFlashCounter--; 0216 Q_ASSERT (m_suppressingFlashCounter >= 0); 0217 } 0218 0219 //--------------------------------------------------------------------- 0220 0221 // private 0222 void kpColorSimilarityToolBarItem::updateToolTip () 0223 { 0224 #if DEBUG_KP_COLOR_SIMILARITY_TOOL_BAR_ITEM 0225 qCDebug(kpLogWidgets) << "kpColorSimilarityToolBarItem::updateToolTip()"; 0226 #endif 0227 0228 if (colorSimilarity () > 0) 0229 { 0230 setToolTip ( 0231 i18n ("<p>Color Similarity: %1%</p>" 0232 "<p align=\"center\">Click to configure.</p>", 0233 qRound (colorSimilarity () * 100))); 0234 } 0235 else 0236 { 0237 setToolTip ( 0238 i18n ("<p>Color Similarity: Exact Match</p>" 0239 "<p align=\"center\">Click to configure.</p>")); 0240 } 0241 } 0242 0243 //--------------------------------------------------------------------- 0244 0245 // private 0246 // LOOPT: This gets called twice on KolourPaint startup by: 0247 // 0248 // 1. setColorSimilarityInternal() called by the ctor 0249 // 2. resizeEvent() when it's first shown() 0250 // 0251 // We could get rid of the first and save a few milliseconds. 0252 void kpColorSimilarityToolBarItem::updateIcon () 0253 { 0254 const int side = width () * 6 / 8; 0255 #if DEBUG_KP_COLOR_SIMILARITY_TOOL_BAR_ITEM 0256 qCDebug(kpLogWidgets) << "kpColorSimilarityToolBarItem::updateIcon() width=" << width () 0257 << " side=" << side; 0258 #endif 0259 0260 QPixmap icon(side, side); 0261 icon.fill(Qt::transparent); 0262 0263 kpColorSimilarityCubeRenderer::Paint (&icon, 0264 0/*x*/, 0/*y*/, side, 0265 colorSimilarity (), m_flashHighlight); 0266 0267 setIconSize(QSize(side, side)); 0268 setIcon(icon); 0269 } 0270 0271 //--------------------------------------------------------------------- 0272 0273 // private virtual [base QWidget] 0274 void kpColorSimilarityToolBarItem::resizeEvent (QResizeEvent *e) 0275 { 0276 #if DEBUG_KP_COLOR_SIMILARITY_TOOL_BAR_ITEM 0277 qCDebug(kpLogWidgets) << "kpColorSimilarityToolBarItem::resizeEvent() size=" << size () 0278 << " oldSize=" << e->oldSize (); 0279 #endif 0280 QToolButton::resizeEvent (e); 0281 0282 updateIcon (); 0283 } 0284 0285 //--------------------------------------------------------------------- 0286 0287 #include "moc_kpColorSimilarityToolBarItem.cpp"