-
Notifications
You must be signed in to change notification settings - Fork 298
/
MessagesWidget.cpp
506 lines (483 loc) · 18.6 KB
/
MessagesWidget.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, 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.
*
*/
/*
* @author Adeel Asghar <adeel.asghar@liu.se>
*/
#include "MessagesWidget.h"
#include "MainWindow.h"
#include "Modeling/ModelWidgetContainer.h"
#include "LibraryTreeWidget.h"
#include "Util/Helper.h"
#include "Options/OptionsDialog.h"
#include <QMenu>
#include <QMessageBox>
/*!
* \class MessageItem
* \brief Holds the error message data.
*/
/*!
* \brief MessageItem::MessageItem
*/
MessageItem::MessageItem()
{
mMessageItemType = MessageItem::Modelica;
mTime = QTime::currentTime().toString();
mFileName = "";
mReadOnly = false;
mLineStart = 0;
mColumnStart = 0;
mLineEnd = 0;
mColumnEnd = 0;
mMessage = "";
mErrorKind = StringHandler::NoOMErrorKind;
mErrorType = StringHandler::NoOMError;
}
/*!
* \brief MessageItem::MessageItem
* \param type
* \param filename - the error filename.
* \param readOnly - the error file readOnly state.
* \param lineStart - the index where the error starts.
* \param columnStart - the indexed column where the error starts.
* \param lineEnd - the index where the error ends.
* \param columnEnd - the indexed column where the error ends.
* \param message - the error message.
* \param errorKind - the error kind.
* \param errorType - the error type.
*/
MessageItem::MessageItem(MessageItemType type, QString filename, bool readOnly, int lineStart, int columnStart, int lineEnd,
int columnEnd, QString message, QString errorKind, QString errorType)
: mMessageItemType(type)
{
mTime = QTime::currentTime().toString();
mFileName = filename;
mReadOnly = readOnly;
mLineStart = lineStart;
mColumnStart = columnStart;
mLineEnd = lineEnd;
mColumnEnd = columnEnd;
mMessage = message;
mErrorKind = StringHandler::getErrorKind(errorKind);
mErrorType = StringHandler::getErrorType(errorType);
}
/*!
* \brief MessageItem::MessageItem
* \param type
* \param message - the error message.
* \param errorKind - the error kind.
* \param errorType - the error type.
*/
MessageItem::MessageItem(MessageItemType type, QString message, QString errorKind, QString errorType)
: mMessageItemType(type)
{
mTime = QTime::currentTime().toString();
mFileName = "";
mReadOnly = false;
mLineStart = 0;
mColumnStart = 0;
mLineEnd = 0;
mColumnEnd = 0;
mMessage = message;
mErrorKind = StringHandler::getErrorKind(errorKind);
mErrorType = StringHandler::getErrorType(errorType);
}
/*!
Returns the location of the error message.
*/
QString MessageItem::getLocation()
{
return QString("%1:%2-%3:%4")
.arg(QString::number(mLineStart))
.arg(QString::number(mColumnStart))
.arg(QString::number(mLineEnd))
.arg(QString::number(mColumnEnd));
}
/*!
* \class MessageWidget
* \brief Message widget with QTextBrowser for showing notifications, warning and error messages.
*/
/*!
* \brief MessageWidget::MessageWidget
* \param pParent
*/
MessageWidget::MessageWidget(QWidget *pParent)
: QWidget(pParent)
{
mMessageNumber = 1;
mpMessagesTextBrowser = new QTextBrowser;
mpMessagesTextBrowser->setOpenLinks(false);
mpMessagesTextBrowser->setOpenExternalLinks(false);
mpMessagesTextBrowser->setFrameStyle(QFrame::NoFrame);
mpMessagesTextBrowser->setContextMenuPolicy(Qt::CustomContextMenu);
connect(mpMessagesTextBrowser, SIGNAL(anchorClicked(QUrl)), SLOT(openErrorMessageClass(QUrl)));
connect(mpMessagesTextBrowser, SIGNAL(customContextMenuRequested(QPoint)), SLOT(showContextMenu(QPoint)));
// create actions
mpSelectAllAction = new QAction(tr("Select All"), this);
mpSelectAllAction->setShortcut(QKeySequence("Ctrl+a"));
mpSelectAllAction->setStatusTip(tr("Selects all the Messages"));
connect(mpSelectAllAction, SIGNAL(triggered()), mpMessagesTextBrowser, SLOT(selectAll()));
mpCopyAction = new QAction(QIcon(":/Resources/icons/copy.svg"), Helper::copy, this);
mpCopyAction->setShortcut(QKeySequence("Ctrl+c"));
mpCopyAction->setStatusTip(tr("Copy the Message"));
connect(mpCopyAction, SIGNAL(triggered()), mpMessagesTextBrowser, SLOT(copy()));
mpClearThisTabAction = new QAction(tr("Clear This Tab"), this);
mpClearThisTabAction->setStatusTip(tr("clears the messages from this tab"));
connect(mpClearThisTabAction, SIGNAL(triggered()), SLOT(clearThisTabMessages()));
mpClearAllTabsAction = new QAction(tr("Clear All Tabs"), this);
mpClearAllTabsAction->setStatusTip(tr("clears the messages from all tabs"));
connect(mpClearAllTabsAction, SIGNAL(triggered()), SLOT(clearAllTabsMessages()));
// Main Layout
QHBoxLayout *pMainLayout = new QHBoxLayout;
pMainLayout->setContentsMargins(0, 0, 0, 0);
pMainLayout->addWidget(mpMessagesTextBrowser);
setLayout(pMainLayout);
}
/*!
* \brief MessageWidget::applyMessagesSettings
* Applies the Messages settings e.g size, font, color.
*/
void MessageWidget::applyMessagesSettings()
{
MessagesPage *pMessagesPage = OptionsDialog::instance()->getMessagesPage();
// set the output size
mpMessagesTextBrowser->document()->setMaximumBlockCount(pMessagesPage->getOutputSizeSpinBox()->value());
// set the font
QString fontFamily = pMessagesPage->getFontFamilyComboBox()->currentFont().family();
double fontSize = pMessagesPage->getFontSizeSpinBox()->value();
QFont font(fontFamily);
font.setPointSizeF(fontSize);
mpMessagesTextBrowser->setFont(font);
// set the messages color by setting the style sheet
QString messagesCSS = QString(".notification {color: %1}"
".warning {color: %2}"
".error {color: %3}")
.arg(OptionsDialog::instance()->getMessagesPage()->getNotificationColor().name())
.arg(OptionsDialog::instance()->getMessagesPage()->getWarningColor().name())
.arg(OptionsDialog::instance()->getMessagesPage()->getErrorColor().name());
mpMessagesTextBrowser->document()->setDefaultStyleSheet(messagesCSS);
// move the cursor to end.
QTextCursor textCursor = mpMessagesTextBrowser->textCursor();
textCursor.movePosition(QTextCursor::End);
mpMessagesTextBrowser->setTextCursor(textCursor);
}
/*!
* \brief MessageWidget::addGUIMessage
* Adds the message.\n
* Moves to the most recent message in the view.
* \param messageItem
*/
void MessageWidget::addGUIMessage(MessageItem messageItem)
{
// move the cursor down before adding message.
QTextCursor textCursor = mpMessagesTextBrowser->textCursor();
textCursor.movePosition(QTextCursor::End);
mpMessagesTextBrowser->setTextCursor(textCursor);
// set the CSS class depending on message type
QString messageCSSClass;
switch (messageItem.getErrorType()) {
case StringHandler::Internal:
messageCSSClass = "error";
break;
case StringHandler::Warning:
messageCSSClass = "warning";
break;
case StringHandler::OMError:
messageCSSClass = "error";
break;
case StringHandler::Notification:
default:
messageCSSClass = "notification";
break;
}
QString linkFormat = QString("[%1: %2]: <a href=\"omeditmessagesbrowser:///%3?lineNumber=%4\">%5</a>");
QString errorMessage;
QString message;
if(messageItem.getMessageItemType()== MessageItem::Modelica) {
// if message already have tags then just use it.
if (Qt::mightBeRichText(messageItem.getMessage())) {
message = messageItem.getMessage();
} else {
message = Qt::convertFromPlainText(messageItem.getMessage()).remove("<p>").remove("</p>");
}
} else if(messageItem.getMessageItemType()== MessageItem::CompositeModel) {
message = messageItem.getMessage().remove("<p>").remove("</p>");
}
if (messageItem.getFileName().isEmpty()) { // if custom error message
errorMessage = message;
} else if (messageItem.getMessageItemType()== MessageItem::CompositeModel ||
MainWindow::instance()->getLibraryWidget()->getLibraryTreeModel()->findLibraryTreeItem(messageItem.getFileName())) {
// If the class is only loaded in AST via loadString then create link for the error message.
errorMessage = linkFormat.arg(messageItem.getFileName())
.arg(messageItem.getLocation())
.arg(messageItem.getFileName())
.arg(messageItem.getLineStart())
.arg(message);
} else {
// Find the class name using the file name and line number.
LibraryTreeItem *pLibraryTreeItem;
pLibraryTreeItem = MainWindow::instance()->getLibraryWidget()->getLibraryTreeModel()->getLibraryTreeItemFromFile(messageItem.getFileName(),
messageItem.getLineStart().toInt());
if (pLibraryTreeItem) {
errorMessage = linkFormat.arg(pLibraryTreeItem->getNameStructure())
.arg(messageItem.getLocation())
.arg(pLibraryTreeItem->getNameStructure())
.arg(messageItem.getLineStart())
.arg(message);
} else {
// otherwise display filename to user where error occurred.
errorMessage = QString("[%1: %2]: %3")
.arg(messageItem.getFileName())
.arg(messageItem.getLocation())
.arg(message);
}
}
QString errorString = QString("<div class=\"%1\">"
"<b>[%2] %3 %4 %5</b><br>"
"%6"
"</div><br>")
.arg(messageCSSClass)
.arg(QString::number(mMessageNumber))
.arg(QTime::currentTime().toString())
.arg(StringHandler::getErrorKindString(messageItem.getErrorKind()))
.arg(StringHandler::getErrorTypeDisplayString(messageItem.getErrorType()))
.arg(errorMessage);
mpMessagesTextBrowser->insertHtml(errorString);
mMessageNumber++;
// move the cursor down after adding message.
textCursor.movePosition(QTextCursor::End);
mpMessagesTextBrowser->setTextCursor(textCursor);
}
/*!
* \brief MessageWidget::openErrorMessageClass
* Slot activated when a link e.g., "<a href="omeditmessagesbrowser:///className?lineNumber=4></a>" is clicked from MessagesWidget.\n
* Parses the url and loads the Modelica class with the line selected.
* \param url - the url that is clicked
*/
void MessageWidget::openErrorMessageClass(QUrl url)
{
if (url.scheme() != "omeditmessagesbrowser") {
/*! @todo Write error-message?! */
return;
}
QString className = url.path();
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
QUrlQuery query(url);
int lineNumber = query.queryItemValue("lineNumber").toInt();
#else /* Qt4 */
int lineNumber = url.queryItemValue("lineNumber").toInt();
#endif
if (className.startsWith("/")) {
className.remove(0, 1);
}
// find the class that has the error
LibraryTreeItem *pLibraryTreeItem = MainWindow::instance()->getLibraryWidget()->getLibraryTreeModel()->findLibraryTreeItem(className);
if (pLibraryTreeItem) {
/* the error could be in P.M but we get P as error class in this case we see if current class has the same file as P
* and also contains the line number. If we have correct current class then no need to show root parent class i.e., P.
*/
ModelWidget *pModelWidget = MainWindow::instance()->getModelWidgetContainer()->getCurrentModelWidget();
if (pModelWidget /* Might be NULL */ && (pModelWidget->getLibraryTreeItem()->getFileName().compare(pLibraryTreeItem->getFileName()) == 0) &&
pModelWidget->getLibraryTreeItem()->inRange(lineNumber)) {
pLibraryTreeItem = pModelWidget->getLibraryTreeItem();
}
MainWindow::instance()->getLibraryWidget()->getLibraryTreeModel()->showModelWidget(pLibraryTreeItem);
if (pLibraryTreeItem->getModelWidget() && pLibraryTreeItem->getModelWidget()->getEditor()) {
pLibraryTreeItem->getModelWidget()->getTextViewToolButton()->setChecked(true);
pLibraryTreeItem->getModelWidget()->getEditor()->getPlainTextEdit()->goToLineNumber(lineNumber);
}
} else {
QMessageBox::information(MainWindow::instance(), QString("%1 - %2").arg(Helper::applicationName, Helper::information),
GUIMessages::getMessage(GUIMessages::CLASS_NOT_FOUND)
.arg(className), Helper::ok);
}
}
/*!
* \brief MessageWidget::showContextMenu
* Shows a context menu when user right click on the Messages tree.
* Slot activated when mpMessagesTextBrowser customContextMenuRequested signal is raised.
* \param point
*/
void MessageWidget::showContextMenu(QPoint point)
{
QMenu menu(this);
menu.addAction(mpSelectAllAction);
menu.addAction(mpCopyAction);
menu.addAction(mpClearThisTabAction);
menu.addAction(mpClearAllTabsAction);
menu.exec(mpMessagesTextBrowser->viewport()->mapToGlobal(point));
}
/*!
* \brief MessageWidget::clearThisTabMessages
* Clears the messages and resets the messages number from this tab.
*/
void MessageWidget::clearThisTabMessages()
{
resetMessagesNumber();
mpMessagesTextBrowser->clear();
}
/*!
* \brief MessageWidget::clearAllTabsMessages
* Clears the messages and resets the messages number from all tabs.
*/
void MessageWidget::clearAllTabsMessages()
{
MessagesWidget::instance()->getAllMessageWidget()->resetMessagesNumber();
MessagesWidget::instance()->getAllMessageWidget()->getMessagesTextBrowser()->clear();
MessagesWidget::instance()->getNotificationMessageWidget()->resetMessagesNumber();
MessagesWidget::instance()->getNotificationMessageWidget()->getMessagesTextBrowser()->clear();
MessagesWidget::instance()->getWarningMessageWidget()->resetMessagesNumber();
MessagesWidget::instance()->getWarningMessageWidget()->getMessagesTextBrowser()->clear();
MessagesWidget::instance()->getErrorMessageWidget()->resetMessagesNumber();
MessagesWidget::instance()->getErrorMessageWidget()->getMessagesTextBrowser()->clear();
}
/*!
* \class MessagesWidget
* \brief Tab widget for showing notifications, warning and error messages.
*/
MessagesWidget *MessagesWidget::mpInstance = 0;
/*!
* \brief MessagesWidget::create
*/
void MessagesWidget::create()
{
if (!mpInstance) {
mpInstance = new MessagesWidget;
}
}
/*!
* \brief MessagesWidget::destroy
*/
void MessagesWidget::destroy()
{
mpInstance->deleteLater();
}
/*!
* \brief MessagesWidget::MessagesWidget
* \param pParent
*/
MessagesWidget::MessagesWidget(QWidget *pParent)
: QWidget(pParent)
{
mpMessagesTabWidget = new QTabWidget;
mpAllMessageWidget = new MessageWidget;
mpMessagesTabWidget->addTab(mpAllMessageWidget, tr("All"));
mpNotificationMessageWidget = new MessageWidget;
mpMessagesTabWidget->addTab(mpNotificationMessageWidget, tr("Notifications"));
mpWarningMessageWidget = new MessageWidget;
mpMessagesTabWidget->addTab(mpWarningMessageWidget, tr("Warnings"));
mpErrorMessageWidget = new MessageWidget;
mpMessagesTabWidget->addTab(mpErrorMessageWidget, tr("Errors"));
mSuppressMessagesList.clear();
#ifdef Q_OS_WIN
// nothing
#elif defined(Q_OS_MAC)
mSuppressMessagesList << "modalSession has been exited prematurely*"; /* This warning is fixed in latest Qt versions but out OSX build still uses old Qt. */
#else
mSuppressMessagesList << "libpng warning*" /* libpng warning comes from QWebView default images. */
<< "Gtk-Message:*" /* Gtk warning comes when Qt tries to open the native dialogs. */;
#endif
// Main Layout
QHBoxLayout *pMainLayout = new QHBoxLayout;
pMainLayout->setContentsMargins(0, 0, 0, 0);
pMainLayout->addWidget(mpMessagesTabWidget);
setLayout(pMainLayout);
}
/*!
* \brief MessagesWidget::resetMessagesNumber
* Resets the Message number of the appropriate message tab widget.
*/
void MessagesWidget::resetMessagesNumber()
{
mpAllMessageWidget->resetMessagesNumber();
mpNotificationMessageWidget->resetMessagesNumber();
mpWarningMessageWidget->resetMessagesNumber();
mpErrorMessageWidget->resetMessagesNumber();
}
/*!
* \brief MessagesWidget::applyMessagesSettings
* Applies the Messages settings to the appropriate message tab widget.
*/
void MessagesWidget::applyMessagesSettings()
{
mpAllMessageWidget->applyMessagesSettings();
mpNotificationMessageWidget->applyMessagesSettings();
mpWarningMessageWidget->applyMessagesSettings();
mpErrorMessageWidget->applyMessagesSettings();
}
/*!
* \brief MessagesWidget::addGUIMessage
* Adds the error message to the appropriate message tab widget.
* \param messageItem
*/
void MessagesWidget::addGUIMessage(MessageItem messageItem)
{
// suppress the unnecessary qt warning messages
foreach (QString suppressMessage, mSuppressMessagesList) {
QRegExp rx(suppressMessage);
rx.setPatternSyntax(QRegExp::Wildcard);
if (rx.exactMatch(messageItem.getMessage())) {
return;
}
}
switch (messageItem.getErrorType()) {
case StringHandler::Internal:
mpErrorMessageWidget->addGUIMessage(messageItem);
mpAllMessageWidget->addGUIMessage(messageItem);
case StringHandler::Notification:
mpNotificationMessageWidget->addGUIMessage(messageItem);
mpAllMessageWidget->addGUIMessage(messageItem);
break;
case StringHandler::Warning:
mpWarningMessageWidget->addGUIMessage(messageItem);
mpAllMessageWidget->addGUIMessage(messageItem);
break;
case StringHandler::OMError:
mpErrorMessageWidget->addGUIMessage(messageItem);
mpAllMessageWidget->addGUIMessage(messageItem);
break;
default:
mpAllMessageWidget->addGUIMessage(messageItem);
break;
}
mpMessagesTabWidget->setCurrentWidget(mpAllMessageWidget);
emit MessageAdded();
}
/*!
* \brief MessagesWidget::clearMessages
* Slot activated when mpClearAllAction triggered signal is raised.
*/
void MessagesWidget::clearMessages()
{
mpAllMessageWidget->clearAllTabsMessages();
mpNotificationMessageWidget->clearAllTabsMessages();
mpWarningMessageWidget->clearAllTabsMessages();
mpErrorMessageWidget->clearAllTabsMessages();
}