Andre Poenitz wrote: > On Mon, Jun 18, 2007 at 04:22:09PM +0200, Leuven, E. wrote: >> + /* i didn't find a smarter way to find out >> + * whether a move was made by the user >> + * because we also move the widget ourselves >> + * to make it popup in the right position... >> + */ >> + switch (event->type()) { >> + case QEvent::WindowActivate: >> + active_ = true; >> + case QEvent::WindowDeactivate: >> + active_ = false; > > So active_ == false in both cases? (missing 'break'?)
i changed it to the attached...
Index: src/frontends/qt4/IconPalette.cpp =================================================================== --- src/frontends/qt4/IconPalette.cpp (revision 18813) +++ src/frontends/qt4/IconPalette.cpp (working copy) @@ -13,6 +13,7 @@ #include "IconPalette.h" #include "qt_helpers.h" #include "controllers/ControlMath.h" // for find_xpm +#include "debug.h" #include <QPixmap> #include <QGridLayout> @@ -24,100 +25,40 @@ #include <QPainter> #include <QStyle> #include <QStyleOptionFrame> -#include <QMouseEvent> +#include <QEvent> 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)) + : QWidget(parent, Qt::Tool) { + layout_ = new QGridLayout(this); + layout_->setSpacing(0); + layout_->setMargin(3); + detached_ = false; } -void IconPalette::addButton(QAction * action) +void IconPalette::moveEvent(QMoveEvent * event) { - actions_.push_back(action); -} + // if active we know the user is moving + if (isActiveWindow()) + detached_ = true; - -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; + event->ignore(); } -void IconPalette::setIconSize(const QSize & size) +bool IconPalette::event(QEvent * event) { - 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; + if (event->type() == QEvent::WindowDeactivate) + if (!detached_) { + setVisible(false); + return true; } - // 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); + return QWidget::event(event); } @@ -144,22 +85,24 @@ void IconPalette::clicked(QAction * action) { triggered(action); - setVisible(false); + if (!detached_) + setVisible(false); } void IconPalette::showEvent(QShowEvent * event) { - int hoffset = - parentWidget()->pos().x(); - int voffset = - parentWidget()->pos().y(); + int const framewidth = (frameGeometry().width() - width()) / 2; int const parwidth = parentWidget()->geometry().width(); int const parheight = parentWidget()->geometry().height(); + int hoffset = - parentWidget()->pos().x() + framewidth; + int voffset = - parentWidget()->pos().y() + parheight - 2 * framewidth; // vertical toolbar? QToolBar * toolbar = qobject_cast<QToolBar *>(parentWidget()->parentWidget()); if (toolbar && toolbar->orientation() == Qt::Vertical) { hoffset += parwidth; - voffset -= parheight; + voffset -= parheight - framewidth; } QRect const screen = qApp->desktop()->availableGeometry(this); @@ -167,29 +110,33 @@ parentWidget()->geometry().bottomLeft()); // space to the right? - if (gpos.x() + hoffset + width() > screen.width()) { - hoffset -= width(); + if (gpos.x() + hoffset + frameGeometry().width() > screen.width()) { + hoffset -= frameGeometry().width(); if (toolbar && toolbar->orientation() == Qt::Vertical) hoffset -= parwidth; else hoffset += parwidth; } // space at the bottom? - if (gpos.y() + voffset + height() > screen.height()) { - voffset -= height(); + if (gpos.y() + voffset + frameGeometry().height() > screen.height()) { + voffset -= frameGeometry().height(); if (toolbar && toolbar->orientation() == Qt::Horizontal) - voffset -= parheight; + voffset -= parheight - framewidth; else voffset += parheight; } - move(gpos.x() + hoffset, gpos.y() + voffset); + QRect r = geometry(); + r.moveTo(gpos.x() + hoffset, gpos.y() + voffset); + setGeometry(r); + QWidget::showEvent(event); } void IconPalette::hideEvent(QHideEvent * event ) { + detached_ = false; visible(false); QWidget::hideEvent(event); } @@ -204,39 +151,13 @@ break; } + if (!detached_) + setVisible(false); + parentWidget()->setEnabled(enable); } -void IconPalette::paintEvent(QPaintEvent * event) -{ - // draw border - QPainter p(this); - QRegion emptyArea = QRegion(rect()); - const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this); - if (fw) { - QRegion borderReg; - borderReg += QRect(0, 0, fw, height()); //left - borderReg += QRect(width()-fw, 0, fw, height()); //right - borderReg += QRect(0, 0, width(), fw); //top - borderReg += QRect(0, height()-fw, width(), fw); //bottom - p.setClipRegion(borderReg); - emptyArea -= borderReg; - QStyleOptionFrame frame; - frame.rect = rect(); - frame.palette = palette(); - frame.state = QStyle::State_None; - frame.lineWidth = style()->pixelMetric(QStyle::PM_MenuPanelWidth); - frame.midLineWidth = 0; - style()->drawPrimitive(QStyle::PE_FrameMenu, &frame, &p, this); - } - p.end(); - // draw the rest (buttons) - QWidget::paintEvent(event); -} -#endif // QT_VERSION >= 0x040200 - - ButtonMenu::ButtonMenu(const QString & title, QWidget * parent) : QMenu(title, parent) { Index: src/frontends/qt4/IconPalette.h =================================================================== --- src/frontends/qt4/IconPalette.h (revision 18812) +++ src/frontends/qt4/IconPalette.h (working copy) @@ -17,46 +17,19 @@ #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: IconPalette(QWidget * parent); void addButton(QAction *); - + void moveEvent(QMoveEvent *); + bool event(QEvent *); public Q_SLOTS: void updateParent(); @@ -67,7 +40,6 @@ protected: void showEvent(QShowEvent * event); void hideEvent(QHideEvent * event); - void paintEvent(QPaintEvent * event); private Q_SLOTS: virtual void clicked(QAction *); @@ -75,9 +47,10 @@ private: QGridLayout * layout_; QList<QAction *> actions_; + bool detached_; + bool active_; }; -#endif // QT_VERSION >= QT_VERSION_CHECK(4, 2, 0) /** * Popup menu for a toolbutton. Index: src/frontends/qt4/QLToolbar.cpp =================================================================== --- src/frontends/qt4/QLToolbar.cpp (revision 18813) +++ src/frontends/qt4/QLToolbar.cpp (working copy) @@ -212,15 +212,7 @@ 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(); @@ -239,18 +231,9 @@ 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;