QGIS API Documentation  2.99.0-Master (314842d)
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 
26 QgsMessageBarItem::QgsMessageBarItem( const QString &text, QgsMessageBar::MessageLevel level, int duration, QWidget *parent )
27  : QWidget( parent )
28  , mTitle( QLatin1String( "" ) )
29  , mText( text )
30  , mLevel( level )
31  , mDuration( duration )
32  , mWidget( nullptr )
33  , mUserIcon( QIcon() )
34  , mLayout( nullptr )
35 {
36  writeContent();
37 }
38 
39 QgsMessageBarItem::QgsMessageBarItem( const QString &title, const QString &text, QgsMessageBar::MessageLevel level, int duration, QWidget *parent )
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 
52 QgsMessageBarItem::QgsMessageBarItem( const QString &title, const QString &text, QWidget *widget, QgsMessageBar::MessageLevel level, int duration, QWidget *parent )
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( QLatin1String( "" ) )
68  , mText( QLatin1String( "" ) )
69  , mLevel( level )
70  , mDuration( duration )
71  , mWidget( widget )
72  , mUserIcon( QIcon() )
73  , mLayout( nullptr )
74 {
75  writeContent();
76 }
77 
78 void QgsMessageBarItem::writeContent()
79 {
80  if ( !mLayout )
81  {
82  mLayout = new QHBoxLayout( this );
83  mLayout->setContentsMargins( 0, 0, 0, 0 );
84  mTextEdit = nullptr;
85  mLblIcon = nullptr;
86  }
87 
88  // ICON
89  if ( !mLblIcon )
90  {
91  mLblIcon = new QLabel( this );
92  mLayout->addWidget( mLblIcon );
93  }
94  QIcon icon;
95  if ( !mUserIcon.isNull() )
96  {
97  icon = mUserIcon;
98  }
99  else
100  {
101  QString msgIcon( QStringLiteral( "/mIconInfo.svg" ) );
102  switch ( mLevel )
103  {
105  msgIcon = QStringLiteral( "/mIconCritical.png" );
106  break;
108  msgIcon = QStringLiteral( "/mIconWarning.svg" );
109  break;
111  msgIcon = QStringLiteral( "/mIconSuccess.png" );
112  break;
113  default:
114  break;
115  }
116  icon = QgsApplication::getThemeIcon( msgIcon );
117  }
118  mLblIcon->setPixmap( icon.pixmap( 24 ) );
119 
120  // TITLE AND TEXT
121  if ( mTitle.isEmpty() && mText.isEmpty() )
122  {
123  if ( mTextEdit )
124  {
125  delete mTextEdit;
126  mTextEdit = nullptr;
127  }
128  }
129  else
130  {
131  if ( !mTextEdit )
132  {
133  mTextEdit = new QTextEdit( this );
134  mTextEdit->setObjectName( QStringLiteral( "textEdit" ) );
135  mTextEdit->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Maximum );
136  mTextEdit->setReadOnly( true );
137  mTextEdit->setFrameShape( QFrame::NoFrame );
138  // stylesheet set here so Qt-style substitued scrollbar arrows can show within limited height
139  // adjusts to height of font set in app options
140  mTextEdit->setStyleSheet( "QTextEdit { background-color: rgba(0,0,0,0); margin-top: 0.25em; max-height: 1.75em; min-height: 1.75em; } "
141  "QScrollBar { background-color: rgba(0,0,0,0); } "
142  "QScrollBar::add-page,QScrollBar::sub-page,QScrollBar::handle { background-color: rgba(0,0,0,0); color: rgba(0,0,0,0); } "
143  "QScrollBar::up-arrow,QScrollBar::down-arrow { color: rgb(0,0,0); } " );
144  mLayout->addWidget( mTextEdit );
145  }
146  QString content = mText;
147  if ( !mTitle.isEmpty() )
148  {
149  // add ':' to end of title
150  QString t = mTitle.trimmed();
151  if ( !content.isEmpty() && !t.endsWith( ':' ) && !t.endsWith( QLatin1String( ": " ) ) )
152  t += QLatin1String( ": " );
153  content.prepend( QStringLiteral( "<b>" ) + t + " </b>" );
154  }
155  mTextEdit->setText( content );
156  }
157 
158  // WIDGET
159  if ( mWidget )
160  {
161  QLayoutItem *item = mLayout->itemAt( 2 );
162  if ( !item || item->widget() != mWidget )
163  {
164  mLayout->addWidget( mWidget );
165  }
166  }
167 
168  // STYLESHEET
169  if ( mLevel == QgsMessageBar::SUCCESS )
170  {
171  mStyleSheet = "QgsMessageBar { background-color: #dff0d8; border: 1px solid #8e998a; } "
172  "QLabel,QTextEdit { color: black; } ";
173  }
174  else if ( mLevel == QgsMessageBar::CRITICAL )
175  {
176  mStyleSheet = "QgsMessageBar { background-color: #d65253; border: 1px solid #9b3d3d; } "
177  "QLabel,QTextEdit { color: white; } ";
178  }
179  else if ( mLevel == QgsMessageBar::WARNING )
180  {
181  mStyleSheet = "QgsMessageBar { background-color: #ffc800; border: 1px solid #e0aa00; } "
182  "QLabel,QTextEdit { color: black; } ";
183  }
184  else if ( mLevel == QgsMessageBar::INFO )
185  {
186  mStyleSheet = "QgsMessageBar { background-color: #e7f5fe; border: 1px solid #b9cfe4; } "
187  "QLabel,QTextEdit { color: #2554a1; } ";
188  }
189  mStyleSheet += QLatin1String( "QLabel#mItemCount { font-style: italic; }" );
190 }
191 
193 {
194  mText = text;
195  writeContent();
196  return this;
197 }
198 
200 {
201  mTitle = title;
202  writeContent();
203  return this;
204 }
205 
207 {
208  mLevel = level;
209  writeContent();
210  emit styleChanged( mStyleSheet );
211  return this;
212 }
213 
215 {
216  if ( mWidget )
217  {
218  QLayoutItem *item = nullptr;
219  item = mLayout->itemAt( 2 );
220  if ( item->widget() == mWidget )
221  {
222  delete item->widget();
223  }
224  }
225  mWidget = widget;
226  writeContent();
227  return this;
228 }
229 
231 {
232  mUserIcon = icon;
233  return this;
234 }
235 
236 
238 {
239  mDuration = duration;
240  return this;
241 }
242 
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
int duration() const
returns the duration in second of the message
QgsMessageBar::MessageLevel level()
returns the level
QgsMessageBarItem * setTitle(const QString &title)
QgsMessageBarItem * setWidget(QWidget *widget)
QgsMessageBarItem * setText(const QString &text)
QgsMessageBarItem * setIcon(const QIcon &icon)
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
QgsMessageBarItem * setLevel(QgsMessageBar::MessageLevel level)
QgsMessageBarItem * setDuration(int duration)
void styleChanged(const QString &styleSheet)
emitted when the message level has changed