1 /*---------------------------------------------------------------------\
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
11 \----------------------------------------------------------------------/
13 File: YQPkgTextDialog.cc
15 Author: Stefan Hundhammer <sh@suse.de>
17 Textdomain "packages-qt"
23 #define y2log_component "qt-pkg"
24 #include <ycp/y2log.h>
27 #include <q3textbrowser.h>
28 #include <qpushbutton.h>
34 #include <q3boxlayout.h>
37 #include "YQPkgTextDialog.h"
39 #include "QY2LayoutUtils.h"
43 #define SPACING 6 // between subwidgets
44 #define MARGIN 4 // around the widget
50 YQPkgTextDialog::YQPkgTextDialog( const QString & text, QWidget * parent )
53 buildDialog( text, parent, _( "&OK" ) );
57 YQPkgTextDialog::YQPkgTextDialog( const QString & text,
59 const QString & acceptButtonLabel,
60 const QString & rejectButtonLabel )
63 buildDialog( text, parent, acceptButtonLabel, rejectButtonLabel );
67 YQPkgTextDialog::~YQPkgTextDialog()
73 void YQPkgTextDialog::buildDialog( const QString & text,
75 const QString & acceptButtonLabel,
76 const QString & rejectButtonLabel )
78 // Enable dialog resizing even without window manager
79 setSizeGripEnabled( true );
82 setCaption( _( "YaST2" ) );
84 // Layout for the dialog ( can't simply insert a QVBox )
86 Q3VBoxLayout * layout = new Q3VBoxLayout( this, MARGIN, SPACING );
87 Q_CHECK_PTR( layout );
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 );
103 Q3HBox * buttonBox = new Q3HBox( this );
104 Q_CHECK_PTR( buttonBox );
105 buttonBox->setSpacing( SPACING );
106 buttonBox->setMargin ( MARGIN );
107 layout->addWidget( buttonBox );
109 addHStretch( buttonBox );
111 // Accept (OK) button
113 _acceptButton = new QPushButton( acceptButtonLabel, buttonBox );
114 Q_CHECK_PTR( _acceptButton );
115 _acceptButton->setDefault( true );
117 connect( _acceptButton, SIGNAL( clicked() ),
118 this, SLOT ( accept() ) );
120 addHStretch( buttonBox );
122 if ( ! rejectButtonLabel.isEmpty() )
124 // Reject (Cancel) button
126 _rejectButton = new QPushButton( rejectButtonLabel, buttonBox );
127 Q_CHECK_PTR( _rejectButton );
128 _rejectButton->setDefault( true );
130 connect( _rejectButton, SIGNAL( clicked() ),
131 this, SLOT ( reject() ) );
133 addHStretch( buttonBox );
145 YQPkgTextDialog::sizeHint() const
147 return limitToScreenSize( this, 500, 450 );
152 YQPkgTextDialog::eventFilter( QObject * obj, QEvent * ev )
154 if ( ev && ev->type() == QEvent::KeyPress )
156 QKeyEvent * keyEvent = dynamic_cast<QKeyEvent *> (ev);
160 if ( keyEvent->key() == Qt::Key_Return ||
161 keyEvent->key() == Qt::Key_Enter )
163 _acceptButton->animateClick();
164 return true; // Stop event processing
166 else if ( keyEvent->key() == Qt::Key_Escape )
170 _rejectButton->animateClick();
171 return true; // Stop event processing
177 return false; // Don't stop event processing
181 void YQPkgTextDialog::setText( const QString & text )
183 _textBrowser->setText( text );
187 void YQPkgTextDialog::setText( const string & text )
189 setText( fromUTF8( text ) );
193 void YQPkgTextDialog::setText( ZyppSel selectable,
194 const string & text )
196 setText( htmlHeading( selectable ) + htmlParagraphs( text ) );
200 void YQPkgTextDialog::showText( QWidget * parent, const QString & text )
202 YQPkgTextDialog * dia = new YQPkgTextDialog( text, parent );
209 void YQPkgTextDialog::showText( QWidget * parent,
211 const string & text )
213 showText( parent, htmlHeading( selectable ) + fromUTF8( text ) );
217 bool YQPkgTextDialog::confirmText( QWidget * parent,
218 const QString & text,
219 const QString & acceptButtonLabel,
220 const QString & rejectButtonLabel )
222 YQPkgTextDialog * dia = new YQPkgTextDialog( text,
227 bool confirmed = ( dia->exec() == QDialog::Accepted );
234 bool YQPkgTextDialog::confirmText( QWidget * parent, const QString & text )
236 // Translators: "Accept" here refers to licenses or similar
237 return confirmText( parent, text, _( "&Accept" ), _( "&Cancel" ) );
241 bool YQPkgTextDialog::confirmText( QWidget * parent, const char * text )
243 return confirmText( parent, QString( text ) );
247 bool YQPkgTextDialog::confirmText( QWidget * parent,
249 const string & text )
251 return confirmText( parent, htmlHeading( selectable ) + htmlParagraphs( text ) );
259 YQPkgTextDialog::htmlEscape( const QString & plainText )
261 QString html = plainText;
262 // y2debug( "Escaping '%s'", (const char *) plainText );
264 html.replace( QRegExp( "&" ), "&" );
265 html.replace( QRegExp( "<" ), "<" );
266 html.replace( QRegExp( ">" ), ">" );
274 YQPkgTextDialog::htmlParagraphs( const string & rawText )
276 QString text = fromUTF8( rawText );
278 if ( text.contains( "<!-- DT:Rich -->" ) ) // Special doctype for preformatted HTML
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>" );
292 YQPkgTextDialog::htmlHeading( const QString & text )
295 "<table bgcolor=#E0E0F8><tr><td><b>"
297 + "</b></td></tr></table><br>";
304 YQPkgTextDialog::htmlHeading( ZyppSel selectable )
309 ZyppObj zyppObj = selectable->theObj();
314 QString summary = fromUTF8( zyppObj->summary() );
317 "<table bgcolor=#E0E0F8><tr><td><b>"
318 + fromUTF8( zyppObj->name() )
321 if ( ! summary.isEmpty() )
322 html += " - " + summary;
324 html += "</td></tr></table><br>";
332 #include "YQPkgTextDialog.moc"