]> sigrok.org Git - pulseview.git/blob - pv/widgets/popup.hpp
Session: Fix issue #67 by improving error handling
[pulseview.git] / pv / widgets / popup.hpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef PULSEVIEW_PV_WIDGETS_POPUP_HPP
21 #define PULSEVIEW_PV_WIDGETS_POPUP_HPP
22
23 #include <QScrollArea>
24 #include <QWidget>
25
26 namespace pv {
27 namespace widgets {
28
29
30 // A regular QScrollArea has a fixed size and provides scroll bars when the
31 // content can't be shown in its entirety. However, we want no horizontal
32 // scroll bar and want the scroll area to adjust its width to fit the content
33 // instead.
34 // Inspired by https://stackoverflow.com/questions/21253755/qscrollarea-with-dynamically-changing-contents?answertab=votes#tab-top
35 class QWidthAdjustingScrollArea : public QScrollArea
36 {
37         Q_OBJECT
38
39 public:
40         QWidthAdjustingScrollArea(QWidget* parent = nullptr);
41         void setWidget(QWidget* w);
42         bool eventFilter(QObject* obj, QEvent* ev);
43 };
44
45
46 class Popup : public QWidget
47 {
48         Q_OBJECT
49
50 public:
51         enum Position
52         {
53                 Right,
54                 Top,
55                 Left,
56                 Bottom
57         };
58
59 private:
60         static const unsigned int ArrowLength;
61         static const unsigned int ArrowOverlap;
62         static const unsigned int MarginWidth;
63
64 public:
65         Popup(QWidget *parent);
66
67         const QPoint& point() const;
68         Position position() const;
69
70         void set_position(const QPoint point, Position pos);
71
72         bool eventFilter(QObject *obj, QEvent *event);
73
74         virtual void show();
75
76 private:
77         bool space_for_arrow() const;
78
79         QPolygon arrow_polygon() const;
80
81         QRegion arrow_region() const;
82
83         QRect bubble_rect() const;
84
85         QRegion bubble_region() const;
86
87         QRegion popup_region() const;
88
89         void reposition_widget();
90
91 private:
92         void closeEvent(QCloseEvent*);
93
94         void paintEvent(QPaintEvent*);
95
96         void resizeEvent(QResizeEvent*);
97
98         void mouseReleaseEvent(QMouseEvent *event);
99
100 protected:
101         void showEvent(QShowEvent *);
102
103 Q_SIGNALS:
104         void closed();
105
106 private:
107         QPoint point_;
108         Position pos_;
109 };
110
111 } // namespace widgets
112 } // namespace pv
113
114 #endif // PULSEVIEW_PV_WIDGETS_POPUP_HPP