Commit 7841f361 authored by Silvio Heinrich's avatar Silvio Heinrich

Used the new categorized list where it was possible and removed old code.

* done a lot of cleanup and renaming
parent 0542842f
/* This file is part of the KDE project
* Copyright (c) 2010 Cyrille Berger <cberger@cberger.net>
* Copyright (c) 2011 Silvio Heinrich <plassyqweb.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
......@@ -18,79 +19,62 @@
*/
#include "kis_paintop_options_model.h"
#include <kcategorizedsortfilterproxymodel.h>
#include "kis_paintop_option.h"
KisPaintOpOptionsModel::KisPaintOpOptionsModel()
void KisPaintOpOptionListModel::addPaintOpOption(KisPaintOpOption* option, int widgetIndex)
{
BaseCalss::addEntry(option->category(), KisOptionInfo(option, widgetIndex));
}
KisPaintOpOptionsModel::~KisPaintOpOptionsModel()
QString KisPaintOpOptionListModel::categoryToString(const QString& val) const
{
return val;
}
int KisPaintOpOptionsModel::rowCount(const QModelIndex & /*parent*/ ) const
QString KisPaintOpOptionListModel::entryToString(const KisOptionInfo& val) const
{
return m_list.count();
return val.option->label();
}
bool KisPaintOpOptionsModel::setData(const QModelIndex &index, const QVariant &value, int role )
QVariant KisPaintOpOptionListModel::data(const QModelIndex& idx, int role) const
{
if (index.isValid())
{
switch (role) {
case Qt::CheckStateRole: {
if(m_list[index.row()]->isCheckable()) {
m_list[index.row()]->setChecked( value.toInt() == Qt::Checked);
return true;
}
break;
}
}
if(idx.isValid() && role == Qt::CheckStateRole) {
KisOptionInfo info;
if(BaseCalss::entryAt(info, idx.row()) && info.option->isCheckable())
return info.option->isChecked() ? Qt::Checked : Qt::Unchecked;
return QVariant();
}
return false;
return BaseCalss::data(idx, role);
}
QVariant KisPaintOpOptionsModel::data(const QModelIndex & index, int role ) const
bool KisPaintOpOptionListModel::setData(const QModelIndex& idx, const QVariant& value, int role)
{
if (index.isValid()) {
switch (role) {
case Qt::DisplayRole: {
return m_list[index.row()]->label();
}
case Qt::CheckStateRole: {
if(m_list[index.row()]->isCheckable()) {
return m_list[index.row()]->isChecked() ? Qt::Checked : Qt::Unchecked;
}
break;
}
case SortingRole:
return index.row();
case WidgetIndexRole:
return m_widgetIndex[index.row()];
case KCategorizedSortFilterProxyModel::CategoryDisplayRole:
case KCategorizedSortFilterProxyModel::CategorySortRole:
return m_list[index.row()]->category();
if(idx.isValid() && role == Qt::CheckStateRole) {
KisOptionInfo info;
if(BaseCalss::entryAt(info, idx.row()) && info.option->isCheckable()) {
info.option->setChecked(value.toInt() == Qt::Checked);
return true;
}
return false;
}
return QVariant();
return BaseCalss::setData(idx, value, role);
}
Qt::ItemFlags KisPaintOpOptionsModel::flags(const QModelIndex & index) const
Qt::ItemFlags KisPaintOpOptionListModel::flags(const QModelIndex& idx) const
{
Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
if(m_list[index.row()]->isCheckable())
{
flags |= Qt::ItemIsUserCheckable;
Qt::ItemFlags flags = 0;
KisOptionInfo info;
if(idx.isValid() && BaseCalss::entryAt(info, idx.row())) {
if(info.option->isCheckable())
flags |= Qt::ItemIsUserCheckable;
}
return flags;
}
void KisPaintOpOptionsModel::addPaintOpOption(KisPaintOpOption * option, int widgetIndex)
{
beginResetModel();
m_list.append(option);
m_widgetIndex.append(widgetIndex);
endResetModel();
return BaseCalss::flags(idx) | flags;
}
/* This file is part of the KDE project
* Copyright (c) 2010 Cyrille Berger <cberger@cberger.net>
* Copyright (c) 2011 Silvio Heinrich <plassyqweb.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
......@@ -17,35 +18,39 @@
* Boston, MA 02110-1301, USA.
*/
#ifndef _KIS_PAINTOP_OPTIONS_MODEL_H_
#define _KIS_PAINTOP_OPTIONS_MODEL_H_
#ifndef _KIS_PAINTOP_OPTION_LIST_MODEL_H_
#define _KIS_PAINTOP_OPTION_LIST_MODEL_H_
#include <QAbstractListModel>
#include <kis_categorized_list_model.h>
class KisPaintOpOption;
struct KisOptionInfo
{
KisOptionInfo() { }
KisOptionInfo(KisPaintOpOption* o, int i): option(o), index(i) { }
KisOptionInfo(const KisOptionInfo& info): option(info.option), index(info.index) { }
KisPaintOpOption* option;
int index;
};
/**
* This model can be use to show a list of visible composite op in a list view.
*/
class KisPaintOpOptionsModel : public QAbstractListModel
class KisPaintOpOptionListModel: public KisCategorizedListModel<QString,KisOptionInfo>
{
typedef KisCategorizedListModel<QString,KisOptionInfo> BaseCalss;
public:
enum CustomRoles {
WidgetIndexRole = 0x3201,
SortingRole = 0x3202
};
public:
KisPaintOpOptionsModel();
~KisPaintOpOptionsModel();
int rowCount(const QModelIndex & parent = QModelIndex()) const;
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
void addPaintOpOption(KisPaintOpOption * option, int widgetIndex);
Qt::ItemFlags flags(const QModelIndex & index) const;
void addPaintOpOption(KisPaintOpOption* option, int widgetIndex);
virtual QVariant data(const QModelIndex& idx, int role=Qt::DisplayRole) const;
virtual bool setData(const QModelIndex& idx, const QVariant& value, int role=Qt::EditRole);
virtual Qt::ItemFlags flags(const QModelIndex& idx) const;
using QAbstractListModel::reset;
private:
QList< KisPaintOpOption* > m_list;
QList< int > m_widgetIndex;
protected:
virtual QString categoryToString(const QString& val) const;
virtual QString entryToString(const KisOptionInfo& val) const;
};
#endif
#endif // _KIS_PAINTOP_OPTION_LIST_MODEL_H_
/* This file is part of the KDE project
* Copyright (C) Boudewijn Rempt <boud@valdyas.org>, (C) 2008
*
* Copyright (C) Silvio Heinrich <plassy@web.de> , (C) 2011
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
......@@ -19,60 +20,24 @@
#include "kis_paintop_options_widget.h"
#include "kis_paintop_option.h"
#include "kis_paintop_options_model.h"
#include <QHBoxLayout>
#include <QItemDelegate>
#include <QList>
#include <QListView>
#include <QLabel>
#include <QMap>
#include <QStackedWidget>
#include <QVBoxLayout>
#include <klocale.h>
#include <KCategorizedSortFilterProxyModel>
#include <kis_paintop_preset.h>
#include "kis_paintop_options_model.h"
#include <kis_cmb_composite.h>
#include <kis_categorized_item_delegate.h>
#include <qstyleditemdelegate.h>
class KisPaintopOptionDelegate : public QStyledItemDelegate {
public:
KisPaintopOptionDelegate(QObject* parent = 0): QStyledItemDelegate(parent), m_minimumItemHeight(0)
{
}
virtual QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
//on first calling this calculates the mininmal height of the items
if(m_minimumItemHeight == 0) {
for(int i = 0; i < index.model()->rowCount(); i++) {
QSize indexSize = QStyledItemDelegate::sizeHint(option, index.model()->index(i, 0));
m_minimumItemHeight = qMax(m_minimumItemHeight, indexSize.height());
}
}
QSize sizeHint = QStyledItemDelegate::sizeHint(option, index);
sizeHint.setHeight(m_minimumItemHeight);
return sizeHint;
}
private:
mutable int m_minimumItemHeight;
};
class KisPaintOpOptionsWidget::Private
struct KisPaintOpOptionsWidget::Private
{
public:
QList<KisPaintOpOption*> paintOpOptions;
QListView* optionsList;
KisPaintOpOptionsModel* model;
KCategorizedSortFilterProxyModel* proxyModel;
QStackedWidget * optionsStack;
QList<KisPaintOpOption*> paintOpOptions;
KisCategorizedListView* optionsList;
KisPaintOpOptionListModel* model;
KisCategorizedItemDelegate* delegate;
QStackedWidget* optionsStack;
};
KisPaintOpOptionsWidget::KisPaintOpOptionsWidget(QWidget * parent)
......@@ -80,16 +45,14 @@ KisPaintOpOptionsWidget::KisPaintOpOptionsWidget(QWidget * parent)
, m_d(new Private())
{
setObjectName("KisPaintOpPresetsWidget");
QHBoxLayout * layout = new QHBoxLayout(this);
m_d->optionsList = new QListView(this);
m_d->model = new KisPaintOpOptionsModel;
m_d->proxyModel = new KCategorizedSortFilterProxyModel;
m_d->proxyModel->setSourceModel(m_d->model);
m_d->proxyModel->setCategorizedModel(true);
m_d->proxyModel->setSortRole(KisPaintOpOptionsModel::SortingRole);
m_d->optionsList->setModel(m_d->proxyModel);
m_d->optionsList->setItemDelegate(new KisCategorizedItemDelegate(new KisPaintopOptionDelegate));
m_d->model = new KisPaintOpOptionListModel();
m_d->delegate = new KisCategorizedItemDelegate(m_d->model, false);
m_d->optionsList = new KisCategorizedListView();
m_d->optionsList->setModel(m_d->model);
m_d->optionsList->setItemDelegate(m_d->delegate);
m_d->optionsList->setFixedWidth(128);
QSizePolicy policy = QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
policy.setHorizontalStretch(0);
m_d->optionsList->setSizePolicy(policy);
......@@ -98,15 +61,13 @@ KisPaintOpOptionsWidget::KisPaintOpOptionsWidget(QWidget * parent)
policy = QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
policy.setHorizontalStretch(3);
m_d->optionsStack->setSizePolicy(policy);
QHBoxLayout* layout = new QHBoxLayout(this);
layout->addWidget(m_d->optionsList);
layout->addWidget(m_d->optionsStack);
connect(m_d->optionsList,
SIGNAL(activated(const QModelIndex&)),
this, SLOT(changePage(const QModelIndex&)));
connect(m_d->optionsList,
SIGNAL(clicked(QModelIndex)),
this, SLOT(changePage(const QModelIndex&)));
connect(m_d->optionsList, SIGNAL(activated(const QModelIndex&)), this, SLOT(changePage(const QModelIndex&)));
connect(m_d->optionsList, SIGNAL(clicked(QModelIndex)) , this, SLOT(changePage(const QModelIndex&)));
}
......@@ -114,6 +75,7 @@ KisPaintOpOptionsWidget::~KisPaintOpOptionsWidget()
{
qDeleteAll(m_d->paintOpOptions);
delete m_d->model;
delete m_d->delegate;
delete m_d;
}
......@@ -127,13 +89,14 @@ void KisPaintOpOptionsWidget::addPaintOpOption(KisPaintOpOption * option)
m_d->optionsStack->addWidget(option->configurationPage());
m_d->paintOpOptions << option;
m_d->proxyModel->sort(0);
}
void KisPaintOpOptionsWidget::setConfiguration(const KisPropertiesConfiguration * config)
{
Q_ASSERT(!config->getString("paintop").isEmpty());
m_d->model->reset();
foreach(KisPaintOpOption* option, m_d->paintOpOptions) {
option->readOptionSetting(config);
}
......@@ -155,8 +118,12 @@ void KisPaintOpOptionsWidget::setImage(KisImageWSP image)
void KisPaintOpOptionsWidget::changePage(const QModelIndex& index)
{
m_d->optionsStack->setCurrentIndex( m_d->proxyModel->data(index, KisPaintOpOptionsModel::WidgetIndexRole).toInt());
emit sigConfigurationItemChanged();
KisOptionInfo info;
if(m_d->model->entryAt(info, index.row())) {
m_d->optionsStack->setCurrentIndex(info.index);
emit sigConfigurationItemChanged();
}
}
#include "kis_paintop_options_widget.moc"
......@@ -60,9 +60,9 @@ private slots:
void changePage(const QModelIndex&);
private:
class Private;
Private * const m_d;
struct Private;
Private* const m_d;
};
......
......@@ -134,7 +134,7 @@ set(kritaui_LIB_SRCS
tool/kis_color_picker_utils.cpp
widgets/kis_channelflags_widget.cpp
widgets/kis_cmb_composite.cc
widgets/kis_cmb_paintop.cpp
widgets/kis_paintop_list_widget.cpp
widgets/kis_cmb_idlist.cc
widgets/kis_color_space_selector.cc
widgets/kis_curve_widget.cpp
......@@ -160,10 +160,10 @@ set(kritaui_LIB_SRCS
widgets/kis_scratch_pad.cpp
widgets/kis_tree_view_popup.cc
widgets/kis_slider_spin_box.cpp
widgets/kis_categorized_list_model.cpp
widgets/kis_wdg_generator.cpp
widgets/kis_workspace_chooser.cpp
widgets/squeezedcombobox.cpp
widgets/kis_categorized_list_view.cpp
)
if(HAVE_OPENGL)
......
......@@ -6,33 +6,11 @@
<rect>
<x>0</x>
<y>0</y>
<width>500</width>
<width>613</width>
<height>335</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout" columnstretch="0,0,0,1">
<item row="0" column="0" rowspan="3">
<widget class="KisCmbPaintop" name="paintopList" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>150</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>150</width>
<height>16777215</height>
</size>
</property>
</widget>
</item>
<layout class="QGridLayout" name="gridLayout" columnstretch="0,0,0,0">
<item row="0" column="1" colspan="2">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
......@@ -109,7 +87,6 @@
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<zorder>paintopList</zorder>
</widget>
</item>
<item row="2" column="1">
......@@ -164,6 +141,28 @@
</item>
</layout>
</item>
<item row="0" column="0" rowspan="3">
<widget class="KisPaintOpListWidget" name="paintopList" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>150</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>150</width>
<height>16777215</height>
</size>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>
......@@ -174,9 +173,9 @@
<container>1</container>
</customwidget>
<customwidget>
<class>KisCmbPaintop</class>
<class>KisPaintOpListWidget</class>
<extends>QWidget</extends>
<header>kis_cmb_paintop.h</header>
<header>kis_paintop_list_widget.h</header>
<container>1</container>
</customwidget>
</customwidgets>
......
/*
* Copyright (c) 2009 Cyrille Berger <cberger@cberger.net>
* Copyright (c) 2011 Silvio Heinrich <plassy@web.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
......@@ -17,171 +18,63 @@
*/
#include "kis_categorized_item_delegate.h"
#include <kdeversion.h>
#include <kcategorydrawer.h>
#include <KCategorizedSortFilterProxyModel>
#include "kis_categorized_list_model.h"
#include <QPainter>
#include <QApplication>
#if KDE_IS_VERSION(4,5,0)
class KisCategoryDrawer : public KCategoryDrawerV3
#else
class KisCategoryDrawer : public KCategoryDrawer
#endif
{
public:
#if KDE_IS_VERSION(4,5,0)
KisCategoryDrawer(KCategorizedView *view = 0)
: KCategoryDrawerV3(view)
#else
KisCategoryDrawer()
: KCategoryDrawer()
#endif
{
}
virtual void drawCategory ( const QModelIndex& index, int /*sortRole*/, const QStyleOption& option, QPainter* painter ) const
{
painter->setRenderHint(QPainter::Antialiasing);
const QString category = index.model()->data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
QLinearGradient gradient(option.rect.topLeft(), option.rect.bottomLeft());
if (index.row() != 0) {
gradient.setColorAt(0, Qt::transparent);
}
gradient.setColorAt(0.3, option.palette.background().color());
gradient.setColorAt(0.8, option.palette.background().color());
gradient.setColorAt(1, Qt::transparent);
painter->fillRect(option.rect, gradient);
QFont font(QApplication::font());
font.setBold(true);
const QFontMetrics fontMetrics = QFontMetrics(font);
QRect textRect = option.rect.adjusted(5, 0, 0, 0);
painter->save();
painter->setFont(font);
painter->setPen(option.palette.text().color());
painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, category);
painter->restore();
}
};
struct KisCategorizedItemDelegate::Private {
QAbstractItemDelegate* fallback;
KisCategoryDrawer* categoryDrawer;
bool isFirstOfCategory(const QModelIndex& index);
};
bool KisCategorizedItemDelegate::Private::isFirstOfCategory(const QModelIndex& index)
{
if (index.row() == 0) return true;
QModelIndex idx = index.model()->index(index.row() - 1, index.column(), index.parent());
const QString category1 = index.model()->data(index, KCategorizedSortFilterProxyModel::CategorySortRole).toString();
const QString category2 = index.model()->data(idx, KCategorizedSortFilterProxyModel::CategorySortRole).toString();
return category1 != category2;
}
KisCategorizedItemDelegate::KisCategorizedItemDelegate(QAbstractItemDelegate* _fallback, QObject* parent)
: QAbstractItemDelegate(parent)
, d(new Private)
{
_fallback->setParent(this);
d->fallback = _fallback;
d->categoryDrawer = new KisCategoryDrawer;
}
KisCategorizedItemDelegate::~KisCategorizedItemDelegate()
{
delete d->fallback;
delete d->categoryDrawer;
delete d;
}
QWidget * KisCategorizedItemDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const
KisCategorizedItemDelegate::KisCategorizedItemDelegate(QAbstractListModel* model, bool indicateError):
m_model(model), m_indicateError(indicateError)
{
return d->fallback->createEditor(parent, option, index);
m_errorIcon = QIcon::fromTheme("dialog-warning");
}
bool KisCategorizedItemDelegate::editorEvent(QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index)
void KisCategorizedItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
return d->fallback->editorEvent(event, model, option, index);
}
void KisCategorizedItemDelegate::paint(QPainter * painter, const QStyleOptionViewItem & _option, const QModelIndex & index) const
{
// We will need to edit the option to make sure the header isn't drawned as selected
QStyleOptionViewItem* option = 0;
if (const QStyleOptionViewItemV4 *v4 = qstyleoption_cast<const QStyleOptionViewItemV4*>(&_option)) {
option = new QStyleOptionViewItemV4(*v4);
} else if (const QStyleOptionViewItemV3 *v3 = qstyleoption_cast<const QStyleOptionViewItemV3*>(&_option)) {
option = new QStyleOptionViewItemV3(*v3);
} else if (const QStyleOptionViewItemV2 *v2 = qstyleoption_cast<const QStyleOptionViewItemV2*>(&_option)) {
option = new QStyleOptionViewItemV2(*v2);
} else {
option = new QStyleOptionViewItem(_option);
}
Q_ASSERT(option);
// If it's a first category then we need to draw it
if (d->isFirstOfCategory(index)) {
// Prepare the rectangle for drawing the category
int h = d->categoryDrawer->categoryHeight(index, *option);
QRect rect = option->rect;
// Make sure the categroy isn't drawned as selected
option->state &= (~QStyle::State_Selected);
Q_ASSERT(!(option->state & QStyle::State_Selected));
option->state &= (~QStyle::State_HasFocus);
Q_ASSERT(!(option->state & QStyle::State_HasFocus));
option->state &= (~QStyle::State_MouseOver);
Q_ASSERT(!(option->state & QStyle::State_MouseOver));
option->rect.setHeight(h);
// draw the cateogry
d->categoryDrawer->drawCategory(index, 0, *option, painter);
// Prepare the rectangle for the item
option->rect = rect;
option->rect.setY(rect.y() + h);
option->rect.setHeight(rect.height() - h);
option->state = _option.state;
qint32 border = 4;
qint32 xpos = border + option.rect.x() + option.rect.height();
qint32 ypos = option.rect.y();
qint32 size = option.rect.height();
qint32 mx = size / 2;
qint32 my = ypos + size / 2;
QRect rect(xpos, ypos, option.rect.width()-xpos, option.rect.height());
painter->resetTransform();
if(!m_model->data(index, IsHeaderRole).toBool()) {
QStyleOptionViewItem sovi = option;
if(m_indicateError)
sovi.rect = rect;
QStyledItemDelegate::paint(painter, sovi, index);
if(m_indicateError && !(m_model->flags(index) & Qt::ItemIsEnabled))
m_errorIcon.paint(painter, 0, ypos, size, size, Qt::AlignCenter, QIcon::Normal, QIcon::On);
}
d->fallback->paint(painter, *option, index);
delete option;
}
void KisCategorizedItemDelegate::setEditorData(QWidget * editor, const QModelIndex & index) const
{
d->fallback->setEditorData(editor, index);
}
void KisCategorizedItemDelegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
{
d->fallback->setModelData(editor, model, index);
}
QSize KisCategorizedItemDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
{
QSize size = d->fallback->sizeHint(option, index);
// If is first of a category, then add the space needed to paint the category
if (d->isFirstOfCategory(index)) {
size.setHeight(d->categoryDrawer->categoryHeight(index, option) + size.height());
}
return size;
}
void KisCategorizedItemDelegate::updateEditorGeometry(QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
d->fallback->updateEditorGeometry(editor, option, index);
// If it's the first category, then the editor need to be moved
if (d->isFirstOfCategory(index)) {
int h = d->categoryDrawer->categoryHeight(index, option);
editor->move(editor->x(), editor->y() + h);
editor->resize(editor->width(), editor->height() - h);
else {
if(option.state & QStyle::State_MouseOver)
painter->fillRect(option.rect, Qt::lightGray);
else
painter->fillRect(option.rect, Qt::gray);
painter->drawText(rect, m_model->data(index).toString());
QPolygonF triangle;