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