QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 : denis.rouzaud@gmail.com
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( "" )
29  , mText( text )
30  , mLevel( level )
31  , mDuration( duration )
32  , mWidget( 0 )
33  , mUserIcon( QIcon() )
34  , mLayout( 0 )
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( 0 )
46  , mUserIcon( QIcon() )
47  , mLayout( 0 )
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( 0 )
61 {
62  writeContent();
63 }
64 
65 QgsMessageBarItem::QgsMessageBarItem( QWidget *widget, QgsMessageBar::MessageLevel level, int duration, QWidget *parent ) :
66  QWidget( parent )
67  , mTitle( "" )
68  , mText( "" )
69  , mLevel( level )
70  , mDuration( duration )
71  , mWidget( widget )
72  , mUserIcon( QIcon() )
73  , mLayout( 0 )
74 {
75  writeContent();
76 }
77 
79 {
80 }
81 
83 {
84  if ( mLayout == 0 )
85  {
86  mLayout = new QHBoxLayout( this );
87  mLayout->setContentsMargins( 0, 0, 0, 0 );
88  mTextEdit = 0;
89  mLblIcon = 0;
90  }
91 
92  // ICON
93  if ( mLblIcon == 0 )
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( "/mIconWarn.png" );
113  break;
114  default:
115  break;
116  }
117  icon = QgsApplication::getThemeIcon( msgIcon );
118  }
119  mLblIcon->setPixmap( icon.pixmap( 24 ) );
120 
121  // TITLE AND TEXT
122  if ( mTitle.isEmpty() && mText.isEmpty() )
123  {
124  if ( mTextEdit != 0 )
125  {
126  delete mTextEdit;
127  mTextEdit = 0;
128  }
129  }
130  else
131  {
132  if ( mTextEdit == 0 )
133  {
134  mTextEdit = new QTextEdit( this );
135  mTextEdit->setObjectName( "textEdit" );
136  mTextEdit->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Maximum );
137  mTextEdit->setReadOnly( true );
138  mTextEdit->setFrameShape( QFrame::NoFrame );
139  // stylesheet set here so Qt-style substitued scrollbar arrows can show within limited height
140  // adjusts to height of font set in app options
141  mTextEdit->setStyleSheet( "* { background-color: rgba(0,0,0,0); margin-top: 0.25em; max-height: 1.75em; min-height: 1.75em; } "
142  "QScrollBar::add-page,QScrollBar::sub-page,QScrollBar::handle { background-color: rgba(0,0,0,0); color: rgba(0,0,0,0); }" );
143  mLayout->addWidget( mTextEdit );
144  }
145  QString content = mText;
146  if ( !mTitle.isEmpty() )
147  {
148  // add ':' to end of title
149  QString t = mTitle.trimmed();
150  if ( !t.endsWith( ":" ) && !content.isEmpty() )
151  t += ": ";
152  content.prepend( QString( "<b>" ) + t + "</b>" );
153  }
154  mTextEdit->setText( content );
155  }
156 
157  // WIDGET
158  if ( mWidget != 0 )
159  {
160  QLayoutItem *item = mLayout->itemAt( 2 );
161  if ( !item || item->widget() != mWidget )
162  {
163  mLayout->addWidget( mWidget );
164  }
165  }
166 
167  // STYLESHEET
169  {
170  mStyleSheet = "QgsMessageBar { background-color: #d65253; border: 1px solid #9b3d3d; } "
171  "QLabel,QTextEdit { color: white; } ";
172  }
173  else if ( mLevel == QgsMessageBar::WARNING )
174  {
175  mStyleSheet = "QgsMessageBar { background-color: #ffc800; border: 1px solid #e0aa00; } "
176  "QLabel,QTextEdit { color: black; } ";
177  }
178  else if ( mLevel <= QgsMessageBar::INFO )
179  {
180  mStyleSheet = "QgsMessageBar { background-color: #e7f5fe; border: 1px solid #b9cfe4; } "
181  "QLabel,QTextEdit { color: #2554a1; } ";
182  }
183  mStyleSheet += "QLabel#mItemCount { font-style: italic; }";
184 }
185 
187 {
188  mText = text;
189  writeContent();
190  return this;
191 }
192 
194 {
195  mTitle = title;
196  writeContent();
197  return this;
198 }
199 
201 {
202  mLevel = level;
203  writeContent();
204  emit styleChanged( mStyleSheet );
205  return this;
206 }
207 
209 {
210  if ( mWidget != 0 )
211  {
212  QLayoutItem *item;
213  item = mLayout->itemAt( 2 );
214  if ( item->widget() == mWidget )
215  {
216  delete item->widget();
217  }
218  }
219  mWidget = widget;
220  writeContent();
221  return this;
222 }
223 
225 {
226  mUserIcon = icon;
227  return this;
228 }
229 
230 
232 {
234  return this;
235 }
236