Create Abstruct base class for renderer. Fix cppcheck.

master
Melroy van den Berg 2020-11-22 00:05:51 +01:00
parent 0c77212398
commit 7f76913183
11 changed files with 305 additions and 24 deletions

245
big.md Normal file
View File

@ -0,0 +1,245 @@
---
__Advertisement :)__
- __[pica](https://nodeca.github.io/pica/demo/)__ - high quality and fast image
resize in browser.
- __[babelfish](https://github.com/nodeca/babelfish/)__ - developer friendly
i18n with plurals support and easy syntax.
You will like those projects!
---
# h1 Heading 8-)
## h2 Heading
### h3 Heading
#### h4 Heading
##### h5 Heading
###### h6 Heading
## Horizontal Rules
___
---
***
## Typographic replacements
Enable typographer option to see result.
(c) (C) (r) (R) (tm) (TM) (p) (P) +-
test.. test... test..... test?..... test!....
!!!!!! ???? ,, -- ---
"Smartypants, double quotes" and 'single quotes'
## Emphasis
**This is bold text**
__This is bold text__
*This is italic text*
_This is italic text_
~~Strikethrough~~
## Blockquotes
> Blockquotes can also be nested...
>> ...by using additional greater-than signs right next to each other...
> > > ...or with spaces between arrows.
## Lists
Unordered
+ Create a list by starting a line with `+`, `-`, or `*`
+ Sub-lists are made by indenting 2 spaces:
- Marker character change forces new list start:
* Ac tristique libero volutpat at
+ Facilisis in pretium nisl aliquet
- Nulla volutpat aliquam velit
+ Very easy!
Ordered
1. Lorem ipsum dolor sit amet
2. Consectetur adipiscing elit
3. Integer molestie lorem at massa
1. You can use sequential numbers...
1. ...or keep all the numbers as `1.`
Start numbering with offset:
57. foo
1. bar
## Code
Inline `code`
Indented code
// Some comments
line 1 of code
line 2 of code
line 3 of code
Block code "fences"
```
Sample text here...
```
Syntax highlighting
``` js
var foo = function (bar) {
return bar++;
};
console.log(foo(5));
```
## Tables
| Option | Description |
| ------ | ----------- |
| data | path to data files to supply the data that will be passed into templates. |
| engine | engine to be used for processing templates. Handlebars is the default. |
| ext | extension to be used for dest files. |
Right aligned columns
| Option | Description |
| ------:| -----------:|
| data | path to data files to supply the data that will be passed into templates. |
| engine | engine to be used for processing templates. Handlebars is the default. |
| ext | extension to be used for dest files. |
## Links
[link text](http://dev.nodeca.com)
[link with title](http://nodeca.github.io/pica/demo/ "title text!")
Autoconverted link https://github.com/nodeca/pica (enable linkify to see)
## Images
![Minion](https://octodex.github.com/images/minion.png)
![Stormtroopocat](https://octodex.github.com/images/stormtroopocat.jpg "The Stormtroopocat")
Like links, Images also have a footnote style syntax
![Alt text][id]
With a reference later in the document defining the URL location:
[id]: https://octodex.github.com/images/dojocat.jpg "The Dojocat"
## Plugins
The killer feature of `markdown-it` is very effective support of
[syntax plugins](https://www.npmjs.org/browse/keyword/markdown-it-plugin).
### [Emojies](https://github.com/markdown-it/markdown-it-emoji)
> Classic markup: :wink: :crush: :cry: :tear: :laughing: :yum:
>
> Shortcuts (emoticons): :-) :-( 8-) ;)
see [how to change output](https://github.com/markdown-it/markdown-it-emoji#change-output) with twemoji.
### [Subscript](https://github.com/markdown-it/markdown-it-sub) / [Superscript](https://github.com/markdown-it/markdown-it-sup)
- 19^th^
- H~2~O
### [\<ins>](https://github.com/markdown-it/markdown-it-ins)
++Inserted text++
### [\<mark>](https://github.com/markdown-it/markdown-it-mark)
==Marked text==
### [Footnotes](https://github.com/markdown-it/markdown-it-footnote)
Footnote 1 link[^first].
Footnote 2 link[^second].
Inline footnote^[Text of inline footnote] definition.
Duplicated footnote reference[^second].
[^first]: Footnote **can have markup**
and multiple paragraphs.
[^second]: Footnote text.
### [Definition lists](https://github.com/markdown-it/markdown-it-deflist)
Term 1
: Definition 1
with lazy continuation.
Term 2 with *inline markup*
: Definition 2
{ some code, part of Definition 2 }
Third paragraph of definition 2.
_Compact style:_
Term 1
~ Definition 1
Term 2
~ Definition 2a
~ Definition 2b
### [Abbreviations](https://github.com/markdown-it/markdown-it-abbr)
This is HTML abbreviation example.
It converts "HTML", but keep intact partial entries like "xxxHTMLyyy" and so on.
*[HTML]: Hyper Text Markup Language
### [Custom containers](https://github.com/markdown-it/markdown-it-container)
::: warning
*here be dragons*
:::

Binary file not shown.

After

Width:  |  Height:  |  Size: 80 KiB

View File

@ -37,8 +37,11 @@ set(SOURCES
main.cc
md-parser.cc
md-parser.h
md-render.cc
md-render.h
renderer-interface.h
qt-renderer.cc
qt-renderer.h
imgui-renderer.cc
imgui-renderer.h
mainwindow.cc
mainwindow.h
scene.h

0
src/imgui-renderer.cc Normal file
View File

0
src/imgui-renderer.h Normal file
View File

View File

@ -1,7 +1,7 @@
#include "mainwindow.h"
#include "scene.h"
#include "md-parser.h"
#include "md-render.h"
#include "qt-renderer.h"
#include <chrono>
#include <iostream>
@ -61,7 +61,8 @@ MainWindow::MainWindow()
void MainWindow::setupParser()
{
parser = new Parser();
renderer = new Renderer(scene);
renderer = new QtRenderer();
renderer->setScene(scene);
std::string exePath = n_fs::current_path().string();
std::string htmlOutput = "";

View File

@ -5,7 +5,7 @@
class Scene;
class Parser;
class Renderer;
class QtRenderer;
QT_BEGIN_NAMESPACE
class QAction;
class QTextEdit;
@ -28,7 +28,7 @@ private:
QTextEdit *textEdit;
Scene *scene;
Parser *parser;
Renderer *renderer;
QtRenderer *renderer;
void setupParser();
// void resizeEvent(QResizeEvent *);

View File

@ -6,7 +6,7 @@
#include <render.h>
/**
* Parser class will parse the content to a AST model
* \class Parser Markdown parser class, parse the content to an AST model
*/
class Parser
{

View File

@ -1,4 +1,4 @@
#include "md-render.h"
#include "qt-renderer.h"
#include "scene.h"
#include "cmark-gfm.h"
@ -9,8 +9,8 @@
#include <QGraphicsSimpleTextItem>
#include <QPainter>
Renderer::Renderer(Scene* scene) :
scene(scene),
QtRenderer::QtRenderer():
scene(NULL),
defaultFontSize(12),
sceneMarginX(3.0),
sceneMarginY(3.0),
@ -32,10 +32,15 @@ Renderer::Renderer(Scene* scene) :
font->setFamily(fontFamilty);
}
void QtRenderer::setScene(Scene* scene)
{
this->scene = scene;
}
/**
* Render the whole document to scene/screen
*/
void Renderer::renderDocument(cmark_node *root, int width)
void QtRenderer::renderDocument(cmark_node *root, int width)
{
cmark_event_type ev_type;
cmark_iter *iter = cmark_iter_new(root);
@ -49,7 +54,7 @@ void Renderer::renderDocument(cmark_node *root, int width)
* Calculates the locations, render and paint the content/objects
* to a QGraphicsScene
*/
void Renderer::renderNode(cmark_node *node, cmark_event_type ev_type)
void QtRenderer::renderNode(cmark_node *node, cmark_event_type ev_type)
{
bool entering = (ev_type == CMARK_EVENT_ENTER);
@ -203,16 +208,16 @@ void Renderer::renderNode(cmark_node *node, cmark_event_type ev_type)
}
}
QRectF const Renderer::drawText(const QString& text)
QRectF const QtRenderer::drawText(const QString& text)
{
// We can still extend the QGraphicsSimpleTextItem class (or QAbstractGraphicsShapeItem) and override paint method.
// Or just use QPainter with a paint device (like QWidgets), to have maximal control.
QGraphicsSimpleTextItem *textItem = new QGraphicsSimpleTextItem(text);
font->setBold(bold);
font->setItalic(italic);
//font->setBold(bold);
//font->setItalic(italic);
if (headingLevel > 0) {
font->setBold(true);
//font->setBold(true);
switch(headingLevel) {
case 1:
font->setPixelSize(24);
@ -245,7 +250,7 @@ QRectF const Renderer::drawText(const QString& text)
return textItem->boundingRect();
}
QRectF const Renderer::drawBullet()
QRectF const QtRenderer::drawBullet()
{
QGraphicsSimpleTextItem *bullet = new QGraphicsSimpleTextItem("\u2022");
bullet->setFont(*font);

View File

@ -1,23 +1,27 @@
#ifndef MD_RENDER_H
#define MD_RENDER_H
#ifndef QT_RENDER_H
#define QT_RENDER_H
#include <cmark-gfm.h>
#include <render.h>
#include <QtGlobal>
#include <QString>
#include "renderer-interface.h"
class Scene;
class QRectF;
class QFont;
/**
* The Renderer will use Qt to render AST directly to a QGraphicsScene
* \class QtRenderer Class will use Qt to render AST object to a QGraphicsScene
*/
class Renderer
class QtRenderer : public RendererI
{
public:
explicit Renderer(Scene *scene);
void renderDocument(cmark_node *root, int width = 0);
explicit QtRenderer();
void setScene(Scene *scene) override;
void setUnknownYet() override {}; // No implementation
void renderDocument(cmark_node *root, int width = 0) override;
private:
Scene *scene;
@ -37,9 +41,13 @@ private:
qreal paragraphHeightOffset;
qreal headingHeightOffset;
qreal listXOffset;
qreal bulletWithTemp;
// Copy contructor (not used/non-copyable)
QtRenderer(const QtRenderer& _);
// Copy assignment (not used/not assignable)
QtRenderer& operator=(const QtRenderer& _);
void renderNode(cmark_node *node, cmark_event_type ev_type);
QRectF const drawText(const QString& text);
QRectF const drawBullet();

19
src/renderer-interface.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef RENDER_INTERFACE_H
#define RENDER_INTERFACE_H
class Scene;
struct cmark_node;
/**
* \class RendererI Renderer Abstract Base Class
*/
class RendererI
{
public:
// TODO: Combien those two sets to 1, create an abstract class for setting a scene in Qt and/or Imgui.
virtual void setScene(Scene *scene) = 0;// For Qt
virtual void setUnknownYet() = 0; // For Imgui
virtual void renderDocument(cmark_node *root, int width = 0) = 0;
};
#endif