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