Bo Peng wrote:
> Dear all,
>
> The attached patch save/restore toolbar information (right now
> on/off). The patch almost works, but all toolbars are displayed even
> when session disables QToolbar::show() (see patch).
>
> Peter and Abdel: please comment.
I've added the Qt functions and changed the patch so that the initial
ToolbarBackend::Flags are just updated with the saved flags, so
we don't have to touch the show/hide code.
See some other comments below
>
>
> + ToolbarSection::ToolbarInfo & status =
> LyX::ref().session().toolbars().load(tbb.name);
> + status.visible = status.visible || (status.empty && tbb.flags &&
> ToolbarBackend::ON);
> + tb_ptr->show(false, status);
>
> The show function now accepts a full structure that contains various
> toolbar information. How to make use of them is supposed to be
> frontend-specific.
>
>
> +void QLToolbar::show(bool, ToolbarSection::ToolbarInfo const & info)
> {
> - QToolBar::show();
> + // FIXME: info should contain information about toolbar location
> + // etc. This function should make use of them to restore toolbar
> + // position etc.
> + // FIXME: the following seems to be appropriate, but toolbars are
> shown
> + // even with the following commented out???
> +// if (info.visible)
> +// QToolBar::show();
> }
>
This is not necessary any more.
> This is where Peter should move toolbars around when lyx starts. This
> does not work now since all toolbars are displayed even when I comment
> out QToolBar::show(). I do not know what is going on here.
>
> +void QLToolbar::saveInfo(ToolbarSection::ToolbarInfo & info)
> +{
> + // update Toolbar info with current toolbar status
> + // FIXME: there are more information can be saved here
> + info.visible = QLToolbar::isVisible();
> +}
>
> This is where Peter should save toolbar information when lyx ends.
Added.
>
> +class ToolbarSection : SessionSection
> +{
> +public:
> + /// information about a toolbar, not all information can be
> + /// saved/restored by all frontends, but this class provides
> + /// a superset of things that can be managed by session.
> + class ToolbarInfo
> + {
> + public:
> + ///
> + ToolbarInfo() :
> + empty(true), visible(false), posX(-1), posY(-1),
> location(-1) { }
> + ///
> + ToolbarInfo(bool v, int x, int y) :
> + empty(false), visible(v), posX(x), posY(y), location(-1) { }
> +
> + public:
> + /// true means no information is available (so ignore visible
> settings)
> + bool empty;
> + /// on/off
> + bool visible;
> + /// position
> + int posX;
> + ///
> + int posY;
> + /// location: this can be intepreted differently.
> + int location;
> + /// potentially, icons
> + };
>
Here I've added the enum, suggested by Abdel.
> This is where Toolbar info is handled. Peter, what information do you
> want to save? Maybe toolbar orders as well?
>
> Cheers,
> Bo
>
>
> ------------------------------------------------------------------------
>
> Index: src/session.C
> ===================================================================
> --- src/session.C (revision 15650)
> +++ src/session.C (working copy)
> @@ -48,6 +48,7 @@
> string const sec_lastopened = "[last opened files]";
> string const sec_bookmarks = "[bookmarks]";
> string const sec_session = "[session info]";
> +string const sec_toolbars = "[toolbars]";
>
> } // anon namespace
>
> @@ -267,6 +268,57 @@
> }
>
>
> +void ToolbarSection::read(istream & is)
> +{
> + string tmp;
> + do {
> + char c = is.peek();
> + if (c == '[')
> + break;
> + getline(is, tmp);
> +
> + // Read session info, saved as key/value pairs
> + // would better yell if pos returns npos
> + string::size_type pos = tmp.find_first_of(" = ");
> + // silently ignore lines without " = "
> + if (pos != string::npos) {
> + string key = tmp.substr(0, pos);
> + bool visible;
> + int posX;
> + int posY;
> + int location;
> + istringstream value(tmp.substr(pos + 3));
> + value >> posX;
> + value.ignore(2); // ignore ", "
> + value >> posY;
> + value.ignore(2); // ignore ", "
> + value >> location;
> + toolbars[key] = ToolbarInfo(posX, posY, location);
This I've changed to use no ',' because it doesn't work here
- visible added
> + }
> + } while (is.good());
> +}
> +
> +
> +void ToolbarSection::write(ostream & os) const
> +{
> + os << '\n' << sec_toolbars << '\n';
> + for (ToolbarMap::const_iterator tb = toolbars.begin();
> + tb != toolbars.end(); ++tb) {
> + os << tb->first << " = "
> + << tb->second.visible << ", "
> + << tb->second.posX << ", "
> + << tb->second.posY << ", "
> + << tb->second.location << '\n';
> + }
> +}
> +
Also change to use only one space.
> +
> +ToolbarSection::ToolbarInfo & ToolbarSection::load(string const & name)
> +{
> + return toolbars[name];
> +}
> +
> +
> void SessionInfoSection::read(istream & is)
> {
> string tmp;
> @@ -349,6 +401,8 @@
> lastFilePos().read(is);
> else if (tmp == sec_bookmarks)
> bookmarks().read(is);
> + else if (tmp == sec_toolbars)
> + toolbars().read(is);
> else if (tmp == sec_session)
> sessionInfo().read(is);
> else
> @@ -368,6 +422,7 @@
> lastOpened().write(os);
> lastFilePos().write(os);
> bookmarks().write(os);
> + toolbars().write(os);
> sessionInfo().write(os);
> } else
> lyxerr << "LyX: Warning: unable to save Session: "
> Index: src/frontends/LyXView.h
> ===================================================================
> --- src/frontends/LyXView.h (revision 15649)
> +++ src/frontends/LyXView.h (working copy)
> @@ -136,6 +136,8 @@
>
> /// update the toolbar
> void updateToolbars();
> + /// save the status of toolbars
> + void saveToolbarInfo();
> /// update the menubar
> void updateMenubar();
> /// update the status bar
> Index: src/frontends/Toolbars.C
> ===================================================================
> --- src/frontends/Toolbars.C (revision 15649)
> +++ src/frontends/Toolbars.C (working copy)
> @@ -84,6 +84,19 @@
> }
>
>
> +void Toolbars::saveToolbarInfo()
> +{
> + ToolbarSection & tb = LyX::ref().session().toolbars();
> +
> + for (ToolbarBackend::Toolbars::const_iterator cit =
> toolbarbackend.begin();
> + cit != toolbarbackend.end(); ++cit) {
> + ToolbarsMap::iterator it = toolbars_.find(cit->name);
> + BOOST_ASSERT(it != toolbars_.end());
> + it->second->saveInfo(tb.load(cit->name));
> + }
> +}
> +
> +
> void Toolbars::setLayout(string const & layout)
> {
> if (layout_)
> @@ -124,10 +137,9 @@
> ToolbarPtr tb_ptr = owner_.makeToolbar(tbb);
> toolbars_[tbb.name] = tb_ptr;
>
> - if (tbb.flags & ToolbarBackend::ON)
> - tb_ptr->show(false);
> - else
> - tb_ptr->hide(false);
> + ToolbarSection::ToolbarInfo & status =
> LyX::ref().session().toolbars().load(tbb.name);
> + status.visible = status.visible || (status.empty && tbb.flags &&
> ToolbarBackend::ON);
> + tb_ptr->show(false, status);
This is not necessary, because the tbb.flags is up to date.
>
> if (tb_ptr->layout())
> layout_ = tb_ptr->layout();
> @@ -140,10 +152,10 @@
> ToolbarsMap::iterator it = toolbars_.find(tbb.name);
> BOOST_ASSERT(it != toolbars_.end());
>
> - if (show_it)
> - it->second->show(true);
> - else
> - it->second->hide(true);
> + ToolbarSection::ToolbarInfo & status =
> LyX::ref().session().toolbars().load(tbb.name);
> + status.visible = show_it;
> +
> + it->second->show(true, status);
> }
>
same here: This is not necessary, because the tbb.flags is up to date.
>
> Index: src/frontends/qt4/QLToolbar.C
> ===================================================================
> --- src/frontends/qt4/QLToolbar.C (revision 15649)
> +++ src/frontends/qt4/QLToolbar.C (working copy)
> @@ -208,12 +208,26 @@
> }
>
>
> -void QLToolbar::show(bool)
> +void QLToolbar::show(bool, ToolbarSection::ToolbarInfo const & info)
> {
> - QToolBar::show();
> + // FIXME: info should contain information about toolbar location
> + // etc. This function should make use of them to restore toolbar
> + // position etc.
> + // FIXME: the following seems to be appropriate, but toolbars are shown
> + // even with the following commented out???
> +// if (info.visible)
> +// QToolBar::show();
> }
>
same here: This is not necessary, because the tbb.flags is up to date.
>
> +void QLToolbar::saveInfo(ToolbarSection::ToolbarInfo & info)
> +{
> + // update Toolbar info with current toolbar status
> + // FIXME: there are more information can be saved here
> + info.visible = QLToolbar::isVisible();
> +}
> +
> +
> void QLToolbar::update()
> {
> // This is a speed bottleneck because this is called on every keypress
> Index: src/frontends/qt4/QLToolbar.h
> ===================================================================
> --- src/frontends/qt4/QLToolbar.h (revision 15649)
> +++ src/frontends/qt4/QLToolbar.h (working copy)
> @@ -22,6 +22,9 @@
> #include <QToolBar>
> #include <vector>
>
> +#include "lyx_main.h"
> +#include "session.h"
> +
> class QComboBox;
>
> namespace lyx {
> @@ -66,7 +69,8 @@
>
> void add(FuncRequest const & func, lyx::docstring const & tooltip);
> void hide(bool);
> - void show(bool);
> + void show(bool, ToolbarSection::ToolbarInfo const & info);
same here: This is not necessary, because the tbb.flags is up to date.
> + void saveInfo(ToolbarSection::ToolbarInfo & info);
> void update();
> LayoutBox * layout() const { return layout_.get(); }
>
> Index: src/frontends/qt4/GuiView.C
> ===================================================================
> --- src/frontends/qt4/GuiView.C (revision 15649)
> +++ src/frontends/qt4/GuiView.C (working copy)
> @@ -182,6 +182,8 @@
> session.sessionInfo().save("WindowPosX",
> convert<string>(geometry.x()));
> session.sessionInfo().save("WindowPosY",
> convert<string>(geometry.y()));
> }
> + // FIXME: put savetoolbar here?
Why not?
> + saveToolbarInfo();
> }
>
> void GuiView::setGeometry(unsigned int width,
> Index: src/frontends/LyXView.C
> ===================================================================
> --- src/frontends/LyXView.C (revision 15649)
> +++ src/frontends/LyXView.C (working copy)
> @@ -305,6 +305,12 @@
> }
>
>
> +void LyXView::saveToolbarInfo()
> +{
> + toolbars_->saveToolbarInfo();
> +}
> +
> +
Maybe this could be removed as Abdel suggested.
> void LyXView::updateMenubar()
> {
> menubar_->update();
> Index: src/frontends/Toolbars.h
> ===================================================================
> --- src/frontends/Toolbars.h (revision 15649)
> +++ src/frontends/Toolbars.h (working copy)
> @@ -26,6 +26,8 @@
> #include "ToolbarBackend.h"
> #include <boost/shared_ptr.hpp>
> #include <map>
> +#include "lyx_main.h"
> +#include "session.h"
>
>
> namespace lyx {
> @@ -63,7 +65,11 @@
> * \param update_metrics is a hint to the layout engine that the
> * metrics should be updated.
> */
> - virtual void show(bool update_metrics) = 0;
> + virtual void show(bool update_metrics, ToolbarSection::ToolbarInfo
> const & info) = 0;
same here: This is not necessary, because the tbb.flags is up to date.
> + /** update toolbar information
> + * ToolbarInfo will then be saved by session
> + */
> + virtual void saveInfo(ToolbarSection::ToolbarInfo & info) = 0;
>
> /// Refresh the contents of the bar.
> virtual void update() = 0;
> @@ -86,6 +92,9 @@
> /// Update the state of the toolbars.
> void update(bool in_math, bool in_table, bool review);
>
> + /// save toolbar information
> + void saveToolbarInfo();
> +
> /// Select the right layout in the combox.
> void setLayout(std::string const & layout);
>
> Index: src/session.h
> ===================================================================
> --- src/session.h (revision 15650)
> +++ src/session.h (working copy)
> @@ -238,6 +238,55 @@
> };
>
>
> +class ToolbarSection : SessionSection
> +{
> +public:
> + /// information about a toolbar, not all information can be
> + /// saved/restored by all frontends, but this class provides
> + /// a superset of things that can be managed by session.
> + class ToolbarInfo
> + {
> + public:
> + ///
> + ToolbarInfo() :
> + empty(true), visible(false), posX(-1), posY(-1),
> location(-1) { }
> + ///
> + ToolbarInfo(bool v, int x, int y) :
> + empty(false), visible(v), posX(x), posY(y),
> location(-1) { }
visible added.
> +
> + public:
> + /// true means no information is available (so ignore visible
> settings)
> + bool empty;
> + /// on/off
> + bool visible;
> + /// position
> + int posX;
> + ///
> + int posY;
> + /// location: this can be intepreted differently.
> + int location;
> + /// potentially, icons
> + };
> +
enum added:
enum Location {
top,
bottom,
left,
right,
notset
};
> + /// info for each toolbar
> + typedef std::map<std::string, ToolbarInfo> ToolbarMap;
> +
> +public:
> + ///
> + void read(std::istream & is);
> +
> + ///
> + void write(std::ostream & os) const;
> +
> + /// return reference to toolbar info, create a new one if needed
> + ToolbarInfo & load(std::string const & name);
> +
> +private:
> + /// toolbar information
> + ToolbarMap toolbars;
> +};
> +
> +
> class SessionInfoSection : SessionSection
> {
> public:
> @@ -307,6 +356,12 @@
> BookmarksSection const & bookmarks() const { return bookmarks_; }
>
> ///
> + ToolbarSection & toolbars() { return toolbars_; }
> +
> + ///
> + ToolbarSection const & toolbars() const { return toolbars_; }
> +
> + ///
> SessionInfoSection & sessionInfo() { return session_info; }
>
> ///
> @@ -336,6 +391,9 @@
> BookmarksSection bookmarks_;
>
> ///
> + ToolbarSection toolbars_;
> +
> + ///
> SessionInfoSection session_info;
> };
>
--
Peter Kümmel