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

0001 /*
0002     SPDX-FileCopyrightText: 2007 Carsten Niehaus <cniehaus@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "legendwidget.h"
0008 
0009 #include "prefs.h"
0010 #include "kalzium_debug.h"
0011 
0012 #include <QGridLayout>
0013 #include <QHBoxLayout>
0014 #include <QStyle>
0015 
0016 #include <KLocalizedString>
0017 
0018 LegendWidget::LegendWidget(QWidget *parent)
0019     : QWidget(parent)
0020 {
0021     m_update = true;
0022     m_dockArea = Qt::BottomDockWidgetArea;
0023     setContentsMargins(
0024         style()->pixelMetric(QStyle::PM_LayoutLeftMargin),
0025         style()->pixelMetric(QStyle::PM_LayoutTopMargin),
0026         style()->pixelMetric(QStyle::PM_LayoutRightMargin),
0027         style()->pixelMetric(QStyle::PM_LayoutBottomMargin)
0028     );
0029 }
0030 
0031 LegendWidget::~LegendWidget()
0032 {
0033     qDeleteAll(m_legendItemList);
0034 }
0035 
0036 void LegendWidget::setDockArea(Qt::DockWidgetArea newDockArea)
0037 {
0038     qCDebug(KALZIUM_LOG) << "dock Area changed" << newDockArea;
0039 
0040     m_dockArea = newDockArea;
0041     updateContent();
0042 }
0043 
0044 void LegendWidget::LockWidget()
0045 {
0046     m_update = false;
0047 }
0048 
0049 void LegendWidget::UnLockWidget()
0050 {
0051     m_update = true;
0052 }
0053 
0054 void LegendWidget::updateContent()
0055 {
0056     if (!m_update) {
0057         return;
0058     }
0059 
0060     QString gradientDesc;
0061     QList<QPair<QString, QColor>> items;
0062     KalziumElementProperty *elementProperty = KalziumElementProperty::instance();
0063     // Handle different Gradients
0064     switch (elementProperty->gradientId()) {
0065     case KalziumElementProperty::NOGRADIENT: // None
0066         break;
0067 
0068     case KalziumElementProperty::SOMGradientType:
0069         items << qMakePair(elementProperty->gradient()->description(), QColor());
0070         items << qMakePair(i18nc("one of the three states of matter (solid, liquid, vaporous or unknown)", "Solid"), QColor(Prefs::color_solid()));
0071 
0072         items << qMakePair(i18nc("one of the three states of matter (solid, liquid, vaporous or unknown)", "Liquid"), QColor(Prefs::color_liquid()));
0073 
0074         items << qMakePair(i18nc("one of the three states of matter (solid, liquid, vaporous or unknown)", "Vaporous"), QColor(Prefs::color_vapor()));
0075 
0076         items << qMakePair(i18nc("one of the three states of matter (solid, liquid, vaporous or unknown)", "Unknown"), QColor(Qt::lightGray));
0077         break;
0078 
0079     default:
0080         if (elementProperty->gradient()->logarithmicGradient()) {
0081             gradientDesc = i18nc("one of the two types of gradients available", "logarithmic");
0082         } else {
0083             gradientDesc = i18nc("one of the two types of gradients available", "linear");
0084         }
0085         items << qMakePair(i18n("%1 (%2)", elementProperty->gradient()->description(), gradientDesc), QColor());
0086         items << qMakePair(i18nc("Minimum value of the gradient",
0087                                  "Minimum: %1",
0088                                  QString::number(elementProperty->gradient()->minValue()) + ' ' + elementProperty->gradient()->unit()),
0089                            QColor(elementProperty->gradient()->firstColor()));
0090 
0091         items << qMakePair(i18nc("Maximum value of the gradient",
0092                                  "Maximum: %1",
0093                                  QString::number(elementProperty->gradient()->maxValue()) + ' ' + elementProperty->gradient()->unit()),
0094                            QColor(elementProperty->gradient()->secondColor()));
0095         break;
0096     }
0097     // schemes are always there
0098     items << qMakePair(i18n("Scheme: %1", elementProperty->scheme()->description()), QColor());
0099     items << elementProperty->scheme()->legendItems();
0100 
0101     updateLegendItemLayout(items);
0102 }
0103 
0104 void LegendWidget::updateLegendItemLayout(const QList<legendPair> &list)
0105 {
0106     if (layout()) {
0107         delete layout();
0108     }
0109     foreach (LegendItem *i, m_legendItemList) {
0110         delete i;
0111     }
0112 
0113     m_legendItemList.clear();
0114 
0115     auto layout = new QGridLayout(this);
0116     layout->setSpacing(0);
0117     layout->setVerticalSpacing(style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing));
0118     layout->setContentsMargins(0, 0, 0, 0);
0119 
0120     int x = 0;
0121     int y = 0;
0122 
0123     for (const legendPair &pair : list) {
0124         auto item = new LegendItem(pair, this);
0125         m_legendItemList.append(item);
0126 
0127         if ((m_dockArea == Qt::BottomDockWidgetArea || m_dockArea == Qt::TopDockWidgetArea)) {
0128             if (!pair.second.isValid()) {
0129                 ++y;
0130                 x = 0;
0131             }
0132             if (x >= 3) {
0133                 ++y;
0134                 x = 1;
0135             }
0136         }
0137         layout->addWidget(item, x, y);
0138         ++x;
0139     }
0140     setLayout(layout);
0141 }
0142 
0143 void LegendWidget::legendItemAction(QColor color)
0144 {
0145     Q_EMIT resetElementMatch();
0146 
0147     if (color.operator==(QColor())) {
0148         return;
0149     }
0150 
0151     for (int element = 1; element <= 118; ++element) {
0152         if (isElementMatch(element, color)) {
0153             Q_EMIT elementMatched(element);
0154         }
0155     }
0156 }
0157 
0158 bool LegendWidget::isElementMatch(int element, QColor &color)
0159 {
0160     QColor elementBackgroundColor;
0161     QColor elementBorderColor;
0162 
0163     elementBackgroundColor = KalziumElementProperty::instance()->getElementColor(element);
0164     elementBorderColor = KalziumElementProperty::instance()->getBorderColor(element);
0165 
0166     switch (Prefs::colorgradientbox()) {
0167     case KalziumElementProperty::NOGRADIENT:
0168         if (elementBackgroundColor.operator!=(color)) {
0169             return true;
0170         }
0171         break;
0172 
0173     default: // all other gradients
0174         if (elementBorderColor.operator==(color) || elementBackgroundColor.operator==(color)) {
0175             return true;
0176         }
0177     }
0178     return false;
0179 }
0180 
0181 LegendItem::LegendItem(const QPair<QString, QColor> &pair, LegendWidget *parent)
0182     : QLabel(parent)
0183 {
0184     auto ItemLayout = new QHBoxLayout(this);
0185     ItemLayout->setContentsMargins(0, 0, 0, 0);
0186 
0187     if (pair.second.isValid()) {
0188         legendItemColor = pair.second;
0189         if (parent) {
0190             connect(this, &LegendItem::legenItemHoovered, parent, &LegendWidget::legendItemAction);
0191         }
0192 
0193         QPixmap LegendPixmap(20, height());
0194         LegendPixmap.fill(pair.second);
0195 
0196         auto LabelPixmap = new QLabel(this);
0197         LabelPixmap->setPixmap(LegendPixmap);
0198         ItemLayout->addWidget(LabelPixmap);
0199 
0200         setFrameShape(QFrame::NoFrame);
0201     }
0202     auto LegendLabel = new QLabel(this);
0203     LegendLabel->setText(pair.first);
0204     ItemLayout->addWidget(LegendLabel);
0205 
0206     ItemLayout->setAlignment(Qt::AlignLeft);
0207     setLayout(ItemLayout);
0208 }
0209 
0210 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
0211 void LegendItem::enterEvent(QEnterEvent *event)
0212 #else
0213 void LegendItem::enterEvent(QEvent *event)
0214 #endif
0215 {
0216     Q_EMIT legenItemHoovered(legendItemColor);
0217     QWidget::enterEvent(event);
0218 }
0219 
0220 void LegendItem::leaveEvent(QEvent *event)
0221 {
0222     Q_EMIT legenItemHoovered(QColor());
0223     QWidget::leaveEvent(event);
0224 }
0225 
0226 #include "moc_legendwidget.cpp"