QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
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 "moc_qgsmessagebaritem.cpp"
21#include "qgsmessagebar.h"
22#include "qgsgui.h"
23#include "qgsnative.h"
24#include <QHBoxLayout>
25#include <QLabel>
26#include <QTextBrowser>
27#include <QDesktopServices>
28#include <QFileInfo>
29
30QgsMessageBarItem::QgsMessageBarItem( const QString &text, Qgis::MessageLevel level, int duration, QWidget *parent )
31 : QWidget( parent )
32 , mText( text )
33 , mLevel( level )
34 , mDuration( duration )
35{
36 writeContent();
37}
38
39QgsMessageBarItem::QgsMessageBarItem( const QString &title, const QString &text, Qgis::MessageLevel level, int duration, QWidget *parent )
40 : QWidget( parent )
41 , mTitle( title )
42 , mText( text )
43 , mLevel( level )
44 , mDuration( duration )
45{
46 writeContent();
47}
48
49QgsMessageBarItem::QgsMessageBarItem( const QString &title, const QString &text, QWidget *widget, Qgis::MessageLevel level, int duration, QWidget *parent )
50 : QWidget( parent )
51 , mTitle( title )
52 , mText( text )
53 , mLevel( level )
54 , mDuration( duration )
55 , mWidget( widget )
56 , mUserIcon( QIcon() )
57
58{
59 writeContent();
60}
61
62QgsMessageBarItem::QgsMessageBarItem( QWidget *widget, Qgis::MessageLevel level, int duration, QWidget *parent )
63 : QWidget( parent )
64 , mLevel( level )
65 , mDuration( duration )
66 , mWidget( widget )
67 , mUserIcon( QIcon() )
68
69{
70 writeContent();
71}
72
73void QgsMessageBarItem::writeContent()
74{
75 if ( mDuration < 0 )
76 mDuration = QgsMessageBar::defaultMessageTimeout( mLevel );
77
78 if ( !mLayout )
79 {
80 mLayout = new QHBoxLayout( this );
81 mLayout->setContentsMargins( 0, 0, 0, 0 );
82 mTextBrowser = nullptr;
83 mLblIcon = nullptr;
84 }
85
86 // ICON
87 if ( !mLblIcon )
88 {
89 mLblIcon = new QLabel( this );
90 mLayout->addWidget( mLblIcon );
91 }
92 QIcon icon;
93 if ( !mUserIcon.isNull() )
94 {
95 icon = mUserIcon;
96 }
97 else
98 {
99 QString msgIcon( QStringLiteral( "/mIconInfo.svg" ) );
100 switch ( mLevel )
101 {
103 msgIcon = QStringLiteral( "/mIconCritical.svg" );
104 break;
106 msgIcon = QStringLiteral( "/mIconWarning.svg" );
107 break;
109 msgIcon = QStringLiteral( "/mIconSuccess.svg" );
110 break;
111 default:
112 break;
113 }
115 }
116 const int iconSize = std::max( 24.0, fontMetrics().height() * 1.2 );
117 mLblIcon->setPixmap( icon.pixmap( iconSize ) );
118
119
120 // STYLESHEETS
121 QString contentStyleSheet;
122 if ( mLevel == Qgis::MessageLevel::Success )
123 {
124 mStyleSheet = QStringLiteral( "QgsMessageBar { background-color: #dff0d8; border: 1px solid #8e998a; } "
125 "QLabel,QTextEdit { color: black; } " );
126 contentStyleSheet = QStringLiteral( "<style> a, a:visited, a:hover { color:#268300; } </style>" );
127 }
128 else if ( mLevel == Qgis::MessageLevel::Critical )
129 {
130 mStyleSheet = QStringLiteral( "QgsMessageBar { background-color: #d65253; border: 1px solid #9b3d3d; } "
131 "QLabel,QTextEdit { color: white; } " );
132 contentStyleSheet = QStringLiteral( "<style>a, a:visited, a:hover { color:#4e0001; }</style>" );
133 }
134 else if ( mLevel == Qgis::MessageLevel::Warning )
135 {
136 mStyleSheet = QStringLiteral( "QgsMessageBar { background-color: #ffc800; border: 1px solid #e0aa00; } "
137 "QLabel,QTextEdit { color: black; } " );
138 contentStyleSheet = QStringLiteral( "<style>a, a:visited, a:hover { color:#945a00; }</style>" );
139 }
140 else if ( mLevel == Qgis::MessageLevel::Info )
141 {
142 mStyleSheet = QStringLiteral( "QgsMessageBar { background-color: #e7f5fe; border: 1px solid #b9cfe4; } "
143 "QLabel,QTextEdit { color: #2554a1; } " );
144 contentStyleSheet = QStringLiteral( "<style>a, a:visited, a:hover { color:#3bb2fe; }</style>" );
145 }
146 mStyleSheet += QLatin1String( "QLabel#mItemCount { font-style: italic; }" );
147
148 // TITLE AND TEXT
149 if ( mTitle.isEmpty() && mText.isEmpty() )
150 {
151 if ( mTextBrowser )
152 {
153 delete mTextBrowser;
154 mTextBrowser = nullptr;
155 }
156 }
157 else
158 {
159 if ( !mTextBrowser )
160 {
161 mTextBrowser = new QTextBrowser( this );
162 mTextBrowser->setObjectName( QStringLiteral( "textEdit" ) );
163 mTextBrowser->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Maximum );
164 mTextBrowser->setReadOnly( true );
165 mTextBrowser->setOpenLinks( false );
166 connect( mTextBrowser, &QTextBrowser::anchorClicked, this, &QgsMessageBarItem::urlClicked );
167
168 mTextBrowser->setFrameShape( QFrame::NoFrame );
169 // stylesheet set here so Qt-style substituted scrollbar arrows can show within limited height
170 // adjusts to height of font set in app options
171 mTextBrowser->setStyleSheet( "QTextEdit { background-color: rgba(0,0,0,0); margin-top: 0.25em; max-height: 1.75em; min-height: 1.75em; } "
172 "QScrollBar { background-color: rgba(0,0,0,0); } "
173 "QScrollBar::add-page,QScrollBar::sub-page,QScrollBar::handle { background-color: rgba(0,0,0,0); color: rgba(0,0,0,0); } "
174 "QScrollBar::up-arrow,QScrollBar::down-arrow { color: rgb(0,0,0); } " );
175 mLayout->addWidget( mTextBrowser );
176 }
177 QString content = mText;
178 if ( !mTitle.isEmpty() )
179 {
180 // add ':' to end of title
181 QString t = mTitle.trimmed();
182 if ( !content.isEmpty() && !t.endsWith( ':' ) && !t.endsWith( QLatin1String( ": " ) ) )
183 t += QLatin1String( ": " );
184 content.prepend( QStringLiteral( "<b>" ) + t + " </b>" );
185 }
186 content.prepend( contentStyleSheet );
187 mTextBrowser->setText( content );
188 }
189
190 // WIDGET
191 if ( mWidget )
192 {
193 QLayoutItem *item = mLayout->itemAt( 2 );
194 if ( !item || item->widget() != mWidget )
195 {
196 mLayout->addWidget( mWidget );
197 }
198 }
199}
200
202{
203 mText = text;
204 writeContent();
205 return this;
206}
207
209{
210 return mText;
211}
212
214{
215 mTitle = title;
216 writeContent();
217 return this;
218}
219
221{
222 return mTitle;
223}
224
226{
227 if ( level != mLevel )
228 {
229 mLevel = level;
230 writeContent();
231 emit styleChanged( mStyleSheet );
232 }
233
234 return this;
235}
236
238{
239 return mLevel;
240}
241
243{
244 if ( mWidget )
245 {
246 QLayoutItem *item = nullptr;
247 item = mLayout->itemAt( 2 );
248 if ( item->widget() == mWidget )
249 {
250 delete item->widget();
251 }
252 }
253 mWidget = widget;
254 writeContent();
255 return this;
256}
257
259{
260 return mWidget;
261}
262
264{
265 mUserIcon = icon;
266 return this;
267}
268
270{
271 return mUserIcon;
272}
273
274
276{
277 mDuration = duration;
278 return this;
279}
280
282{
283 if ( !mMessageBar )
284 return;
285
286 mMessageBar->popWidget( this );
287}
288
289void QgsMessageBarItem::urlClicked( const QUrl &url )
290{
291 const QFileInfo file( url.toLocalFile() );
292 if ( file.exists() && !file.isDir() )
293 QgsGui::nativePlatformInterface()->openFileExplorerAndSelectFile( url.toLocalFile() );
294 else
295 QDesktopServices::openUrl( url );
296 dismiss();
297}
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition qgis.h:154
@ Warning
Warning message.
Definition qgis.h:156
@ Critical
Critical/error message.
Definition qgis.h:157
@ Info
Information message.
Definition qgis.h:155
@ Success
Used for reporting a successful operation.
Definition qgis.h:158
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:85
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.