1 /*---------------------------------------------------------------------\
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
11 \----------------------------------------------------------------------/
13 File: YQPkgTextDialog.cc
15 Author: Stefan Hundhammer <sh@suse.de>
17 Textdomain "packages-qt"
22 #define y2log_component "qt-pkg"
23 #include <ycp/y2log.h>
26 #include <q3textbrowser.h>
27 #include <qpushbutton.h>
33 #include <q3boxlayout.h>
36 #include "YQPkgTextDialog.h"
38 #include "QY2LayoutUtils.h"
42 #define SPACING 6 // between subwidgets
43 #define MARGIN 4 // around the widget
49 YQPkgTextDialog::YQPkgTextDialog( const QString & text, QWidget * parent )
52 buildDialog( text, parent, _( "&OK" ) );
56 YQPkgTextDialog::YQPkgTextDialog( const QString & text,
58 const QString & acceptButtonLabel,
59 const QString & rejectButtonLabel )
62 buildDialog( text, parent, acceptButtonLabel, rejectButtonLabel );
66 YQPkgTextDialog::~YQPkgTextDialog()
72 void YQPkgTextDialog::buildDialog( const QString & text,
74 const QString & acceptButtonLabel,
75 const QString & rejectButtonLabel )
77 // Enable dialog resizing even without window manager
78 setSizeGripEnabled( true );
81 setCaption( _( "YaST2" ) );
83 // Layout for the dialog ( can't simply insert a QVBox )
85 Q3VBoxLayout * layout = new Q3VBoxLayout( this, MARGIN, SPACING );
86 Q_CHECK_PTR( layout );
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 );
102 Q3HBox * buttonBox = new Q3HBox( this );
103 Q_CHECK_PTR( buttonBox );
104 buttonBox->setSpacing( SPACING );
105 buttonBox->setMargin ( MARGIN );
106 layout->addWidget( buttonBox );
108 addHStretch( buttonBox );
110 // Accept (OK) button
112 _acceptButton = new QPushButton( acceptButtonLabel, buttonBox );
113 Q_CHECK_PTR( _acceptButton );
114 _acceptButton->setDefault( true );
116 connect( _acceptButton, SIGNAL( clicked() ),
117 this, SLOT ( accept() ) );
119 addHStretch( buttonBox );
121 if ( ! rejectButtonLabel.isEmpty() )
123 // Reject (Cancel) button
125 _rejectButton = new QPushButton( rejectButtonLabel, buttonBox );
126 Q_CHECK_PTR( _rejectButton );
127 _rejectButton->setDefault( true );
129 connect( _rejectButton, SIGNAL( clicked() ),
130 this, SLOT ( reject() ) );
132 addHStretch( buttonBox );
144 YQPkgTextDialog::sizeHint() const
146 return limitToScreenSize( this, 500, 450 );
151 YQPkgTextDialog::eventFilter( QObject * obj, QEvent * ev )
153 if ( ev && ev->type() == QEvent::KeyPress )
155 QKeyEvent * keyEvent = dynamic_cast<QKeyEvent *> (ev);
159 if ( keyEvent->key() == Qt::Key_Return ||
160 keyEvent->key() == Qt::Key_Enter )
162 _acceptButton->animateClick();
163 return true; // Stop event processing
165 else if ( keyEvent->key() == Qt::Key_Escape )
169 _rejectButton->animateClick();
170 return true; // Stop event processing
176 return false; // Don't stop event processing
180 void YQPkgTextDialog::setText( const QString & text )
182 _textBrowser->setText( text );
186 void YQPkgTextDialog::setText( const string & text )
188 setText( fromUTF8( text ) );
192 void YQPkgTextDialog::setText( ZyppSel selectable,
193 const string & text )
195 setText( htmlHeading( selectable ) + htmlParagraphs( text ) );
199 void YQPkgTextDialog::showText( QWidget * parent, const QString & text )
201 YQPkgTextDialog * dia = new YQPkgTextDialog( text, parent );
208 void YQPkgTextDialog::showText( QWidget * parent,
210 const string & text )
212 showText( parent, htmlHeading( selectable ) + fromUTF8( text ) );
216 bool YQPkgTextDialog::confirmText( QWidget * parent,
217 const QString & text,
218 const QString & acceptButtonLabel,
219 const QString & rejectButtonLabel )
221 YQPkgTextDialog * dia = new YQPkgTextDialog( text,
226 bool confirmed = ( dia->exec() == QDialog::Accepted );
233 bool YQPkgTextDialog::confirmText( QWidget * parent, const QString & text )
235 // Translators: "Accept" here refers to licenses or similar
236 return confirmText( parent, text, _( "&Accept" ), _( "&Cancel" ) );
240 bool YQPkgTextDialog::confirmText( QWidget * parent, const char * text )
242 return confirmText( parent, QString( text ) );
246 bool YQPkgTextDialog::confirmText( QWidget * parent,
248 const string & text )
250 return confirmText( parent, htmlHeading( selectable ) + htmlParagraphs( text ) );
258 YQPkgTextDialog::htmlEscape( const QString & plainText )
260 QString html = plainText;
261 // y2debug( "Escaping '%s'", (const char *) plainText );
263 html.replace( QRegExp( "&" ), "&" );
264 html.replace( QRegExp( "<" ), "<" );
265 html.replace( QRegExp( ">" ), ">" );
273 YQPkgTextDialog::htmlParagraphs( const string & rawText )
275 QString text = fromUTF8( rawText );
277 if ( text.contains( "<!-- DT:Rich -->" ) ) // Special doctype for preformatted HTML
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>" );
291 YQPkgTextDialog::htmlHeading( const QString & text )
294 "<table bgcolor=#E0E0F8><tr><td><b>"
296 + "</b></td></tr></table><br>";
303 YQPkgTextDialog::htmlHeading( ZyppSel selectable )
308 ZyppObj zyppObj = selectable->theObj();
313 QString summary = fromUTF8( zyppObj->summary() );
316 "<table bgcolor=#E0E0F8><tr><td><b>"
317 + fromUTF8( zyppObj->name() )
320 if ( ! summary.isEmpty() )
321 html += " - " + summary;
323 html += "</td></tr></table><br>";
331 #include "YQPkgTextDialog.moc"