25#include <QDesktopServices>
30#include <QTextBrowser>
32#include "moc_qgsmessagebaritem.cpp"
34using namespace Qt::StringLiterals;
62 , mUserIcon( QIcon() )
73 , mUserIcon( QIcon() )
79void QgsMessageBarItem::writeContent()
86 mLayout =
new QHBoxLayout(
this );
87 mLayout->setContentsMargins( 0, 0, 0, 0 );
88 mTextBrowser =
nullptr;
95 mLblIcon =
new QLabel(
this );
96 mLayout->addWidget( mLblIcon );
99 if ( !mUserIcon.isNull() )
105 QString msgIcon( u
"/mIconInfo.svg"_s );
109 msgIcon = u
"/mIconCritical.svg"_s;
112 msgIcon = u
"/mIconWarning.svg"_s;
115 msgIcon = u
"/mIconSuccess.svg"_s;
122 const int iconSize = std::max( 24.0, fontMetrics().height() * 1.2 );
123 mLblIcon->setPixmap(
icon.pixmap( iconSize ) );
127 QString contentStyleSheet;
130 mStyleSheet = QStringLiteral(
131 "QgsMessageBar { background-color: #dff0d8; border: 1px solid #8e998a; } "
132 "QLabel,QTextEdit { color: black; } "
134 contentStyleSheet = u
"<style> a, a:visited, a:hover { color:#268300; } </style>"_s;
138 mStyleSheet = QStringLiteral(
139 "QgsMessageBar { background-color: #d65253; border: 1px solid #9b3d3d; } "
140 "QLabel,QTextEdit { color: white; } "
142 contentStyleSheet = u
"<style>a, a:visited, a:hover { color:#4e0001; }</style>"_s;
146 mStyleSheet = QStringLiteral(
147 "QgsMessageBar { background-color: #ffc800; border: 1px solid #e0aa00; } "
148 "QLabel,QTextEdit { color: black; } "
150 contentStyleSheet = u
"<style>a, a:visited, a:hover { color:#945a00; }</style>"_s;
154 mStyleSheet = QStringLiteral(
155 "QgsMessageBar { background-color: #e7f5fe; border: 1px solid #b9cfe4; } "
156 "QLabel,QTextEdit { color: #2554a1; } "
158 contentStyleSheet = u
"<style>a, a:visited, a:hover { color:#3bb2fe; }</style>"_s;
160 mStyleSheet +=
"QLabel#mItemCount { font-style: italic; }"_L1;
163 if ( mTitle.isEmpty() && mText.isEmpty() )
168 mTextBrowser =
nullptr;
175 mTextBrowser =
new QTextBrowser(
this );
176 mTextBrowser->setObjectName( u
"textEdit"_s );
177 mTextBrowser->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Maximum );
178 mTextBrowser->setReadOnly(
true );
179 mTextBrowser->setOpenLinks(
false );
180 connect( mTextBrowser, &QTextBrowser::anchorClicked,
this, &QgsMessageBarItem::urlClicked );
182 mTextBrowser->setFrameShape( QFrame::NoFrame );
185 mTextBrowser->setStyleSheet(
186 "QTextEdit { background-color: rgba(0,0,0,0); margin-top: 0.25em; max-height: 1.75em; min-height: 1.75em; } "
187 "QScrollBar { background-color: rgba(0,0,0,0); } "
188 "QScrollBar::add-page,QScrollBar::sub-page,QScrollBar::handle { background-color: rgba(0,0,0,0); color: rgba(0,0,0,0); } "
189 "QScrollBar::up-arrow,QScrollBar::down-arrow { color: rgb(0,0,0); } "
191 mLayout->addWidget( mTextBrowser );
193 QString content = mText;
194 if ( !mTitle.isEmpty() )
197 QString t = mTitle.trimmed();
198 if ( !content.isEmpty() && !t.endsWith(
':' ) && !t.endsWith(
": "_L1 ) )
200 content.prepend( u
"<b>"_s + t +
" </b>" );
202 content.prepend( contentStyleSheet );
203 mTextBrowser->setText( content );
209 QLayoutItem *item = mLayout->itemAt( 2 );
210 if ( !item || item->widget() != mWidget )
212 mLayout->addWidget( mWidget );
243 if (
level != mLevel )
262 QLayoutItem *item =
nullptr;
263 item = mLayout->itemAt( 2 );
264 if ( item->widget() == mWidget )
266 delete item->widget();
302 mMessageBar->popWidget(
this );
305void QgsMessageBarItem::urlClicked(
const QUrl &url )
307 const QFileInfo file( url.toLocalFile() );
308 if ( file.exists() && !file.isDir() )
311 QDesktopServices::openUrl( url );
MessageLevel
Level for messages This will be used both for message log and message bar in application.
@ Warning
Warning message.
@ Critical
Critical/error message.
@ Info
Information message.
@ Success
Used for reporting a successful operation.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QgsNative * nativePlatformInterface()
Returns the global native interface, which offers abstraction to the host OS's underlying public inte...
void styleChanged(const QString &styleSheet)
Emitted when the item's message level has changed and the message bar style will need to be updated a...
QIcon icon() const
Returns the icon for the message.
QgsMessageBarItem * setLevel(Qgis::MessageLevel level)
Sets the message level for the item, which controls how the message bar is styled when the item is di...
int duration() const
Returns the duration (in seconds) of the message.
QgsMessageBarItem * setText(const QString &text)
Sets the message text to show in the item.
QgsMessageBarItem * setIcon(const QIcon &icon)
Sets the icon associated with the message.
QString text() const
Returns the text for the message.
QWidget * widget() const
Returns the widget for the message.
QgsMessageBarItem(const QString &text, Qgis::MessageLevel level=Qgis::MessageLevel::Info, int duration=0, QWidget *parent=nullptr)
Constructor for QgsMessageBarItem, containing a message with the specified text to be displayed on th...
QString title() const
Returns the title for the message.
QgsMessageBarItem * setTitle(const QString &title)
Sets the title for in the item.
Qgis::MessageLevel level() const
Returns the message level for the message.
QgsMessageBarItem * setDuration(int duration)
Sets the duration (in seconds) to show the message for.
QgsMessageBarItem * setWidget(QWidget *widget)
Sets a custom widget to show in the item.
void dismiss()
Dismisses the item, removing it from the message bar and deleting it.
static int defaultMessageTimeout(Qgis::MessageLevel level=Qgis::MessageLevel::NoLevel)
Returns the default timeout in seconds for timed messages of the specified level.
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.