KReport

KReportBoundedTextItem.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2014 Adam Pigg <adam@piggz.co.uk>
3 Copyright (C) 2016 Jarosław Staniek <staniek@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#include "KReportBoundedTextItem.h"
22#include <QPainter>
23#include <QStyleOptionGraphicsItem>
24#include <QKeyEvent>
25
26BoundedTextItem::BoundedTextItem(QGraphicsItem* parent): QGraphicsTextItem(parent)
27 , m_backgroundOpacity(1.0)
28{
29 setTextInteractionFlags(Qt::TextEditorInteraction);
30 setCursor(QCursor(Qt::IBeamCursor));
31}
32
33QRectF BoundedTextItem::boundingRect() const
34{
35 if (parentItem()) {
36 return parentItem()->boundingRect();
37 }
38
40}
41
42void BoundedTextItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *o, QWidget *w)
43{
44 QColor bg = m_backgroundColor.isValid() ? m_backgroundColor : o->palette.base().color();
45 bg.setAlphaF(m_backgroundOpacity * 0.01);
46
47 QColor fc = m_foregroundColor.isValid() ? m_foregroundColor : o->palette.text().color();
48 painter->setBrush(bg);
49 painter->setPen(fc);
50
51 painter->drawRect(boundingRect());
52
54 opt.state &= ~QStyle::State_HasFocus;
55 QGraphicsTextItem::paint(painter, &opt, w);
56}
57
58void BoundedTextItem::setBackgroudColor(const QColor& bc)
59{
60 m_backgroundColor = bc;
61}
62
63void BoundedTextItem::setForegroundColor(const QColor& fc)
64{
65 m_foregroundColor = fc;
66}
67
68void BoundedTextItem::setDisplayFont(const QFont& f)
69{
70 m_font = f;
71 setFont(m_font);
72}
73
75{
76 return m_backgroundOpacity;
77}
78
80{
81 m_backgroundOpacity = std::min(1.0, std::max(0.0, opacity));
82}
83
84void BoundedTextItem::keyReleaseEvent(QKeyEvent* event)
85{
86 if (event->key() == Qt::Key_Escape) {
87 emit exitEditMode();
88 } else {
90 }
91}
void setBackgroudOpacity(qreal opacity)
Sets background opacity, 0..1.0.
bool isValid() const const
void setAlphaF(float alpha)
virtual QRectF boundingRect() const const=0
QGraphicsItem * parentItem() const const
virtual bool event(QEvent *ev) override
virtual QRectF boundingRect() const const override
virtual void keyReleaseEvent(QKeyEvent *event) override
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
void setFont(const QFont &font)
void drawRect(const QRect &rectangle)
void setBrush(Qt::BrushStyle style)
void setPen(Qt::PenStyle style)
IBeamCursor
Key_Escape
TextEditorInteraction
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:06:04 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.