Debian Codegraph @

src/Gui/ReportView.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23
24 #include <QApplication>
25 #include <QContextMenuEvent>
26 #include <QGridLayout>
27 #include <QMenu>
28 #include <QTextCursor>
29 #include <QTextStream>
30 #include <QTime>
31
32
33 #include <Base/Interpreter.h>
34 #include <Base/Color.h>
35
36 #include "ReportView.h"
37 #include "Application.h"
38 #include "BitmapFactory.h"
39 #include "DockWindowManager.h"
40 #include "FileDialog.h"
41 #include "PythonConsole.h"
42 #include "PythonConsolePy.h"
43 #include "Tools.h"
44
45
46 using namespace Gui;
47 using namespace Gui::DockWnd;
48
49 /* TRANSLATOR Gui::DockWnd::ReportView */
50
51 /**
52 * Constructs a ReportView which is a child of 'parent', with the
53 * name 'name' and widget flags set to 'f'
54 */
55 ReportView::ReportView(QWidget* parent)
56 : QWidget(parent)
57 {
58 setObjectName(QLatin1String("ReportOutput"));
59
60 resize(529, 162);
61 auto tabLayout = new QGridLayout(this);
62 tabLayout->setSpacing(0);
63 tabLayout->setContentsMargins(0, 0, 0, 0);
64
65 tabWidget = new QTabWidget(this);
66 tabWidget->setObjectName(QStringLiteral("tabWidget"));
67 tabWidget->setTabPosition(QTabWidget::South);
68 tabWidget->setTabShape(QTabWidget::Rounded);
69 tabLayout->addWidget(tabWidget, 0, 0);
70
71
72 // create the output window for 'Report view'
73 tabOutput = new ReportOutput();
74 tabOutput->setWindowTitle(tr("Output"));
75 tabOutput->setWindowIcon(BitmapFactory().pixmap("MacroEditor"));
76 int output = tabWidget->addTab(tabOutput, tabOutput->windowTitle());
77 tabWidget->setTabIcon(output, tabOutput->windowIcon());
78
79 // create the python console
80 tabPython = new PythonConsole();
81 tabPython->setWordWrapMode(QTextOption::NoWrap);
82 tabPython->setWindowTitle(tr("Python Console"));
83 tabPython->setWindowIcon(BitmapFactory().iconFromTheme("applications-python"));
84 int python = tabWidget->addTab(tabPython, tabPython->windowTitle());
85 tabWidget->setTabIcon(python, tabPython->windowIcon());
86 tabWidget->setCurrentIndex(0);
87
88 // raise the tab page set in the preferences
89 ParameterGrp::handle hGrp = WindowParameter::getDefaultParameter()->GetGroup("General");
90 int index = hGrp->GetInt("AutoloadTab", 0);
91 tabWidget->setCurrentIndex(index);
92 }
93
94 /**
95 * Destroys the object and frees any allocated resources
96 */
97 ReportView::~ReportView() = default;
98
99 void ReportView::changeEvent(QEvent* e)
100 {
101 QWidget::changeEvent(e);
102 if (e->type() == QEvent::LanguageChange) {
103 tabOutput->setWindowTitle(tr("Output"));
104 tabPython->setWindowTitle(tr("Python Console"));
105 for (int i = 0; i < tabWidget->count(); i++) {
106 tabWidget->setTabText(i, tabWidget->widget(i)->windowTitle());
107 }
108 }
109 }
110
111 // ----------------------------------------------------------
112
113 namespace Gui
114 {
115 struct TextBlockData: public QTextBlockUserData
116 {
117 struct State
118 {
119 int length;
120 ReportHighlighter::Paragraph type;
121 };
122 QVector<State> block;
123 };
124 } // namespace Gui
125
126 ReportHighlighter::ReportHighlighter(QTextEdit* edit)
127 : QSyntaxHighlighter(edit)
128 , type(Message)
129 {
130 QPalette pal = edit->palette();
131 txtCol = pal.windowText().color();
132 logCol = Qt::blue;
133 warnCol = QColor(255, 170, 0);
134 errCol = Qt::red;
135 }
136
137 ReportHighlighter::~ReportHighlighter() = default;
138
139 void ReportHighlighter::highlightBlock(const QString& text)
140 {
141 if (text.isEmpty()) {
142 return;
143 }
144 auto ud = static_cast<TextBlockData*>(this->currentBlockUserData());
145 if (!ud) {
146 ud = new TextBlockData;
147 this->setCurrentBlockUserData(ud);
148 }
149
150 TextBlockData::State b;
151 b.length = text.length();
152 b.type = this->type;
153 ud->block.append(b);
154
155 QVector<TextBlockData::State> block = ud->block;
156 int start = 0;
157 for (const auto& it : block) {
158 switch (it.type) {
159 case Message:
160 setFormat(start, it.length - start, txtCol);
161 break;
162 case Warning:
163 setFormat(start, it.length - start, warnCol);
164 break;
165 case Error:
166 setFormat(start, it.length - start, errCol);
167 break;
168 case LogText:
169 setFormat(start, it.length - start, logCol);
170 break;
171 case Critical:
172 setFormat(start, it.length - start, criticalCol);
173 break;
174 default:
175 break;
176 }
177
178 start = it.length;
179 }
180 }
181
182 void ReportHighlighter::setParagraphType(ReportHighlighter::Paragraph t)
183 {
184 type = t;
185 }
186
187 void ReportHighlighter::setTextColor(const QColor& col)
188 {
189 txtCol = col;
190 }
191
192 void ReportHighlighter::setLogColor(const QColor& col)
193 {
194 logCol = col;
195 }
196
197 void ReportHighlighter::setWarningColor(const QColor& col)
198 {
199 warnCol = col;
200 }
201
202 void ReportHighlighter::setErrorColor(const QColor& col)
203 {
204 errCol = col;
205 }
206
207 void ReportHighlighter::setCriticalColor(const QColor& col)
208 {
209 criticalCol = col;
210 }
211
212 // ----------------------------------------------------------------------------
213
214 namespace Gui
215 {
216 class ReportOutputParameter
217 {
218 public:
219 static bool showOnLogMessage()
220 {
221 return getGroup()->GetBool("checkShowReportViewOnLogMessage", false);
222 }
223 static void toggleShowOnLogMessage()
224 {
225 bool show = showOnLogMessage();
226 getGroup()->SetBool("checkShowReportViewOnLogMessage", !show);
227 }
228 static bool showOnMessage()
229 {
230 return getGroup()->GetBool("checkShowReportViewOnNormalMessage", false);
231 }
232 static void toggleShowOnMessage()
233 {
234 bool show = showOnMessage();
235 getGroup()->SetBool("checkShowReportViewOnNormalMessage", !show);
236 }
237 static bool showOnWarning()
238 {
239 return getGroup()->GetBool("checkShowReportViewOnWarning", false);
240 }
241 static void toggleShowOnWarning()
242 {
243 bool show = showOnWarning();
244 getGroup()->SetBool("checkShowReportViewOnWarning", !show);
245 }
246 static bool showOnError()
247 {
248 return getGroup()->GetBool("checkShowReportViewOnError", false);
249 }
250 static void toggleShowOnError()
251 {
252 bool show = showOnError();
253 getGroup()->SetBool("checkShowReportViewOnError", !show);
254 }
255 static bool showOnCritical()
256 {
257 return getGroup()->GetBool("checkShowReportViewOnCritical", false);
258 }
259 static void toggleShowOnCritical()
260 {
261 bool show = showOnMessage();
262 getGroup()->SetBool("checkShowReportViewOnCritical", !show);
263 }
264
265 private:
266 static ParameterGrp::handle getGroup()
267 {
268 return App::GetApplication()
269 .GetUserParameter()
270 .GetGroup("BaseApp")
271 ->GetGroup("Preferences")
272 ->GetGroup("OutputWindow");
273 }
274 };
275 } // namespace Gui
276
277 // ----------------------------------------------------------
278
279 /**
280 * The CustomReportEvent class is used to send report events in the methods Log(),
281 * Error(), Warning() and Message() of the ReportOutput class to itself instead of
282 * printing the messages directly in its text view.
283 *
284 * This makes the methods Log(), Error(), Warning() and Message() thread-safe.
285 * @author Werner Mayer
286 */
287 class CustomReportEvent: public QEvent
288 {
289 public:
290 CustomReportEvent(ReportHighlighter::Paragraph p, const QString& s)
291 : QEvent(QEvent::Type(QEvent::User))
292 {
293 par = p;
294 msg = s;
295 }
296 ~CustomReportEvent() override = default;
297 const QString& message() const
298 {
299 return msg;
300 }
301 ReportHighlighter::Paragraph messageType() const
302 {
303 return par;
304 }
305
306 private:
307 ReportHighlighter::Paragraph par;
308 QString msg;
309 };
310
311 // ----------------------------------------------------------
312
313 /**
314 * The ReportOutputObserver class is used to check if messages sent to the
315 * report view are warnings or errors, and if so and if the user has not
316 * disabled this in preferences, the report view is toggled on so the
317 * user always gets the warnings/errors
318 */
319
320 ReportOutputObserver::ReportOutputObserver(ReportOutput* report)
321 : QObject(report)
322 {
323 this->reportView = report;
324 }
325
326 void ReportOutputObserver::showReportView()
327 {
328 // get the QDockWidget parent of the report view
329 DockWindowManager::instance()->activate(reportView);
330 }
331
332 bool ReportOutputObserver::eventFilter(QObject* obj, QEvent* event)
333 {
334 if (event->type() == QEvent::User && obj == reportView.data()) {
335 auto cr = dynamic_cast<CustomReportEvent*>(event);
336 if (cr) {
337 ReportHighlighter::Paragraph msgType = cr->messageType();
338 if (msgType == ReportHighlighter::Warning) {
339 if (ReportOutputParameter::showOnWarning()) {
340 showReportView();
341 }
342 }
343 else if (msgType == ReportHighlighter::Error) {
344 if (ReportOutputParameter::showOnError()) {
345 showReportView();
346 }
347 }
348 else if (msgType == ReportHighlighter::Message) {
349 if (ReportOutputParameter::showOnMessage()) {
350 showReportView();
351 }
352 }
353 else if (msgType == ReportHighlighter::LogText) {
354 if (ReportOutputParameter::showOnLogMessage()) {
355 showReportView();
356 }
357 }
358 else if (msgType == ReportHighlighter::Critical) {
359 if (ReportOutputParameter::showOnCritical()) {
360 showReportView();
361 }
362 }
363 }
364 return false; // true would prevent the messages reaching the report view
365 }
366
367 // standard event processing
368 return QObject::eventFilter(obj, event);
369 }
370
371 // ----------------------------------------------------------
372
373 class ReportOutput::Data
374 {
375 public:
376 Data()
377 {
378 if (!default_stdout) {
379 Base::PyGILStateLocker lock;
380 default_stdout = PySys_GetObject("stdout");
381 replace_stdout = new OutputStdout();
382 redirected_stdout = false;
383 }
384
385 if (!default_stderr) {
386 Base::PyGILStateLocker lock;
387 default_stderr = PySys_GetObject("stderr");
388 replace_stderr = new OutputStderr();
389 redirected_stderr = false;
390 }
391 }
392 ~Data()
393 {
394 if (replace_stdout) {
395 Base::PyGILStateLocker lock;
396 Py_DECREF(replace_stdout);
397 replace_stdout = nullptr;
398 }
399
400 if (replace_stderr) {
401 Base::PyGILStateLocker lock;
402 Py_DECREF(replace_stderr);
403 replace_stderr = nullptr;
404 }
405 }
406
407 // make them static because redirection should done only once
408 static bool redirected_stdout;
409 static PyObject* default_stdout;
410 static PyObject* replace_stdout;
411
412 static bool redirected_stderr;
413 static PyObject* default_stderr;
414 static PyObject* replace_stderr;
415 #ifdef FC_DEBUG
416 long logMessageSize = 0;
417 #else
418 long logMessageSize = 2048;
419 #endif
420 };
421
422 bool ReportOutput::Data::redirected_stdout = false;
423 PyObject* ReportOutput::Data::default_stdout = nullptr;
424 PyObject* ReportOutput::Data::replace_stdout = nullptr;
425
426 bool ReportOutput::Data::redirected_stderr = false;
427 PyObject* ReportOutput::Data::default_stderr = nullptr;
428 PyObject* ReportOutput::Data::replace_stderr = nullptr;
429
430 /* TRANSLATOR Gui::DockWnd::ReportOutput */
431
432 /**
433 * Constructs a ReportOutput which is a child of 'parent', with the
434 * name 'name' and widget flags set to 'f'
435 */
436 ReportOutput::ReportOutput(QWidget* parent)
437 : QTextEdit(parent)
438 , WindowParameter("OutputWindow")
439 , d(new Data)
440 , gotoEnd(false)
441 , blockStart(true)
442 {
443 bLog = false;
444 reportHl = new ReportHighlighter(this);
445
446 restoreFont();
447 setReadOnly(true);
448 clear();
449 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
450
451 Base::Console().attachObserver(this);
452 getWindowParameter()->Attach(this);
453 getWindowParameter()->NotifyAll();
454 // do this explicitly because the keys below might not yet be part of a group
455 getWindowParameter()->Notify("RedirectPythonOutput");
456 getWindowParameter()->Notify("RedirectPythonErrors");
457
458 _prefs = WindowParameter::getDefaultParameter()->GetGroup("Editor");
459 _prefs->Attach(this);
460 _prefs->Notify("FontSize");
461
462 messageSize = _prefs->GetInt("LogMessageSize", d->logMessageSize);
463
464 // scroll to bottom at startup to make sure that last appended text is visible
465 ensureCursorVisible();
466 }
467
468 /**
469 * Destroys the object and frees any allocated resources
470 */
471 ReportOutput::~ReportOutput()
472 {
473 getWindowParameter()->Detach(this);
474 _prefs->Detach(this);
475 Base::Console().detachObserver(this);
476 delete reportHl;
477 delete d;
478 }
479
480 void ReportOutput::restoreFont()
481 {
482 QFont font;
483 auto hPrefGrp = App::GetApplication().GetParameterGroupByPath(
484 "User parameter:BaseApp/Preferences/Editor"
485 );
486 int fontSize = hPrefGrp->GetInt("FontSize", 10);
487 auto serifFont = hPrefGrp->GetASCII("Font");
488 if (serifFont.empty()) {
489 font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
490 font.setPointSize(fontSize);
491 }
492 else {
493 font = QFont(QString::fromStdString(serifFont), fontSize);
494 }
495 setFont(font);
496 }
497
498 void ReportOutput::sendLog(
499 const std::string& notifiername,
500 const std::string& msg,
501 Base::LogStyle level,
502 Base::IntendedRecipient recipient,
503 Base::ContentType content
504 )
505 {
506 // Do not log translated messages, or messages intended only to the user to the Report View
507 if (recipient == Base::IntendedRecipient::User || content == Base::ContentType::Translated) {
508 return;
509 }
510
511 ReportHighlighter::Paragraph style = ReportHighlighter::LogText;
512 switch (level) {
513 case Base::LogStyle::Warning:
514 style = ReportHighlighter::Warning;
515 break;
516 case Base::LogStyle::Message:
517 style = ReportHighlighter::Message;
518 break;
519 case Base::LogStyle::Error:
520 style = ReportHighlighter::Error;
521 break;
522 case Base::LogStyle::Log:
523 style = ReportHighlighter::LogText;
524 break;
525 case Base::LogStyle::Critical:
526 style = ReportHighlighter::Critical;
527 break;
528 default:
529 break;
530 }
531
532 QString qMsg;
533
534 if (!notifiername.empty()) {
535 qMsg = QStringLiteral("%1: %2").arg(
536 QString::fromUtf8(notifiername.c_str()),
537 QString::fromUtf8(msg.c_str())
538 );
539 }
540 else {
541 qMsg = QString::fromUtf8(msg.c_str());
542 }
543
544 // This truncates log messages that are too long
545 if (style == ReportHighlighter::LogText) {
546 if (messageSize > 0 && qMsg.size() > messageSize) {
547 qMsg.truncate(messageSize);
548 qMsg += QStringLiteral("...\n");
549 }
550 }
551
552 // Send the event to itself to allow thread-safety. Qt will delete it when done.
553 auto ev = new CustomReportEvent(style, qMsg);
554 QApplication::postEvent(this, ev);
555 }
556
557 void ReportOutput::customEvent(QEvent* ev)
558 {
559 // Appends the text stored in the event to the text view
560 if (ev->type() == QEvent::User) {
561 auto ce = static_cast<CustomReportEvent*>(ev);
562 reportHl->setParagraphType(ce->messageType());
563
564 bool showTimecode = getWindowParameter()->GetBool("checkShowReportTimecode", true);
565 QString text = ce->message();
566
567 // The time code can only be set when the cursor is at the block start
568 if (showTimecode && blockStart) {
569 QTime time = QTime::currentTime();
570 text.prepend(time.toString(QLatin1String("hh:mm:ss ")));
571 }
572
573 QTextCursor cursor(this->document());
574 cursor.beginEditBlock();
575 cursor.movePosition(QTextCursor::End);
576 cursor.insertText(text);
577 cursor.endEditBlock();
578
579 blockStart = cursor.atBlockStart();
580 if (gotoEnd) {
581 setTextCursor(cursor);
582 }
583 ensureCursorVisible();
584 }
585 }
586
587
588 bool ReportOutput::event(QEvent* event)
589 {
590 if (event && event->type() == QEvent::ShortcutOverride) {
591 auto kevent = static_cast<QKeyEvent*>(event);
592 if (kevent == QKeySequence::Copy || kevent == QKeySequence::SelectAll) {
593 kevent->accept();
594 }
595 }
596 return QTextEdit::event(event);
597 }
598
599 void ReportOutput::changeEvent(QEvent* ev)
600 {
601 if (ev->type() == QEvent::StyleChange) {
602 QPalette pal = qApp->palette();
603 QColor color = pal.windowText().color();
604 unsigned int text = Base::Color::asPackedRGB<QColor>(color);
605 auto value = static_cast<unsigned long>(text);
606 // if this parameter is not already set use the style's window text color
607 value = getWindowParameter()->GetUnsigned("colorText", value);
608 getWindowParameter()->SetUnsigned("colorText", value);
609 }
610 QTextEdit::changeEvent(ev);
611 }
612
613 void ReportOutput::contextMenuEvent(QContextMenuEvent* e)
614 {
615 bool bShowOnLog = ReportOutputParameter::showOnLogMessage();
616 bool bShowOnNormal = ReportOutputParameter::showOnMessage();
617 bool bShowOnWarn = ReportOutputParameter::showOnWarning();
618 bool bShowOnError = ReportOutputParameter::showOnError();
619 bool bShowOnCritical = ReportOutputParameter::showOnCritical();
620
621 auto menu = new QMenu(this);
622 auto optionMenu = new QMenu(menu);
623 optionMenu->setTitle(tr("Options"));
624 menu->addMenu(optionMenu);
625 menu->addSeparator();
626
627 auto displayMenu = new QMenu(optionMenu);
628 displayMenu->setTitle(tr("Display Message Types"));
629 optionMenu->addMenu(displayMenu);
630
631 QAction* logMsg
632 = displayMenu->addAction(tr("Normal Messages"), this, &ReportOutput::onToggleNormalMessage);
633 logMsg->setCheckable(true);
634 logMsg->setChecked(bMsg);
635
636 QAction* logAct
637 = displayMenu->addAction(tr("Log Messages"), this, &ReportOutput::onToggleLogMessage);
638 logAct->setCheckable(true);
639 logAct->setChecked(bLog);
640
641 QAction* wrnAct = displayMenu->addAction(tr("Warnings"), this, &ReportOutput::onToggleWarning);
642 wrnAct->setCheckable(true);
643 wrnAct->setChecked(bWrn);
644
645 QAction* errAct = displayMenu->addAction(tr("Errors"), this, &ReportOutput::onToggleError);
646 errAct->setCheckable(true);
647 errAct->setChecked(bErr);
648
649 QAction* logCritical
650 = displayMenu->addAction(tr("Critical Messages"), this, &ReportOutput::onToggleCritical);
651 logCritical->setCheckable(true);
652 logCritical->setChecked(bCritical);
653
654 auto showOnMenu = new QMenu(optionMenu);
655 showOnMenu->setTitle(tr("Show Report View On"));
656 optionMenu->addMenu(showOnMenu);
657
658 QAction* showNormAct = showOnMenu->addAction(
659 tr("Normal Messages"),
660 this,
661 &ReportOutput::onToggleShowReportViewOnNormalMessage
662 );
663 showNormAct->setCheckable(true);
664 showNormAct->setChecked(bShowOnNormal);
665
666 QAction* showLogAct = showOnMenu->addAction(
667 tr("Log Messages"),
668 this,
669 &ReportOutput::onToggleShowReportViewOnLogMessage
670 );
671 showLogAct->setCheckable(true);
672 showLogAct->setChecked(bShowOnLog);
673
674 QAction* showWrnAct
675 = showOnMenu->addAction(tr("Warnings"), this, &ReportOutput::onToggleShowReportViewOnWarning);
676 showWrnAct->setCheckable(true);
677 showWrnAct->setChecked(bShowOnWarn);
678
679 QAction* showErrAct
680 = showOnMenu->addAction(tr("Errors"), this, &ReportOutput::onToggleShowReportViewOnError);
681 showErrAct->setCheckable(true);
682 showErrAct->setChecked(bShowOnError);
683
684 QAction* showCriticalAct = showOnMenu->addAction(
685 tr("Critical Messages"),
686 this,
687 SLOT(onToggleShowReportViewOnCritical())
688 );
689 showCriticalAct->setCheckable(true);
690 showCriticalAct->setChecked(bShowOnCritical);
691
692 optionMenu->addSeparator();
693
694 QAction* stdoutAct = optionMenu->addAction(
695 tr("Redirect Python Output"),
696 this,
697 &ReportOutput::onToggleRedirectPythonStdout
698 );
699 stdoutAct->setCheckable(true);
700 stdoutAct->setChecked(d->redirected_stdout);
701
702 QAction* stderrAct = optionMenu->addAction(
703 tr("Redirect Python Errors"),
704 this,
705 &ReportOutput::onToggleRedirectPythonStderr
706 );
707 stderrAct->setCheckable(true);
708 stderrAct->setChecked(d->redirected_stderr);
709
710 optionMenu->addSeparator();
711 QAction* botAct = optionMenu->addAction(tr("Go to End"), this, &ReportOutput::onToggleGoToEnd);
712 botAct->setCheckable(true);
713 botAct->setChecked(gotoEnd);
714
715 // Use Qt's internal translation of the Copy & Select All commands
716 const char* context = "QWidgetTextControl";
717 QString copyStr = QCoreApplication::translate(context, "&Copy");
718 QAction* copy = menu->addAction(copyStr, this, &ReportOutput::copy);
719 copy->setShortcut(QKeySequence(QKeySequence::Copy));
720 copy->setEnabled(textCursor().hasSelection());
721 QIcon icon = QIcon::fromTheme(QStringLiteral("edit-copy"));
722 if (!icon.isNull()) {
723 copy->setIcon(icon);
724 }
725
726 menu->addSeparator();
727 QString selectStr = QCoreApplication::translate(context, "Select All");
728 QAction* select = menu->addAction(selectStr, this, &ReportOutput::selectAll);
729 select->setShortcut(QKeySequence(QKeySequence::SelectAll));
730
731 menu->addAction(tr("Clear"), this, &ReportOutput::clear);
732 menu->addSeparator();
733 menu->addAction(tr("Save As…"), this, &ReportOutput::onSaveAs);
734
735 menu->exec(e->globalPos());
736 delete menu;
737 }
738
739 void ReportOutput::onSaveAs()
740 {
741 QString fn = QFileDialog::getSaveFileName(
742 this,
743 tr("Save Report Output"),
744 QString(),
745 QStringLiteral("%1 (*.txt *.log)").arg(tr("Plain text files"))
746 );
747 if (!fn.isEmpty()) {
748 QFileInfo fi(fn);
749 if (fi.completeSuffix().isEmpty()) {
750 fn += QLatin1String(".log");
751 }
752 QFile f(fn);
753 if (f.open(QIODevice::WriteOnly)) {
754 QTextStream t(&f);
755 t << toPlainText();
756 f.close();
757 }
758 }
759 }
760
761 bool ReportOutput::isError() const
762 {
763 return bErr;
764 }
765
766 bool ReportOutput::isWarning() const
767 {
768 return bWrn;
769 }
770
771 bool ReportOutput::isLogMessage() const
772 {
773 return bLog;
774 }
775
776 bool ReportOutput::isNormalMessage() const
777 {
778 return bMsg;
779 }
780
781
782 bool ReportOutput::isCritical() const
783 {
784 return bCritical;
785 }
786
787 void ReportOutput::onToggleError()
788 {
789 bErr = bErr ? false : true;
790 getWindowParameter()->SetBool("checkError", bErr);
791 }
792
793 void ReportOutput::onToggleWarning()
794 {
795 bWrn = bWrn ? false : true;
796 getWindowParameter()->SetBool("checkWarning", bWrn);
797 }
798
799 void ReportOutput::onToggleLogMessage()
800 {
801 bLog = bLog ? false : true;
802 getWindowParameter()->SetBool("checkLogging", bLog);
803 }
804
805 void ReportOutput::onToggleNormalMessage()
806 {
807 bMsg = bMsg ? false : true;
808 getWindowParameter()->SetBool("checkMessage", bMsg);
809 }
810
811 void ReportOutput::onToggleCritical()
812 {
813 bCritical = bCritical ? false : true;
814 getWindowParameter()->SetBool("checkCritical", bCritical);
815 }
816
817 void ReportOutput::onToggleShowReportViewOnWarning()
818 {
819 ReportOutputParameter::toggleShowOnWarning();
820 }
821
822 void ReportOutput::onToggleShowReportViewOnError()
823 {
824 ReportOutputParameter::toggleShowOnError();
825 }
826
827 void ReportOutput::onToggleShowReportViewOnNormalMessage()
828 {
829 ReportOutputParameter::toggleShowOnMessage();
830 }
831
832 void ReportOutput::onToggleShowReportViewOnCritical()
833 {
834 ReportOutputParameter::toggleShowOnCritical();
835 }
836
837 void ReportOutput::onToggleShowReportViewOnLogMessage()
838 {
839 ReportOutputParameter::toggleShowOnLogMessage();
840 }
841
842 void ReportOutput::onToggleRedirectPythonStdout()
843 {
844 if (d->redirected_stdout) {
845 d->redirected_stdout = false;
846 Base::PyGILStateLocker lock;
847 PySys_SetObject("stdout", d->default_stdout);
848 }
849 else {
850 d->redirected_stdout = true;
851 Base::PyGILStateLocker lock;
852 PySys_SetObject("stdout", d->replace_stdout);
853 }
854
855 getWindowParameter()->SetBool("RedirectPythonOutput", d->redirected_stdout);
856 }
857
858 void ReportOutput::onToggleRedirectPythonStderr()
859 {
860 if (d->redirected_stderr) {
861 d->redirected_stderr = false;
862 Base::PyGILStateLocker lock;
863 PySys_SetObject("stderr", d->default_stderr);
864 }
865 else {
866 d->redirected_stderr = true;
867 Base::PyGILStateLocker lock;
868 PySys_SetObject("stderr", d->replace_stderr);
869 }
870
871 getWindowParameter()->SetBool("RedirectPythonErrors", d->redirected_stderr);
872 }
873
874 void ReportOutput::onToggleGoToEnd()
875 {
876 gotoEnd = gotoEnd ? false : true;
877 getWindowParameter()->SetBool("checkGoToEnd", gotoEnd);
878 }
879
880 void ReportOutput::OnChange(Base::Subject<const char*>& rCaller, const char* sReason)
881 {
882 ParameterGrp& rclGrp = ((ParameterGrp&)rCaller);
883 if (strcmp(sReason, "checkLogging") == 0) {
884 bLog = rclGrp.GetBool(sReason, bLog);
885 }
886 else if (strcmp(sReason, "checkWarning") == 0) {
887 bWrn = rclGrp.GetBool(sReason, bWrn);
888 }
889 else if (strcmp(sReason, "checkError") == 0) {
890 bErr = rclGrp.GetBool(sReason, bErr);
891 }
892 else if (strcmp(sReason, "checkMessage") == 0) {
893 bMsg = rclGrp.GetBool(sReason, bMsg);
894 }
895 else if (strcmp(sReason, "checkCritical") == 0) {
896 bMsg = rclGrp.GetBool(sReason, bMsg);
897 }
898 else if (strcmp(sReason, "colorText") == 0) {
899 unsigned long col = rclGrp.GetUnsigned(sReason);
900 reportHl->setTextColor(Base::Color::fromPackedRGB<QColor>(col));
901 }
902 else if (strcmp(sReason, "colorCriticalText") == 0) {
903 unsigned long col = rclGrp.GetUnsigned(sReason);
904 reportHl->setTextColor(QColor((col >> 24) & 0xff, (col >> 16) & 0xff, (col >> 8) & 0xff));
905 }
906 else if (strcmp(sReason, "colorLogging") == 0) {
907 unsigned long col = rclGrp.GetUnsigned(sReason);
908 reportHl->setLogColor(Base::Color::fromPackedRGB<QColor>(col));
909 }
910 else if (strcmp(sReason, "colorWarning") == 0) {
911 unsigned long col = rclGrp.GetUnsigned(sReason);
912 reportHl->setWarningColor(Base::Color::fromPackedRGB<QColor>(col));
913 }
914 else if (strcmp(sReason, "colorError") == 0) {
915 unsigned long col = rclGrp.GetUnsigned(sReason);
916 reportHl->setErrorColor(Base::Color::fromPackedRGB<QColor>(col));
917 }
918 else if (strcmp(sReason, "checkGoToEnd") == 0) {
919 gotoEnd = rclGrp.GetBool(sReason, gotoEnd);
920 }
921 else if (strcmp(sReason, "FontSize") == 0 || strcmp(sReason, "Font") == 0) {
922 int fontSize = rclGrp.GetInt("FontSize", 10);
923 QFont font;
924 auto fontName = rclGrp.GetASCII("Font");
925 if (fontName.empty()) {
926 font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
927 font.setPointSize(fontSize);
928 }
929 else {
930 font = QFont(QString::fromStdString(fontName), fontSize);
931 }
932 setFont(font);
933 QFontMetrics metric(font);
934 int width = QtTools::horizontalAdvance(metric, QLatin1String("0000"));
935 setTabStopDistance(width);
936 }
937 else if (strcmp(sReason, "RedirectPythonOutput") == 0) {
938 bool checked = rclGrp.GetBool(sReason, true);
939 if (checked != d->redirected_stdout) {
940 onToggleRedirectPythonStdout();
941 }
942 }
943 else if (strcmp(sReason, "RedirectPythonErrors") == 0) {
944 bool checked = rclGrp.GetBool(sReason, true);
945 if (checked != d->redirected_stderr) {
946 onToggleRedirectPythonStderr();
947 }
948 }
949 else if (strcmp(sReason, "LogMessageSize") == 0) {
950 messageSize = rclGrp.GetInt(sReason, d->logMessageSize);
951 }
952 }
953
954 #include "moc_ReportView.cpp"