Avoid segfaults in batch mode

This commit is contained in:
Matthias Koefferlein 2022-06-11 00:41:06 +02:00
parent a853ed904f
commit 51be84ebc1
3 changed files with 46 additions and 100 deletions

View File

@ -23,6 +23,7 @@
#include "layAbstractMenu.h" #include "layAbstractMenu.h"
#include "layDispatcher.h" #include "layDispatcher.h"
#include "layPlugin.h" #include "layPlugin.h"
#include "layUtils.h"
#include "tlExceptions.h" #include "tlExceptions.h"
#include "tlAssert.h" #include "tlAssert.h"
#include "gtf.h" #include "gtf.h"
@ -366,8 +367,12 @@ id_from_action (QAction *action)
Action::Action () : Action::Action () :
#if defined(HAVE_QT) #if defined(HAVE_QT)
mp_menu (0), mp_menu (0),
mp_action (new ActionObject (0)), mp_action (lay::has_gui () ? new ActionObject (0) : 0),
#endif #endif
m_checked (false),
m_checkable (false),
m_enabled (true),
m_separator (false),
mp_dispatcher (0), mp_dispatcher (0),
m_owned (true), m_owned (true),
m_visible (true), m_visible (true),
@ -381,8 +386,10 @@ Action::Action () :
#if defined(HAVE_QT) #if defined(HAVE_QT)
// catch the destroyed signal to tell if the QAction object is deleted. // catch the destroyed signal to tell if the QAction object is deleted.
connect (mp_action, SIGNAL (destroyed (QObject *)), this, SLOT (destroyed (QObject *))); if (mp_action) {
connect (mp_action, SIGNAL (triggered ()), this, SLOT (qaction_triggered ())); connect (mp_action, SIGNAL (destroyed (QObject *)), this, SLOT (destroyed (QObject *)));
connect (mp_action, SIGNAL (triggered ()), this, SLOT (qaction_triggered ()));
}
#endif #endif
} }
@ -390,6 +397,10 @@ Action::Action () :
Action::Action (QAction *action, bool owned) Action::Action (QAction *action, bool owned)
: mp_menu (0), : mp_menu (0),
mp_action (action), mp_action (action),
m_checked (false),
m_checkable (false),
m_enabled (true),
m_separator (false),
mp_dispatcher (0), mp_dispatcher (0),
m_owned (owned), m_owned (owned),
m_visible (true), m_visible (true),
@ -409,6 +420,10 @@ Action::Action (QAction *action, bool owned)
Action::Action (QMenu *menu, bool owned) Action::Action (QMenu *menu, bool owned)
: mp_menu (menu), : mp_menu (menu),
mp_action (menu->menuAction ()), mp_action (menu->menuAction ()),
m_checked (false),
m_checkable (false),
m_enabled (true),
m_separator (false),
mp_dispatcher (0), mp_dispatcher (0),
m_owned (owned), m_owned (owned),
m_visible (true), m_visible (true),
@ -429,8 +444,12 @@ Action::Action (QMenu *menu, bool owned)
Action::Action (const std::string &title) : Action::Action (const std::string &title) :
#if defined(HAVE_QT) #if defined(HAVE_QT)
mp_menu (0), mp_menu (0),
mp_action (new QAction (0)), mp_action (lay::has_gui () ? new ActionObject (0) : 0),
#endif #endif
m_checked (false),
m_checkable (false),
m_enabled (true),
m_separator (false),
mp_dispatcher (0), mp_dispatcher (0),
m_owned (true), m_owned (true),
m_visible (true), m_visible (true),
@ -442,19 +461,14 @@ Action::Action (const std::string &title) :
} }
sp_actionHandles->insert (this); 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); configure_from_title (title);
#if defined(HAVE_QT) #if defined(HAVE_QT)
// catch the destroyed signal to tell if the QAction object is deleted. // catch the destroyed signal to tell if the QAction object is deleted.
connect (mp_action, SIGNAL (destroyed (QObject *)), this, SLOT (destroyed (QObject *))); if (mp_action) {
connect (mp_action, SIGNAL (triggered ()), this, SLOT (qaction_triggered ())); connect (mp_action, SIGNAL (destroyed (QObject *)), this, SLOT (destroyed (QObject *)));
connect (mp_action, SIGNAL (triggered ()), this, SLOT (qaction_triggered ()));
}
#endif #endif
} }
@ -509,26 +523,17 @@ Action::configure_from_title (const std::string &s)
parse_menu_title (s, title, shortcut, res, tool_tip); parse_menu_title (s, title, shortcut, res, tool_tip);
#if defined(HAVE_QT) set_title (title);
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)));
}
if (! shortcut.empty ()) { if (! shortcut.empty ()) {
set_default_shortcut (shortcut); set_default_shortcut (shortcut);
} }
#else if (! tool_tip.empty ()) {
m_title = title; set_tool_tip (tool_tip);
m_default_shortcut = shortcut; }
m_tooltip = tool_tip; if (! res.empty ()) {
m_icon = res; set_icon (res);
#endif }
} }
#if defined(HAVE_QT) #if defined(HAVE_QT)
@ -664,21 +669,13 @@ Action::set_shortcut (const std::string &sc)
std::string std::string
Action::get_default_shortcut () const Action::get_default_shortcut () const
{ {
#if defined(HAVE_QT)
return tl::to_string (m_default_key_sequence.toString ());
#else
return m_default_shortcut; return m_default_shortcut;
#endif
} }
std::string std::string
Action::get_shortcut () const 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; return m_no_key_sequence ? Action::no_shortcut () : m_shortcut;
#endif
} }
#if defined(HAVE_QT) #if defined(HAVE_QT)
@ -727,31 +724,19 @@ Action::set_title (const std::string &t)
if (qaction ()) { if (qaction ()) {
qaction ()->setText (tl::to_qstring (t)); qaction ()->setText (tl::to_qstring (t));
} }
#else
m_title = t;
#endif #endif
m_title = t;
} }
std::string std::string
Action::get_title () const Action::get_title () const
{ {
#if defined(HAVE_QT)
if (qaction ()) {
return tl::to_string (qaction ()->text ());
} else {
return std::string ();
}
#else
return m_title; return m_title;
#endif
} }
std::string std::string
Action::get_effective_shortcut () const 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) { if (m_hidden || m_no_key_sequence) {
// A hidden menu item does not have a key sequence too. // A hidden menu item does not have a key sequence too.
return std::string (); return std::string ();
@ -760,15 +745,11 @@ Action::get_effective_shortcut () const
} else { } else {
return m_shortcut; return m_shortcut;
} }
#endif
} }
std::string std::string
Action::get_effective_shortcut_for (const std::string &sc) const 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) { if (m_hidden) {
// A hidden menu item does not have a key sequence too. // A hidden menu item does not have a key sequence too.
return std::string (); return std::string ();
@ -779,7 +760,6 @@ Action::get_effective_shortcut_for (const std::string &sc) const
} else { } else {
return sc; return sc;
} }
#endif
} }
void void
@ -796,11 +776,7 @@ Action::add_to_exclusive_group (lay::AbstractMenu *menu, const std::string &grou
bool bool
Action::is_checkable () const Action::is_checkable () const
{ {
#if defined(HAVE_QT)
return qaction () && qaction ()->isCheckable ();
#else
return m_checkable; return m_checkable;
#endif
} }
bool bool
@ -816,21 +792,13 @@ Action::is_checked () const
bool bool
Action::is_enabled () const Action::is_enabled () const
{ {
#if defined(HAVE_QT)
return qaction () && qaction ()->isEnabled ();
#else
return m_enabled; return m_enabled;
#endif
} }
bool bool
Action::is_separator () const Action::is_separator () const
{ {
#if defined(HAVE_QT)
return qaction () && qaction ()->isSeparator ();
#else
return m_separator; return m_separator;
#endif
} }
void void
@ -840,9 +808,8 @@ Action::set_enabled (bool b)
if (qaction ()) { if (qaction ()) {
qaction ()->setEnabled (b); qaction ()->setEnabled (b);
} }
#else
m_enabled = b;
#endif #endif
m_enabled = b;
} }
void void
@ -852,9 +819,8 @@ Action::set_checked (bool c)
if (qaction ()) { if (qaction ()) {
qaction ()->setChecked (c); qaction ()->setChecked (c);
} }
#else
m_checked = c;
#endif #endif
m_checked = c;
} }
void void
@ -864,9 +830,8 @@ Action::set_checkable (bool c)
if (qaction ()) { if (qaction ()) {
qaction ()->setCheckable (c); qaction ()->setCheckable (c);
} }
#else
m_checkable = c;
#endif #endif
m_checkable = c;
} }
void void
@ -876,9 +841,8 @@ Action::set_separator (bool s)
if (qaction ()) { if (qaction ()) {
qaction ()->setSeparator (s); qaction ()->setSeparator (s);
} }
#else
m_separator = s;
#endif #endif
m_separator = s;
} }
void void
@ -892,23 +856,14 @@ Action::set_icon (const std::string &filename)
qaction ()->setIcon (QIcon (tl::to_qstring (filename))); qaction ()->setIcon (QIcon (tl::to_qstring (filename)));
} }
} }
#else
m_icon = filename;
#endif #endif
m_icon = filename;
} }
std::string std::string
Action::get_tool_tip () const 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; return m_tooltip;
#endif
} }
void void
@ -922,23 +877,14 @@ Action::set_tool_tip (const std::string &text)
qaction ()->setToolTip (tl::to_qstring (text)); qaction ()->setToolTip (tl::to_qstring (text));
} }
} }
#else
m_tooltip = text;
#endif #endif
m_tooltip = text;
} }
std::string std::string
Action::get_icon_text () const 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; return m_icontext;
#endif
} }
void void
@ -952,9 +898,8 @@ Action::set_icon_text (const std::string &icon_text)
qaction ()->setIconText (tl::to_qstring (icon_text)); qaction ()->setIconText (tl::to_qstring (icon_text));
} }
} }
#else
m_icontext = icon_text;
#endif #endif
m_icontext = icon_text;
} }
void void

View File

@ -352,7 +352,7 @@ private:
#if defined(HAVE_QT) #if defined(HAVE_QT)
QMenu *mp_menu; QMenu *mp_menu;
QAction *mp_action; QAction *mp_action;
#else #endif
std::string m_title; std::string m_title;
std::string m_icon; std::string m_icon;
std::string m_icontext; std::string m_icontext;
@ -361,7 +361,6 @@ private:
bool m_checkable; bool m_checkable;
bool m_enabled; bool m_enabled;
bool m_separator; bool m_separator;
#endif
lay::Dispatcher *mp_dispatcher; lay::Dispatcher *mp_dispatcher;
bool m_owned; bool m_owned;
bool m_visible; bool m_visible;

View File

@ -416,7 +416,9 @@ LayoutViewBase::finish ()
#if defined(HAVE_QT) #if defined(HAVE_QT)
set_menu_parent_widget (widget ()); set_menu_parent_widget (widget ());
init_menu (); init_menu ();
menu ()->build (0, 0); if (widget ()) {
menu ()->build (0, 0);
}
#else #else
init_menu (); init_menu ();
#endif #endif