Qt 4.8
qerrormessage.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qerrormessage.h"
43 
44 #ifndef QT_NO_ERRORMESSAGE
45 
46 #include "qapplication.h"
47 #include "qcheckbox.h"
48 #include "qlabel.h"
49 #include "qlayout.h"
50 #include "qmessagebox.h"
51 #include "qpushbutton.h"
52 #include "qstringlist.h"
53 #include "qtextedit.h"
54 #include "qdialog_p.h"
55 #include "qpixmap.h"
56 #include "qmetaobject.h"
57 #include "qthread.h"
58 #include "qqueue.h"
59 #include "qset.h"
60 
61 #include <stdio.h>
62 #include <stdlib.h>
63 
64 #ifdef Q_WS_WINCE
65 extern bool qt_wince_is_mobile(); //defined in qguifunctions_wince.cpp
66 extern bool qt_wince_is_high_dpi(); //defined in qguifunctions_wince.cpp
67 
68 #include "qguifunctions_wince.h"
69 #endif
70 
71 #if defined(QT_SOFTKEYS_ENABLED)
72 #include <qaction.h>
73 #endif
74 #ifdef Q_WS_S60
75 #include "private/qt_s60_p.h"
76 #endif
77 
79 
81 {
83 public:
88 #ifdef QT_SOFTKEYS_ENABLED
89  QAction *okAction;
90 #endif
96 
97  bool nextPending();
98  void retranslateStrings();
99 };
100 
102 {
103 public:
105  : QTextEdit(parent) { setReadOnly(true); }
106 
107  virtual QSize minimumSizeHint() const;
108  virtual QSize sizeHint() const;
109 };
110 
112 {
113 #ifdef Q_WS_WINCE
114  if (qt_wince_is_mobile())
115  if (qt_wince_is_high_dpi())
116  return QSize(200, 200);
117  else
118  return QSize(100, 100);
119  else
120  return QSize(70, 70);
121 #else
122  return QSize(50, 50);
123 #endif
124 }
125 
127 {
128 #ifdef Q_WS_WINCE
129  if (qt_wince_is_mobile())
130  if (qt_wince_is_high_dpi())
131  return QSize(400, 200);
132  else
133  return QSize(320, 120);
134  else
135  return QSize(300, 100);
136 #else
137 
138 #ifdef Q_WS_S60
139  const int smallerDimension = qMin(S60->screenHeightInPixels, S60->screenWidthInPixels);
140  // In S60 layout data, error messages seem to be one third of the screen height (in portrait) minus two.
141  return QSize(smallerDimension, smallerDimension/3-2);
142 #else
143  return QSize(250, 75);
144 #endif //Q_WS_S60
145 #endif //Q_WS_WINCE
146 }
147 
190 
191 static void deleteStaticcQErrorMessage() // post-routine
192 {
193  if (qtMessageHandler) {
194  delete qtMessageHandler;
195  qtMessageHandler = 0;
196  }
197 }
198 
199 static bool metFatal = false;
200 
201 static void jump(QtMsgType t, const char * m)
202 {
203  if (!qtMessageHandler)
204  return;
205 
206  QString rich;
207 
208  switch (t) {
209  case QtDebugMsg:
210  default:
211  rich = QErrorMessage::tr("Debug Message:");
212  break;
213  case QtWarningMsg:
214  rich = QErrorMessage::tr("Warning:");
215  break;
216  case QtFatalMsg:
217  rich = QErrorMessage::tr("Fatal Error:");
218  }
219  rich = QString::fromLatin1("<p><b>%1</b></p>").arg(rich);
221 
222  // ### work around text engine quirk
223  if (rich.endsWith(QLatin1String("</p>")))
224  rich.chop(4);
225 
226  if (!metFatal) {
227  if (QThread::currentThread() == qApp->thread()) {
228  qtMessageHandler->showMessage(rich);
229  } else {
230  QMetaObject::invokeMethod(qtMessageHandler,
231  "showMessage",
233  Q_ARG(QString, rich));
234  }
235  metFatal = (t == QtFatalMsg);
236  }
237 }
238 
239 
246  : QDialog(*new QErrorMessagePrivate, parent)
247 {
249  QGridLayout * grid = new QGridLayout(this);
250  d->icon = new QLabel(this);
251 #ifndef QT_NO_MESSAGEBOX
253  d->icon->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
254 #endif
255 #ifdef Q_WS_S60
256  //In Symbian, messagebox icons are in LtR UIs on right. Thus, layout needs to switch icon and text columns.
257  const int preferredIconColumn = (QApplication::layoutDirection() == Qt::LeftToRight) ? 1 : 0;
258  const int preferredTextColumn = (QApplication::layoutDirection() == Qt::LeftToRight) ? 0 : 1;
259 #else
260  const int preferredIconColumn = 0;
261  const int preferredTextColumn = 1;
262 #endif
263  grid->addWidget(d->icon, 0, preferredIconColumn, Qt::AlignTop);
264  d->errors = new QErrorMessageTextView(this);
265  grid->addWidget(d->errors, 0, preferredTextColumn);
266  d->again = new QCheckBox(this);
267  d->again->setChecked(true);
268  grid->addWidget(d->again, 1, preferredTextColumn, Qt::AlignTop);
269  d->ok = new QPushButton(this);
270 #ifdef QT_SOFTKEYS_ENABLED
271  d->okAction = new QAction(d->ok);
272  d->okAction->setSoftKeyRole(QAction::PositiveSoftKey);
273  connect(d->okAction, SIGNAL(triggered()), this, SLOT(accept()));
274  addAction(d->okAction);
275 #endif
276 
277 
278 #if defined(Q_WS_WINCE) || defined(Q_WS_S60)
279  d->ok->setFixedSize(0,0);
280 #endif
281  connect(d->ok, SIGNAL(clicked()), this, SLOT(accept()));
282  d->ok->setFocus();
283  grid->addWidget(d->ok, 2, 0, 1, 2, Qt::AlignCenter);
284  grid->setColumnStretch(preferredTextColumn, 42);
285  grid->setRowStretch(0, 42);
286  d->retranslateStrings();
287 }
288 
289 
295 {
296  if (this == qtMessageHandler) {
297  qtMessageHandler = 0;
299  // in case someone else has later stuck in another...
300  if (tmp != jump)
301  qInstallMsgHandler(tmp);
302  }
303 }
304 
305 
309 {
311  if (!d->again->isChecked() && !d->currentMessage.isEmpty() && d->currentType.isEmpty()) {
312  d->doNotShow.insert(d->currentMessage);
313  }
314  if (!d->again->isChecked() && !d->currentType.isEmpty()) {
315  d->doNotShowType.insert(d->currentType);
316  }
317  d->currentMessage.clear();
318  d->currentType.clear();
319  if (!d->nextPending()) {
320  QDialog::done(a);
321  if (this == qtMessageHandler && metFatal)
322  exit(1);
323  }
324 }
325 
326 
334 {
335  if (!qtMessageHandler) {
336  qtMessageHandler = new QErrorMessage(0);
338  qtMessageHandler->setWindowTitle(QApplication::applicationName());
340  }
341  return qtMessageHandler;
342 }
343 
344 
348 {
349  while (!pending.isEmpty()) {
350  QPair<QString,QString> pendingMessage = pending.dequeue();
351  QString message = pendingMessage.first;
352  QString type = pendingMessage.second;
353  if (!message.isEmpty() && ((type.isEmpty() && !doNotShow.contains(message)) || (!type.isEmpty() && !doNotShowType.contains(type)))) {
354 #ifndef QT_NO_TEXTHTMLPARSER
355  errors->setHtml(message);
356 #else
357  errors->setPlainText(message);
358 #endif
359  currentMessage = message;
360  currentType = type;
361  return true;
362  }
363  }
364  return false;
365 }
366 
367 
378 {
380  if (d->doNotShow.contains(message))
381  return;
382  d->pending.enqueue(qMakePair(message,QString()));
383  if (!isVisible() && d->nextPending())
384  show();
385 }
386 
404 void QErrorMessage::showMessage(const QString &message, const QString &type)
405 {
407  if (d->doNotShow.contains(message) && d->doNotShowType.contains(type))
408  return;
409  d->pending.push_back(qMakePair(message,type));
410  if (!isVisible() && d->nextPending())
411  show();
412 }
413 
418 {
420  if (e->type() == QEvent::LanguageChange) {
421  d->retranslateStrings();
422  }
424 }
425 
427 {
428  again->setText(QErrorMessage::tr("&Show this message again"));
429  ok->setText(QErrorMessage::tr("&OK"));
430 #ifdef QT_SOFTKEYS_ENABLED
431  okAction->setText(ok->text());
432 #endif
433 }
434 
445 
446 #endif // QT_NO_ERRORMESSAGE
double d
Definition: qnumeric_p.h:62
static Qt::LayoutDirection layoutDirection()
static void jump(QtMsgType t, const char *m)
static QPixmap standardIcon(Icon icon)
Returns the pixmap used for a standard icon.
int type
Definition: qmetatype.cpp:239
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
The QQueue class is a generic container that provides a queue.
Definition: qcontainerfwd.h:61
Q_GUI_EXPORT QString convertFromPlainText(const QString &plain, WhiteSpaceMode mode=WhiteSpacePre)
Converts the plain text string plain to an HTML-formatted paragraph while preserving most of its look...
void qAddPostRoutine(QtCleanUpFunction p)
The QDialog class is the base class of dialog windows.
Definition: qdialog.h:56
bool isVisible() const
Definition: qwidget.h:1005
static void deleteStaticcQErrorMessage()
void chop(int n)
Removes n characters from the end of the string.
Definition: qstring.cpp:4623
#define SLOT(a)
Definition: qobjectdefs.h:226
QSet< QString > doNotShow
T1 first
Definition: qpair.h:65
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
virtual void accept()
Hides the modal dialog and sets the result code to Accepted.
Definition: qdialog.cpp:639
T2 second
Definition: qpair.h:66
~QErrorMessage()
Destroys the error message dialog.
The QPushButton widget provides a command button.
Definition: qpushbutton.h:57
#define Q_ARG(type, data)
Definition: qobjectdefs.h:246
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QQueue< QPair< QString, QString > > pending
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
void done(int)
Reimplemented Function
long ASN1_INTEGER_get ASN1_INTEGER * a
void(* QtMsgHandler)(QtMsgType, const char *)
Definition: qglobal.h:1885
virtual QSize minimumSizeHint() const
QErrorMessage(QWidget *parent=0)
Constructs and installs an error handler window with the given parent.
The QString class provides a Unicode character string.
Definition: qstring.h:83
QtMsgType
This enum describes the messages that can be sent to a message handler (QtMsgHandler).
Definition: qglobal.h:1881
#define Q_D(Class)
Definition: qglobal.h:2482
void addAction(QAction *action)
Appends the action action to this widget&#39;s list of actions.
Definition: qwidget.cpp:3317
#define SIGNAL(a)
Definition: qobjectdefs.h:227
bool qt_wince_is_high_dpi()
void setWindowTitle(const QString &)
Definition: qwidget.cpp:6312
static QThread * currentThread()
Returns a pointer to a QThread which manages the currently executing thread.
Definition: qthread.cpp:419
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static bool metFatal
static QErrorMessage * qtHandler()
Returns a pointer to a QErrorMessage object that outputs the default Qt messages. ...
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
#define qApp
static QErrorMessage * qtMessageHandler
virtual void changeEvent(QEvent *)
This event handler can be reimplemented to handle state changes.
Definition: qwidget.cpp:9170
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
void show()
Shows the widget and its child widgets.
virtual QSize sizeHint() const
void showMessage(const QString &message)
Shows the given message, message, and returns immediately.
virtual void done(int)
Closes the dialog and sets its result code to r.
Definition: qdialog.cpp:617
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
QErrorMessageTextView(QWidget *parent)
void setColumnStretch(int column, int stretch)
Sets the stretch factor of column column to stretch.
QtMsgHandler qInstallMsgHandler(QtMsgHandler h)
Definition: qglobal.cpp:2698
friend class QPushButton
Definition: qdialog.h:59
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
void changeEvent(QEvent *e)
Reimplemented Function
Q_OUTOFLINE_TEMPLATE QPair< T1, T2 > qMakePair(const T1 &x, const T2 &y)
Definition: qpair.h:102
The QGridLayout class lays out widgets in a grid.
Definition: qgridlayout.h:60
The QLabel widget provides a text or image display.
Definition: qlabel.h:55
bool qt_wince_is_mobile()
The QErrorMessage class provides an error message display dialog.
Definition: qerrormessage.h:57
QObject * parent
Definition: qobject.h:92
static bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType, QGenericReturnArgument ret, QGenericArgument val0=QGenericArgument(0), QGenericArgument val1=QGenericArgument(), QGenericArgument val2=QGenericArgument(), QGenericArgument val3=QGenericArgument(), QGenericArgument val4=QGenericArgument(), QGenericArgument val5=QGenericArgument(), QGenericArgument val6=QGenericArgument(), QGenericArgument val7=QGenericArgument(), QGenericArgument val8=QGenericArgument(), QGenericArgument val9=QGenericArgument())
Invokes the member (a signal or a slot name) on the object obj.
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
void setRowStretch(int row, int stretch)
Sets the stretch factor of row row to stretch.
QSet< QString > doNotShowType
void addWidget(QWidget *w)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qgridlayout.h:116
static QString applicationName()
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition: qstring.cpp:3796
The QCheckBox widget provides a checkbox with a text label.
Definition: qcheckbox.h:56
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64
The QTextEdit class provides a widget that is used to edit and display both plain and rich text...
Definition: qtextedit.h:70