/
DocumentationWidget.cpp
286 lines (257 loc) · 10.1 KB
/
DocumentationWidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Linkoping University,
* Department of Computer and Information Science,
* SE-58183 Linkoping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3
* AND THIS OSMC PUBLIC LICENSE (OSMC-PL).
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S
* ACCEPTANCE OF THE OSMC PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linkoping University, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
* OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
* Main Authors 2010: Syed Adeel Asghar, Sonia Tariq
* Contributors 2011: Abhinn Kothari
*/
#include "DocumentationWidget.h"
DocumentationWidget::DocumentationWidget(MainWindow *pParent)
: QWidget(pParent)
{
setObjectName(tr("DocumentationWidget"));
mpParentMainWindow = pParent;
mpDocumentationViewer = new DocumentationViewer(this);
mpDocumentationEditor = new DocumentationEditor(this);
setIsCustomModel(false);
mpHeadingLabel = new QLabel;
mpHeadingLabel->setFont(QFont("", Helper::headingFontSize - 5));
mpHeadingLabel->setAlignment(Qt::AlignTop);
mpPixmapLabel = new QLabel;
mpPixmapLabel->setObjectName(tr("componentPixmap"));
mpPixmapLabel->setMaximumSize(QSize(86, 86));
mpPixmapLabel->setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);
mpPixmapLabel->setAlignment(Qt::AlignCenter);
mpEditButton = new QPushButton(tr("Edit"));
mpEditButton->setAutoDefault(true);
mpEditButton->setMaximumSize(QSize(100,20));
connect(mpEditButton, SIGNAL(clicked()), SLOT(editDocumentation()));
mpSaveButton = new QPushButton(tr("Save"));
mpSaveButton->setAutoDefault(false);
mpSaveButton->setMaximumSize(QSize(100,20));
connect(mpSaveButton, SIGNAL(clicked()), SLOT(saveChanges()));
QHBoxLayout *horizontalLayout = new QHBoxLayout;
horizontalLayout->addWidget(mpPixmapLabel);
horizontalLayout->addWidget(mpHeadingLabel);
horizontalLayout->addWidget(mpSaveButton);
horizontalLayout->addWidget(mpEditButton);
QVBoxLayout *verticalLayout = new QVBoxLayout;
verticalLayout->addLayout(horizontalLayout);
verticalLayout->addWidget(mpDocumentationViewer);
verticalLayout->addWidget(mpDocumentationEditor);
setLayout(verticalLayout);
}
DocumentationWidget::~DocumentationWidget()
{
delete mpDocumentationViewer;
delete mpDocumentationEditor;
}
void DocumentationWidget::show(QString className)
{
mClassName=className;
mpHeadingLabel->setText(className);
LibraryComponent *libraryComponent = mpParentMainWindow->mpLibrary->getLibraryComponentObject(className);
if(libraryComponent)
{
mpPixmapLabel->setVisible(true);
mpPixmapLabel->setPixmap(libraryComponent->getComponentPixmap(QSize(75, 75)));
}
else
{
mpPixmapLabel->setVisible(false);
}
// show edit and save buttons if show is called for a custom model
if (isCustomModel())
{
mpEditButton->setVisible(true);
mpEditButton->setDisabled(false);
mpSaveButton->setVisible(true);
mpSaveButton->setDisabled(true);
}
else
{
mpEditButton->setVisible(false);
mpSaveButton->setVisible(false);
}
QString documentation = mpParentMainWindow->mpOMCProxy->getDocumentationAnnotation(className);
mpDocumentationViewer->setHtml(documentation, mpDocumentationViewer->getBaseUrl());
mpDocumentationViewer->setVisible(true);
mpDocumentationEditor->setVisible(false);
}
void DocumentationWidget::showDocumentationEditView(QString className)
{
mpDocumentationViewer->hide();
// get the already existing documentation text of the model
mpDocumentationEditor->toPlainText();
if (!mpParentMainWindow->mpOMCProxy->getDocumentationAnnotation(className).isEmpty())
mpDocumentationEditor->setPlainText(mpParentMainWindow->mpOMCProxy->getDocumentationAnnotation(className));
else
mpDocumentationEditor->setPlainText("<html>\n\n</html>");
mpDocumentationEditor->setFocus();
mpDocumentationEditor->show();
}
void DocumentationWidget::setIsCustomModel(bool isCustomModel)
{
mIsCustomModel = isCustomModel;
}
bool DocumentationWidget::isCustomModel()
{
return mIsCustomModel;
}
void DocumentationWidget::editDocumentation()
{
showDocumentationEditView(mClassName);
mpEditButton->setDisabled(true);
mpSaveButton->setDisabled(false);
}
void DocumentationWidget::saveChanges()
{
QString doc = mpDocumentationEditor->toPlainText();
if(doc.startsWith("<html>", Qt::CaseInsensitive) && doc.endsWith("</html>", Qt::CaseInsensitive))
{
mpParentMainWindow->mpOMCProxy->addClassAnnotation(mClassName,"annotate=Documentation(info = \""+doc+"\")");
show(mClassName);
}
else
{
QString message = QString(GUIMessages::getMessage(GUIMessages::INCORRECT_HTML_TAGS));
mpParentMainWindow->mpMessageWidget->addGUIProblem(new ProblemItem("", false, 0, 0, 0, 0, message, Helper::scriptingKind,
Helper::errorLevel, 0, mpParentMainWindow->mpMessageWidget->mpProblem));
}
}
void DocumentationWidget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter (this);
painter.setPen(Qt::gray);
QRect rectangle = this->rect();
rectangle.setWidth(this->rect().width() - 2);
rectangle.setHeight(this->rect().height() - 2);
painter.drawRect(rectangle);
QWidget::paintEvent(event);
}
DocumentationEditor::DocumentationEditor(DocumentationWidget *pParent)
: QTextEdit(pParent)
{
mpParentDocumentationWidget = pParent;
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
setTabStopWidth(Helper::tabWidth);
setObjectName(tr("DocumentationEditor"));
setAutoFormatting(QTextEdit::AutoNone);
setAcceptRichText(false);
this->setFontFamily(qApp->font().family()); // get system font
this->setFontPointSize(10.0);
}
DocumentationViewer::DocumentationViewer(DocumentationWidget *pParent)
: QWebView(pParent)
{
mpParentDocumentationWidget = pParent;
// set the base url for documentation.
mpParentDocumentationWidget->mpParentMainWindow->mpOMCProxy->sendCommand("getNamedAnnotation(Modelica,version)");
QStringList lst = StringHandler::unparseStrings(mpParentDocumentationWidget->mpParentMainWindow->mpOMCProxy->getResult());
QString versionStr = lst.empty() ? "" : lst.at(0);
// We need to replace the back slashes(\) with forward slash(/), since QWebView baseurl doesn't handle it.
QString baseUrl = QString(Helper::OpenModelicaLibrary).replace("\\", "/").append("/Modelica ").append(versionStr).append("/Images/");
setBaseUrl(baseUrl);
// set page font settings
settings()->setFontFamily(QWebSettings::StandardFont, "Verdana");
settings()->setFontSize(QWebSettings::DefaultFontSize, 10);
// set page links settings
page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
connect(this, SIGNAL(linkClicked(QUrl)), SLOT(processLinkClick(QUrl)));
connect(this->page(), SIGNAL(linkHovered(QString,QString,QString)), SLOT(processLinkHover(QString,QString,QString)));
}
void DocumentationViewer::setBaseUrl(QString url)
{
mBaseUrl.setUrl(url);
}
QUrl DocumentationViewer::getBaseUrl()
{
return mBaseUrl;
}
void DocumentationViewer::processLinkClick(QUrl url)
{
//! @todo
/* Send all http requests to desktop services for now. If we need to display web pages then we will change it,
but for now else portion is never reached. */
// if url contains http or mailto: send it to desktop services
if ((url.toString().startsWith("http")) or (url.toString().startsWith("mailto:")))
{
QDesktopServices::openUrl(url);
}
// if the user has clicked on some Modelica Links like Modelica://
else if (url.toString().startsWith("Modelica"))
{
// remove Modelica:// from link
QString className;
className = url.toString().mid(10, url.toString().length() - 1);
// send the new className to DocumentationWidget
mpParentDocumentationWidget->show(className);
}
// if it is normal http request then check if its not redirected to https
else
{
QNetworkAccessManager* accessManager = page()->networkAccessManager();
QNetworkRequest request(url);
QNetworkReply* reply = accessManager->get(request);
connect(reply, SIGNAL(finished()), SLOT(requestFinished()));
}
}
void DocumentationViewer::requestFinished()
{
QNetworkReply *reply = qobject_cast<QNetworkReply*>(const_cast<QObject*>(sender()));
QUrl possibleRedirectedUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
//if the url contains https
if (possibleRedirectedUrl.toString().contains("https"))
QDesktopServices::openUrl(possibleRedirectedUrl);
else
this->load(reply->url());
reply->deleteLater();
}
void DocumentationViewer::processLinkHover(QString link, QString title, QString textContent)
{
Q_UNUSED(title);
Q_UNUSED(textContent);
if (link.isEmpty())
mpParentDocumentationWidget->mpParentMainWindow->mpStatusBar->clearMessage();
else
mpParentDocumentationWidget->mpParentMainWindow->mpStatusBar->showMessage(link);
}
void DocumentationViewer::mousePressEvent(QMouseEvent *event)
{
// dont allow right click on Documentation Viewer
if (event->button() == Qt::LeftButton)
{
QWebView::mousePressEvent(event);
}
else if (event->button() == Qt::RightButton)
{
event->ignore();
}
}