simplify build requires
[duncan/yast2-qt4.git] / src / pkg / YQPkgTextDialog.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQPkgTextDialog.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
25 #include <QTextBrowser>
26 #include <QPushButton>
27 #include <QRegExp>
28 #include <QLayout>
29 #include <QHBoxLayout>
30 #include <QKeyEvent>
31 #include <QBoxLayout>
32 #include <QEvent>
33
34 #include "YQPkgTextDialog.h"
35
36 #include "QY2LayoutUtils.h"
37 #include "YQi18n.h"
38 #include "utf8.h"
39
40 #define SPACING                 6       // between subwidgets
41 #define MARGIN                  4       // around the widget
42
43 using std::string;
44
45
46 YQPkgTextDialog::YQPkgTextDialog( const QString & text, QWidget * parent )
47     : QDialog( parent )
48 {
49     buildDialog( text, parent, _( "&OK" ) );
50 }
51
52
53 YQPkgTextDialog::YQPkgTextDialog( const QString &       text,
54                                   QWidget *             parent,
55                                   const QString &       acceptButtonLabel,
56                                   const QString &       rejectButtonLabel )
57     : QDialog( parent )
58 {
59     buildDialog( text, parent, acceptButtonLabel, rejectButtonLabel );
60 }
61
62
63 YQPkgTextDialog::~YQPkgTextDialog()
64 {
65     // NOP
66 }
67
68
69 void YQPkgTextDialog::buildDialog( const QString &      text,
70                                    QWidget *            parent,
71                                    const QString &      acceptButtonLabel,
72                                    const QString &      rejectButtonLabel )
73 {
74     // Enable dialog resizing even without window manager
75     setSizeGripEnabled( true );
76
77     // Dialog title
78     setWindowTitle( _( "YaST2" ) );
79
80     // Layout for the dialog ( can't simply insert a QVBox )
81
82     QVBoxLayout * layout = new QVBoxLayout( this );
83     layout->setMargin(MARGIN);
84     layout->setSpacing(SPACING);
85     Q_CHECK_PTR( layout );
86
87
88     // Text browser
89
90     _textBrowser = new QTextBrowser( this );
91     Q_CHECK_PTR( _textBrowser );
92     layout->addWidget( _textBrowser );
93     layout->addSpacing(8);
94     _textBrowser->document()->setHtml( text );
95     _textBrowser->installEventFilter( this );
96
97
98     // Button box
99
100     QHBoxLayout * buttonBox     = new QHBoxLayout( this );
101     Q_CHECK_PTR( buttonBox );
102     buttonBox->setSpacing( SPACING );
103     buttonBox->setMargin ( MARGIN  );
104     layout->addLayout( buttonBox );
105
106     //addHStretch( buttonBox );
107
108     // Accept (OK) button
109
110     _acceptButton = new QPushButton( acceptButtonLabel, this );
111     buttonBox->addWidget(_acceptButton);
112     Q_CHECK_PTR( _acceptButton );
113     _acceptButton->setDefault( true );
114
115     connect( _acceptButton,     SIGNAL( clicked() ),
116              this,              SLOT  ( accept()  ) );
117
118     //addHStretch( buttonBox );
119
120     if ( ! rejectButtonLabel.isEmpty() )
121     {
122         // Reject (Cancel) button
123
124         _rejectButton = new QPushButton( rejectButtonLabel, this );
125   buttonBox->addWidget(_rejectButton);
126         Q_CHECK_PTR( _rejectButton );
127         _rejectButton->setDefault( true );
128
129         connect( _rejectButton, SIGNAL( clicked() ),
130                  this,          SLOT  ( reject()  ) );
131
132 //      addHStretch( buttonBox );
133     }
134     else
135     {
136         _rejectButton = 0;
137     }
138
139     updateGeometry();
140 }
141
142
143 QSize
144 YQPkgTextDialog::sizeHint() const
145 {
146     return limitToScreenSize( this, 500, 450 );
147 }
148
149
150 bool
151 YQPkgTextDialog::eventFilter( QObject * obj, QEvent * ev )
152 {
153     if ( ev && ev->type() == QEvent::KeyPress )
154     {
155         QKeyEvent * keyEvent = dynamic_cast<QKeyEvent *> (ev);
156
157         if ( keyEvent )
158         {
159             if ( keyEvent->key() == Qt::Key_Return ||
160                  keyEvent->key() == Qt::Key_Enter    )
161             {
162                 _acceptButton->animateClick();
163                 return true; // Stop event processing
164             }
165             else if ( keyEvent->key() == Qt::Key_Escape )
166             {
167                 if ( _rejectButton )
168                 {
169                     _rejectButton->animateClick();
170                     return true; // Stop event processing
171                 }
172             }
173         }
174     }
175
176     return false;       // Don't stop event processing
177 }
178
179
180 void YQPkgTextDialog::setText( const QString & text )
181 {
182     _textBrowser->document()->setHtml( text );
183 }
184
185
186 void YQPkgTextDialog::setText( const string & text )
187 {
188     setText( fromUTF8( text ) );
189 }
190
191
192 void YQPkgTextDialog::setText( ZyppSel selectable,
193                                const string &            text )
194 {
195     setText( htmlHeading( selectable ) + htmlParagraphs( text ) );
196 }
197
198
199 void YQPkgTextDialog::showText( QWidget * parent, const QString & text )
200 {
201     YQPkgTextDialog * dia = new YQPkgTextDialog( text, parent );
202     Q_CHECK_PTR( dia );
203     dia->exec();
204     delete dia;
205 }
206
207
208 void YQPkgTextDialog::showText( QWidget *       parent,
209                                 ZyppSel         selectable,
210                                 const string &  text )
211 {
212     showText( parent, htmlHeading( selectable ) + fromUTF8( text ) );
213 }
214
215
216 bool YQPkgTextDialog::confirmText( QWidget *            parent,
217                                    const QString &      text,
218                                    const QString &      acceptButtonLabel,
219                                    const QString &      rejectButtonLabel )
220 {
221     YQPkgTextDialog * dia = new YQPkgTextDialog( text,
222                                                  parent,
223                                                  acceptButtonLabel,
224                                                  rejectButtonLabel );
225     Q_CHECK_PTR( dia );
226     bool confirmed = ( dia->exec() == QDialog::Accepted );
227     delete dia;
228
229     return confirmed;
230 }
231
232
233 bool YQPkgTextDialog::confirmText( QWidget * parent, const QString & text )
234 {
235     // Translators: "Accept" here refers to licenses or similar
236     return confirmText( parent, text, _( "&Accept" ), _( "&Cancel" ) );
237 }
238
239
240 bool YQPkgTextDialog::confirmText( QWidget * parent, const char * text )
241 {
242     return confirmText( parent, QString( text ) );
243 }
244
245
246 bool YQPkgTextDialog::confirmText( QWidget *            parent,
247                                    ZyppSel              selectable,
248                                    const string &       text )
249 {
250     return confirmText( parent, htmlHeading( selectable ) + htmlParagraphs( text ) );
251 }
252
253
254
255
256
257 QString
258 YQPkgTextDialog::htmlEscape( const QString & plainText )
259 {
260     QString html = plainText;
261     // y2debug( "Escaping '%s'", (const char *) plainText );
262
263     html.replace( QRegExp( "&" ), "&amp;" );
264     html.replace( QRegExp( "<" ), "&lt;"  );
265     html.replace( QRegExp( ">" ), "&gt;"  );
266
267     return html;
268 }
269
270
271
272 QString
273 YQPkgTextDialog::htmlParagraphs( const string & rawText )
274 {
275     QString text = fromUTF8( rawText );
276
277     if ( text.contains( "<!-- DT:Rich -->" ) )  // Special doctype for preformatted HTML
278         return text;
279
280     text = htmlEscape( text );                  // Escape '<', '>', '&'
281     text.replace( "\n\n", "</p><p>" );          // Empty lines mean new paragraph
282     text.prepend( "<p>"  );
283     text.append ( "</p>" );
284
285     return text;
286 }
287
288
289
290 QString
291 YQPkgTextDialog::htmlHeading( const QString & text )
292 {
293     QString html =
294         "<table bgcolor=#E0E0F8><tr><td><b>"
295         + text
296         + "</b></td></tr></table><br>";
297
298     return html;
299 }
300
301
302 QString
303 YQPkgTextDialog::htmlHeading( ZyppSel selectable )
304 {
305     if ( ! selectable )
306         return "";
307
308     ZyppObj zyppObj = selectable->theObj();
309
310     if ( ! zyppObj )
311         return "";
312
313     QString summary = fromUTF8( zyppObj->summary() );
314
315     QString html =
316         "<table bgcolor=#E0E0F8><tr><td><b>"
317         + fromUTF8( zyppObj->name() )
318         + "</b>";
319
320     if ( ! summary.isEmpty() )
321         html += " - " + summary;
322
323     html += "</td></tr></table><br>";
324
325     return html;
326 }
327
328
329
330
331 #include "YQPkgTextDialog.moc"