File indexing completed on 2024-04-21 03:41:41
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 "kalziumelementproperty.h" 0009 #include "kalziumdataobject.h" 0010 #include "kalziumutils.h" 0011 #include "kalzium_debug.h" 0012 0013 #include <KLocalizedString> 0014 0015 #include <QFile> 0016 #include <QFontDatabase> 0017 #include <QPainter> 0018 #include <QRect> 0019 #include <QStandardPaths> 0020 #include <QSvgRenderer> 0021 #include <QGuiApplication> 0022 0023 #include "prefs.h" 0024 #include "colorutils.h" 0025 #include <element.h> 0026 0027 DetailedGraphicalOverview::DetailedGraphicalOverview(QWidget *parent) 0028 : QWidget(parent) 0029 { 0030 setAttribute(Qt::WA_OpaquePaintEvent, true); 0031 0032 setMinimumSize(300, 200); 0033 0034 // Set Hydrogen as initial element. 0035 setElement(1); 0036 } 0037 0038 void DetailedGraphicalOverview::setElement(int el) 0039 { 0040 m_element = KalziumDataObject::instance()->element(el); 0041 setBackgroundColor(KalziumElementProperty::instance()->getElementColor(el)); 0042 update(); 0043 } 0044 0045 0046 void DetailedGraphicalOverview::setBackgroundColor(QColor bgColor) 0047 { 0048 if (bgColor == Qt::transparent) { 0049 bgColor = palette().window().color(); 0050 } 0051 0052 m_textColor = ColorUtils::contrastColor(bgColor); 0053 m_backgroundBrush = QBrush(ColorUtils::tintWithAlpha(qGuiApp->palette().color(QPalette::Normal, QPalette::Window), bgColor, 0.2)); 0054 } 0055 0056 void DetailedGraphicalOverview::paintEvent(QPaintEvent *) 0057 { 0058 const int margins = 4; 0059 QPainter p; 0060 p.begin(this); 0061 0062 p.setBrush(Qt::SolidPattern); 0063 0064 if (!m_element) { 0065 p.setBrush(palette().window().color()); 0066 p.drawRect(rect()); 0067 p.drawText(0, 0, width(), height(), Qt::AlignCenter | Qt::TextWordWrap, i18n("No element selected")); 0068 } else if (Prefs::colorschemebox() == 2) { // The iconic view is the 3rd view (0,1,2,...) 0069 p.setBrush(palette().window().color()); 0070 p.drawRect(rect()); 0071 0072 QString pathname = QStandardPaths::locate(QStandardPaths::AppLocalDataLocation, QStringLiteral("data/iconsets/"), QStandardPaths::LocateDirectory); 0073 0074 int enumii = m_element->dataAsVariant(ChemicalDataObject::atomicNumber).toInt(); 0075 0076 QString filename = pathname + "school" + '/' + QString::number(enumii) + ".svg"; 0077 0078 QSvgRenderer svgrenderer; 0079 if (QFile::exists(filename) && svgrenderer.load(filename)) { 0080 QSize size = svgrenderer.defaultSize(); 0081 size.scale(width(), height(), Qt::KeepAspectRatio); 0082 0083 QRect bounds(QPoint(0, 0), size); 0084 bounds.moveCenter(QPoint(width() / 2, height() / 2)); 0085 svgrenderer.render(&p, bounds); 0086 } else { 0087 p.drawText(rect(), Qt::AlignCenter | Qt::TextWordWrap, i18n("No graphic found")); 0088 } 0089 } else { 0090 const int h_t = 20; // height of the texts 0091 0092 p.setPen(Qt::NoPen); 0093 p.setBrush(m_backgroundBrush); 0094 p.drawRect(rect()); 0095 p.setBrush(Qt::black); 0096 p.setBrush(Qt::NoBrush); 0097 0098 p.setPen(m_textColor); 0099 0100 QFont fA = QFontDatabase::systemFont(QFontDatabase::GeneralFont); 0101 QFont fB = QFontDatabase::systemFont(QFontDatabase::GeneralFont); 0102 QFont fC = QFontDatabase::systemFont(QFontDatabase::GeneralFont); 0103 0104 fA.setPointSize(fA.pointSize() + 20); // Huge font 0105 fA.setBold(true); 0106 fB.setPointSize(fB.pointSize() + 6); // Big font 0107 fC.setPointSize(fC.pointSize() + 4); // Big font 0108 fC.setBold(true); 0109 QFontMetrics fmA = QFontMetrics(fA); 0110 QFontMetrics fmB = QFontMetrics(fB); 0111 0112 // coordinates for element symbol: near the center 0113 int xA = 4 * width() / 10; 0114 int yA = height() / 2; 0115 0116 // coordinates for the atomic number: offset from element symbol to the upper left 0117 int xB = xA - fmB.boundingRect(m_element->dataAsString(ChemicalDataObject::atomicNumber)).width(); 0118 int yB = yA + fmB.height() / 2; 0119 0120 // Element Symbol 0121 p.setFont(fA); 0122 p.drawText(xA, yA, m_element->dataAsString(ChemicalDataObject::symbol)); 0123 0124 // Atomic number 0125 p.setFont(fB); 0126 p.drawText(xB, yB, m_element->dataAsString(ChemicalDataObject::atomicNumber)); 0127 0128 // Name and other data 0129 fC.setPointSize(h_t); 0130 p.setFont(fC); 0131 0132 // Name 0133 p.drawText(margins, 0, width(), height(), Qt::AlignLeft, m_element->dataAsString(ChemicalDataObject::name)); 0134 0135 // TODO Oxidationstates -> not there yet 0136 0137 // Mass 0138 QString massString = i18nc("For example '1.0079u', the mass of an element in units", "%1 u", m_element->dataAsString(ChemicalDataObject::mass)); 0139 int size3 = KalziumUtils::maxSize(massString, rect(), fC, &p); 0140 fC.setPointSize(size3); 0141 p.setFont(fC); 0142 int offset = KalziumUtils::StringHeight(massString, fC, &p); 0143 p.drawText(-margins, height() - offset, width(), offset, Qt::AlignRight, massString); 0144 } 0145 0146 p.end(); 0147 } 0148 0149 #include "moc_detailedgraphicaloverview.cpp"