]> sigrok.org Git - pulseview.git/blobdiff - pv/widgets/popup.cpp
Session: Fix issue #67 by improving error handling
[pulseview.git] / pv / widgets / popup.cpp
index de2d1b57010643228bb28c5ba713eb25723c55eb..bcfd8753c6b6dbe2c8e6da5a8032d75057b8bc50 100644 (file)
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #include <algorithm>
+#include <cassert>
 
-#include <assert.h>
-
-#include <QtGui>
 #include <QApplication>
+#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
+#include <QScreen>
+#else
 #include <QDesktopWidget>
+#endif
 #include <QLineEdit>
+#include <QScrollBar>
+#include <QStyle>
+#include <QtGui>
 
 #include "popup.hpp"
 
@@ -39,6 +43,33 @@ const unsigned int Popup::ArrowLength = 10;
 const unsigned int Popup::ArrowOverlap = 3;
 const unsigned int Popup::MarginWidth = 6;
 
+
+QWidthAdjustingScrollArea::QWidthAdjustingScrollArea(QWidget* parent) :
+       QScrollArea(parent)
+{
+}
+
+void QWidthAdjustingScrollArea::setWidget(QWidget* w)
+{
+       QScrollArea::setWidget(w);
+       // It happens that QScrollArea already filters widget events,
+       // but that's an implementation detail that we shouldn't rely on.
+       w->installEventFilter(this);
+}
+
+bool QWidthAdjustingScrollArea::eventFilter(QObject* obj, QEvent* ev)
+{
+       if (obj == widget() && ev->type() == QEvent::Resize) {
+               if (widget()->height() > height())
+                       setMinimumWidth(widget()->width() + qApp->style()->pixelMetric(QStyle::PM_ScrollBarExtent));
+               else
+                       setMinimumWidth(widget()->width());
+       }
+
+       return QScrollArea::eventFilter(obj, ev);
+}
+
+
 Popup::Popup(QWidget *parent) :
        QWidget(parent, Qt::Popup | Qt::FramelessWindowHint),
        point_(),
@@ -65,17 +96,16 @@ void Popup::set_position(const QPoint point, Position pos)
                MarginWidth + ((pos == Bottom) ? ArrowLength : 0),
                MarginWidth + ((pos == Left) ? ArrowLength : 0),
                MarginWidth + ((pos == Top) ? ArrowLength : 0));
-
 }
 
-bool Popup::eventFilter(QObject *obj, QEvent *evt)
+bool Popup::eventFilter(QObject *obj, QEvent *event)
 {
        QKeyEvent *keyEvent;
 
        (void)obj;
 
-       if (evt->type() == QEvent::KeyPress) {
-               keyEvent = static_cast<QKeyEvent*>(evt);
+       if (event->type() == QEvent::KeyPress) {
+               keyEvent = static_cast<QKeyEvent*>(event);
                if (keyEvent->key() == Qt::Key_Enter ||
                    keyEvent->key() == Qt::Key_Return) {
                        close();
@@ -122,7 +152,7 @@ bool Popup::space_for_arrow() const
        case Bottom:
                if (point_.y() > y())
                        return false;
-               return true;            
+               return true;
 
        case Left:
                if (point_.x() < (x() + width()))
@@ -143,10 +173,9 @@ QPolygon Popup::arrow_polygon() const
        QPolygon poly;
 
        const QPoint p = mapFromGlobal(point_);
-       const int l = ArrowLength + ArrowOverlap; 
+       const int l = ArrowLength + ArrowOverlap;
 
-       switch (pos_)
-       {
+       switch (pos_) {
        case Right:
                poly << QPoint(p.x() + l, p.y() - l);
                break;
@@ -163,8 +192,7 @@ QPolygon Popup::arrow_polygon() const
 
        poly << p;
 
-       switch (pos_)
-       {
+       switch (pos_) {
        case Right:
        case Bottom:
                poly << QPoint(p.x() + l, p.y() + l);
@@ -173,7 +201,7 @@ QPolygon Popup::arrow_polygon() const
        case Left:
                poly << QPoint(p.x() - l, p.y() + l);
                break;
-               
+
        case Top:
                poly << QPoint(p.x() + l, p.y() - l);
                break;
@@ -228,8 +256,12 @@ void Popup::reposition_widget()
 {
        QPoint o;
 
+#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
+       const QRect screen_rect = QApplication::screenAt(point_)->availableGeometry();
+#else
        const QRect screen_rect = QApplication::desktop()->availableGeometry(
                QApplication::desktop()->screenNumber(point_));
+#endif
 
        if (pos_ == Right || pos_ == Left)
                o.ry() = -height() / 2;
@@ -294,13 +326,13 @@ void Popup::resizeEvent(QResizeEvent*)
        setMask(popup_region());
 }
 
-void Popup::mouseReleaseEvent(QMouseEvent *e)
+void Popup::mouseReleaseEvent(QMouseEvent *event)
 {
-       assert(e);
+       assert(event);
 
        // We need our own out-of-bounds click handler because QWidget counts
        // the drop-shadow region as inside the widget
-       if (!bubble_rect().contains(e->pos()))
+       if (!bubble_rect().contains(event->pos()))
                close();
 }