Commit 9d07d4e0 authored by Casper Boemann's avatar Casper Boemann

port krita to use KoResource rather than KisResource

same class though just moved and renamed


svn path=/trunk/koffice/; revision=659240
parent 422cac8b
......@@ -60,6 +60,7 @@ if (SHOULD_BUILD_KRITA)
${KOFFICEUI_INCLUDES}
${LCMS_INCLUDE_DIR}
${PIGMENT_INCLUDES}
${KORESOURCES_INCLUDES}
# 'Export' this for use by filters
CACHE INTERNAL ""
......
......@@ -60,11 +60,9 @@ set(kritaimage_LIB_SRCS
kis_painter.cc
kis_paintop.cc
kis_paintop_registry.cc
kis_palette.cc
kis_pattern.cc
kis_random_accessor.cpp
kis_random_sub_accessor.cpp
kis_resource.cc
kis_rotate_visitor.cc
kis_selected_transaction.cc
kis_selection.cc
......@@ -105,7 +103,7 @@ kde4_automoc(${kritaimage_LIB_SRCS})
kde4_add_library(kritaimage SHARED ${kritaimage_LIB_SRCS})
target_link_libraries(kritaimage ${KDE4_KDECORE_LIBS} pigment kofficecore kopainter ${KDE4_THREADWEAVER_LIBRARIES} )
target_link_libraries(kritaimage ${KDE4_KDECORE_LIBS} pigment kofficecore kopainter koresources ${KDE4_THREADWEAVER_LIBRARIES} )
set_target_properties(kritaimage PROPERTIES VERSION ${GENERIC_KOFFICE_LIB_VERSION} SOVERSION ${GENERIC_KOFFICE_LIB_SOVERSION} )
install(TARGETS kritaimage DESTINATION ${LIB_INSTALL_DIR})
......
......@@ -27,7 +27,7 @@
#include "kis_types.h"
#include "kis_resource.h"
#include "KoResource.h"
#include "kis_paint_information.h"
#include "kis_qimage_mask.h"
......@@ -50,8 +50,8 @@ enum enumBrushType {
AIRBRUSH
};
class KRITAIMAGE_EXPORT KisBrush : public KisResource {
typedef KisResource super;
class KRITAIMAGE_EXPORT KisBrush : public KoResource {
typedef KoResource super;
Q_OBJECT
public:
......
......@@ -29,10 +29,12 @@
#include <kio/job.h>
#include "kis_resource.h"
#include "kis_global.h"
#include <KoResource.h>
#include "KoColor.h"
#include "kis_global.h"
#include "krita_export.h"
class QImage;
enum {
......@@ -222,8 +224,8 @@ class KRITAIMAGE_EXPORT KisGradientSegment {
KoColor m_endColor;
};
class KRITAIMAGE_EXPORT KisGradient : public KisResource {
typedef KisResource super;
class KRITAIMAGE_EXPORT KisGradient : public KoResource {
typedef KoResource super;
Q_OBJECT
public:
......
/*
* Copyright (c) 2005 Boudewijn Rempt <boud@valdyas.org>
*
* 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 <QImage>
#include <QPoint>
#include <QVector>
#include <QFile>
#include <QTextStream>
#include <kdebug.h>
#include <klocale.h>
#include "kis_debug_areas.h"
#include "kis_palette.h"
#include "kis_iterators_pixel.h"
namespace {
enum enumPaletteType {
FORMAT_UNKNOWN,
FORMAT_GPL, // Gimp palette
FORMAT_PAL, // RIFF palette
FORMAT_ACT // Photoshop binary color palette
};
}
KisPalette::KisPalette(const QImage * img, qint32 nColors, const QString & name)
: super(QString("")),
m_name(name)
{
Q_ASSERT(nColors > 0);
Q_ASSERT(!img->isNull());
// XXX: Implement
m_columns = 0; // Set the default value that the GIMP uses...
}
KisPalette::KisPalette(const KisPaintDeviceSP device, qint32 nColors, const QString & name)
: super(QString("")),
m_name(name)
{
Q_ASSERT(nColors > 0);
Q_ASSERT(!device.isNull());
// XXX: Implement
m_columns = 0; // Set the default value that the GIMP uses...
}
KisPalette::KisPalette(const KisGradient * gradient, qint32 nColors, const QString & name)
: super(QString("")),
m_name(name)
{
Q_ASSERT(nColors > 0);
Q_ASSERT(gradient != 0);
double dx, cur_x;
qint32 i;
dx = 1.0 / (nColors - 1);
KisPaletteEntry e;
KoColor c;
for (i = 0, cur_x = 0; i < nColors; i++, cur_x += dx) {
gradient->colorAt(c,cur_x);
e.color = c.toQColor();
e.name = "Untitled";
add(e);
}
m_columns = 0; // Set the default value that the GIMP uses...
}
KisPalette::KisPalette(const QString& filename)
: super(filename)
{
// Implemented in super class
m_columns = 0; // Set the default value that the GIMP uses...
}
KisPalette::KisPalette()
: super("")
{
m_columns = 0; // Set the default value that the GIMP uses...
}
/// Create an copied palette
KisPalette::KisPalette(const KisPalette& rhs)
: super("")
{
setFilename(rhs.filename());
m_ownData = false;
m_img = rhs.m_img;
m_name = rhs.m_name;
m_comment = rhs.m_comment;
m_columns = rhs.m_columns;
m_colors = rhs.m_colors;
setValid(true);
}
KisPalette::~KisPalette()
{
}
bool KisPalette::load()
{
QFile file(filename());
file.open(QIODevice::ReadOnly);
m_data = file.readAll();
file.close();
return init();
}
bool KisPalette::save()
{
QFile file(filename());
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
return false;
}
QTextStream stream(&file);
// Header: Magic\nName: <name>\nColumns: <no idea what this means, but default = 0>
// In any case, we don't use Columns...
stream << "GIMP Palette\nName: " << name() << "\nColumns: " << m_columns << "\n#\n";
for (int i = 0; i < m_colors.size(); i++) {
const KisPaletteEntry& entry = m_colors.at(i);
QColor c = entry.color;
stream << c.red() << " " << c.green() << " " << c.blue() << "\t";
if (entry.name.isEmpty())
stream << "Untitled\n";
else
stream << entry.name << "\n";
}
file.close();
return true;
}
QImage KisPalette::img()
{
return m_img;
}
qint32 KisPalette::nColors()
{
return m_colors.count();
}
bool KisPalette::init()
{
enumPaletteType format = FORMAT_UNKNOWN;
QString s = QString::fromUtf8(m_data.data(), m_data.count());
if (s.isEmpty() || s.isNull() || s.length() < 50) {
kWarning(DBG_AREA_FILE) << "Illegal Gimp palette file: " << filename() << "\n";
return false;
}
if (s.startsWith("RIFF") || s.startsWith("PAL data"))
{
format = FORMAT_PAL;
}
else if (s.startsWith("GIMP Palette"))
{
// XXX: No checks for wrong input yet!
quint32 index = 0;
QStringList lines = s.split("\n", QString::SkipEmptyParts);
if (lines.size() < 3) {
return false;
}
QString entry, channel, columns;
QStringList c;
qint32 r, g, b;
QColor color;
KisPaletteEntry e;
format = FORMAT_GPL;
// Read name
if (!lines[1].startsWith("Name: ") || !lines[0].startsWith("GIMP") )
{
kWarning(DBG_AREA_FILE) << "Illegal Gimp palette file: " << filename() << "\n";
return false;
}
setName(i18n(lines[1].mid(strlen("Name: ")).trimmed().toAscii()));
index = 2;
// Read columns
if (lines[index].startsWith("Columns: ")) {
columns = lines[index].mid(strlen("Columns: ")).trimmed();;
m_columns = columns.toInt();
index = 3;
}
for (qint32 i = index; i < lines.size(); i++) {
if (lines[i].startsWith("#")) {
m_comment += lines[i].mid(1).trimmed() + ' ';
}
else if (!lines[i].isEmpty())
{
QStringList a = lines[i].replace(QChar('\t'), " ").split(" ");
if (a.count() < 3)
{
break;
}
r = a[0].toInt();
a.pop_front();
g = a[0].toInt();
a.pop_front();
b = a[0].toInt();
a.pop_front();
if (r < 0 || r > 255 ||
g < 0 || g > 255 ||
b < 0 || b > 255)
{
break;
}
color = QColor(r, g, b);
e.color = color;
QString name = a.join(" ");
e.name = name.isEmpty() ? i18n("Untitled") : name;
add(e);
}
}
setValid(true);
return true;
}
else if (s.length() == 768) {
kWarning(DBG_AREA_FILE) << "Photoshop format palette file. Not implemented yet\n";
format = FORMAT_ACT;
}
return false;
}
void KisPalette::add(const KisPaletteEntry & c)
{
m_colors.push_back(c);
}
void KisPalette::remove(const KisPaletteEntry & c)
{
QVector<KisPaletteEntry>::iterator it = m_colors.begin();
QVector<KisPaletteEntry>::iterator end = m_colors.end();
while (it != end) {
if ((*it) == c) {
m_colors.erase(it);
return;
}
++it;
}
}
KisPaletteEntry KisPalette::getColor(quint32 index)
{
return m_colors[index];
}
#include "kis_palette.moc"
/*
* Copyright (c) 2005 Boudewijn Rempt <boud@valdyas.org>
*
* 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_
#define KIS_PALETTE_
#include <QImage>
#include <QColor>
#include <QVector>
#include <QPixmap>
#include <kio/job.h>
#include <kpalette.h>
#include "kis_types.h"
#include "kis_resource.h"
#include "kis_global.h"
#include "kis_gradient.h"
#include "kis_qimage_mask.h"
class QPoint;
class QPixmap;
class KisPaintDevice;
struct KisPaletteEntry {
QColor color;
QString name;
bool operator==(const KisPaletteEntry& rhs) const {
return color == rhs.color && name == rhs.name;
}
};
/**
* Open Gimp, Photoshop or RIFF palette files. This is a straight port
* from the Gimp.
*/
class KRITAIMAGE_EXPORT KisPalette : public KisResource {
typedef KisResource super;
Q_OBJECT
public:
/**
* Create a palette from the colors in an image
*/
KisPalette(const QImage * img, qint32 nColors, const QString & name);
/**
* Create a palette from the colors in a paint device
*/
KisPalette(const KisPaintDeviceSP device, qint32 nColors, const QString & name);
/**
* Create a palette from the colors in a gradient
*/
KisPalette(const KisGradient * gradient, qint32 nColors, const QString & name);
/**
* Load a palette from a file. This can be a Gimp
* palette, a RIFF palette or a Photoshop palette.
*/
KisPalette(const QString& filename);
/// Create an empty palette
KisPalette();
/// Explicit copy constructor (KisResource copy constructor is private)
KisPalette(const KisPalette& rhs);
virtual ~KisPalette();
virtual bool load();
virtual bool save();
virtual QImage img();
public:
void add(const KisPaletteEntry &);
void remove(const KisPaletteEntry &);
KisPaletteEntry getColor(quint32 index);
qint32 nColors();
private:
bool init();
private:
QByteArray m_data;
bool m_ownData;
QImage m_img;
QString m_name;
QString m_comment;
qint32 m_columns;
QVector<KisPaletteEntry> m_colors;
};
#endif // KIS_PALETTE_
......@@ -24,7 +24,7 @@
#include <kio/job.h>
#include "kis_debug_areas.h"
#include "kis_resource.h"
#include "KoResource.h"
#include <krita_export.h>
#include "kis_types.h"
......@@ -33,8 +33,8 @@ class QImage;
class KoColorSpace;
class KisPaintDevice;
class KRITAIMAGE_EXPORT KisPattern : public KisResource {
typedef KisResource super;
class KRITAIMAGE_EXPORT KisPattern : public KoResource {
typedef KoResource super;
Q_OBJECT
public:
......
/*
* Copyright (c) 2003 Patrick Julien <freak@codepimps.org>
*
* 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_resource.h"
#include "kis_global.h"
KisResource::KisResource(const QString& filename)
{
m_filename = filename;
m_valid = false;
}
KisResource::~KisResource()
{
}
QString KisResource::filename() const
{
return m_filename;
}
void KisResource::setFilename(const QString& filename)
{
m_filename = filename;
}
QString KisResource::name() const
{
return m_name;
}
void KisResource::setName(const QString& name)
{
m_name = name;
}
bool KisResource::valid() const
{
return m_valid;
}
void KisResource::setValid(bool valid)
{
m_valid = valid;
}
#include "kis_resource.moc"
/*
* Copyright (c) 2003 Patrick Julien <freak@codepimps.org>
*
* 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_RESOURCE_H_
#define KIS_RESOURCE_H_
#include <QImage>
#include <QObject>
#include <QString>
#include <krita_export.h>
/**
* The KisResource class provides a representation of Krita image resources. This
* includes, but not limited to, brushes and patterns.
*
* This replaces the KisKrayon facility that used to be present in Krayon.
*/
class KRITAIMAGE_EXPORT KisResource : public QObject {
typedef QObject super;
Q_OBJECT
public:
/**
* Creates a new KisResource object using @p filename. No file is opened
* in the constructor, you have to call load.
*
* @param filename the file name to save and load from.
*/
KisResource(const QString& filename);
virtual ~KisResource();
public:
/**
* Load this resource.
*/
virtual bool load() = 0;
/**
* Save this resource asynchronously. The signal saveComplete is emitted when
* the resource has been saved.
*/
virtual bool save() = 0;
/**
* Returns a QImage representing this resource. This image could be null.
*/
virtual QImage img() = 0;
public:
QString filename() const;
void setFilename(const QString& filename);
QString name() const;
void setName(const QString& name);
bool valid() const;
void setValid(bool valid);
private:
KisResource(const KisResource&);
KisResource& operator=(const KisResource&);
private:
QString m_name;
QString m_filename;
bool m_valid;
};
#endif // KIS_RESOURCE_H_
......@@ -43,7 +43,7 @@
#include "kis_iterators_pixel.h"
#include "KoColor.h"
#include "kis_resourceserver.h"
#include "kis_palette.h"
#include "KoColorSet.h"
namespace {
// The location of the sample all visible layers in the combobox
......@@ -169,11 +169,11 @@ void KisToolColorPicker::mousePressEvent(KoPointerEvent *event)
if (m_addPalette) {
// Convert to RGB to add to palette, we ought to have our own format :(
KisPaletteEntry ent;
KoColorSetEntry ent;
ent.color = m_pickedColor.toQColor();
// We don't ask for a name, too intrusive here
KisPalette* palette = m_palettes.at(m_optionsWidget->cmbPalette->currentIndex());
KoColorSet* palette = m_palettes.at(m_optionsWidget->cmbPalette->currentIndex());
palette->add(ent);
if (!palette->save()) {
......@@ -243,10 +243,10 @@ QWidget* KisToolColorPicker::createOptionWidget()
return m_optionsWidget;
}
QList<KisResource*> palettes = srv->resources();
QList<KoResource*> palettes = srv->resources();
foreach (KisResource *resource, palettes) {
KisPalette* palette = dynamic_cast<KisPalette*>(resource);
foreach (KoResource *resource, palettes) {
KoColorSet* palette = dynamic_cast<KoColorSet*>(resource);
if (palette) {
m_optionsWidget->cmbPalette->addItem(palette->name());
m_palettes.append(palette);
......@@ -283,8 +283,8 @@ void KisToolColorPicker::slotChangeRadius(int value) {
m_radius = value;