MessageWindow.h 2.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
/**
 * @file MessageWindow.h
 * @brief Message Window.
 * @see MessageWindow
 * @author Micha? Policht
 */

#ifndef MESSAGEWINDOW_H_
#define MESSAGEWINDOW_H_

#include <QDockWidget>
#include <QTextEdit>
#include <QEvent>

/**
 * Message Window. Handling errors and other messages.
 */
class MessageWindow: public QDockWidget
{
    Q_OBJECT

    QTextEdit msgTextEdit;              ///< Main widget.
    static MessageWindow *MsgHandler;   ///< Set in constructor.
    static const char *WINDOW_TITLE;    ///< Window title.

private:
    static QString QtMsgToQString(QtMsgType type, const char *msg);

protected:
    /**
         * Handle custom events. MessageWindow hadles custom events listed in
         * EventType enum.
         */
    virtual void customEvent(QEvent* event);

public:
    enum EventType {MessageEventType = QEvent::User};    ///< Custom event types.

    /**
         * Default constructor.
         *     @param parent parent widget.
         *     @param flags widget flags.
         */
    MessageWindow(QWidget* parent = 0, Qt::WindowFlags flags = 0);

    /**
         * Append message wrapper. Since ISO forbids casting member functions
         * to C functions, wrapper is needed to use this class as QtMsgHandler.
         * This method is thread-safe but not reentrant.
         *     @param type message type.
         *     @param msg message string.
         */
    static void AppendMsgWrapper(QtMsgType type, const char *msg);
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
    static void AppendMsgWrapper(QtMsgType type, const QMessageLogContext &context, const QString &msg);
#endif
    /**
         * Post message event to the main event loop. This function encapsulates
         * message into MessageEvent object and passes it to the main event loop.
         *     @param type message type.
         *     @param msg message string.
         */
    void postMsgEvent(QtMsgType type, const char *msg);

};


/**
 * Message Event. Custom event used by @ref MessageWindow to provide multi-threaded
 * access. Encapsulates message inside @a msg variable.
 */
class MessageEvent: public QEvent
{
public:
    QString msg;    ///< Message string.

    /**
         * Contructor.
         *     @param msg message to post.
         */
    MessageEvent(QString &msg);
};

#endif /*MESSAGEWINDOW_H_*/