* can run compiled exe now

* correctly set editor line number width
This commit is contained in:
royqh1979@gmail.com 2021-04-21 18:58:35 +08:00
parent cea9b757ab
commit 807cd1502e
13 changed files with 208 additions and 19 deletions

View File

@ -11,6 +11,7 @@ CONFIG += c++14
SOURCES += \
compiler/compiler.cpp \
compiler/compilermanager.cpp \
compiler/executablerunner.cpp \
compiler/filecompiler.cpp \
editor.cpp \
editorlist.cpp \
@ -27,6 +28,7 @@ SOURCES += \
HEADERS += \
compiler/compiler.h \
compiler/compilermanager.h \
compiler/executablerunner.h \
compiler/filecompiler.h \
editor.h \
editorlist.h \
@ -53,7 +55,12 @@ qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
LIBS += -L$$PWD -lqscintilla2_qt5
RESOURCES += \
icons.qrc
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../../QScintilla/src/release/ -lqscintilla2_qt5d
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../../QScintilla/src/debug/ -lqscintilla2_qt5d
else:unix: LIBS += -L$$OUT_PWD/../../QScintilla/src/ -lqscintilla2_qt5d
INCLUDEPATH += $$PWD/../../QScintilla/src
DEPENDPATH += $$PWD/../../QScintilla/src

View File

@ -178,6 +178,7 @@ void Compiler::runCommand(const QString &cmd, const QString &arguments, const Q
if (!inputText.isEmpty())
process.write(inputText.toUtf8());
process.closeWriteChannel();
process.waitForStarted(5000);
while (true) {
process.waitForFinished(1000);
if (process.state()!=QProcess::Running) {

View File

@ -2,11 +2,14 @@
#include "filecompiler.h"
#include <QDebug>
#include "../mainwindow.h"
#include "executablerunner.h"
#include "utils.h"
CompilerManager::CompilerManager(QObject *parent) : QObject(parent)
{
mCompiler = nullptr;
mBackgroundSyntaxChecker = nullptr;
mRunner = nullptr;
}
bool CompilerManager::compiling()
@ -21,24 +24,50 @@ bool CompilerManager::backgroundSyntaxChecking()
void CompilerManager::compile(const QString& filename, const QByteArray& encoding, bool silent, bool onlyCheckSyntax)
{
{
QMutexLocker locker(&compileMutex);
if (mCompiler!=nullptr) {
return;
}
mCompiler = new FileCompiler(filename,encoding,silent,onlyCheckSyntax);
connect(mCompiler, &Compiler::compileFinished, this ,&CompilerManager::onCompileFinished);
connect(mCompiler, &Compiler::compileOutput, pMainWindow, &MainWindow::onCompileLog);
connect(mCompiler, &Compiler::compileError, pMainWindow, &MainWindow::onCompileError);
mCompiler->start();
QMutexLocker locker(&compileMutex);
if (mCompiler!=nullptr) {
return;
}
mCompiler = new FileCompiler(filename,encoding,silent,onlyCheckSyntax);
connect(mCompiler, &Compiler::compileFinished, this ,&CompilerManager::onCompileFinished);
connect(mCompiler, &Compiler::compileOutput, pMainWindow, &MainWindow::onCompileLog);
connect(mCompiler, &Compiler::compileError, pMainWindow, &MainWindow::onCompileError);
mCompiler->start();
}
void CompilerManager::run(const QString &filename, const QString &arguments, const QString &workDir)
{
QMutexLocker locker(&runnerMutex);
if (mRunner!=nullptr) {
return;
}
qDebug()<<"lala1";
if (programHasConsole(filename)) {
QString newArguments = QString(" 0 \"%1\" %2").arg(toLocalPath(filename)).arg(arguments);
mRunner = new ExecutableRunner(includeTrailingPathDelimiter(pSettings->dirs().app())+"ConsolePauser.exe",newArguments,workDir);
} else {
mRunner = new ExecutableRunner(filename,arguments,workDir);
}
connect(mRunner, &ExecutableRunner::terminated, this ,&CompilerManager::onRunnerTerminated);
mRunner->start();
}
bool CompilerManager::canCompile(const QString &filename)
{
return !compiling();
}
void CompilerManager::onCompileFinished()
{
QMutexLocker locker(&compileMutex);
qDebug() << " Compile Finished";
qDebug() << "Compile finished";
mCompiler=nullptr;
}
void CompilerManager::onRunnerTerminated()
{
QMutexLocker locker(&runnerMutex);
qDebug() << "Runner Terminated";
mRunner=nullptr;
}

View File

@ -2,9 +2,10 @@
#define COMPILERMANAGER_H
#include <QObject>
#include "compiler.h"
#include <QMutex>
class ExecutableRunner;
class Compiler;
class CompilerManager : public QObject
{
Q_OBJECT
@ -15,13 +16,18 @@ public:
bool backgroundSyntaxChecking();
void compile(const QString& filename, const QByteArray& encoding, bool silent=false,bool onlyCheckSyntax=false);
void run(const QString& filename, const QString& arguments, const QString& workDir);
bool canCompile(const QString& filename);
private slots:
void onCompileFinished();
void onRunnerTerminated();
private:
Compiler* mCompiler;
Compiler* mBackgroundSyntaxChecker;
ExecutableRunner* mRunner;
QMutex compileMutex;
QMutex backgroundSyntaxChekMutex;
QMutex runnerMutex;
};
#endif // COMPILERMANAGER_H

View File

@ -0,0 +1,51 @@
#include "executablerunner.h"
#include <QProcess>
#include <windows.h>
#include <QDebug>
ExecutableRunner::ExecutableRunner(const QString &filename, const QString &arguments, const QString &workDir):
QThread(),
mFilename(filename),
mArguments(arguments),
mWorkDir(workDir),
mStop(false)
{
}
void ExecutableRunner::stop()
{
mStop = true;
}
void ExecutableRunner::run()
{
emit started();
QProcess process;
mStop = false;
process.setProgram(mFilename);
process.setArguments(QProcess::splitCommand(mArguments));
process.setWorkingDirectory(mWorkDir);
process.setCreateProcessArgumentsModifier([](QProcess::CreateProcessArguments * args){
args->flags |= CREATE_NEW_CONSOLE;
args->startupInfo -> dwFlags &= ~STARTF_USESTDHANDLES;
});
qDebug() << mFilename;
qDebug() << QProcess::splitCommand(mArguments);
process.start();
process.closeWriteChannel();
process.waitForStarted(5000);
while (true) {
process.waitForFinished(1000);
if (process.state()!=QProcess::Running) {
break;
}
if (mStop) {
process.kill();
break;
}
}
emit terminated();
this->deleteLater();
}

View File

@ -0,0 +1,30 @@
#ifndef EXECUTABLERUNNER_H
#define EXECUTABLERUNNER_H
#include <QThread>
class ExecutableRunner : public QThread
{
Q_OBJECT
public:
ExecutableRunner(const QString& filename, const QString& arguments, const QString& workDir);
signals:
void started();
void terminated();
public slots:
void stop();
private:
QString mFilename;
QString mArguments;
QString mWorkDir;
bool mStop;
// QThread interface
protected:
void run() override;
};
#endif // EXECUTABLERUNNER_H

View File

@ -69,7 +69,7 @@ Editor::Editor(QWidget *parent, const QString& filename,
//行号显示区域
setMarginType(0, QsciScintilla::NumberMargin);
setMarginLineNumbers(0, true);
setMarginWidth(0,30);
setMarginWidth(0,"10");
//断点设置区域
setMarginType(1, QsciScintilla::SymbolMargin);
setMarginLineNumbers(1, false);
@ -277,6 +277,7 @@ void Editor::wheelEvent(QWheelEvent *event) {
} else {
this->zoomOut();
}
onLinesChanged();
}
}
@ -289,6 +290,8 @@ void Editor::onCursorPositionChanged(int line, int index) {
}
void Editor::onLinesChanged() {
this->setMarginWidth(0,QString("0%1").arg(lines()));
qDebug()<<marginWidth(0);
qDebug()<<"Editor lines changed"<<lines();
}

View File

@ -185,7 +185,16 @@ void MainWindow::onCompileError(const QString &msg)
void MainWindow::on_actionCompile_triggered()
{
Editor * editor = mEditorList->getEditor();
if (editor != NULL) {
if (editor != NULL ) {
mCompilerManager->compile(editor->filename(),editor->fileEncoding());
}
}
void MainWindow::on_actionRun_triggered()
{
Editor * editor = mEditorList->getEditor();
if (editor != NULL ) {
QString exeName= getCompiledExecutableName(editor->filename());
mCompilerManager->run(exeName,"",QFileInfo(exeName).absolutePath());
}
}

View File

@ -45,6 +45,8 @@ private slots:
void on_actionCompile_triggered();
void on_actionRun_triggered();
public slots:
void onCompileLog(const QString& msg);

View File

@ -231,6 +231,7 @@
<string>Run</string>
</property>
<addaction name="actionCompile"/>
<addaction name="actionRun"/>
</widget>
<addaction name="menuFile"/>
<addaction name="menuRun"/>
@ -274,6 +275,7 @@
<bool>false</bool>
</attribute>
<addaction name="actionCompile"/>
<addaction name="actionRun"/>
</widget>
<action name="actionNew">
<property name="icon">
@ -361,6 +363,22 @@
<string>F9</string>
</property>
</action>
<action name="actionRun">
<property name="icon">
<iconset>
<normalon>:/icons/images/newlook24/069-run.png</normalon>
</iconset>
</property>
<property name="text">
<string>Run</string>
</property>
<property name="toolTip">
<string>Run</string>
</property>
<property name="shortcut">
<string>F10</string>
</property>
</action>
</widget>
<resources/>
<connections/>

View File

@ -12,6 +12,7 @@
#include <QTextCodec>
#include <QtGlobal>
#include <QDebug>
#include <windows.h>
const QByteArray GuessTextEncoding(const QByteArray& text){
bool allAscii;
@ -106,8 +107,7 @@ QByteArray runAndGetOutput(const QString &cmd, const QString& workingDir, const
});
process.start(cmd,arguments);
process.closeWriteChannel();
process.waitForFinished(-1);
process.waitForFinished();
return result;
}
@ -223,3 +223,33 @@ void splitStringArguments(const QString &arguments, QStringList &argumentList)
argumentList.append(word);
}
}
bool programHasConsole(const QString &filename)
{
bool result = false;
HANDLE handle = CreateFile(filename.toStdWString().c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (handle != INVALID_HANDLE_VALUE) {
IMAGE_DOS_HEADER dos_header;
DWORD signature;
DWORD bytesread;
IMAGE_FILE_HEADER pe_header;
IMAGE_OPTIONAL_HEADER opt_header;
ReadFile(handle, &dos_header, sizeof(dos_header), &bytesread, NULL);
SetFilePointer(handle, dos_header.e_lfanew, NULL, 0);
ReadFile(handle, &signature, sizeof(signature), &bytesread, NULL);
ReadFile(handle, &pe_header, sizeof(pe_header), &bytesread, NULL);
ReadFile(handle, &opt_header, sizeof(opt_header), &bytesread, NULL);
result = (opt_header.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI);
}
CloseHandle(handle);
return result;
}
QString toLocalPath(const QString &filename)
{
QString newPath {filename};
newPath.replace("/",QDir::separator());
return newPath;
}

View File

@ -43,6 +43,8 @@ QString excludeTrailingPathDelimiter(const QString& path);
FileType getFileType(const QString& filename);
QString getCompiledExecutableName(const QString filename);
void splitStringArguments(const QString& arguments, QStringList& argumentList);
bool programHasConsole(const QString& filename);
QString toLocalPath(const QString& filename);
template <class F>
class final_action

View File

@ -1,4 +1,5 @@
TEMPLATE = subdirs
SUBDIRS += \
../QScintilla/src/qscintilla.pro \
RedPandaIDE