Commit 455f46ae authored by Michael Zhou's avatar Michael Zhou

Moved KisPaletteView to kritawidgets

The next step is to modify KoEditColorSetEditor and KoColorSetWidget to use it
parent c8f5a15f
This diff is collapsed.
/*
* Copyright (c) 2013 Sven Langkamp <sven.langkamp@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef KIS_PALETTEMODEL_H
#define KIS_PALETTEMODEL_H
#include <QPointer>
#include <QModelIndex>
#include <KoColorDisplayRendererInterface.h>
/// #include <kis_types.h>
#include "kritawidgets_export.h"
#include <KoColorSet.h>
#include <QScopedPointer>
class KoColorSet;
/**
* @brief The KisPaletteModel class
* This, together with kis_palette_view and kis_palette_delegate forms a mvc way to access kocolorsets.
*/
class KRITAWIDGETS_EXPORT KisPaletteModel : public QAbstractTableModel
{
Q_OBJECT
public:
KisPaletteModel(QObject* parent = 0);
~KisPaletteModel() override;
enum AdditionalRoles {
IsHeaderRole = Qt::UserRole + 1,
ExpandCategoryRole = Qt::UserRole + 2,
RetrieveEntryRole = Qt::UserRole + 3
};
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
int columnCount(const QModelIndex& parent = QModelIndex()) const override;
Qt::ItemFlags flags(const QModelIndex& index) const override;
QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override;
void setColorSet(KoColorSet* colorSet);
KoColorSet* colorSet() const;
/**
* Installs a display renderer object for a palette that will
* convert the KoColor to the displayable QColor. Default is the
* dumb renderer.
*/
void setDisplayRenderer(KoColorDisplayRendererInterface *displayRenderer);
/**
* @brief indexFromId
* convenience function to get the tableindex from the global palette color.
* used by lazybrush.
* @param i
* @return index in table.
*/
QModelIndex indexFromId(int i) const;
/**
* @brief idFromIndex
* convenience function to get the global colorset entry id from the table index.
* If you just want to use this to get the kocolorsetentry, use colorsetEntryFromIndex instead.
* @param index
* @return
*/
int idFromIndex(const QModelIndex &index) const;
/**
* @brief colorSetEntryFromIndex
* This gives the colorset entry for the given table model index.
* @param index the QModelIndex
* @return the kocolorsetentry
*/
KoColorSetEntry colorSetEntryFromIndex(const QModelIndex &index) const;
/**
* @brief addColorSetEntry
* proper function to handle adding entries.
* @return whether successful.
*/
bool addColorSetEntry(KoColorSetEntry entry, QString groupName=QString());
/**
* @brief removeEntry
* proper function to remove the colorsetentry at the given index.
* The consolidtes both removeentry and removegroup.
* @param keepColors: This bool determines whether, when deleting a group,
* the colors should be added to the default group. This is usually desirable,
* so hence the default is true.
* @return if successful
*/
bool removeEntry(QModelIndex index, bool keepColors=true);
/**
* @brief addGroup
* Adds a group to the list.
* @param groupName
* @return if successful
*/
bool addGroup(QString groupName = QString());
bool removeRows(int row, int count, const QModelIndex &parent) override;
/**
* @brief dropMimeData
* This is an overridden function that handles dropped mimedata.
* right now only colorsetentries and colorsetgroups are handled.
* @return
*/
bool dropMimeData(const QMimeData *data, Qt::DropAction action,
int row, int column, const QModelIndex &parent) override;
/**
* @brief mimeData
* gives the mimedata for a kocolorsetentry or a kocolorsetgroup.
* @param indexes
* @return the mimedata for the given indices
*/
QMimeData *mimeData(const QModelIndexList &indexes) const override;
QStringList mimeTypes() const override;
Qt::DropActions supportedDropActions() const override;
private Q_SLOTS:
void slotDisplayConfigurationChanged();
private:
KoColorSet* m_colorSet;
QPointer<KoColorDisplayRendererInterface> m_displayRenderer;
QModelIndex getLastEntryIndex();
};
#endif
/*
* Copyright (c) 2016 Dmitry Kazakov <dimula73@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "kis_palette_delegate.h"
#include <QPen>
#include <QPainter>
#include <kis_global.h>
#include "kis_debug.h"
#include <KisPaletteModel.h>
KisPaletteDelegate::KisPaletteDelegate(QObject *parent)
: QAbstractItemDelegate(parent)
{
}
KisPaletteDelegate::~KisPaletteDelegate()
{
}
void KisPaletteDelegate::setCrossedKeyword(const QString &value)
{
m_crossedKeyword = value;
}
void KisPaletteDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
painter->save();
if (! index.isValid())
return;
const bool isSelected = option.state & QStyle::State_Selected;
const int minSize = qMin(option.rect.width(), option.rect.height());
const int maxWidth = qBound(2, minSize / 6, 4);
const int width = isSelected ? maxWidth : 1;
if (qvariant_cast<bool>(index.data(KisPaletteModel::IsHeaderRole))) {
QString name = qvariant_cast<QString>(index.data(Qt::DisplayRole));
if (isSelected) {
painter->fillRect(option.rect, option.palette.highlight());
}
QRect paintRect = kisGrowRect(option.rect, -width);
painter->drawText(paintRect, name);
} else {
if (isSelected) {
painter->fillRect(option.rect, option.palette.highlight());
}
QRect paintRect = kisGrowRect(option.rect, -width);
QBrush brush = qvariant_cast<QBrush>(index.data(Qt::BackgroundRole));
painter->fillRect(paintRect, brush);
}
painter->restore();
QString name = qvariant_cast<QString>(index.data(Qt::DisplayRole));
if (!m_crossedKeyword.isNull() && name.toLower().contains(m_crossedKeyword)) {
QRect crossRect = kisGrowRect(option.rect, -maxWidth);
painter->save();
painter->setRenderHint(QPainter::Antialiasing, true);
painter->setPen(QPen(Qt::white, 2.5));
painter->drawLine(crossRect.topLeft(), crossRect.bottomRight());
painter->setPen(QPen(Qt::red, 1.0));
painter->drawLine(crossRect.topLeft(), crossRect.bottomRight());
painter->restore();
}
}
QSize KisPaletteDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex &) const
{
return option.decorationSize;
}
/*
* Copyright (c) 2016 Dmitry Kazakov <dimula73@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef __KIS_PALETTE_DELEGATE_H
#define __KIS_PALETTE_DELEGATE_H
#include <QAbstractItemDelegate>
#include "kritawidgets_export.h"
class KRITAWIDGETS_EXPORT KisPaletteDelegate : public QAbstractItemDelegate
{
public:
KisPaletteDelegate(QObject * parent = 0);
~KisPaletteDelegate() override;
void setCrossedKeyword(const QString &value);
void paint(QPainter *, const QStyleOptionViewItem &, const QModelIndex &) const override;
QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex &) const override;
private:
QString m_crossedKeyword;
};
#endif /* __KIS_PALETTE_DELEGATE_H */
This diff is collapsed.
/*
* Copyright (c) 2016 Dmitry Kazakov <dimula73@gmail.com>
* Copyright (c) 2017 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef __KIS_PALETTE_VIEW_H
#define __KIS_PALETTE_VIEW_H
#include <QScopedPointer>
#include <QColorDialog>
#include <QPushButton>
#include <QPixmap>
#include <QIcon>
#include <KoTableView.h>
#include <KoColorSet.h>
#include "kritawidgets_export.h"
class KisPaletteModel;
class QWheelEvent;
class KRITAWIDGETS_EXPORT KisPaletteView : public KoTableView
{
Q_OBJECT
public:
KisPaletteView(QWidget *parent = 0);
~KisPaletteView() override;
void setPaletteModel(KisPaletteModel *model);
KisPaletteModel* paletteModel() const;
/**
* @brief updateRows
* update the rows so they have the proper columnspanning.
*/
void updateRows();
/**
* @brief setAllowModification
* Set whether doubleclick calls up a modification window. This is to prevent users from editing
* the palette when the palette is intended to be a list of items.
*/
void setAllowModification(bool allow);
/**
* @brief setCrossedKeyword
* this apparently allows you to set keywords that can cross out colors.
* This is implemented to mark the lazybrush "transparent" color.
* @param value
*/
void setCrossedKeyword(const QString &value);
public Q_SLOTS:
void paletteModelChanged();
/**
* add an entry with a dialog window.
*/
bool addEntryWithDialog(KoColor color);
/**
* @brief addGroupWithDialog
* summons a little dialog to name the new group.
*/
bool addGroupWithDialog();
/**
* remove entry with a dialog window.(Necessary for groups.
*/
bool removeEntryWithDialog(QModelIndex index);
/**
* This tries to select the closest color in the palette.
* This doesn't update the foreground color, just the visual selection.
*/
void trySelectClosestColor(KoColor color);
Q_SIGNALS:
/**
* @brief entrySelected
* signals when an entry is selected.
* @param entry the selected entry.
*/
void entrySelected(KoColorSetEntry entry);
void entrySelectedBackGround(KoColorSetEntry entry);
void indexEntrySelected(QModelIndex index);
protected:
void mouseReleaseEvent(QMouseEvent *event) override;
void wheelEvent(QWheelEvent *event) override;
private:
struct Private;
class SimpleColorButton;
const QScopedPointer<Private> m_d;
private Q_SLOTS:
/**
* @brief entrySelection
* the function that will emit entrySelected when the entry changes.
*/
void entrySelection(bool foreground = true);
/**
* @brief modifyEntry
* function for changing the entry at the given index.
* if modification isn't allow(@see setAllowModification), this does nothing.
*/
void modifyEntry(QModelIndex index);
};
class KisPaletteView::SimpleColorButton : public QPushButton
{
Q_OBJECT
public:
SimpleColorButton(QWidget *parent = nullptr)
: QPushButton(parent)
, m_color(KoColor())
{
m_color.fromQColor(QColor(0, 0, 0));
connect(this, SIGNAL(clicked()), this, SLOT(getColor()));
updateIcon();
}
KoColor color() const {
return m_color;
}
void setColor(KoColor c) {
m_color = c;
updateIcon();
}
private Q_SLOTS:
void getColor() {
QColor qc = QColorDialog::getColor(m_color.toQColor(), this);
KoColor kc;
kc.fromQColor(qc);
m_color = kc;
updateIcon();
}
private:
void updateIcon() {
QPixmap iconPixmap = QPixmap(32, 32);
iconPixmap.fill(m_color.toQColor());
setIcon(QIcon(iconPixmap));
}
KoColor m_color;
};
#endif /* __KIS_PALETTE_VIEW_H */
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