1 /**
2  * UGENE - Integrated Bioinformatics Tools.
3  * Copyright (C) 2008-2021 UniPro <ugene@unipro.ru>
4  * http://ugene.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  */
21 
22 #ifndef _NOTIFICATION_H_
23 #define _NOTIFICATION_H_
24 
25 #include <QAction>
26 #include <QCheckBox>
27 #include <QDialog>
28 #include <QHBoxLayout>
29 #include <QHelpEvent>
30 #include <QLabel>
31 #include <QLayout>
32 #include <QMainWindow>
33 #include <QMouseEvent>
34 #include <QPoint>
35 #include <QPushButton>
36 #include <QScrollArea>
37 #include <QTextEdit>
38 #include <QTimer>
39 #include <QToolTip>
40 #include <QVBoxLayout>
41 
42 #include <U2Core/U2OpStatusUtils.h>
43 #include <U2Core/global.h>
44 
45 #include "NotificationWidget.h"
46 #include "NotificationsTypes.h"
47 
48 namespace U2 {
49 
50 #define MAX_NOTIFICATION 100
51 
52 class U2GUI_EXPORT Notification : public QLabel {
53     Q_OBJECT
54 
55 public:
56     Notification(const QString &message, NotificationType _type, QAction *_action = 0);
57 
58     void showNotification(int x, int y);
59 
60     const QString &getText() const;
61 
62     NotificationType getType() const;
63 
64     bool eventFilter(QObject *watched, QEvent *event) override;
65 
66     void incrementCounter();
67 
68     /**
69      * Switches notification to the embedded visual state:
70      * In the embedded state notification is shown inside NotificationWidget.
71      */
72     void switchEmbeddedVisualState();
73 
74 private slots:
75     void sl_timeout();
76 
77 private:
78     void hideNotification();
79     void updateStyle(bool isHovered);
80     void updateCloseButtonStyle(bool isHovered);
81 
82 signals:
83     /** The signal is emitted after the notification is hidden. */
84     void si_notificationHideEvent();
85     void si_delete();
86 
87 protected:
88     bool event(QEvent *e) override;
89     void mousePressEvent(QMouseEvent *ev) override;
90 
91 private:
92     QAction *action;
93     QLabel *close;
94     QTimer timer;
95 
96     QString text;
97     NotificationType type;
98     int timeCounter = 0;
99     // counter for duplicate notifications
100     int counter = 0;
101 };
102 
103 class U2GUI_EXPORT NotificationStack : public QObject {
104     Q_OBJECT
105 
106 public:
107     NotificationStack(QObject *o = nullptr);
108     ~NotificationStack();
109 
110     void addNotification(Notification *t);
111     int count() const;
112     Notification *getNotification(int row) const;
113     QList<Notification *> getItems() const;
114     void showStack();
115     bool hasError() const;
116     void setFixed(bool val);
117 
118     /** Adds instance of Notification to the notification stack. */
119     static void addNotification(const QString &message, NotificationType type, QAction *action = 0);
120 
121 private slots:
122     /** Called when notification is hidden. The called is the 'Notification' instance. */
123     void sl_onNotificationHidden();
124     void sl_delete();
125 
126 signals:
127     void si_changed();
128 
129 private:
130     static QPoint getBottomRightOfMainWindow();  // because of Mac's strange behavior
131 
132     // Adds notification as a child to notification widget
133     void addToNotificationWidget(Notification *n);
134 
135     NotificationWidget *notificationWidget;
136 
137     QList<Notification *> notifications;
138     QList<Notification *> notificationsOnScreen;
139     int notificationPosition;
140     int notificationNumber;
141 };
142 
143 /**
144     Used to handle important errors that needs to be reported to user.
145     Dumps error to coreLog while showing notification to user.
146     LogLevel and NotificationType can be passed as params.
147     Defaults are LogLevel_ERROR and Error_Not, respectively
148 */
149 class U2GUI_EXPORT U2OpStatus2Notification : public U2OpStatus2Log {
150 public:
151     U2OpStatus2Notification(NotificationType type = Error_Not, LogLevel level = LogLevel_ERROR)
U2OpStatus2Log(level)152         : U2OpStatus2Log(level), notificationType(type) {
153     }
154 
setError(const QString & error)155     void setError(const QString &error) override {
156         U2OpStatus2Log::setError(error);
157         NotificationStack::addNotification(error, notificationType);
158     }
159 
160 private:
161     NotificationType notificationType;
162 };
163 
164 }  // namespace U2
165 
166 #endif
167