- Don't create layouts with parent. Qt 4.x automatically reparents
[duncan/yast2-qt4.git] / src / pkg / YQPkgDiskUsageWarningDialog.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQPkgDiskUsageWarningDialog.cc
14
15   Author:     Stefan Hundhammer <sh@suse.de>
16
17   Textdomain "packages-qt"
18
19 /-*/
20
21 #define y2log_component "qt-pkg"
22 #include <ycp/y2log.h>
23
24 #include <QApplication>
25 #include <QLabel>
26 #include <QLayout>
27 #include <QPushButton>
28 #include <QStyle>
29 #include <QBoxLayout>
30
31 #include "YQPkgDiskUsageWarningDialog.h"
32 #include "YQPkgDiskUsageList.h"
33 #include "QY2LayoutUtils.h"
34 #include "YQUI.h"
35 #include "YQi18n.h"
36
37
38 #define SPACING                 2       // between subwidgets
39 #define MARGIN                  4       // around the widget
40
41
42 YQPkgDiskUsageWarningDialog::YQPkgDiskUsageWarningDialog( QWidget *             parent,
43                                                           const QString &       message,
44                                                           int                   thresholdPercent,
45                                                           const QString &       acceptButtonLabel,
46                                                           const QString &       rejectButtonLabel )
47     : QDialog( parent )
48 {
49     // Dialog title
50     setWindowTitle( _( "Disk Space Warning" ) );
51
52     // Enable dialog resizing even without window manager
53     setSizeGripEnabled( true );
54
55     // Layout for the dialog ( can't simply insert a QVBox )
56
57     QVBoxLayout * layout = new QVBoxLayout();
58     Q_CHECK_PTR( layout );
59     layout->setSpacing( SPACING );
60     layout->setMargin ( MARGIN  );
61     setLayout(layout);
62
63     // HBox for icon and message
64     QHBoxLayout * hbox = new QHBoxLayout();
65     Q_CHECK_PTR( hbox );
66     layout->addLayout( hbox );
67
68
69     // Icon
70
71     //addHSpacing( hbox );
72     QLabel * iconLabel = new QLabel( this );
73     Q_CHECK_PTR( iconLabel );
74     hbox->addWidget(iconLabel);
75 #ifdef FIXME
76     iconLabel->setPixmap( QApplication::style().stylePixmap( QStyle::SP_MessageBoxWarning ) );
77 #endif
78     iconLabel->setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum ) ); // hor/vert
79
80     // Label for the message
81
82     QLabel * label = new QLabel( message, this);
83     Q_CHECK_PTR( label );
84     hbox->addWidget(label);
85     label->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum ) ); // hor/vert
86
87
88     // Disk usage list
89
90     YQPkgDiskUsageList * duList = new YQPkgDiskUsageList( this, thresholdPercent );
91     Q_CHECK_PTR( duList );
92
93     layout->addWidget( duList );
94
95
96     // Button box
97
98     hbox = new QHBoxLayout();
99     Q_CHECK_PTR( hbox );
100     hbox->setSpacing( SPACING );
101     hbox->setMargin ( MARGIN  );
102     layout->addLayout( hbox );
103
104     //addHStretch( hbox );
105
106
107     // Accept button - usually "OK" or "Continue"
108
109     QPushButton * button = new QPushButton( acceptButtonLabel, this );
110     Q_CHECK_PTR( button );
111     hbox->addWidget(button);
112
113     connect( button,    SIGNAL( clicked() ),
114              this,      SLOT  ( accept()  ) );
115
116     //addHStretch( hbox );
117
118
119     if ( ! rejectButtonLabel.isEmpty() )
120     {
121         // Reject button ( if desired ) - usually "Cancel"
122
123         button = new QPushButton( rejectButtonLabel, this );
124         Q_CHECK_PTR( button );
125   hbox->addWidget(button);
126
127         connect( button,        SIGNAL( clicked() ),
128                  this,          SLOT  ( reject()  ) );
129
130         //addHStretch( hbox );
131     }
132
133     // If there is only one button, it's safe to make that one ( the accept
134     // button ) the default. If there are two, better be safe than sorry and
135     // make the reject button the default.
136
137     button->setDefault( true );
138 }
139
140
141 bool
142 YQPkgDiskUsageWarningDialog::diskUsageWarning( const QString &  message,
143                                                int              thresholdPercent,
144                                                const QString &  acceptButtonLabel,
145                                                const QString &  rejectButtonLabel )
146 {
147     YQPkgDiskUsageWarningDialog dialog( 0,
148                                         message,
149                                         thresholdPercent,
150                                         acceptButtonLabel,
151                                         rejectButtonLabel );
152     YQUI::ui()->normalCursor();
153     dialog.exec();
154
155     return dialog.result() == QDialog::Accepted;
156 }
157
158
159
160
161 #include "YQPkgDiskUsageWarningDialog.moc"