Commit e0048087 authored by Casper Boemann's avatar Casper Boemann

Fix potential crash In KoColor (saw it during testing)

Simplify KoDualColorButton API
Use KoDualColorButton in krita (temporarity placed in toolbar)

svn path=/trunk/koffice/; revision=563579
parent fe451e26
......@@ -163,6 +163,7 @@
<Action name="patterns"/>
<Separator/>
<Action name="paintops"/>
<Action name="dual"/>
<Action name="paintop_options"/>
</ToolBar>
......
......@@ -42,6 +42,7 @@
#include <kaction.h>
#include <koFrameButton.h>
#include <kactioncollection.h>
#include <KoDualColorButton.h>
#include "kis_resourceserver.h"
#include "kis_controlframe.h"
......@@ -129,6 +130,15 @@ KisControlFrame::KisControlFrame( KMainWindow * /*window*/, KisView * view, cons
action = new KAction(i18n("&Painter's Tools"), view->actionCollection(), "paintops");
action->setDefaultWidget( m_paintopBox );
/**** Temporary hack to test the KoDualColorButton ***/
KoDualColorButton * dual = new KoDualColorButton(view->canvasSubject()->fgColor(), view->canvasSubject()->fgColor(), view, view);
action = new KAction(i18n("&Painter's Tools"), view->actionCollection(), "dual");
action->setDefaultWidget( dual );
connect(dual, SIGNAL(foregroundColorChanged(const KoColor &)), view, SLOT(slotSetFGColor(const KoColor &)));
connect(dual, SIGNAL(backgroundColorChanged(const KoColor &)), view, SLOT(slotSetBGColor(const KoColor &)));
connect(view, SIGNAL(sigFGColorChanged(const KoColor &)), dual, SLOT(setForegroundColor(const KoColor &)));
dual->setFixedSize( 26, 26 );
/*******/
m_brushWidget->setFixedSize( 26, 26 );
m_patternWidget->setFixedSize( 26, 26 );
m_gradientWidget->setFixedSize( 26, 26 );
......
......@@ -2239,12 +2239,14 @@ void KisView::slotSetFGColor(const KoColor& c)
{
m_fg = c;
notifyObservers();
emit sigFGColorChanged( c );
}
void KisView::slotSetBGColor(const KoColor& c)
{
m_bg = c;
notifyObservers();
emit sigBGColorChanged( c );
}
void KisView::setupPrinter(KPrinter& printer)
......
......@@ -36,9 +36,11 @@
class KoDualColorButton::Private
{
public:
Private()
: dragFlag( false ), miniCtlFlag( false ),
selection( Foreground )
Private(const KoColor &fgColor, const KoColor &bgColor)
: dragFlag( false )
, miniCtlFlag( false )
, foregroundColor(fgColor)
, backgroundColor(bgColor)
{
arrowBitmap = QBitmap::fromData( QSize(dcolorarrow_width, dcolorarrow_height),
(const unsigned char *)dcolorarrow_bits, QImage::Format_MonoLSB );
......@@ -52,23 +54,20 @@ class KoDualColorButton::Private
QBitmap arrowBitmap;
QPixmap resetPixmap;
bool dragFlag, miniCtlFlag;
KoColor foregroundColor;
KoColor backgroundColor;
QPoint dragPosition;
bool dragFlag, miniCtlFlag;
Selection selection, tmpSelection;
Selection tmpSelection;
bool popDialog;
};
KoDualColorButton::KoDualColorButton(const KoColor &foregroundColor, const KoColor &backgroundColor, QWidget *parent, QWidget* dialogParent )
: QWidget( parent ),
d( new Private )
d( new Private(foregroundColor, backgroundColor) )
{
d->dialogParent = dialogParent;
d->foregroundColor = foregroundColor;
d->backgroundColor = backgroundColor;
if ( sizeHint().isValid() )
setMinimumSize( sizeHint() );
......@@ -90,21 +89,11 @@ KoColor KoDualColorButton::backgroundColor() const
return d->backgroundColor;
}
KoDualColorButton::Selection KoDualColorButton::selection() const
{
return d->selection;
}
bool KoDualColorButton::popDialog() const
{
return d->popDialog;
}
KoColor KoDualColorButton::currentColor() const
{
return ( d->selection == Background ? d->backgroundColor : d->foregroundColor );
}
QSize KoDualColorButton::sizeHint() const
{
return QSize( 34, 34 );
......@@ -114,33 +103,12 @@ void KoDualColorButton::setForegroundColor( const KoColor &color )
{
d->foregroundColor = color;
repaint();
emit foregroundColorChanged( d->foregroundColor );
}
void KoDualColorButton::setBackgroundColor( const KoColor &color )
{
d->backgroundColor = color;
repaint();
emit backgroundColorChanged( d->backgroundColor );
}
void KoDualColorButton::setCurrentColor( const KoColor &color )
{
if ( d->selection == Background )
d->backgroundColor = color;
else
d->foregroundColor = color;
repaint();
}
void KoDualColorButton::setSelection( Selection selection )
{
d->selection = selection;
repaint();
}
void KoDualColorButton::setPopDialog( bool popDialog )
......@@ -167,11 +135,10 @@ void KoDualColorButton::paintEvent(QPaintEvent *)
QBrush foregroundBrush( d->foregroundColor.toQColor(), Qt::SolidPattern );
QBrush backgroundBrush( d->backgroundColor.toQColor(), Qt::SolidPattern );
qDrawShadeRect( &painter, backgroundRect, palette(), d->selection == Background, 2, 0,
qDrawShadeRect( &painter, backgroundRect, palette(), false, 2, 0,
isEnabled() ? &backgroundBrush : &defBrush );
qDrawShadeRect( &painter, foregroundRect, palette(), d->selection == Foreground, 2, 0,
qDrawShadeRect( &painter, foregroundRect, palette(), false, 2, 0,
isEnabled() ? &foregroundBrush : &defBrush );
painter.setPen( palette().color( QPalette::Shadow ) );
......@@ -266,14 +233,11 @@ void KoDualColorButton::mouseReleaseEvent( QMouseEvent *event )
metrics( foregroundRect, backgroundRect );
if ( foregroundRect.contains( event->pos() )) {
d->selection = Foreground;
if(d->tmpSelection == Foreground ) {
KoColor newColor = d->foregroundColor;
if( d->popDialog) {
KoUniColorDialog *dialog = new KoUniColorDialog(newColor, d->dialogParent);
KoUniColorDialog *dialog = new KoUniColorDialog(d->foregroundColor, d->dialogParent);
if(dialog->exec() != KPageDialog::Accepted) {
d->foregroundColor = KoColor(newColor);
// d->foregroundColor = newColor;
emit foregroundColorChanged( d->foregroundColor );
}
}
......@@ -284,16 +248,12 @@ void KoDualColorButton::mouseReleaseEvent( QMouseEvent *event )
d->foregroundColor = d->backgroundColor;
emit foregroundColorChanged( d->foregroundColor );
}
emit selectionChanged( Foreground );
} else if ( backgroundRect.contains( event->pos() )) {
d->selection = Background;
if(d->tmpSelection == Background ) {
KoColor newColor = d->backgroundColor;
if( d->popDialog) {
KoUniColorDialog *dialog = new KoUniColorDialog(newColor, d->dialogParent);
KoUniColorDialog *dialog = new KoUniColorDialog(d->backgroundColor, d->dialogParent);
if(dialog->exec() != KPageDialog::Accepted) {
d->backgroundColor = KoColor(newColor);
//d->backgroundColor = ;
emit backgroundColorChanged( d->backgroundColor );
}
}
......@@ -303,7 +263,6 @@ void KoDualColorButton::mouseReleaseEvent( QMouseEvent *event )
d->backgroundColor = d->foregroundColor;
emit backgroundColorChanged( d->backgroundColor );
}
emit selectionChanged( Background );
}
repaint();
......
......@@ -53,9 +53,7 @@ class KDEUI_EXPORT KoDualColorButton : public QWidget
Q_ENUMS( Selection )
Q_PROPERTY( KoColor foregroundColor READ foregroundColor WRITE setForegroundColor )
Q_PROPERTY( KoColor backgroundColor READ backgroundColor WRITE setBackgroundColor )
Q_PROPERTY( KoColor currentColor READ currentColor WRITE setCurrentColor )
Q_PROPERTY( bool popDialog READ popDialog WRITE setPopDialog )
Q_PROPERTY( Selection selection READ selection WRITE setSelection STORED false DESIGNABLE false )
public:
......@@ -98,27 +96,6 @@ class KDEUI_EXPORT KoDualColorButton : public QWidget
*/
KoColor backgroundColor() const;
/**
* Returns the current color depending on the
* selection.
*
* This is equal to
*
* \code
* if ( selection() == Foreground )
* return foregroundColor();
* else
* return backgroundColor();
* \endcode
*/
KoColor currentColor() const;
/**
* Returns whether the foreground or background item
* is selected.
*/
Selection selection() const;
/**
* Returns if a dialog with a KoUniColorChooser will be popped up when clicking
* If false then you could/should connect to the pleasePopDialog signal
......@@ -143,16 +120,6 @@ class KDEUI_EXPORT KoDualColorButton : public QWidget
*/
void setBackgroundColor( const KoColor &color );
/**
* Sets the color of the selected item.
*/
void setCurrentColor( const KoColor &color );
/**
* Sets the current selected color item.
*/
void setSelection( Selection selection );
/**
* Sets if a dialog with a KoUniColorChooser should be popped up when clicking
* If you set this to false then you could connect to the pleasePopDialog signal
......@@ -171,11 +138,6 @@ class KDEUI_EXPORT KoDualColorButton : public QWidget
*/
void backgroundColorChanged( const KoColor &color );
/**
* Emitted when the user changes the current color selection.
*/
void selectionChanged( KoDualColorButton::Selection selection );
/**
* Emitted when the user clicks one of the two color patches.
* You should/could pop you own color chooser dialog in response.
......
......@@ -283,6 +283,14 @@ KoUniColorChooser::KoUniColorChooser(QWidget *parent) : super(parent)
setLayout(mGrowGrid);
}
void KoUniColorChooser::setColor(const KoColor & c)
{
m_currentColor = c;
updateValues();
updateSelectorsCurrent();
m_colorpatch->setColor(m_currentColor);
}
void KoUniColorChooser::slotHChanged(int )
{
}
......
......@@ -46,13 +46,17 @@ public:
virtual ~KoUniColorChooser() {}
public slots:
/**
* Sets the current color
* Does not emit a signal
*/
void setColor(const KoColor & c);
signals:
/**
* Emitted when the current color is changed.
*/
void sigColorChanged(const QColor & c);
void sigColorChanged(const KoColor & c);
protected slots:
......
......@@ -81,14 +81,19 @@ KoColor::KoColor(const KoColor & rhs)
{
if (this == &rhs) return;
m_data = 0;
m_colorSpace = rhs.colorSpace();
m_data = new quint8[m_colorSpace->pixelSize()];
memset(m_data, 0, m_colorSpace->pixelSize());
memcpy(m_data, rhs.data(), m_colorSpace->pixelSize());
if(m_colorSpace && rhs.m_data)
{
m_data = new quint8[m_colorSpace->pixelSize()];
memcpy(m_data, rhs.data(), m_colorSpace->pixelSize());
}
}
KoColor & KoColor::operator=(const KoColor & rhs)
{
if (this == &rhs) return *this;
delete [] m_data;
m_data = 0;
m_colorSpace = rhs.colorSpace();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment