]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/pkg/YQPkgTextDialog.cc
picking up branches/tmp/sh/qt4-port/, merging it with trunk
[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 <q3textbrowser.h>
27 #include <qpushbutton.h>
28 #include <qregexp.h>
29 #include <qlayout.h>
30 #include <q3hbox.h>
31 //Added by qt3to4:
32 #include <qevent.h>
33 #include <q3boxlayout.h>
34 #include <qevent.h>
35
36 #include "YQPkgTextDialog.h"
37
38 #include "QY2LayoutUtils.h"
39 #include "YQi18n.h"
40 #include "utf8.h"
41
42 #define SPACING                 6       // between subwidgets
43 #define MARGIN                  4       // around the widget
44
45 using std::string;
46
47
48
49 YQPkgTextDialog::YQPkgTextDialog( const QString & text, QWidget * parent )
50     : QDialog( parent )
51 {
52     buildDialog( text, parent, _( "&OK" ) );
53 }
54
55
56 YQPkgTextDialog::YQPkgTextDialog( const QString &       text,
57                                   QWidget *             parent,
58                                   const QString &       acceptButtonLabel,
59                                   const QString &       rejectButtonLabel )
60     : QDialog( parent )
61 {
62     buildDialog( text, parent, acceptButtonLabel, rejectButtonLabel );
63 }
64
65
66 YQPkgTextDialog::~YQPkgTextDialog()
67 {
68     // NOP
69 }
70
71
72 void YQPkgTextDialog::buildDialog( const QString &      text,
73                                    QWidget *            parent,
74                                    const QString &      acceptButtonLabel,
75                                    const QString &      rejectButtonLabel )
76 {
77     // Enable dialog resizing even without window manager
78     setSizeGripEnabled( true );
79
80     // Dialog title
81     setCaption( _( "YaST2" ) );
82
83     // Layout for the dialog ( can't simply insert a QVBox )
84
85     Q3VBoxLayout * layout = new Q3VBoxLayout( this, MARGIN, SPACING );
86     Q_CHECK_PTR( layout );
87
88
89     // Text browser
90
91     _textBrowser = new Q3TextBrowser( this );
92     Q_CHECK_PTR( _textBrowser );
93     layout->addWidget( _textBrowser );
94     layout->addSpacing(8);
95     _textBrowser->setText( text );
96     _textBrowser->setTextFormat( Qt::RichText );
97     _textBrowser->installEventFilter( this );
98
99
100     // Button box
101
102     Q3HBox * buttonBox  = new Q3HBox( this );
103     Q_CHECK_PTR( buttonBox );
104     buttonBox->setSpacing( SPACING );
105     buttonBox->setMargin ( MARGIN  );
106     layout->addWidget( buttonBox );
107
108     addHStretch( buttonBox );
109
110     // Accept (OK) button
111
112     _acceptButton = new QPushButton( acceptButtonLabel, buttonBox );
113     Q_CHECK_PTR( _acceptButton );
114     _acceptButton->setDefault( true );
115
116     connect( _acceptButton,     SIGNAL( clicked() ),
117              this,              SLOT  ( accept()  ) );
118
119     addHStretch( buttonBox );
120
121     if ( ! rejectButtonLabel.isEmpty() )
122     {
123         // Reject (Cancel) button
124
125         _rejectButton = new QPushButton( rejectButtonLabel, buttonBox );
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->setText( 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"