File indexing completed on 2024-05-19 04:23:11

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 #include <qdockwidget.h>
0030 #define DEBUG_KP_COLOR_TOOL_BAR 0
0031 
0032 
0033 #include "widgets/toolbars/kpColorToolBar.h"
0034 
0035 #include <QBoxLayout>
0036 #include <QDragEnterEvent>
0037 #include <QDragMoveEvent>
0038 #include <QMenu>
0039 
0040 #include <KColorMimeData>
0041 #include <KLocalizedString>
0042 #include <KDualAction>
0043 #include "kpLogCategories.h"
0044 
0045 #include "widgets/kpColorCells.h"
0046 #include "widgets/kpColorPalette.h"
0047 #include "widgets/colorSimilarity/kpColorSimilarityToolBarItem.h"
0048 #include "kpDefs.h"
0049 #include "widgets/kpDualColorButton.h"
0050 #include "lgpl/generic/kpUrlFormatter.h"
0051 
0052 //---------------------------------------------------------------------
0053 
0054 kpColorToolBar::kpColorToolBar (const QString &label, QWidget *parent)
0055     : QDockWidget (parent)
0056 {
0057     setWindowTitle (label);
0058 
0059     setFeatures(QDockWidget::NoDockWidgetFeatures);
0060 
0061     setAcceptDrops (true);
0062 
0063     QWidget *base = new QWidget (this);
0064     m_boxLayout = new QBoxLayout (QBoxLayout::LeftToRight, base);
0065     m_boxLayout->setContentsMargins(5, 5, 5, 5);
0066     m_boxLayout->setSpacing (10 * 3);
0067 
0068     // This holds the current global foreground and background colors, for
0069     // tools.
0070     m_dualColorButton = new kpDualColorButton (base);
0071     connect (m_dualColorButton, &kpDualColorButton::colorsSwapped,
0072              this, &kpColorToolBar::colorsSwapped);
0073 
0074      connect (m_dualColorButton, &kpDualColorButton::foregroundColorChanged,
0075               this, &kpColorToolBar::foregroundColorChanged);
0076 
0077      connect (m_dualColorButton, &kpDualColorButton::backgroundColorChanged,
0078               this, &kpColorToolBar::backgroundColorChanged);
0079 
0080     m_boxLayout->addWidget (m_dualColorButton, 0/*stretch*/, Qt::AlignVCenter);
0081 
0082     m_colorPalette = new kpColorPalette (base);
0083     connect (m_colorPalette, &kpColorPalette::foregroundColorChanged,
0084              m_dualColorButton, &kpDualColorButton::setForegroundColor);
0085 
0086     connect (m_colorPalette, &kpColorPalette::backgroundColorChanged,
0087              m_dualColorButton, &kpDualColorButton::setBackgroundColor);
0088 
0089     connect (m_colorPalette->colorCells (), &kpColorCells::isModifiedChanged,
0090              this, &kpColorToolBar::updateNameOrUrlLabel);
0091 
0092     connect (m_colorPalette->colorCells (), &kpColorCells::urlChanged,
0093              this, &kpColorToolBar::updateNameOrUrlLabel);
0094 
0095     connect (m_colorPalette->colorCells (), &kpColorCells::nameChanged,
0096              this, &kpColorToolBar::updateNameOrUrlLabel);
0097 
0098     updateNameOrUrlLabel ();
0099 
0100     m_boxLayout->addWidget (m_colorPalette, 0/*stretch*/);
0101 
0102     m_colorSimilarityToolBarItem = new kpColorSimilarityToolBarItem (base);
0103     connect (m_colorSimilarityToolBarItem,
0104              &kpColorSimilarityToolBarItem::colorSimilarityChanged,
0105              this, &kpColorToolBar::colorSimilarityChanged);
0106 
0107     m_boxLayout->addWidget (m_colorSimilarityToolBarItem, 0/*stretch*/);
0108 
0109     // Pad out all the horizontal space on the right of the Color Tool Bar so that
0110     // that the real Color Tool Bar widgets aren't placed in the center of the
0111     // Color Tool Bar.
0112     m_boxLayout->addItem (
0113         new QSpacerItem (1, 1, QSizePolicy::Expanding, QSizePolicy::Preferred));
0114 
0115     adjustToOrientation (Qt::Horizontal);
0116 
0117     setWidget (base);
0118 
0119     auto lockLayoutAction = new KDualAction(this);
0120     lockLayoutAction->setActiveText(i18nc("@action:inmenu Panels", "Unlock Panels"));
0121     lockLayoutAction->setActiveIcon(QIcon::fromTheme(QStringLiteral("object-unlocked")));
0122     lockLayoutAction->setInactiveText(i18nc("@action:inmenu Panels", "Lock Panels"));
0123     lockLayoutAction->setInactiveIcon(QIcon::fromTheme(QStringLiteral("object-locked")));
0124     lockLayoutAction->setWhatsThis(xi18nc("@info:whatsthis",
0125                                           "This "
0126                                           "switches between having panels <emphasis>locked</emphasis> or "
0127                                           "<emphasis>unlocked</emphasis>.<nl/>Unlocked panels can be "
0128                                           "dragged to the other side of the window and have a close "
0129                                           "button.<nl/>Locked panels are embedded more cleanly."));
0130     lockLayoutAction->setActive(true);
0131     connect(lockLayoutAction, &KDualAction::triggered, this, [this, lockLayoutAction]() {
0132         setLocked(lockLayoutAction->isActive());
0133     });
0134     setCustomContextMenuActions({lockLayoutAction});
0135 }
0136 
0137 QList<QAction *> kpColorToolBar::customContextMenuActions() const
0138 {
0139     return m_customContextMenuActions;
0140 }
0141 
0142 void kpColorToolBar::setCustomContextMenuActions(QList<QAction *> customContextMenuActions)
0143 {
0144     m_customContextMenuActions = customContextMenuActions;
0145 }
0146 
0147 //---------------------------------------------------------------------
0148 
0149 void kpColorToolBar::adjustToOrientation (Qt::Orientation o)
0150 {
0151 #if DEBUG_KP_COLOR_TOOL_BAR
0152     qCDebug(kpLogWidgets) << "kpColorToolBar::adjustToOrientation("
0153                << (o == Qt::Vertical ? "vertical" : "horizontal")
0154                << ") called!";
0155 #endif
0156 
0157     Q_ASSERT (o == Qt::Horizontal);
0158 
0159     if (o == Qt::Horizontal)
0160     {
0161         m_boxLayout->setDirection (QBoxLayout::LeftToRight);
0162     }
0163     else
0164     {
0165         m_boxLayout->setDirection (QBoxLayout::TopToBottom);
0166     }
0167 
0168     m_colorPalette->setOrientation (o);
0169 }
0170 
0171 //---------------------------------------------------------------------
0172 
0173 // public
0174 kpColorCells *kpColorToolBar::colorCells () const
0175 {
0176     return m_colorPalette->colorCells ();
0177 }
0178 
0179 //---------------------------------------------------------------------
0180 
0181 kpColor kpColorToolBar::color (int which) const
0182 {
0183     Q_ASSERT (which == 0 || which == 1);
0184 
0185     return m_dualColorButton->color (which);
0186 }
0187 
0188 //---------------------------------------------------------------------
0189 
0190 void kpColorToolBar::setColor (int which, const kpColor &color)
0191 {
0192     Q_ASSERT (which == 0 || which == 1);
0193 
0194     m_dualColorButton->setColor (which, color);
0195 }
0196 
0197 //---------------------------------------------------------------------
0198 
0199 kpColor kpColorToolBar::foregroundColor () const
0200 {
0201     return m_dualColorButton->foregroundColor ();
0202 }
0203 
0204 //---------------------------------------------------------------------
0205 
0206 void kpColorToolBar::setForegroundColor (const kpColor &color)
0207 {
0208 #if DEBUG_KP_COLOR_TOOL_BAR
0209     qCDebug(kpLogWidgets) << "kpColorToolBar::setForegroundColor("
0210               << (int *) color.toQRgb () << ")";
0211 #endif
0212     m_dualColorButton->setForegroundColor (color);
0213 }
0214 
0215 //---------------------------------------------------------------------
0216 
0217 kpColor kpColorToolBar::backgroundColor () const
0218 {
0219     return m_dualColorButton->backgroundColor ();
0220 }
0221 
0222 //---------------------------------------------------------------------
0223 
0224 void kpColorToolBar::setBackgroundColor (const kpColor &color)
0225 {
0226 #if DEBUG_KP_COLOR_TOOL_BAR
0227     qCDebug(kpLogWidgets) << "kpColorToolBar::setBackgroundColor("
0228               << (int *) color.toQRgb () << ")";
0229 #endif
0230     m_dualColorButton->setBackgroundColor (color);
0231 }
0232 
0233 //---------------------------------------------------------------------
0234 
0235 
0236 kpColor kpColorToolBar::oldForegroundColor () const
0237 {
0238     return m_dualColorButton->oldForegroundColor ();
0239 }
0240 
0241 //---------------------------------------------------------------------
0242 
0243 kpColor kpColorToolBar::oldBackgroundColor () const
0244 {
0245     return m_dualColorButton->oldBackgroundColor ();
0246 }
0247 
0248 //---------------------------------------------------------------------
0249 
0250 double kpColorToolBar::oldColorSimilarity () const
0251 {
0252     return m_colorSimilarityToolBarItem->oldColorSimilarity ();
0253 }
0254 
0255 //---------------------------------------------------------------------
0256 
0257 double kpColorToolBar::colorSimilarity () const
0258 {
0259     return m_colorSimilarityToolBarItem->colorSimilarity ();
0260 }
0261 
0262 //---------------------------------------------------------------------
0263 
0264 void kpColorToolBar::setColorSimilarity (double similarity)
0265 {
0266     m_colorSimilarityToolBarItem->setColorSimilarity (similarity);
0267 }
0268 
0269 //---------------------------------------------------------------------
0270 
0271 int kpColorToolBar::processedColorSimilarity () const
0272 {
0273     return m_colorSimilarityToolBarItem->processedColorSimilarity ();
0274 }
0275 
0276 //---------------------------------------------------------------------
0277 
0278 void kpColorToolBar::openColorSimilarityDialog ()
0279 {
0280     m_colorSimilarityToolBarItem->openDialog ();
0281 }
0282 
0283 //---------------------------------------------------------------------
0284 
0285 void kpColorToolBar::flashColorSimilarityToolBarItem ()
0286 {
0287     m_colorSimilarityToolBarItem->flash ();
0288 }
0289 
0290 //---------------------------------------------------------------------
0291 
0292 // private slot
0293 void kpColorToolBar::updateNameOrUrlLabel ()
0294 {
0295     QString name;
0296 
0297     kpColorCells *colorCells = m_colorPalette->colorCells ();
0298     if (!colorCells->url ().isEmpty ()) {
0299         name = kpUrlFormatter::PrettyFilename (colorCells->url ());
0300     }
0301     else
0302     {
0303         if (!colorCells->name ().isEmpty ()) {
0304             name = colorCells->name ();
0305         }
0306         else {
0307             name = i18n ("KolourPaint Defaults");
0308         }
0309     }
0310 
0311     if (name.isEmpty ()) {
0312         name = i18n ("Untitled");
0313     }
0314 
0315 
0316     KLocalizedString labelStr;
0317 
0318     if (!m_colorPalette->colorCells ()->isModified ())
0319     {
0320         labelStr =
0321             ki18nc ("Colors: name_or_url_of_color_palette",
0322                     "Colors: %1")
0323                 .subs (name);
0324     }
0325     else
0326     {
0327         labelStr =
0328             ki18nc ("Colors: name_or_url_of_color_palette [modified]",
0329                     "Colors: %1 [modified]")
0330                 .subs (name);
0331     }
0332 
0333     // Kill 2 birds with 1 stone:
0334     //
0335     // 1. Hide the windowTitle() when it's docked.
0336     // 2. Add a label containing the name of the open color palette.
0337     //
0338     // TODO: This currently hides the windowTitle() even when it's not docked,
0339     //       because we've abused it to show the name of open color palette
0340     //       instead.
0341     setWindowTitle (labelStr.toString ());
0342 }
0343 
0344 //---------------------------------------------------------------------
0345 
0346 // protected virtual [base QWidget]
0347 void kpColorToolBar::dragEnterEvent (QDragEnterEvent *e)
0348 {
0349     // Grab the color drag for this widget, preventing it from being
0350     // handled by our parent, the main window.
0351     e->setAccepted (KColorMimeData::canDecode (e->mimeData ()));
0352 #if DEBUG_KP_COLOR_TOOL_BAR
0353     qCDebug(kpLogWidgets) << "isAccepted=" << e->isAccepted ();
0354 #endif
0355 }
0356 
0357 //---------------------------------------------------------------------
0358 
0359 // protected virtual [base QWidget]
0360 void kpColorToolBar::dragMoveEvent (QDragMoveEvent *e)
0361 {
0362     // Stop the grabbed drag from being dropped.
0363     e->setAccepted (!KColorMimeData::canDecode (e->mimeData ()));
0364 #if DEBUG_KP_COLOR_TOOL_BAR
0365     qCDebug(kpLogWidgets) << "isAccepted=" << e->isAccepted ();
0366 #endif
0367 }
0368 
0369 void kpColorToolBar::contextMenuEvent(QContextMenuEvent *event)
0370 {
0371     QMenu popup(this);
0372     const auto actions = customContextMenuActions();
0373     for (QAction *action : actions) {
0374         popup.addAction(action);
0375     }
0376     popup.addSeparator();
0377     popup.exec(event->globalPos());
0378 }
0379 
0380 void kpColorToolBar::setLocked(bool lock)
0381 {
0382     if (lock != m_locked) {
0383         m_locked = lock;
0384 
0385         if (lock) {
0386             setFeatures(QDockWidget::NoDockWidgetFeatures);
0387         } else {
0388             setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetClosable);
0389         }
0390     }
0391 }
0392 
0393 bool kpColorToolBar::isLocked() const
0394 {
0395     return m_locked;
0396 }
0397 
0398 //---------------------------------------------------------------------
0399 
0400 #include "moc_kpColorToolBar.cpp"