[r503]: trunk / src / flowparts / flowcodedocument.cpp Maximize Restore History

Download this file

flowcodedocument.cpp    278 lines (216 with data), 8.0 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
/***************************************************************************
* Copyright (C) 2003-2005 by David Saxton *
* david@bluehaze.org *
* *
* 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 "canvasmanipulator.h"
#include "documentiface.h"
#include "drawpart.h"
#include "flowcode.h"
#include "flowcodedocument.h"
#include "flowcodeview.h"
#include "flowpart.h"
#include "itemdocumentdata.h"
#include "languagemanager.h"
#include "ktechlab.h"
#include "microinfo.h"
#include "microlibrary.h"
#include "outputmethoddlg.h"
#include "picitem.h"
#include "programmerdlg.h"
#include <kdebug.h>
#include <klocale.h>
#include <kmessagebox.h>
FlowCodeDocument::FlowCodeDocument(const QString &caption, const char *name)
: FlowICNDocument(caption, name) {
m_pDocumentIface = new FlowCodeDocumentIface(this);
m_type = Document::dt_flowcode;
m_microInfo = 0;
m_microSettings = 0;
m_picItem = 0;
m_pLastTextOutputTarget = 0;
m_cmManager->addManipulatorInfo(CMSelect::manipulatorInfo());
m_cmManager->addManipulatorInfo(CMDraw::manipulatorInfo());
m_cmManager->addManipulatorInfo(CMRightClick::manipulatorInfo());
m_cmManager->addManipulatorInfo(CMRepeatedItemAdd::manipulatorInfo());
m_cmManager->addManipulatorInfo(CMItemResize::manipulatorInfo());
m_cmManager->addManipulatorInfo(CMItemDrag::manipulatorInfo());
m_fileExtensionInfo = QString("*.flowcode|FlowCode (*.flowcode)\n*|%1").arg(i18n("All Files"));
requestStateSave();
}
FlowCodeDocument::~FlowCodeDocument() {
m_bDeleted = true;
if (m_picItem) m_picItem->removeItem();
delete m_microSettings;
delete m_pDocumentIface;
}
View *FlowCodeDocument::createView(ViewContainer *viewContainer, uint viewAreaId, const char *name) {
View* view = new FlowCodeView(this, viewContainer, viewAreaId, name);
handleNewView(view);
return view;
}
void FlowCodeDocument::setPicType(const QString &id) {
if (m_microSettings && m_microSettings->microInfo() && m_microSettings->microInfo()->id() == id)
return;
MicroInfo *microInfo = MicroLibrary::self()->microInfoWithID(id);
if (!microInfo) {
kdWarning() << "FlowCodeDocument::setPicType: Could not set the pic type to PIC \"" << id << "\"\n";
return;
}
m_microInfo = microInfo;
if (m_microSettings) {
//TODO write the pic settings to somewhere temporary and then restore them
delete m_microSettings;
}
m_microSettings = new MicroSettings(m_microInfo);
connect(m_microSettings, SIGNAL(pinMappingsChanged()), this, SIGNAL(pinMappingsChanged()));
//TODO restore pic settings from temporary location if appropriate
delete m_picItem;
m_picItem = new PicItem(this, true, "picItem", m_microSettings);
m_picItem->show();
emit picTypeChanged();
}
bool FlowCodeDocument::isValidItem(const QString &itemId) {
return itemId.startsWith("flow/") || itemId.startsWith("dp/");
}
bool FlowCodeDocument::isValidItem(Item *item) {
if (!dynamic_cast<FlowPart*>(item) && !dynamic_cast<DrawPart*>(item))
return false;
if (!item->id().startsWith("START") && !item->id().startsWith("PPEND"))
return true;
const ItemMap::iterator ciEnd = m_itemList.end();
if (item->id().startsWith("START")) {
int count = 0;
for (ItemMap::iterator it = m_itemList.begin(); it != ciEnd; ++it) {
if (it->second->id().startsWith("START"))
count++;
}
if (count > 1)
return false;
} else if (item->id().startsWith("PPEND")) {
int count = 0;
for (ItemMap::iterator it = m_itemList.begin(); it != ciEnd; ++it) {
if (it->second->id().startsWith("PPEND"))
count++;
}
if (count > 1)
return false;
}
return true;
}
void FlowCodeDocument::setLastTextOutputTarget(TextDocument * target) {
m_pLastTextOutputTarget = target;
}
void FlowCodeDocument::slotConvertTo(int target) {
switch ((ConvertToTarget)target) {
case FlowCodeDocument::MicrobeOutput:
convertToMicrobe();
break;
case FlowCodeDocument::AssemblyOutput:
convertToAssembly();
break;
case FlowCodeDocument::HexOutput:
convertToHex();
break;
case FlowCodeDocument::PICOutput:
convertToPIC();
break;
}
}
void FlowCodeDocument::convertToMicrobe() {
OutputMethodDlg *dlg = new OutputMethodDlg(i18n("Microbe Code Output"), url(), false, activeView());
dlg->setOutputExtension(".microbe");
dlg->setFilter(QString("*.microbe|Microbe (*.microbe)\n*|%1").arg(i18n("All Files")));
dlg->exec();
if (!dlg->isAccepted()) {
delete dlg;
return;
}
ProcessOptions o(dlg->info());
o.setTextOutputTarget(m_pLastTextOutputTarget, this, SLOT(setLastTextOutputTarget(TextDocument*)));
o.p_flowCodeDocument = this;
o.setProcessPath(ProcessOptions::ProcessPath::FlowCode_Microbe);
LanguageManager::self()->compile(o);
delete dlg;
}
void FlowCodeDocument::convertToAssembly() {
OutputMethodDlg *dlg = new OutputMethodDlg(i18n("Assembly Code Output"), url(), false, activeView());
dlg->setOutputExtension(".asm");
dlg->setFilter(QString("*.asm *.src *.inc|%1 (*.asm, *.src, *.inc)\n*|%2").arg(i18n("Assembly Code")).arg(i18n("All Files")));
dlg->exec();
if (!dlg->isAccepted()) {
delete dlg;
return;
}
ProcessOptions o(dlg->info());
o.setTextOutputTarget(m_pLastTextOutputTarget, this, SLOT(setLastTextOutputTarget(TextDocument*)));
o.p_flowCodeDocument = this;
o.setProcessPath(ProcessOptions::ProcessPath::FlowCode_AssemblyAbsolute);
LanguageManager::self()->compile(o);
delete dlg;
}
void FlowCodeDocument::convertToHex() {
OutputMethodDlg *dlg = new OutputMethodDlg(i18n("Hex Code Output"), url(), false, KTechlab::self());
dlg->setOutputExtension(".hex");
dlg->setFilter(QString("*.hex|Hex (*.hex)\n*|%1").arg(i18n("All Files")));
dlg->exec();
if (!dlg->isAccepted()) {
delete dlg;
return;
}
ProcessOptions o(dlg->info());
o.setTextOutputTarget(m_pLastTextOutputTarget, this, SLOT(setLastTextOutputTarget(TextDocument*)));
o.p_flowCodeDocument = this;
o.setProcessPath(ProcessOptions::ProcessPath::FlowCode_Program);
LanguageManager::self()->compile(o);
delete dlg;
}
void FlowCodeDocument::convertToPIC() {
ProgrammerDlg * dlg = new ProgrammerDlg(microSettings()->microInfo()->id(), (QWidget*)KTechlab::self(), "Programmer Dlg");
dlg->exec();
if (!dlg->isAccepted()) {
dlg->deleteLater();
return;
}
ProcessOptions o;
dlg->initOptions(& o);
o.p_flowCodeDocument = this;
o.setProcessPath(ProcessOptions::ProcessPath::FlowCode_PIC);
LanguageManager::self()->compile(o);
dlg->deleteLater();
}
void FlowCodeDocument::varNameChanged(const QString &newValue, const QString &oldValue) {
if (m_bDeleted) return;
// Decrease the old variable count
// If none are left after, remove it from microsettings
StringIntMap::iterator it = m_varNames.find(oldValue);
if (it != m_varNames.end()) {
--(it.data());
if (it.data() <= 0) {
VariableInfo *info = microSettings()->variableInfo(it.key());
if (info && !info->permanent) microSettings()->deleteVariable(oldValue);
m_varNames.erase(it);
}
}
// Add the new variable to a count, tell microsettings about it if it is new
if (!newValue.isEmpty()) {
it = m_varNames.find(newValue);
if (it != m_varNames.end()) {
++it.data();
} else {
m_varNames[newValue] = 1;
microSettings()->setVariable(newValue, QVariant(), false);
}
}
// Tell all FlowParts to update their variable lists
const ItemMap::iterator end = m_itemList.end();
for (ItemMap::iterator it = m_itemList.begin(); it != end; ++it) {
if (FlowPart *flowPart = dynamic_cast<FlowPart*>(it->second))
flowPart->updateVarNames();
}
}
#include "flowcodedocument.moc"