Qucs-GUI  0.0.19
/home/travis/build/Qucs/qucs/qucs/qucs/octave_window.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002     copyright            : (C) 2010 by Michael Margraf
00003     email                : michael.margraf@alumni.tu-berlin.de
00004  ***************************************************************************/
00005 #include "octave_window.h"
00006 #include "main.h"
00007 
00008 #include <QSize>
00009 #include <QColor>
00010 #include <QKeyEvent>
00011 #include <QWidget>
00012 #include <QVBoxLayout>
00013 #include <QTextEdit>
00014 #include <QLineEdit>
00015 #include <QDockWidget>
00016 #include <QDebug>
00017 #include <QMessageBox>
00018 
00019 
00020 #ifdef __MINGW32__
00021 #define executableSuffix ".exe"
00022 #else
00023 #define executableSuffix ""
00024 #endif
00025 
00026 
00027 OctaveWindow::OctaveWindow(QDockWidget *parent_): QWidget()
00028 {
00029   QFont font;
00030   font = QFont("Courier New");
00031   font.setPointSize(QucsSettings.font.pointSize()-1);
00032   font.setStyleHint(QFont::Courier);
00033   font.setFixedPitch(true);
00034   setFont(font);
00035 
00036   QWidget *all = new QWidget();
00037   QVBoxLayout *allLayout = new QVBoxLayout();
00038 
00039   output = new QTextEdit(this);
00040   output->setReadOnly(true);
00041   output->setUndoRedoEnabled(false);
00042   output->setTextFormat(Qt::LogText);
00043   output->setLineWrapMode(QTextEdit::NoWrap);
00044   output->setPaletteBackgroundColor(QucsSettings.BGColor);
00045   allLayout->addWidget(output);
00046 
00047   input = new QLineEdit(this);
00048   connect(input, SIGNAL(returnPressed()), SLOT(slotSendCommand()));
00049   allLayout->addWidget(input);
00050   all->setLayout(allLayout);
00051 
00052   setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
00053   parent_->setWidget(all);
00054 
00055   //parent_->setResizeEnabled(true);
00056   //parent_->setHorizontallyStretchable(true);
00057   histPosition = 0;
00058 
00059   input->installEventFilter(this);
00060 }
00061 
00062 // -----------------------------------------------------------------
00063 OctaveWindow::~OctaveWindow()
00064 {
00065   if(octProcess.state()==QProcess::Running)
00066     octProcess.kill();
00067 }
00068 
00069 // -----------------------------------------------------------------
00070 QSize OctaveWindow::sizeHint() const
00071 {
00072   QSize Size;
00073   int w=0, h=0;
00074 
00075   Size = output->sizeHint();
00076   w = Size.width();
00077   h = Size.height() + input->sizeHint().height();
00078   return QSize(w, h);
00079 }
00080 
00081 // ------------------------------------------------------------------------
00082 bool OctaveWindow::startOctave()
00083 {
00084   if(octProcess.state()==QProcess::Running)
00085     return true;
00086 
00087   QString OctavePath=QucsSettings.OctaveBinDir.canonicalPath();
00088 
00089 
00090   QString Program;
00091   QStringList Arguments;
00092 
00093   OctavePath = QDir::toNativeSeparators(OctavePath+"/"+"octave"+QString(executableSuffix));
00094 
00095   QFileInfo progOctave(OctavePath);
00096 
00097   if (! progOctave.exists()) {
00098       qDebug() << "Octave not found: " << OctavePath;
00099       QMessageBox::critical(0, QObject::tr("Error"),
00100                             QObject::tr("Octave not found in: %1\n\n"
00101                                         "Set the Octave location on the application settings.").arg(OctavePath));
00102       return false;
00103   }
00104 
00105   Program = OctavePath;
00106   Arguments << "--no-history" << "-i" << "-f" << "-p"
00107             << QDir::toNativeSeparators(QucsSettings.OctaveDir); // m-files location
00108 
00109   disconnect(&octProcess, 0, 0, 0);
00110   connect(&octProcess, SIGNAL(readyReadStandardError()), SLOT(slotDisplayErr()));
00111   connect(&octProcess, SIGNAL(readyReadStandardOutput()), SLOT(slotDisplayMsg()));
00112   connect(&octProcess, SIGNAL(finished(int)), SLOT(slotOctaveEnded(int)));
00113 #ifdef __MINGW32__
00114   QString sep(";"); // path separator
00115 #else
00116   QString sep(":");
00117 #endif
00118 
00119   // append process PATH, othewise Octave does not find gnuplot
00120   QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
00121   env.insert("PATH", env.value("PATH") + sep + QucsSettings.BinDir );
00122   octProcess.setProcessEnvironment(env);
00123   output->clear();
00124 
00125   qDebug() << "Command :" << Program << Arguments.join(" ");
00126   octProcess.start(Program, Arguments);
00127 
00128   if(octProcess.state()!=QProcess::Running&&
00129           octProcess.state()!=QProcess::Starting) {
00130     output->setText(tr("ERROR: Cannot start Octave!"));
00131     return false;
00132   }
00133 
00134   adjustDirectory();
00135   return true;
00136 }
00137 
00138 // ------------------------------------------------------------------------
00139 void OctaveWindow::adjustDirectory()
00140 {
00141   sendCommand("cd \"" + QucsSettings.QucsWorkDir.absPath() + "\"");
00142 }
00143 
00144 // ------------------------------------------------------------------------
00145 void OctaveWindow::sendCommand(const QString& cmd)
00146 {
00147   //int par = output->paragraphs() - 1;
00148   //int idx = output->paragraphLength(par);
00149   output->setTextColor(QColor(Qt::blue));
00150   output->append(cmd);
00151   QString cmdstr = cmd + "\n";
00152   //output->insertAt(cmdstr, par, idx);
00153   //output->scrollToBottom();
00154   octProcess.write(cmdstr);
00155 }
00156 
00157 // ------------------------------------------------------------------------
00158 void OctaveWindow::runOctaveScript(const QString& name)
00159 {
00160   QFileInfo info(name);
00161   sendCommand(info.baseName(true));
00162 }
00163 
00164 // ------------------------------------------------------------------------
00165 void OctaveWindow::slotSendCommand()
00166 {
00167   sendCommand(input->text());
00168   if(!input->text().trimmed().isEmpty())
00169     cmdHistory.append(input->text());
00170   //histIterator = cmdHistory.end();
00171   histPosition++;
00172   input->clear();
00173   qDebug() << cmdHistory << histPosition;
00174 }
00175 
00176 
00177 bool OctaveWindow::eventFilter(QObject *obj, QEvent *event) {
00178     Q_UNUSED(obj);
00179 
00180     if (event->type() == QEvent::KeyPress) {
00181         QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
00182             if (keyEvent->key() == Qt::Key_PageUp) {
00183                 //if(histIterator == cmdHistory.begin())
00184                 if(histPosition == 0)
00185                     return false;
00186                 //histIterator--;
00187                 histPosition--;
00188                 input->setText(cmdHistory.at(histPosition));//*histIterator);
00189                 return true;
00190             }
00191             else if(keyEvent->key() == Qt::Key_PageDown) {
00192                 //if(histIterator == cmdHistory.end())
00193                 if(histPosition == cmdHistory.length()-1)
00194                     return false;
00195                 //histIterator++;
00196                 histPosition++;
00197                 input->setText(cmdHistory.at(histPosition));//*histIterator);
00198                 return true;
00199             }
00200      }
00201      return false;
00202 }
00203 
00204 // ------------------------------------------------------------------------
00205 // Is called when the process sends an output to stdout.
00206 void OctaveWindow::slotDisplayMsg()
00207 {
00208   //int par = output->paragraphs() - 1;
00209   //int idx = output->paragraphLength(par);
00210   //output->insertAt(QString(octProcess.readAllStandardOutput()), par, idx);
00211   //output->scrollToBottom();
00212   output->setTextColor(QColor(Qt::black));
00213   output->append(octProcess.readAllStandardOutput());
00214 }
00215 
00216 // ------------------------------------------------------------------------
00217 // Is called when the process sends an output to stderr.
00218 void OctaveWindow::slotDisplayErr()
00219 {
00220   //if(!isVisible())
00221   //  ((Q3DockWindow*)parent())->show();  // always show an error
00222 
00223   //int par = output->paragraphs() - 1;
00224   //int idx = output->paragraphLength(par);
00225   //output->insertAt(QString(octProcess.readAllStandardError()), par, idx);
00226   //output->scrollToBottom();
00227     output->setTextColor(QColor(Qt::red));
00228     output->append(octProcess.readAllStandardError());
00229 }
00230 
00231 // ------------------------------------------------------------------------
00232 // Is called when the simulation process terminates.
00233 void OctaveWindow::slotOctaveEnded(int status)
00234 {
00235   qDebug() << "Octave ended status" << status;
00236   output->clear();
00237 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines