File indexing completed on 2022-12-06 18:50:09

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2012 Martin Kuettler <martin.kuettler@gmail.com>
0004 */
0005 
0006 #include "actionbar.h"
0007 #include "worksheet.h"
0008 #include "worksheetentry.h"
0009 #include "worksheettoolbutton.h"
0010 #include "worksheetview.h"
0011 
0012 #include <QGraphicsProxyWidget>
0013 
0014 ActionBar::ActionBar(WorksheetEntry* parent)
0015     : QGraphicsObject(parent)
0016 {
0017     m_pos = 0;
0018     m_height = 0;
0019     QPointF p = worksheet()->worksheetView()->viewRect().topRight();
0020     qreal w = qMin(parent->size().width() - WorksheetEntry::RightMargin,
0021                    parent->mapFromScene(p).x());
0022     setPos(w, 0);
0023     connect(worksheet()->worksheetView(), SIGNAL(viewRectChanged(QRectF)),
0024             this, SLOT(updatePosition()));
0025 }
0026 
0027 WorksheetToolButton* ActionBar::addButton(const QIcon& icon, const QString& toolTip,
0028                                    QObject* receiver, const char* method )
0029 {
0030     WorksheetToolButton* button = new WorksheetToolButton(this);
0031     button->setIcon(icon);
0032     button->setIconScale(worksheet()->renderer()->scale());
0033     button->setToolTip(toolTip);
0034     if (receiver && method)
0035         connect(button, SIGNAL(clicked()), receiver, method);
0036     m_pos -= button->width() + 2;
0037     m_height = (m_height > button->height()) ? m_height : button->height();
0038     button->setPos(m_pos, 4);
0039     m_buttons.append(button);
0040     return button;
0041 }
0042 
0043 void ActionBar::addSpace()
0044 {
0045     m_pos -= 8;
0046 }
0047 
0048 void ActionBar::updatePosition()
0049 {
0050     if (!parentEntry())
0051         return;
0052     QPointF p = worksheet()->worksheetView()->viewRect().topRight();
0053     qreal w = qMin(parentEntry()->size().width() - WorksheetEntry::RightMargin,
0054                    parentEntry()->mapFromScene(p).x());
0055     setPos(w, 0);
0056     const qreal scale = worksheet()->renderer()->scale();
0057     foreach(WorksheetToolButton* button, m_buttons) {
0058         button->setIconScale(scale);
0059     }
0060 }
0061 
0062 WorksheetEntry* ActionBar::parentEntry()
0063 {
0064     return qobject_cast<WorksheetEntry*>(parentObject());
0065 }
0066 
0067 QRectF ActionBar::boundingRect() const
0068 {
0069     return QRectF(m_pos, 0, -m_pos, m_height);
0070 }
0071 
0072 void ActionBar::paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*)
0073 {
0074 }
0075 
0076 Worksheet* ActionBar::worksheet()
0077 {
0078     return qobject_cast<Worksheet*>(scene());
0079 }