-
Notifications
You must be signed in to change notification settings - Fork 238
Implementation of global messages #2598
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3d242d6
eee5c8b
e92fd96
7a1fffb
00924c3
a441d5c
ca933c1
cb9a5e6
424f667
5837a93
3ff4ad8
cebc2b4
dca5145
41999f6
ad3194c
ea5553a
28a042b
a6f8843
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,190 @@ | ||||||
| /******************************************************************************\ | ||||||
| * Copyright (c) 2022 | ||||||
| * | ||||||
| * Author(s): | ||||||
| * Peter Goderie (pgScorpio) | ||||||
| * | ||||||
| ****************************************************************************** | ||||||
| * | ||||||
| * This program is free software; you can redistribute it and/or modify it under | ||||||
| * the terms of the GNU General Public License as published by the Free Software | ||||||
| * Foundation; either version 2 of the License, or (at your option) any later | ||||||
| * version. | ||||||
| * | ||||||
| * This program is distributed in the hope that it will be useful, but WITHOUT | ||||||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||||||
| * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||||||
| * details. | ||||||
| * | ||||||
| * You should have received a copy of the GNU General Public License along with | ||||||
| * this program; if not, write to the Free Software Foundation, Inc., | ||||||
| * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA | ||||||
| * | ||||||
| \******************************************************************************/ | ||||||
|
|
||||||
| #include "messages.h" | ||||||
|
|
||||||
| #ifndef HEADLESS | ||||||
| # include <QTextEdit> | ||||||
| #endif | ||||||
|
|
||||||
| tMainform* CMessages::pMainForm = NULL; | ||||||
| QString CMessages::strMainFormName; | ||||||
|
|
||||||
| QString CMessages::ToUtf8Printable ( const QString& text ) | ||||||
| { | ||||||
| QString plainText; | ||||||
|
|
||||||
| #ifndef HEADLESS | ||||||
| { | ||||||
| QTextEdit textEdit; | ||||||
|
|
||||||
| textEdit.setText ( text ); // Text can be html... | ||||||
| plainText = textEdit.toPlainText(); // Text will be plain Text ! | ||||||
| } | ||||||
| #else | ||||||
| plainText = text; | ||||||
| // Remove htmlBold | ||||||
| plainText.replace ( "<b>", "" ); | ||||||
| plainText.replace ( "</b>", "" ); | ||||||
| // Translate htmlNewLine | ||||||
| plainText.replace ( "<br>", "\n" ); | ||||||
|
|
||||||
| // no multiple newlines | ||||||
| while ( plainText.contains ( "\n\n" ) ) | ||||||
| { | ||||||
| plainText.replace ( "\n\n", "\n" ); | ||||||
| } | ||||||
| #endif | ||||||
|
|
||||||
| #ifdef _WIN32 | ||||||
| // LF to CRLF | ||||||
| plainText.replace ( "\n", "\r\n" ); | ||||||
| #endif | ||||||
|
|
||||||
| return qUtf8Printable ( plainText ); | ||||||
| } | ||||||
|
|
||||||
| /******************************************************************************\ | ||||||
| * Message Boxes * | ||||||
| \******************************************************************************/ | ||||||
|
|
||||||
| void CMessages::ShowError ( QString strError ) | ||||||
| { | ||||||
| #ifndef HEADLESS | ||||||
| QMessageBox::critical ( pMainForm, strMainFormName + ": " + QObject::tr ( "Error" ), strError, QObject::tr ( "Ok" ), nullptr ); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Is "Ok" really not an integrated button?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Well we first have to merge this one before we can modify the other messagebox calls of coarse...
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The standard messagebox has one button and here we just set the button text.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. Thanks for the note here. Concerning the QObject::tr() call: I think the code doesn't use it in other places? Should't we stay consistent and use tr()
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, however the default values exist for a reason. Also they're translated automatically.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, but since tr is a static QObject function it is only available in a QObject, so since here we are not a QOject here using QObject::tr here is actually the same as tr in a QObject. |
||||||
| #else | ||||||
| qCritical().noquote() << "Error: " << ToUtf8Printable ( strError ); | ||||||
| #endif | ||||||
| } | ||||||
|
|
||||||
| void CMessages::ShowWarning ( QString strWarning ) | ||||||
| { | ||||||
| #ifndef HEADLESS | ||||||
| QMessageBox::warning ( pMainForm, strMainFormName + ": " + QObject::tr ( "Warning" ), strWarning, QObject::tr ( "Ok" ), nullptr ); | ||||||
| #else | ||||||
| qWarning().noquote() << "Warning: " << ToUtf8Printable ( strWarning ); | ||||||
| #endif | ||||||
| } | ||||||
|
|
||||||
| void CMessages::ShowInfo ( QString strInfo ) | ||||||
| { | ||||||
| #ifndef HEADLESS | ||||||
| QMessageBox::information ( pMainForm, strMainFormName + ": " + QObject::tr ( "Information" ), strInfo, QObject::tr ( "Ok" ), nullptr ); | ||||||
| #else | ||||||
| qInfo().noquote() << "Info: " << ToUtf8Printable ( strInfo ); | ||||||
| #endif | ||||||
| } | ||||||
|
|
||||||
| bool CMessages::ShowErrorWait ( QString strError, const QString strActionButtonText, const QString strAbortButtonText, bool bDefault ) | ||||||
| { | ||||||
| #ifndef HEADLESS | ||||||
| QMessageBox msgBox ( pMainForm ); | ||||||
| QPushButton* actionButton = msgBox.addButton ( strActionButtonText, QMessageBox::ActionRole ); | ||||||
| QPushButton* abortButton = NULL; | ||||||
| if ( !strAbortButtonText.isEmpty() ) | ||||||
| { | ||||||
| abortButton = msgBox.addButton ( strAbortButtonText, QMessageBox::ActionRole ); | ||||||
| if ( bDefault ) | ||||||
| { | ||||||
| msgBox.setDefaultButton ( actionButton ); | ||||||
| } | ||||||
| else | ||||||
| { | ||||||
| msgBox.setDefaultButton ( abortButton ); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| msgBox.setIcon ( QMessageBox::Icon::Critical ); | ||||||
| msgBox.setWindowTitle ( strMainFormName + ": " + QObject::tr ( "Error" ) ); | ||||||
| msgBox.setText ( strError ); | ||||||
| return ( msgBox.exec() == 0 ); | ||||||
| #else | ||||||
| Q_UNUSED ( strActionButtonText ) | ||||||
| Q_UNUSED ( strAbortButtonText ) | ||||||
| qCritical().noquote() << "Error: " << ToUtf8Printable ( strError ); | ||||||
| return bDefault; | ||||||
| #endif | ||||||
| } | ||||||
|
|
||||||
| bool CMessages::ShowWarningWait ( QString strWarning, const QString strActionButtonText, const QString strAbortButtonText, bool bDefault ) | ||||||
| { | ||||||
| #ifndef HEADLESS | ||||||
| QMessageBox msgBox ( pMainForm ); | ||||||
| QPushButton* actionButton = msgBox.addButton ( strActionButtonText, QMessageBox::ActionRole ); | ||||||
| QPushButton* abortButton = NULL; | ||||||
| if ( !strAbortButtonText.isEmpty() ) | ||||||
| { | ||||||
| abortButton = msgBox.addButton ( strAbortButtonText, QMessageBox::ActionRole ); | ||||||
| if ( bDefault ) | ||||||
| { | ||||||
| msgBox.setDefaultButton ( actionButton ); | ||||||
| } | ||||||
| else | ||||||
| { | ||||||
| msgBox.setDefaultButton ( abortButton ); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| msgBox.setIcon ( QMessageBox::Icon::Warning ); | ||||||
| msgBox.setWindowTitle ( strMainFormName + ": " + QObject::tr ( "Warning" ) ); | ||||||
| msgBox.setText ( strWarning ); | ||||||
| return ( msgBox.exec() == 0 ); | ||||||
| #else | ||||||
| Q_UNUSED ( strActionButtonText ) | ||||||
| Q_UNUSED ( strAbortButtonText ) | ||||||
| qWarning().noquote() << "Warning: " << ToUtf8Printable ( strWarning ); | ||||||
| return bDefault; | ||||||
| #endif | ||||||
| } | ||||||
|
|
||||||
| bool CMessages::ShowInfoWait ( QString strInfo, const QString strActionButtonText, const QString strAbortButtonText, bool bDefault ) | ||||||
| { | ||||||
| #ifndef HEADLESS | ||||||
| QMessageBox msgBox ( pMainForm ); | ||||||
| QPushButton* actionButton = msgBox.addButton ( strActionButtonText, QMessageBox::ActionRole ); | ||||||
| QPushButton* abortButton = NULL; | ||||||
| if ( !strAbortButtonText.isEmpty() ) | ||||||
| { | ||||||
| abortButton = msgBox.addButton ( strAbortButtonText, QMessageBox::ActionRole ); | ||||||
| if ( bDefault ) | ||||||
| { | ||||||
| msgBox.setDefaultButton ( actionButton ); | ||||||
| } | ||||||
| else | ||||||
| { | ||||||
| msgBox.setDefaultButton ( abortButton ); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| msgBox.setIcon ( QMessageBox::Icon::Warning ); | ||||||
| msgBox.setWindowTitle ( strMainFormName + ": " + QObject::tr ( "Info" ) ); | ||||||
| msgBox.setText ( strInfo ); | ||||||
| return ( msgBox.exec() == 0 ); | ||||||
| #else | ||||||
| Q_UNUSED ( strActionButtonText ) | ||||||
| Q_UNUSED ( strAbortButtonText ) | ||||||
| qInfo().noquote() << "Info: " << ToUtf8Printable ( strInfo ); | ||||||
| return bDefault; | ||||||
| #endif | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| #pragma once | ||
|
|
||
| /******************************************************************************\ | ||
| * Copyright (c) 2022 | ||
| * Author(s): | ||
| * Peter Goderie (pgScorpio) | ||
| * | ||
| * Use this static class to show basic Error, Warning and Info messages | ||
| * | ||
| * For own created special message boxes you should always use | ||
| * CMessages::MainForm() as parent parameter | ||
| * and CMessages::MainFormName() as base of the title parameter | ||
| \******************************************************************************/ | ||
|
|
||
| #include <QString> | ||
| #ifndef HEADLESS | ||
| # include <QMessageBox> | ||
| # define tMainform QDialog | ||
| #else | ||
| # include <QDebug> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does QDebug include? Does it add bloat on a release build?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. QDebug includes what's neccesary for the cli streams. |
||
| # define tMainform void | ||
| #endif | ||
|
|
||
| // html text macro's (for use in message texts) | ||
| #define htmlBold( T ) "<b>" + T + "</b>" | ||
| #define htmlNewLine() "<br>" | ||
|
|
||
| class CMessages | ||
| { | ||
| protected: | ||
| static tMainform* pMainForm; | ||
| static QString strMainFormName; | ||
|
|
||
| public: | ||
| static QString ToUtf8Printable ( const QString& text ); // Converts html to plain utf8 text | ||
|
|
||
| static void init ( tMainform* theMainForm, QString theMainFormName ) | ||
| { | ||
| pMainForm = theMainForm; | ||
| strMainFormName = theMainFormName; | ||
| } | ||
|
|
||
| static tMainform* MainForm() { return pMainForm; } | ||
| static const QString& MainFormName() { return strMainFormName; } | ||
|
|
||
| static void ShowError ( QString strError ); | ||
| static void ShowWarning ( QString strWarning ); | ||
| static void ShowInfo ( QString strInfo ); | ||
|
|
||
| // Modal message boxes (wait for user confirmation in gui mode) | ||
| // Returns true if action button is pressed | ||
| // Returns false if abort button is pressed | ||
| // | ||
| // NOTE: The abort button is only present if a non empty strAbortButtonText is given | ||
| // if this is the case bDefault pre-selects the default Button. | ||
| // | ||
| // NOTE: In HEADLESS mode there is NO wait and always bDefault is returned. | ||
|
|
||
| static bool ShowErrorWait ( QString strError, | ||
| const QString strActionButtonText = "Ok", | ||
| const QString strAbortButtonText = "", | ||
| bool bDefault = true ); | ||
| static bool ShowWarningWait ( QString strWarning, | ||
| const QString strActionButtonText = "Ok", | ||
| const QString strAbortButtonText = "", | ||
| bool bDefault = true ); | ||
| static bool ShowInfoWait ( QString strInfo, | ||
| const QString strActionButtonText = "Ok", | ||
| const QString strAbortButtonText = "", | ||
| bool bDefault = true ); | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.