File indexing completed on 2024-05-19 04:44:33

0001 /* This file is part of the KDE project
0002   Copyright (C) 2014 Adam Pigg <adam@piggz.co.uk>
0003   Copyright (C) 2016 Jarosław Staniek <staniek@kde.org>
0004 
0005   This library is free software; you can redistribute it and/or
0006   modify it under the terms of the GNU Library General Public
0007   License as published by the Free Software Foundation; either
0008   version 2.1 of the License, or (at your option) any later version.
0009 
0010   This library is distributed in the hope that it will be useful,
0011   but WITHOUT ANY WARRANTY; without even the implied warranty of
0012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013   Library General Public License for more details.
0014 
0015   You should have received a copy of the GNU Library General Public License
0016   along with this library; see the file COPYING.LIB.  If not, write to
0017   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018   Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #include "KReportBoundedTextItem.h"
0022 #include <QPainter>
0023 #include <QStyleOptionGraphicsItem>
0024 #include <QKeyEvent>
0025 
0026 BoundedTextItem::BoundedTextItem(QGraphicsItem* parent): QGraphicsTextItem(parent)
0027   , m_backgroundOpacity(1.0)
0028 {
0029     setTextInteractionFlags(Qt::TextEditorInteraction);
0030     setCursor(QCursor(Qt::IBeamCursor));
0031 }
0032 
0033 QRectF BoundedTextItem::boundingRect() const
0034 {
0035     if (parentItem()) {
0036         return parentItem()->boundingRect();
0037     }
0038 
0039     return QGraphicsTextItem::boundingRect();
0040 }
0041 
0042 void BoundedTextItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *o, QWidget *w)
0043 {
0044     QColor bg = m_backgroundColor.isValid() ? m_backgroundColor : o->palette.base().color();
0045     bg.setAlphaF(m_backgroundOpacity * 0.01);
0046 
0047     QColor fc = m_foregroundColor.isValid() ? m_foregroundColor : o->palette.text().color();
0048     painter->setBrush(bg);
0049     painter->setPen(fc);
0050 
0051     painter->drawRect(boundingRect());
0052 
0053     QStyleOptionGraphicsItem opt(*o);
0054     opt.state &= ~QStyle::State_HasFocus;
0055     QGraphicsTextItem::paint(painter, &opt, w);
0056 }
0057 
0058 void BoundedTextItem::setBackgroudColor(const QColor& bc)
0059 {
0060     m_backgroundColor = bc;
0061 }
0062 
0063 void BoundedTextItem::setForegroundColor(const QColor& fc)
0064 {
0065     m_foregroundColor = fc;
0066 }
0067 
0068 void BoundedTextItem::setDisplayFont(const QFont& f)
0069 {
0070     m_font = f;
0071     setFont(m_font);
0072 }
0073 
0074 qreal BoundedTextItem::backgroudOpacity() const
0075 {
0076     return m_backgroundOpacity;
0077 }
0078 
0079 void BoundedTextItem::setBackgroudOpacity(qreal opacity)
0080 {
0081     m_backgroundOpacity = std::min(1.0, std::max(0.0, opacity));
0082 }
0083 
0084 void BoundedTextItem::keyReleaseEvent(QKeyEvent* event)
0085 {
0086     if (event->key() == Qt::Key_Escape) {
0087         emit exitEditMode();
0088     } else {
0089         QGraphicsTextItem::keyReleaseEvent(event);
0090     }
0091 }