My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
octave_window.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  copyright : (C) 2010 by Michael Margraf
3  email : michael.margraf@alumni.tu-berlin.de
4  ***************************************************************************/
5 
6 #include "octave_window.h"
7 #include "main.h"
8 
9 #include <qsize.h>
10 #include <qvbox.h>
11 #include <qcolor.h>
12 #include <qaccel.h>
13 #include <qlayout.h>
14 #include <qlineedit.h>
15 #include <qtextedit.h>
16 #include <qdockwindow.h>
17 
18 extern QDir QucsWorkDir; // current project path
19 
20 OctaveWindow::OctaveWindow(QDockWindow *parent_): QWidget(parent_, 0)
21 {
22  QFont font;
23  font = QFont("Courier New");
24  font.setPointSize(QucsSettings.font.pointSize()-1);
25  font.setStyleHint(QFont::Courier);
26  font.setFixedPitch(true);
27  setFont(font);
28 
29  vBox = new QVBoxLayout(this);
30 
31  output = new QTextEdit(this);
32  output->setReadOnly(true);
33  output->setUndoRedoEnabled(false);
34  output->setTextFormat(Qt::LogText);
35  output->setMaxLogLines(2000);
36  output->setWordWrap(QTextEdit::NoWrap);
37  output->setPaletteBackgroundColor(QucsSettings.BGColor);
38  vBox->addWidget(output, 10);
39 
40  input = new QLineEdit(this);
41  connect(input, SIGNAL(returnPressed()), SLOT(slotSendCommand()));
42  vBox->addWidget(input);
43 
44  setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
45  parent_->setWidget(this);
46  parent_->setResizeEnabled(true);
47  parent_->setHorizontallyStretchable(true);
48 
49  histIterator = cmdHistory.end();
50 }
51 
52 // -----------------------------------------------------------------
54 {
55  if(octProcess.isRunning())
56  octProcess.kill();
57 }
58 
59 // -----------------------------------------------------------------
61 {
62  QSize Size;
63  int w=0, h=0;
64 
65  Size = output->sizeHint();
66  w = Size.width();
67  h = Size.height() + input->sizeHint().height();
68  return QSize(w, h);
69 }
70 
71 // ------------------------------------------------------------------------
73 {
74  if(octProcess.isRunning())
75  return true;
76 
77  QStringList CommandLine;
78  CommandLine << "octave" << "--no-history" << "-i" << "-f" << "-p"
79  << QDir::convertSeparators(QucsSettings.OctaveDir);
80  octProcess.setArguments(CommandLine);
81 
82  disconnect(&octProcess, 0, 0, 0);
83  connect(&octProcess, SIGNAL(readyReadStderr()), SLOT(slotDisplayErr()));
84  connect(&octProcess, SIGNAL(readyReadStdout()), SLOT(slotDisplayMsg()));
85  connect(&octProcess, SIGNAL(processExited()), SLOT(slotOctaveEnded()));
86 
87  output->clear();
88 
89  if(!octProcess.start()) {
90  output->setText(tr("ERROR: Cannot start Octave!"));
91  return false;
92  }
93 
95  return true;
96 }
97 
98 // ------------------------------------------------------------------------
100 {
101  sendCommand("cd \"" + QucsWorkDir.absPath() + "\"");
102 }
103 
104 // ------------------------------------------------------------------------
105 void OctaveWindow::sendCommand(const QString& cmd)
106 {
107  int par = output->paragraphs() - 1;
108  int idx = output->paragraphLength(par);
109  output->insertAt(cmd + "\n", par, idx);
110  octProcess.writeToStdin(cmd + "\n");
111  output->scrollToBottom();
112 }
113 
114 // ------------------------------------------------------------------------
115 void OctaveWindow::runOctaveScript(const QString& name)
116 {
117  QFileInfo info(name);
118  sendCommand(info.baseName(true));
119 }
120 
121 // ------------------------------------------------------------------------
122 void OctaveWindow::slotSendCommand()
123 {
124  sendCommand(input->text());
125  if(!input->text().stripWhiteSpace().isEmpty())
126  cmdHistory.append(input->text());
127  histIterator = cmdHistory.end();
128  input->clear();
129 }
130 
131 // ------------------------------------------------------------------------
132 void OctaveWindow::keyPressEvent(QKeyEvent *event)
133 {
134  if(event->key() == Qt::Key_Up) {
135  if(histIterator == cmdHistory.begin())
136  return;
137  histIterator--;
138  input->setText(*histIterator);
139  return;
140  }
141 
142  if(event->key() == Qt::Key_Down) {
143  if(histIterator == cmdHistory.end())
144  return;
145  histIterator++;
146  input->setText(*histIterator);
147  return;
148  }
149 }
150 
151 // ------------------------------------------------------------------------
152 // Is called when the process sends an output to stdout.
153 void OctaveWindow::slotDisplayMsg()
154 {
155  int par = output->paragraphs() - 1;
156  int idx = output->paragraphLength(par);
157  output->insertAt(QString(octProcess.readStdout()), par, idx);
158  output->scrollToBottom();
159 }
160 
161 // ------------------------------------------------------------------------
162 // Is called when the process sends an output to stderr.
163 void OctaveWindow::slotDisplayErr()
164 {
165  if(!isVisible())
166  ((QDockWindow*)parent())->show(); // always show an error
167 
168  int par = output->paragraphs() - 1;
169  int idx = output->paragraphLength(par);
170  output->insertAt(QString(octProcess.readStderr()), par, idx);
171  output->scrollToBottom();
172 }
173 
174 // ------------------------------------------------------------------------
175 // Is called when the simulation process terminates.
176 void OctaveWindow::slotOctaveEnded()
177 {
178  output->clear();
179 }