OpenWalnut  1.5.0dev
WQtMessagePopup.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <algorithm>
26 
27 #include <QHBoxLayout>
28 #include <QLabel>
29 #include <QPushButton>
30 #include <QCloseEvent>
31 #include <QHideEvent>
32 #include <QShowEvent>
33 #include <QMessageBox>
34 #include <QTimer>
35 
36 #include "core/common/WLogger.h"
37 
38 #include "guiElements/WScaleLabel.h"
39 
40 #include "WMainWindow.h"
41 #include "WIconManager.h"
42 
43 #include "WQtMessagePopup.h"
44 
45 #define OUTERMARGIN 10
46 #define BORDERWIDTH 2
47 #define MAXWIDTH 600
48 
49 WQtMessagePopup::WQtMessagePopup( QWidget* parent, const QString& title, const QString& message, MessageType type ):
50  QDialog( parent, Qt::Popup | Qt::FramelessWindowHint ),
51  m_title( title ),
52  m_message( message ),
53  m_type( type ),
54  m_autoClose( true ),
55  m_autoMove( true )
56 {
57  setAutoClose( true );
58 
59  setWindowRole( "MessagePopup" );
60 
61  // these settings seem to be ignored somehow
62  setWindowModality( Qt::NonModal );
63 
64  QString borderColor = "red";
65  QString titlePrefix = "";
66 
67  // text height. Use FontMetrics and get it directly from the title label
68  WScaleLabel* titleLabel = new WScaleLabel( this );
69  titleLabel->setText( titlePrefix + title );
70  QFontMetrics fm( titleLabel->fontMetrics() );
71  int textHeight = fm.height();
72 
73  // determine a width and height for the popup
74  unsigned int w = std::min( parent->width() - ( 2 * OUTERMARGIN ), MAXWIDTH );
75  unsigned int h = textHeight + BORDERWIDTH + BORDERWIDTH;
76 
77  // change size of popup
78  resize( w, h );
79 
80  switch( m_type )
81  {
82  case LL_ERROR:
83  borderColor = "#f44141";
84  titlePrefix = "Error: ";
85  break;
86  case LL_WARNING:
87  borderColor = "#ffa200";
88  titlePrefix = "Warning: ";
89  break;
90  case LL_INFO:
91  borderColor = "#40aca0";
92  titlePrefix = "Info: ";
93  break;
94  case LL_DEBUG:
95  default:
96  borderColor = "#7d729c";
97  titlePrefix = "Debug: ";
98  break;
99  }
100 
101  // setup widget
102  setStyleSheet(
103  "QDialog#popupDialog{"
104  "background: #222222;"
105  "border-style: solid;"
106  "border-width: " + QString::number( BORDERWIDTH ) + "px;"
107  "border-color: " + borderColor + ";"
108  "padding: 0px;"
109  "}"
110  "QWidget#popupDialogTitle{"
111  "background: " + borderColor + ";"
112  "color: white;"
113  "font-weight: bold;"
114  "padding: 0px;"
115  "}"
116  "QLabel#popupDialogTitle{"
117  "background: " + borderColor + ";"
118  "color: white;"
119  "font-weight: bold;"
120  "padding: 0px;"
121  "}"
122  "QLabel#popupDialogMessage{"
123  "background: #222222;"
124  "color: white;"
125  "padding: 0px;"
126  "}"
127  "QPushButton#popupDialogButton{"
128  "border-style: none;"
129  "background: " + borderColor + ";"
130  "color: white;"
131  "padding: 0px;"
132  "}"
133  );
134 
135  setContentsMargins( 0, 0, 0, 0 );
136 
137  QHBoxLayout* topLayout = new QHBoxLayout( this );
138  topLayout->setContentsMargins( 0, 0, 0, 0 );
139  topLayout->setSpacing( 0 );
140  // this is the layout of some widget
141  QWidget* titleWidget = new QWidget();
142  titleWidget->setMinimumHeight( textHeight + 3 * BORDERWIDTH );
143  titleWidget->setContentsMargins( BORDERWIDTH, 0, 0, 0 );
144  titleWidget->setLayout( topLayout );
145 
146  titleLabel->setContentsMargins( 0, 0, 0, 0 );
147  topLayout->addWidget( titleLabel );
148 
149  QPushButton* detailsBtn = new QPushButton( "", this );
150  detailsBtn->setContentsMargins( 0, 0, 0, 0 );
151  detailsBtn->setIcon( WQtGui::getMainWindow()->getIconManager()->getIcon( "popup_more" ) );
152  detailsBtn->setFixedWidth( textHeight );
153  detailsBtn->setFixedHeight( textHeight );
154  detailsBtn->setToolTip( "Show complete message" );
155  topLayout->addWidget( detailsBtn );
156  connect( detailsBtn, SIGNAL( released() ), this, SLOT( showMessage() ) );
157 
158  m_closeBtn = new QPushButton( "", this );
159  m_closeBtn->setContentsMargins( 0, 0, 0, 0 );
160  m_closeBtn->setIcon( WQtGui::getMainWindow()->getIconManager()->getIcon( "popup_close" ) );
161  m_closeBtn->setFixedWidth( textHeight );
162  m_closeBtn->setFixedHeight( textHeight );
163  m_closeBtn->setToolTip( "Close this message" );
164  topLayout->addWidget( m_closeBtn );
165  connect( m_closeBtn, SIGNAL( released() ), this, SLOT( closePopup() ) );
166 
167  QVBoxLayout* popupLayout = new QVBoxLayout( this );
168  popupLayout->setSpacing( 0 );
169  popupLayout->setContentsMargins( BORDERWIDTH, 0, BORDERWIDTH, BORDERWIDTH );
170 
171  WScaleLabel* messageLabel = new WScaleLabel( this );
172  messageLabel->setText( message );
173  messageLabel->setContentsMargins( 2 * BORDERWIDTH, 2 * BORDERWIDTH, 2 * BORDERWIDTH, 2 * BORDERWIDTH );
174  popupLayout->addWidget( titleWidget );
175  popupLayout->addWidget( messageLabel );
176 
177  setLayout( popupLayout );
178 
179  // set the names. Needed for the stylesheet to work
180  setObjectName( "popupDialog" );
181  titleLabel->setObjectName( "popupDialogTitle" );
182  titleWidget->setObjectName( "popupDialogTitle" );
183  messageLabel->setObjectName( "popupDialogMessage" );
184  m_closeBtn->setObjectName( "popupDialogButton" );
185  detailsBtn->setObjectName( "popupDialogButton" );
186 }
187 
188 void WQtMessagePopup::showEvent( QShowEvent* event )
189 {
190  // move widget to correct position
191  if( m_autoMove )
192  {
193  // get top left corner
194  QPoint p = parentWidget()->mapToGlobal( QPoint( parentWidget()->width() / 2, parentWidget()->height() ) );
195 
196  // set position, include margins
197  move( p.x() - width() / 2 - OUTERMARGIN / 2, p.y() - 2 * height()- OUTERMARGIN );
198  }
199 
200  QDialog::showEvent( event );
201 }
202 
204 {
205  // cleanup
206 }
207 
209 {
210  if( m_autoClose )
211  {
212  closePopup();
213  }
214  switch( m_type )
215  {
216  case LL_ERROR:
217  QMessageBox::critical( this, m_title, m_message );
218  break;
219  case LL_WARNING:
220  QMessageBox::warning( this, m_title, m_message );
221  break;
222  case LL_INFO:
223  case LL_DEBUG:
224  default:
225  QMessageBox::information( this, m_title, m_message );
226  break;
227  }
228 }
229 
231 {
232  QWidget::show();
233  QTimer::singleShot( 2000, this, SLOT( close() ) );
234 }
235 
236 void WQtMessagePopup::setAutoClose( bool autoClose )
237 {
238  m_autoClose = autoClose;
239 
240  // use popup type of window if auto close is enabled
241  if( autoClose )
242  {
243  setWindowFlags( Qt::Popup );
244  }
245  else
246  {
247  setWindowFlags( Qt::Widget );
248  }
249 }
250 
252 {
253  emit onClose( this );
254  close();
255 }
256 
257 void WQtMessagePopup::setShowCloseButton( bool showCloseButton )
258 {
259  m_closeBtn->setHidden( !showCloseButton );
260 }
261 
262 void WQtMessagePopup::setAutoPosition( bool autoPosition )
263 {
264  m_autoMove = autoPosition;
265 }
266 
267 WQtMessagePopup::MessageType WQtMessagePopup::getType() const
268 {
269  return m_type;
270 }
static WMainWindow * getMainWindow()
Returns the current main window instance or NULL if not existent.
Definition: WQtGui.cpp:88
void setAutoClose(bool autoClose=true)
When set to true, the widget gets closed when loosing focus or when clicking the detail button.
MessageType getType() const
Get this popups message type.
void show()
Reimplement show from QWidget to be able to auto-close it.
void onClose(WQtMessagePopup *me)
Called when closing the popup.
void setShowCloseButton(bool showCloseButton=true)
Show or hide the close button.
virtual void showEvent(QShowEvent *event)
On show.
virtual ~WQtMessagePopup()
Destructor.
MessageType m_type
What kind of message is this.
bool m_autoMove
See setAutoPosition.
void showMessage()
Shows the message.
void closePopup()
Cloes this popup.
WQtMessagePopup(QWidget *parent, const QString &title, const QString &message, MessageType type)
Constructor.
void setAutoPosition(bool autoPosition=true)
If true, the widget moves itself to the bottom of its parent widget.
QString m_message
The message text.
bool m_autoClose
See setAutoClose.
QPushButton * m_closeBtn
Close button.
QString m_title
Title Text.
Special Label that can shrink and expand in a layout.
Definition: WScaleLabel.h:37
virtual void setText(const QString &text)
reimplemented function to setText
Definition: WScaleLabel.cpp:93