Bo Peng wrote:
> Now, with qt 4.2.2 and your patch, I can
> 1. click to open a subpanel
> 2. click a symbol and the panel does not disappear
> 3. click again on vacant area, the panel will disappear.

strange, it disappears here

2 or 3? Both behaviors are fine, right? My understanding is that 2 is
used to enter multiple symbols from the same panel.


i would like the menu to close after clicking on a symbol when it is not torn off

when the menu is torn off i'd like it close only when clicking on the close button

latest patch attached (with andre's event->ignore())

Index: src/frontends/qt4/IconPalette.cpp
===================================================================
--- src/frontends/qt4/IconPalette.cpp	(revision 18754)
+++ src/frontends/qt4/IconPalette.cpp	(working copy)
@@ -28,6 +28,83 @@
 namespace lyx {
 namespace frontend {
 
+#if QT_VERSION >= 0x040200
+
+
+class MathButton : public QToolButton
+{
+public:
+	MathButton(QWidget * parent = 0);
+	void mouseReleaseEvent(QMouseEvent *event); 
+};
+
+
+MathButton::MathButton(QWidget * parent)
+	: QToolButton(parent)
+{
+}
+
+
+void MathButton::mouseReleaseEvent(QMouseEvent *event)
+{
+	QToolButton::mouseReleaseEvent(event);
+	event->ignore();
+}
+
+
+IconPalette::IconPalette(QObject * parent)
+	: QWidgetAction(parent)
+{
+}
+
+
+void IconPalette::addButton(QAction * action)
+{
+	actions_.push_back(action);
+}
+
+
+QWidget * IconPalette::createWidget(QWidget * parent)
+{
+	QWidget * widget = new QWidget(parent);
+	QGridLayout * layout = new QGridLayout(widget);
+	layout->setSpacing(0);
+	layout->setMargin(0);
+	widget->setLayout(layout);
+
+	QToolBar * toolbar = qobject_cast<QToolBar *>(parentWidget()->parentWidget());
+	for (int i = 0; i < actions_.size(); ++i) {
+		MathButton * tb = new MathButton(widget);
+		tb->setAutoRaise(true);
+		tb->setDefaultAction(actions_.at(i));
+		tb->setIconSize(toolbar->iconSize());
+		connect(toolbar, SIGNAL(iconSizeChanged(const QSize &)),
+			tb, SLOT(setIconSize(const QSize &)));
+	
+		int const ncols = qMin(6, i + 1);
+		int const row = i/ncols + 1;
+		int const col = qMax(1, i + 1 - (row - 1) * 6);
+		layout->addWidget(tb, row, col);
+	}
+
+	return widget;
+}
+
+
+void IconPalette::updateParent()
+{
+	bool enable = false;
+	for (int i = 0; i < actions_.size(); ++i)
+		if (actions_.at(i)->isEnabled()) {
+			enable = true;
+			break;
+		}
+
+	parentWidget()->setEnabled(enable);
+}
+
+#else  // QT_VERSION >= 0x040200
+
 IconPalette::IconPalette(QWidget * parent)
 	: QWidget(parent, Qt::Popup)
 {
@@ -151,6 +228,7 @@
 	// draw the rest (buttons)
 	QWidget::paintEvent(event);
 }
+#endif // QT_VERSION >= 0x040200
 
 
 ButtonMenu::ButtonMenu(const QString & title, QWidget * parent)
Index: src/frontends/qt4/IconPalette.h
===================================================================
--- src/frontends/qt4/IconPalette.h	(revision 18754)
+++ src/frontends/qt4/IconPalette.h	(working copy)
@@ -17,12 +17,35 @@
 #include <QLayout>
 #include "Action.h"
 
+// FIXME: this can go when we move to Qt 4.3
+#define QT_VERSION_CHECK(major, minor, patch) ((major<<16)|(minor<<8)|(patch))
+
+#if QT_VERSION >= QT_VERSION_CHECK(4, 2, 0)
+#include <QWidgetAction>
+#endif
+
 namespace lyx {
 namespace frontend {
 
 /**
  * For holding an arbitrary set of icons.
  */
+#if QT_VERSION >= QT_VERSION_CHECK(4, 2, 0)
+
+class IconPalette : public QWidgetAction {
+	Q_OBJECT
+public:
+	IconPalette(QObject * parent);
+	void addButton(QAction *);
+	QWidget * createWidget(QWidget * parent);
+public Q_SLOTS:
+	void updateParent();
+private:
+	QList<QAction *> actions_;
+};
+
+#else
+
 class IconPalette : public QWidget {
 	Q_OBJECT
 public:
@@ -49,6 +72,8 @@
 	QList<QAction *> actions_;
 };
 
+#endif // QT_VERSION >= QT_VERSION_CHECK(4, 2, 0)
+
 /**
  * Popup menu for a toolbutton.
  * We need this to keep track whether
Index: src/frontends/qt4/QLToolbar.cpp
===================================================================
--- src/frontends/qt4/QLToolbar.cpp	(revision 18754)
+++ src/frontends/qt4/QLToolbar.cpp	(working copy)
@@ -207,7 +207,6 @@
 		}
 	case ToolbarItem::ICONPALETTE: {
 		QToolButton * tb = new QToolButton(this);
-		tb->setCheckable(true);
 		tb->setToolTip(qt_(to_ascii(item.label_)));
 		tb->setStatusTip(qt_(to_ascii(item.label_)));
 		tb->setText(qt_(to_ascii(item.label_)));
@@ -232,8 +231,20 @@
 				if (it == tbinfo.items.begin())
 					tb->setIcon(QPixmap(getIcon(it->func_).c_str()));
 			}
+
+#if QT_VERSION >= 0x040200
+		QMenu * m = new QMenu(tb);
+		m->addAction(panel);
+		m->setTearOffEnabled(true);
+		m->setWindowTitle(qt_(to_ascii(item.label_)));
+		tb->setPopupMode(QToolButton::InstantPopup);
+		tb->setMenu(m);
+#else
+		tb->setCheckable(true);
 		connect(tb, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
 		connect(panel, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
+#endif // QT_VERSION >= 0x040200
+
 		addWidget(tb);
 		break;
 		}
@@ -249,6 +260,8 @@
 			tb, SLOT(setIconSize(const QSize &)));
 
 		ButtonMenu * m = new ButtonMenu(qt_(to_ascii(item.label_)), tb);
+		m->setWindowTitle(qt_(to_ascii(item.label_)));
+		m->setTearOffEnabled(true);
 		connect(this, SIGNAL(updated()), m, SLOT(updateParent()));
 		ToolbarInfo const & tbinfo = toolbarbackend.getToolbar(item.name_);
 		ToolbarInfo::item_iterator it = tbinfo.items.begin();

Reply via email to