chalk

kis_tool_registry.cpp
1/*
2 * Copyright (c) 2004 Boudewijn Rempt <boud@valdyas.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19#include "kdebug.h"
20#include <tdeaction.h>
21#include <tdeparts/plugin.h>
22#include <kservice.h>
23#include <ktrader.h>
24#include <tdeparts/componentfactory.h>
25
26#include "kis_generic_registry.h"
27#include "kis_types.h"
28#include "kis_tool_registry.h"
29#include "kis_tool.h"
30#include "kis_tool_factory.h"
31#include "kis_canvas_subject.h"
32#include "kis_id.h"
33#include "kis_debug_areas.h"
34
35KisToolRegistry *KisToolRegistry::m_singleton = 0;
36
37KisToolRegistry::KisToolRegistry()
38{
39 // Load all modules: color models, paintops, filters
40 TDETrader::OfferList offers = TDETrader::self()->query(TQString::fromLatin1("Chalk/Tool"),
41 TQString::fromLatin1("(Type == 'Service') and "
42 "([X-Chalk-Version] == 2)"));
43
44 TDETrader::OfferList::ConstIterator iter;
45
46 for(iter = offers.begin(); iter != offers.end(); ++iter)
47 {
48 KService::Ptr service = *iter;
49 int errCode = 0;
50 KParts::Plugin* plugin =
51 KParts::ComponentFactory::createInstanceFromService<KParts::Plugin> ( service, this, 0, TQStringList(), &errCode);
52 if ( plugin )
53 kdDebug(DBG_AREA_PLUGINS) << "found plugin " << service->property("Name").toString() << "\n";
54 else {
55 kdDebug(41006) << "found plugin " << service->property("Name").toString() << ", " << errCode << "\n";
56 if( errCode == KParts::ComponentFactory::ErrNoLibrary)
57 {
58 kdWarning(41006) << " Error loading plugin was : ErrNoLibrary " << KLibLoader::self()->lastErrorMessage() << endl;
59 }
60 }
61
62 }
63
64}
65
66KisToolRegistry::~KisToolRegistry()
67{
68}
69
70KisToolRegistry* KisToolRegistry::instance()
71{
72 if(KisToolRegistry::m_singleton == 0)
73 {
74 KisToolRegistry::m_singleton = new KisToolRegistry();
75 }
76 return KisToolRegistry::m_singleton;
77}
78
79
80
81vKisTool KisToolRegistry::createTools(TDEActionCollection * ac, KisCanvasSubject *subject) const
82{
83 Q_ASSERT(subject);
84
85 vKisTool tools;
86
87 KisIDList factories = listKeys();
88
89 for (KisIDList::Iterator it = factories.begin(); it != factories.end(); ++it )
90 {
91 KisToolFactorySP f = get(*it);
92
93 KisTool * tool = f->createTool(ac);
94 subject->attach(tool);
95 tools.push_back(tool);
96 }
97
98 subject->notifyObservers();
99
100 return tools;
101}
102
103KisTool * KisToolRegistry::createTool(TDEActionCollection * ac, KisCanvasSubject * subject, KisID & id) const
104{
105 KisToolFactorySP f = get(id);
106 KisTool * t = f->createTool(ac);
107 subject->attach(t);
108 return t;
109}
110
111#include "kis_tool_registry.moc"
KisCanvasSubject is part of the Observer pattern.
virtual void attach(KisCanvasObserver *observer)=0
From now on, the observer will be notified of changes in brush, foreground color, background color,...
virtual void notifyObservers()=0
Calling this method will notify all observers.
KisIDList listKeys() const
This function return a list of all the keys.
KisToolFactorySP get(const KisID &name) const
This function allow to get an object from its KisID.
Chalk has a large number of extensible resources.
Definition kis_id.h:57
A registry, similar to the tool and colormodel registry where new tool plugins can register themselve...