kis_colorspace_factory_registry.cc 6.63 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_alpha_colorspace.h"
Casper Boemann's avatar
Casper Boemann committed
32
#include "kis_lab_colorspace.h"
33 34


35
KisColorSpaceFactoryRegistry::KisColorSpaceFactoryRegistry(QStringList profileFilenames)
36
{
37 38 39 40 41 42 43 44 45 46 47
    // 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);

48 49
            profile->load();
            if (profile->valid()) {
50 51 52 53 54
                m_profileMap[profile->productName()] = profile;
            }
        }
    }

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

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

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

74
    KTrader::OfferList::ConstIterator iter;
75 76 77 78 79 80 81
    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 )
Laurent Montel's avatar
Laurent Montel committed
82
            kDebug(DBG_AREA_PLUGINS) << "found colorspace " << service->property("Name").toString() << "\n";
83
        else {
Laurent Montel's avatar
Laurent Montel committed
84
            kDebug(41006) << "found plugin " << service->property("Name").toString() << ", " << errCode << "\n";
85 86
            if( errCode == KParts::ComponentFactory::ErrNoLibrary)
            {
Laurent Montel's avatar
Laurent Montel committed
87
                kWarning(41006) << " Error loading plugin was : ErrNoLibrary " << KLibLoader::self()->lastErrorMessage() << endl;
88 89
            }
        }
90
    }
91 92
}

93
KisColorSpaceFactoryRegistry::KisColorSpaceFactoryRegistry()
94 95 96
{
}

97
KisColorSpaceFactoryRegistry::~KisColorSpaceFactoryRegistry()
98
{
99 100
}

101
KisProfile *  KisColorSpaceFactoryRegistry::getProfileByName(const QString & name)
102 103 104 105 106 107 108 109
{
    if (m_profileMap.find(name) == m_profileMap.end()) {
        return 0;
    }

    return m_profileMap[name];
}

Laurent Montel's avatar
qt3to4  
Laurent Montel committed
110
Q3ValueVector<KisProfile *>  KisColorSpaceFactoryRegistry::profilesFor(KisID id)
111 112 113 114
{
    return profilesFor(get(id));
}

Laurent Montel's avatar
qt3to4  
Laurent Montel committed
115
Q3ValueVector<KisProfile *>  KisColorSpaceFactoryRegistry::profilesFor(KisColorSpaceFactory * csf)
116
{
117

Laurent Montel's avatar
qt3to4  
Laurent Montel committed
118
    Q3ValueVector<KisProfile *>  profiles;
119

120
    QMap<QString, KisProfile * >::Iterator it;
121
    for (it = m_profileMap.begin(); it != m_profileMap.end(); ++it) {
122
        KisProfile *  profile = it.data();
123
        if (profile->colorSpaceSignature() == csf->colorSpaceSignature()) {
124 125
            profiles.push_back(profile);
        }
126
    }
127 128 129
    return profiles;
}

Casper Boemann's avatar
Casper Boemann committed
130 131
void KisColorSpaceFactoryRegistry::addProfile(KisProfile *p)
{
132
      if (p->valid()) {
Casper Boemann's avatar
Casper Boemann committed
133 134 135 136
          m_profileMap[p->productName()] = p;
      }
}

137 138
void KisColorSpaceFactoryRegistry::addPaintDeviceAction(KisColorSpace* cs,
        KisPaintDeviceAction* action) {
139
    m_paintDevActionMap[cs->id()].append(action);
140 141
}

Laurent Montel's avatar
qt3to4  
Laurent Montel committed
142
Q3ValueVector<KisPaintDeviceAction *>
143
KisColorSpaceFactoryRegistry::paintDeviceActionsFor(KisColorSpace* cs) {
144
    return m_paintDevActionMap[cs->id()];
145 146
}

147
KisColorSpace * KisColorSpaceFactoryRegistry::getColorSpace(const KisID & csID, const QString & pName)
148
{
149 150
    QString profileName = pName;

151
    if(profileName.isEmpty())
152 153 154 155 156
    {
        KisColorSpaceFactory *csf = get(csID);

        if(!csf)
            return 0;
157

158
        profileName = csf->defaultProfile();
159
    }
160

161
    QString name = csID.id() + "<comb>" + profileName;
162

163 164 165 166
    if (m_csMap.find(name) == m_csMap.end()) {
        KisColorSpaceFactory *csf = get(csID);
        if(!csf)
            return 0;
167

168
        KisProfile *p = getProfileByName(profileName);
169 170
        if(!p && profileName != "")
            return 0;
171
        KisColorSpace *cs = csf->createColorSpace(this, p);
172 173 174 175
        if(!cs)
            return 0;

        m_csMap[name] = cs;
176 177
    }

178 179 180 181
    if(m_csMap.contains(name))
        return m_csMap[name];
    else
        return 0;
182 183
}

184 185 186

KisColorSpace * KisColorSpaceFactoryRegistry::getColorSpace(const KisID & csID, const KisProfile * profile)
{
187 188 189 190 191 192 193 194 195 196 197
    if( profile )
    {
        KisColorSpace *cs = getColorSpace( csID, profile->productName());

        if(!cs)
        {
            // The profile was not stored and thus not the combination either
            KisColorSpaceFactory *csf = get(csID);
            if(!csf)
                return 0;

198
            cs = csf->createColorSpace(this, const_cast<KisProfile *>(profile));
199 200 201 202 203 204 205 206
            if(!cs )
                return 0;

            QString name = csID.id() + "<comb>" + profile->productName();
            m_csMap[name] = cs;
        }

        return cs;
207 208 209
    } else {
        return getColorSpace( csID, "");
    }
210
}
211

212 213
KisColorSpace * KisColorSpaceFactoryRegistry::getAlpha8()
{
214
   return m_alphaCs;
215
}
216 217 218 219 220 221 222

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

#include "kis_colorspace_factory_registry.moc"