QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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
6  email : [email protected]
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 
29 QgsMessageBarItem::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 
38 QgsMessageBarItem::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 
48 QgsMessageBarItem::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 
61 QgsMessageBarItem::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 
72 void QgsMessageBarItem::writeContent()
73 {
74  if ( !mLayout )
75  {
76  mLayout = new QHBoxLayout( this );
77  mLayout->setContentsMargins( 0, 0, 0, 0 );
78  mTextBrowser = nullptr;
79  mLblIcon = nullptr;
80  }
81 
82  // ICON
83  if ( !mLblIcon )
84  {
85  mLblIcon = new QLabel( this );
86  mLayout->addWidget( mLblIcon );
87  }
88  QIcon icon;
89  if ( !mUserIcon.isNull() )
90  {
91  icon = mUserIcon;
92  }
93  else
94  {
95  QString msgIcon( QStringLiteral( "/mIconInfo.svg" ) );
96  switch ( mLevel )
97  {
98  case Qgis::Critical:
99  msgIcon = QStringLiteral( "/mIconCritical.svg" );
100  break;
101  case Qgis::Warning:
102  msgIcon = QStringLiteral( "/mIconWarning.svg" );
103  break;
104  case Qgis::Success:
105  msgIcon = QStringLiteral( "/mIconSuccess.svg" );
106  break;
107  default:
108  break;
109  }
110  icon = QgsApplication::getThemeIcon( msgIcon );
111  }
112  const int iconSize = std::max( 24.0, fontMetrics().height() * 1.2 );
113  mLblIcon->setPixmap( icon.pixmap( iconSize ) );
114 
115 
116  // STYLESHEETS
117  QString contentStyleSheet;
118  if ( mLevel == Qgis::Success )
119  {
120  mStyleSheet = QStringLiteral( "QgsMessageBar { background-color: #dff0d8; border: 1px solid #8e998a; } "
121  "QLabel,QTextEdit { color: black; } " );
122  contentStyleSheet = QStringLiteral( "<style> a, a:visited, a:hover { color:#268300; } </style>" );
123  }
124  else if ( mLevel == Qgis::Critical )
125  {
126  mStyleSheet = QStringLiteral( "QgsMessageBar { background-color: #d65253; border: 1px solid #9b3d3d; } "
127  "QLabel,QTextEdit { color: white; } " );
128  contentStyleSheet = QStringLiteral( "<style>a, a:visited, a:hover { color:#4e0001; }</style>" );
129  }
130  else if ( mLevel == Qgis::Warning )
131  {
132  mStyleSheet = QStringLiteral( "QgsMessageBar { background-color: #ffc800; border: 1px solid #e0aa00; } "
133  "QLabel,QTextEdit { color: black; } " );
134  contentStyleSheet = QStringLiteral( "<style>a, a:visited, a:hover { color:#945a00; }</style>" );
135  }
136  else if ( mLevel == Qgis::Info )
137  {
138  mStyleSheet = QStringLiteral( "QgsMessageBar { background-color: #e7f5fe; border: 1px solid #b9cfe4; } "
139  "QLabel,QTextEdit { color: #2554a1; } " );
140  contentStyleSheet = QStringLiteral( "<style>a, a:visited, a:hover { color:#3bb2fe; }</style>" );
141  }
142  mStyleSheet += QLatin1String( "QLabel#mItemCount { font-style: italic; }" );
143 
144  // TITLE AND TEXT
145  if ( mTitle.isEmpty() && mText.isEmpty() )
146  {
147  if ( mTextBrowser )
148  {
149  delete mTextBrowser;
150  mTextBrowser = nullptr;
151  }
152  }
153  else
154  {
155  if ( !mTextBrowser )
156  {
157  mTextBrowser = new QTextBrowser( this );
158  mTextBrowser->setObjectName( QStringLiteral( "textEdit" ) );
159  mTextBrowser->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Maximum );
160  mTextBrowser->setReadOnly( true );
161  mTextBrowser->setOpenLinks( false );
162  connect( mTextBrowser, &QTextBrowser::anchorClicked, this, &QgsMessageBarItem::urlClicked );
163 
164  mTextBrowser->setFrameShape( QFrame::NoFrame );
165  // stylesheet set here so Qt-style substitued scrollbar arrows can show within limited height
166  // adjusts to height of font set in app options
167  mTextBrowser->setStyleSheet( "QTextEdit { background-color: rgba(0,0,0,0); margin-top: 0.25em; max-height: 1.75em; min-height: 1.75em; } "
168  "QScrollBar { background-color: rgba(0,0,0,0); } "
169  "QScrollBar::add-page,QScrollBar::sub-page,QScrollBar::handle { background-color: rgba(0,0,0,0); color: rgba(0,0,0,0); } "
170  "QScrollBar::up-arrow,QScrollBar::down-arrow { color: rgb(0,0,0); } " );
171  mLayout->addWidget( mTextBrowser );
172  }
173  QString content = mText;
174  if ( !mTitle.isEmpty() )
175  {
176  // add ':' to end of title
177  QString t = mTitle.trimmed();
178  if ( !content.isEmpty() && !t.endsWith( ':' ) && !t.endsWith( QLatin1String( ": " ) ) )
179  t += QLatin1String( ": " );
180  content.prepend( QStringLiteral( "<b>" ) + t + " </b>" );
181  }
182  content.prepend( contentStyleSheet );
183  mTextBrowser->setText( content );
184  }
185 
186  // WIDGET
187  if ( mWidget )
188  {
189  QLayoutItem *item = mLayout->itemAt( 2 );
190  if ( !item || item->widget() != mWidget )
191  {
192  mLayout->addWidget( mWidget );
193  }
194  }
195 }
196 
198 {
199  mText = text;
200  writeContent();
201  return this;
202 }
203 
204 QString QgsMessageBarItem::text() const
205 {
206  return mText;
207 }
208 
210 {
211  mTitle = title;
212  writeContent();
213  return this;
214 }
215 
217 {
218  return mTitle;
219 }
220 
222 {
223  if ( level != mLevel )
224  {
225  mLevel = level;
226  writeContent();
227  emit styleChanged( mStyleSheet );
228  }
229 
230  return this;
231 }
232 
234 {
235  return mLevel;
236 }
237 
239 {
240  if ( mWidget )
241  {
242  QLayoutItem *item = nullptr;
243  item = mLayout->itemAt( 2 );
244  if ( item->widget() == mWidget )
245  {
246  delete item->widget();
247  }
248  }
249  mWidget = widget;
250  writeContent();
251  return this;
252 }
253 
255 {
256  return mWidget;
257 }
258 
260 {
261  mUserIcon = icon;
262  return this;
263 }
264 
266 {
267  return mUserIcon;
268 }
269 
270 
272 {
273  mDuration = duration;
274  return this;
275 }
276 
278 {
279  if ( !mMessageBar )
280  return;
281 
282  mMessageBar->popWidget( this );
283 }
284 
285 void QgsMessageBarItem::urlClicked( const QUrl &url )
286 {
287  QFileInfo file( url.toLocalFile() );
288  if ( file.exists() && !file.isDir() )
289  QgsGui::instance()->nativePlatformInterface()->openFileExplorerAndSelectFile( url.toLocalFile() );
290  else
291  QDesktopServices::openUrl( url );
292  dismiss();
293 }
QgsMessageBarItem::widget
QWidget * widget() const
Returns the widget for the message.
Definition: qgsmessagebaritem.cpp:254
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
Definition: qgsapplication.cpp:626
qgsmessagebaritem.h
QgsMessageBarItem::title
QString title() const
Returns the title for the message.
Definition: qgsmessagebaritem.cpp:216
QgsMessageBarItem::setWidget
QgsMessageBarItem * setWidget(QWidget *widget)
Sets a custom widget to show in the item.
Definition: qgsmessagebaritem.cpp:238
qgsgui.h
Qgis::Warning
@ Warning
Definition: qgis.h:91
Qgis::Success
@ Success
Definition: qgis.h:93
QgsMessageBarItem::level
Qgis::MessageLevel level() const
Returns the message level for the message.
Definition: qgsmessagebaritem.cpp:233
QgsMessageBar::popWidget
bool popWidget(QgsMessageBarItem *item)
Remove the specified item from the bar, and display the next most recent one in the stack.
Definition: qgsmessagebar.cpp:160
QgsMessageBarItem::setText
QgsMessageBarItem * setText(const QString &text)
Sets the message text to show in the item.
Definition: qgsmessagebaritem.cpp:197
QgsMessageBarItem::icon
QIcon icon() const
Returns the icon for the message.
Definition: qgsmessagebaritem.cpp:265
QgsGuiUtils::iconSize
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
Definition: qgsguiutils.cpp:250
qgsapplication.h
QgsMessageBarItem::duration
int duration() const
Returns the duration (in seconds) of the message.
Definition: qgsmessagebaritem.h:175
Qgis::Info
@ Info
Definition: qgis.h:90
QgsMessageBarItem::QgsMessageBarItem
QgsMessageBarItem(const QString &text, Qgis::MessageLevel level=Qgis::Info, int duration=0, QWidget *parent=nullptr)
Constructor for QgsMessageBarItem, containing a message with the specified text to be displayed on th...
Definition: qgsmessagebaritem.cpp:29
QgsMessageBarItem
Represents an item shown within a QgsMessageBar widget.
Definition: qgsmessagebaritem.h:39
QgsMessageBarItem::setIcon
QgsMessageBarItem * setIcon(const QIcon &icon)
Sets the icon associated with the message.
Definition: qgsmessagebaritem.cpp:259
QgsMessageBarItem::setTitle
QgsMessageBarItem * setTitle(const QString &title)
Sets the title for in the item.
Definition: qgsmessagebaritem.cpp:209
qgsmessagebar.h
QgsMessageBarItem::dismiss
void dismiss()
Dismisses the item, removing it from the message bar and deleting it.
Definition: qgsmessagebaritem.cpp:277
QgsMessageBarItem::text
QString text() const
Returns the text for the message.
Definition: qgsmessagebaritem.cpp:204
QgsGui::nativePlatformInterface
static QgsNative * nativePlatformInterface()
Returns the global native interface, which offers abstraction to the host OS's underlying public inte...
Definition: qgsgui.cpp:69
QgsGui::instance
static QgsGui * instance()
Returns a pointer to the singleton instance.
Definition: qgsgui.cpp:63
Qgis::MessageLevel
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:89
QgsMessageBarItem::styleChanged
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...
QgsMessageBarItem::setDuration
QgsMessageBarItem * setDuration(int duration)
Sets the duration (in seconds) to show the message for.
Definition: qgsmessagebaritem.cpp:271
Qgis::Critical
@ Critical
Definition: qgis.h:92
QgsMessageBarItem::setLevel
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...
Definition: qgsmessagebaritem.cpp:221