QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 <QTextEdit>
25 
27  : QWidget( parent )
28  , mTitle( "" )
29  , mText( text )
30  , mLevel( level )
31  , mDuration( duration )
32  , mWidget( nullptr )
33  , mUserIcon( QIcon() )
34  , mLayout( nullptr )
35 {
36  writeContent();
37 }
38 
40  : QWidget( parent )
41  , mTitle( title )
42  , mText( text )
43  , mLevel( level )
44  , mDuration( duration )
45  , mWidget( nullptr )
46  , mUserIcon( QIcon() )
47  , mLayout( nullptr )
48 {
49  writeContent();
50 }
51 
53  : QWidget( parent )
54  , mTitle( title )
55  , mText( text )
56  , mLevel( level )
57  , mDuration( duration )
58  , mWidget( widget )
59  , mUserIcon( QIcon() )
60  , mLayout( nullptr )
61 {
62  writeContent();
63 }
64 
66  : QWidget( parent )
67  , mTitle( "" )
68  , mText( "" )
69  , mLevel( level )
70  , mDuration( duration )
71  , mWidget( widget )
72  , mUserIcon( QIcon() )
73  , mLayout( nullptr )
74 {
75  writeContent();
76 }
77 
79 {
80 }
81 
82 void QgsMessageBarItem::writeContent()
83 {
84  if ( !mLayout )
85  {
86  mLayout = new QHBoxLayout( this );
87  mLayout->setContentsMargins( 0, 0, 0, 0 );
88  mTextEdit = nullptr;
89  mLblIcon = nullptr;
90  }
91 
92  // ICON
93  if ( !mLblIcon )
94  {
95  mLblIcon = new QLabel( this );
96  mLayout->addWidget( mLblIcon );
97  }
98  QIcon icon;
99  if ( !mUserIcon.isNull() )
100  {
101  icon = mUserIcon;
102  }
103  else
104  {
105  QString msgIcon( "/mIconInfo.png" );
106  switch ( mLevel )
107  {
109  msgIcon = QString( "/mIconCritical.png" );
110  break;
112  msgIcon = QString( "/mIconWarning.svg" );
113  break;
115  msgIcon = QString( "/mIconSuccess.png" );
116  break;
117  default:
118  break;
119  }
120  icon = QgsApplication::getThemeIcon( msgIcon );
121  }
122  mLblIcon->setPixmap( icon.pixmap( 24 ) );
123 
124  // TITLE AND TEXT
125  if ( mTitle.isEmpty() && mText.isEmpty() )
126  {
127  if ( mTextEdit )
128  {
129  delete mTextEdit;
130  mTextEdit = nullptr;
131  }
132  }
133  else
134  {
135  if ( !mTextEdit )
136  {
137  mTextEdit = new QTextEdit( this );
138  mTextEdit->setObjectName( "textEdit" );
139  mTextEdit->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Maximum );
140  mTextEdit->setReadOnly( true );
141  mTextEdit->setFrameShape( QFrame::NoFrame );
142  // stylesheet set here so Qt-style substitued scrollbar arrows can show within limited height
143  // adjusts to height of font set in app options
144  mTextEdit->setStyleSheet( "QTextEdit { background-color: rgba(0,0,0,0); margin-top: 0.25em; max-height: 1.75em; min-height: 1.75em; } "
145  "QScrollBar { background-color: rgba(0,0,0,0); } "
146  "QScrollBar::add-page,QScrollBar::sub-page,QScrollBar::handle { background-color: rgba(0,0,0,0); color: rgba(0,0,0,0); } "
147  "QScrollBar::up-arrow,QScrollBar::down-arrow { color: rgb(0,0,0); } " );
148  mLayout->addWidget( mTextEdit );
149  }
150  QString content = mText;
151  if ( !mTitle.isEmpty() )
152  {
153  // add ':' to end of title
154  QString t = mTitle.trimmed();
155  if ( !content.isEmpty() && !t.endsWith( ':' ) && !t.endsWith( ": " ) )
156  t += ": ";
157  content.prepend( QLatin1String( "<b>" ) + t + " </b>" );
158  }
159  mTextEdit->setText( content );
160  }
161 
162  // WIDGET
163  if ( mWidget )
164  {
165  QLayoutItem *item = mLayout->itemAt( 2 );
166  if ( !item || item->widget() != mWidget )
167  {
168  mLayout->addWidget( mWidget );
169  }
170  }
171 
172  // STYLESHEET
173  if ( mLevel == QgsMessageBar::SUCCESS )
174  {
175  mStyleSheet = "QgsMessageBar { background-color: #dff0d8; border: 1px solid #8e998a; } "
176  "QLabel,QTextEdit { color: black; } ";
177  }
178  else if ( mLevel == QgsMessageBar::CRITICAL )
179  {
180  mStyleSheet = "QgsMessageBar { background-color: #d65253; border: 1px solid #9b3d3d; } "
181  "QLabel,QTextEdit { color: white; } ";
182  }
183  else if ( mLevel == QgsMessageBar::WARNING )
184  {
185  mStyleSheet = "QgsMessageBar { background-color: #ffc800; border: 1px solid #e0aa00; } "
186  "QLabel,QTextEdit { color: black; } ";
187  }
188  else if ( mLevel == QgsMessageBar::INFO )
189  {
190  mStyleSheet = "QgsMessageBar { background-color: #e7f5fe; border: 1px solid #b9cfe4; } "
191  "QLabel,QTextEdit { color: #2554a1; } ";
192  }
193  mStyleSheet += "QLabel#mItemCount { font-style: italic; }";
194 }
195 
197 {
198  mText = text;
199  writeContent();
200  return this;
201 }
202 
204 {
205  mTitle = title;
206  writeContent();
207  return this;
208 }
209 
211 {
212  mLevel = level;
213  writeContent();
214  emit styleChanged( mStyleSheet );
215  return this;
216 }
217 
219 {
220  if ( mWidget )
221  {
222  QLayoutItem *item;
223  item = mLayout->itemAt( 2 );
224  if ( item->widget() == mWidget )
225  {
226  delete item->widget();
227  }
228  }
229  mWidget = widget;
230  writeContent();
231  return this;
232 }
233 
235 {
236  mUserIcon = icon;
237  return this;
238 }
239 
240 
242 {
243  mDuration = duration;
244  return this;
245 }
246 
void setStyleSheet(const QString &styleSheet)
void setContentsMargins(int left, int top, int right, int bottom)
void setFrameShape(Shape)
QString & prepend(QChar ch)
static QIcon getThemeIcon(const QString &theName)
Helper to get a theme icon.
void setPixmap(const QPixmap &)
virtual QWidget * widget()
const QPixmap * icon() const
int duration() const
returns the duration in second of the message
QgsMessageBar::MessageLevel level()
returns the level
QPixmap pixmap(const QSize &size, Mode mode, State state) const
QgsMessageBarItem * setTitle(const QString &title)
QgsMessageBarItem * setWidget(QWidget *widget)
QgsMessageBarItem * setText(const QString &text)
QgsMessageBarItem * setIcon(const QIcon &icon)
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QgsMessageBarItem(const QString &text, QgsMessageBar::MessageLevel level=QgsMessageBar::INFO, int duration=0, QWidget *parent=nullptr)
make out a widget containing a message to be displayed on the bar
void setObjectName(const QString &name)
bool isEmpty() const
QString trimmed() const
virtual QLayoutItem * itemAt(int index) const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
void setSizePolicy(QSizePolicy)
QgsMessageBarItem * setLevel(QgsMessageBar::MessageLevel level)
QgsMessageBarItem * setDuration(int duration)
bool isNull() const
void setReadOnly(bool ro)
void styleChanged(const QString &styleSheet)
emitted when the message level has changed
QObject * parent() const
void setText(const QString &text)