Commit 0a9f5d06 authored by Sven Langkamp's avatar Sven Langkamp
Browse files

use presets instead of paintops in popup palette

BUG:273876
parent 882a91be
/* This file is part of the KDE project
Copyright 2009 Vera Lukman <shicmap@gmail.com>
Copyright 2011 Sven Langkamp <sven.langkamp@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
......@@ -43,57 +44,61 @@
#include "kis_paintop_settings_widget.h"
#include "kis_shared_ptr.h"
#include "ko_favorite_resource_manager.h"
#include "KoResourceModel.h"
#include "KoResourceItemView.h"
#include "kis_resource_server_provider.h"
#include "KoResourceServerAdapter.h"
#include "kis_paintop_preset.h"
#include "kis_preset_chooser.h"
KisPaletteManager::KisPaletteManager(KoFavoriteResourceManager *manager, KisPaintopBox *paintOpBox)
: QDialog(paintOpBox)
, m_model(0)
, m_brushList (0)
, m_saveButton(0)
, m_removeButton(0)
, m_resourceManager(manager)
, m_paintOpBox(paintOpBox)
, m_currentBrushLabel(0)
{
setWindowTitle(i18n("Krita - Palette Manager"));
m_currentBrushLabel = new QLabel ("");
setWindowTitle(i18n("Palette Manager"));
m_allPresetsView = new KisPresetChooser(this);
m_allPresetsView->showButtons(false);
m_allPresetsView->setPresetFilter(KoID("dummy",""));
m_allPresetsView->setShowAll(true);
/*SETTING MODEL*/
m_model = new QStringListModel();
resetDataModel();
m_palettePresetsView = new KisPresetChooser(this);
m_palettePresetsView->showButtons(false);
m_palettePresetsView->setPresetFilter(KoID("dummy",""));
m_palettePresetsView->setShowAll(true);
/*LEFT COMPONENTS*/
QFrame *HSeparator = new QFrame();
HSeparator->setFrameStyle(QFrame::HLine | QFrame::Sunken);
m_saveButton = new QPushButton (i18n("&Save to Palette"));
m_saveButton = new QPushButton (i18n("Add to Palette"));
m_saveButton->setSizePolicy(QSizePolicy::Fixed , QSizePolicy::Fixed);
m_saveButton->setEnabled(false);
/*LEFT LAYOUT*/
QVBoxLayout *leftLayout = new QVBoxLayout ();
leftLayout->addWidget(new QLabel (i18n("Current Brush")));
leftLayout->addWidget(HSeparator);
leftLayout->addWidget(m_currentBrushLabel);
// leftLayout->addWidget(paintOpSettings->widget());
leftLayout->addStretch();
leftLayout->addWidget(new QLabel(i18n("Available Presets")));
leftLayout->addWidget(m_allPresetsView);
leftLayout->addWidget(m_saveButton);
m_allPresetsView->updateViewSettings();
/*CENTER COMPONENT : Divider*/
QFrame *VSeparator = new QFrame();
VSeparator->setFrameStyle(QFrame::VLine | QFrame::Sunken);
/*RIGHT COMPONENTS*/
m_brushList = new QListView;
m_brushList->setModel(m_model);
m_brushList->setWordWrap(true);
m_removeButton = new QPushButton(i18n("Remove Brush"));
m_removeButton = new QPushButton(i18n("Remove Preset"));
m_removeButton->setSizePolicy(QSizePolicy::Fixed , QSizePolicy::Fixed);
m_removeButton->setEnabled(false);//set the button to center
/*RIGHT LAYOUT*/
QVBoxLayout *rightLayout = new QVBoxLayout();
rightLayout->addWidget(new QLabel(i18n("Favorite Brushes")));
rightLayout->addWidget(m_brushList);
rightLayout->addWidget(new QLabel(i18n("Favorite Presets")));
rightLayout->addWidget(m_palettePresetsView);
rightLayout->addWidget(m_removeButton);
/*MAIN LAYOUT*/
......@@ -103,32 +108,27 @@ KisPaletteManager::KisPaletteManager(KoFavoriteResourceManager *manager, KisPain
mainLayout->addLayout(rightLayout);
setLayout(mainLayout);
changeCurrentBrushLabel();
/*SIGNALS AND SLOTS*/
connect(m_brushList, SIGNAL(pressed(QModelIndex)), this, SLOT(slotEnableRemoveButton()));
connect(m_allPresetsView, SIGNAL(resourceSelected(KoResource*)), this, SLOT(slotUpdateAddButton()) );
connect(m_palettePresetsView, SIGNAL(resourceSelected(KoResource*)), this, SLOT(slotEnableRemoveButton()) );
connect(m_removeButton, SIGNAL(clicked()), this, SLOT(slotDeleteBrush()));
connect(m_saveButton, SIGNAL(clicked()), this, SLOT(slotAddBrush()));
}
void KisPaletteManager::changeCurrentBrushLabel()
{
m_currentBrushLabel->setText(m_paintOpBox->currentPaintop().id());
//m_paintOpBox->currentPaintopKoID().name() doesn't work properly.
updatePaletteView();
}
void KisPaletteManager::resetDataModel()
KisPaletteManager::~KisPaletteManager()
{
m_nameList = m_resourceManager->favoriteBrushesStringList();
m_model->setStringList(m_nameList);
m_resourceManager = 0;
m_paintOpBox = 0;
}
void KisPaletteManager::slotAddBrush()
{
KisPaintOpPresetSP newBrush = m_paintOpBox->paintOpPresetSP();
int pos = m_resourceManager->addFavoriteBrush(newBrush);
KisPaintOpPreset* newPreset = static_cast<KisPaintOpPreset*>(m_allPresetsView->currentResource());
int pos = m_resourceManager->addFavoritePreset(newPreset->name());
QModelIndex index;
......@@ -138,42 +138,45 @@ void KisPaletteManager::slotAddBrush()
}
else if (pos == -1) //favorite brush is successfully saved
{
m_nameList.append(m_paintOpBox->currentPaintop().id());
m_model->setStringList(m_nameList);
index = m_model->index(m_resourceManager->favoriteBrushesTotal()-1);
}
else //brush has already existed
{
index = m_model->index(pos);
updatePaletteView();
}
}
m_brushList->setCurrentIndex(index);
slotEnableRemoveButton();
void KisPaletteManager::slotUpdateAddButton()
{
KoResource * resource = m_allPresetsView-> currentResource();
if( resource ) {
m_saveButton->setEnabled(true);
} else {
m_saveButton->setEnabled(false);
}
}
void KisPaletteManager::slotEnableRemoveButton()
{
QModelIndex index = m_brushList->currentIndex();
m_removeButton->setEnabled(index.row() != -1);
KoResource * resource = m_allPresetsView->currentResource();
m_removeButton->setEnabled(resource != 0);
}
void KisPaletteManager::slotDeleteBrush()
{
int pos = m_brushList->currentIndex().row();
m_nameList.removeAt(pos);
m_resourceManager->removeFavoriteBrush(pos);
m_model->setStringList(m_nameList);
KoResource * resource = m_palettePresetsView->currentResource();
m_resourceManager->removeFavoritePreset(resource->name());
m_removeButton->setEnabled(false);
updatePaletteView();
}
KisPaletteManager::~KisPaletteManager()
void KisPaletteManager::showEvent(QShowEvent* e)
{
delete m_model;
delete m_brushList;
delete m_saveButton;
delete m_removeButton;
m_resourceManager = 0;
m_paintOpBox = 0;
QDialog::showEvent(e);
m_allPresetsView->updateViewSettings();
m_palettePresetsView->updateViewSettings();
}
void KisPaletteManager::updatePaletteView()
{
m_palettePresetsView->setFilteredNames(m_resourceManager->favoritePresetList());
}
#include "kis_palette_manager.moc"
......@@ -31,6 +31,7 @@ class QLabel;
class QPushButton;
class KoFavoriteResourceManager;
class KisPaintopBox;
class KisPresetChooser;
class KisPaletteManager : public QDialog
{
......@@ -39,25 +40,24 @@ class KisPaletteManager : public QDialog
public:
KisPaletteManager(KoFavoriteResourceManager*, KisPaintopBox*);
~KisPaletteManager();
void changeCurrentBrushLabel();
virtual void showEvent(QShowEvent* );
void updatePaletteView();
private slots:
void slotUpdateAddButton();
void slotEnableRemoveButton();
void slotDeleteBrush();
void slotAddBrush();
private:
QStringListModel * m_model;
QListView *m_brushList;
QStringList m_nameList;
QPushButton *m_saveButton;
QPushButton *m_removeButton;
KoFavoriteResourceManager *m_resourceManager;
KisPaintopBox *m_paintOpBox;
QLabel *m_currentBrushLabel;
void resetDataModel();
KisPresetChooser* m_allPresetsView;
KisPresetChooser* m_palettePresetsView;
};
......
/* This file is part of the KDE project
Copyright 2009 Vera Lukman <shicmap@gmail.com>
Copyright 2011 Sven Langkamp <sven.langkamp@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
......@@ -21,6 +22,7 @@
#include "kis_paintop_box.h"
#include "ko_favorite_resource_manager.h"
#include "KoColorSpaceRegistry.h"
#include "KoResource.h"
#include <kis_types.h>
#include <QtGui>
#include <QDebug>
......@@ -28,6 +30,8 @@
#include <QtGui>
#include <math.h>
#define maxPresetCount 10
class PopupColorTriangle : public KoTriangleColorSelector
{
public:
......@@ -76,7 +80,7 @@ KisPopupPalette::KisPopupPalette(KoFavoriteResourceManager* manager, QWidget *pa
, m_timer(0)
{
m_triangleColorSelector = new PopupColorTriangle(this);
m_triangleColorSelector->move(60, 60);
m_triangleColorSelector->move(90, 90);
m_triangleColorSelector->setVisible(true);
setAutoFillBackground(true);
......@@ -106,9 +110,8 @@ KisPopupPalette::KisPopupPalette(KoFavoriteResourceManager* manager, QWidget *pa
connect(m_colorChangeTimer, SIGNAL(timeout()), this, SLOT(slotColorChangeTimeout()));
setMouseTracking(true);
setHoveredBrush(-1);
setHoveredPreset(-1);
setHoveredColor(-1);
setSelectedBrush(-1);
setSelectedColor(-1);
setVisible(true);
......@@ -116,10 +119,8 @@ KisPopupPalette::KisPopupPalette(KoFavoriteResourceManager* manager, QWidget *pa
}
//setting KisPopupPalette properties
int KisPopupPalette::hoveredBrush() const { return m_hoveredBrush; }
void KisPopupPalette::setHoveredBrush(int x) { m_hoveredBrush = x; }
int KisPopupPalette::selectedBrush() const { return m_selectedBrush; }
void KisPopupPalette::setSelectedBrush(int x) { m_selectedBrush = x; }
int KisPopupPalette::hoveredPreset() const { return m_hoveredPreset; }
void KisPopupPalette::setHoveredPreset(int x) { m_hoveredPreset = x; }
int KisPopupPalette::hoveredColor() const { return m_hoveredColor; }
void KisPopupPalette::setHoveredColor(int x) { m_hoveredColor = x; }
int KisPopupPalette::selectedColor() const { return m_selectedColor; }
......@@ -195,7 +196,7 @@ void KisPopupPalette::setVisible(bool b)
QSize KisPopupPalette::sizeHint() const
{
return QSize(220,220);
return QSize(280,280);
}
void KisPopupPalette::resizeEvent(QResizeEvent*)
......@@ -215,36 +216,28 @@ void KisPopupPalette::paintEvent(QPaintEvent* e)
painter.translate(width()/2, height()/2);
//painting favorite brushes
QList<QPixmap> pixmaps (m_resourceManager->favoriteBrushPixmaps());
rotationAngle = 360.0/pixmaps.size();
//highlight hovered brush
if (hoveredBrush() > -1)
{
painter.rotate((pixmaps.size() - hoveredBrush()) *rotationAngle);
QPainterPath path(drawDonutPathAngle(brushInnerRadius,brushOuterRadius, pixmaps.size()));
painter.fillPath(path, palette().color(QPalette::Highlight));
painter.rotate(hoveredBrush() *rotationAngle);
}
//highlight selected brush
if (selectedBrush() > -1)
{
painter.rotate((pixmaps.size() - selectedBrush()) *rotationAngle);
QPainterPath path(drawDonutPathAngle(brushInnerRadius,brushOuterRadius, pixmaps.size()));
painter.fillPath(path, palette().color(QPalette::Highlight).darker(130));
painter.rotate(selectedBrush() *rotationAngle);
}
QList<QImage> images (m_resourceManager->favoritePresetImages());
//painting favorite brushes pixmap/icon
for (int pos = 0; pos < pixmaps.size(); pos++)
QPainterPath path;
for (int pos = 0; pos < images.size(); pos++)
{
QPixmap pixmap(pixmaps.at(pos));
QPointF pixmapOffset(pixmap.width()/2, pixmap.height()/2);
float angle = pos*M_PI*2.0/pixmaps.size();
QPointF pointTemp(brushRadius*sin(angle),brushRadius*cos(angle));
painter.drawPixmap(QPoint(pointTemp.x()-pixmapOffset.x(), pointTemp.y()-pixmapOffset.y()), pixmap);
painter.save();
path = pathFromPresetIndex(pos);
painter.setClipPath(path);
QRect bounds = path.boundingRect().toAlignedRect();
painter.drawImage(bounds.topLeft(), images.at(pos).scaled(bounds.size(), Qt::KeepAspectRatioByExpanding));
painter.restore();
}
if(hoveredPreset() > -1) {
path = pathFromPresetIndex(hoveredPreset());
QPen pen(palette().color(QPalette::Highlight));
pen.setWidth(3);
painter.setPen(pen);
painter.drawPath(path);
}
QColor currColor;
......@@ -372,17 +365,16 @@ void KisPopupPalette::mouseMoveEvent (QMouseEvent* event)
QPainterPath pathBrush(drawDonutPathFull(width()/2, height()/2, brushInnerRadius, brushOuterRadius));
QPainterPath pathColor(drawDonutPathFull(width()/2, height()/2, colorInnerRadius, colorOuterRadius));
setHoveredBrush(-1);
setHoveredPreset(-1);
setHoveredColor(-1);
if (pathBrush.contains(point))
{ //in favorite brushes area
int pos = calculateIndex(point, m_resourceManager->favoriteBrushesTotal());
int pos = calculatePresetIndex(point, m_resourceManager->favoritePresetsTotal());
if (pos >= 0 && pos < m_resourceManager->favoriteBrushesTotal()
&& isPointInPixmap(point, pos))
if (pos >= 0)
{
setHoveredBrush(pos);
setHoveredPreset(pos);
}
}
else if (pathColor.contains(point))
......@@ -408,11 +400,11 @@ void KisPopupPalette::mousePressEvent(QMouseEvent* event)
if (pathBrush.contains(point))
{ //in favorite brushes area
int pos = calculateIndex(point, m_resourceManager->favoriteBrushesTotal());
if (pos >= 0 && pos < m_resourceManager->favoriteBrushesTotal()
int pos = calculateIndex(point, m_resourceManager->favoritePresetsTotal());
if (pos >= 0 && pos < m_resourceManager->favoritePresetsTotal()
&& isPointInPixmap(point, pos))
{
setSelectedBrush(pos);
//setSelectedBrush(pos);
update();
}
}
......@@ -431,10 +423,10 @@ void KisPopupPalette::mouseReleaseEvent ( QMouseEvent * event )
if (pathBrush.contains(point))
{ //in favorite brushes area
if (hoveredBrush() > -1)
if (hoveredPreset() > -1)
{
setSelectedBrush(hoveredBrush());
emit sigChangeActivePaintop(hoveredBrush());
//setSelectedBrush(hoveredBrush());
emit sigChangeActivePaintop(hoveredPreset());
}
}
else if (pathColor.contains(point))
......@@ -482,6 +474,7 @@ void KisPopupPalette::tabletEvent(QTabletEvent* event)
int KisPopupPalette::calculateIndex(QPointF point, int n)
{
calculatePresetIndex(point, n);
//translate to (0,0)
point.setX(point.x() - width()/2);
point.setY(point.y() - height()/2);
......@@ -501,16 +494,10 @@ int KisPopupPalette::calculateIndex(QPointF point, int n)
bool KisPopupPalette::isPointInPixmap(QPointF& point, int pos)
{
QPixmap pixmap(m_resourceManager->favoriteBrushPixmap(pos));
//calculating if the point is inside the pixmap
float angle = pos*M_PI*2.0/m_resourceManager->favoriteBrushesTotal();
QPainterPath path;
path.addRect(brushRadius*sin(angle)-pixmap.width()/2+width()/2,
brushRadius*cos(angle)-pixmap.height()/2+height()/2,
pixmap.width(), pixmap.height());
return path.contains(point) || pixmap.isNull();
if(pathFromPresetIndex(pos).contains(point + QPointF(-width()/2, -height()/2))) {
return true;
}
return false;
}
KisPopupPalette::~KisPopupPalette()
......@@ -520,4 +507,40 @@ KisPopupPalette::~KisPopupPalette()
m_resourceManager = 0;
}
QPainterPath KisPopupPalette::pathFromPresetIndex(int index)
{
QRect outerRect(-width()/2, -height()/2, width(), height());
outerRect.adjust(3, 3, -3, -3);
int ringSize = brushOuterRadius - colorOuterRadius;
QRect innerRect = outerRect.adjusted(ringSize, ringSize, -ringSize, -ringSize);
qreal angleLength = 360/10;
qreal angle = index*angleLength;
QPainterPath path;
path.moveTo( brushOuterRadius*cos(angle/180*M_PI),-brushOuterRadius*sin(angle/180*M_PI));
path.arcTo ( outerRect, angle, angleLength );
path.arcTo ( innerRect, angle + angleLength, -angleLength );
path.closeSubpath();
return path;
}
int KisPopupPalette::calculatePresetIndex(QPointF point, int n)
{
int x = point.x() - width()/2;
int y = point.y() - height()/2;
qreal radius = sqrt ( x*x + y*y );
qreal angle;
// y coordinate is the reverse of the cartesian one
if(y < 0) {
angle = acos(x/radius);
} else {
angle = 2*M_PI - acos(x/radius);
}
int pos = floor(angle/(2*M_PI/10));
return pos;
}
#include "kis_popup_palette.moc"
......@@ -20,7 +20,7 @@
#define KIS_POPUP_PALETTE_H
#define brushInnerRadius 80.0
#define brushOuterRadius 100.0
#define brushOuterRadius 130.0
#define colorInnerRadius 55.0
#define colorOuterRadius 75.0
#define brushRadius (brushInnerRadius+brushOuterRadius)/2
......@@ -39,8 +39,7 @@ class KisTriangleColorSelector;
class KisPopupPalette : public QWidget
{
Q_OBJECT
Q_PROPERTY (int hoveredBrush READ hoveredBrush WRITE setHoveredBrush);
Q_PROPERTY (int selectedBrush READ selectedBrush WRITE setSelectedBrush);
Q_PROPERTY (int hoveredPreset READ hoveredPreset WRITE setHoveredPreset);
Q_PROPERTY (int hoveredColor READ hoveredColor WRITE setHoveredColor);
Q_PROPERTY (int selectedColor READ selectedColor WRITE setSelectedColor);
......@@ -70,10 +69,12 @@ protected:
//functions to calculate index of favorite brush or recent color in array
//n is the total number of favorite brushes or recent colors
int calculateIndex(QPointF, int n);
int calculatePresetIndex(QPointF, int n);
//functions to set up hoveredBrush
void setHoveredBrush( int x );
int hoveredBrush() const;
void setHoveredPreset( int x );
int hoveredPreset() const;
//functions to set up hoveredColor
void setHoveredColor( int x );
int hoveredColor() const;
......@@ -87,9 +88,10 @@ private:
void drawArcRisen(QPainter& painter, const QColor& color1, const QColor& color2, int radius);
bool isPointInPixmap(QPointF&, int pos);
QPainterPath pathFromPresetIndex(int index);
private:
int m_hoveredBrush;
int m_selectedBrush;
int m_hoveredPreset;
int m_hoveredColor;
int m_selectedColor;
KoFavoriteResourceManager* m_resourceManager;
......
/* This file is part of the KDE project
Copyright 2009 Vera Lukman <shicmap@gmail.com>
Copyright 2011 Sven Langkamp <sven.langkamp@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
......@@ -33,9 +34,10 @@
#include "kis_paintop_box.h"
#include "kis_palette_manager.h"
#include "kis_view2.h"
#include "kis_resource_server_provider.h"
#ifndef _MSC_EXTENSIONS
const int KoFavoriteResourceManager::MAX_FAVORITE_BRUSHES;
const int KoFavoriteResourceManager::MAX_FAVORITE_PRESETS;
//const int KoFavoriteResourceManager::MAX_RECENT_COLORS;
#endif
......@@ -50,54 +52,27 @@ KoFavoriteResourceManager::KoFavoriteResourceManager(KisPaintopBox *paintopBox,
//take favorite brushes from a file then append to QList
KConfigGroup group(KGlobal::config(), "favoriteList");
QStringList favoriteList = (group.readEntry("favoriteBrushes")).split(',', QString::SkipEmptyParts);
for (int pos = 0; pos < favoriteList.size(); pos++)
{
KoID id(favoriteList[pos], favoriteList[pos]);
if (KisPaintOpRegistry::instance()->contains(id.id())) {
KisPaintOpPresetSP newBrush = m_paintopBox->paintOpPresetSP(&id);
m_favoriteBrushesList.append(newBrush);
}
}
m_favoritePresetsList = (group.readEntry("favoritePresets")).split(',', QString::SkipEmptyParts);
m_popupPalette = new KisPopupPalette(this, popupParent);
m_popupPalette->showPopupPalette(false);
m_colorList = new KisColorDataList();
KoResourceServer<KisPaintOpPreset>* rServer = KisResourceServerProvider::instance()->paintOpPresetServer();
rServer->addObserver(this);
}
QStringList KoFavoriteResourceManager::favoriteBrushesStringList()
KoFavoriteResourceManager::~KoFavoriteResourceManager()
{
QStringList list;
for (int pos = 0; pos < m_favoriteBrushesList.size(); pos++)
{
list.append(m_favoriteBrushesList.at(pos)->paintOp().id());
}
return list;
KoResourceServer<KisPaintOpPreset>* rServer = KisResourceServerProvider::instance()->paintOpPresetServer();
rServer->removeObserver(this);
delete m_favoriteBrushManager;
delete m_colorList;
}
void KoFavoriteResourceManager::slotChangePaintopLabel(KisPaintOpPresetSP paintop)
QStringList KoFavoriteResourceManager::favoritePresetList()
{
if (m_favoriteBrushManager)
m_favoriteBrushManager->changeCurrentBrushLabel();
//setting selected brush on pop up palette
if (m_popupPalette)
{
int pos = isFavoriteBrushSaved(paintop);
if (pos > -1) //paintop is in the list, set selected brush
{
m_popupPalette->setSelectedBrush(pos);
}
else
{
m_popupPalette->setSelectedBrush(-1);
}
m_popupPalette->update();
}
return m_favoritePresetsList;
}
//Popup Palette
......@@ -115,27 +90,27 @@ void KoFavoriteResourceManager::resetPopupPaletteParent(QWidget* w)
}
}
QList<QPixmap> KoFavoriteResourceManager::favoriteBrushPixmaps()
QList<QImage> KoFavoriteResourceManager::favoritePresetImages()
{