Skip to content

Commit

Permalink
Avoid extra QFile objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
adeas31 committed Sep 28, 2016
1 parent 1dd8804 commit a4b17ce
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 30 deletions.
32 changes: 8 additions & 24 deletions OMEdit/OMEditGUI/MainWindow.cpp
Expand Up @@ -97,31 +97,17 @@ MainWindow::MainWindow(QSplashScreen *pSplashScreen, bool debug, QWidget *parent
freopen(outputFileName.toStdString().c_str(), "w", stdout);
setbuf(stdout, NULL); // used non-buffered stdout
mpOutputFileDataNotifier = 0;
mOutputFile.setFileName(outputFileName);
if (mOutputFile.open(QIODevice::ReadOnly)) {
mpOutputFileDataNotifier = new FileDataNotifier(outputFileName);
connect(mpOutputFileDataNotifier, SIGNAL(bytesAvailable(qint64)), SLOT(readOutputFile(qint64)));
mpOutputFileDataNotifier->start();
} else {
mpMessagesWidget->addGUIMessage(MessageItem(MessageItem::Modelica, "", false, 0, 0, 0, 0, tr("Can't open file %1.").arg(outputFileName), Helper::scriptingKind,
Helper::errorLevel));

}
mpOutputFileDataNotifier = new FileDataNotifier(outputFileName);
connect(mpOutputFileDataNotifier, SIGNAL(bytesAvailable(qint64)), SLOT(readOutputFile(qint64)));
mpOutputFileDataNotifier->start();
// Reopen the standard error stream.
QString errorFileName = Utilities::tempDirectory() + "/omediterror.txt";
freopen(errorFileName.toStdString().c_str(), "w", stderr);
setbuf(stderr, NULL); // used non-buffered stderr
mpErrorFileDataNotifier = 0;
mErrorFile.setFileName(errorFileName);
if (mErrorFile.open(QIODevice::ReadOnly)) {
mpErrorFileDataNotifier = new FileDataNotifier(errorFileName);
connect(mpErrorFileDataNotifier, SIGNAL(bytesAvailable(qint64)), SLOT(readErrorFile(qint64)));
mpErrorFileDataNotifier->start();
} else {
mpMessagesWidget->addGUIMessage(MessageItem(MessageItem::Modelica, "", false, 0, 0, 0, 0, tr("Can't open file %1.").arg(errorFileName), Helper::scriptingKind,
Helper::errorLevel));

}
mpErrorFileDataNotifier = new FileDataNotifier(errorFileName);
connect(mpErrorFileDataNotifier, SIGNAL(bytesAvailable(qint64)), SLOT(readErrorFile(qint64)));
mpErrorFileDataNotifier->start();
// Create an object of QProgressBar
mpProgressBar = new QProgressBar;
mpProgressBar->setMaximumWidth(300);
Expand Down Expand Up @@ -440,13 +426,11 @@ void MainWindow::beforeClosingMainWindow()
{
mpOMCProxy->quitOMC();
if (mpOutputFileDataNotifier) {
mOutputFile.close();
mpOutputFileDataNotifier->exit();
mpOutputFileDataNotifier->wait();
delete mpOutputFileDataNotifier;
}
if (mpErrorFileDataNotifier) {
mErrorFile.close();
mpErrorFileDataNotifier->exit();
mpErrorFileDataNotifier->wait();
delete mpErrorFileDataNotifier;
Expand Down Expand Up @@ -1333,7 +1317,7 @@ void MainWindow::loadSystemLibrary()
*/
void MainWindow::readOutputFile(qint64 bytes)
{
QString data = mOutputFile.read(bytes);
QString data = mpOutputFileDataNotifier->read(bytes);
mpMessagesWidget->addGUIMessage(MessageItem(MessageItem::Modelica, "", false, 0, 0, 0, 0, data, Helper::scriptingKind, Helper::notificationLevel));
}

Expand All @@ -1344,7 +1328,7 @@ void MainWindow::readOutputFile(qint64 bytes)
*/
void MainWindow::readErrorFile(qint64 bytes)
{
QString data = mErrorFile.read(bytes);
QString data = mpErrorFileDataNotifier->read(bytes);
mpMessagesWidget->addGUIMessage(MessageItem(MessageItem::Modelica, "", false, 0, 0, 0, 0, data, Helper::scriptingKind, Helper::notificationLevel));
}

Expand Down
2 changes: 0 additions & 2 deletions OMEdit/OMEditGUI/MainWindow.h
Expand Up @@ -208,9 +208,7 @@ class MainWindow : public QMainWindow
OptionsDialog *mpOptionsDialog;
MessagesWidget *mpMessagesWidget;
QDockWidget *mpMessagesDockWidget;
QFile mOutputFile;
FileDataNotifier *mpOutputFileDataNotifier;
QFile mErrorFile;
FileDataNotifier *mpErrorFileDataNotifier;
LibraryWidget *mpLibraryWidget;
QDockWidget *mpLibraryDockWidget;
Expand Down
6 changes: 3 additions & 3 deletions OMEdit/OMEditGUI/TLM/TLMCoSimulationThread.cpp
Expand Up @@ -145,6 +145,7 @@ void TLMCoSimulationThread::managerProcessStarted()
{
setIsManagerProcessRunning(true);
emit sendManagerStarted();
// start monitor
runMonitor();
}

Expand Down Expand Up @@ -197,12 +198,11 @@ void TLMCoSimulationThread::monitorProcessStarted()
{
setIsMonitorProcessRunning(true);
emit sendMonitorStarted();
QFileInfo fileInfo(mpTLMCoSimulationOutputWidget->getTLMCoSimulationOptions().getFileName());
// give 5 secs to tlmmonitor to create .run file
QFileInfo fileInfo(mpTLMCoSimulationOutputWidget->getTLMCoSimulationOptions().getFileName());
mProgressFile.setFileName(fileInfo.absoluteDir().absolutePath() + "/" + fileInfo.completeBaseName() + ".run");
int ticks = 0;
while (ticks < 5) {
if (mProgressFile.exists()) break;
while (ticks < 5 && !mProgressFile.exists()) {
Sleep::sleep(1);
ticks++;
}
Expand Down
13 changes: 12 additions & 1 deletion OMEdit/OMEditGUI/Util/Utilities.cpp
Expand Up @@ -163,6 +163,17 @@ void FileDataNotifier::exit(int retcode)
QThread::exit(retcode);
}

/*!
* \brief FileDataNotifier::read
* Reads the bytes from the file.
* \param maxlen
* \return
*/
QByteArray FileDataNotifier::read(qint64 maxlen)
{
return mFile.read(maxlen);
}

/*!
* \brief FileDataNotifier::run
* Reimplentation of QThread::run().
Expand All @@ -172,7 +183,7 @@ void FileDataNotifier::exit(int retcode)
void FileDataNotifier::run()
{
if (mFile.open(QIODevice::ReadOnly)) {
while(!mStop) {
while (!mStop) {
// if file doesn't exist then break.
if (!mFile.exists()) {
break;
Expand Down
1 change: 1 addition & 0 deletions OMEdit/OMEditGUI/Util/Utilities.h
Expand Up @@ -114,6 +114,7 @@ class FileDataNotifier : public QThread
public:
FileDataNotifier(const QString fileName);
void exit(int retcode = 0);
QByteArray read(qint64 maxlen);
private:
QFile mFile;
bool mStop;
Expand Down

0 comments on commit a4b17ce

Please sign in to comment.