QElectroTech  0.70
helpercell.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2006-2019 The QElectroTech Team
3  This file is part of QElectroTech.
4 
5  QElectroTech is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 2 of the License, or
8  (at your option) any later version.
9 
10  QElectroTech 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
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "helpercell.h"
19 
24 HelperCell::HelperCell(QGraphicsItem *parent) :
25  QGraphicsObject(parent),
26  QGraphicsLayoutItem(),
27  background_color(Qt::white),
28  foreground_color(Qt::black),
29  label(),
30  orientation(Qt::Horizontal),
31  index(-1)
32 {
33  setGraphicsItem(this);
34  setFlag(QGraphicsItem::ItemIsSelectable, false);
35 }
36 
41 }
42 
48 void HelperCell::setGeometry(const QRectF &g) {
49  prepareGeometryChange();
50  QGraphicsLayoutItem::setGeometry(g);
51  setPos(g.topLeft());
52 }
53 
59 QSizeF HelperCell::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const {
60  Q_UNUSED(which);
61  return(constraint);
62 }
63 
67 QRectF HelperCell::boundingRect() const {
68  return QRectF(QPointF(0,0), geometry().size());
69 }
70 
77 void HelperCell::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
78  Q_UNUSED(option);
79  Q_UNUSED(widget);
80 
81  QRectF drawing_rectangle(QPointF(0, 0), geometry().size());
82 
83  painter -> setPen(Qt::black);
84  painter -> setBrush(background_color);
85  painter -> drawRect(drawing_rectangle);
86 
87  painter -> setPen(foreground_color);
88  painter -> drawText(drawing_rectangle, Qt::AlignHCenter | Qt::AlignVCenter, label);
89 }
90 
95  if (type == QET::Absolute) {
96  background_color = QColor("#C0FFFF");
97  foreground_color = Qt::black;
98  } else if (type == QET::RelativeToTotalLength) {
99  background_color = QColor("#FFA858");
100  foreground_color = Qt::black;
101  } else if (type == QET::RelativeToRemainingLength) {
102  background_color = QColor("#FFC0C0");
103  foreground_color = Qt::black;
104  }
105 }
106 
110 void HelperCell::setActions(const QList<QAction *> &actions) {
111  actions_ = actions;
112 }
113 
117 QList<QAction *> HelperCell::actions() const {
118  return actions_;
119 }
120 
125 void HelperCell::setLabel(const QString &text, bool set_as_tooltip) {
126  label = text;
127  if (set_as_tooltip) {
128  setToolTip(text);
129  }
130 }
131 
136 void HelperCell::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
137  if (actions_.isEmpty()) return;
138 
139  QMenu context_menu;
140  foreach (QAction *action, actions_) {
141  context_menu.addAction(action);
142  }
143  emit(contextMenuTriggered(this));
144  context_menu.exec(event -> screenPos());
145 }
146 
150 void HelperCell::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *) {
151 
152  emit(doubleClicked(this));
153 }
the length is just a fraction of the length that is still available when other types of lengths have ...
Definition: qet.h:146
~HelperCell() override
Definition: helpercell.cpp:40
QSizeF sizeHint(Qt::SizeHint, const QSizeF &=QSizeF()) const override
Definition: helpercell.cpp:59
the length is absolute and should be applied as is
Definition: qet.h:144
TitleBlockColumnLength
enum used to specify the type of a length
Definition: qet.h:143
void contextMenuTriggered(HelperCell *)
QColor background_color
Background color when rendering this cell.
Definition: helpercell.h:40
virtual QList< QAction * > actions() const
Definition: helpercell.cpp:117
void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *=nullptr) override
Definition: helpercell.cpp:77
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *) override
Definition: helpercell.cpp:150
QRectF boundingRect() const override
Definition: helpercell.cpp:67
virtual void setActions(const QList< QAction *> &)
Definition: helpercell.cpp:110
virtual void setType(QET::TitleBlockColumnLength)
Definition: helpercell.cpp:94
the length is just a fraction of the total available length
Definition: qet.h:145
QColor foreground_color
Text color when rendering this cell.
Definition: helpercell.h:41
virtual void setLabel(const QString &text, bool=true)
Definition: helpercell.cpp:125
HelperCell(QGraphicsItem *=nullptr)
Definition: helpercell.cpp:24
void doubleClicked(HelperCell *)
void contextMenuEvent(QGraphicsSceneContextMenuEvent *) override
Definition: helpercell.cpp:136
void setGeometry(const QRectF &) override
Definition: helpercell.cpp:48
QString label
Label displayed in this cell.
Definition: helpercell.h:42
Horizontal segment.
Definition: qet.h:87
QList< QAction * > actions_
List of actions displayed by the context menu.
Definition: helpercell.h:66