1 #include "titleblocktemplaterenderer.h"
2 #include "titleblocktemplate.h"
3 
4 /**
5 	Constructor
6 	@param parnet Parent QObject of this renderer
7 */
TitleBlockTemplateRenderer(QObject * parent)8 TitleBlockTemplateRenderer::TitleBlockTemplateRenderer(QObject *parent) :
9 	QObject(parent),
10 	m_titleblock_template(nullptr),
11 	m_use_cache(true),
12 	m_last_known_titleblock_width(-1)
13 {
14 }
15 
16 /**
17 	Destructor
18 */
~TitleBlockTemplateRenderer()19 TitleBlockTemplateRenderer::~TitleBlockTemplateRenderer() {
20 }
21 
22 /**
23 	@return the titleblock template used for the rendering
24 */
titleBlockTemplate() const25 const TitleBlockTemplate *TitleBlockTemplateRenderer::titleBlockTemplate() const {
26 	return(m_titleblock_template);
27 }
28 
29 /**
30 	@param titleblock_template TitleBlock template to render.
31 */
setTitleBlockTemplate(const TitleBlockTemplate * titleblock_template)32 void TitleBlockTemplateRenderer::setTitleBlockTemplate(const TitleBlockTemplate *titleblock_template) {
33 	if (titleblock_template != m_titleblock_template) {
34 		m_titleblock_template = titleblock_template;
35 		invalidateRenderedTemplate();
36 	}
37 }
38 
39 /**
40  * @brief TitleBlockTemplateRenderer::setContext
41  * @param context : Context to use when rendering the titleblock
42  */
setContext(const DiagramContext & context)43 void TitleBlockTemplateRenderer::setContext(const DiagramContext &context) {
44 	m_context = context;
45 	invalidateRenderedTemplate();
46 }
47 
48 /**
49  * @brief TitleBlockTemplateRenderer::context
50  * @return the current diagram context use when render the titleblock
51  */
context() const52 DiagramContext TitleBlockTemplateRenderer::context() const {
53 	return  m_context;
54 }
55 
56 /**
57 	@return the height of the rendered template, or -1 if no template has been
58 	set for this renderer.
59 	@see TitleBlockTemplate::height()
60 */
height() const61 int TitleBlockTemplateRenderer::height() const {
62 	if (!m_titleblock_template) return(-1);
63 	return(m_titleblock_template -> height());
64 }
65 
66 /**
67 	Render the titleblock.
68 	@param provided_painter QPainter to use to render the titleblock.
69 	@param titleblock_width The total width of the titleblock to render
70 */
render(QPainter * provided_painter,int titleblock_width)71 void TitleBlockTemplateRenderer::render(QPainter *provided_painter, int titleblock_width) {
72 	if (!m_titleblock_template) return;
73 
74 	if (m_use_cache) {
75 		// Do we really need to calculate all this again?
76 		if (titleblock_width != m_last_known_titleblock_width || m_rendered_template.isNull()) {
77 			renderToQPicture(titleblock_width);
78 		}
79 
80 		provided_painter -> save();
81 		m_rendered_template.play(provided_painter);
82 		provided_painter -> restore();
83 	} else {
84 		m_titleblock_template -> render(*provided_painter, m_context, titleblock_width);
85 	}
86 }
87 
88 
renderDxf(QRectF & title_block_rect,int titleblock_width,QString & file_path,int color)89 void TitleBlockTemplateRenderer::renderDxf(QRectF &title_block_rect, int titleblock_width, QString &file_path, int color) {
90 	if (!m_titleblock_template) return;
91 	m_titleblock_template -> renderDxf(title_block_rect, m_context, titleblock_width, file_path, color);
92 }
93 
94 /**
95 	Renders the titleblock to the internal QPicture
96 	@param titleblock_width Width of the titleblock to render
97 */
renderToQPicture(int titleblock_width)98 void TitleBlockTemplateRenderer::renderToQPicture(int titleblock_width) {
99 	if (!m_titleblock_template) return;
100 
101 	// we render the template on our internal QPicture
102 	QPainter painter(&m_rendered_template);
103 
104 	m_titleblock_template -> render(painter, m_context, titleblock_width);
105 
106 	// memorize the last known width
107 	m_last_known_titleblock_width = titleblock_width;
108 }
109 
110 /**
111 	Invalidates the previous rendering of the template by resetting the internal
112 	QPicture.
113 */
invalidateRenderedTemplate()114 void TitleBlockTemplateRenderer::invalidateRenderedTemplate() {
115 	m_rendered_template = QPicture();
116 }
117 
118 /**
119 	@param use_cache true for this renderer to use its QPicture-based cache,
120 	false otherwise.
121 */
setUseCache(bool use_cache)122 void TitleBlockTemplateRenderer::setUseCache(bool use_cache) {
123 	m_use_cache = use_cache;
124 }
125 
126 /**
127 	@return true if this renderer uses its QPicture-based cache, false
128 	otherwise.
129 */
useCache() const130 bool TitleBlockTemplateRenderer::useCache() const {
131 	return(m_use_cache);
132 }
133 
134