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