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