work done: make qsynedit a standalone module

This commit is contained in:
Roy Qu 2022-09-26 14:54:28 +08:00
parent 15bbf15d38
commit ebf78dbecb
13 changed files with 263 additions and 14 deletions

View File

@ -52,7 +52,19 @@ msvc {
# In order to do so, uncomment the following line. # In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG(debug_and_release_target): {
CONFIG(debug): {
OBJ_OUT_PWD = debug
}
CONFIG(release): {
OBJ_OUT_PWD = release
}
}
INCLUDEPATH += ../libs/qsynedit ../libs/redpanda_qt_utils INCLUDEPATH += ../libs/qsynedit ../libs/redpanda_qt_utils
LIBS += -L$$OUT_PWD/../libs/redpanda_qt_utils/$$OBJ_OUT_PWD \
-L$$OUT_PWD/../libs/qsynedit/$$OBJ_OUT_PWD \
-lqsynedit -lredpanda_qt_utils
SOURCES += \ SOURCES += \
HighlighterManager.cpp \ HighlighterManager.cpp \

View File

@ -50,6 +50,7 @@
#include "editorlist.h" #include "editorlist.h"
#include <QDebug> #include <QDebug>
#include "project.h" #include "project.h"
#include <qt_utils/charsetinfo.h>
using namespace std; using namespace std;
@ -1372,7 +1373,7 @@ void Editor::copyAsHTML()
{ {
if (!selAvail()) if (!selAvail())
return; return;
QSynedit::SynHTMLExporter exporter(tabWidth()); QSynedit::SynHTMLExporter exporter(tabWidth(), pCharsetInfoManager->getDefaultSystemEncoding());
exporter.setTitle(QFileInfo(mFilename).fileName()); exporter.setTitle(QFileInfo(mFilename).fileName());
exporter.setExportAsText(false); exporter.setExportAsText(false);
@ -2837,7 +2838,7 @@ void Editor::print()
void Editor::exportAsRTF(const QString &rtfFilename) void Editor::exportAsRTF(const QString &rtfFilename)
{ {
QSynedit::SynRTFExporter exporter; QSynedit::SynRTFExporter exporter(pCharsetInfoManager->getDefaultSystemEncoding());
exporter.setTitle(extractFileName(rtfFilename)); exporter.setTitle(extractFileName(rtfFilename));
exporter.setExportAsText(true); exporter.setExportAsText(true);
exporter.setUseBackground(pSettings->editor().copyRTFUseBackground()); exporter.setUseBackground(pSettings->editor().copyRTFUseBackground());
@ -2862,7 +2863,7 @@ void Editor::exportAsRTF(const QString &rtfFilename)
void Editor::exportAsHTML(const QString &htmlFilename) void Editor::exportAsHTML(const QString &htmlFilename)
{ {
QSynedit::SynHTMLExporter exporter(tabWidth()); QSynedit::SynHTMLExporter exporter(tabWidth(), pCharsetInfoManager->getDefaultSystemEncoding());
exporter.setTitle(extractFileName(htmlFilename)); exporter.setTitle(extractFileName(htmlFilename));
exporter.setExportAsText(false); exporter.setExportAsText(false);
exporter.setUseBackground(pSettings->editor().copyHTMLUseBackground()); exporter.setUseBackground(pSettings->editor().copyHTMLUseBackground());

View File

@ -32,7 +32,7 @@
#include "colorscheme.h" #include "colorscheme.h"
#include "iconsmanager.h" #include "iconsmanager.h"
#include "autolinkmanager.h" #include "autolinkmanager.h"
#include "platform.h" #include <qt_utils/charsetinfo.h>
#include "parser/parserutils.h" #include "parser/parserutils.h"
#include "editorlist.h" #include "editorlist.h"
#include "widgets/choosethemedialog.h" #include "widgets/choosethemedialog.h"

View File

@ -27,7 +27,7 @@
#include "project.h" #include "project.h"
#include "projecttemplate.h" #include "projecttemplate.h"
#include "widgets/newprojectdialog.h" #include "widgets/newprojectdialog.h"
#include "platform.h" #include <qt_utils/charsetinfo.h>
#include "widgets/aboutdialog.h" #include "widgets/aboutdialog.h"
#include "shortcutmanager.h" #include "shortcutmanager.h"
#include "colorscheme.h" #include "colorscheme.h"

View File

@ -22,7 +22,7 @@
#include <QMessageBox> #include <QMessageBox>
#include "../utils.h" #include "../utils.h"
#include "../iconsmanager.h" #include "../iconsmanager.h"
#include "../platform.h" #include <qt_utils/charsetinfo.h>
#include <QDebug> #include <QDebug>
#include <QFileDialog> #include <QFileDialog>
#include <QInputDialog> #include <QInputDialog>

View File

@ -19,7 +19,7 @@
#include "../settings.h" #include "../settings.h"
#include "../project.h" #include "../project.h"
#include "../mainwindow.h" #include "../mainwindow.h"
#include "../platform.h" #include <qt_utils/charsetinfo.h>
ProjectCompilerWidget::ProjectCompilerWidget(const QString &name, const QString &group, QWidget *parent) : ProjectCompilerWidget::ProjectCompilerWidget(const QString &name, const QString &group, QWidget *parent) :
SettingsWidget(name,group,parent), SettingsWidget(name,group,parent),

View File

@ -18,7 +18,7 @@
#include "ui_projectfileswidget.h" #include "ui_projectfileswidget.h"
#include "../mainwindow.h" #include "../mainwindow.h"
#include "../systemconsts.h" #include "../systemconsts.h"
#include "../platform.h" #include <qt_utils/charsetinfo.h>
ProjectFilesWidget::ProjectFilesWidget(const QString &name, const QString &group, QWidget *parent) : ProjectFilesWidget::ProjectFilesWidget(const QString &name, const QString &group, QWidget *parent) :
SettingsWidget(name,group,parent), SettingsWidget(name,group,parent),

View File

@ -2,12 +2,14 @@
#include "systemconsts.h" #include "systemconsts.h"
#include <QDate> #include <QDate>
#include <QDateTime> #include <QDateTime>
#include <QApplication>
#include "editor.h" #include "editor.h"
#include "editorlist.h" #include "editorlist.h"
#include "settings.h" #include "settings.h"
#include "mainwindow.h" #include "mainwindow.h"
#include "project.h" #include "project.h"
#include "parser/cppparser.h" #include "parser/cppparser.h"
#include "compiler/executablerunner.h"
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
#include <QMimeDatabase> #include <QMimeDatabase>
#include <windows.h> #include <windows.h>
@ -355,3 +357,167 @@ int getNewFileNumber()
count++; count++;
return count; return count;
} }
#ifdef Q_OS_WIN
static bool gIsGreenEdition = true;
static bool gIsGreenEditionInited = false;
#endif
bool isGreenEdition()
{
#ifdef Q_OS_WIN
if (!gIsGreenEditionInited) {
QString keyString = QString("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\RedPanda-C++");
QString value;
if (!readRegistry(HKEY_LOCAL_MACHINE,keyString.toLocal8Bit(),"UninstallString",value)) {
keyString = "SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\RedPanda-C++";
if (!readRegistry(HKEY_LOCAL_MACHINE,keyString.toLocal8Bit(),"UninstallString",value)) {
value="";
}
}
if (!value.isEmpty()) {
QString regPath = extractFileDir(value);
QString appPath = QApplication::instance()->applicationDirPath();
gIsGreenEdition = excludeTrailingPathDelimiter(regPath).compare(excludeTrailingPathDelimiter(appPath),
Qt::CaseInsensitive)!=0;
}
gIsGreenEditionInited = true;
}
return gIsGreenEdition;
#else
return false;
#endif
}
QByteArray runAndGetOutput(const QString &cmd, const QString& workingDir, const QStringList& arguments,
const QByteArray &inputContent, bool inheritEnvironment,
const QProcessEnvironment& env)
{
QProcess process;
QByteArray result;
if (env.isEmpty()) {
if (inheritEnvironment) {
process.setProcessEnvironment(QProcessEnvironment::systemEnvironment());
} else {
process.setProcessEnvironment(QProcessEnvironment());
}
} else {
process.setProcessEnvironment(env);
}
process.setWorkingDirectory(workingDir);
process.connect(&process,&QProcess::readyReadStandardError,
[&](){
result.append(process.readAllStandardError());
});
process.connect(&process,&QProcess::readyReadStandardOutput,
[&](){
result.append(process.readAllStandardOutput());
});
process.start(cmd,arguments);
if (!inputContent.isEmpty()) {
process.write(inputContent);
}
process.closeWriteChannel();
process.waitForFinished();
return result;
}
void executeFile(const QString &fileName, const QString &params, const QString &workingDir, const QString &tempFile)
{
ExecutableRunner* runner=new ExecutableRunner(
fileName,
params,
workingDir);
runner->connect(runner, &QThread::finished,
[runner,tempFile](){
if (!tempFile.isEmpty()) {
QFile::remove(tempFile);
}
runner->deleteLater();
});
runner->connect(runner, &Runner::runErrorOccurred,
[](const QString&){
//todo
});
runner->setStartConsole(true);
runner->start();
}
#ifdef Q_OS_WIN
bool readRegistry(HKEY key,const QByteArray& subKey, const QByteArray& name, QString& value) {
DWORD dataSize;
LONG result;
result = RegGetValueA(key,subKey,
name, RRF_RT_REG_SZ | RRF_RT_REG_MULTI_SZ,
NULL,
NULL,
&dataSize);
if (result!=ERROR_SUCCESS)
return false;
char * buffer = new char[dataSize+10];
result = RegGetValueA(key,subKey,
name, RRF_RT_REG_SZ | RRF_RT_REG_MULTI_SZ,
NULL,
buffer,
&dataSize);
if (result!=ERROR_SUCCESS) {
delete[] buffer;
return false;
}
value=QString::fromLocal8Bit(buffer);
delete [] buffer;
return true;
}
#endif
qulonglong stringToHex(const QString &str, bool &isOk)
{
qulonglong value = str.toULongLong(&isOk,16);
return value;
}
bool findComplement(const QString &s, const QChar &fromToken, const QChar &toToken, int &curPos, int increment)
{
int curPosBackup = curPos;
int level = 0;
//todo: skip comment, char and strings
while ((curPos < s.length()) && (curPos >= 0)) {
if (s[curPos] == fromToken) {
level++;
} else if (s[curPos] == toToken) {
level--;
if (level == 0)
return true;
}
curPos += increment;
}
curPos = curPosBackup;
return false;
}
bool haveGoodContrast(const QColor& c1, const QColor &c2) {
int lightness1 = qGray(c1.rgb());
int lightness2 = qGray(c2.rgb());
return std::abs(lightness1 - lightness2)>=120;
}
QByteArray getHTTPBody(const QByteArray& content) {
int i= content.indexOf("\r\n\r\n");
if (i>=0) {
return content.mid(i+4);
}
return "";
}
QString getSizeString(int size)
{
if (size < 1024) {
return QString("%1 ").arg(size)+QObject::tr("bytes");
} else if (size < 1024 * 1024) {
return QString("%1 ").arg(size / 1024.0)+QObject::tr("KB");
} else if (size < 1024 * 1024 * 1024) {
return QString("%1 ").arg(size / 1024.0 / 1024.0)+QObject::tr("MB");
} else {
return QString("%1 ").arg(size / 1024.0 / 1024.0 / 1024.0)+QObject::tr("GB");
}
}

View File

@ -114,4 +114,34 @@ void resetCppParser(std::shared_ptr<CppParser> parser, int compilerSetIndex=-1);
int getNewFileNumber(); int getNewFileNumber();
QByteArray runAndGetOutput(const QString& cmd, const QString& workingDir, const QStringList& arguments,
const QByteArray& inputContent = QByteArray(),
bool inheritEnvironment = false,
const QProcessEnvironment& env = QProcessEnvironment() );
void executeFile(const QString& fileName,
const QString& params,
const QString& workingDir,
const QString& tempFile);
bool isGreenEdition();
#ifdef Q_OS_WIN
bool readRegistry(HKEY key,const QByteArray& subKey, const QByteArray& name, QString& value);
#endif
qulonglong stringToHex(const QString& str, bool &isOk);
bool findComplement(const QString& s,
const QChar& fromToken,
const QChar& toToken,
int& curPos,
int increment);
bool haveGoodContrast(const QColor& c1, const QColor &c2);
QByteArray getHTTPBody(const QByteArray& content);
QString getSizeString(int size);
#endif // UTILS_H #endif // UTILS_H

View File

@ -19,7 +19,7 @@
#include "../mainwindow.h" #include "../mainwindow.h"
#include "../editorlist.h" #include "../editorlist.h"
#include "../editor.h" #include "../editor.h"
#include "../qsynedit/Constants.h" #include <qsynedit/Constants.h>
#include <QFileInfo> #include <QFileInfo>

View File

@ -20,8 +20,8 @@
#include "../editor.h" #include "../editor.h"
#include "../mainwindow.h" #include "../mainwindow.h"
#include "../editorlist.h" #include "../editorlist.h"
#include "../qsynedit/Search.h" #include <qsynedit/Search.h>
#include "../qsynedit/SearchRegex.h" #include <qsynedit/SearchRegex.h>
#include "../project.h" #include "../project.h"
#include "../settings.h" #include "../settings.h"
#include <QMessageBox> #include <QMessageBox>

View File

@ -18,7 +18,7 @@
#define SEARCHDIALOG_H #define SEARCHDIALOG_H
#include <QDialog> #include <QDialog>
#include "../qsynedit/SynEdit.h" #include <qsynedit/SynEdit.h>
#include "../utils.h" #include "../utils.h"
namespace Ui { namespace Ui {

View File

@ -37,6 +37,28 @@
#include <windows.h> #include <windows.h>
#endif #endif
BaseError::BaseError(const QString &reason):
mReason(reason)
{
}
QString BaseError::reason() const
{
return mReason;
}
IndexOutOfRange::IndexOutOfRange(int Index):
BaseError(QObject::tr("Index %1 out of range").arg(Index))
{
}
FileError::FileError(const QString &reason): BaseError(reason)
{
}
const QByteArray guessTextEncoding(const QByteArray& text){ const QByteArray guessTextEncoding(const QByteArray& text){
bool allAscii; bool allAscii;
int ii; int ii;
@ -606,5 +628,23 @@ float pixelToPoint(float pixel)
} }
void decodeKey(const int combinedKey, int &key, Qt::KeyboardModifiers &modifiers)
{
modifiers = Qt::NoModifier;
if (combinedKey & Qt::ShiftModifier) {
modifiers|=Qt::ShiftModifier;
}
if (combinedKey & Qt::ControlModifier) {
modifiers|=Qt::ControlModifier;
}
if (combinedKey & Qt::AltModifier) {
modifiers|=Qt::AltModifier;
}
if (combinedKey & Qt::MetaModifier) {
modifiers|=Qt::MetaModifier;
}
if (combinedKey & Qt::KeypadModifier) {
modifiers|= Qt::KeypadModifier;
}
key = combinedKey & ~(Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier | Qt::KeypadModifier);
}