File indexing completed on 2023-09-24 03:52:09

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