File indexing completed on 2024-04-28 04:20:26

0001 /*
0002    Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
0003    All rights reserved.
0004 
0005    Redistribution and use in source and binary forms, with or without
0006    modification, are permitted provided that the following conditions
0007    are met:
0008 
0009    1. Redistributions of source code must retain the above copyright
0010       notice, this list of conditions and the following disclaimer.
0011    2. Redistributions in binary form must reproduce the above copyright
0012       notice, this list of conditions and the following disclaimer in the
0013       documentation and/or other materials provided with the distribution.
0014 
0015    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0016    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0017    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0018    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0019    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0020    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0021    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0022    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0023    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0024    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0025 */
0026 
0027 //
0028 // Tool initialisation and basic accessors.
0029 //
0030 
0031 #define DEBUG_KP_TOOL 0
0032 
0033 #include "kpTool.h"
0034 #include "kpToolPrivate.h"
0035 
0036 #include <climits>
0037 
0038 #include <KActionCollection>
0039 #include "kpLogCategories.h"
0040 #include <KLocalizedString>
0041 
0042 #include "imagelib/kpColor.h"
0043 #include "widgets/toolbars/kpColorToolBar.h"
0044 #include "environments/tools/kpToolEnvironment.h"
0045 #include "widgets/toolbars/kpToolToolBar.h"
0046 #include "views/kpView.h"
0047 #include "views/manager/kpViewManager.h"
0048 #undef environ  // macro on win32
0049 
0050 //---------------------------------------------------------------------
0051 
0052 kpTool::kpTool(const QString &text, const QString &description,
0053                int key,
0054                kpToolEnvironment *environ,
0055                QObject *parent, const QString &name)
0056     : QObject(parent),
0057       d(new kpToolPrivate())
0058 {
0059     d->key = key;
0060     d->action = nullptr;
0061     d->ignoreColorSignals = 0;
0062     d->shiftPressed = false;
0063     d->controlPressed = false;
0064     d->altPressed = false;  // set in beginInternal()
0065     d->beganDraw = false;
0066     d->text = text;
0067     d->description = description;
0068     d->began = false;
0069     d->viewUnderStartPoint = nullptr;
0070     d->userShapeStartPoint = KP_INVALID_POINT;
0071     d->userShapeEndPoint = KP_INVALID_POINT;
0072     d->userShapeSize = KP_INVALID_SIZE;
0073 
0074     d->environ = environ;
0075 
0076     setObjectName(name);
0077     initAction();
0078 }
0079 
0080 //---------------------------------------------------------------------
0081 
0082 kpTool::~kpTool ()
0083 {
0084     // before destructing, stop using the tool
0085     if (d->began) {
0086         endInternal ();
0087     }
0088 
0089     delete d->action;
0090 
0091     delete d;
0092 }
0093 
0094 //---------------------------------------------------------------------
0095 
0096 // private
0097 void kpTool::initAction ()
0098 {
0099     KActionCollection *ac = d->environ->actionCollection ();
0100     Q_ASSERT (ac);
0101 
0102     d->action = ac->add<KToggleAction>(objectName());
0103     d->action->setText(text());
0104     d->action->setWhatsThis(d->description);
0105     d->action->setIcon(QIcon::fromTheme(objectName()));
0106     ac->setDefaultShortcuts(d->action, shortcutForKey(d->key));
0107 
0108     connect(d->action, &QAction::triggered, this, &kpTool::actionActivated);
0109 
0110     // Make tools mutually exclusive by placing them in the same group.
0111     d->action->setActionGroup(d->environ->toolsActionGroup ());
0112 }
0113 
0114 //---------------------------------------------------------------------
0115 // public
0116 
0117 QString kpTool::text () const
0118 {
0119     return d->text;
0120 }
0121 
0122 //---------------------------------------------------------------------
0123 // public static
0124 
0125 QList<QKeySequence> kpTool::shortcutForKey (int key)
0126 {
0127     QList<QKeySequence> shortcut;
0128 
0129     if (key)
0130     {
0131         shortcut.append (QKeySequence (key));
0132         // (CTRL+<key>, ALT+<key>, CTRL+ALT+<key>, CTRL+SHIFT+<key>
0133         //  all clash with global KDE shortcuts)
0134         shortcut.append (QKeySequence (static_cast<int>(Qt::ALT) + static_cast<int>(Qt::SHIFT) + key));
0135     }
0136 
0137     return shortcut;
0138 }
0139 
0140 //---------------------------------------------------------------------
0141 // public
0142 
0143 KToggleAction *kpTool::action () const
0144 {
0145     return d->action;
0146 }
0147 
0148 //---------------------------------------------------------------------
0149 
0150 kpDocument *kpTool::document () const
0151 {
0152     return d->environ->document ();
0153 }
0154 
0155 //---------------------------------------------------------------------
0156 
0157 kpViewManager *kpTool::viewManager () const
0158 {
0159     return d->environ->viewManager ();
0160 }
0161 
0162 //---------------------------------------------------------------------
0163 
0164 kpToolToolBar *kpTool::toolToolBar () const
0165 {
0166     return d->environ->toolToolBar ();
0167 }
0168 
0169 //---------------------------------------------------------------------
0170 
0171 kpColor kpTool::color (int which) const
0172 {
0173     return d->environ->color (which);
0174 }
0175 
0176 //---------------------------------------------------------------------
0177 
0178 kpColor kpTool::foregroundColor () const
0179 {
0180     return color (0);
0181 }
0182 
0183 //---------------------------------------------------------------------
0184 
0185 kpColor kpTool::backgroundColor () const
0186 {
0187     return color (1);
0188 }
0189 
0190 //---------------------------------------------------------------------
0191 
0192 // TODO: Some of these might not be common enough.
0193 //       Just put in kpToolEnvironment?
0194 
0195 double kpTool::colorSimilarity () const
0196 {
0197     return d->environ->colorSimilarity ();
0198 }
0199 
0200 int kpTool::processedColorSimilarity () const
0201 {
0202     return d->environ->processedColorSimilarity ();
0203 }
0204 
0205 
0206 kpColor kpTool::oldForegroundColor () const
0207 {
0208     return d->environ->oldForegroundColor ();
0209 }
0210 
0211 kpColor kpTool::oldBackgroundColor () const
0212 {
0213     return d->environ->oldBackgroundColor ();
0214 }
0215 
0216 double kpTool::oldColorSimilarity () const
0217 {
0218     return d->environ->oldColorSimilarity ();
0219 }
0220 
0221 kpCommandHistory *kpTool::commandHistory () const
0222 {
0223     return d->environ->commandHistory ();
0224 }
0225 
0226 
0227 kpToolEnvironment *kpTool::environ () const
0228 {
0229     return d->environ;
0230 }
0231 
0232 #include "moc_kpTool.cpp"