Skip to content

Commit

Permalink
修改CmakeLists.txt 支持QWebEngine
Browse files Browse the repository at this point in the history
  • Loading branch information
LC044 committed Aug 16, 2023
1 parent 264c508 commit 0d66f1a
Show file tree
Hide file tree
Showing 17 changed files with 1,325 additions and 64 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Thumbs.db
# --------
*.dll
*.exe
.VS
build
.vscode
maddy
46 changes: 36 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,39 @@ set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# 设置 Qt 库的地址(按需设置)
set(QT_PATH "D:\\ProgramData\\Qt\\Qt5.12.12\\5.12.12\\mingw73_64")
# set(QT_PATH "D:\\ProgramData\\Qt\\Qt5.12.12\\5.12.12\\mingw73_64")
set(QT_PATH "D:\\ProgramData\\Qt\\Qt5.12.12\\5.12.12\\msvc2017_64")
set(CMAKE_PREFIX_PATH ${QT_PATH}/lib/cmake)
find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets LinguistTools REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets WebEngineWidgets WebChannel REQUIRED)


find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets LinguistTools REQUIRED)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/include)
set(TS_FILES OneMarkdown_zh_CN.ts)
include(FetchContent)

# FetchContent_Declare(
# maddy
# URL https://github.com/progsource/maddy/.../maddy-src.zip
# )
# FetchContent_MakeAvailable(maddy)
# set(TS_FILES OneMarkdown_zh_CN.ts)

set(PROJECT_SOURCES
main.cpp
OneMarkdown.cpp
OneMarkdown.h
OneMarkdown.ui
${TS_FILES}
)

if(MATRIX_BUILD AND NOT MATRIX_SUBBUILD AND NOT QT_SUPERBUILD)
add_build(webengine_webrtc OFF)
add_build(webengine_proprietary_codecs ON)
add_build(webengine_printing_and_pdf OFF)
add_build(webengine_extensions OFF)
add_build(webengine_spellchecker OFF)
add_build(qtwebengine_build OFF)
return()
endif()
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(OneMarkdown
MANUAL_FINALIZATION
${PROJECT_SOURCES}
)

# Define target properties for Android with Qt 6 as:
# set_property(TARGET OneMarkdown APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
# ${CMAKE_CURRENT_SOURCE_DIR}/android)
Expand All @@ -60,6 +67,8 @@ else()
endif()

target_link_libraries(OneMarkdown PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
target_link_libraries(OneMarkdown PRIVATE Qt${QT_VERSION_MAJOR}::WebEngineWidgets Qt${QT_VERSION_MAJOR}::WebChannel)

set_target_properties(OneMarkdown PROPERTIES
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
Expand All @@ -69,3 +78,20 @@ set_target_properties(OneMarkdown PROPERTIES
if(QT_VERSION_MAJOR EQUAL 6)
qt_finalize_executable(OneMarkdown)
endif()

# ------------------------------------------------------------------------------

if(${MADDY_BUILD_WITH_TESTS})
add_subdirectory(tests)
endif()

# ------------------------------------------------------------------------------

if(${MADDY_CREATE_PACKAGE})
set(MADDY_PACKAGE_FILES include/ CMakeLists.txt LICENSE)
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/${PROJECT_NAME}-src.zip
COMMAND ${CMAKE_COMMAND} -E tar c ${CMAKE_BINARY_DIR}/${PROJECT_NAME}-src.zip --format=zip -- ${MADDY_PACKAGE_FILES}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${MADDY_PACKAGE_FILES})
add_custom_target(${PROJECT_NAME}_package DEPENDS ${CMAKE_BINARY_DIR}/${PROJECT_NAME}-src.zip)
endif()
443 changes: 439 additions & 4 deletions CMakeLists.txt.user

Large diffs are not rendered by default.

43 changes: 32 additions & 11 deletions OneMarkdown.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,41 @@
#include "./ui_OneMarkdown.h"
#include <memory>
#include <string>
#include <iostream>
#include <fstream>
#include "maddy/parser.h"
#include<QDebug>
std::stringstream markdownInput("# 一级标题");
#include <QDebug>
#include<QtWebEngine/QtWebEngine>
std::string html_end = "</div></body></html>";

// config is optional
// std::shared_ptr<maddy::ParserConfig> config = std::make_shared<maddy::ParserConfig>();

// config->isEmphasizedParserEnabled = false; // default true - this flag is deprecated
// config->isHTMLWrappedInParagraph = false; // default true - this flag is deprecated
// config->enabledParsers &= ~maddy::types::EMPHASIZED_PARSER; // equivalent to !isEmphasizedParserEnabled
// config->enabledParsers |= maddy::types::HTML_PARSER; // equivalent to !isHTMLWrappedInParagraph

// std::shared_ptr<maddy::Parser> parser = std::make_shared<maddy::Parser>(config);


auto parser = std::make_shared<maddy::Parser>();
std::string readFileIntoString(const std::string& path) {
std::ifstream input_file(path);
if (!input_file.is_open()) {
std::cerr << "Could not open the file - '"
<< path << "'" << endl;
exit(EXIT_FAILURE);
}
return std::string((std::istreambuf_iterator<char>(input_file)), std::istreambuf_iterator<char>());
}
std::string html_head = readFileIntoString("index.html");
OneMarkdown::OneMarkdown(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::OneMarkdown)
{
ui->setupUi(this);
QString text = "<h1> 一级标题 </h1>";
// ui->textEdit->setPlainText(text);//加载html用setHtml
ui->textEdit->setHtml(text);
// ui->textEdit->setHtml(text);
ui->webEngineView->load(QUrl("file:///data/html/index.html"));
}

OneMarkdown::~OneMarkdown()
Expand All @@ -36,10 +48,19 @@ OneMarkdown::~OneMarkdown()
void OneMarkdown::on_textEdit_textChanged()
{
QString text = ui->textEdit->toPlainText();
QString html = ui->textEdit->toHtml();
// std::string htmlOutput = parser->Parse(markdownInput);
// html = QString::fromStdString(htmlOutput);
ui->textBrowser->setHtml(html);
qDebug() << "OneMarkdown::on_textEdit_" <<text<<endl;
// QString html = ui->textEdit->toHtml();
std::string str = text.toStdString();
std::stringstream markdownInput(str);
// std::shared_ptr<maddy::Parser> parser = std::make_shared<maddy::Parser>();

std::string htmlOutput = parser->Parse(markdownInput);
QString html = QString::fromStdString(htmlOutput);
// ui->textBrowser->setHtml(html);
// ui->webEngineView->load(QUrl("https://baidu.com/"));
// ui->webEngineView->setHtml(html);
// qDebug() << "OneMarkdown::on_textEdit_" <<text<<endl;
// qDebug() << "OneMarkdown::on_textEdit_" <<QString::fromStdString(htmlOutput)<<endl;
// m_view->page()->runJavaScript(QString("showPie(%1)").arg(sHtml));
ui->webEngineView->page()->runJavaScript(QString("add(%1)").arg(text));
}

9 changes: 7 additions & 2 deletions OneMarkdown.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
#define MAINWINDOW_H

#include <QMainWindow>

// #include <QWeb
// #include <QWebChannel>
// #include <QtWebEngineWidgets/QWebEngineView>
#include <QtWebEngineWidgets/QWebEngineView>
#include<QtWebEngine/QtWebEngine>
QT_BEGIN_NAMESPACE
namespace Ui { class OneMarkdown; }
QT_END_NAMESPACE

// QWebEngineView *webView = nullptr;
// QWebChannel *webChannel = nullptr;
class OneMarkdown : public QMainWindow
{
Q_OBJECT
Expand Down
41 changes: 34 additions & 7 deletions OneMarkdown.ui
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,27 @@
</sizepolicy>
</property>
<property name="windowTitle">
<string>OneMarkdown</string>
<string>ProMakr</string>
</property>
<property name="windowIcon">
<iconset>
<normaloff>../../../Python/WeChatMsg/app/data/icon60x60.png</normaloff>../../../Python/WeChatMsg/app/data/icon60x60.png</iconset>
<normaloff>data/icon/logo.png</normaloff>data/icon/logo.png</iconset>
</property>
<property name="iconSize">
<size>
<width>30</width>
<height>30</height>
</size>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonIconOnly</enum>
<enum>Qt::ToolButtonTextBesideIcon</enum>
</property>
<property name="animated">
<bool>true</bool>
</property>
<property name="documentMode">
<bool>false</bool>
</property>
<widget class="QWidget" name="centralwidget">
<property name="enabled">
<bool>true</bool>
Expand Down Expand Up @@ -67,16 +76,27 @@
<widget class="QTextEdit" name="textEdit">
<property name="html">
<string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;title&gt;readme&lt;/title&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'SimSun'; font-size:9pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot; margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:16pt;&quot;&gt;132456&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot; margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:16pt;&quot;&gt;# 132456&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
<item>
<widget class="QTextBrowser" name="textBrowser"/>
<widget class="QWebEngineView" name="webEngineView">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="url">
<url>
<string>about:blank</string>
</url>
</property>
</widget>
</item>
</layout>
</item>
Expand Down Expand Up @@ -149,6 +169,13 @@ p, li { white-space: pre-wrap; }
</property>
</action>
</widget>
<customwidgets>
<customwidget>
<class>QWebEngineView</class>
<extends>QWidget</extends>
<header location="global">QtWebEngineWidgets/QWebEngineView</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
60 changes: 35 additions & 25 deletions OneMarkdown_zh_CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,72 +5,82 @@
<name>OneMarkdown</name>
<message>
<location filename="OneMarkdown.ui" line="20"/>
<location filename="build/OneMarkdown_autogen/include/ui_OneMarkdown.h" line="133"/>
<source>OneMarkdown</source>
<location filename="build/OneMarkdown_autogen/include_Debug/ui_OneMarkdown.h" line="135"/>
<location filename="out/build/x64-Debug/OneMarkdown_autogen/include/ui_OneMarkdown.h" line="135"/>
<source>ProMakr</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="OneMarkdown.ui" line="69"/>
<location filename="build/OneMarkdown_autogen/include/ui_OneMarkdown.h" line="136"/>
<location filename="OneMarkdown.ui" line="78"/>
<location filename="build/OneMarkdown_autogen/include_Debug/ui_OneMarkdown.h" line="138"/>
<location filename="out/build/x64-Debug/OneMarkdown_autogen/include/ui_OneMarkdown.h" line="138"/>
<source>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;title&gt;readme&lt;/title&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:&apos;SimSun&apos;; font-size:9pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot; margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:16pt;&quot;&gt;132456&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot; margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:16pt;&quot;&gt;# 132456&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</source>
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="OneMarkdown.ui" line="96"/>
<location filename="build/OneMarkdown_autogen/include/ui_OneMarkdown.h" line="142"/>
<location filename="OneMarkdown.ui" line="104"/>
<location filename="build/OneMarkdown_autogen/include_Debug/ui_OneMarkdown.h" line="143"/>
<location filename="out/build/x64-Debug/OneMarkdown_autogen/include/ui_OneMarkdown.h" line="143"/>
<source>文件(F)</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="OneMarkdown.ui" line="104"/>
<location filename="build/OneMarkdown_autogen/include/ui_OneMarkdown.h" line="143"/>
<location filename="OneMarkdown.ui" line="112"/>
<location filename="build/OneMarkdown_autogen/include_Debug/ui_OneMarkdown.h" line="144"/>
<location filename="out/build/x64-Debug/OneMarkdown_autogen/include/ui_OneMarkdown.h" line="144"/>
<source>编辑</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="OneMarkdown.ui" line="109"/>
<location filename="build/OneMarkdown_autogen/include/ui_OneMarkdown.h" line="144"/>
<location filename="OneMarkdown.ui" line="117"/>
<location filename="build/OneMarkdown_autogen/include_Debug/ui_OneMarkdown.h" line="145"/>
<location filename="out/build/x64-Debug/OneMarkdown_autogen/include/ui_OneMarkdown.h" line="145"/>
<source>段落</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="OneMarkdown.ui" line="114"/>
<location filename="build/OneMarkdown_autogen/include/ui_OneMarkdown.h" line="145"/>
<location filename="OneMarkdown.ui" line="122"/>
<location filename="build/OneMarkdown_autogen/include_Debug/ui_OneMarkdown.h" line="146"/>
<location filename="out/build/x64-Debug/OneMarkdown_autogen/include/ui_OneMarkdown.h" line="146"/>
<source>格式</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="OneMarkdown.ui" line="119"/>
<location filename="build/OneMarkdown_autogen/include/ui_OneMarkdown.h" line="146"/>
<location filename="OneMarkdown.ui" line="127"/>
<location filename="build/OneMarkdown_autogen/include_Debug/ui_OneMarkdown.h" line="147"/>
<location filename="out/build/x64-Debug/OneMarkdown_autogen/include/ui_OneMarkdown.h" line="147"/>
<source>视图</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="OneMarkdown.ui" line="124"/>
<location filename="build/OneMarkdown_autogen/include/ui_OneMarkdown.h" line="147"/>
<location filename="OneMarkdown.ui" line="132"/>
<location filename="build/OneMarkdown_autogen/include_Debug/ui_OneMarkdown.h" line="148"/>
<location filename="out/build/x64-Debug/OneMarkdown_autogen/include/ui_OneMarkdown.h" line="148"/>
<source>主题</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="OneMarkdown.ui" line="129"/>
<location filename="build/OneMarkdown_autogen/include/ui_OneMarkdown.h" line="148"/>
<location filename="OneMarkdown.ui" line="137"/>
<location filename="build/OneMarkdown_autogen/include_Debug/ui_OneMarkdown.h" line="149"/>
<location filename="out/build/x64-Debug/OneMarkdown_autogen/include/ui_OneMarkdown.h" line="149"/>
<source>帮助</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="OneMarkdown.ui" line="143"/>
<location filename="build/OneMarkdown_autogen/include/ui_OneMarkdown.h" line="134"/>
<location filename="OneMarkdown.ui" line="151"/>
<location filename="build/OneMarkdown_autogen/include_Debug/ui_OneMarkdown.h" line="136"/>
<location filename="out/build/x64-Debug/OneMarkdown_autogen/include/ui_OneMarkdown.h" line="136"/>
<source>新建</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="OneMarkdown.ui" line="148"/>
<location filename="build/OneMarkdown_autogen/include/ui_OneMarkdown.h" line="135"/>
<location filename="OneMarkdown.ui" line="156"/>
<location filename="build/OneMarkdown_autogen/include_Debug/ui_OneMarkdown.h" line="137"/>
<location filename="out/build/x64-Debug/OneMarkdown_autogen/include/ui_OneMarkdown.h" line="137"/>
<source>新建窗口</source>
<translation type="unfinished"></translation>
</message>
Expand Down
Loading

0 comments on commit 0d66f1a

Please sign in to comment.