[43d0bd]: src / plugins / circuit / ktlcircuitplugin.cpp Maximize Restore History

Download this file

ktlcircuitplugin.cpp    227 lines (188 with data), 7.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/***************************************************************************
* Copyright (C) 2008 Julian B채ume <julian@svg4all.de> *
* *
* 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. *
***************************************************************************/
#include "ktlcircuitplugin.h"
#include "circuitdocument.h"
#include "interfaces/component/componentmodel.h"
#include "interfaces/component/icomponentplugin.h"
#include "interfaces/component/icomponent.h"
#include "shell/core.h"
#include <interfaces/iuicontroller.h>
#include <interfaces/idocumentcontroller.h>
#include <KGenericFactory>
#include <KAboutData>
#include <KDebug>
#include <QTreeView>
#include <QHeaderView>
#include "componenteditorview.h"
#include "fakecomponentitemfactory.h"
#include <kactioncollection.h>
#include <kaction.h>
#include <qdir.h>
#include <qtemporaryfile.h>
using namespace KTechLab;
K_PLUGIN_FACTORY(KTLCircuitFactory, registerPlugin<KTLCircuitPlugin>(); )
K_EXPORT_PLUGIN(KTLCircuitFactory(KAboutData("ktlcircuit","ktlcircuit", ki18n("KTechLab Circuits"), "0.1", ki18n("Managing, viewing, manipulating circuit files"), KAboutData::License_LGPL)))
class KTechLab::KTLComponentViewFactory: public KDevelop::IToolViewFactory
{
public:
KTLComponentViewFactory( KTLCircuitPlugin *plugin )
: m_plugin(plugin)
{};
virtual QWidget * create( QWidget *parent )
{
QTreeView *componentView = new QTreeView( parent );
componentView->setModel( m_plugin->componentModel() );
componentView->header()->hide();
componentView->setAcceptDrops( false );
componentView->setDragEnabled( true );
return componentView;
};
virtual QString id() const
{
return "org.ktechlab.ComponentView";
};
virtual Qt::DockWidgetArea defaultPosition()
{
return Qt::LeftDockWidgetArea;
};
private:
KTLCircuitPlugin * m_plugin;
};
class KTechLab::KTLComponentEditorFactory: public KDevelop::IToolViewFactory
{
public:
KTLComponentEditorFactory( KTLCircuitPlugin *plugin )
: m_plugin(plugin)
{};
virtual QWidget * create( QWidget *parent )
{
ComponentEditorView *componentEditor = new ComponentEditorView( parent );
KDevelop::IDocumentController* docController = m_plugin->core()->documentController();
componentEditor->activated(docController->activeDocument());
QObject::connect( docController, SIGNAL( documentActivated( KDevelop::IDocument* ) ),
componentEditor, SLOT( activated( KDevelop::IDocument* ) ) );
return componentEditor;
};
virtual QString id() const
{
return "org.ktechlab.ComponentEditor";
};
virtual Qt::DockWidgetArea defaultPosition()
{
return Qt::RightDockWidgetArea;
};
private:
KTLCircuitPlugin * m_plugin;
};
class KTechLab::KTLCircuitDocumentFactory: public KDevelop::IDocumentFactory
{
public:
KTLCircuitDocumentFactory( KTLCircuitPlugin *plugin )
: m_plugin(plugin)
{};
virtual KDevelop::IDocument * create( const KUrl &url, KDevelop::ICore *core )
{
KDevelop::Core *ktlCore = dynamic_cast<KDevelop::Core*>(core);
if ( ktlCore )
return new CircuitDocument( url, ktlCore );
return 0;
}
private:
KTLCircuitPlugin * m_plugin;
};
KTLCircuitPlugin::KTLCircuitPlugin( QObject *parent, const QVariantList& args )
: KTechLab::IDocumentPlugin( KTLCircuitFactory::componentData(), parent ),
m_componentModel( new ComponentModel() )
{
init();
}
void KTLCircuitPlugin::init()
{
m_componentViewFactory = new KTLComponentViewFactory(this);
KDevelop::Core::self()->uiController()->addToolView( i18n("Components"), m_componentViewFactory );
m_componentEditorFactory = new KTLComponentEditorFactory(this);
KDevelop::Core::self()->uiController()->addToolView( i18n("Component Editor"), m_componentEditorFactory );
m_documentFactory = new KTLCircuitDocumentFactory(this);
KDevelop::Core::self()->documentController()->registerDocumentForMimetype( "application/x-circuit", m_documentFactory );
m_fakeComponentItemFactory = new FakeComponentItemFactory;
registerComponentFactory(m_fakeComponentItemFactory);
}
void KTLCircuitPlugin::createActionsForMainWindow(Sublime::MainWindow* window, QString& xmlFile, KActionCollection& actions)
{
xmlFile = "ktlcircuitui.rc";
KIconLoader *loader = KIconLoader::global();
KAction *newCircuit = actions.addAction("file_new_circuit");
newCircuit->setText( i18n("New Circuit" ) );
newCircuit->setIcon( loader->loadIcon( "ktechlab_circuit", KIconLoader::NoGroup, KIconLoader::SizeHuge ) );
connect(newCircuit, SIGNAL(triggered()), this, SLOT(newCircuitFile()));
}
KTLCircuitPlugin::~KTLCircuitPlugin()
{
// it crashes, when we delete this. I guess,
// it has been deleted before, somewhere else.
//delete m_componentViewFactory;
delete m_fakeComponentItemFactory;
delete m_documentFactory;
delete m_componentModel;
}
ComponentModel * KTLCircuitPlugin::componentModel()
{
return m_componentModel;
}
void KTLCircuitPlugin::registerComponentFactory( IComponentItemFactory *factory )
{
QList<ComponentMetaData> metaData = factory->allMetaData();
kDebug() << "registering" << metaData.size() << "components";
foreach (ComponentMetaData data, metaData) {
m_componentModel->insertComponentData( data, factory );
}
}
void KTLCircuitPlugin::deregisterComponentFactory(IComponentItemFactory* factory)
{
QList<ComponentMetaData> metaData = factory->allMetaData();
kDebug() << "deregistering" << metaData.size() << "components";
foreach (ComponentMetaData data, metaData) {
m_componentModel->removeComponentData( data, factory );
}
}
IComponentItemFactory* KTLCircuitPlugin::componentItemFactory(const QString& name, Theme* theme)
{
IComponentItemFactory* factory = m_componentModel->factoryForComponent(name);
if (!factory) {
kWarning() << "factory for data not found";
return m_componentModel->factoryForComponent("ec/unknown");
}
return factory;
}
void KTLCircuitPlugin::unload()
{
KDevelop::Core::self()->uiController()->removeToolView(m_componentViewFactory);
KDevelop::Core::self()->uiController()->removeToolView(m_componentEditorFactory);
}
void KTLCircuitPlugin::newCircuitFile()
{
qDebug() << "KTLCircuitPlugin::newCircuitFile() activated\n";
// get a temporary file name
QTemporaryFile tmpFile(QDir::tempPath().append(QDir::separator())
.append("ktlXXXXXX.circuit"));
tmpFile.setAutoRemove(false);
tmpFile.open();
qDebug() << "creating temporary file: " << tmpFile.fileName()
<< "pattern: " << tmpFile.fileTemplate();
// write a minial circuit document into the temporary file
tmpFile.write("<!DOCTYPE KTechlab>\n"
"<document type=\"circuit\" >"
"</document>"
);
tmpFile.close();
KUrl url(tmpFile.fileName());
core()->documentController()->openDocument(url, "ktlcircuit");
}
#include "ktlcircuitplugin.moc"
// vim: sw=4 sts=4 et tw=100