From: Joel Holdsworth Date: Sat, 10 Jan 2015 17:18:32 +0000 (+0000) Subject: MainWindow: Hide the main menu unless Altt is pressed X-Git-Tag: pulseview-0.3.0~262 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=1c4c23b87cde3778792caa1400030075b0504129;p=pulseview.git MainWindow: Hide the main menu unless Altt is pressed --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 3e621a1f..3f9009ab 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -190,6 +190,7 @@ set(pulseview_SOURCES pv/widgets/colourbutton.cpp pv/widgets/colourpopup.cpp pv/widgets/devicetoolbutton.cpp + pv/widgets/hidingmenubar.cpp pv/widgets/popup.cpp pv/widgets/popuptoolbutton.cpp pv/widgets/sweeptimingwidget.cpp @@ -233,6 +234,7 @@ set(pulseview_HEADERS pv/widgets/colourbutton.hpp pv/widgets/colourpopup.hpp pv/widgets/devicetoolbutton.hpp + pv/widgets/hidingmenubar.hpp pv/widgets/popup.hpp pv/widgets/popuptoolbutton.hpp pv/widgets/sweeptimingwidget.hpp diff --git a/pv/mainwindow.cpp b/pv/mainwindow.cpp index dc9e7de5..21b66b53 100644 --- a/pv/mainwindow.cpp +++ b/pv/mainwindow.cpp @@ -52,6 +52,7 @@ #ifdef ENABLE_DECODE #include "widgets/decodermenu.hpp" #endif +#include "widgets/hidingmenubar.hpp" #include #include @@ -211,8 +212,8 @@ void MainWindow::setup_ui() vertical_layout_->addWidget(view_); // Setup the menu bar - QMenuBar *const menu_bar = new QMenuBar(this); - menu_bar->setGeometry(QRect(0, 0, 400, 25)); + pv::widgets::HidingMenuBar *const menu_bar = + new pv::widgets::HidingMenuBar(this); // File Menu QMenu *const menu_file = new QMenu; @@ -276,7 +277,7 @@ void MainWindow::setup_ui() QString::fromUtf8("actionViewZoomFit")); menu_view->addAction(action_view_zoom_fit_); - action_view_zoom_one_to_one_->setText(tr("Zoom to &One-to-One")); + action_view_zoom_one_to_one_->setText(tr("Zoom to O&ne-to-One")); action_view_zoom_one_to_one_->setIcon(QIcon::fromTheme("zoom-original", QIcon(":/icons/zoom-original.png"))); action_view_zoom_one_to_one_->setShortcut(QKeySequence(Qt::Key_O)); @@ -447,6 +448,15 @@ void MainWindow::closeEvent(QCloseEvent *event) event->accept(); } +void MainWindow::keyReleaseEvent(QKeyEvent *event) +{ + if (event->key() == Qt::Key_Alt) { + menuBar()->setHidden(!menuBar()->isHidden()); + menuBar()->setFocus(); + } + QMainWindow::keyReleaseEvent(event); +} + void MainWindow::load_file(QString file_name) { const QString errorMessage( diff --git a/pv/mainwindow.hpp b/pv/mainwindow.hpp index 2e6e55c1..c0717e4f 100644 --- a/pv/mainwindow.hpp +++ b/pv/mainwindow.hpp @@ -104,10 +104,13 @@ private: /** * Updates the device list in the toolbar */ - void update_device_list(); + void update_device_list(); +private: void closeEvent(QCloseEvent *event); + void keyReleaseEvent(QKeyEvent *event); + private Q_SLOTS: void load_file(QString file_name); diff --git a/pv/widgets/hidingmenubar.cpp b/pv/widgets/hidingmenubar.cpp new file mode 100644 index 00000000..dcc8fde5 --- /dev/null +++ b/pv/widgets/hidingmenubar.cpp @@ -0,0 +1,56 @@ +/* + * This file is part of the PulseView project. + * + * Copyright (C) 2015 Joel Holdsworth + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * 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 + */ + +#include + +#include "hidingmenubar.hpp" + +namespace pv { +namespace widgets { + +HidingMenuBar::HidingMenuBar(QWidget *parent) : + QMenuBar(parent) +{ + setHidden(true); + connect(this, SIGNAL(triggered(QAction*)), + this, SLOT(item_triggered())); +} + +void HidingMenuBar::focusOutEvent(QFocusEvent *e) +{ + if (e->reason() != Qt::PopupFocusReason) + setHidden(true); + QMenuBar::focusOutEvent(e); +} + +void HidingMenuBar::keyPressEvent(QKeyEvent *e) +{ + if (e->key() == Qt::Key_Escape) + setHidden(true); + QMenuBar::keyPressEvent(e); +} + +void HidingMenuBar::item_triggered() +{ + setHidden(true); +} + +} // namespace widgets +} // namespace pv diff --git a/pv/widgets/hidingmenubar.hpp b/pv/widgets/hidingmenubar.hpp new file mode 100644 index 00000000..bd9a1151 --- /dev/null +++ b/pv/widgets/hidingmenubar.hpp @@ -0,0 +1,66 @@ +/* + * This file is part of the PulseView project. + * + * Copyright (C) 2015 Joel Holdsworth + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * 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 + */ + +#ifndef PULSEVIEW_PV_WIDGETS_HIDINGMENUBAR_H +#define PULSEVIEW_PV_WIDGETS_HIDINGMENUBAR_H + +#include + +namespace pv { +namespace widgets { + +/** + * A menu bar widget that only remains visible while it retains focus. + */ +class HidingMenuBar : public QMenuBar +{ + Q_OBJECT + +public: + /** + * Constructor + * @param parent The parent widget. + */ + HidingMenuBar(QWidget *parent = nullptr); + +private: + /** + * Handles the event that the widget loses keyboard focus. + * @param e the representation of the event details. + */ + void focusOutEvent(QFocusEvent *e); + + /** + * Handles the event that a key is depressed. + * @param e the representation of the event details. + */ + void keyPressEvent(QKeyEvent *e); + +private Q_SLOTS: + /** + * Handles a menu items being triggered. + */ + void item_triggered(); +}; + +} // widgets +} // pv + +#endif // PULSEVIEW_PV_WIDGETS_HIDINGMENUBAR_H