From 51be84ebc15c6bbc410aafe3b5c041a107bbd8cd Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Sat, 11 Jun 2022 00:41:06 +0200 Subject: [PATCH] Avoid segfaults in batch mode --- src/laybasic/laybasic/layAbstractMenu.cc | 139 +++++++-------------- src/laybasic/laybasic/layAbstractMenu.h | 3 +- src/laybasic/laybasic/layLayoutViewBase.cc | 4 +- 3 files changed, 46 insertions(+), 100 deletions(-) diff --git a/src/laybasic/laybasic/layAbstractMenu.cc b/src/laybasic/laybasic/layAbstractMenu.cc index beb6b2045..7c24ce749 100644 --- a/src/laybasic/laybasic/layAbstractMenu.cc +++ b/src/laybasic/laybasic/layAbstractMenu.cc @@ -23,6 +23,7 @@ #include "layAbstractMenu.h" #include "layDispatcher.h" #include "layPlugin.h" +#include "layUtils.h" #include "tlExceptions.h" #include "tlAssert.h" #include "gtf.h" @@ -366,8 +367,12 @@ id_from_action (QAction *action) Action::Action () : #if defined(HAVE_QT) mp_menu (0), - mp_action (new ActionObject (0)), + mp_action (lay::has_gui () ? new ActionObject (0) : 0), #endif + m_checked (false), + m_checkable (false), + m_enabled (true), + m_separator (false), mp_dispatcher (0), m_owned (true), m_visible (true), @@ -381,8 +386,10 @@ Action::Action () : #if defined(HAVE_QT) // catch the destroyed signal to tell if the QAction object is deleted. - connect (mp_action, SIGNAL (destroyed (QObject *)), this, SLOT (destroyed (QObject *))); - connect (mp_action, SIGNAL (triggered ()), this, SLOT (qaction_triggered ())); + if (mp_action) { + connect (mp_action, SIGNAL (destroyed (QObject *)), this, SLOT (destroyed (QObject *))); + connect (mp_action, SIGNAL (triggered ()), this, SLOT (qaction_triggered ())); + } #endif } @@ -390,6 +397,10 @@ Action::Action () : Action::Action (QAction *action, bool owned) : mp_menu (0), mp_action (action), + m_checked (false), + m_checkable (false), + m_enabled (true), + m_separator (false), mp_dispatcher (0), m_owned (owned), m_visible (true), @@ -409,6 +420,10 @@ Action::Action (QAction *action, bool owned) Action::Action (QMenu *menu, bool owned) : mp_menu (menu), mp_action (menu->menuAction ()), + m_checked (false), + m_checkable (false), + m_enabled (true), + m_separator (false), mp_dispatcher (0), m_owned (owned), m_visible (true), @@ -429,8 +444,12 @@ Action::Action (QMenu *menu, bool owned) Action::Action (const std::string &title) : #if defined(HAVE_QT) mp_menu (0), - mp_action (new QAction (0)), + mp_action (lay::has_gui () ? new ActionObject (0) : 0), #endif + m_checked (false), + m_checkable (false), + m_enabled (true), + m_separator (false), mp_dispatcher (0), m_owned (true), m_visible (true), @@ -442,19 +461,14 @@ Action::Action (const std::string &title) : } sp_actionHandles->insert (this); -#if !defined(HAVE_QT) - m_checked = false; - m_checkable = false; - m_enabled = true; - m_separator = false; -#endif - configure_from_title (title); #if defined(HAVE_QT) // catch the destroyed signal to tell if the QAction object is deleted. - connect (mp_action, SIGNAL (destroyed (QObject *)), this, SLOT (destroyed (QObject *))); - connect (mp_action, SIGNAL (triggered ()), this, SLOT (qaction_triggered ())); + if (mp_action) { + connect (mp_action, SIGNAL (destroyed (QObject *)), this, SLOT (destroyed (QObject *))); + connect (mp_action, SIGNAL (triggered ()), this, SLOT (qaction_triggered ())); + } #endif } @@ -509,26 +523,17 @@ Action::configure_from_title (const std::string &s) parse_menu_title (s, title, shortcut, res, tool_tip); -#if defined(HAVE_QT) - qaction ()->setText (tl::to_qstring (title)); - - if (! tool_tip.empty ()) { - qaction ()->setToolTip (tl::to_qstring (tool_tip)); - } - - if (! res.empty ()) { - qaction ()->setIcon (QIcon (tl::to_qstring (res))); - } + set_title (title); if (! shortcut.empty ()) { set_default_shortcut (shortcut); } -#else - m_title = title; - m_default_shortcut = shortcut; - m_tooltip = tool_tip; - m_icon = res; -#endif + if (! tool_tip.empty ()) { + set_tool_tip (tool_tip); + } + if (! res.empty ()) { + set_icon (res); + } } #if defined(HAVE_QT) @@ -664,21 +669,13 @@ Action::set_shortcut (const std::string &sc) std::string Action::get_default_shortcut () const { -#if defined(HAVE_QT) - return tl::to_string (m_default_key_sequence.toString ()); -#else return m_default_shortcut; -#endif } std::string Action::get_shortcut () const { -#if defined(HAVE_QT) - return m_no_key_sequence ? Action::no_shortcut () : tl::to_string (m_key_sequence.toString ()); -#else return m_no_key_sequence ? Action::no_shortcut () : m_shortcut; -#endif } #if defined(HAVE_QT) @@ -727,31 +724,19 @@ Action::set_title (const std::string &t) if (qaction ()) { qaction ()->setText (tl::to_qstring (t)); } -#else - m_title = t; #endif + m_title = t; } std::string Action::get_title () const { -#if defined(HAVE_QT) - if (qaction ()) { - return tl::to_string (qaction ()->text ()); - } else { - return std::string (); - } -#else return m_title; -#endif } std::string Action::get_effective_shortcut () const { -#if defined(HAVE_QT) - return tl::to_string (get_key_sequence ().toString ()); -#else if (m_hidden || m_no_key_sequence) { // A hidden menu item does not have a key sequence too. return std::string (); @@ -760,15 +745,11 @@ Action::get_effective_shortcut () const } else { return m_shortcut; } -#endif } std::string Action::get_effective_shortcut_for (const std::string &sc) const { -#if defined(HAVE_QT) - return tl::to_string (get_key_sequence_for (sc).toString ()); -#else if (m_hidden) { // A hidden menu item does not have a key sequence too. return std::string (); @@ -779,7 +760,6 @@ Action::get_effective_shortcut_for (const std::string &sc) const } else { return sc; } -#endif } void @@ -796,11 +776,7 @@ Action::add_to_exclusive_group (lay::AbstractMenu *menu, const std::string &grou bool Action::is_checkable () const { -#if defined(HAVE_QT) - return qaction () && qaction ()->isCheckable (); -#else return m_checkable; -#endif } bool @@ -816,21 +792,13 @@ Action::is_checked () const bool Action::is_enabled () const { -#if defined(HAVE_QT) - return qaction () && qaction ()->isEnabled (); -#else return m_enabled; -#endif } bool Action::is_separator () const { -#if defined(HAVE_QT) - return qaction () && qaction ()->isSeparator (); -#else return m_separator; -#endif } void @@ -840,9 +808,8 @@ Action::set_enabled (bool b) if (qaction ()) { qaction ()->setEnabled (b); } -#else - m_enabled = b; #endif + m_enabled = b; } void @@ -852,9 +819,8 @@ Action::set_checked (bool c) if (qaction ()) { qaction ()->setChecked (c); } -#else - m_checked = c; #endif + m_checked = c; } void @@ -864,9 +830,8 @@ Action::set_checkable (bool c) if (qaction ()) { qaction ()->setCheckable (c); } -#else - m_checkable = c; #endif + m_checkable = c; } void @@ -876,9 +841,8 @@ Action::set_separator (bool s) if (qaction ()) { qaction ()->setSeparator (s); } -#else - m_separator = s; #endif + m_separator = s; } void @@ -892,23 +856,14 @@ Action::set_icon (const std::string &filename) qaction ()->setIcon (QIcon (tl::to_qstring (filename))); } } -#else - m_icon = filename; #endif + m_icon = filename; } std::string Action::get_tool_tip () const { -#if defined(HAVE_QT) - if (qaction ()) { - return tl::to_string (qaction ()->toolTip ()); - } else { - return std::string (); - } -#else return m_tooltip; -#endif } void @@ -922,23 +877,14 @@ Action::set_tool_tip (const std::string &text) qaction ()->setToolTip (tl::to_qstring (text)); } } -#else - m_tooltip = text; #endif + m_tooltip = text; } std::string Action::get_icon_text () const { -#if defined(HAVE_QT) - if (qaction ()) { - return tl::to_string (qaction ()->iconText ()); - } else { - return std::string (); - } -#else return m_icontext; -#endif } void @@ -952,9 +898,8 @@ Action::set_icon_text (const std::string &icon_text) qaction ()->setIconText (tl::to_qstring (icon_text)); } } -#else - m_icontext = icon_text; #endif + m_icontext = icon_text; } void diff --git a/src/laybasic/laybasic/layAbstractMenu.h b/src/laybasic/laybasic/layAbstractMenu.h index 555afcfe2..1a51ff398 100644 --- a/src/laybasic/laybasic/layAbstractMenu.h +++ b/src/laybasic/laybasic/layAbstractMenu.h @@ -352,7 +352,7 @@ private: #if defined(HAVE_QT) QMenu *mp_menu; QAction *mp_action; -#else +#endif std::string m_title; std::string m_icon; std::string m_icontext; @@ -361,7 +361,6 @@ private: bool m_checkable; bool m_enabled; bool m_separator; -#endif lay::Dispatcher *mp_dispatcher; bool m_owned; bool m_visible; diff --git a/src/laybasic/laybasic/layLayoutViewBase.cc b/src/laybasic/laybasic/layLayoutViewBase.cc index 5e4230c88..4e5b86646 100644 --- a/src/laybasic/laybasic/layLayoutViewBase.cc +++ b/src/laybasic/laybasic/layLayoutViewBase.cc @@ -416,7 +416,9 @@ LayoutViewBase::finish () #if defined(HAVE_QT) set_menu_parent_widget (widget ()); init_menu (); - menu ()->build (0, 0); + if (widget ()) { + menu ()->build (0, 0); + } #else init_menu (); #endif