Qucs-GUI  0.0.18
 All Data Structures 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 #include <QtGui>
6 #include "octave_window.h"
7 #include "main.h"
8 
9 #include <QSize>
10 #include <QColor>
11 #include <QKeyEvent>
12 #include <QWidget>
13 #include <QVBoxLayout>
14 
15 
16 #ifdef __MINGW32__
17 #define executableSuffix ".exe"
18 #else
19 #define executableSuffix ""
20 #endif
21 
22 
23 OctaveWindow::OctaveWindow(QDockWidget *parent_): QWidget()
24 {
25  QFont font;
26  font = QFont("Courier New");
27  font.setPointSize(QucsSettings.font.pointSize()-1);
28  font.setStyleHint(QFont::Courier);
29  font.setFixedPitch(true);
30  setFont(font);
31 
32  QWidget *all = new QWidget();
33  QVBoxLayout *allLayout = new QVBoxLayout();
34 
35  output = new QTextEdit(this);
36  output->setReadOnly(true);
37  output->setUndoRedoEnabled(false);
38  output->setTextFormat(Qt::LogText);
39  output->setLineWrapMode(QTextEdit::NoWrap);
40  output->setPaletteBackgroundColor(QucsSettings.BGColor);
41  allLayout->addWidget(output);
42 
43  input = new QLineEdit(this);
44  connect(input, SIGNAL(returnPressed()), SLOT(slotSendCommand()));
45  allLayout->addWidget(input);
46  all->setLayout(allLayout);
47 
48  setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
49  parent_->setWidget(all);
50 
51  //parent_->setResizeEnabled(true);
52  //parent_->setHorizontallyStretchable(true);
53  histPosition = 0;
54 
55  input->installEventFilter(this);
56 }
57 
58 // -----------------------------------------------------------------
60 {
61  if(octProcess.state()==QProcess::Running)
62  octProcess.kill();
63 }
64 
65 // -----------------------------------------------------------------
67 {
68  QSize Size;
69  int w=0, h=0;
70 
71  Size = output->sizeHint();
72  w = Size.width();
73  h = Size.height() + input->sizeHint().height();
74  return QSize(w, h);
75 }
76 
77 // ------------------------------------------------------------------------
79 {
80  if(octProcess.state()==QProcess::Running)
81  return true;
82 
83  QString OctavePath=QucsSettings.OctaveBinDir.canonicalPath();
84 
85 
86  QString Program;
87  QStringList Arguments;
88 
89  OctavePath = QDir::toNativeSeparators(OctavePath+"/"+"octave"+QString(executableSuffix));
90 
91  QFileInfo progOctave(OctavePath);
92 
93  if (! progOctave.exists()) {
94  qDebug() << "Octave not found: " << OctavePath;
95  QMessageBox::critical(0, QObject::tr("Error"),
96  QObject::tr("Octave not found in: %1\n\n"
97  "Set the Octave location on the application settings.").arg(OctavePath));
98  return false;
99  }
100 
101  Program = OctavePath;
102  Arguments << "--no-history" << "-i" << "-f" << "-p"
103  << QDir::toNativeSeparators(QucsSettings.OctaveDir); // m-files location
104 
105  disconnect(&octProcess, 0, 0, 0);
106  connect(&octProcess, SIGNAL(readyReadStandardError()), SLOT(slotDisplayErr()));
107  connect(&octProcess, SIGNAL(readyReadStandardOutput()), SLOT(slotDisplayMsg()));
108  connect(&octProcess, SIGNAL(finished(int)), SLOT(slotOctaveEnded(int)));
109 #ifdef __MINGW32__
110  QString sep(";"); // path separator
111 #else
112  QString sep(":");
113 #endif
114 
115  // append process PATH, othewise Octave does not find gnuplot
116  QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
117  env.insert("PATH", env.value("PATH") + sep + QucsSettings.BinDir );
118  octProcess.setProcessEnvironment(env);
119  output->clear();
120 
121  qDebug() << "Command :" << Program << Arguments.join(" ");
122  octProcess.start(Program, Arguments);
123 
124  if(octProcess.state()!=QProcess::Running&&
125  octProcess.state()!=QProcess::Starting) {
126  output->setText(tr("ERROR: Cannot start Octave!"));
127  return false;
128  }
129 
130  adjustDirectory();
131  return true;
132 }
133 
134 // ------------------------------------------------------------------------
136 {
137  sendCommand("cd \"" + QucsSettings.QucsWorkDir.absPath() + "\"");
138 }
139 
140 // ------------------------------------------------------------------------
141 void OctaveWindow::sendCommand(const QString& cmd)
142 {
143  //int par = output->paragraphs() - 1;
144  //int idx = output->paragraphLength(par);
145  output->setTextColor(QColor(Qt::blue));
146  output->append(cmd);
147  QString cmdstr = cmd + "\n";
148  //output->insertAt(cmdstr, par, idx);
149  //output->scrollToBottom();
150  octProcess.write(cmdstr);
151 }
152 
153 // ------------------------------------------------------------------------
154 void OctaveWindow::runOctaveScript(const QString& name)
155 {
156  QFileInfo info(name);
157  sendCommand(info.baseName(true));
158 }
159 
160 // ------------------------------------------------------------------------
162 {
163  sendCommand(input->text());
164  if(!input->text().stripWhiteSpace().isEmpty())
165  cmdHistory.append(input->text());
166  //histIterator = cmdHistory.end();
167  histPosition++;
168  input->clear();
169  qDebug() << cmdHistory << histPosition;
170 }
171 
172 
173 bool OctaveWindow::eventFilter(QObject *obj, QEvent *event) {
174  if (event->type() == QEvent::KeyPress) {
175  QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
176  if (keyEvent->key() == Qt::Key_PageUp) {
177  //if(histIterator == cmdHistory.begin())
178  if(histPosition == 0)
179  return false;
180  //histIterator--;
181  histPosition--;
182  input->setText(cmdHistory.at(histPosition));//*histIterator);
183  return true;
184  }
185  else if(keyEvent->key() == Qt::Key_PageDown) {
186  //if(histIterator == cmdHistory.end())
187  if(histPosition == cmdHistory.length()-1)
188  return false;
189  //histIterator++;
190  histPosition++;
191  input->setText(cmdHistory.at(histPosition));//*histIterator);
192  return true;
193  }
194  }
195  return false;
196 }
197 
198 // ------------------------------------------------------------------------
199 // Is called when the process sends an output to stdout.
201 {
202  //int par = output->paragraphs() - 1;
203  //int idx = output->paragraphLength(par);
204  //output->insertAt(QString(octProcess.readAllStandardOutput()), par, idx);
205  //output->scrollToBottom();
206  output->setTextColor(QColor(Qt::black));
207  output->append(octProcess.readAllStandardOutput());
208 }
209 
210 // ------------------------------------------------------------------------
211 // Is called when the process sends an output to stderr.
213 {
214  //if(!isVisible())
215  // ((Q3DockWindow*)parent())->show(); // always show an error
216 
217  //int par = output->paragraphs() - 1;
218  //int idx = output->paragraphLength(par);
219  //output->insertAt(QString(octProcess.readAllStandardError()), par, idx);
220  //output->scrollToBottom();
221  output->setTextColor(QColor(Qt::red));
222  output->append(octProcess.readAllStandardError());
223 }
224 
225 // ------------------------------------------------------------------------
226 // Is called when the simulation process terminates.
228 {
229  qDebug() << "Octave ended status" << status;
230  output->clear();
231 }
void slotOctaveEnded(int status)
QString OctaveDir
Definition: main.h:60
QDir QucsWorkDir
Definition: main.h:65
bool eventFilter(QObject *obj, QEvent *event)
QFont font
Definition: main.h:46
bool startOctave()
void slotDisplayErr()
tQucsSettings QucsSettings
Definition: main.cpp:52
void slotSendCommand()
QSize sizeHint()
QString BinDir
Definition: main.h:57
Definitions and declarations for the main application.
void slotDisplayMsg()
QProcess octProcess
Definition: octave_window.h:41
void runOctaveScript(const QString &)
QLineEdit * input
Definition: octave_window.h:42
void adjustDirectory()
OctaveWindow(QDockWidget *)
QDir OctaveBinDir
Definition: main.h:69
void sendCommand(const QString &)
#define executableSuffix
QTextEdit * output
Definition: octave_window.h:40
QStringList cmdHistory
Definition: octave_window.h:43
QColor BGColor
Definition: main.h:48