OpenWalnut  1.5.0dev
WQtMessageDialog.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 <iostream>
26 
27 #include <QHBoxLayout>
28 #include <QVBoxLayout>
29 
30 #include "WMainWindow.h"
31 
32 #include "WQtMessageDialog.h"
33 
34 WQtMessageDialog::WQtMessageDialog( QString msgID, QString title, QWidget* content, QSettings& settings, QWidget* parent ): // NOLINT - yes use a non-const ref
35  QDialog( parent ),
36  m_content( content ),
37  m_msgID( msgID ),
38  m_settings( settings )
39 {
40  setWindowTitle( title );
41  setModal( false );
42 
43  // setup contents
44  QVBoxLayout* mainLayout = new QVBoxLayout();
45 
46  // text widget. Force stretching the content instead of the button bar
47  mainLayout->addWidget( m_content, 100 );
48 
49  // dialog buttons and checkbox to bottom layout
50  QHBoxLayout* bottomLayout = new QHBoxLayout();
51  QWidget* bottomWidget = new QWidget( this );
52  bottomWidget->setLayout( bottomLayout );
53  mainLayout->addWidget( bottomWidget );
54  this->setLayout( mainLayout );
55 
56  m_checkBox = new QCheckBox( bottomWidget );
57  m_checkBox->setText( "Do not show again" );
58  m_buttonBox = new QDialogButtonBox( bottomWidget );
59  m_buttonBox->setOrientation( Qt::Horizontal );
60  m_buttonBox->setStandardButtons( QDialogButtonBox::Ok );
61  bottomLayout->addWidget( m_checkBox );
62  bottomLayout->addWidget( m_buttonBox );
63 
64  // connect dialog box to close event
65  QObject::connect( m_buttonBox, SIGNAL( accepted() ), this, SLOT( accept() ) );
66  QObject::connect( m_buttonBox, SIGNAL( rejected() ), this, SLOT( reject() ) );
67 }
68 
70 {
71  // cleanup
72 }
73 
75 {
76  show( false ); // NOTE: this also calls QDialog::show.
77 }
78 
79 void WQtMessageDialog::show( bool force )
80 {
81  // check if message is allowed
82  bool show = m_settings.value( m_msgID + "_showAgain", true ).toBool();
83  m_checkBox->setCheckState( show ? Qt::Unchecked : Qt::Checked );
84 
85  // forced to show?
86  // NOTE: we set the m_checkBox earlier to keep the old showAgain state
87  show = show || force;
88 
89  // only show if wanted by user
90  if( !show )
91  {
92  return;
93  }
94 
95  QDialog::show();
96 }
97 
99 {
100  QDialog::reject();
101 }
102 
104 {
105  handleClose();
106  QDialog::accept();
107 }
108 
110 {
111  m_settings.setValue( m_msgID + "_showAgain", m_checkBox->checkState() == Qt::Unchecked );
112 }
113 
virtual void accept()
Dialog closed.
QWidget * m_content
The widget showing the content.
QCheckBox * m_checkBox
Do not show again - Checkbox.
WQtMessageDialog(QString msgID, QString title, QWidget *content, QSettings &settings, QWidget *parent)
Construct a message dialog.
QSettings & m_settings
Settings object of the main window.
virtual ~WQtMessageDialog()
Destructor.
virtual void reject()
Dialog closed.
QDialogButtonBox * m_buttonBox
Dialog buttons.
void handleClose()
Handles close and saves setting.
QString m_msgID
The message ID.
virtual void show()
Shows the dialog if the user does not disabled it.