- Don't create layouts with parent. Qt 4.x automatically reparents
[duncan/yast2-qt4.git] / src / pkg / YQPkgChangesDialog.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQPkgChangesDialog.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 <QDesktopWidget>
26 #include <QLabel>
27 #include <QLayout>
28 #include <QPushButton>
29 #include <QStyle>
30 #include <QBoxLayout>
31
32 #include "YQZypp.h"
33 #include <zypp/ResStatus.h>
34 #include <zypp/ui/UserWantedPackages.h>
35
36 #include "YQPkgChangesDialog.h"
37 #include "YQPkgList.h"
38 #include "QY2LayoutUtils.h"
39 #include "YQi18n.h"
40 #include "YQUI.h"
41
42 using std::set;
43 using std::string;
44
45
46 #define SPACING                 2       // between subwidgets
47 #define MARGIN                  4       // around the widget
48
49
50 YQPkgChangesDialog::YQPkgChangesDialog( QWidget *               parent,
51                                         const QString &         message,
52                                         const QString &         acceptButtonLabel,
53                                         const QString &         rejectButtonLabel )
54     : QDialog( parent )
55 {
56     // Dialog title
57     setWindowTitle( _( "Changed Packages" ) );
58
59     // Enable dialog resizing even without window manager
60     setSizeGripEnabled( true );
61
62     // Limit dialog size to available screen size
63     setMaximumSize( qApp->desktop()->availableGeometry().size() );
64
65     // Layout for the dialog ( can't simply insert a QVBox )
66
67     QVBoxLayout * layout = new QVBoxLayout();
68     Q_CHECK_PTR( layout );
69     layout->setMargin(MARGIN);
70     layout->setSpacing(SPACING);
71     setLayout(layout);
72
73     // HBox for icon and message
74
75     QHBoxLayout * hbox = new QHBoxLayout();
76     Q_CHECK_PTR( hbox );
77     layout->addLayout( hbox );
78
79
80     // Icon
81
82     hbox->addSpacing(SPACING);
83     QLabel * iconLabel = new QLabel( this );
84     Q_CHECK_PTR( iconLabel );
85     hbox->addWidget(iconLabel);
86 #ifdef FIXME
87     iconLabel->setPixmap( QApplication::style().stylePixmap( QStyle::SP_MessageBoxInformation ) );
88 #endif
89     iconLabel->setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum ) ); // hor/vert
90     hbox->addSpacing(SPACING);
91
92     // Label for the message
93
94     QLabel * label = new QLabel( message, this );
95     Q_CHECK_PTR( label );
96     hbox->addWidget(label);
97     label->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum ) ); // hor/vert
98
99
100     // Pkg list
101
102     _pkgList = new YQPkgList( this );
103     Q_CHECK_PTR( _pkgList );
104     _pkgList->setEditable( false );
105
106     layout->addWidget( _pkgList );
107
108
109     // Button box
110
111     hbox = new QHBoxLayout();
112     Q_CHECK_PTR( hbox );
113     hbox->setSpacing( SPACING );
114     hbox->setMargin ( MARGIN  );
115     layout->addLayout( hbox );
116
117     hbox->addStretch();
118
119
120     // Accept button - usually "OK" or "Continue"
121
122     QPushButton * button = new QPushButton( acceptButtonLabel, this );
123     Q_CHECK_PTR( button );
124     layout->addWidget( button );
125     button->setDefault( true );
126
127     connect( button,    SIGNAL( clicked() ),
128              this,      SLOT  ( accept()  ) );
129
130     hbox->addStretch();
131
132
133     if ( ! rejectButtonLabel.isEmpty() )
134     {
135         // Reject button ( if desired ) - usually "Cancel"
136
137         button = new QPushButton( rejectButtonLabel, this );
138         Q_CHECK_PTR( button );
139   hbox->addWidget(button);
140         connect( button,        SIGNAL( clicked() ),
141                  this,          SLOT  ( reject()  ) );
142
143         hbox->addStretch();
144     }
145 }
146
147
148 void
149 YQPkgChangesDialog::filter( bool byAuto, bool byApp, bool byUser )
150 {
151     filter( QRegExp( "" ), byAuto, byApp, byUser );
152 }
153
154
155 void
156 YQPkgChangesDialog::filter( const QRegExp & regexp, bool byAuto, bool byApp, bool byUser )
157 {
158     YQUI::ui()->busyCursor();
159     _pkgList->clear();
160
161     set<string> ignoredNames;
162     
163     if ( ! byUser || ! byApp )
164         ignoredNames = zypp::ui::userWantedPackageNames();
165
166     for ( ZyppPoolIterator it = zyppPkgBegin();
167           it != zyppPkgEnd();
168           ++it )
169     {
170         ZyppSel selectable = *it;
171
172         if ( selectable->toModify() )
173         {
174             zypp::ResStatus::TransactByValue modifiedBy = selectable->modifiedBy();
175
176             if ( ( ( modifiedBy == zypp::ResStatus::SOLVER     ) && byAuto ) ||
177                  ( ( modifiedBy == zypp::ResStatus::APPL_LOW ||
178                    modifiedBy == zypp::ResStatus::APPL_HIGH  ) && byApp ) ||
179                  ( ( modifiedBy == zypp::ResStatus::USER       ) && byUser )  )
180             {
181                 if ( regexp.isEmpty() || regexp.indexIn( selectable->name().c_str() ) >= 0 )
182                 {
183                     if ( ! contains( ignoredNames, selectable->name() ) )
184                         _pkgList->addPkgItem( selectable, tryCastToZyppPkg( selectable->theObj() ) );
185                 }
186             }
187         }
188     }
189
190     YQUI::ui()->normalCursor();
191 }
192
193
194 bool
195 YQPkgChangesDialog::isEmpty() const
196 {
197     return _pkgList->topLevelItemCount() == 0;
198 }
199
200
201 QSize
202 YQPkgChangesDialog::sizeHint() const
203 {
204     return limitToScreenSize( this, QDialog::sizeHint() );
205 }
206
207
208 bool
209 YQPkgChangesDialog::showChangesDialog( const QString &  message,
210                                        const QString &  acceptButtonLabel,
211                                        const QString &  rejectButtonLabel,
212                                        bool             showIfListEmpty   )
213 {
214     YQPkgChangesDialog dialog( 0,
215                                message,
216                                acceptButtonLabel,
217                                rejectButtonLabel );
218     dialog.filter();
219
220     if ( dialog.isEmpty() && ! showIfListEmpty )
221         return true;
222
223     dialog.exec();
224
225     return dialog.result() == QDialog::Accepted;
226 }
227
228
229 bool
230 YQPkgChangesDialog::showChangesDialog( const QString &  message,
231                                        const QRegExp &  regexp,
232                                        const QString &  acceptButtonLabel,
233                                        const QString &  rejectButtonLabel,
234                                        bool             showIfListEmpty   )
235 {
236     YQPkgChangesDialog dialog( 0,
237                                message,
238                                acceptButtonLabel,
239                                rejectButtonLabel );
240     dialog.filter( regexp, true, true, true );
241
242     if ( dialog.isEmpty() && ! showIfListEmpty )
243         return true;
244
245     dialog.exec();
246
247     return dialog.result() == QDialog::Accepted;
248 }
249
250
251
252
253 #include "YQPkgChangesDialog.moc"