File indexing completed on 2024-04-21 03:41:39

0001 /*
0002     NumerationItem - Numeration Item, part of the Periodic Table Graphics View
0003     for Kalzium
0004 
0005     SPDX-FileCopyrightText: 2007-2009 Marcus D. Hanwell <marcus@cryos.org>
0006     SPDX-FileContributor: 2010 Konstantin Tokarev
0007     SPDX-FileCopyrightText: 2010 Etienne Rebetez <etienne.rebetez@oberwallis.ch>
0008 
0009     This file is part of the Avogadro molecular editor project.
0010     For more information, see <https://avogadro.cc/>
0011 
0012     SPDX-License-Identifier: LGPL-2.1-or-later
0013 */
0014 
0015 #include "numerationitem.h"
0016 
0017 #include <prefs.h>
0018 
0019 #include <QFont>
0020 #include <QPainter>
0021 #include <QStyleOption>
0022 #include <QGuiApplication>
0023 #include <QPalette>
0024 
0025 #include <KLocalizedString>
0026 #include <qbrush.h>
0027 
0028 #include "kalziumnumerationtype.h"
0029 
0030 NumerationItem::NumerationItem(int xPosition)
0031     : m_width(40)
0032     , m_height(20)
0033     , m_xPosition(xPosition)
0034 {
0035     setNumerationType(Prefs::numeration());
0036 }
0037 
0038 NumerationItem::~NumerationItem() = default;
0039 
0040 QRectF NumerationItem::boundingRect() const
0041 {
0042     return QRectF(0, 0, m_width, m_height);
0043 }
0044 
0045 QPainterPath NumerationItem::shape() const
0046 {
0047     QPainterPath path;
0048     path.addRect(0, 0, m_width, m_height);
0049     return path;
0050 }
0051 
0052 void NumerationItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
0053 {
0054     QColor color = m_type == 0 ? QColor(Qt::transparent) : qGuiApp->palette().window().color();
0055     painter->setBrush(color);
0056     painter->setPen(Qt::NoPen);
0057 
0058     QRectF rect(0, 0, m_width, m_height);
0059     painter->drawRoundedRect(rect, 3, 3);
0060 
0061     QPen pen;
0062     painter->setPen(qGuiApp->palette().text().color());
0063     painter->drawText(rect, Qt::AlignCenter, m_numeration);
0064 }
0065 
0066 void NumerationItem::setNumerationType(int type)
0067 {
0068     m_type = type;
0069 
0070     m_numeration = KalziumNumerationTypeFactory::instance()->build(type)->item(m_xPosition);
0071 
0072     update();
0073 }
0074 
0075 #include "moc_numerationitem.cpp"