Debian Codegraph @

avogadro/qtgui/elementitem_p.cpp

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #include "elementitem_p.h"
7 #include "elementtranslator.h"
8
9 #include <avogadro/core/elements.h>
10
11 #include <QtGui/QFont>
12 #include <QtGui/QFontMetrics>
13 #include <QtGui/QPainter>
14 #include <QtWidgets/QGraphicsSceneMouseEvent>
15 #include <QtWidgets/QStyleOption>
16
17 namespace Avogadro::QtGui {
18
19 using Core::Elements;
20
21 ElementItem::ElementItem(int elementNumber)
22 : m_valid(false), m_color(Qt::white), m_width(26), m_height(26),
23 m_element(elementNumber)
24 {
25 // Want these items to be selectable
26 setFlags(QGraphicsItem::ItemIsSelectable);
27
28 m_symbol = Elements::symbol(static_cast<unsigned char>(m_element));
29 if (!m_symbol.isEmpty())
30 m_valid = true;
31 const unsigned char* color =
32 Elements::color(static_cast<unsigned char>(m_element));
33 if (color) {
34 m_color.setRgb(static_cast<int>(color[0]), static_cast<int>(color[1]),
35 static_cast<int>(color[2]));
36 }
37 // Set some custom data to make it easy to figure out which element we are
38 setData(0, m_element);
39 }
40
41 ElementItem::~ElementItem() {}
42
43 QRectF ElementItem::boundingRect() const
44 {
45 return QRectF(-m_width / 2, -m_height / 2, m_width, m_height);
46 }
47
48 QPainterPath ElementItem::shape() const
49 {
50 QPainterPath path;
51 path.addRect(-m_width / 2, -m_height / 2, m_width, m_height);
52 return path;
53 }
54
55 void ElementItem::paint(QPainter* painter, const QStyleOptionGraphicsItem*,
56 QWidget*)
57 {
58 if (!m_valid)
59 return;
60
61 // Fill the rectangle with the element colour
62 QColor bgColor;
63 QPen pen;
64 if (isSelected()) {
65 bgColor = QColor(m_color).lighter(150);
66 pen.setColor(QColor(m_color).darker(150));
67 pen.setWidth(4);
68 } else {
69 bgColor = QColor(m_color);
70 }
71 painter->setPen(pen);
72 painter->setBrush(bgColor);
73 QRectF rect(-m_width / 2, -m_height / 2, m_width, m_height);
74 painter->drawRect(rect);
75 // Handle the case where the item is selected
76 if (bgColor.value() < 150)
77 pen.setColor(Qt::white);
78 else
79 pen.setColor(Qt::black);
80 painter->setPen(pen);
81 painter->drawText(rect, Qt::AlignCenter, m_symbol);
82 }
83
84 } // namespace Avogadro::QtGui