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-11-19 17:12:33 +00:00
|
|
|
#include "controllers/ControlMath.h" // for find_xpm
|
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>
|
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-17 17:55:36 +00:00
|
|
|
#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
|
|
|
|
|
2007-04-19 22:01:28 +00:00
|
|
|
IconPalette::IconPalette(QWidget * parent)
|
2007-04-19 20:29:27 +00:00
|
|
|
: QWidget(parent, Qt::Popup)
|
|
|
|
{
|
|
|
|
layout_ = new QGridLayout(this);
|
2007-06-17 17:55:36 +00:00
|
|
|
layout->setSpacing(0);
|
|
|
|
layout->setMargin(3);
|
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);
|
|
|
|
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-04-19 22:01:28 +00:00
|
|
|
void IconPalette::clicked(QAction * action)
|
2007-04-19 20:29:27 +00:00
|
|
|
{
|
|
|
|
triggered(action);
|
|
|
|
setVisible(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-19 22:01:28 +00:00
|
|
|
void IconPalette::showEvent(QShowEvent * event)
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
move(gpos.x() + hoffset, gpos.y() + voffset);
|
|
|
|
QWidget::showEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-19 22:01:28 +00:00
|
|
|
void IconPalette::hideEvent(QHideEvent * event )
|
2007-04-19 20:29:27 +00:00
|
|
|
{
|
|
|
|
visible(false);
|
|
|
|
QWidget::hideEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-19 22:01:28 +00:00
|
|
|
void IconPalette::updateParent()
|
2007-04-19 20:29:27 +00:00
|
|
|
{
|
|
|
|
bool enable = false;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
parentWidget()->setEnabled(enable);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-19 22:01:28 +00:00
|
|
|
void IconPalette::paintEvent(QPaintEvent * event)
|
2007-04-19 20:29:27 +00:00
|
|
|
{
|
|
|
|
// 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);
|
|
|
|
}
|
2007-06-17 17:55:36 +00:00
|
|
|
#endif // QT_VERSION >= 0x040200
|
2007-04-19 20:29:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
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-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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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"
|