X-Git-Url: http://git.code-monkey.de/?p=pulseview.git;a=blobdiff_plain;f=pv%2Fwidgets%2Fpopup.cpp;h=52701380787e8845dd7830955e526dabdd86a93b;hp=f89e38a6b1b84e3fdc9b923060f31688277c87f1;hb=1db1bdd6079178e218b3b1b0bd3e30efdf3e1690;hpb=6e3f046e779b939efebdfa4d9e68fe28d9beee59 diff --git a/pv/widgets/popup.cpp b/pv/widgets/popup.cpp index f89e38a..5270138 100644 --- a/pv/widgets/popup.cpp +++ b/pv/widgets/popup.cpp @@ -20,18 +20,24 @@ #include +#include + #include +#include +#include +#include #include "popup.h" -using namespace std; +using std::max; +using std::min; namespace pv { namespace widgets { -const unsigned int Popup::ArrowLength = 15; +const unsigned int Popup::ArrowLength = 10; const unsigned int Popup::ArrowOverlap = 3; -const unsigned int Popup::MarginWidth = 10; +const unsigned int Popup::MarginWidth = 6; Popup::Popup(QWidget *parent) : QWidget(parent, Qt::Popup | Qt::FramelessWindowHint), @@ -62,6 +68,76 @@ void Popup::set_position(const QPoint point, Position pos) } +bool Popup::eventFilter(QObject *obj, QEvent *evt) +{ + QKeyEvent *keyEvent; + + (void)obj; + + if (evt->type() == QEvent::KeyPress) { + keyEvent = static_cast(evt); + if (keyEvent->key() == Qt::Key_Enter || + keyEvent->key() == Qt::Key_Return) { + close(); + return true; + } + } + + return false; +} + +void Popup::show() +{ + QLineEdit* le; + + QWidget::show(); + + // We want to close the popup when the Enter key was + // pressed and the first editable widget had focus. + if ((le = this->findChild())) { + + // For combo boxes we need to hook into the parent of + // the line edit (i.e. the QComboBox). For edit boxes + // we hook into the widget directly. + if (le->parent()->metaObject()->className() == + this->metaObject()->className()) + le->installEventFilter(this); + else + le->parent()->installEventFilter(this); + + le->selectAll(); + le->setFocus(); + } +} + +bool Popup::space_for_arrow() const +{ + // Check if there is room for the arrow + switch (_pos) { + case Right: + if (_point.x() > x()) + return false; + return true; + + case Bottom: + if (_point.y() > y()) + return false; + return true; + + case Left: + if (_point.x() < (x() + width())) + return false; + return true; + + case Top: + if (_point.y() < (y() + height())) + return false; + return true; + } + + return true; +} + QPolygon Popup::arrow_polygon() const { QPolygon poly; @@ -142,13 +218,19 @@ QRegion Popup::bubble_region() const QRegion Popup::popup_region() const { - return arrow_region().united(bubble_region()); + if (space_for_arrow()) + return arrow_region().united(bubble_region()); + else + return bubble_region(); } void Popup::reposition_widget() { QPoint o; + const QRect screen_rect = QApplication::desktop()->availableGeometry( + QApplication::desktop()->screenNumber(_point)); + if (_pos == Right || _pos == Left) o.ry() = -height() / 2; else @@ -159,7 +241,16 @@ void Popup::reposition_widget() else if(_pos == Top) o.ry() = -height(); - move(_point + o); + o += _point; + move(max(min(o.x(), screen_rect.right() - width()), + screen_rect.left()), + max(min(o.y(), screen_rect.bottom() - height()), + screen_rect.top())); +} + +void Popup::closeEvent(QCloseEvent*) +{ + closed(); } void Popup::paintEvent(QPaintEvent*) @@ -170,6 +261,7 @@ void Popup::paintEvent(QPaintEvent*) const QColor outline_color(QApplication::palette().color( QPalette::Dark)); + // Draw the bubble const QRegion b = bubble_region(); const QRegion bubble_outline = QRegion(rect()).subtracted( b.translated(1, 0).intersected(b.translated(0, 1).intersected( @@ -179,6 +271,10 @@ void Popup::paintEvent(QPaintEvent*) painter.setBrush(QApplication::palette().brush(QPalette::Window)); painter.drawRect(rect()); + // Draw the arrow + if (!space_for_arrow()) + return; + const QPoint ArrowOffsets[] = { QPoint(1, 0), QPoint(0, -1), QPoint(-1, 0), QPoint(0, 1)}; @@ -198,11 +294,6 @@ void Popup::resizeEvent(QResizeEvent*) setMask(popup_region()); } -void Popup::showEvent(QShowEvent*) -{ - reposition_widget(); -} - void Popup::mouseReleaseEvent(QMouseEvent *e) { assert(e); @@ -213,6 +304,11 @@ void Popup::mouseReleaseEvent(QMouseEvent *e) close(); } +void Popup::showEvent(QShowEvent*) +{ + reposition_widget(); +} + } // namespace widgets } // namespace pv