1 /*---------------------------------------------------------------------\
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
11 \----------------------------------------------------------------------/
13 File: YQPkgTextDialog.cc
15 Author: Stefan Hundhammer <sh@suse.de>
17 Textdomain "packages-qt"
21 #define y2log_component "qt-pkg"
22 #include <ycp/y2log.h>
25 #include <QTextBrowser>
26 #include <QPushButton>
29 #include <QHBoxLayout>
34 #include "YQPkgTextDialog.h"
36 #include "QY2LayoutUtils.h"
40 #define SPACING 6 // between subwidgets
41 #define MARGIN 4 // around the widget
46 YQPkgTextDialog::YQPkgTextDialog( const QString & text, QWidget * parent )
49 buildDialog( text, parent, _( "&OK" ) );
53 YQPkgTextDialog::YQPkgTextDialog( const QString & text,
55 const QString & acceptButtonLabel,
56 const QString & rejectButtonLabel )
59 buildDialog( text, parent, acceptButtonLabel, rejectButtonLabel );
63 YQPkgTextDialog::~YQPkgTextDialog()
69 void YQPkgTextDialog::buildDialog( const QString & text,
71 const QString & acceptButtonLabel,
72 const QString & rejectButtonLabel )
74 // Enable dialog resizing even without window manager
75 setSizeGripEnabled( true );
78 setWindowTitle( _( "YaST2" ) );
80 // Layout for the dialog ( can't simply insert a QVBox )
82 QVBoxLayout * layout = new QVBoxLayout( this );
83 layout->setMargin(MARGIN);
84 layout->setSpacing(SPACING);
85 Q_CHECK_PTR( layout );
90 _textBrowser = new QTextBrowser( this );
91 Q_CHECK_PTR( _textBrowser );
92 layout->addWidget( _textBrowser );
93 layout->addSpacing(8);
94 _textBrowser->document()->setHtml( text );
95 _textBrowser->installEventFilter( this );
100 QHBoxLayout * buttonBox = new QHBoxLayout( this );
101 Q_CHECK_PTR( buttonBox );
102 buttonBox->setSpacing( SPACING );
103 buttonBox->setMargin ( MARGIN );
104 layout->addLayout( buttonBox );
106 //addHStretch( buttonBox );
108 // Accept (OK) button
110 _acceptButton = new QPushButton( acceptButtonLabel, this );
111 buttonBox->addWidget(_acceptButton);
112 Q_CHECK_PTR( _acceptButton );
113 _acceptButton->setDefault( true );
115 connect( _acceptButton, SIGNAL( clicked() ),
116 this, SLOT ( accept() ) );
118 //addHStretch( buttonBox );
120 if ( ! rejectButtonLabel.isEmpty() )
122 // Reject (Cancel) button
124 _rejectButton = new QPushButton( rejectButtonLabel, this );
125 buttonBox->addWidget(_rejectButton);
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->document()->setHtml( 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"