MCPcopy Create free account
hub / github.com/FreeCAD/FreeCAD / runSourceFromMimeData

Method runSourceFromMimeData

src/Gui/PythonConsole.cpp:1227–1322  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1225}
1226
1227void PythonConsole::runSourceFromMimeData(const QString& source)
1228{
1229 // When inserting a big text block we must break it down into several command
1230 // blocks instead of processing the text block as a whole or each single line.
1231 // If we processed the complete block as a whole only the first valid Python
1232 // command would be executed and the rest would be ignored. However, if we
1233 // processed each line separately the interpreter might be confused that a block
1234 // is complete but it might be not. This is for instance, if a class or method
1235 // definition contains several empty lines which leads to error messages (almost
1236 // indentation errors) later on.
1237 QString text = source;
1238 if (text.isNull()) {
1239 return;
1240 }
1241
1242#if defined(Q_OS_LINUX)
1243 // Need to convert CRLF to LF
1244 text.replace(QLatin1String("\r\n"), QLatin1String("\n"));
1245#elif defined(Q_OS_WIN32)
1246 // Need to convert CRLF to LF
1247 text.replace(QLatin1String("\r\n"), QLatin1String("\n"));
1248#elif defined(Q_OS_MACOS)
1249 // need to convert CR to LF
1250 text.replace(QLatin1Char('\r'), QLatin1Char('\n'));
1251#endif
1252
1253 // separate the lines and get the last one
1254 QStringList lines = text.split(QLatin1Char('\n'));
1255 QString last = lines.back();
1256 lines.pop_back();
1257
1258 QTextCursor cursor = textCursor();
1259 QStringList buffer = d->interpreter->getBuffer();
1260 d->interpreter->clearBuffer();
1261
1262 int countNewlines = lines.count(), i = 0;
1263 for (QStringList::Iterator it = lines.begin(); it != lines.end(); ++it, ++i) {
1264 QString line = *it;
1265
1266 // insert the text to the current cursor position
1267 cursor.insertText(*it);
1268
1269 // for the very first line get the complete block
1270 // because it may differ from the inserted text
1271 if (i == 0) {
1272 // get the text from the current cursor position to the end, remove it
1273 // and add it to the last line
1274 cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
1275 QString select = cursor.selectedText();
1276 cursor.removeSelectedText();
1277 last = last + select;
1278 line = stripPromptFrom(cursor.block().text());
1279 }
1280
1281 // put statement to the history
1282 d->history.append(line);
1283
1284 buffer.append(line);

Callers

nothing calls this directly

Calls 15

stripPromptFromFunction · 0.85
clearBufferMethod · 0.80
insertTextMethod · 0.80
blockMethod · 0.80
compileCommandMethod · 0.80
setBufferMethod · 0.80
isNullMethod · 0.45
replaceMethod · 0.45
splitMethod · 0.45
backMethod · 0.45
getBufferMethod · 0.45
countMethod · 0.45

Tested by

no test coverage detected