I have attached a first attempt at implementing a toolbar lock. (Unfortunately, it is a bit intermingled with a lfun implementation for changing the icon-size.)

Toolbars can be (un)locked individually via the lfun command

toolbar-movable <name>, e.g. toolbar-movable standard

Toolbars can be (un)locked altogether via the lfun command

toolbar-movable *

or via the menu

View > Toolbars > Lock Toolbars Positions

The toolbar's locked state is saved per toolbar via QSettings.

I save the state of "all toolbars locked" in an extra QSetting. But probably this would just better derived from the state of individual toolbars, i.e. Lock Toolbars Position is checked iff all toolbar positions are locked.

I must confess that I was not always 100% certain what I was doing. I am happy for any critical comments. :)

Daniel
diff --git a/src/FuncCode.h b/src/FuncCode.h
index 40ec9d3..36c5ea5 100644
--- a/src/FuncCode.h
+++ b/src/FuncCode.h
@@ -362,6 +362,8 @@ enum FuncCode
        LFUN_LISTING_INSERT,            // Herbert 20011110, bpeng 20070502
        // 275
        LFUN_TOOLBAR_TOGGLE,            // Edwin 20070521
+       LFUN_TOOLBAR_MOVABLE,           // Daniel, 20160712
+       LFUN_ICON_SIZE,                 // Daniel, 20160712
        LFUN_BUFFER_WRITE_ALL,          // rgh, gpothier 200707XX
        LFUN_PARAGRAPH_PARAMS,
        LFUN_LAYOUT_MODULES_CLEAR,
diff --git a/src/LyXAction.cpp b/src/LyXAction.cpp
index 3702810..9594699 100644
--- a/src/LyXAction.cpp
+++ b/src/LyXAction.cpp
@@ -2728,6 +2728,26 @@ void LyXAction::init()
  */
                { LFUN_TOOLBAR_TOGGLE, "toolbar-toggle", NoBuffer, Buffer },
 /*!
+* \var lyx::FuncCode lyx::LFUN_TOOLBAR_MOVABLE
+* \li Action: Toggles movability of a given toolbar between true/false.
+* \li Syntax: toolbar-toggle <NAME>
+* \li Params: <NAME>: standard|extra|table|math|mathmacrotemplate|\n
+minibuffer|review|view/update|math_panels|vcs|
+view-others|update-others
+* \li Origin: 11 July 2016
+* \endvar
+*/
+               { LFUN_TOOLBAR_MOVABLE, "toolbar-movable", NoBuffer, Buffer },
+/*!
+* \var lyx::FuncCode lyx::LFUN_ICON_SIZE
+* \li Action: Sets icon size of toolbars.
+* \li Syntax: icon-size <SIZE>
+* \li Params: <SIZE>: int
+* \li Origin: 11 July 2016
+* \endvar
+*/
+               { LFUN_ICON_SIZE, "icon-size", NoBuffer, Buffer },
+/*!
  * \var lyx::FuncCode lyx::LFUN_MENU_OPEN
  * \li Action: Opens the menu given by its name.
  * \li Syntax: menu-open <NAME>
diff --git a/src/frontends/qt4/GuiToolbar.cpp b/src/frontends/qt4/GuiToolbar.cpp
index 77471c9..abba387 100644
--- a/src/frontends/qt4/GuiToolbar.cpp
+++ b/src/frontends/qt4/GuiToolbar.cpp
@@ -53,7 +53,7 @@ namespace lyx {
 namespace frontend {
 
 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
-       : QToolBar(toqstr(tbinfo.gui_name), &owner), visibility_(0),
+       : QToolBar(toqstr(tbinfo.gui_name), &owner), visibility_(0), 
movability_(0),
          owner_(owner), command_buffer_(0), tbinfo_(tbinfo), filled_(false),
          restored_(false)
 {
@@ -61,8 +61,6 @@ GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiView & 
owner)
        connect(&owner, SIGNAL(iconSizeChanged(QSize)), this,
                SLOT(setIconSize(QSize)));
 
-       // Toolbar dragging is allowed.
-       setMovable(true);
        // This is used by QMainWindow::restoreState for proper main window 
state
        // restauration.
        setObjectName(toqstr(tbinfo.name));
@@ -110,6 +108,11 @@ void GuiToolbar::setVisibility(int visibility)
        visibility_ = visibility;
 }
 
+void GuiToolbar::setMovability(bool movability)
+{
+       movability_ = movability;
+}
+
 
 Action * GuiToolbar::addItem(ToolbarItem const & item)
 {
@@ -358,6 +361,7 @@ void GuiToolbar::saveSession() const
 {
        QSettings settings;
        settings.setValue(sessionKey() + "/visibility", visibility_);
+       settings.setValue(sessionKey() + "/movability", movability_);
 }
 
 
@@ -374,6 +378,12 @@ void GuiToolbar::restoreSession()
                        
guiApp->toolbars().defaultVisibility(fromqstr(objectName()));
        }
        setVisibility(visibility);
+
+       // restore movability
+       bool movability =
+               settings.value(sessionKey() + "/movability", false).toBool();
+       setMovability(movability);
+       setMovable(movability);
 }
 
 
@@ -409,6 +419,20 @@ void GuiToolbar::toggle()
                qstring_to_ucs4(windowTitle()), state));
 }
 
+void GuiToolbar::movable()
+{
+       setMovability(!isMovable());
+       setMovable(!isMovable());
+       docstring state;
+       if (isMovable()) {
+               state = _("movable");
+       } else {
+               state = _("immovable");
+       }
+       owner_.message(bformat(_("Toolbar \"%1$s\" state set to %2$s"),
+               qstring_to_ucs4(windowTitle()), state));
+}
+
 } // namespace frontend
 } // namespace lyx
 
diff --git a/src/frontends/qt4/GuiToolbar.h b/src/frontends/qt4/GuiToolbar.h
index caad355..5d50734 100644
--- a/src/frontends/qt4/GuiToolbar.h
+++ b/src/frontends/qt4/GuiToolbar.h
@@ -74,6 +74,8 @@ public:
 
        ///
        void setVisibility(int visibility);
+       ///
+       void setMovability(bool movability);
 
        /// Add a button to the bar.
        void add(ToolbarItem const & item);
@@ -98,6 +100,9 @@ public:
        void toggle();
 
        ///
+       void movable();
+
+       ///
        GuiCommandBuffer * commandBuffer() { return command_buffer_; }
 
        ///
@@ -119,6 +124,8 @@ private:
        QList<Action *> actions_;
        /// initial visibility flags
        int visibility_;
+       /// initial movability flags
+       bool movability_;
        ///
        GuiView & owner_;
        ///
diff --git a/src/frontends/qt4/GuiView.cpp b/src/frontends/qt4/GuiView.cpp
index b94625b..bca731e 100644
--- a/src/frontends/qt4/GuiView.cpp
+++ b/src/frontends/qt4/GuiView.cpp
@@ -703,6 +703,7 @@ void GuiView::saveLayout() const
 #endif
        settings.setValue("layout", saveState(0));
        settings.setValue("icon_size", iconSize());
+       settings.setValue("toolbars_movable", toolbarsMovable);
 }
 
 
@@ -731,14 +732,14 @@ bool GuiView::restoreLayout()
        //code below is skipped when when ~/.config/LyX is (re)created
        QSize icon_size = settings.value(icon_key).toSize();
        // Check whether session size changed.
-       if (icon_size.width() != int(d.smallIconSize) &&
+       /*if (icon_size.width() != int(d.smallIconSize) &&
            icon_size.width() != int(d.normalIconSize) &&
            icon_size.width() != int(d.bigIconSize) &&
            icon_size.width() != int(d.hugeIconSize) &&
            icon_size.width() != int(d.giantIconSize)) {
                icon_size.setWidth(d.normalIconSize);
                icon_size.setHeight(d.normalIconSize);
-       }
+       }*/
        setIconSize(icon_size);
 
 #if defined(Q_WS_X11) || defined(QPA_XCB)
@@ -770,14 +771,18 @@ bool GuiView::restoreLayout()
 
        if (!restoreState(settings.value("layout").toByteArray(), 0))
                initToolbars();
+
+       toolbarsMovable = settings.value("toolbars_movable", false).toBool();
        
        // init the toolbars that have not been restored
        Toolbars::Infos::iterator cit = guiApp->toolbars().begin();
        Toolbars::Infos::iterator end = guiApp->toolbars().end();
        for (; cit != end; ++cit) {
                GuiToolbar * tb = toolbar(cit->name);
-               if (tb && !tb->isRestored())
-                       initToolbar(cit->name);
+               if (tb) {
+                       if (!tb->isRestored())
+                               initToolbar(cit->name);
+               }
        }
 
        updateDialogs();
@@ -863,6 +868,10 @@ void GuiView::initToolbar(string const & name)
 
        if (visibility & Toolbars::ON)
                tb->setVisible(true);
+
+       bool const movability = 
false;//guiApp->toolbars().defaultMovability(name);
+       tb->setMovable(movability);
+       tb->setMovability(movability);
 }
 
 
@@ -1096,7 +1105,6 @@ void GuiView::updateStatusBarMessage(QString const & str)
        d.statusbar_timer_.start(3000);
 }
 
-
 void GuiView::smallSizedIcons()
 {
        setIconSize(QSize(d.smallIconSize, d.smallIconSize));
@@ -1892,6 +1900,27 @@ bool GuiView::getStatus(FuncRequest const & cmd, 
FuncStatus & flag)
                break;
        }
 
+       case LFUN_TOOLBAR_MOVABLE: {
+               string const name = cmd.getArg(0);
+               // toolbar name * locks all toolbars
+               // use negation since locked == !(movable)
+               if (name == "*") {
+                       flag.setOnOff(!toolbarsMovable);
+               } else if (GuiToolbar * t = toolbar(name))
+                       flag.setOnOff(!(t->isMovable()));
+               else {
+                       enable = false;
+                       docstring const msg =
+                               bformat(_("Unknown toolbar \"%1$s\""), 
from_utf8(name));
+                       flag.message(msg);
+               }
+               break;
+       }
+
+       case LFUN_ICON_SIZE:
+               // Nothing to check.
+               break;
+
        case LFUN_DROP_LAYOUTS_CHOICE:
                enable = buf != 0;
                break;
@@ -3773,6 +3802,29 @@ void GuiView::dispatch(FuncRequest const & cmd, 
DispatchResult & dr)
                        break;
                }
 
+               case LFUN_TOOLBAR_MOVABLE: {
+                       string const name = cmd.getArg(0);
+                       if (name == "*") {
+                               // set all toolbars (non-)movable
+                               toolbarsMovable = !toolbarsMovable;
+                               Toolbars::Infos::iterator cit = 
guiApp->toolbars().begin();
+                               Toolbars::Infos::iterator end = 
guiApp->toolbars().end();
+                               for (; cit != end; ++cit) {
+                                       GuiToolbar * tb = toolbar(cit->name);
+                                       tb->setMovable(toolbarsMovable);
+                                       tb->setMovability(toolbarsMovable);
+                               }
+                       } else if (GuiToolbar * t = toolbar(name))
+                               t->movable();
+                       break;
+               }
+
+               case LFUN_ICON_SIZE: {
+                       string const size = cmd.getArg(0);
+                       setIconSize(QSize(stoi(size), stoi(size)));
+                       break;
+               }
+
                case LFUN_DIALOG_UPDATE: {
                        string const name = to_utf8(cmd.argument());
                        if (name == "prefs" || name == "document")
diff --git a/src/frontends/qt4/GuiView.h b/src/frontends/qt4/GuiView.h
index 33bfd97..5a6c143 100644
--- a/src/frontends/qt4/GuiView.h
+++ b/src/frontends/qt4/GuiView.h
@@ -210,6 +210,9 @@ public:
        /// Current ratio between physical pixels and device-independent pixels
        double pixelRatio() const;
 
+       // movability flag of all toolbars
+       bool toolbarsMovable;
+
 Q_SIGNALS:
        void closing(int);
        void triggerShowDialog(QString const & qname, QString const & qdata, 
Inset * inset);
diff --git a/src/frontends/qt4/Menus.cpp b/src/frontends/qt4/Menus.cpp
index 0d80b56..e9c9298 100644
--- a/src/frontends/qt4/Menus.cpp
+++ b/src/frontends/qt4/Menus.cpp
@@ -1389,6 +1389,11 @@ void MenuDefinition::expandToolbars()
                item.setSubmenu(other_lists);
                add(item);
        }
+
+       add(MenuItem(MenuItem::Separator));
+       MenuItem const item(MenuItem::Command, "Lock Toolbars Positions",
+               FuncRequest(LFUN_TOOLBAR_MOVABLE, "*"));
+       add(item);
 }
 
 

Reply via email to