QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
qgsmessagebaritem.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmessagebaritem.h - description
3 -------------------
4 begin : August 2013
5 copyright : (C) 2013 by Denis Rouzaud
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgsapplication.h"
19#include "qgsmessagebaritem.h"
20#include "qgsmessagebar.h"
21#include "qgsgui.h"
22#include "qgsnative.h"
23#include <QHBoxLayout>
24#include <QLabel>
25#include <QTextBrowser>
26#include <QDesktopServices>
27#include <QFileInfo>
28
29QgsMessageBarItem::QgsMessageBarItem( const QString &text, Qgis::MessageLevel level, int duration, QWidget *parent )
30 : QWidget( parent )
31 , mText( text )
32 , mLevel( level )
33 , mDuration( duration )
34{
35 writeContent();
36}
37
38QgsMessageBarItem::QgsMessageBarItem( const QString &title, const QString &text, Qgis::MessageLevel level, int duration, QWidget *parent )
39 : QWidget( parent )
40 , mTitle( title )
41 , mText( text )
42 , mLevel( level )
43 , mDuration( duration )
44{
45 writeContent();
46}
47
48QgsMessageBarItem::QgsMessageBarItem( const QString &title, const QString &text, QWidget *widget, Qgis::MessageLevel level, int duration, QWidget *parent )
49 : QWidget( parent )
50 , mTitle( title )
51 , mText( text )
52 , mLevel( level )
53 , mDuration( duration )
54 , mWidget( widget )
55 , mUserIcon( QIcon() )
56
57{
58 writeContent();
59}
60
61QgsMessageBarItem::QgsMessageBarItem( QWidget *widget, Qgis::MessageLevel level, int duration, QWidget *parent )
62 : QWidget( parent )
63 , mLevel( level )
64 , mDuration( duration )
65 , mWidget( widget )
66 , mUserIcon( QIcon() )
67
68{
69 writeContent();
70}
71
72void QgsMessageBarItem::writeContent()
73{
74 if ( mDuration < 0 )
75 mDuration = QgsMessageBar::defaultMessageTimeout( mLevel );
76
77 if ( !mLayout )
78 {
79 mLayout = new QHBoxLayout( this );
80 mLayout->setContentsMargins( 0, 0, 0, 0 );
81 mTextBrowser = nullptr;
82 mLblIcon = nullptr;
83 }
84
85 // ICON
86 if ( !mLblIcon )
87 {
88 mLblIcon = new QLabel( this );
89 mLayout->addWidget( mLblIcon );
90 }
91 QIcon icon;
92 if ( !mUserIcon.isNull() )
93 {
94 icon = mUserIcon;
95 }
96 else
97 {
98 QString msgIcon( QStringLiteral( "/mIconInfo.svg" ) );
99 switch ( mLevel )
100 {
101 case Qgis::MessageLevel::Critical:
102 msgIcon = QStringLiteral( "/mIconCritical.svg" );
103 break;
104 case Qgis::MessageLevel::Warning:
105 msgIcon = QStringLiteral( "/mIconWarning.svg" );
106 break;
107 case Qgis::MessageLevel::Success:
108 msgIcon = QStringLiteral( "/mIconSuccess.svg" );
109 break;
110 default:
111 break;
112 }
114 }
115 const int iconSize = std::max( 24.0, fontMetrics().height() * 1.2 );
116 mLblIcon->setPixmap( icon.pixmap( iconSize ) );
117
118
119 // STYLESHEETS
120 QString contentStyleSheet;
121 if ( mLevel == Qgis::MessageLevel::Success )
122 {
123 mStyleSheet = QStringLiteral( "QgsMessageBar { background-color: #dff0d8; border: 1px solid #8e998a; } "
124 "QLabel,QTextEdit { color: black; } " );
125 contentStyleSheet = QStringLiteral( "<style> a, a:visited, a:hover { color:#268300; } </style>" );
126 }
127 else if ( mLevel == Qgis::MessageLevel::Critical )
128 {
129 mStyleSheet = QStringLiteral( "QgsMessageBar { background-color: #d65253; border: 1px solid #9b3d3d; } "
130 "QLabel,QTextEdit { color: white; } " );
131 contentStyleSheet = QStringLiteral( "<style>a, a:visited, a:hover { color:#4e0001; }</style>" );
132 }
133 else if ( mLevel == Qgis::MessageLevel::Warning )
134 {
135 mStyleSheet = QStringLiteral( "QgsMessageBar { background-color: #ffc800; border: 1px solid #e0aa00; } "
136 "QLabel,QTextEdit { color: black; } " );
137 contentStyleSheet = QStringLiteral( "<style>a, a:visited, a:hover { color:#945a00; }</style>" );
138 }
139 else if ( mLevel == Qgis::MessageLevel::Info )
140 {
141 mStyleSheet = QStringLiteral( "QgsMessageBar { background-color: #e7f5fe; border: 1px solid #b9cfe4; } "
142 "QLabel,QTextEdit { color: #2554a1; } " );
143 contentStyleSheet = QStringLiteral( "<style>a, a:visited, a:hover { color:#3bb2fe; }</style>" );
144 }
145 mStyleSheet += QLatin1String( "QLabel#mItemCount { font-style: italic; }" );
146
147 // TITLE AND TEXT
148 if ( mTitle.isEmpty() && mText.isEmpty() )
149 {
150 if ( mTextBrowser )
151 {
152 delete mTextBrowser;
153 mTextBrowser = nullptr;
154 }
155 }
156 else
157 {
158 if ( !mTextBrowser )
159 {
160 mTextBrowser = new QTextBrowser( this );
161 mTextBrowser->setObjectName( QStringLiteral( "textEdit" ) );
162 mTextBrowser->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Maximum );
163 mTextBrowser->setReadOnly( true );
164 mTextBrowser->setOpenLinks( false );
165 connect( mTextBrowser, &QTextBrowser::anchorClicked, this, &QgsMessageBarItem::urlClicked );
166
167 mTextBrowser->setFrameShape( QFrame::NoFrame );
168 // stylesheet set here so Qt-style substitued scrollbar arrows can show within limited height
169 // adjusts to height of font set in app options
170 mTextBrowser->setStyleSheet( "QTextEdit { background-color: rgba(0,0,0,0); margin-top: 0.25em; max-height: 1.75em; min-height: 1.75em; } "
171 "QScrollBar { background-color: rgba(0,0,0,0); } "
172 "QScrollBar::add-page,QScrollBar::sub-page,QScrollBar::handle { background-color: rgba(0,0,0,0); color: rgba(0,0,0,0); } "
173 "QScrollBar::up-arrow,QScrollBar::down-arrow { color: rgb(0,0,0); } " );
174 mLayout->addWidget( mTextBrowser );
175 }
176 QString content = mText;
177 if ( !mTitle.isEmpty() )
178 {
179 // add ':' to end of title
180 QString t = mTitle.trimmed();
181 if ( !content.isEmpty() && !t.endsWith( ':' ) && !t.endsWith( QLatin1String( ": " ) ) )
182 t += QLatin1String( ": " );
183 content.prepend( QStringLiteral( "<b>" ) + t + " </b>" );
184 }
185 content.prepend( contentStyleSheet );
186 mTextBrowser->setText( content );
187 }
188
189 // WIDGET
190 if ( mWidget )
191 {
192 QLayoutItem *item = mLayout->itemAt( 2 );
193 if ( !item || item->widget() != mWidget )
194 {
195 mLayout->addWidget( mWidget );
196 }
197 }
198}
199
201{
202 mText = text;
203 writeContent();
204 return this;
205}
206
208{
209 return mText;
210}
211
213{
214 mTitle = title;
215 writeContent();
216 return this;
217}
218
220{
221 return mTitle;
222}
223
225{
226 if ( level != mLevel )
227 {
228 mLevel = level;
229 writeContent();
230 emit styleChanged( mStyleSheet );
231 }
232
233 return this;
234}
235
237{
238 return mLevel;
239}
240
242{
243 if ( mWidget )
244 {
245 QLayoutItem *item = nullptr;
246 item = mLayout->itemAt( 2 );
247 if ( item->widget() == mWidget )
248 {
249 delete item->widget();
250 }
251 }
252 mWidget = widget;
253 writeContent();
254 return this;
255}
256
258{
259 return mWidget;
260}
261
263{
264 mUserIcon = icon;
265 return this;
266}
267
269{
270 return mUserIcon;
271}
272
273
275{
276 mDuration = duration;
277 return this;
278}
279
281{
282 if ( !mMessageBar )
283 return;
284
285 mMessageBar->popWidget( this );
286}
287
288void QgsMessageBarItem::urlClicked( const QUrl &url )
289{
290 const QFileInfo file( url.toLocalFile() );
291 if ( file.exists() && !file.isDir() )
292 QgsGui::nativePlatformInterface()->openFileExplorerAndSelectFile( url.toLocalFile() );
293 else
294 QDesktopServices::openUrl( url );
295 dismiss();
296}
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:99
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...
Definition: qgsgui.cpp:78
Represents an item shown within a QgsMessageBar widget.
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.
bool popWidget(QgsMessageBarItem *item)
Remove the specified item from the bar, and display the next most recent one in the stack.
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.