File indexing completed on 2022-11-29 18:11:57

0001 /*
0002     SPDX-FileCopyrightText: 2004, 2005, 2006, 2007 Carsten Niehaus <cniehaus@kde.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "detailedgraphicaloverview.h"
0007 
0008 #include "kalziumdataobject.h"
0009 #include "kalziumutils.h"
0010 
0011 #include <KLocalizedString>
0012 
0013 #include "kalzium_debug.h"
0014 #include <QFile>
0015 #include <QFontDatabase>
0016 #include <QPainter>
0017 #include <QRect>
0018 #include <QStandardPaths>
0019 #include <QSvgRenderer>
0020 
0021 #include "prefs.h"
0022 #include <element.h>
0023 
0024 DetailedGraphicalOverview::DetailedGraphicalOverview(QWidget *parent)
0025     : QWidget(parent)
0026 {
0027     setAttribute(Qt::WA_OpaquePaintEvent, true);
0028 
0029     setMinimumSize(300, 200);
0030 
0031     // Set Hydrogen as initial element.
0032     setElement(1);
0033 }
0034 
0035 void DetailedGraphicalOverview::setElement(int el)
0036 {
0037     m_element = KalziumDataObject::instance()->element(el);
0038     setBackgroundColor(KalziumElementProperty::instance()->getElementColor(el));
0039     update();
0040 }
0041 
0042 void DetailedGraphicalOverview::setBackgroundColor(QColor bgColor)
0043 {
0044     if (bgColor == Qt::transparent) {
0045         bgColor = palette().window().color();
0046     }
0047 
0048     // add a gradient
0049     QLinearGradient grad(QPointF(0, 0), QPointF(0, height()));
0050     grad.setColorAt(0, bgColor);
0051 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0052     qreal h, s, v, a;
0053 #else
0054     float h, s, v, a;
0055 #endif
0056     bgColor.getHsvF(&h, &s, &v, &a);
0057     bgColor.setHsvF(h, s, v * 0.6, a);
0058     grad.setColorAt(1, bgColor);
0059 
0060     m_backgroundBrush = QBrush(grad);
0061 }
0062 
0063 void DetailedGraphicalOverview::paintEvent(QPaintEvent *)
0064 {
0065     qreal dpr = devicePixelRatioF();
0066     qreal dprWidth = dpr * width();
0067     qreal dprHeight = dpr * height();
0068     QRect rect(0, 0, dprWidth, dprHeight);
0069 
0070     QPixmap pm(dprWidth, dprHeight);
0071     pm.setDevicePixelRatio(dpr);
0072 
0073     QPainter p;
0074     p.begin(&pm);
0075 
0076     p.setBrush(Qt::SolidPattern);
0077 
0078     if (!m_element) {
0079         pm.fill(palette().window().color());
0080         p.drawText(0, 0, width(), height(), Qt::AlignCenter | Qt::TextWordWrap, i18n("No element selected"));
0081     } else if (Prefs::colorschemebox() == 2) { // The iconic view is the 3rd view (0,1,2,...)
0082         pm.fill(palette().window().color());
0083 
0084         QString pathname = QStandardPaths::locate(QStandardPaths::AppLocalDataLocation, QStringLiteral("data/iconsets/"), QStandardPaths::LocateDirectory);
0085 
0086         int enumii = m_element->dataAsVariant(ChemicalDataObject::atomicNumber).toInt();
0087 
0088         QString filename = pathname + "school" + '/' + QString::number(enumii) + ".svg";
0089 
0090         QSvgRenderer svgrenderer;
0091         if (QFile::exists(filename) && svgrenderer.load(filename)) {
0092             QSize size = svgrenderer.defaultSize();
0093             size.scale(width(), height(), Qt::KeepAspectRatio);
0094 
0095             QRect bounds(QPoint(0, 0), size);
0096             bounds.moveCenter(QPoint(width() / 2, height() / 2));
0097             svgrenderer.render(&p, bounds);
0098         } else {
0099             p.drawText(rect, Qt::AlignCenter | Qt::TextWordWrap, i18n("No graphic found"));
0100         }
0101     } else {
0102         const int h_t = 20; // height of the texts
0103 
0104         p.setBrush(m_backgroundBrush);
0105         p.drawRect(rect);
0106         p.setBrush(Qt::black);
0107         p.setBrush(Qt::NoBrush);
0108 
0109         QFont fA = QFontDatabase::systemFont(QFontDatabase::GeneralFont);
0110         QFont fB = QFontDatabase::systemFont(QFontDatabase::GeneralFont);
0111         QFont fC = QFontDatabase::systemFont(QFontDatabase::GeneralFont);
0112 
0113         fA.setPointSize(fA.pointSize() + 20); // Huge font
0114         fA.setBold(true);
0115         fB.setPointSize(fB.pointSize() + 6); // Big font
0116         fC.setPointSize(fC.pointSize() + 4); // Big font
0117         fC.setBold(true);
0118         QFontMetrics fmA = QFontMetrics(fA);
0119         QFontMetrics fmB = QFontMetrics(fB);
0120 
0121         // coordinates for element symbol: near the center
0122         int xA = 4 * width() / 10;
0123         int yA = height() / 2;
0124 
0125         // coordinates for the atomic number: offset from element symbol to the upper left
0126         int xB = xA - fmB.boundingRect(m_element->dataAsString(ChemicalDataObject::atomicNumber)).width();
0127         int yB = yA + fmB.height() / 2;
0128 
0129         // Element Symbol
0130         p.setFont(fA);
0131         p.drawText(xA, yA, m_element->dataAsString(ChemicalDataObject::symbol));
0132 
0133         // Atomic number
0134         p.setFont(fB);
0135         p.drawText(xB, yB, m_element->dataAsString(ChemicalDataObject::atomicNumber));
0136 
0137         // Name and other data
0138         fC.setPointSize(h_t);
0139         p.setFont(fC);
0140 
0141         // Name
0142         p.drawText(1, 0, width(), height(), Qt::AlignLeft, m_element->dataAsString(ChemicalDataObject::name));
0143 
0144         // TODO Oxidationstates -> not there yet
0145 
0146         // Mass
0147         QString massString = i18nc("For example '1.0079u', the mass of an element in units", "%1 u", m_element->dataAsString(ChemicalDataObject::mass));
0148         int size3 = KalziumUtils::maxSize(massString, rect, fC, &p);
0149         fC.setPointSize(size3);
0150         p.setFont(fC);
0151         int offset = KalziumUtils::StringHeight(massString, fC, &p);
0152         p.drawText(0, height() - offset, width(), offset, Qt::AlignRight, massString);
0153     }
0154 
0155     p.end();
0156 
0157     p.begin(this);
0158     p.drawPixmap(0, 0, pm);
0159     p.end();
0160 }