QGIS API Documentation  3.2.0-Bonn (bc43194)
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 
22 #include <QHBoxLayout>
23 #include <QLabel>
24 #include <QTextBrowser>
25 #include <QDesktopServices>
26 
27 QgsMessageBarItem::QgsMessageBarItem( const QString &text, Qgis::MessageLevel level, int duration, QWidget *parent )
28  : QWidget( parent )
29  , mText( text )
30  , mLevel( level )
31  , mDuration( duration )
32 {
33  writeContent();
34 }
35 
36 QgsMessageBarItem::QgsMessageBarItem( const QString &title, const QString &text, Qgis::MessageLevel level, int duration, QWidget *parent )
37  : QWidget( parent )
38  , mTitle( title )
39  , mText( text )
40  , mLevel( level )
41  , mDuration( duration )
42 {
43  writeContent();
44 }
45 
46 QgsMessageBarItem::QgsMessageBarItem( const QString &title, const QString &text, QWidget *widget, Qgis::MessageLevel level, int duration, QWidget *parent )
47  : QWidget( parent )
48  , mTitle( title )
49  , mText( text )
50  , mLevel( level )
51  , mDuration( duration )
52  , mWidget( widget )
53  , mUserIcon( QIcon() )
54 
55 {
56  writeContent();
57 }
58 
60  : QWidget( parent )
61  , mLevel( level )
62  , mDuration( duration )
63  , mWidget( widget )
64  , mUserIcon( QIcon() )
65 
66 {
67  writeContent();
68 }
69 
70 void QgsMessageBarItem::writeContent()
71 {
72  if ( !mLayout )
73  {
74  mLayout = new QHBoxLayout( this );
75  mLayout->setContentsMargins( 0, 0, 0, 0 );
76  mTextBrowser = nullptr;
77  mLblIcon = nullptr;
78  }
79 
80  // ICON
81  if ( !mLblIcon )
82  {
83  mLblIcon = new QLabel( this );
84  mLayout->addWidget( mLblIcon );
85  }
86  QIcon icon;
87  if ( !mUserIcon.isNull() )
88  {
89  icon = mUserIcon;
90  }
91  else
92  {
93  QString msgIcon( QStringLiteral( "/mIconInfo.svg" ) );
94  switch ( mLevel )
95  {
96  case Qgis::Critical:
97  msgIcon = QStringLiteral( "/mIconCritical.svg" );
98  break;
99  case Qgis::Warning:
100  msgIcon = QStringLiteral( "/mIconWarning.svg" );
101  break;
102  case Qgis::Success:
103  msgIcon = QStringLiteral( "/mIconSuccess.svg" );
104  break;
105  default:
106  break;
107  }
108  icon = QgsApplication::getThemeIcon( msgIcon );
109  }
110  mLblIcon->setPixmap( icon.pixmap( 24 ) );
111 
112 
113  // STYLESHEETS
114  QString contentStyleSheet;
115  if ( mLevel == Qgis::Success )
116  {
117  mStyleSheet = "QgsMessageBar { background-color: #dff0d8; border: 1px solid #8e998a; } "
118  "QLabel,QTextEdit { color: black; } ";
119  contentStyleSheet = "<style> a, a:visited, a:hover { color:#268300; } </style>";
120  }
121  else if ( mLevel == Qgis::Critical )
122  {
123  mStyleSheet = "QgsMessageBar { background-color: #d65253; border: 1px solid #9b3d3d; } "
124  "QLabel,QTextEdit { color: white; } ";
125  contentStyleSheet = "<style>a, a:visited, a:hover { color:#4e0001; }</style>";
126  }
127  else if ( mLevel == Qgis::Warning )
128  {
129  mStyleSheet = "QgsMessageBar { background-color: #ffc800; border: 1px solid #e0aa00; } "
130  "QLabel,QTextEdit { color: black; } ";
131  contentStyleSheet = "<style>a, a:visited, a:hover { color:#945a00; }</style>";
132  }
133  else if ( mLevel == Qgis::Info )
134  {
135  mStyleSheet = "QgsMessageBar { background-color: #e7f5fe; border: 1px solid #b9cfe4; } "
136  "QLabel,QTextEdit { color: #2554a1; } ";
137  contentStyleSheet = "<style>a, a:visited, a:hover { color:#3bb2fe; }</style>";
138  }
139  mStyleSheet += QLatin1String( "QLabel#mItemCount { font-style: italic; }" );
140 
141  // TITLE AND TEXT
142  if ( mTitle.isEmpty() && mText.isEmpty() )
143  {
144  if ( mTextBrowser )
145  {
146  delete mTextBrowser;
147  mTextBrowser = nullptr;
148  }
149  }
150  else
151  {
152  if ( !mTextBrowser )
153  {
154  mTextBrowser = new QTextBrowser( this );
155  mTextBrowser->setObjectName( QStringLiteral( "textEdit" ) );
156  mTextBrowser->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Maximum );
157  mTextBrowser->setReadOnly( true );
158  mTextBrowser->setOpenLinks( false );
159  connect( mTextBrowser, &QTextBrowser::anchorClicked, this, &QgsMessageBarItem::urlClicked );
160 
161  mTextBrowser->setFrameShape( QFrame::NoFrame );
162  // stylesheet set here so Qt-style substitued scrollbar arrows can show within limited height
163  // adjusts to height of font set in app options
164  mTextBrowser->setStyleSheet( "QTextEdit { background-color: rgba(0,0,0,0); margin-top: 0.25em; max-height: 1.75em; min-height: 1.75em; } "
165  "QScrollBar { background-color: rgba(0,0,0,0); } "
166  "QScrollBar::add-page,QScrollBar::sub-page,QScrollBar::handle { background-color: rgba(0,0,0,0); color: rgba(0,0,0,0); } "
167  "QScrollBar::up-arrow,QScrollBar::down-arrow { color: rgb(0,0,0); } " );
168  mLayout->addWidget( mTextBrowser );
169  }
170  QString content = mText;
171  if ( !mTitle.isEmpty() )
172  {
173  // add ':' to end of title
174  QString t = mTitle.trimmed();
175  if ( !content.isEmpty() && !t.endsWith( ':' ) && !t.endsWith( QLatin1String( ": " ) ) )
176  t += QLatin1String( ": " );
177  content.prepend( QStringLiteral( "<b>" ) + t + " </b>" );
178  }
179  content.prepend( contentStyleSheet );
180  mTextBrowser->setText( content );
181  }
182 
183  // WIDGET
184  if ( mWidget )
185  {
186  QLayoutItem *item = mLayout->itemAt( 2 );
187  if ( !item || item->widget() != mWidget )
188  {
189  mLayout->addWidget( mWidget );
190  }
191  }
192 }
193 
195 {
196  mText = text;
197  writeContent();
198  return this;
199 }
200 
201 QString QgsMessageBarItem::text() const
202 {
203  return mText;
204 }
205 
207 {
208  mTitle = title;
209  writeContent();
210  return this;
211 }
212 
214 {
215  return mTitle;
216 }
217 
219 {
220  mLevel = level;
221  writeContent();
222  emit styleChanged( mStyleSheet );
223  return this;
224 }
225 
227 {
228  return mLevel;
229 }
230 
232 {
233  if ( mWidget )
234  {
235  QLayoutItem *item = nullptr;
236  item = mLayout->itemAt( 2 );
237  if ( item->widget() == mWidget )
238  {
239  delete item->widget();
240  }
241  }
242  mWidget = widget;
243  writeContent();
244  return this;
245 }
246 
248 {
249  return mWidget;
250 }
251 
253 {
254  mUserIcon = icon;
255  return this;
256 }
257 
259 {
260  return mUserIcon;
261 }
262 
263 
265 {
266  mDuration = duration;
267  return this;
268 }
269 
270 void QgsMessageBarItem::urlClicked( const QUrl &url )
271 {
272  QDesktopServices::openUrl( url );
273 }
QString title() const
Returns the title for the message.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
int duration() const
returns the duration in second of the message
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:78
QIcon icon() const
Returns the icon for the message.
QgsMessageBarItem * setTitle(const QString &title)
Qgis::MessageLevel level() const
Returns the message level for the message.
QgsMessageBarItem * setWidget(QWidget *widget)
QgsMessageBarItem * setText(const QString &text)
QgsMessageBarItem * setIcon(const QIcon &icon)
QString text() const
Returns the text for the message.
QgsMessageBarItem(const QString &text, Qgis::MessageLevel level=Qgis::Info, int duration=0, QWidget *parent=nullptr)
make out a widget containing a message to be displayed on the bar
QgsMessageBarItem * setDuration(int duration)
QWidget * widget() const
Returns the widget for the message.
void styleChanged(const QString &styleSheet)
emitted when the message level has changed
QgsMessageBarItem * setLevel(Qgis::MessageLevel level)