commit 367a7a6dddc7917d5e99fa6ba894812ad6f62e48
Author: Guillaume Munch <[email protected]>
Date:   Tue Mar 22 21:57:17 2016 +0000

    LyXToolBox: a QToolBox with minimum size management
    
    The purpose of this custom widget is to allow the use of a QToolBox in a 
limited
    area. The stock QToolBox does not provide a minimum size hint that depends 
on
    the size of the pages; it assumes that there is enough room.  This subclass 
sets
    the minimal size of the QToolbox. Without this, the size of the QToolbox is 
only
    determined by values in the ui file and therefore causes portability and
    localisation issues. Note that the computation of the minimum size hint 
depends
    on the minimum size hints of the page widgets. Therefore page widgets must 
have
    a layout with layoutSizeContraint = SetMinimumSize or similar.

diff --git a/src/frontends/qt4/GuiCitation.cpp 
b/src/frontends/qt4/GuiCitation.cpp
index 16c0990..83ee308 100644
--- a/src/frontends/qt4/GuiCitation.cpp
+++ b/src/frontends/qt4/GuiCitation.cpp
@@ -17,6 +17,7 @@
 #include "GuiCitation.h"
 
 #include "GuiSelectionManager.h"
+#include "LyXToolBox.h"
 #include "qt_helpers.h"
 
 #include "Buffer.h"
@@ -625,33 +626,6 @@ bool GuiCitation::initialiseParams(string const & data)
        citeCmds_ = documentBuffer().params().citeCommands();
        citeStyles_ = documentBuffer().params().citeStyles();
        init();
-
-       // Set the minimal size of the QToolbox. Without this, the size of the
-       // QToolbox is only determined by values in the ui file (e.g. computed 
by
-       // qtcreator) and therefore causes portability and localisation issues. 
In
-       // the future, this should be integrated into a custom widget if plans 
are
-       // made to generalise such a use of QToolboxes. Note that the page 
widgets
-       // must have a layout with layoutSizeContraint = SetMinimumSize or 
similar.
-       if (!isVisible()) {
-               // only reliable way to get the size calculations up-to-date
-               show();
-               layout()->invalidate();
-               hide();
-               // this does not show any window since hide() is called before
-               // relinquishing control
-       }
-       QSize minimum_size = QSize(0,0);
-       // Compute the max of the minimal sizes of the pages
-       QWidget * page;
-       for (int i = 0; (page = citationTB->widget(i)); ++i)
-               minimum_size = minimum_size.expandedTo(page->minimumSizeHint());
-       // Add the height of the tabs
-       if (citationTB->currentWidget())
-               minimum_size.rheight() += citationTB->height() -
-                       citationTB->currentWidget()->height();
-       citationTB->setMinimumSize(minimum_size);
-       updateGeometry();
-
        return true;
 }
 
diff --git a/src/frontends/qt4/LyXToolBox.cpp b/src/frontends/qt4/LyXToolBox.cpp
new file mode 100644
index 0000000..fcd3dbe
--- /dev/null
+++ b/src/frontends/qt4/LyXToolBox.cpp
@@ -0,0 +1,55 @@
+// -*- C++ -*-
+/**
+ * \file LyXToolBox.cpp
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Guillaume Munch
+ *
+ * Full author contact details are available in file CREDITS.
+ */
+
+#include "LyXToolBox.h"
+
+#include <QApplication>
+#include <QLayout>
+
+#include "support/debug.h"
+
+namespace lyx {
+namespace frontend {
+
+
+QSize LyXToolBox::minimumSizeHint() const
+{
+       QSize s(0,0);
+       // Compute the max of the minimal sizes of the pages
+       QWidget * page;
+       for (int i = 0; (page = widget(i)); ++i)
+               s = s.expandedTo(page->minimumSizeHint());
+       // Add the height of the tabs
+       if (currentWidget())
+               s.rheight() += height() - currentWidget()->height();
+       return s;
+}
+
+void LyXToolBox::showEvent(QShowEvent * e)
+{
+       // Computation of the tab height might be incorrect yet (the proper 
sizes of
+       // the pages have only been computed now).
+       // It might still be incorrect after this. All this would be 
unnecessary if
+       // QToolBox made our life easier and exposed more information; for 
instance
+       // let us access the scroll areas enclosing the pages (from which one 
can
+       // deduce the real tab height).
+       layout()->invalidate();
+       // proceed with geometry update to avoid flicker
+       qApp->processEvents(QEventLoop::ExcludeUserInputEvents, 50);
+       QToolBox::showEvent(e);
+}
+
+
+} // namespace frontend
+} // namespace lyx
+
+#include "moc_LyXToolBox.cpp"
+
diff --git a/src/frontends/qt4/LyXToolBox.h b/src/frontends/qt4/LyXToolBox.h
new file mode 100644
index 0000000..ec70ceb
--- /dev/null
+++ b/src/frontends/qt4/LyXToolBox.h
@@ -0,0 +1,46 @@
+// -*- C++ -*-
+/**
+ * \file LyXToolBox.h
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Guillaume Munch
+ *
+ * Full author contact details are available in file CREDITS.
+ */
+
+#ifndef LYXTOOLBOX_H
+#define LYXTOOLBOX_H
+
+#include <QtGui/QToolBox>
+
+namespace lyx {
+namespace frontend {
+
+// The purpose of this custom widget is to allow the use of a QToolBox in a
+// limited area. The stock QToolBox does not provide a minimum size hint that
+// depends on the size of the pages; it assumes that there is enough room.  
This
+// subclass sets the minimal size of the QToolbox. Without this, the size of 
the
+// QToolbox is only determined by values in the ui file and therefore causes
+// portability and localisation issues. Note that the computation of the 
minimum
+// size hint depends on the minimum size hints of the page widgets. Therefore
+// page widgets must have a layout with layoutSizeContraint = SetMinimumSize or
+// similar.
+class LyXToolBox : public QToolBox
+{
+       Q_OBJECT
+
+public:
+       LyXToolBox(QWidget * p = 0, Qt::WindowFlags f = 0) : QToolBox(p, f) {}
+       QSize minimumSizeHint() const;
+
+protected:
+       void showEvent(QShowEvent * e);
+};
+
+
+} // namespace frontend
+} // namespace lyx
+
+
+#endif // LYXTOOLBOX_H
diff --git a/src/frontends/qt4/Makefile.am b/src/frontends/qt4/Makefile.am
index 1c693e4..124144b 100644
--- a/src/frontends/qt4/Makefile.am
+++ b/src/frontends/qt4/Makefile.am
@@ -144,6 +144,7 @@ SOURCEFILES = \
        InsetParamsWidget.cpp \
        LengthCombo.cpp \
        LyXFileDialog.cpp \
+       LyXToolBox.cpp \
        LaTeXHighlighter.cpp \
        LayoutBox.cpp \
        Menus.cpp \
@@ -253,6 +254,7 @@ MOCHEADER = \
        LayoutBox.h \
        LengthCombo.h \
        LyXFileDialog.h \
+       LyXToolBox.h \
        Menus.h \
        PanelStack.h \
        TocModel.h \
diff --git a/src/frontends/qt4/ui/CitationUi.ui 
b/src/frontends/qt4/ui/CitationUi.ui
index d10af98..ce40759 100644
--- a/src/frontends/qt4/ui/CitationUi.ui
+++ b/src/frontends/qt4/ui/CitationUi.ui
@@ -194,7 +194,7 @@
     </layout>
    </item>
    <item row="1" column="0">
-    <widget class="QToolBox" name="citationTB">
+    <widget class="lyx::frontend::LyXToolBox" name="citationTB">
      <property name="sizePolicy">
       <sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
        <horstretch>0</horstretch>
@@ -208,14 +208,6 @@
       <number>1</number>
      </property>
      <widget class="QWidget" name="page">
-      <property name="geometry">
-       <rect>
-        <x>0</x>
-        <y>0</y>
-        <width>572</width>
-        <height>111</height>
-       </rect>
-      </property>
       <attribute name="label">
        <string>&amp;Search Citation</string>
       </attribute>
@@ -377,14 +369,6 @@
       </layout>
      </widget>
      <widget class="QWidget" name="page_2">
-      <property name="geometry">
-       <rect>
-        <x>0</x>
-        <y>0</y>
-        <width>572</width>
-        <height>105</height>
-       </rect>
-      </property>
       <attribute name="label">
        <string>For&amp;matting</string>
       </attribute>
@@ -558,6 +542,14 @@
    </item>
   </layout>
  </widget>
+ <customwidgets>
+  <customwidget>
+   <class>lyx::frontend::LyXToolBox</class>
+   <extends>QToolBox</extends>
+   <header>LyXToolBox.h</header>
+   <container>1</container>
+  </customwidget>
+ </customwidgets>
  <tabstops>
   <tabstop>availableLV</tabstop>
   <tabstop>addPB</tabstop>

Reply via email to