KReport

KReportBoundedTextItem.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2014 Adam Pigg <[email protected]>
3  Copyright (C) 2016 JarosÅ‚aw Staniek <[email protected]>
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 
26 BoundedTextItem::BoundedTextItem(QGraphicsItem* parent): QGraphicsTextItem(parent)
27  , m_backgroundOpacity(1.0)
28 {
29  setTextInteractionFlags(Qt::TextEditorInteraction);
30  setCursor(QCursor(Qt::IBeamCursor));
31 }
32 
33 QRectF BoundedTextItem::boundingRect() const
34 {
35  if (parentItem()) {
36  return parentItem()->boundingRect();
37  }
38 
40 }
41 
42 void 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 
58 void BoundedTextItem::setBackgroudColor(const QColor& bc)
59 {
60  m_backgroundColor = bc;
61 }
62 
63 void BoundedTextItem::setForegroundColor(const QColor& fc)
64 {
65  m_foregroundColor = fc;
66 }
67 
68 void 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 
84 void BoundedTextItem::keyReleaseEvent(QKeyEvent* event)
85 {
86  if (event->key() == Qt::Key_Escape) {
87  emit exitEditMode();
88  } else {
90  }
91 }
QGraphicsItem * parentItem() const const
void setPen(const QColor &color)
void drawRect(const QRectF &rectangle)
virtual void keyReleaseEvent(QKeyEvent *event) override
void setFont(const QFont &font)
qreal backgroudOpacity() const
TextEditorInteraction
virtual QRectF boundingRect() const const=0
void setBackgroudOpacity(qreal opacity)
Sets background opacity, 0..1.0.
void setAlphaF(qreal alpha)
IBeamCursor
virtual bool event(QEvent *ev) override
Key_Escape
void setBrush(const QBrush &brush)
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
virtual QRectF boundingRect() const const override
bool isValid() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Fri Sep 29 2023 04:08:56 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.