Engauge Digitizer 2
Loading...
Searching...
No Matches
ChecklistGuideBrowser.cpp
Go to the documentation of this file.
1/******************************************************************************************************
2 * (C) 2014 markummitchell@github.com. This file is part of Engauge Digitizer, which is released *
3 * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4 * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission. *
5 ******************************************************************************************************/
6
8#include "ChecklistTemplate.h"
9#include "CmdMediator.h"
10#include "Document.h"
11#include "EngaugeAssert.h"
12#include "Logger.h"
13#include <QDebug>
14#include <QRegularExpression>
15
16const int MIN_WIDTH_BROWSER = 340; // Make just big enough that each "More..." appears on same line
17
19{
20 setOpenLinks (false); // Disable automatic link following
21 setMinimumWidth(MIN_WIDTH_BROWSER);
22
23 connect (this, SIGNAL (anchorClicked (const QUrl &)), this, SLOT (slotAnchorClicked (const QUrl &)));
24}
25
26QString ChecklistGuideBrowser::ahref (QString &html,
27 const QString &name) const
28{
29
30 LOG4CPP_INFO_S ((*mainCat)) << "ChecklistGuideBrowser::bindToDocument";
31
32 QString expression = QString("%1%2%3").arg(TAG_AHREF_DELIMITER_START,
33 name,
35
36 QString link;
37 if (name == m_anchor) {
38 // Click on this hyperlink to reload the page without details under this link, since anchor is empty
39 link = QString("<a href="
40 "#"
41 ">Less ...</a>");
42
43 } else {
44 // Click on this hyperlink to reload the page with details under this link
45 link = QString("<a href="
46 "#%1"
47 ">More ...</a>")
48 .arg(name);
49 }
50
51 html.replace (expression, link);
52
53 return html;
54}
55
56void ChecklistGuideBrowser::check (QString &html,
57 const QString &anchor,
58 bool isChecked) const
59{
60 LOG4CPP_INFO_S ((*mainCat)) << "ChecklistGuideBrowser::check";
61
62 QString tag = QString("%1%2%3").arg(TAG_ANCHOR_DELIMITER_START,
63 anchor,
65
66 if (isChecked) {
67 html.replace(tag,
68 "<img src="
69 ":/engauge/img/16-checked.png"
70 ">");
71 } else {
72 html.replace(tag,
73 "<img src="
74 ":/engauge/img/16-unchecked.png"
75 ">");
76 }
77}
78
79void ChecklistGuideBrowser::divHide (QString &html,
80 const QString &anchor) const
81{
82 LOG4CPP_INFO_S ((*mainCat)) << "ChecklistGuideBrowser::divHide"
83 << " anchor=" << anchor.toLatin1().data();
84
85 // Remove everything between the start and end tags, inclusive
86 QString expression = QString("\\%1%2\\%3.*\\%4%5\\%6")
88 anchor,
91 anchor,
93 QRegularExpression regExp(expression);
94 html.replace(regExp, "");
95}
96
97void ChecklistGuideBrowser::divShow(QString &html, const QString &anchor) const
98{
99 LOG4CPP_INFO_S ((*mainCat)) << "ChecklistGuideBrowser::divShow"
100 << " anchor=" << anchor.toLatin1().data();
101
102 if (!anchor.isEmpty ()) {
103
104 // Remove the start and end tags, but leave the text in between
105 QString expressionStart = QString("\\%1%2\\%3")
107 QString expressionEnd = QString("\\%1%2\\%3")
109 QRegularExpression regExpStart(expressionStart);
110 QRegularExpression regExpEnd(expressionEnd);
111 html.replace (regExpStart, "");
112 html.replace (regExpEnd, "");
113 }
114}
115
116QString ChecklistGuideBrowser::processAhrefs (const QString &htmlBefore)
117{
118 LOG4CPP_INFO_S ((*mainCat)) << "ChecklistGuideBrowser::processAhrefs";
119
120 QString html = htmlBefore;
121
122 // Background
123 ahref (html, NAME_BACKGROUND);
124
125 // Fixed axis point hrefs
126 ahref (html, NAME_AXIS1);
127 ahref (html, NAME_AXIS2);
128 ahref (html, NAME_AXIS3);
129
130 // Curves
131 QStringList::const_iterator itr;
132 for (itr = m_curveNames.begin(); itr != m_curveNames.end(); itr++) {
133
134 QString curveName = *itr;
135 ahref (html, curveName);
136 }
137
138 // Export
139 ahref (html, NAME_EXPORT);
140
141 return html;
142}
143
144QString ChecklistGuideBrowser::processCheckboxes (const QString &htmlBefore)
145{
146 LOG4CPP_INFO_S ((*mainCat)) << "ChecklistGuideBrowser::processCheckboxes";
147
148 QString html = htmlBefore;
149
150 check (html, NAME_BACKGROUND, m_checkedTags.contains (NAME_BACKGROUND));
151
152 check (html, NAME_AXIS1, m_checkedTags.contains (NAME_AXIS1));
153 check (html, NAME_AXIS2, m_checkedTags.contains (NAME_AXIS2));
154 check (html, NAME_AXIS3, m_checkedTags.contains (NAME_AXIS3));
155
156 // Curves
157 QStringList::const_iterator itr;
158 for (itr = m_curveNames.begin(); itr != m_curveNames.end(); itr++) {
159
160 QString curveName = *itr;
161 check (html, curveName, m_checkedTags.contains (curveName));
162 }
163
164 check (html, NAME_EXPORT, m_checkedTags.contains (NAME_EXPORT));
165
166 return html;
167}
168
169QString ChecklistGuideBrowser::processDivs (const QString &htmlBefore)
170{
171 LOG4CPP_INFO_S ((*mainCat)) << "ChecklistGuideBrowser::processDivs";
172
173 QString html = htmlBefore;
174
175 // Show div associated with anchor. Since this removes the tags, applying divHide below
176 // will have no effect, so we apply divHide to everything
177 divShow (html, m_anchor);
178
179 // Background
180 divHide (html, NAME_BACKGROUND);
181
182 // Fixed axis point tags
183 divHide (html, NAME_AXIS1);
184 divHide (html, NAME_AXIS2);
185 divHide (html, NAME_AXIS3);
186
187 // Curve name tags
188 QStringList::const_iterator itr;
189 for (itr = m_curveNames.begin(); itr != m_curveNames.end(); itr++) {
190
191 QString curveName = *itr;
192 divHide (html, curveName);
193 }
194
195 divHide (html, NAME_EXPORT);
196
197 return html;
198}
199
200void ChecklistGuideBrowser::refresh ()
201{
202 LOG4CPP_INFO_S ((*mainCat)) << "ChecklistGuideBrowser::refresh";
203
204 QString html = m_templateHtml;
205
206 html = processAhrefs (html);
207 html = processCheckboxes (html);
208 html = processDivs (html);
209
210 QTextBrowser::setHtml (html);
211}
212
213void ChecklistGuideBrowser::repopulateCheckedTags (const CmdMediator &cmdMediator,
214 bool documentIsExported)
215{
216 LOG4CPP_INFO_S ((*mainCat)) << "ChecklistGuideBrowser::repopulateCheckedTags";
217
218 m_checkedTags.clear();
219
220 if (cmdMediator.document().curveAxes().numPoints() > 0) {
221 m_checkedTags [NAME_AXIS1] = true;
222 }
223
224 if (cmdMediator.document().curveAxes().numPoints() > 1) {
225 m_checkedTags [NAME_AXIS2] = true;
226 }
227
228 if (cmdMediator.document().curveAxes().numPoints() > 2) {
229 m_checkedTags [NAME_AXIS3] = true;
230 }
231
232 // Curves
233 QStringList::const_iterator itr;
234 for (itr = m_curveNames.begin(); itr != m_curveNames.end(); itr++) {
235
236 QString curveName = *itr;
237 if (cmdMediator.document().curvesGraphsNumPoints (curveName) > 0) {
238 m_checkedTags [curveName] = true;
239 }
240 }
241
242 // The export case is very tricky. The modified/dirty flag tells us when there are unsaved points,
243 // but for some reason the value returned by CmdMediator.isModified (which is QUndoStack::isClean)
244 // is often wrong at this point in execution. So we use a different trick - asking MainWindow if file has
245 // been saved
246 if (documentIsExported) {
247 m_checkedTags [NAME_EXPORT] = true;
248 }
249}
250
252 const QStringList &curveNames)
253{
254 m_templateHtml = html;
255 m_curveNames = curveNames;
256
257 refresh();
258}
259
260void ChecklistGuideBrowser::slotAnchorClicked (const QUrl &url)
261{
262 LOG4CPP_INFO_S ((*mainCat)) << "ChecklistGuideBrowser::slotAnchorClicked";
263
264 m_anchor = "";
265 if (url.hasFragment ()) {
266 m_anchor = url.fragment ();
267 }
268
269 refresh();
270}
271
273 bool documentIsExported)
274{
275 LOG4CPP_INFO_S ((*mainCat)) << "ChecklistGuideBrowser::update";
276
277 repopulateCheckedTags(cmdMediator,
278 documentIsExported);
279
280 refresh();
281}
const int MIN_WIDTH_BROWSER
const QString TAG_DIV_DELIMITER_END
const QString NAME_EXPORT
const QString NAME_AXIS1
const QString NAME_AXIS2
const QString TAG_AHREF_DELIMITER_END
const QString NAME_BACKGROUND
const QString TAG_DIV_DELIMITER_START_SLASH
const QString NAME_AXIS3
const QString TAG_ANCHOR_DELIMITER_END
const QString TAG_AHREF_DELIMITER_START
const QString TAG_DIV_DELIMITER_START
const QString TAG_ANCHOR_DELIMITER_START
log4cpp::Category * mainCat
Definition Logger.cpp:14
ChecklistGuideBrowser()
Single constructor.
void update(const CmdMediator &cmdMediator, bool documentIsExported)
Update using current CmdMediator/Document state.
virtual void setTemplateHtml(const QString &html, const QStringList &curveNames)
Populate the browser with template html. The template html will be converted to real html.
Command queue stack.
Definition CmdMediator.h:24
Document & document()
Provide the Document to commands, primarily for undo/redo processing.
int numPoints() const
Number of points.
Definition Curve.cpp:434
const Curve & curveAxes() const
Get method for axis curve.
Definition Document.cpp:327
int curvesGraphsNumPoints(const QString &curveName) const
See CurvesGraphs::curvesGraphsNumPoints.
Definition Document.cpp:362
#define LOG4CPP_INFO_S(logger)
Definition convenience.h:18