| 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 "elementdetail_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 |
ElementDetail::ElementDetail(int elementNumber) |
| 22 |
: m_width(100), m_height(70), m_element(elementNumber) |
| 23 |
{ |
| 24 |
} |
| 25 |
|
| 26 |
QRectF ElementDetail::boundingRect() const |
| 27 |
{ |
| 28 |
return QRectF(-m_width / 2, -m_height / 2, m_width, m_height); |
| 29 |
} |
| 30 |
|
| 31 |
QPainterPath ElementDetail::shape() const |
| 32 |
{ |
| 33 |
QPainterPath path; |
| 34 |
path.addRect(-m_width / 2, -m_height / 2, m_width, m_height); |
| 35 |
return path; |
| 36 |
} |
| 37 |
|
| 38 |
void ElementDetail::paint(QPainter* painter, const QStyleOptionGraphicsItem*, |
| 39 |
QWidget*) |
| 40 |
{ |
| 41 |
// Set up a font object and get its height |
| 42 |
QFont font(QStringLiteral("sans-serif")); |
| 43 |
font.setPixelSize(12); |
| 44 |
painter->setFont(font); |
| 45 |
QFontMetrics fm(font); |
| 46 |
int pixelHeight = fm.height(); |
| 47 |
|
| 48 |
QString symbol = Elements::symbol(static_cast<unsigned char>(m_element)); |
| 49 |
QString name(ElementTranslator::name(m_element)); |
| 50 |
QString mass = QStringLiteral("%L1").arg( |
| 51 |
Elements::mass(static_cast<unsigned char>(m_element)), 0, 'f', 3); |
| 52 |
|
| 53 |
const unsigned char* colorTmp = |
| 54 |
Elements::color(static_cast<unsigned char>(m_element)); |
| 55 |
QColor color(Qt::white); |
| 56 |
if (colorTmp) { |
| 57 |
color.setRgb(static_cast<int>(colorTmp[0]), static_cast<int>(colorTmp[1]), |
| 58 |
static_cast<int>(colorTmp[2])); |
| 59 |
} |
| 60 |
|
| 61 |
// Draw the element detail border and fill with the element colour |
| 62 |
painter->setBrush(color); |
| 63 |
painter->setPen(Qt::black); |
| 64 |
QRectF rect(-m_width / 2, -m_height / 2, m_width, m_height); |
| 65 |
painter->drawRect(rect); |
| 66 |
|
| 67 |
// Draw the element symbol bigger than everything else |
| 68 |
font.setPixelSize(24); |
| 69 |
QFontMetrics fm2(font); |
| 70 |
pixelHeight = fm2.height(); |
| 71 |
int pixelWidth = fm2.horizontalAdvance(symbol); |
| 72 |
painter->setFont(font); |
| 73 |
QRectF symbolRect(-10, -m_height / 2 + 8, pixelWidth, pixelHeight); |
| 74 |
painter->drawText(symbolRect, Qt::AlignCenter, symbol); |
| 75 |
|
| 76 |
// Reduce the font size to draw the other parts |
| 77 |
font.setPixelSize(12); |
| 78 |
int pixelHeight2 = fm.height(); |
| 79 |
painter->setFont(font); |
| 80 |
|
| 81 |
// I don't seem to be able to get a nice, cross platform layout working here |
| 82 |
// I would really like to figure out how to make this more portable - ideas? |
| 83 |
#ifdef Q_OS_MAC |
| 84 |
// Draw the proton number |
| 85 |
QRectF protonNumberRect(-m_width / 2 - 10, -m_height / 2 + 8, m_width / 2, |
| 86 |
pixelHeight2); |
| 87 |
painter->drawText(protonNumberRect, Qt::AlignRight, |
| 88 |
QString::number(m_element)); |
| 89 |
|
| 90 |
// Draw the mass |
| 91 |
QRectF massNumberRect(-m_width / 2, -m_height / 2 + 8 + pixelHeight * 1.1, |
| 92 |
m_width, pixelHeight2); |
| 93 |
painter->drawText(massNumberRect, Qt::AlignCenter, mass); |
| 94 |
|
| 95 |
// Finally the full element name |
| 96 |
QRectF nameRect(-m_width / 2, |
| 97 |
-m_height / 2 + 4 + pixelHeight * 1.1 + pixelHeight2, m_width, |
| 98 |
pixelHeight); |
| 99 |
painter->drawText(nameRect, Qt::AlignCenter, name); |
| 100 |
#else |
| 101 |
// Draw the proton number |
| 102 |
QRectF protonNumberRect(-m_width / 2 - 10, -m_height / 2 + 16, m_width / 2, |
| 103 |
pixelHeight2); |
| 104 |
painter->drawText(protonNumberRect, Qt::AlignRight, |
| 105 |
QString::number(m_element)); |
| 106 |
|
| 107 |
// Draw the mass |
| 108 |
QRectF massNumberRect(-m_width / 2, -m_height / 2 + 4 + pixelHeight, m_width, |
| 109 |
pixelHeight2); |
| 110 |
painter->drawText(massNumberRect, Qt::AlignCenter, mass); |
| 111 |
|
| 112 |
// Finally the full element name |
| 113 |
QRectF nameRect(-m_width / 2, |
| 114 |
-m_height / 2 + pixelHeight + 0.8 * pixelHeight2, m_width, |
| 115 |
pixelHeight); |
| 116 |
painter->drawText(nameRect, Qt::AlignCenter, name); |
| 117 |
#endif |
| 118 |
} |
| 119 |
|
| 120 |
void ElementDetail::setElement(int element) |
| 121 |
{ |
| 122 |
if (m_element != element) { |
| 123 |
m_element = element; |
| 124 |
update(boundingRect()); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
} // namespace Avogadro::QtGui |