Commit d25b10a4 authored by Saurabh  Kumar's avatar Saurabh Kumar 😌 Committed by Agata Cacko

Prevent crash on addition of color to deleted palette with colorpicker

This commit makes the deletion and addition of palette
in palette docker show in colorpicker's tool options'
Add to palette comboBox. This also prevents a crash
due to a dangling pointer due to palette pointing to
deleted memory location.

BUG:413548
parent ef55574a
......@@ -48,6 +48,8 @@ KisToolColorPicker::KisToolColorPicker(KoCanvasBase *canvas)
m_isActivated = false;
m_optionsWidget = 0;
m_pickedColor = KoColor();
KoResourceServer<KoColorSet> *srv = KoResourceServerProvider::instance()->paletteServer();
srv->addObserver(this);
}
KisToolColorPicker::~KisToolColorPicker()
......@@ -55,6 +57,8 @@ KisToolColorPicker::~KisToolColorPicker()
if (m_isActivated) {
m_config->save(m_toolActivationSource == KisTool::DefaultActivation);
}
KoResourceServer<KoColorSet> *srv = KoResourceServerProvider::instance()->paletteServer();
srv->removeObserver(this);
}
void KisToolColorPicker::paint(QPainter &gc, const KoViewConverter &converter)
......@@ -200,12 +204,12 @@ void KisToolColorPicker::endPrimaryAction(KoPointerEvent *event)
swatch.setColor(m_pickedColor);
// We don't ask for a name, too intrusive here
KoColorSet *palette = m_palettes.at(m_optionsWidget->cmbPalette->currentIndex());
KoColorSet *palette = m_palettes.at(m_optionsWidget->cmbPalette->currentIndex());
palette->add(swatch);
if (!palette->save()) {
QMessageBox::critical(0, i18nc("@title:window", "Krita"), i18n("Cannot write to palette file %1. Maybe it is read-only.", palette->filename()));
}
}
}
}
......@@ -361,7 +365,7 @@ void KisToolColorPicker::slotSetColorSource(int value)
{
m_config->sampleMerged = value == SAMPLE_MERGED;
}
/*
void KisToolColorPicker::slotAddPalette(KoResource *resource)
{
KoColorSet *palette = dynamic_cast<KoColorSet*>(resource);
......@@ -370,3 +374,55 @@ void KisToolColorPicker::slotAddPalette(KoResource *resource)
m_palettes.append(palette);
}
}
*/
void KisToolColorPicker::unsetResourceServer()
{
KoResourceServer<KoColorSet> *srv = KoResourceServerProvider::instance()->paletteServer();
srv->removeObserver(this);
}
void KisToolColorPicker::resourceAdded(KoColorSet* resource){
if( !resource || !m_optionsWidget || m_palettes.contains(resource)) return;
if(m_config->addColorToCurrentPalette){
updateCmbPalette();
}
}
void KisToolColorPicker::removingResource(KoColorSet* resource){
if(!resource || !m_optionsWidget || !m_palettes.contains(resource)) return;
if(m_config->addColorToCurrentPalette){
updateCmbPalette();
}
}
void KisToolColorPicker::updateCmbPalette(){
m_optionsWidget->cmbPalette->clear();
m_palettes.clear();
KoResourceServer<KoColorSet> *srv = KoResourceServerProvider::instance()->paletteServer();
if (!srv) {
return ;
}
QList<KoColorSet*> palettes = srv->resources();
Q_FOREACH (KoColorSet *palette, palettes) {
if (palette) {
m_optionsWidget->cmbPalette->addSqueezedItem(palette->name());
m_palettes.append(palette);
}
}
}
void KisToolColorPicker::resourceChanged(PointerType /*resource*/)
{}
void KisToolColorPicker::syncTaggedResourceView() {}
void KisToolColorPicker::syncTagAddition(const QString& /*tag*/) {}
void KisToolColorPicker::syncTagRemoval(const QString& /*tag*/) {}
\ No newline at end of file
......@@ -28,6 +28,8 @@
#include "kis_tool.h"
#include <kis_icon.h>
#include <KoResourceServerObserver.h>
class KoResource;
class KoColorSet;
......@@ -45,7 +47,7 @@ public:
}
};
class KisToolColorPicker : public KisTool
class KisToolColorPicker : public KisTool,public KoResourceServerObserver<KoColorSet>
{
Q_OBJECT
Q_PROPERTY(bool toForeground READ toForeground WRITE setToForeground NOTIFY toForegroundChanged)
......@@ -81,6 +83,15 @@ public:
bool toForeground() const;
public: //KoResourceServerObserver
void unsetResourceServer() override;
void resourceAdded(KoColorSet* resource) override;
void removingResource(KoColorSet* resource) override;
void resourceChanged(KoColorSet* resource) override;
void syncTaggedResourceView() override;
void syncTagAddition(const QString& tag) override;
void syncTagRemoval(const QString& tag) override;
Q_SIGNALS:
void toForegroundChanged();
......@@ -95,14 +106,14 @@ public Q_SLOTS:
void slotSetAddPalette(bool);
void slotChangeRadius(int);
void slotChangeBlend(int);
void slotAddPalette(KoResource* resource);
//void slotAddPalette(KoResource* resource);
void slotSetColorSource(int value);
private:
void displayPickedColor();
bool pickColor(const QPointF& pos);
void updateOptionWidget();
void updateCmbPalette();
// Configuration
QScopedPointer<KisToolUtils::ColorPickerConfig> m_config;
......
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