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