- Don't create layouts with parent. Qt 4.x automatically reparents
[duncan/yast2-qt4.git] / src / pkg / YQPkgDescriptionDialog.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQPkgDescriptionDialog.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 <QHBoxLayout>
27 #include <QLabel>
28 #include <QPushButton>
29 #include <QSplitter>
30 #include <QStyle>
31 #include <QList>
32 #include <QBoxLayout>
33
34 #include "YQPkgDescriptionDialog.h"
35 #include "YQPkgDescriptionView.h"
36 #include "YQPkgList.h"
37 #include "QY2LayoutUtils.h"
38 #include "YQi18n.h"
39 #include "YQUI.h"
40
41
42 #define SPACING                 2       // between subwidgets
43 #define MARGIN                  4       // around the widget
44
45
46 YQPkgDescriptionDialog::YQPkgDescriptionDialog( QWidget * parent, const QString & pkgName )
47     : QDialog( parent )
48 {
49     // Dialog title
50     setWindowTitle( _( "Package Description" ) );
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     setLayout(layout);
60     layout->setMargin(MARGIN);
61     layout->setSpacing(SPACING);
62     
63
64
65     // VBox for splitter
66
67     QSplitter * splitter = new QSplitter( Qt::Vertical, this );
68     Q_CHECK_PTR( splitter );
69     layout->addWidget( splitter );
70
71     // Pkg list
72
73     _pkgList = new YQPkgList( splitter );
74     Q_CHECK_PTR( _pkgList );
75     _pkgList->resize( _pkgList->width(), 80 );
76
77
78     // Description view
79
80     _pkgDescription = new YQPkgDescriptionView( splitter );
81     Q_CHECK_PTR( _pkgDescription );
82     _pkgDescription->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) ); // hor/vert
83
84     connect( _pkgList,          SIGNAL( currentItemChanged    ( ZyppSel ) ),
85              _pkgDescription,   SLOT  ( showDetailsIfVisible( ZyppSel ) ) );
86
87
88     // Button box (to center the single button)
89
90     QHBoxLayout * hbox = new QHBoxLayout();
91     Q_CHECK_PTR( hbox );
92     hbox->setSpacing( SPACING );
93     hbox->setMargin ( MARGIN  );
94     layout->addLayout( hbox );
95
96     // "OK" button
97
98     QPushButton * button = new QPushButton( _( "&OK" ), this );
99     Q_CHECK_PTR( button );
100     hbox->addWidget(button);
101     button->setDefault( true );
102
103     connect( button,    SIGNAL( clicked() ),
104              this,      SLOT  ( accept()  ) );
105
106     hbox->addStretch();
107
108
109     filter( pkgName );
110 }
111
112
113 void
114 YQPkgDescriptionDialog::filter( const QString & qPkgName )
115 {
116     std::string pkgName( qPrintable(qPkgName) );
117     YQUI::ui()->busyCursor();
118     _pkgList->clear();
119
120
121     // Search for pkgs with that name
122
123     for ( ZyppPoolIterator it = zyppPkgBegin();
124           it != zyppPkgEnd();
125           ++it )
126     {
127         ZyppObj zyppObj = (*it)->theObj();
128
129         if ( zyppObj && zyppObj->name() == pkgName )
130             _pkgList->addPkgItem( *it, tryCastToZyppPkg( zyppObj ) );
131     }
132
133 #if FIXME
134     // Display description of the first pkg with that name
135
136     YQPkgObjListItem * firstItem = dynamic_cast<YQPkgObjListItem *> ( _pkgList->firstChild() );
137
138     if ( firstItem )
139         _pkgDescription->showDetailsIfVisible( firstItem->selectable() );
140     else
141         _pkgDescription->clear();
142 #endif
143
144     YQUI::ui()->normalCursor();
145 }
146
147
148 bool
149 YQPkgDescriptionDialog::isEmpty() const
150 {
151 #if FIXME
152     return _pkgList->childCount() == 0;
153 #else
154     return true;
155 #endif
156 }
157
158
159 QSize
160 YQPkgDescriptionDialog::sizeHint() const
161 {
162     QRect available = qApp->desktop()->availableGeometry( (QWidget *) this );
163     QSize size = QDialog::sizeHint();
164     size = size.boundedTo( QSize( available.width(), available.height() ) );
165
166     return size;
167 }
168
169
170 void
171 YQPkgDescriptionDialog::showDescriptionDialog( const QString & pkgName )
172 {
173     YQPkgDescriptionDialog dialog( 0, pkgName );
174     dialog.exec();
175 }
176
177
178
179
180 #include "YQPkgDescriptionDialog.moc"