2006-03-05 17:24:44 +00:00
|
|
|
/**
|
2007-04-26 03:53:02 +00:00
|
|
|
* \file IconPalette.cpp
|
2006-03-05 17:24:44 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
2007-04-19 22:01:28 +00:00
|
|
|
* \author Edwin Leuven
|
2006-03-05 17:24:44 +00:00
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-04-26 03:53:02 +00:00
|
|
|
#include "IconPalette.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
#include "qt_helpers.h"
|
2006-08-17 08:49:05 +00:00
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
#include <QPixmap>
|
2006-05-08 11:44:37 +00:00
|
|
|
#include <QGridLayout>
|
2007-04-19 20:29:27 +00:00
|
|
|
#include <QToolButton>
|
2006-08-17 08:49:05 +00:00
|
|
|
#include <QToolTip>
|
2007-04-19 20:29:27 +00:00
|
|
|
#include <QToolBar>
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QDesktopWidget>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QStyle>
|
|
|
|
#include <QStyleOptionFrame>
|
2007-06-17 17:55:36 +00:00
|
|
|
#include <QMouseEvent>
|
2007-06-21 09:51:50 +00:00
|
|
|
#include <QVBoxLayout>
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
namespace lyx {
|
2006-11-19 17:12:33 +00:00
|
|
|
namespace frontend {
|
2006-10-21 00:16:43 +00:00
|
|
|
|
2007-06-21 09:51:50 +00:00
|
|
|
TearOff::TearOff(QWidget * parent)
|
|
|
|
: QWidget(parent)
|
2007-06-17 17:55:36 +00:00
|
|
|
{
|
2007-06-21 09:51:50 +00:00
|
|
|
highlighted_ = false;
|
|
|
|
// + 2 because the default is a bit tight, see also:
|
|
|
|
// http://trolltech.com/developer/task-tracker/index_html?id=167954&method=entry
|
|
|
|
setMinimumHeight(style()->pixelMetric(QStyle::PM_MenuTearoffHeight) + 2);
|
|
|
|
setToolTip(qt_("Click to detach"));
|
|
|
|
// trigger tooltip (children of popups do not receive mousemove events)
|
|
|
|
setMouseTracking(true);
|
2007-06-17 17:55:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-10 20:14:54 +00:00
|
|
|
void TearOff::mouseReleaseEvent(QMouseEvent * /*event*/)
|
2007-06-17 17:55:36 +00:00
|
|
|
{
|
2007-06-21 09:51:50 +00:00
|
|
|
// signal
|
|
|
|
tearOff();
|
2007-06-17 17:55:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-21 09:51:50 +00:00
|
|
|
void TearOff::enterEvent(QEvent * event)
|
2007-06-17 17:55:36 +00:00
|
|
|
{
|
2007-06-21 09:51:50 +00:00
|
|
|
highlighted_ = true;
|
|
|
|
update();
|
|
|
|
event->ignore();
|
2007-06-17 17:55:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-21 09:51:50 +00:00
|
|
|
void TearOff::leaveEvent(QEvent * event)
|
2007-06-17 17:55:36 +00:00
|
|
|
{
|
2007-06-21 09:51:50 +00:00
|
|
|
highlighted_ = false;
|
|
|
|
update();
|
|
|
|
event->ignore();
|
2007-06-17 17:55:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-10 20:14:54 +00:00
|
|
|
void TearOff::paintEvent(QPaintEvent * /*event*/)
|
2007-06-17 17:55:36 +00:00
|
|
|
{
|
2007-06-21 09:51:50 +00:00
|
|
|
QPainter p(this);
|
|
|
|
const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
|
|
|
|
QStyleOptionMenuItem menuOpt;
|
|
|
|
menuOpt.initFrom(this);
|
|
|
|
menuOpt.palette = palette();
|
|
|
|
menuOpt.state = QStyle::State_None;
|
|
|
|
menuOpt.checkType = QStyleOptionMenuItem::NotCheckable;
|
|
|
|
menuOpt.menuRect = rect();
|
|
|
|
menuOpt.maxIconWidth = 0;
|
|
|
|
menuOpt.tabWidth = 0;
|
|
|
|
menuOpt.menuItemType = QStyleOptionMenuItem::TearOff;
|
|
|
|
menuOpt.rect.setRect(fw, fw, width() - (fw * 2),
|
|
|
|
style()->pixelMetric(QStyle::PM_MenuTearoffHeight, 0, this));
|
|
|
|
p.setClipRect(menuOpt.rect);
|
|
|
|
menuOpt.state = QStyle::State_None;
|
|
|
|
if (highlighted_)
|
|
|
|
menuOpt.state |= QStyle::State_Selected;
|
|
|
|
style()->drawControl(QStyle::CE_MenuTearoff, &menuOpt, &p, this);
|
2007-06-17 17:55:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-19 22:01:28 +00:00
|
|
|
IconPalette::IconPalette(QWidget * parent)
|
2007-06-21 09:51:50 +00:00
|
|
|
: QWidget(parent, Qt::Popup), tornoff_(false)
|
2007-04-19 20:29:27 +00:00
|
|
|
{
|
2007-06-21 09:51:50 +00:00
|
|
|
QVBoxLayout * v = new QVBoxLayout(this);
|
|
|
|
v->setMargin(0);
|
|
|
|
v->setSpacing(0);
|
|
|
|
layout_ = new QGridLayout;
|
2007-06-17 18:24:27 +00:00
|
|
|
layout_->setSpacing(0);
|
2007-06-21 09:51:50 +00:00
|
|
|
const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
|
|
|
|
layout_->setMargin(fw);
|
|
|
|
tearoffwidget_ = new TearOff(this);
|
|
|
|
connect(tearoffwidget_, SIGNAL(tearOff()), this, SLOT(tearOff()));
|
|
|
|
v->addWidget(tearoffwidget_);
|
|
|
|
v->addLayout(layout_);
|
2007-04-19 20:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-19 22:01:28 +00:00
|
|
|
void IconPalette::addButton(QAction * action)
|
2007-04-19 20:29:27 +00:00
|
|
|
{
|
|
|
|
actions_.push_back(action);
|
|
|
|
QToolButton * tb = new QToolButton;
|
|
|
|
tb->setAutoRaise(true);
|
|
|
|
tb->setDefaultAction(action);
|
2008-01-16 20:51:00 +00:00
|
|
|
QToolButton * pb = qobject_cast<QToolButton *>(parentWidget());
|
|
|
|
tb->setIconSize(pb->iconSize());
|
2007-06-21 09:51:50 +00:00
|
|
|
// trigger tooltip (children of popups do not receive mousemove events)
|
|
|
|
tb->setMouseTracking(true);
|
|
|
|
|
2007-04-19 20:29:27 +00:00
|
|
|
connect(tb, SIGNAL(triggered(QAction *)),
|
|
|
|
this, SLOT(clicked(QAction *)));
|
|
|
|
QToolBar * toolbar = qobject_cast<QToolBar *>(parentWidget()->parentWidget());
|
|
|
|
connect(toolbar, SIGNAL(iconSizeChanged(const QSize &)),
|
|
|
|
tb, SLOT(setIconSize(const QSize &)));
|
|
|
|
|
|
|
|
int const i = actions_.size();
|
|
|
|
int const ncols = qMin(6, i);
|
|
|
|
int const row = (i - 1)/ncols + 1;
|
|
|
|
int const col = qMax(1, i - (row - 1) * 6);
|
|
|
|
layout_->addWidget(tb, row, col);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-21 09:51:50 +00:00
|
|
|
void IconPalette::tearOff()
|
|
|
|
{
|
|
|
|
blockSignals(true);
|
|
|
|
hide();
|
|
|
|
setWindowFlags(Qt::Tool);
|
|
|
|
tornoff_ = true;
|
|
|
|
tearoffwidget_->setVisible(!tornoff_);
|
|
|
|
show();
|
|
|
|
blockSignals(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-19 22:01:28 +00:00
|
|
|
void IconPalette::clicked(QAction * action)
|
2007-04-19 20:29:27 +00:00
|
|
|
{
|
|
|
|
triggered(action);
|
2007-06-21 09:51:50 +00:00
|
|
|
if (!tornoff_)
|
|
|
|
setVisible(false);
|
2007-04-19 20:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-10 20:14:54 +00:00
|
|
|
void IconPalette::showEvent(QShowEvent * /*event*/)
|
2007-04-19 20:29:27 +00:00
|
|
|
{
|
2007-06-21 09:51:50 +00:00
|
|
|
resize(sizeHint());
|
|
|
|
setMaximumSize(sizeHint());
|
|
|
|
|
2007-04-19 20:29:27 +00:00
|
|
|
int hoffset = - parentWidget()->pos().x();
|
|
|
|
int voffset = - parentWidget()->pos().y();
|
|
|
|
int const parwidth = parentWidget()->geometry().width();
|
|
|
|
int const parheight = parentWidget()->geometry().height();
|
|
|
|
|
|
|
|
// vertical toolbar?
|
|
|
|
QToolBar * toolbar = qobject_cast<QToolBar *>(parentWidget()->parentWidget());
|
|
|
|
if (toolbar && toolbar->orientation() == Qt::Vertical) {
|
|
|
|
hoffset += parwidth;
|
|
|
|
voffset -= parheight;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect const screen = qApp->desktop()->availableGeometry(this);
|
|
|
|
QPoint const gpos = parentWidget()->mapToGlobal(
|
|
|
|
parentWidget()->geometry().bottomLeft());
|
|
|
|
|
|
|
|
// space to the right?
|
|
|
|
if (gpos.x() + hoffset + width() > screen.width()) {
|
|
|
|
hoffset -= 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 (toolbar && toolbar->orientation() == Qt::Horizontal)
|
|
|
|
voffset -= parheight;
|
|
|
|
else
|
|
|
|
voffset += parheight;
|
|
|
|
}
|
|
|
|
|
2007-06-21 09:51:50 +00:00
|
|
|
QRect r = rect();
|
|
|
|
r.moveTo(gpos.x() + hoffset, gpos.y() + voffset);
|
|
|
|
setGeometry(r);
|
2007-04-19 20:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-19 22:01:28 +00:00
|
|
|
void IconPalette::hideEvent(QHideEvent * event )
|
2007-04-19 20:29:27 +00:00
|
|
|
{
|
|
|
|
QWidget::hideEvent(event);
|
2007-06-21 09:51:50 +00:00
|
|
|
visible(false);
|
|
|
|
if (tornoff_) {
|
|
|
|
setWindowFlags(Qt::Popup);
|
|
|
|
tornoff_ = false;
|
|
|
|
tearoffwidget_->setVisible(!tornoff_);
|
|
|
|
}
|
2007-04-19 20:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-10 20:14:54 +00:00
|
|
|
void IconPalette::paintEvent(QPaintEvent * /*event*/)
|
2007-04-19 20:29:27 +00:00
|
|
|
{
|
|
|
|
// draw border
|
|
|
|
const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
|
2007-06-21 09:51:50 +00:00
|
|
|
if (fw && !tornoff_) {
|
|
|
|
QPainter p(this);
|
2007-04-19 20:29:27 +00:00
|
|
|
QRegion borderReg;
|
|
|
|
borderReg += QRect(0, 0, fw, height()); //left
|
2007-06-21 09:51:50 +00:00
|
|
|
borderReg += QRect(width() - fw, 0, fw, height()); //right
|
2007-04-19 20:29:27 +00:00
|
|
|
borderReg += QRect(0, 0, width(), fw); //top
|
2007-06-21 09:51:50 +00:00
|
|
|
borderReg += QRect(0, height() - fw, width(), fw); //bottom
|
2007-04-19 20:29:27 +00:00
|
|
|
p.setClipRegion(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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ButtonMenu::ButtonMenu(const QString & title, QWidget * parent)
|
|
|
|
: QMenu(title, parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ButtonMenu::add(QAction * action)
|
|
|
|
{
|
|
|
|
addAction(action);
|
|
|
|
actions_.push_back(action);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ButtonMenu::updateParent()
|
|
|
|
{
|
|
|
|
bool enable = false;
|
2007-10-01 13:17:56 +00:00
|
|
|
// FIXME: so this is commented out for speed considerations
|
|
|
|
// true fix is to repair the updating mechanism of the toolbar
|
|
|
|
#if 0
|
2007-05-28 22:27:45 +00:00
|
|
|
for (int i = 0; i < actions_.size(); ++i)
|
2007-04-19 20:29:27 +00:00
|
|
|
if (actions_.at(i)->isEnabled()) {
|
|
|
|
enable = true;
|
|
|
|
break;
|
|
|
|
}
|
2007-10-01 13:17:56 +00:00
|
|
|
#else
|
|
|
|
// we check only the first action to enable/disable the menu
|
2007-10-01 17:40:42 +00:00
|
|
|
if (!actions_.isEmpty())
|
2007-10-01 13:17:56 +00:00
|
|
|
enable = actions_.at(0)->isEnabled();
|
|
|
|
#endif
|
2007-04-19 20:29:27 +00:00
|
|
|
|
|
|
|
parentWidget()->setEnabled(enable);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-19 17:12:33 +00:00
|
|
|
} // namespace frontend
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|
|
|
|
|
2007-04-26 03:53:02 +00:00
|
|
|
#include "IconPalette_moc.cpp"
|