Commit 5026c383 authored by Martin Koller's avatar Martin Koller
Browse files

code cleanup/refactoring kpToolAction, etc.

parent 53b80340
......@@ -299,25 +299,17 @@ void kpMainWindow::init ()
setAutoSaveSettings ();
// Put our non-XMLGUI toolbars in a sane place, the first time around
// (have to do this _after_ setAutoSaveSettings as that applies default
// (i.e. random) settings to the toolbars)
// our non-XMLGUI tools-toolbar will get initially the toolButtonStyle as
// all other toolbars, but we want to show only icons for the tools by default
// (have to do this _after_ setAutoSaveSettings as that applies the default settings)
if (d->configFirstTime)
{
#if DEBUG_KP_MAIN_WINDOW
kDebug () << "\tfirstTime: positioning toolbars";
#endif
// Not needed due to above toolbar HACK.
#if 0
addToolBar (Qt::LeftToolBarArea, d->toolToolBar);
addToolBar (Qt::BottomToolBarArea, d->colorToolBar);
#endif
d->toolToolBar->setToolButtonStyle(Qt::ToolButtonIconOnly);
KConfigGroup cfg (KGlobal::config (), kpSettingsGroupGeneral);
KConfigGroup cfg(KGlobal::config(), kpSettingsGroupGeneral);
cfg.writeEntry (kpSettingFirstTime, d->configFirstTime = false);
cfg.sync ();
cfg.writeEntry(kpSettingFirstTime, d->configFirstTime = false);
cfg.sync();
}
......
......@@ -132,22 +132,13 @@ void kpMainWindow::slotShowPathToggled ()
// private slot
void kpMainWindow::slotKeyBindings ()
{
#if DEBUG_KP_MAIN_WINDOW
kDebug () << "kpMainWindow::slotKeyBindings()";
#endif
toolEndShape ();
if (KShortcutsDialog::configure (actionCollection (),
KShortcutsEditor::LetterShortcutsAllowed,
this))
{
#if DEBUG_KP_MAIN_WINDOW
kDebug () << "\tdialog accepted";
#endif
// TODO: PROPAGATE: thru mainWindow's and interprocess
kpToolAction::updateAllActionsToolTips (actionCollection ());
}
}
......
/*
Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
Copyright (c) 2011 Martin Koller <kollix@aon.at>
All rights reserved.
Redistribution and use in source and binary forms, with or without
......@@ -178,10 +178,8 @@ void kpMainWindow::setupToolActions ()
// private
void kpMainWindow::createToolBox ()
{
d->toolToolBar = new kpToolToolBar (i18n ("Tool Box"), 2/*columns/rows*/, this);
// (needed for QMainWindow::saveState())
d->toolToolBar->setObjectName ( QLatin1String("Tool Box" ));
d->toolToolBar = new kpToolToolBar(QLatin1String("Tool Box"), 2/*columns/rows*/, this);
d->toolToolBar->setWindowTitle(i18n("Tool Box"));
connect (d->toolToolBar, SIGNAL (sigToolSelected (kpTool *)),
this, SLOT (slotToolSelected (kpTool *)));
......@@ -193,13 +191,8 @@ void kpMainWindow::createToolBox ()
SLOT (updateActionDrawOpaqueChecked ()));
updateActionDrawOpaqueChecked ();
for (QList <kpTool *>::const_iterator it = d->tools.constBegin ();
it != d->tools.constEnd ();
++it)
{
d->toolToolBar->registerTool (*it);
}
foreach (kpTool *tool, d->tools)
d->toolToolBar->registerTool(tool);
// (from config file)
readLastTool ();
......@@ -234,7 +227,7 @@ void kpMainWindow::enableToolsDocumentActions (bool enable)
}
else if (!enable && d->toolToolBar->isEnabled ())
{
// don't have a disabled Tool Box with an enabled Tool
// don't have a disabled Tool Box with a checked Tool
d->toolToolBar->selectTool (0);
}
......@@ -242,28 +235,13 @@ void kpMainWindow::enableToolsDocumentActions (bool enable)
d->toolToolBar->setEnabled (enable);
for (QList <kpTool *>::const_iterator it = d->tools.constBegin ();
it != d->tools.constEnd ();
++it)
foreach (kpTool *tool, d->tools)
{
kpToolAction *action = (*it)->action ();
if (action)
{
#if DEBUG_KP_MAIN_WINDOW
kDebug () << "\tchanging enabled state of " << (*it)->objectName ();
#endif
if (!enable && action->isChecked ())
action->setChecked (false);
kpToolAction *action = tool->action();
if (!enable && action->isChecked())
action->setChecked(false);
action->setEnabled (enable);
}
else
{
#if DEBUG_KP_MAIN_WINDOW
kDebug () << "\tno action for " << (*it)->objectName ();
#endif
}
action->setEnabled(enable);
}
......
/*
Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
Copyright(c) 2003-2007 Clarence Dang <dang@kde.org>
All rights reserved.
Redistribution and use in source and binary forms, with or without
......@@ -17,7 +17,7 @@
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
......@@ -30,8 +30,6 @@
#define KP_TOOL_FLOW_BASE_H
#include <qlist.h>
#include <qpixmap.h>
#include <qrect.h>
#include <kpTempImage.h>
......@@ -47,80 +45,74 @@ class kpToolFlowCommand;
class kpToolFlowBase : public kpTool
{
Q_OBJECT
Q_OBJECT
public:
kpToolFlowBase (const QString &text, const QString &description,
int key,
kpToolEnvironment *environ, QObject *parent, const QString &name);
virtual ~kpToolFlowBase ();
public:
kpToolFlowBase(const QString &text, const QString &description,
int key,
kpToolEnvironment *environ, QObject *parent, const QString &name);
private:
void clearBrushCursorData ();
virtual ~kpToolFlowBase();
protected:
virtual QString haventBegunDrawUserMessage () const = 0;
virtual bool haveSquareBrushes () const { return false; }
virtual bool haveDiverseBrushes () const { return false; }
bool haveAnyBrushes () const
{
return (haveSquareBrushes () || haveDiverseBrushes ());
}
virtual bool colorsAreSwapped () const { return false; }
// Returns the dirty rectangle for drawing a brush(of size
// <brushWidth>x<brushHeight>) at <mousePoint>. <mousePoint> will end
// up being the midpoint of the returned rectangle(subject to integer
// precision).
static QRect hotRectForMousePointAndBrushWidthHeight(
const QPoint &mousePoint,
int brushWidth, int brushHeight);
public:
virtual void begin ();
virtual void end ();
virtual void begin();
virtual void end();
virtual void beginDraw ();
virtual void hover (const QPoint &point);
virtual void beginDraw();
virtual void hover(const QPoint &point);
// drawPoint() normally calls drawLine(point,point). Override drawPoint()
// if you think you can be more efficient.
virtual QRect drawPoint (const QPoint &point);
virtual QRect drawLine (const QPoint &thisPoint, const QPoint &lastPoint) = 0;
virtual QRect drawPoint(const QPoint &point);
virtual QRect drawLine(const QPoint &thisPoint, const QPoint &lastPoint) = 0;
virtual bool drawShouldProceed (const QPoint & /*thisPoint*/, const QPoint & /*lastPoint*/, const QRect & /*normalizedRect*/) { return true; }
virtual void draw (const QPoint &thisPoint, const QPoint &lastPoint, const QRect &normalizedRect);
virtual void cancelShape ();
virtual void releasedAllButtons ();
virtual void endDraw (const QPoint &, const QRect &);
virtual bool drawShouldProceed(const QPoint & /*thisPoint*/, const QPoint & /*lastPoint*/, const QRect & /*normalizedRect*/) { return true; }
virtual void draw(const QPoint &thisPoint, const QPoint &lastPoint, const QRect &normalizedRect);
virtual void cancelShape();
virtual void releasedAllButtons();
virtual void endDraw(const QPoint &, const QRect &);
protected:
kpTempImage::UserFunctionType brushDrawFunction () const;
void *brushDrawFunctionData () const;
protected:
virtual QString haventBegunDrawUserMessage() const = 0;
int brushWidth () const;
int brushHeight () const;
virtual bool haveSquareBrushes() const { return false; }
virtual bool haveDiverseBrushes() const { return false; }
bool haveAnyBrushes() const
{
return(haveSquareBrushes() || haveDiverseBrushes());
}
bool brushIsDiagonalLine () const;
virtual bool colorsAreSwapped() const { return false; }
kpToolFlowCommand *currentCommand () const;
kpTempImage::UserFunctionType brushDrawFunction() const;
void *brushDrawFunctionData() const;
protected slots:
void updateBrushAndCursor ();
int brushWidth() const;
int brushHeight() const;
virtual void slotForegroundColorChanged (const kpColor &col);
virtual void slotBackgroundColorChanged (const kpColor &col);
bool brushIsDiagonalLine() const;
protected:
virtual kpColor color (int which);
kpToolFlowCommand *currentCommand() const;
virtual kpColor color(int which);
QRect hotRect() const;
public:
// Returns the dirty rectangle for drawing a brush (of size
// <brushWidth>x<brushHeight>) at <mousePoint>. <mousePoint> will end
// up being the midpoint of the returned rectangle (subject to integer
// precision).
static QRect hotRectForMousePointAndBrushWidthHeight (
const QPoint &mousePoint,
int brushWidth, int brushHeight);
protected:
QRect hotRect () const;
protected slots:
void updateBrushAndCursor();
virtual void slotForegroundColorChanged(const kpColor &col);
virtual void slotBackgroundColorChanged(const kpColor &col);
private:
void clearBrushCursorData();
private:
private:
struct kpToolFlowBasePrivate *d;
};
......
......@@ -53,19 +53,19 @@
//---------------------------------------------------------------------
kpTool::kpTool (const QString &text, const QString &description,
int key,
kpToolEnvironment *environ,
QObject *parent, const QString &name)
: QObject (parent),
d (new kpToolPrivate ())
kpTool::kpTool(const QString &text, const QString &description,
int key,
kpToolEnvironment *environ,
QObject *parent, const QString &name)
: QObject(parent),
d(new kpToolPrivate())
{
d->key = key;
d->action = 0;
d->ignoreColorSignals = 0;
d->shiftPressed = false, d->controlPressed = false, d->altPressed = false; // set in beginInternal()
d->beganDraw = false;
d->text = text, d->description = description; setObjectName (name);
d->text = text, d->description = description;
d->began = false;
d->viewUnderStartPoint = 0;
d->userShapeStartPoint = KP_INVALID_POINT;
......@@ -74,7 +74,8 @@ kpTool::kpTool (const QString &text, const QString &description,
d->environ = environ;
initAction ();
setObjectName(name);
initAction();
}
//---------------------------------------------------------------------
......@@ -95,30 +96,24 @@ kpTool::~kpTool ()
// private
void kpTool::initAction ()
{
#if DEBUG_KP_TOOL && 0
kDebug () << "kpTool(" << objectName () << "::initAction()";
#endif
KActionCollection *ac = d->environ->actionCollection ();
Q_ASSERT (ac);
d->action = new kpToolAction (text (), iconName (), shortcutForKey (d->key),
this, SLOT (slotActionActivated ()),
ac, objectName ());
d->action = new kpToolAction(text(), objectName(), shortcutForKey(d->key),
this, SIGNAL(actionActivated()),
ac, objectName());
// Make tools mutually exclusive by placing them in the same group.
d->action->setActionGroup (d->environ->toolsActionGroup ());
d->action->setActionGroup(d->environ->toolsActionGroup ());
d->action->setWhatsThis (description ());
d->action->setWhatsThis(d->description);
connect (d->action, SIGNAL (toolTipChanged (const QString &)),
this, SLOT (slotActionToolTipChanged (const QString &)));
connect(d->action, SIGNAL(changed()), this, SIGNAL (actionToolTipChanged()));
}
//---------------------------------------------------------------------
// public
QString kpTool::text () const
{
return d->text;
......@@ -126,16 +121,6 @@ QString kpTool::text () const
//---------------------------------------------------------------------
// public
void kpTool::setText (const QString &text)
{
d->text = text;
d->action->setText (d->text);
}
//---------------------------------------------------------------------
static bool KeyIsText (int key)
{
// TODO: should work like !QKeyEvent::text().isEmpty()
......@@ -160,35 +145,14 @@ QString kpTool::toolTipForTextAndShortcut (const QString &text,
//---------------------------------------------------------------------
// public static
QString kpTool::toolTip () const
{
return toolTipForTextAndShortcut (text (), shortcut ());
return toolTipForTextAndShortcut(d->text, d->action->shortcut());
}
//---------------------------------------------------------------------
// public
int kpTool::key () const
{
return d->key;
}
//---------------------------------------------------------------------
// public
void kpTool::setKey (int key)
{
d->key = key;
// TODO: this probably not wise since it nukes the user's settings
d->action->setShortcut (shortcutForKey (d->key));
}
//---------------------------------------------------------------------
// public static
KShortcut kpTool::shortcutForKey (int key)
{
KShortcut shortcut;
......@@ -205,42 +169,8 @@ KShortcut kpTool::shortcutForKey (int key)
}
//---------------------------------------------------------------------
// public
KShortcut kpTool::shortcut () const
{
return d->action->shortcut ();
}
//---------------------------------------------------------------------
// public
QString kpTool::description () const
{
return d->description;
}
//---------------------------------------------------------------------
// public
void kpTool::setDescription (const QString &description)
{
d->description = description;
d->action->setWhatsThis (d->description);
}
//---------------------------------------------------------------------
// public virtual
QString kpTool::iconName () const
{
return objectName ();
}
//---------------------------------------------------------------------
// public
kpToolAction *kpTool::action () const
{
return d->action;
......@@ -248,38 +178,47 @@ kpToolAction *kpTool::action () const
//---------------------------------------------------------------------
// REFACTOR: need to add access specifier comments (like "public virtual [base AmOverridingThisClass'Method]") not just in kpTool but all over KolourPaint source.
kpDocument *kpTool::document () const
{
return d->environ->document ();
}
//---------------------------------------------------------------------
kpViewManager *kpTool::viewManager () const
{
return d->environ->viewManager ();
}
//---------------------------------------------------------------------
kpToolToolBar *kpTool::toolToolBar () const
{
return d->environ->toolToolBar ();
}
//---------------------------------------------------------------------
kpColor kpTool::color (int which) const
{
return d->environ->color (which);
}
//---------------------------------------------------------------------
kpColor kpTool::foregroundColor () const
{
return color (0);
}
//---------------------------------------------------------------------
kpColor kpTool::backgroundColor () const
{
return color (1);
}
//---------------------------------------------------------------------
// TODO: Some of these might not be common enough.
// Just put in kpToolEnvironment?
......
......@@ -86,36 +86,17 @@ public:
QObject *parent, const QString &name);
virtual ~kpTool ();
private:
// Only called by ctor to create action().
void initAction ();
signals:
void actionToolTipChanged (const QString &string);
protected slots:
void slotActionToolTipChanged (const QString &string);
kpToolAction *action () const;
public:
QString text () const;
void setText (const QString &text);
static QString toolTipForTextAndShortcut (const QString &text,
const KShortcut &shortcut);
static QString toolTipForTextAndShortcut (const QString &text, const KShortcut &shortcut);
QString toolTip () const;
QString description () const;
void setDescription (const QString &description);
int key () const;
void setKey (int key);
// Given a single <key>, returns a shortcut with <key>
// (disabled when the user is editing text) and as an alternate,
// <some modifiers>+<key>.
static KShortcut shortcutForKey (int key);
KShortcut shortcut () const;
static QRect neededRect (const QRect &rect, int lineWidth);
static QImage neededPixmap (const QImage &image, const QRect &boundingRect);
......@@ -139,6 +120,13 @@ public:
// outside of the document/view.
QPoint calculateCurrentPoint (bool zoomToDoc = true) const;
private:
// Only called by ctor to create action().
void initAction ();
signals:
void actionToolTipChanged();
public slots:
// Call this when something below the mouse cursor may have changed
// and/or if the view has moved relative to the cursor (as opposed to
......@@ -175,10 +163,10 @@ protected:
public: // for kpMainWindow
kpView *viewUnderStartPoint () const;
protected:
kpView *viewUnderCursor () const;
public:
// Called when the tool is selected.
virtual void begin ();
......@@ -218,21 +206,9 @@ signals:
// emitted after cancelShape() has been called
void cancelledShape (const QPoint &point);
public:
// Override this to use an icon whose name is not the same as the tool's
// <name> as passed to the constructor.
virtual QString iconName () const;
kpToolAction *action () const;
signals:
// User clicked on the tool's action - i.e. select this tool
void actionActivated ();
protected slots:
void slotActionActivated ();
void actionActivated();
protected:
// (this method is called by kpTool just as it is needed - its value
......@@ -282,7 +258,6 @@ protected:
kpColor color (int which) const;
// TODO: does anyone actually use these?
kpColor foregroundColor () const;
kpColor backgroundColor () const;
......
/*
Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
Copyright(c) 2003-2007 Clarence Dang <dang@kde.org>
Copyright(c) 2011 Martin Koller <kollix@aon.at>
All rights reserved.
Redistribution and use in source and binary forms, with or without
......@@ -17,7 +17,7 @@
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
......@@ -25,107 +25,44 @@
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define DEBUG_KP_TOOL_ACTION 0
#include <kpToolAction.h>
#include <kactioncollection.h>