kis_colorspace_factory_registry.cc 5.78 KB
Newer Older
1
/*
2
 *  Copyright (c) 2003 Patrick Julien  <freak@codepimps.org>
3
 *  Copyright (c) 2004 Cyrille Berger <cberger@cberger.net>
4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 *  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
17
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
 */
19

20
#include "kdebug.h"
21 22 23 24
#include <kparts/plugin.h>
#include <kservice.h>
#include <ktrader.h>
#include <kparts/componentfactory.h>
25 26
#include <kmessagebox.h>
#include <klocale.h>
Boudewijn Rempt's avatar
Boudewijn Rempt committed
27
#include "kis_debug_areas.h"
28
#include "kis_colorspace.h"
29 30
#include "kis_profile.h"
#include "kis_colorspace_factory_registry.h"
31
#include "kis_xyz_colorspace.h"
32
#include "kis_alpha_colorspace.h"
Casper Boemann's avatar
Casper Boemann committed
33
#include "kis_lab_colorspace.h"
34 35


36
KisColorSpaceFactoryRegistry::KisColorSpaceFactoryRegistry(QStringList profileFilenames)
37
{
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    // Create the built-in colorspaces

    m_alphaCs = new KisAlphaColorSpace(this, 0);

    // Load the profiles
    if (!profileFilenames.empty()) {
        KisProfile * profile = 0;
        for ( QStringList::Iterator it = profileFilenames.begin(); it != profileFilenames.end(); ++it ) {
            profile = new KisProfile(*it);
            Q_CHECK_PTR(profile);

            profile -> load();
            if (profile -> valid()) {
                m_profileMap[profile->productName()] = profile;
            }
        }
    }

Casper Boemann's avatar
Casper Boemann committed
56 57
    KisProfile *labProfile = new KisProfile(cmsCreateLabProfile(NULL));
    addProfile(labProfile);
Casper Boemann's avatar
Casper Boemann committed
58 59 60 61 62 63
    add(new KisLabColorSpaceFactory());
/* XXX where to put this
    KisHistogramProducerFactoryRegistry::instance() -> add(
                new KisBasicHistogramProducerFactory<KisBasicU16HistogramProducer>
                (KisID("LABAHISTO", i18n("L*a*b Histogram")), new KisLabColorSpace(this, 0);) );
*/
Casper Boemann's avatar
Casper Boemann committed
64

65 66 67 68 69
    // Load all colorspace modules
    KTrader::OfferList offers = KTrader::self() -> query(QString::fromLatin1("Krita/ColorSpace"),
                                                         QString::fromLatin1("(Type == 'Service') and "
                                                                             "([X-KDE-Version] == 2)"));

70 71 72 73
    if (offers.empty()) {
        KMessageBox::sorry(0, i18n("Cannot start Krita: no colorspaces available."));
        abort();
    }
74

75
    KTrader::OfferList::ConstIterator iter;
76 77 78 79 80 81 82 83 84
    for(iter = offers.begin(); iter != offers.end(); ++iter)
    {
        KService::Ptr service = *iter;
        int errCode = 0;
        KParts::Plugin* plugin =
             KParts::ComponentFactory::createInstanceFromService<KParts::Plugin> ( service, this, 0, QStringList(), &errCode);
        if ( plugin )
            kdDebug(DBG_AREA_PLUGINS) << "found colorspace " << service -> property("Name").toString() << "\n";
    }
85 86
}

87
KisColorSpaceFactoryRegistry::KisColorSpaceFactoryRegistry()
88 89 90
{
}

91
KisColorSpaceFactoryRegistry::~KisColorSpaceFactoryRegistry()
92
{
93 94
}

95
KisProfile *  KisColorSpaceFactoryRegistry::getProfileByName(const QString & name)
96 97 98 99 100 101 102 103
{
    if (m_profileMap.find(name) == m_profileMap.end()) {
        return 0;
    }

    return m_profileMap[name];
}

104 105 106 107 108 109
QValueVector<KisProfile *>  KisColorSpaceFactoryRegistry::profilesFor(KisID id)
{
    return profilesFor(get(id));
}

QValueVector<KisProfile *>  KisColorSpaceFactoryRegistry::profilesFor(KisColorSpaceFactory * csf)
110
{
111

112
    QValueVector<KisProfile *>  profiles;
113

114
    QMap<QString, KisProfile * >::Iterator it;
115
    for (it = m_profileMap.begin(); it != m_profileMap.end(); ++it) {
116
        KisProfile *  profile = it.data();
117
        if (profile->colorSpaceSignature() == csf->colorSpaceSignature()) {
118 119
            profiles.push_back(profile);
        }
120
    }
121 122 123
    return profiles;
}

Casper Boemann's avatar
Casper Boemann committed
124 125 126 127 128 129 130
void KisColorSpaceFactoryRegistry::addProfile(KisProfile *p)
{
      if (p -> valid()) {
          m_profileMap[p->productName()] = p;
      }
}

131 132 133 134 135 136 137 138 139 140
void KisColorSpaceFactoryRegistry::addPaintDeviceAction(KisColorSpace* cs,
        KisPaintDeviceAction* action) {
    m_paintDevActionMap[cs -> id()].append(action);
}

QValueVector<KisPaintDeviceAction *>
KisColorSpaceFactoryRegistry::paintDeviceActionsFor(KisColorSpace* cs) {
    return m_paintDevActionMap[cs -> id()];
}

141
KisColorSpace * KisColorSpaceFactoryRegistry::getColorSpace(const KisID & csID, const QString & pName)
142
{
143 144 145 146 147 148 149 150
    QString profileName = pName;

    if(profileName == "")
    {
        KisColorSpaceFactory *csf = get(csID);

        if(!csf)
            return 0;
151

152
        profileName = csf->defaultProfile();
153
    }
154

155
    QString name = csID.id() + "<comb>" + profileName;
156

157 158 159 160
    if (m_csMap.find(name) == m_csMap.end()) {
        KisColorSpaceFactory *csf = get(csID);
        if(!csf)
            return 0;
161

162
        KisProfile *p = getProfileByName(profileName);
163

164
        KisColorSpace *cs = csf -> createColorSpace(this, p);
165 166 167 168
        if(!cs)
            return 0;

        m_csMap[name] = cs;
169 170
    }

171 172 173 174
    if(m_csMap.contains(name))
        return m_csMap[name];
    else
        return 0;
175 176
}

177 178 179

KisColorSpace * KisColorSpaceFactoryRegistry::getColorSpace(const KisID & csID, const KisProfile * profile)
{
180 181 182 183 184
    if( profile ) {
        return getColorSpace( csID, profile->productName());
    } else {
        return getColorSpace( csID, "");
    }
185
}
186

187 188
KisColorSpace * KisColorSpaceFactoryRegistry::getAlpha8()
{
189
   return m_alphaCs;
190
}
191 192 193 194 195 196 197

KisColorSpace * KisColorSpaceFactoryRegistry::getRGB8()
{
    return getColorSpace(KisID("RGBA", ""), "");
}

#include "kis_colorspace_factory_registry.moc"