![]() |
TinkerCell Core 1.0
TinkerCell's Core library providing all basic functionalities
|
00001 /**************************************************************************** 00002 Copyright (c) 2008 Deepak Chandran 00003 Contact: Deepak Chandran (dchandran1@gmail.com) 00004 See COPYRIGHT.TXT 00005 00006 This file defines the class that is used as a general output area as well as 00007 a generic command prompt (e.g. by Python plugin) 00008 ****************************************************************************/ 00009 #include <QSettings> 00010 #include "NetworkHandle.h" 00011 #include "MainWindow.h" 00012 #include "ConsoleWindow.h" 00013 #include "GlobalSettings.h" 00014 00015 namespace Tinkercell 00016 { 00017 QString ConsoleWindow::Prompt(">"); 00018 00019 QColor ConsoleWindow::BackgroundColor = QColor("#000000"); 00020 QColor ConsoleWindow::PlainTextColor = QColor("#FEFFEC"); 00021 QColor ConsoleWindow::ErrorTextColor = QColor("#FF6F0F"); 00022 QColor ConsoleWindow::OutputTextColor = QColor("#33FF00"); 00023 QColor ConsoleWindow::TableTextColor = QColor("#FFFF00"); 00024 00025 void CommandTextEdit::setBackgroundColor(const QColor& c) 00026 { 00027 ConsoleWindow::BackgroundColor = c; 00028 setStyleSheet( tr("background : ") + ConsoleWindow::BackgroundColor.name()); 00029 update(); 00030 } 00031 00032 void CommandTextEdit::setPlainTextColor(const QColor& c) 00033 { 00034 ConsoleWindow::PlainTextColor = c; 00035 normalFormat.setForeground(c); 00036 update(); 00037 } 00038 00039 void CommandTextEdit::setOutputTextColor(const QColor& c) 00040 { 00041 messageFormat.setForeground( ConsoleWindow::OutputTextColor = c ); 00042 update(); 00043 } 00044 00045 void CommandTextEdit::setErrorTextColor(const QColor& c) 00046 { 00047 errorFormat.setForeground( ConsoleWindow::ErrorTextColor = c ); 00048 update(); 00049 } 00050 00051 void CommandTextEdit::setTableTextColor(const QColor& c) 00052 { 00053 tableHeaderFormat.setForeground( ConsoleWindow::TableTextColor = c ); 00054 update(); 00055 } 00056 00057 void CommandTextEdit::setCompleter(QCompleter *completer) 00058 { 00059 if (c) 00060 QObject::disconnect(c, 0, this, 0); 00061 00062 c = completer; 00063 00064 if (!c) 00065 return; 00066 00067 c->setWidget(this); 00068 c->setCompletionMode(QCompleter::PopupCompletion); 00069 c->setCaseSensitivity(Qt::CaseInsensitive); 00070 QObject::connect(c, SIGNAL(activated(const QString&)), 00071 this, SLOT(insertCompletion(const QString&))); 00072 } 00073 00074 QCompleter *CommandTextEdit::completer() const 00075 { 00076 return c; 00077 } 00078 00079 void CommandTextEdit::insertCompletion(const QString& completion) 00080 { 00081 if (c->widget() != this) 00082 return; 00083 QTextCursor tc = textCursor(); 00084 int extra = completion.length() - c->completionPrefix().length() - 1; 00085 tc.movePosition(QTextCursor::Left); 00086 tc.movePosition(QTextCursor::EndOfWord); 00087 tc.insertText(completion.right(extra)); 00088 setTextCursor(tc); 00089 } 00090 00091 QString CommandTextEdit::textUnderCursor() const 00092 { 00093 QTextCursor tc = textCursor(); 00094 tc.select(QTextCursor::WordUnderCursor); 00095 return tc.selectedText(); 00096 } 00097 00098 void CommandTextEdit::focusInEvent(QFocusEvent *e) 00099 { 00100 if (c) 00101 c->setWidget(this); 00102 QTextEdit::focusInEvent(e); 00103 } 00104 00105 00106 CommandTextEdit::CommandTextEdit(MainWindow * parent): QTextEdit(parent), c(0), mainWindow(parent) 00107 { 00108 setUndoRedoEnabled ( false ); 00109 00110 QSettings settings(GlobalSettings::ORGANIZATIONNAME, GlobalSettings::ORGANIZATIONNAME); 00111 00112 settings.beginGroup("MainWindow"); 00113 00114 ConsoleWindow::BackgroundColor = QColor(settings.value("ConsoleWindow::BackgroundColor", ConsoleWindow::BackgroundColor.name()).toString()); 00115 ConsoleWindow::PlainTextColor = QColor(settings.value("ConsoleWindow::PlainTextColor", ConsoleWindow::PlainTextColor.name()).toString()); 00116 ConsoleWindow::ErrorTextColor = QColor(settings.value("ConsoleWindow::ErrorTextColor",ConsoleWindow::ErrorTextColor.name()).toString()); 00117 ConsoleWindow::OutputTextColor = QColor(settings.value("ConsoleWindow::OutputTextColor",ConsoleWindow::OutputTextColor.name()).toString()); 00118 ConsoleWindow::TableTextColor = QColor(settings.value("ConsoleWindow::TableTextColor", ConsoleWindow::TableTextColor.name()).toString()); 00119 00120 settings.endGroup(); 00121 00122 setTextInteractionFlags(Qt::TextEditorInteraction); 00123 00124 setCursorWidth(2); 00125 QFont font = this->font(); 00126 font.setPointSize(12); 00127 setFont(font); 00128 00129 setPalette(ConsoleWindow::BackgroundColor); 00130 00131 QTextCursor cursor = textCursor(); 00132 00133 currentHistoryIndex = 0; 00134 frozen = false; 00135 00136 //errorFormat.setFontWeight(QFont::Bold); 00137 errorFormat.setForeground(ConsoleWindow::ErrorTextColor); 00138 00139 //messageFormat.setFontWeight(QFont::Bold); 00140 messageFormat.setForeground(ConsoleWindow::OutputTextColor); 00141 00142 tableHeaderFormat.setFontWeight(QFont::Bold); 00143 tableHeaderFormat.setForeground(ConsoleWindow::TableTextColor); 00144 00145 normalFormat.setFontWeight(QFont::Bold); 00146 normalFormat.setForeground(ConsoleWindow::PlainTextColor); 00147 00148 cursor.setCharFormat(normalFormat); 00149 cursor.insertText(ConsoleWindow::Prompt); 00150 alreadyInsertedPrompt = true; 00151 currentPosition = cursor.position(); 00152 } 00153 00154 void CommandTextEdit::wheelEvent ( QWheelEvent * wheelEvent ) 00155 { 00156 if (wheelEvent == 0) return; 00157 00158 if (wheelEvent->modifiers() == Qt::ControlModifier) 00159 { 00160 if (wheelEvent->delta() > 0) 00161 zoomIn(); 00162 else 00163 zoomOut(); 00164 } 00165 else 00166 { 00167 QTextEdit::wheelEvent(wheelEvent); 00168 } 00169 } 00170 00171 bool CommandTextEdit::isFrozen() 00172 { 00173 return frozen; 00174 } 00175 00176 void CommandTextEdit::error(const QString& s) 00177 { 00178 if (s.isEmpty()) return; 00179 00180 if (frozen) 00181 { 00182 errorsStack << s; 00183 return; 00184 } 00185 00186 _lastError = s; 00187 00188 QTextCursor cursor = textCursor(); 00189 cursor.setPosition(currentPosition); 00190 00191 cursor.setCharFormat(errorFormat); 00192 cursor.insertText(tr("Error: ") + s + tr("\n")); 00193 00194 cursor.setCharFormat(normalFormat); 00195 cursor.insertText(ConsoleWindow::Prompt); 00196 alreadyInsertedPrompt = true; 00197 00198 if (cursor.position() > currentPosition) 00199 currentPosition = cursor.position(); 00200 this->ensureCursorVisible(); 00201 } 00202 00203 void CommandTextEdit::message(const QString& s) 00204 { 00205 if (s.isEmpty()) return; 00206 00207 if (frozen) 00208 { 00209 messagesStack << s; 00210 return; 00211 } 00212 00213 _lastOutput = s; 00214 00215 QTextCursor cursor = textCursor(); 00216 QTextDocument * doc = cursor.document(); 00217 if (doc) 00218 cursor.setPosition(doc->characterCount()); 00219 00220 if (cursor.position() > currentPosition) 00221 currentPosition = cursor.position(); 00222 00223 cursor.setCharFormat(messageFormat); 00224 cursor.insertText(s + tr("\n")); 00225 00226 cursor.setCharFormat(normalFormat); 00227 cursor.insertText(ConsoleWindow::Prompt); 00228 alreadyInsertedPrompt = true; 00229 00230 if (cursor.position() > currentPosition) 00231 currentPosition = cursor.position(); 00232 this->ensureCursorVisible(); 00233 } 00234 00235 void CommandTextEdit::clearText() 00236 { 00237 clear(); 00238 00239 QTextCursor cursor = textCursor(); 00240 cursor.setCharFormat(normalFormat); 00241 cursor.insertText(ConsoleWindow::Prompt); 00242 alreadyInsertedPrompt = true; 00243 cursor.movePosition(QTextCursor::EndOfBlock); 00244 currentPosition = cursor.position(); 00245 00246 if (cursor.position() < currentPosition) 00247 cursor.setPosition(currentPosition); 00248 this->ensureCursorVisible(); 00249 } 00250 00251 void CommandTextEdit::freeze() 00252 { 00253 setFreeze(true); 00254 } 00255 00256 void CommandTextEdit::unfreeze() 00257 { 00258 setFreeze(false); 00259 } 00260 00261 void CommandTextEdit::setFreeze(bool frozen) 00262 { 00263 QTextCursor cursor = textCursor(); 00264 QTextDocument * doc = document(); 00265 if (doc) 00266 cursor.setPosition(doc->characterCount()-1); 00267 00268 if (!frozen) 00269 { 00270 if (!messagesStack.isEmpty()) 00271 { 00272 _lastOutput = messagesStack.last(); 00273 cursor.setCharFormat(messageFormat); 00274 cursor.insertText(messagesStack.join(tr("\n")) + tr("\n")); 00275 alreadyInsertedPrompt = false; 00276 messagesStack.clear(); 00277 } 00278 00279 if (!errorsStack.isEmpty()) 00280 { 00281 _lastError = errorsStack.last(); 00282 cursor.setCharFormat(errorFormat); 00283 cursor.insertText(tr("Error: ") + errorsStack.join(tr("\n")) + tr("\n")); 00284 alreadyInsertedPrompt = false; 00285 errorsStack.clear(); 00286 } 00287 } 00288 00289 if (this->frozen == frozen) 00290 { 00291 currentHistoryIndex = historyStack.size(); 00292 this->ensureCursorVisible(); 00293 } 00294 00295 currentPosition = doc->characterCount(); 00296 this->frozen = frozen; 00297 00298 if (!frozen) 00299 { 00300 if (!alreadyInsertedPrompt) 00301 { 00302 cursor.setCharFormat(normalFormat); 00303 cursor.insertText(ConsoleWindow::Prompt); 00304 alreadyInsertedPrompt = true; 00305 } 00306 } 00307 00308 if (cursor.position() > currentPosition) 00309 currentPosition = cursor.position(); 00310 00311 currentHistoryIndex = historyStack.size(); 00312 this->ensureCursorVisible(); 00313 } 00314 00315 void CommandTextEdit::eval(const QString& command) 00316 { 00317 QTextDocument * doc = document(); 00318 if (!doc || frozen || command.isEmpty()) return; 00319 00320 QTextCursor cursor = textCursor(); 00321 cursor.setCharFormat(normalFormat); 00322 00323 if (cursor.position() <= currentPosition) 00324 cursor.setPosition(currentPosition); 00325 00326 cursor.movePosition(QTextCursor::EndOfBlock); 00327 cursor.insertText(command + tr("\n")); 00328 alreadyInsertedPrompt = false; 00329 currentPosition = cursor.position(); 00330 if (historyStack.isEmpty() || command != historyStack.back()) 00331 { 00332 historyStack << command; 00333 currentHistoryIndex = historyStack.size(); 00334 } 00335 00336 if (command.trimmed().toLower() == tr("clear")) 00337 clearText(); 00338 else 00339 if (mainWindow && QFile::exists(command.trimmed())) 00340 mainWindow->open(command.trimmed()); 00341 else 00342 emit commandExecuted(command); 00343 00344 if (!alreadyInsertedPrompt) 00345 { 00346 cursor.setCharFormat(normalFormat); 00347 cursor.insertText(ConsoleWindow::Prompt); 00348 alreadyInsertedPrompt = true; 00349 } 00350 00351 if (cursor.position() > currentPosition) 00352 currentPosition = cursor.position(); 00353 currentHistoryIndex = historyStack.size(); 00354 this->ensureCursorVisible(); 00355 } 00356 00357 void CommandTextEdit::keyPressEvent ( QKeyEvent * event ) 00358 { 00359 QTextDocument * doc = document(); 00360 00361 if (!event || !doc) return; 00362 00363 currentPosition = doc->lastBlock().position() + ConsoleWindow::Prompt.size(); 00364 00365 if (c && c->popup()->isVisible()) 00366 { 00367 // The following keys are forwarded by the completer to the widget 00368 switch (event->key()) { 00369 case Qt::Key_Enter: 00370 case Qt::Key_Return: 00371 case Qt::Key_Escape: 00372 case Qt::Key_Tab: 00373 case Qt::Key_Backtab: 00374 event->ignore(); 00375 return; // let the completer do default behavior 00376 default: 00377 break; 00378 } 00379 } 00380 00381 int key = event->key(); 00382 QTextCursor cursor = textCursor(); 00383 00384 cursor.setCharFormat(normalFormat); 00385 00386 if (event->matches(QKeySequence::Copy)) 00387 { 00388 QTextEdit::copy(); 00389 return; 00390 } 00391 00392 if (event->matches(QKeySequence::SelectAll)) 00393 { 00394 QTextEdit::selectAll(); 00395 return; 00396 } 00397 00398 if (event->modifiers() == Qt::ControlModifier) 00399 { 00400 if (key == Qt::Key_Equal || key == Qt::Key_Plus) 00401 { 00402 zoomIn(); 00403 return; 00404 } 00405 if (key == Qt::Key_Minus) 00406 { 00407 zoomOut(); 00408 return; 00409 } 00410 } 00411 00412 if (key == Qt::Key_Return || key == Qt::Key_Enter) 00413 { 00414 if (frozen || cursor.position() < currentPosition) return; 00415 00416 QString command = cursor.block().text().remove(0,ConsoleWindow::Prompt.size()); 00417 cursor.movePosition(QTextCursor::EndOfBlock); 00418 cursor.insertText(tr("\n")); 00419 alreadyInsertedPrompt = false; 00420 currentPosition = cursor.position(); 00421 00422 if (!command.isEmpty()) 00423 { 00424 if (historyStack.isEmpty() || command != historyStack.back()) 00425 { 00426 historyStack << command; 00427 currentHistoryIndex = historyStack.size(); 00428 } 00429 00430 if (command.trimmed().toLower() == tr("clear")) 00431 clearText(); 00432 else 00433 if (mainWindow && QFile::exists(command.trimmed())) 00434 mainWindow->open(command.trimmed()); 00435 else 00436 if (!printValue(cursor,command)) 00437 emit commandExecuted(command); 00438 } 00439 00440 if (!alreadyInsertedPrompt) 00441 { 00442 cursor.setCharFormat(normalFormat); 00443 cursor.insertText(ConsoleWindow::Prompt); 00444 alreadyInsertedPrompt = true; 00445 cursor.movePosition(QTextCursor::EndOfBlock); 00446 if (cursor.position() > currentPosition) 00447 currentPosition = cursor.position(); 00448 } 00449 00450 currentHistoryIndex = historyStack.size(); 00451 this->ensureCursorVisible(); 00452 } 00453 else 00454 if (key == Qt::Key_Escape) 00455 { 00456 if (frozen) 00457 { 00458 emit commandInterrupted(); 00459 return; 00460 } 00461 cursor.setPosition(currentPosition,QTextCursor::KeepAnchor); 00462 cursor.removeSelectedText(); 00463 currentHistoryIndex = historyStack.size(); 00464 } 00465 else 00466 if ((document()->lastBlock() == cursor.block()) && (key == Qt::Key_Up || key == Qt::Key_Down) && !event->modifiers()) 00467 { 00468 if (frozen) return; 00469 00470 cursor.setPosition(currentPosition); 00471 00472 if (key == Qt::Key_Up) 00473 --currentHistoryIndex; 00474 00475 if (key == Qt::Key_Down) 00476 ++currentHistoryIndex; 00477 00478 if (currentHistoryIndex > historyStack.size()) currentHistoryIndex = historyStack.size(); 00479 00480 if (currentHistoryIndex < 0) currentHistoryIndex = 0; 00481 00482 if (historyStack.size() > 0) 00483 { 00484 cursor.movePosition(QTextCursor::EndOfBlock); 00485 cursor.setPosition(currentPosition,QTextCursor::KeepAnchor); 00486 cursor.removeSelectedText(); 00487 if (currentHistoryIndex < historyStack.size()) 00488 cursor.insertText(historyStack[currentHistoryIndex]); 00489 cursor.movePosition(QTextCursor::EndOfBlock); 00490 } 00491 } 00492 else 00493 if (key == Qt::Key_Tab && mainWindow && mainWindow->currentNetwork()) 00494 { 00495 QString text = cursor.block().text().remove(0,ConsoleWindow::Prompt.size()); 00496 QStringList keys = mainWindow->currentNetwork()->symbolsTable.uniqueHandlesWithDot.keys(); 00497 QStringList options; 00498 for (int i=0; i < keys.size(); ++i) 00499 if (keys[i].startsWith(text)) 00500 { 00501 //cursor.insertText(keys[i].right(keys[i].size() - text.size())); 00502 options << keys[i]; 00503 } 00504 if (options.isEmpty()) 00505 { 00506 keys = mainWindow->currentNetwork()->symbolsTable.uniqueDataWithDot.keys(); 00507 for (int i=0; i < keys.size(); ++i) 00508 if (keys[i].startsWith(text)) 00509 { 00510 //cursor.insertText(keys[i].right(keys[i].size() - text.size())); 00511 options << keys[i]; 00512 } 00513 } 00514 00515 if (!options.isEmpty()) 00516 { 00517 if (options.size() == 1) 00518 { 00519 cursor.insertText(options[0].right(options[0].size() - text.size())); 00520 alreadyInsertedPrompt = false; 00521 } 00522 else 00523 { 00524 cursor.setCharFormat(messageFormat); 00525 cursor.insertText(tr("\n") + options.join(tr("\n")) + tr("\n")); 00526 cursor.setCharFormat(normalFormat); 00527 cursor.insertText(ConsoleWindow::Prompt + text); 00528 alreadyInsertedPrompt = false; 00529 } 00530 } 00531 } 00532 else 00533 if (frozen && event->modifiers() == Qt::ControlModifier && (key == Qt::Key_Z)) 00534 { 00535 emit commandInterrupted(); 00536 } 00537 else 00538 { 00539 if (key == Qt::Key_Up || key == Qt::Key_Down || key == Qt::Key_Left || key == Qt::Key_Right 00540 || key == Qt::Key_PageUp || key == Qt::Key_PageDown || key == Qt::Key_End || key == Qt::Key_Home 00541 || !( frozen 00542 || cursor.position() < currentPosition 00543 || cursor.selectionStart() < currentPosition 00544 || (cursor.position() <= currentPosition && key == Qt::Key_Backspace))) 00545 { 00546 if (cursor.position() < currentPosition) 00547 { 00548 cursor.setPosition(currentPosition); 00549 this->ensureCursorVisible(); 00550 } 00551 QString completionPrefix = textUnderCursor(); 00552 00553 bool isShortcut = ((event->modifiers() & Qt::ControlModifier) && event->key() == Qt::Key_E); // CTRL+E 00554 00555 if (!c || !isShortcut) // dont process the shortcut when we have a completer 00556 QTextEdit::keyPressEvent(event); 00557 00558 if (key == Qt::Key_Home) 00559 cursor.movePosition(QTextCursor::Right,QTextCursor::MoveAnchor,ConsoleWindow::Prompt.size()); 00560 00561 bool ctrlOrShift = event->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier); 00562 if (!c || (ctrlOrShift && event->text().isEmpty())) 00563 { 00564 if (cursor.position() < currentPosition) 00565 cursor.setPosition(currentPosition); 00566 this->ensureCursorVisible(); 00567 return; 00568 } 00569 00570 //static QString eow("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-= "); // end of word 00571 bool hasModifier = false;//(event->modifiers() != Qt::NoModifier) && !ctrlOrShift; 00572 00573 00574 if (!isShortcut && (hasModifier || event->text().isEmpty() || completionPrefix.length() < 3)) 00575 { 00576 c->popup()->hide(); 00577 if (cursor.position() < currentPosition) 00578 cursor.setPosition(currentPosition); 00579 this->ensureCursorVisible(); 00580 return; 00581 } 00582 00583 if (completionPrefix != c->completionPrefix()) 00584 { 00585 c->setCompletionPrefix(completionPrefix); 00586 c->popup()->setCurrentIndex(c->completionModel()->index(0, 0)); 00587 } 00588 QRect cr = cursorRect(); 00589 cr.setWidth(c->popup()->sizeHintForColumn(0) 00590 + c->popup()->verticalScrollBar()->sizeHint().width()); 00591 c->complete(cr); 00592 } 00593 } 00594 if (cursor.position() < currentPosition) 00595 cursor.setPosition(currentPosition); 00596 this->ensureCursorVisible(); 00597 } 00598 00599 /*********************************** 00600 OUTPUT WINDOW 00601 ************************************/ 00602 00603 ConsoleWindow::ConsoleWindow(MainWindow * main) 00604 : Tool(tr("Console Window"),tr("Coding")), commandTextEdit(main), _interpreter(0) 00605 { 00606 setMainWindow(main); 00607 if (mainWindow) 00608 { 00609 setWindowTitle(name); 00610 setWindowIcon(QIcon(tr(":/images/cmd.png"))); 00611 mainWindow->addToolWindow(this,MainWindow::defaultConsoleWindowOption,Qt::RightDockWidgetArea); 00612 } 00613 00614 QHBoxLayout * layout = new QHBoxLayout; 00615 layout->addWidget(&commandTextEdit); 00616 layout->setContentsMargins(0,0,0,0); 00617 layout->setSpacing(0); 00618 setLayout(layout); 00619 00620 connect(&commandTextEdit,SIGNAL(commandExecuted(const QString&)),this,SIGNAL(commandExecuted(const QString&))); 00621 connect(&commandTextEdit,SIGNAL(commandInterrupted()),this,SIGNAL(commandInterrupted())); 00622 } 00623 00625 void ConsoleWindow::message(const QString& s) 00626 { 00627 commandTextEdit.message(s); 00628 00629 if (!s.isEmpty()) 00630 { 00631 if (parentWidget()) 00632 parentWidget()->show(); 00633 else 00634 show(); 00635 } 00636 } 00637 00638 void ConsoleWindow::error(const QString& s) 00639 { 00640 commandTextEdit.error(s); 00641 00642 if (!s.isEmpty()) 00643 { 00644 if (parentWidget()) 00645 parentWidget()->show(); 00646 else 00647 show(); 00648 } 00649 } 00650 00651 void ConsoleWindow::eval(const QString& s) 00652 { 00653 commandTextEdit.eval(s); 00654 00655 /*if (!s.isEmpty()) 00656 { 00657 if (parentWidget()) 00658 parentWidget()->show(); 00659 else 00660 show(); 00661 }*/ 00662 } 00663 00664 void ConsoleWindow::printTable(const DataTable<qreal>& table) 00665 { 00666 QString outputs; 00667 00668 QStringList colnames = table.columnNames(), rownames = table.rowNames(); 00669 00670 outputs += tr("\n"); 00671 for (int i=0; i < colnames.size(); ++i) 00672 { 00673 outputs += tr("\t") + colnames.at(i); 00674 } 00675 outputs += tr("\n"); 00676 for (int i=0; i < table.rows(); ++i) 00677 { 00678 outputs += rownames.at(i); 00679 for (int j=0; j < table.columns(); ++j) 00680 { 00681 outputs += tr("\t") + QString::number(table.at(i,j)); 00682 } 00683 outputs += tr("\n"); 00684 } 00685 00686 commandTextEdit.message(outputs); 00687 00688 if (!outputs.isEmpty()) 00689 { 00690 if (parentWidget()) 00691 parentWidget()->show(); 00692 else 00693 show(); 00694 } 00695 } 00696 00697 void ConsoleWindow::printTable(const DataTable<QString>& table) 00698 { 00699 QString outputs; 00700 00701 QStringList colnames = table.columnNames(), rownames = table.rowNames(); 00702 00703 outputs += tr("\n"); 00704 for (int i=0; i < colnames.size(); ++i) 00705 { 00706 outputs += tr("\t") + colnames.at(i); 00707 } 00708 outputs += tr("\n"); 00709 for (int i=0; i < table.rows(); ++i) 00710 { 00711 outputs += rownames.at(i); 00712 for (int j=0; j < table.columns(); ++j) 00713 { 00714 outputs += tr("\t") + (table.at(i,j)); 00715 } 00716 outputs += tr("\n"); 00717 } 00718 00719 commandTextEdit.message(outputs); 00720 00721 if (!outputs.isEmpty()) 00722 { 00723 if (parentWidget()) 00724 parentWidget()->show(); 00725 else 00726 show(); 00727 } 00728 } 00729 00730 void ConsoleWindow::freeze() 00731 { 00732 commandTextEdit.freeze(); 00733 } 00734 00735 void ConsoleWindow::unfreeze() 00736 { 00737 commandTextEdit.unfreeze(); 00738 } 00739 00740 void ConsoleWindow::clear() 00741 { 00742 commandTextEdit.clearText(); 00743 } 00744 00745 CommandTextEdit * ConsoleWindow::editor() 00746 { 00747 return &commandTextEdit; 00748 } 00749 00750 bool CommandTextEdit::printValue(QTextCursor& cursor, const QString& s) 00751 { 00752 if (s.isEmpty() || !mainWindow) return false; 00753 00754 NetworkHandle * network = mainWindow->currentNetwork(); 00755 00756 if (!network) return false; 00757 00758 QList<ItemHandle*> list = network->findItem(s); 00759 ItemHandle * h = 0; 00760 00761 if (!list.isEmpty()) h = list[0]; 00762 00763 if (h) 00764 { 00765 printHandleSummary(cursor,h); 00766 return true; 00767 } 00768 00769 QList< QPair<ItemHandle*,QString> > pairs = network->findData(s); 00770 00771 if (pairs.isEmpty() || !pairs[0].first || pairs[0].second.isEmpty()) return false; 00772 00773 h = pairs[0].first; 00774 QString id = pairs[0].second; 00775 00776 cursor.setCharFormat(messageFormat); 00777 if (h->hasTextData(id)) 00778 { 00779 QString s2 = s; 00780 s2.remove(h->fullName(".") + tr(".")); 00781 s2.remove(h->fullName("_") + tr("_")); 00782 if (h->textDataTable(id).rowNames().contains(s2)) 00783 { 00784 cursor.insertText(h->textData(id,s2) + tr("\n")); 00785 cursor.setCharFormat(normalFormat); 00786 cursor.insertText(ConsoleWindow::Prompt); 00787 alreadyInsertedPrompt = true; 00788 return true; 00789 } 00790 } 00791 00792 if (h->hasNumericalData(id)) 00793 { 00794 QString s2 = s; 00795 s2.remove(h->fullName(".") + tr(".")); 00796 s2.remove(h->fullName("_") + tr("_")); 00797 if (h->numericalDataTable(id).rowNames().contains(s2)) 00798 { 00799 cursor.insertText(QString::number(h->numericalData(id,s2)) + tr("\n")); 00800 cursor.setCharFormat(normalFormat); 00801 cursor.insertText(ConsoleWindow::Prompt); 00802 alreadyInsertedPrompt = true; 00803 return true; 00804 } 00805 } 00806 return false; 00807 } 00808 00809 void CommandTextEdit::printHandleSummary(QTextCursor& cursor, ItemHandle * h) 00810 { 00811 if (!h) return; 00812 00813 QString s; 00814 00815 if (h->family()) 00816 { 00817 cursor.setCharFormat(tableHeaderFormat); 00818 cursor.insertText(tr("family: ")); 00819 cursor.setCharFormat(messageFormat); 00820 cursor.insertText(h->family()->name() + tr("\n")); 00821 } 00822 00823 if (h->parent) 00824 { 00825 cursor.setCharFormat(tableHeaderFormat); 00826 cursor.insertText(tr("parent: ")); 00827 cursor.setCharFormat(messageFormat); 00828 cursor.insertText(h->parent->fullName() + tr("\n")); 00829 } 00830 00831 QList<QString> keys = h->numericalDataNames(); 00832 for (int i=0; i < keys.size(); ++i) 00833 { 00834 NumericalDataTable & dat = h->numericalDataTable( keys[i] ); 00835 if (dat.rows() > 0 && dat.columns() > 0) 00836 { 00837 int maxsz = 0; 00838 for (int j=0; j < dat.rows(); ++j) 00839 if (dat.rowName(j).size() > maxsz) 00840 maxsz = dat.rowName(j).size(); 00841 s = QString("").leftJustified(maxsz+1); 00842 00843 cursor.setCharFormat(tableHeaderFormat); 00844 cursor.insertText(keys[i] + tr(":\n")); 00845 00846 cursor.setCharFormat(messageFormat); 00847 for (int k=0; k < dat.columns(); ++k) 00848 s += tr(" ") + dat.columnName(k); 00849 s += tr("\n"); 00850 for (int j=0; j < dat.rows(); ++j) 00851 { 00852 s += dat.rowName(j) + tr(": "); 00853 00854 for (int k=0; k < dat.columns(); ++k) 00855 s += QString::number(dat.at(j,k)) + tr(" "); 00856 s += tr("\n"); 00857 } 00858 cursor.insertText(s); 00859 } 00860 } 00861 00862 keys = h->textDataNames(); 00863 for (int i=0; i < keys.size(); ++i) 00864 { 00865 TextDataTable & dat = h->textDataTable( keys[i] ); 00866 if (dat.rows() > 0 && dat.columns() > 0) 00867 { 00868 int maxsz = 0; 00869 for (int j=0; j < dat.rows(); ++j) 00870 if (dat.rowName(j).size() > maxsz) 00871 maxsz = dat.rowName(j).size(); 00872 s = QString("").leftJustified(maxsz+1); 00873 00874 cursor.setCharFormat(tableHeaderFormat); 00875 cursor.insertText(keys[i] + tr(":\n")); 00876 00877 cursor.setCharFormat(messageFormat); 00878 for (int k=0; k < dat.columns(); ++k) 00879 s += tr(" ") + dat.columnName(k); 00880 s += tr("\n"); 00881 for (int j=0; j < dat.rows(); ++j) 00882 { 00883 s += dat.rowName(j) + tr(": "); 00884 00885 for (int k=0; k < dat.columns(); ++k) 00886 s += (dat.at(j,k)) + tr(" "); 00887 s += tr("\n"); 00888 } 00889 cursor.insertText(s); 00890 } 00891 } 00892 00893 cursor.setCharFormat(normalFormat); 00894 cursor.insertText(ConsoleWindow::Prompt); 00895 alreadyInsertedPrompt = true; 00896 } 00897 00898 InterpreterThread * ConsoleWindow::interpreter() const 00899 { 00900 return _interpreter; 00901 } 00902 00903 void ConsoleWindow::setInterpreter(InterpreterThread * newInterpreter) 00904 { 00905 if (_interpreter) 00906 { 00907 disconnect(this,SIGNAL(commandExecuted(const QString&)),_interpreter,SLOT(exec(const QString&))); 00908 disconnect(this,SIGNAL(commandInterrupted()),_interpreter,SLOT(terminate())); 00909 disconnect(_interpreter,SIGNAL(started()),this->editor(),SLOT(freeze())); 00910 disconnect(_interpreter,SIGNAL(finished()),this->editor(),SLOT(unfreeze())); 00911 disconnect(_interpreter,SIGNAL(terminated()),this->editor(),SLOT(unfreeze())); 00912 } 00913 00914 _interpreter = newInterpreter; 00915 00916 if (_interpreter) 00917 { 00918 connect(this,SIGNAL(commandExecuted(const QString&)),_interpreter,SLOT(exec(const QString&))); 00919 connect(this,SIGNAL(commandInterrupted()),_interpreter,SLOT(terminate())); 00920 connect(_interpreter,SIGNAL(started()),this->editor(),SLOT(freeze())); 00921 connect(_interpreter,SIGNAL(finished()),this->editor(),SLOT(unfreeze())); 00922 connect(_interpreter,SIGNAL(terminated()),this->editor(),SLOT(unfreeze())); 00923 } 00924 } 00925 00926 QString CommandTextEdit::lastError() const 00927 { 00928 return _lastError; 00929 } 00930 00931 QString CommandTextEdit::lastMessage() const 00932 { 00933 return _lastOutput; 00934 } 00935 00936 QString ConsoleWindow::lastError() const 00937 { 00938 return commandTextEdit._lastError; 00939 } 00940 00941 QString ConsoleWindow::lastMessage() const 00942 { 00943 return commandTextEdit._lastOutput; 00944 } 00945 } 00946