From 3c9435c1cfd82f5cc91779eb4b51a5a5628313a0 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Wed, 5 Jul 2017 02:40:10 +0200 Subject: [PATCH] Ref T30, font menu class * font dialog * shortcuts CTRL+/- to increase/decrease font size --- src/blackgui/menus/fontmenus.cpp | 99 ++++++++++++++++++++++++++++++++ src/blackgui/menus/fontmenus.h | 63 ++++++++++++++++++++ src/blackgui/menus/menuaction.h | 15 +++-- 3 files changed, 171 insertions(+), 6 deletions(-) create mode 100644 src/blackgui/menus/fontmenus.cpp create mode 100644 src/blackgui/menus/fontmenus.h diff --git a/src/blackgui/menus/fontmenus.cpp b/src/blackgui/menus/fontmenus.cpp new file mode 100644 index 000000000..1b80a933f --- /dev/null +++ b/src/blackgui/menus/fontmenus.cpp @@ -0,0 +1,99 @@ +/* Copyright (C) 2017 + * swift project Community / Contributors + * + * This file is part of swift Project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#include "fontmenus.h" +#include "blackgui/guiapplication.h" +#include "blackmisc/verify.h" +#include + +using namespace BlackMisc; +using namespace BlackGui::Components; + +namespace BlackGui +{ + namespace Menus + { + const CLogCategoryList &CFontMenu::getLogCategories() + { + static const CLogCategoryList cats { CLogCategory::guiComponent() }; + return cats; + } + + CFontMenu::CFontMenu(QWidget *widget, bool separator) : + IMenuDelegate(widget, separator), m_widget(widget) + { + this->m_fontDialogAction.reset(new QAction(CIcons::font16(), "Font", this)); + QObject::connect(this->m_fontDialogAction.data(), &QAction::triggered, this, &CFontMenu::changeFontDialog); + + m_fontSizePlusShortcut = new QShortcut(Qt::CTRL + Qt::Key_Plus, this->m_widget); + m_fontSizePlusShortcut->setContext(Qt::WidgetShortcut); + QObject::connect(this->m_fontSizePlusShortcut, &QShortcut::activated, this, &CFontMenu::fontSizePlus); + + m_fontSizeMinusShortcut = new QShortcut(Qt::CTRL + Qt::Key_Minus, this->m_widget); + m_fontSizeMinusShortcut->setContext(Qt::WidgetShortcut); + QObject::connect(this->m_fontSizeMinusShortcut, &QShortcut::activated, this, &CFontMenu::fontSizeMinus); + + m_fontResetShortcut = new QShortcut(Qt::CTRL + Qt::Key_0, this->m_widget); + m_fontResetShortcut->setContext(Qt::WidgetShortcut); + QObject::connect(this->m_fontResetShortcut, &QShortcut::activated, this, &CFontMenu::fontReset); + } + + void CFontMenu::customMenu(CMenuActions &menuActions) + { + menuActions.addAction(this->m_fontDialogAction.data(), CMenuAction::pathFont()); + this->nestedCustomMenu(menuActions); + } + + QList CFontMenu::getActions() const + { + return QList({ m_fontDialogAction.data() }); + } + + void CFontMenu::changeFontDialog() + { + Q_ASSERT_X(m_widget, Q_FUNC_INFO, "No widget"); + if (!m_dialog) + { + m_dialog.reset(new CSettingsFontDialog(m_widget)); + m_dialog->setModal(true); + } + m_dialog->setCurrentFont(m_widget->font()); + int r = m_dialog->exec(); + if (r == QDialog::Rejected) { return; } + const QString qss(m_dialog->getQss()); + m_widget->setStyleSheet(qss); + } + + void CFontMenu::fontSizePlus() + { + Q_ASSERT_X(m_widget, Q_FUNC_INFO, "No widget"); + int pt = m_widget->font().pointSize() + 1; + if (pt > 24) { return; } + m_widget->setStyleSheet( + CStyleSheetUtility::asStylesheet(m_widget, pt) + ); + } + + void CFontMenu::fontSizeMinus() + { + Q_ASSERT_X(m_widget, Q_FUNC_INFO, "No widget"); + int pt = m_widget->font().pointSize() - 1; + if (pt < 5) { return; } + m_widget->setStyleSheet( + CStyleSheetUtility::asStylesheet(m_widget, pt) + ); + } + + void CFontMenu::fontReset() + { + Q_ASSERT_X(m_widget, Q_FUNC_INFO, "No widget"); + m_widget->setStyleSheet(""); + } + } // ns +} // ns diff --git a/src/blackgui/menus/fontmenus.h b/src/blackgui/menus/fontmenus.h new file mode 100644 index 000000000..ed2fcd0db --- /dev/null +++ b/src/blackgui/menus/fontmenus.h @@ -0,0 +1,63 @@ +/* Copyright (C) 2017 + * swift project Community / Contributors + * + * This file is part of swift Project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#ifndef BLACKGUI_MENUS_FONTMENUS_H +#define BLACKGUI_MENUS_FONTMENUS_H + +#include "blackgui/components/settingsfontdialog.h" +#include "blackgui/menus/menudelegate.h" +#include "blackgui/views/aircraftmodelview.h" +#include "blackgui/overlaymessagesframe.h" +#include "blackmisc/simulation/aircraftmodelinterfaces.h" + +#include + +class QAction; +class QShortcut; + +namespace BlackGui +{ + namespace Menus + { + //! Standard font (size, style) menus for fonts. + //! Can be used as nested menu or via getActions added manually + class CFontMenu : public IMenuDelegate + { + Q_OBJECT + + public: + //! Constructor + CFontMenu(QWidget *widget, bool separator = true); + + //! Log.categories + static const BlackMisc::CLogCategoryList &getLogCategories(); + + //! \copydoc IMenuDelegate::customMenu + virtual void customMenu(CMenuActions &menuActions) override; + + //! Allow to use the actions directly + QList getActions() const; + + private: + void changeFontDialog(); + void fontSizePlus(); + void fontSizeMinus(); + void fontReset(); + + QWidget *m_widget = nullptr; + QShortcut *m_fontSizePlusShortcut = nullptr; //! owned by widget + QShortcut *m_fontSizeMinusShortcut = nullptr; //! owned by widget + QShortcut *m_fontResetShortcut = nullptr; //! owned by widget + QScopedPointer m_fontDialogAction; + QScopedPointer m_dialog; + }; + } // ns +} // ns + +#endif // guard diff --git a/src/blackgui/menus/menuaction.h b/src/blackgui/menus/menuaction.h index 0104fae75..e6f131c0f 100644 --- a/src/blackgui/menus/menuaction.h +++ b/src/blackgui/menus/menuaction.h @@ -143,6 +143,9 @@ namespace BlackGui //! Log functionality static const QString &pathLog() { static const QString p("Custom15.Log"); return p; } + //! Font menus (font size etc.) + static const QString &pathFont() { static const QString p("Custom20.Font"); return p; } + // ---- client ---- //! Client COM related @@ -200,12 +203,12 @@ namespace BlackGui //! @} private: - QAction *m_action = nullptr; //!< the action - QIcon m_icon; //!< icon - QString m_title; //!< title - QString m_path; //!< path in menu - bool m_separator = false; //!< separator - bool m_isMenu = false; //!< is menu? + QAction *m_action = nullptr; //!< the action + QIcon m_icon; //!< icon + QString m_title; //!< title + QString m_path; //!< path in menu + bool m_separator = false; //!< separator + bool m_isMenu = false; //!< is menu? }; /*!