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 <qtextbrowser.h>
27 #include <qpushbutton.h>
32 #include "YQPkgTextDialog.h"
34 #include "QY2LayoutUtils.h"
38 #define SPACING 6 // between subwidgets
39 #define MARGIN 4 // around the widget
45 YQPkgTextDialog::YQPkgTextDialog( const QString & text, QWidget * parent )
48 buildDialog( text, parent, _( "&OK" ) );
52 YQPkgTextDialog::YQPkgTextDialog( const QString & text,
54 const QString & acceptButtonLabel,
55 const QString & rejectButtonLabel )
58 buildDialog( text, parent, acceptButtonLabel, rejectButtonLabel );
62 YQPkgTextDialog::~YQPkgTextDialog()
68 void YQPkgTextDialog::buildDialog( const QString & text,
70 const QString & acceptButtonLabel,
71 const QString & rejectButtonLabel )
73 // Enable dialog resizing even without window manager
74 setSizeGripEnabled( true );
77 setCaption( _( "YaST2" ) );
79 // Layout for the dialog ( can't simply insert a QVBox )
81 QVBoxLayout * layout = new QVBoxLayout( this, MARGIN, SPACING );
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 );
98 QHBox * buttonBox = new QHBox( this );
99 CHECK_PTR( buttonBox );
100 buttonBox->setSpacing( SPACING );
101 buttonBox->setMargin ( MARGIN );
102 layout->addWidget( buttonBox );
104 addHStretch( buttonBox );
106 // Accept (OK) button
108 _acceptButton = new QPushButton( acceptButtonLabel, buttonBox );
109 CHECK_PTR( _acceptButton );
110 _acceptButton->setDefault( true );
112 connect( _acceptButton, SIGNAL( clicked() ),
113 this, SLOT ( accept() ) );
115 addHStretch( buttonBox );
117 if ( ! rejectButtonLabel.isEmpty() )
119 // Reject (Cancel) button
121 _rejectButton = new QPushButton( rejectButtonLabel, buttonBox );
122 CHECK_PTR( _rejectButton );
123 _rejectButton->setDefault( true );
125 connect( _rejectButton, SIGNAL( clicked() ),
126 this, SLOT ( reject() ) );
128 addHStretch( buttonBox );
140 YQPkgTextDialog::sizeHint() const
142 return limitToScreenSize( this, 500, 450 );
147 YQPkgTextDialog::eventFilter( QObject * obj, QEvent * ev )
149 if ( ev && ev->type() == QEvent::KeyPress )
151 QKeyEvent * keyEvent = dynamic_cast<QKeyEvent *> (ev);
155 if ( keyEvent->key() == Key_Return ||
156 keyEvent->key() == Key_Enter )
158 _acceptButton->animateClick();
159 return true; // Stop event processing
161 else if ( keyEvent->key() == Key_Escape )
165 _rejectButton->animateClick();
166 return true; // Stop event processing
172 return false; // Don't stop event processing
176 void YQPkgTextDialog::setText( const QString & text )
178 _textBrowser->setText( text );
182 void YQPkgTextDialog::setText( const string & text )
184 setText( fromUTF8( text ) );
188 void YQPkgTextDialog::setText( ZyppSel selectable,
189 const string & text )
191 setText( htmlHeading( selectable ) + htmlParagraphs( text ) );
195 void YQPkgTextDialog::showText( QWidget * parent, const QString & text )
197 YQPkgTextDialog * dia = new YQPkgTextDialog( text, parent );
204 void YQPkgTextDialog::showText( QWidget * parent,
206 const string & text )
208 showText( parent, htmlHeading( selectable ) + fromUTF8( text ) );
212 bool YQPkgTextDialog::confirmText( QWidget * parent,
213 const QString & text,
214 const QString & acceptButtonLabel,
215 const QString & rejectButtonLabel )
217 YQPkgTextDialog * dia = new YQPkgTextDialog( text,
222 bool confirmed = ( dia->exec() == QDialog::Accepted );
229 bool YQPkgTextDialog::confirmText( QWidget * parent, const QString & text )
231 // Translators: "Accept" here refers to licenses or similar
232 return confirmText( parent, text, _( "&Accept" ), _( "&Cancel" ) );
236 bool YQPkgTextDialog::confirmText( QWidget * parent, const char * text )
238 return confirmText( parent, QString( text ) );
242 bool YQPkgTextDialog::confirmText( QWidget * parent,
244 const string & text )
246 return confirmText( parent, htmlHeading( selectable ) + htmlParagraphs( text ) );
254 YQPkgTextDialog::htmlEscape( const QString & plainText )
256 QString html = plainText;
257 // y2debug( "Escaping '%s'", (const char *) plainText );
259 html.replace( QRegExp( "&" ), "&" );
260 html.replace( QRegExp( "<" ), "<" );
261 html.replace( QRegExp( ">" ), ">" );
269 YQPkgTextDialog::htmlParagraphs( const string & rawText )
271 QString text = fromUTF8( rawText );
273 if ( text.contains( "<!-- DT:Rich -->" ) ) // Special doctype for preformatted HTML
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>" );
287 YQPkgTextDialog::htmlHeading( const QString & text )
290 "<table bgcolor=#E0E0F8><tr><td><b>"
292 + "</b></td></tr></table><br>";
299 YQPkgTextDialog::htmlHeading( ZyppSel selectable )
304 ZyppObj zyppObj = selectable->theObj();
309 QString summary = fromUTF8( zyppObj->summary() );
312 "<table bgcolor=#E0E0F8><tr><td><b>"
313 + fromUTF8( zyppObj->name() )
316 if ( ! summary.isEmpty() )
317 html += " - " + summary;
319 html += "</td></tr></table><br>";
327 #include "YQPkgTextDialog.moc"