[d9bbba]: src / plugins / ktlgui / ktlguiplugin.cpp Maximize Restore History

Download this file

ktlguiplugin.cpp    300 lines (241 with data), 10.5 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/***************************************************************************
* Copyright (C) 2011 Zoltan Padrah <zoltan_padrah@users.sourceforge.net>*
* *
* 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 "ktlguiplugin.h"
#include <KGenericFactory>
#include <KAboutData>
#include <stdio.h>
#include <sublime/mainwindow.h>
#include <KActionMenu>
#include <KStandardAction>
#include <KActionCollection>
#include "newfiledlg.h"
#include <interfaces/icore.h>
#include <interfaces/iuicontroller.h>
#include <KFileDialog>
#include <shell/core.h>
#include <KIO/NetAccess>
#include <KMessageBox>
#include <kplugininfo.h>
#include <interfaces/iprojectcontroller.h>
#include <interfaces/iplugincontroller.h>
#include <interfaces/idocumentcontroller.h>
#include <QTemporaryFile>
using namespace KTechLab;
K_PLUGIN_FACTORY(KTLGuiPluginFactory, registerPlugin<KTLGuiPlugin>(); )
K_EXPORT_PLUGIN(KTLGuiPluginFactory(
KAboutData("ktlgui","ktlgui",
ki18n("KTechLab GUI"),
"0.1",
ki18n("This plugin provides GUI to KTechLab"),
KAboutData::License_LGPL)))
KTLGuiPlugin::KTLGuiPlugin(QObject* parent, const QVariantList& /* args */)
: IPlugin( KTLGuiPluginFactory::componentData(), parent )
{
printf("creating gui plugin \n");
// create new file dialog
m_mainWindow = core()->uiController()->activeMainWindow()->window();
m_newFileDlg = new NewFileDlg(m_mainWindow);
// hook up the new file dialog
connect(m_newFileDlg,SIGNAL(signalFileNewAssembly()),
this, SLOT(slotFileNewAssembly()));
connect(m_newFileDlg,SIGNAL(signalFileNewC()),
this, SLOT(slotFileNewC()));
connect(m_newFileDlg,SIGNAL(signalFileNewCircuit()),
this, SLOT(slotFileNewCircuit()));
connect(m_newFileDlg,SIGNAL(signalFileNewFlowCode()),
this, SLOT(slotFileNewFlowCode()));
connect(m_newFileDlg,SIGNAL(signalFileNewMechanics()),
this, SLOT(slotFileNewMechanics()));
connect(m_newFileDlg,SIGNAL(signalFileNewMicrobe()),
this, SLOT(slotFileNewMicrobe()));
printf("gui plugin created\n");
}
KTLGuiPlugin::~KTLGuiPlugin()
{
printf("gui plugin destroyed\n");
}
void KTLGuiPlugin::createActionsForMainWindow(Sublime::MainWindow* window,
QString& xmlFile,
KActionCollection& actions)
{
xmlFile = "ktechlabui.rc" ;
KStandardAction::open( this, SLOT(slotFileOpen()), window->actionCollection() );
/*
KStandardAction::openRecent( this, SLOT(slotFileOpenRecent()), actionCollection() );
KStandardAction::save( this, SLOT(slotFileSave()), actionCollection() );
KStandardAction::saveAs( this, SLOT(slotFileSaveAs()), actionCollection() );
KStandardAction::close( this, SLOT(slotViewClose()), actionCollection() );
KStandardAction::print( this, SLOT(slotFilePrint()), actionCollection() );
KStandardAction::quit( this, SLOT(slotFileQuit()), actionCollection() );
KStandardAction::undo( this, SLOT(slotUndo()), actionCollection() );
KStandardAction::redo( this, SLOT(slotRedo()), actionCollection() );
KStandardAction::cut( this, SLOT(slotCut()), actionCollection() );
KStandardAction::copy( this, SLOT(slotCopy()), actionCollection() );
KStandardAction::paste( this, SLOT(slotPaste()), actionCollection() );
*/
KStandardAction::openNew(this, SLOT(slotFileNew()), window->actionCollection() );
KActionMenu *newActionMenu = new KActionMenu( KIcon("document-new"), i18n("&New"), this );
actionCollection()->addAction( KStandardAction::name(KStandardAction::New), newActionMenu );
connect( newActionMenu, SIGNAL(activated()), this, SLOT(slotFileNew()) );
KAction *action;
action = actionCollection()->addAction( QString("newfile_asm") );
action->setText( i18n("Assembly") );
action->setIcon( KIcon("") );
connect( action, SIGNAL(triggered()), this, SLOT(slotFileNewAssembly()) );
newActionMenu->addAction( action );
action = actionCollection()->addAction( QString("newfile_c") );
action->setText( i18n("C source") );
action->setIcon( KIcon("text-x-csrc") );
connect( action, SIGNAL(triggered()), this, SLOT(slotFileNewC()) );
newActionMenu->addAction( action );
action = actionCollection()->addAction( QString("ktechlab_circuit") );
action->setText( i18n("Circuit") );
action->setIcon( KIcon("ktechlab_circuit") );
connect( action, SIGNAL(triggered()), this, SLOT(slotFileNewCircuit()) );
newActionMenu->addAction( action );
action = actionCollection()->addAction( QString("ktechlab_flowcode") );
action->setText( i18n("FlowCode") );
action->setIcon( KIcon("ktechlab_flowcode") );
connect( action, SIGNAL(triggered()), this, SLOT(slotFileNewFlowCode()) );
newActionMenu->addAction( action );
action = actionCollection()->addAction( QString("ktechlab_mechanics") );
action->setText( i18n("Mechanics") );
action->setIcon( KIcon("") );
connect( action, SIGNAL(triggered()), this, SLOT(slotFileNewMechanics()) );
newActionMenu->addAction( action );
action = actionCollection()->addAction( QString("ktechlab_microbe") );
action->setText( i18n("Microbe") );
action->setIcon( KIcon("ktechlab_microbe") );
connect( action, SIGNAL(triggered()), this, SLOT(slotFileNewMicrobe()) );
newActionMenu->addAction( action );
KDevelop::IPlugin::createActionsForMainWindow(window, xmlFile, actions);
window->setupGUI();
// FIXME calculate size dynamicaly, based on window contents
window->resize(750, 470);
}
void KTLGuiPlugin::slotFileOpen()
{
printf("slotFileOpen activated\n");
KUrl file = KFileDialog::getOpenUrl( KUrl("kfiledialog:///<keyword>"), "*",
m_mainWindow );
if ( file.isEmpty() )
return;
if ( !file.isValid() )
return;
QString target;
// the below code is what you should normally do. in this
// example case, we want the url to our own. you probably
// want to use this code instead for your app
// download the contents
if ( !KIO::NetAccess::download( file, target, m_mainWindow ) )
{
// If the file could not be downloaded, for example does not
// exist on disk, NetAccess will tell us what error to use
KMessageBox::error(m_mainWindow, KIO::NetAccess::lastErrorString());
return;
}
// now url points always to local storage
KUrl url(target);
if ( url.url().endsWith( ".ktechlab", Qt::CaseInsensitive ) )
{
// FIXME: create an IProject (i.e. KTechlab::Project) to open the url,
// since this will prevent KTechLab from crashing...
// This is a ktechlab project; it has to be handled separetly from a
// normal file.
core()->projectController()->openProject( url );
return;
}
// addRecentFile( url );
// set our caption
// setCaption( url.url() );
//get interface for extension
QStringList constraints;
constraints << QString("'%1' in [X-KDevelop-SupportedMimeTypes]").arg(
"application/x-circuit" );
// load in the file (target is always local)
KDevelop::ICore *theCore = core();
Q_ASSERT( theCore != 0 );
KDevelop::IPluginController *pc = theCore->pluginController();
Q_ASSERT( pc != 0 );
QList<KPluginInfo> plugins = pc->queryExtensionPlugins( "KTLDocument",
constraints );
foreach (KPluginInfo p, plugins) {
qDebug() << p.name();
}
if(! url.url().endsWith( ".ktechlab", Qt::CaseInsensitive )){
qWarning() << "File extension not known, trying as circuit anyway!\n";
}
// try to open
theCore->documentController()->openDocument(url, "ktlcircuit");
// and remove the temp file
KIO::NetAccess::removeTempFile( target );
}
void KTLGuiPlugin::slotFileNew()
{
printf("slotFileNew activated\n");
m_newFileDlg->show();
}
void KTLGuiPlugin::slotFileNewAssembly()
{
printf("slotFileNewAssembly activated\n");
// get a temporary file name
QTemporaryFile tmpFile(QDir::tempPath().append(QDir::separator())
.append("ktlXXXXXX.asm"));
tmpFile.setAutoRemove(false);
tmpFile.open();
qDebug() << "creating temporary file: " << tmpFile.fileName()
<< "pattern: " << tmpFile.fileTemplate();
KUrl url(tmpFile.fileName());
core()->documentController()->openDocument(url);
}
void KTLGuiPlugin::slotFileNewC()
{
printf("slotFileNewC activated\n");
// get a temporary file name
QTemporaryFile tmpFile(QDir::tempPath().append(QDir::separator())
.append("ktlXXXXXX.c"));
tmpFile.setAutoRemove(false);
tmpFile.open();
qDebug() << "creating temporary file: " << tmpFile.fileName()
<< "pattern: " << tmpFile.fileTemplate();
KUrl url(tmpFile.fileName());
core()->documentController()->openDocument(url);
}
void KTLGuiPlugin::slotFileNewCircuit()
{
printf("slotFileNewCircuit 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");
}
void KTLGuiPlugin::slotFileNewFlowCode()
{
printf("slotFileNewFlowCode activated\n");
}
void KTLGuiPlugin::slotFileNewMechanics()
{
printf("slotFileNewMechanics activated\n");
}
void KTLGuiPlugin::slotFileNewMicrobe()
{
printf("slotFileNewMicrobe activated\n");
}
#include "ktlguiplugin.moc"