]> git.lyx.org Git - features.git/commitdiff
* Allow toolbar menus and panels (qt>=4.2) to be torn off
authorEdwin Leuven <e.leuven@gmail.com>
Sun, 17 Jun 2007 17:55:36 +0000 (17:55 +0000)
committerEdwin Leuven <e.leuven@gmail.com>
Sun, 17 Jun 2007 17:55:36 +0000 (17:55 +0000)
  IconPalette now subclasses QWidgetAction so that we can
  insert them in QMenu (before we were painting ourselves)

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@18812 a592a061-630c-0410-9148-cb99ea01b6c8

src/frontends/qt4/IconPalette.cpp
src/frontends/qt4/IconPalette.h
src/frontends/qt4/QLToolbar.cpp

index e9452541d633a299b4132a0cdf90dc11568b8850..451637bf14bf7d4dc07df39012652372fc9de4b6 100644 (file)
 #include <QPainter>
 #include <QStyle>
 #include <QStyleOptionFrame>
+#include <QMouseEvent>
 
 namespace lyx {
 namespace frontend {
 
+#if QT_VERSION >= 0x040200
+
+
+class MathButton : public QToolButton
+{
+public:
+       MathButton(QWidget * parent = 0) {}
+       void mouseReleaseEvent(QMouseEvent *event); 
+       void mousePressEvent(QMouseEvent *event); 
+};
+
+
+void MathButton::mouseReleaseEvent(QMouseEvent *event)
+{
+       QToolButton::mouseReleaseEvent(event);
+       event->ignore();
+}
+
+
+void MathButton::mousePressEvent(QMouseEvent *event)
+{
+       QToolButton::mousePressEvent(event);
+       event->ignore();
+}
+
+
+IconPalette::IconPalette(QWidget * parent)
+       : QWidgetAction(parent), size_(QSize(22, 22))
+{
+}
+
+
+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);
+
+       for (int i = 0; i < actions_.size(); ++i) {
+               MathButton * tb = new MathButton(widget);
+               tb->setAutoRaise(true);
+               tb->setDefaultAction(actions_.at(i));
+               tb->setIconSize(size_);
+               connect(this, SIGNAL(iconSizeChanged(const QSize &)),
+                       tb, SLOT(setIconSize(const QSize &)));
+       
+               int const row = i/qMin(6, i + 1) + 1;
+               int const col = qMax(1, i + 1 - (row - 1) * 6);
+               layout->addWidget(tb, row, col);
+       }
+
+       return widget;
+}
+
+
+void IconPalette::setIconSize(const QSize & size)
+{
+       size_ = size;
+       // signal
+       iconSizeChanged(size);
+}
+
+
+void IconPalette::updateParent()
+{
+       bool enable = false;
+       for (int i = 0; i < actions_.size(); ++i)
+               if (actions_.at(i)->isEnabled()) {
+                       enable = true;
+                       break;
+               }
+       // signal
+       enabled(enable);
+}
+
+#else  // QT_VERSION >= 0x040200
+
 IconPalette::IconPalette(QWidget * parent)
        : QWidget(parent, Qt::Popup)
 {
        layout_ = new QGridLayout(this);
-       layout_->setSpacing(0);
-       layout_->setMargin(3);
-       setLayout(layout_);
+       layout->setSpacing(0);
+       layout->setMargin(3);
 }
 
 
@@ -151,6 +234,7 @@ void IconPalette::paintEvent(QPaintEvent * event)
        // draw the rest (buttons)
        QWidget::paintEvent(event);
 }
+#endif // QT_VERSION >= 0x040200
 
 
 ButtonMenu::ButtonMenu(const QString & title, QWidget * parent)
index 1f0f37cd80b4548c5e7f6911f192e5bac1055c1a..ed1fb2ae492b5adba6a6c4de410c98d830c449b3 100644 (file)
 #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(QWidget * parent);
+       void addButton(QAction *);
+       QWidget * createWidget(QWidget * parent);
+public Q_SLOTS:
+       void updateParent();
+       void setIconSize(const QSize &);
+Q_SIGNALS:
+       void enabled(bool);
+       void iconSizeChanged(const QSize &);
+private:
+       QList<QAction *> actions_;
+       QSize size_;
+};
+
+#else
+
 class IconPalette : public QWidget {
        Q_OBJECT
 public:
@@ -49,6 +77,8 @@ private:
        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 b7a5cdfd83b0b22cd6887fcf9c9b91ec73364e12..1f93978795b84457a2ed80d7f862a96151b4ec48 100644 (file)
@@ -40,7 +40,6 @@
 #include <QAction>
 #include <QPixmap>
 
-
 namespace lyx {
 
 using std::string;
@@ -207,14 +206,21 @@ void QLToolbar::add(ToolbarItem const & item)
                }
        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_)));
                connect(this, SIGNAL(iconSizeChanged(const QSize &)),
                        tb, SLOT(setIconSize(const QSize &)));
 
+#if QT_VERSION >= 0x040200
+               IconPalette * panel = new IconPalette(&owner_);
+               connect(panel, SIGNAL(enabled(bool)),
+                       tb, SLOT(setEnabled(bool)));
+               connect(this, SIGNAL(iconSizeChanged(const QSize &)),
+                       panel, SLOT(setIconSize(const QSize &)));
+#else
                IconPalette * panel = new IconPalette(tb);
+#endif
                connect(this, SIGNAL(updated()), panel, SLOT(updateParent()));
                ToolbarInfo const & tbinfo = toolbarbackend.getToolbar(item.name_);
                ToolbarInfo::item_iterator it = tbinfo.items.begin();
@@ -232,8 +238,20 @@ void QLToolbar::add(ToolbarItem const & item)
                                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 +267,9 @@ void QLToolbar::add(ToolbarItem const & item)
                        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);
+               //m->setStyleSheet("padding-top: 2px");
                connect(this, SIGNAL(updated()), m, SLOT(updateParent()));
                ToolbarInfo const & tbinfo = toolbarbackend.getToolbar(item.name_);
                ToolbarInfo::item_iterator it = tbinfo.items.begin();