tuchulcha  0.10.1
Graphical Workflow Configuration Editor
DocGenerator.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2009 Jens-Malte Gottfried
2 
3  This file is part of Tuchulcha.
4 
5  Tuchulcha is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  Tuchulcha is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with Tuchulcha. If not, see <http://www.gnu.org/licenses/>.
17 */
24 #include "DocGenerator.h"
25 #include <QUrl>
26 #include <QDir>
27 #include <QFile>
28 #include <QtDebug>
29 #include <QTextBrowser>
30 
31 #include "ParameterFileModel.h"
32 #include "FileManager.h"
33 #include "MetaData.h"
34 
35 DocGenerator::DocGenerator(QTextBrowser* viewer, QObject* myParent) :
36  QObject(myParent),
37  _viewer(viewer),
38  _meta(0) {
39  Q_ASSERT(_viewer);
40 
41  updateMetaData() ;
42 
43  // load stylesheet
44  QFile stylesheetFile(":/help/styles.css");
45  if (stylesheetFile.open(QIODevice::ReadOnly | QIODevice::Text))
46  _stylesheet = stylesheetFile.readAll();
47  else
48  qWarning() << "Stylesheet could not be loaded.";
49  stylesheetFile.close();
50 
51 
52  // load footer
53  QFile footerFile(":/help/footer.txt");
54  if (footerFile.open(QIODevice::ReadOnly | QIODevice::Text))
55  _footer = footerFile.readAll();
56  else
57  qWarning() << "Footer file could not be loaded.";
58  footerFile.close();
59 }
60 
61 DocGenerator::~DocGenerator() {
62  delete _meta ;
63 }
64 
66  showDocPage(":/help/help.txt");
67 }
68 
70  showDocPage(":/help/start.txt");
71 }
72 
73 void DocGenerator::showDocPage(const QString& fileName) {
74  _helpDoc = "";
75 
76  QFile docPage(fileName);
77  if (docPage.open(QIODevice::ReadOnly | QIODevice::Text)) {
78  _viewer->setHtml(QString(
79  "<html><head>"
80  "<meta name=\"qrichtext\" content=\"1\"/>"
81  "<style type=\"text/css\">%1</style>"
82  "</head><body>%2\n%3</body></html>")
83  .arg(_stylesheet)
84  .arg(QString::fromUtf8(docPage.readAll()))
85  .arg(_footer));
86  }
87  else {
88  qWarning() << tr("DocPage file %1 could not be loaded.")
89  .arg(fileName);
90  }
91  docPage.close();
92 }
93 
94 void DocGenerator::showDocString(const QString& doc) {
95  // check if content update needed
96  if (doc == _helpDoc)
97  return;
98 
99  // encapsulation into html document (using the stylesheet)
100  QString html = QString(
101  "<html><head>"
102  "<meta name=\"qrichtext\" content=\"1\"/>"
103  "<style type=\"text/css\">%1</style>"
104  "</head><body><div><p>%2</p></div>\n%3</body></html>")
105  .arg(_stylesheet).arg(_helpDoc=doc).arg(_footer);
106 
107  // display
108  _viewer->setHtml(html);
109 }
110 
112  delete _meta ;
113  _meta = 0 ;
114  _meta = new MetaData(FileManager::instance().classesFile()) ;
115 }
116 
117 QString DocGenerator::_docList(QStringList parList,
118  QString className, QString slotType) const {
119  QString ret;
120  QStringList::const_iterator parIter;
121 
122  if(parList.size()) {
123  ret += "<p><ul>";
124  for(parIter=parList.begin(); parIter!=parList.end(); parIter++) {
125  ret += "<tr>";
126 
127  // show parameter name and type
128  ret += "<td class=\"leftcol\"></td>";
129  QString parType = _meta->getType(*parIter, className);
130  if (parType.contains(QRegExp("^\\s*\\{\\s*\\w.*\\}\\s*$")))
131  parType = "Selection";
132  parType.replace("<", "&lt;").replace(">", "&gt;");
133  ret += QString("<td class=\"dtype firstrow\">%1</td>")
134  .arg(parType);
135  ret += QString("<td class=\"firstrow\"><span class=\"parname\">"
136  "%1</span></td>\n")
137  .arg(*parIter);
138 
139  // show default value, if any
140  QString def = _meta->getDefault(*parIter, className);
141  ret += "<td class=\"firstrow\">";
142 
143  if(!def.isEmpty())
144  ret += "<em>("+tr("default: \"%1\"").arg(def)+")</em>";
145 
146  // show slot flags (if necessary)
147  QString flags;
148  if (slotType == "in") {
149  if(_meta->isMultiSlot(*parIter, className))
150  flags += "MultiSlot";
151  if(_meta->isOptionalSlot(*parIter, className)) {
152  if(!flags.isEmpty())
153  flags += "; ";
154  flags += "optional";
155  }
156  }
157 
158  if (slotType == "out") {
159  if(!_meta->isMultiSlot(*parIter, className))
160  flags += "SingleSlot";
161  if(!_meta->isOptionalSlot(*parIter, className)) {
162  if(!flags.isEmpty())
163  flags += "; ";
164  flags += "required";
165  }
166  }
167  if(!flags.isEmpty())
168  ret += QString("<em>(%1)</em>").arg(flags);
169 
170  ret += "</td></tr>";
171 
172  // show documentation
173  ret += "<tr><td class=\"leftcol\"></td><td></td>";
174  ret += QString("<td colspan=\"2\">%1</td></tr><tr></tr>")
175  .arg(_meta->getDocString(*parIter, className));
176  }
177  }
178  else
179  ret += "<tr><td class=\"leftcol\"></td>"
180  "<td colspan=\"3\">" + tr("(none)") + "</td></tr>\n";
181  return ret;
182 }
183 
184 void DocGenerator::showClassDoc(const QString& className) {
185  QString page =
186  "<h1>"+tr("ClassDocumentation: %1").arg(className)+"</h1>\n<h2>"
187  + tr("Description")
188  + QString("</h2>\n<p>%1</p>\n")
189  .arg(_meta->getDocString("", className));
190 
191  QString docFileName = _meta->getDocFile("", className);
192 
193  if(!docFileName.isEmpty()) {
194  // load docfile and add content
195  QFile docFile(QDir::toNativeSeparators(FileManager::instance()
196  .configDir().absoluteFilePath(docFileName)));
197  if (docFile.open(QIODevice::ReadOnly | QIODevice::Text))
198  page += docFile.readAll();
199  else
200  qWarning() << tr("Documentation file %1 could not be loaded.")
201  .arg(docFileName);
202  docFile.close();
203  }
204 
205  QStringList parList;
206 
207  page += "<table class=\"parlist\">";
208 
209  // input slots
210  parList = _meta->getInputs(className);
211  page += "<tr><td colspan=\"5\"><h2>" + tr("input slots")
212  + "</h2></td></tr>\n";
213  page += _docList(parList, className, "in");
214 
215  // output slots
216  parList = _meta->getOutputs(className);
217  page += "<tr><td colspan=\"5\"><h2>" + tr("output slots")
218  + "</h2></td></tr>\n";
219  page += _docList(parList, className, "out");
220 
221  // parameters
222  parList = _meta->getParameters(className);
223  page += "<tr><td colspan=\"5\"><h2>"+tr("parameters")+"</h2></td></tr>\n";
224  page += _docList(parList, className);
225 
226  page += "</table>";
227 
228  showDocString(page);
229 }
Declaration of class FileManager.
bool isMultiSlot(QString slotName, QString className) const
Check if some slot is a multi slot.
Definition: MetaData.cpp:125
Declaration of class ParameterFileModel.
void showDocString(const QString &doc)
Show the given html-formatted string.
QString getType(QString parName, QString className) const
Get type of some parameter or slot.
Definition: MetaData.cpp:77
static const FileManager & instance()
get a reference to the file Manager instance
Definition: FileManager.cpp:70
Implementation of class ParameterFileModel.
QStringList getParameters(QString className) const
get parameters of some given object
Definition: MetaData.cpp:72
Handle metadata management for ParameterFileModel classes.
Definition: MetaData.h:31
QString _docList(QStringList parList, QString className, QString slotType="") const
Generate documentation for the given list of parameters.
Declaration of class DocGenerator.
QString _footer
footer for doc pages
Definition: DocGenerator.h:95
QString getDefault(QString parName, QString className) const
get default value for some editable parameter
Definition: MetaData.cpp:86
bool isOptionalSlot(QString slotName, QString className) const
Check if some slot is optional.
Definition: MetaData.cpp:117
QString _helpDoc
current docstring in help browser
Definition: DocGenerator.h:98
QString getDocString(QString parName, QString className) const
get docstring for some parameter or some class.
Definition: MetaData.cpp:93
void showHelp()
show help page in help browser
QTextBrowser * _viewer
pointer to used viewer instance
Definition: DocGenerator.h:89
void showIntro()
show introduction message in help browser
DocGenerator(QTextBrowser *viewer, QObject *parent=0)
Default constructor.
void showClassDoc(const QString &className)
show documentation page
QStringList getInputs(QString className) const
get output slots of some given object
Definition: MetaData.cpp:65
QString _stylesheet
stylesheet for help doc
Definition: DocGenerator.h:92
QString getDocFile(QString parName, QString className) const
get filename for some parameter or class documentation.
Definition: MetaData.cpp:102
void updateMetaData()
update internal MetaData Object, call when plugins are updated
MetaData * _meta
MetaData of all loaded Plugins.
Definition: DocGenerator.h:103
QStringList getOutputs(QString className) const
get input slots of some given object
Definition: MetaData.cpp:59
void showDocPage(const QString &fileName)
Show documentation page.