libyui-qt-pkg  2.45.15.2
YQPkgTextDialog.cc
1 /**************************************************************************
2 Copyright (C) 2000 - 2010 Novell, Inc.
3 All Rights Reserved.
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 
19 **************************************************************************/
20 
21 
22 /*---------------------------------------------------------------------\
23 | |
24 | __ __ ____ _____ ____ |
25 | \ \ / /_ _/ ___|_ _|___ \ |
26 | \ V / _` \___ \ | | __) | |
27 | | | (_| |___) || | / __/ |
28 | |_|\__,_|____/ |_| |_____| |
29 | |
30 | core system |
31 | (C) SuSE GmbH |
32 \----------------------------------------------------------------------/
33 
34  File: YQPkgTextDialog.cc
35 
36  Author: Stefan Hundhammer <sh@suse.de>
37 
38  Textdomain "qt-pkg"
39 
40 /-*/
41 
42 #define YUILogComponent "qt-pkg"
43 #include "YUILog.h"
44 
45 
46 #include <QTextBrowser>
47 #include <QPushButton>
48 #include <QRegExp>
49 #include <QLayout>
50 #include <QHBoxLayout>
51 #include <QKeyEvent>
52 #include <QBoxLayout>
53 #include <QEvent>
54 #include <QApplication>
55 
56 #include "YQPkgTextDialog.h"
57 
58 #include "QY2LayoutUtils.h"
59 #include "YQi18n.h"
60 #include "utf8.h"
61 
62 #define SPACING 6 // between subwidgets
63 #define MARGIN 4 // around the widget
64 
65 using std::string;
66 
67 
68 YQPkgTextDialog::YQPkgTextDialog( const QString & text, QWidget * parent )
69  : QDialog( parent )
70 {
71  buildDialog( text, parent, _( "&OK" ) );
72 }
73 
74 
75 YQPkgTextDialog::YQPkgTextDialog( const QString & text,
76  QWidget * parent,
77  const QString & acceptButtonLabel,
78  const QString & rejectButtonLabel )
79  : QDialog( parent )
80 {
81  buildDialog( text, parent, acceptButtonLabel, rejectButtonLabel );
82 }
83 
84 
86 {
87  // NOP
88 }
89 
90 
91 void YQPkgTextDialog::buildDialog( const QString & text,
92  QWidget * parent,
93  const QString & acceptButtonLabel,
94  const QString & rejectButtonLabel )
95 {
96  // Enable dialog resizing even without window manager
97  setSizeGripEnabled( true );
98 
99  // Dialog title
100  setWindowTitle( _( "YaST" ) );
101 
102  // Layout for the dialog ( can't simply insert a QVBox )
103 
104  QVBoxLayout * layout = new QVBoxLayout();
105  Q_CHECK_PTR( layout );
106  setLayout( layout );
107  layout->setMargin ( MARGIN );
108  layout->setSpacing( SPACING );
109 
110  // Text browser
111 
112  _textBrowser = new QTextBrowser( this );
113  Q_CHECK_PTR( _textBrowser );
114  layout->addWidget( _textBrowser );
115  layout->addSpacing( 2 );
116  _textBrowser->document()->setHtml( text );
117  _textBrowser->scrollToAnchor( "top" );
118  _textBrowser->installEventFilter( this );
119 
120 
121  // Button box
122 
123  QHBoxLayout * buttonBox = new QHBoxLayout();
124  Q_CHECK_PTR( buttonBox );
125  buttonBox->setSpacing( SPACING );
126  buttonBox->setMargin ( MARGIN );
127  layout->addLayout( buttonBox );
128  buttonBox->addStretch();
129 
130  // Accept (OK) button
131 
132  _acceptButton = new QPushButton( acceptButtonLabel, this );
133  buttonBox->addWidget(_acceptButton);
134  Q_CHECK_PTR( _acceptButton );
135  _acceptButton->setDefault( true );
136 
137  connect( _acceptButton, SIGNAL( clicked() ),
138  this, SLOT ( accept() ) );
139 
140  buttonBox->addStretch();
141 
142  if ( ! rejectButtonLabel.isEmpty() )
143  {
144  // Reject (Cancel) button
145 
146  _rejectButton = new QPushButton( rejectButtonLabel, this );
147  buttonBox->addWidget(_rejectButton);
148  Q_CHECK_PTR( _rejectButton );
149  _rejectButton->setDefault( true );
150 
151  connect( _rejectButton, SIGNAL( clicked() ),
152  this, SLOT ( reject() ) );
153 
154  buttonBox->addStretch();
155  }
156  else
157  {
158  _rejectButton = 0;
159  }
160 
161  updateGeometry();
162 }
163 
164 
165 QSize
167 {
168  return limitToScreenSize( this, 500, 450 );
169 }
170 
171 
172 bool
173 YQPkgTextDialog::eventFilter( QObject * obj, QEvent * ev )
174 {
175  if ( ev && ev->type() == QEvent::KeyPress )
176  {
177  QKeyEvent * keyEvent = dynamic_cast<QKeyEvent *> (ev);
178 
179  if ( keyEvent )
180  {
181  if ( keyEvent->key() == Qt::Key_Return ||
182  keyEvent->key() == Qt::Key_Enter )
183  {
184  _acceptButton->animateClick();
185  return true; // Stop event processing
186  }
187  else if ( keyEvent->key() == Qt::Key_Escape )
188  {
189  if ( _rejectButton )
190  {
191  _rejectButton->animateClick();
192  return true; // Stop event processing
193  }
194  }
195  }
196  }
197 
198  return false; // Don't stop event processing
199 }
200 
201 
202 void YQPkgTextDialog::setText( const QString & text )
203 {
204  _textBrowser->document()->setHtml( text );
205 }
206 
207 
208 void YQPkgTextDialog::setText( const string & text )
209 {
210  setText( fromUTF8( text ) );
211 }
212 
213 
214 void YQPkgTextDialog::setText( ZyppSel selectable,
215  const string & text )
216 {
217  setText( htmlHeading( selectable ) + htmlParagraphs( text ) );
218 }
219 
220 
221 void YQPkgTextDialog::showText( QWidget * parent, const QString & text )
222 {
223  YQPkgTextDialog * dia = new YQPkgTextDialog( text, parent );
224  Q_CHECK_PTR( dia );
225  dia->exec();
226  delete dia;
227 }
228 
229 
230 void YQPkgTextDialog::showText( QWidget * parent,
231  ZyppSel selectable,
232  const string & text )
233 {
234  showText( parent, htmlHeading( selectable ) + fromUTF8( text ) );
235 }
236 
237 
238 bool YQPkgTextDialog::confirmText( QWidget * parent,
239  const QString & text,
240  const QString & acceptButtonLabel,
241  const QString & rejectButtonLabel )
242 {
243  YQPkgTextDialog * dia = new YQPkgTextDialog( text,
244  parent,
245  acceptButtonLabel,
246  rejectButtonLabel );
247  Q_CHECK_PTR( dia );
248  bool confirmed = ( dia->exec() == QDialog::Accepted );
249  delete dia;
250 
251  return confirmed;
252 }
253 
254 
255 bool YQPkgTextDialog::confirmText( QWidget * parent, const QString & text )
256 {
257  // Translators: "Accept" here refers to licenses or similar
258  return confirmText( parent, text, _( "&Accept" ), _( "&Cancel" ) );
259 }
260 
261 
262 bool YQPkgTextDialog::confirmText( QWidget * parent, const char * text )
263 {
264  return confirmText( parent, QString( text ) );
265 }
266 
267 
268 bool YQPkgTextDialog::confirmText( QWidget * parent,
269  ZyppSel selectable,
270  const string & text )
271 {
272  return confirmText( parent, htmlHeading( selectable ) + htmlParagraphs( text ) );
273 }
274 
275 
276 
277 
278 
279 QString
280 YQPkgTextDialog::htmlEscape( const QString & plainText )
281 {
282  QString html = plainText;
283  // yuiDebug() << "Escaping \"" << plainText << "\"" << endl;
284 
285  html.replace( QRegExp( "&" ), "&amp;" );
286  html.replace( QRegExp( "<" ), "&lt;" );
287  html.replace( QRegExp( ">" ), "&gt;" );
288 
289  return html;
290 }
291 
292 
293 
294 QString
295 YQPkgTextDialog::htmlParagraphs( const string & rawText )
296 {
297  QString text = fromUTF8( rawText );
298 
299  if ( text.contains( "<!-- DT:Rich -->" ) ) // Special doctype for preformatted HTML
300  return text;
301 
302  text = htmlEscape( text ); // Escape '<', '>', '&'
303  text.replace( "\n\n", "</p><p>" ); // Empty lines mean new paragraph
304  text.prepend( "<p>" );
305  text.append ( "</p>" );
306 
307  return text;
308 }
309 
310 
311 
312 QString
313 YQPkgTextDialog::htmlHeading( const QString & text )
314 {
315  QString html =
316  "<table><tr><td><b>"
317  + text
318  + "</b></td></tr></table><br>";
319 
320  return html;
321 }
322 
323 
324 QString
325 YQPkgTextDialog::htmlHeading( ZyppSel selectable )
326 {
327  if ( ! selectable )
328  return "";
329 
330  ZyppObj zyppObj = selectable->theObj();
331 
332  if ( ! zyppObj )
333  return "";
334 
335  QString summary = fromUTF8( zyppObj->summary() );
336 
337  QString html =
338  "<table><tr><td><b>"
339  + fromUTF8( zyppObj->name() )
340  + "</b>";
341 
342  if ( ! summary.isEmpty() )
343  html += " - " + summary;
344 
345  html += "</td></tr></table><br>";
346 
347  return html;
348 }
349 
350 
351 
352 
353 #include "YQPkgTextDialog.moc"
virtual ~YQPkgTextDialog()
Destructor.
bool eventFilter(QObject *obj, QEvent *ev)
Grab [Return] press events and close dialog.
static bool confirmText(QWidget *parent, const QString &text, const QString &acceptButtonLabel, const QString &rejectButtonLabel)
Let the user confirm a text.
void buildDialog(const QString &text, QWidget *parent, const QString &acceptButtonLabel, const QString &rejectButtonLabel="")
Create the dialog.
static void showText(QWidget *parent, const QString &text)
Show a text and wait until the user confirmed with &#39;OK&#39;.
static QString htmlParagraphs(const string &rawText)
Simple HTML formatting: Wrap paragraphs in.
YQPkgTextDialog(const QString &text, QWidget *parent, const QString &acceptButtonLabel, const QString &rejectButtonLabel)
Constructor.
static QString htmlHeading(const QString &text)
Returns a uniform heading in HTML format.
void setText(const QString &text)
Set the text contents.
Dialog that shows a scrolled (HTML) text.
static QString htmlEscape(const QString &plainText)
Escapes characters special to HTML in a ( plain text ) string, such as: &#39;<&#39; -> &#39;<&#39; &#39;>&#39; -> &#39;>&#39; &#39;&&#39; -> ...
virtual QSize sizeHint() const
Reimplemented from QWidget: Reserve a reasonable amount of space.