File indexing completed on 2025-01-05 04:00:05

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2008-01-20
0007  * Description : User interface for searches
0008  *
0009  * SPDX-FileCopyrightText: 2008-2012 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0010  * SPDX-FileCopyrightText: 2011-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "searchfields_p.h"
0017 
0018 namespace Digikam
0019 {
0020 
0021 SearchFieldRating::SearchFieldRating(QObject* const parent)
0022     : SearchField(parent)
0023 {
0024     m_betweenLabel = new QLabel;
0025     m_firstBox     = new RatingComboBox;
0026     m_secondBox    = new RatingComboBox;
0027 }
0028 
0029 void SearchFieldRating::setupValueWidgets(QGridLayout* layout, int row, int column)
0030 {
0031     layout->addWidget(m_firstBox,     row, column);
0032     layout->addWidget(m_betweenLabel, row, column + 1, Qt::AlignHCenter);
0033     layout->addWidget(m_secondBox,    row, column + 2);
0034 
0035     connect(m_firstBox, SIGNAL(ratingValueChanged(int)),
0036             this, SLOT(firstValueChanged()));
0037 
0038     connect(m_secondBox, SIGNAL(ratingValueChanged(int)),
0039             this, SLOT(secondValueChanged()));
0040 }
0041 
0042 void SearchFieldRating::read(SearchXmlCachingReader& reader)
0043 {
0044     SearchXml::Relation relation = reader.fieldRelation();
0045 
0046     switch (relation)
0047     {
0048         case SearchXml::GreaterThanOrEqual:
0049             m_firstBox->setRatingValue((RatingComboBox::RatingValue)reader.valueToInt());
0050             break;
0051 
0052         case SearchXml::GreaterThan:
0053             m_firstBox->setRatingValue((RatingComboBox::RatingValue)(reader.valueToInt() - 1));
0054             break;
0055 
0056         case SearchXml::LessThanOrEqual:
0057             m_secondBox->setRatingValue((RatingComboBox::RatingValue)reader.valueToInt());
0058             break;
0059 
0060         case SearchXml::LessThan:
0061             m_secondBox->setRatingValue((RatingComboBox::RatingValue)(reader.valueToInt() + 1));
0062             break;
0063 
0064         case SearchXml::Equal:
0065             m_firstBox->setRatingValue((RatingComboBox::RatingValue)reader.valueToInt());
0066             m_secondBox->setRatingValue((RatingComboBox::RatingValue)reader.valueToInt());
0067             break;
0068 
0069         case SearchXml::Interval:
0070         case SearchXml::IntervalOpen:
0071         {
0072             QList<int> list = reader.valueToIntList();
0073 
0074             if (list.size() != 2)
0075             {
0076                 return;
0077             }
0078 
0079             m_firstBox->setRatingValue((RatingComboBox::RatingValue)list.first());
0080             m_secondBox->setRatingValue((RatingComboBox::RatingValue)list.last());
0081             break;
0082         }
0083 
0084         default:
0085             break;
0086     }
0087 }
0088 
0089 void SearchFieldRating::write(SearchXmlWriter& writer)
0090 {
0091     RatingComboBox::RatingValue first  = m_firstBox->ratingValue();
0092     RatingComboBox::RatingValue second = m_secondBox->ratingValue();
0093 
0094     if      (first == RatingComboBox::NoRating)
0095     {
0096         writer.writeField(m_name, SearchXml::Equal);
0097         writer.writeValue(-1);
0098         writer.finishField();
0099     }
0100     else if (first != RatingComboBox::Null && first == second)
0101     {
0102         writer.writeField(m_name, SearchXml::Equal);
0103         writer.writeValue(first);
0104         writer.finishField();
0105     }
0106     else if (first != RatingComboBox::Null && second != RatingComboBox::Null)
0107     {
0108         writer.writeField(m_name, SearchXml::Interval);
0109         writer.writeValue(QList<int>() << first << second);
0110         writer.finishField();
0111     }
0112     else
0113     {
0114         if (first != RatingComboBox::Null)
0115         {
0116             writer.writeField(m_name, SearchXml::GreaterThanOrEqual);
0117             writer.writeValue(first);
0118             writer.finishField();
0119         }
0120 
0121         if (second != RatingComboBox::Null)
0122         {
0123             writer.writeField(m_name, SearchXml::LessThanOrEqual);
0124             writer.writeValue(second);
0125             writer.finishField();
0126         }
0127     }
0128 }
0129 
0130 void SearchFieldRating::setBetweenText(const QString& text)
0131 {
0132     m_betweenLabel->setText(text);
0133 }
0134 
0135 void SearchFieldRating::firstValueChanged()
0136 {
0137     RatingComboBox::RatingValue first  = m_firstBox->ratingValue();
0138     RatingComboBox::RatingValue second = m_secondBox->ratingValue();
0139 
0140     if (first == RatingComboBox::NoRating)
0141     {
0142         m_secondBox->setRatingValue(RatingComboBox::Null);
0143         m_secondBox->setEnabled(false);
0144     }
0145     else
0146     {
0147         m_secondBox->setEnabled(true);
0148     }
0149 
0150     if ((first >= RatingComboBox::Rating0) && (first <= RatingComboBox::Rating5))
0151     {
0152         if (first > second)
0153         {
0154             m_secondBox->setRatingValue(RatingComboBox::Null);
0155         }
0156     }
0157 
0158     setValidValueState(first != RatingComboBox::Null || second != RatingComboBox::Null);
0159 }
0160 
0161 void SearchFieldRating::secondValueChanged()
0162 {
0163     RatingComboBox::RatingValue first  = m_firstBox->ratingValue();
0164     RatingComboBox::RatingValue second = m_secondBox->ratingValue();
0165 
0166     // NoRating is not possible for the second box
0167 
0168     if (second >= RatingComboBox::Rating0 && second <= RatingComboBox::Rating5)
0169     {
0170         if (first > second)
0171         {
0172             m_firstBox->setRatingValue(second);
0173         }
0174     }
0175 
0176     setValidValueState((first != RatingComboBox::Null) || (second != RatingComboBox::Null));
0177 }
0178 
0179 void SearchFieldRating::reset()
0180 {
0181     m_firstBox->setRatingValue(RatingComboBox::Null);
0182     m_secondBox->setRatingValue(RatingComboBox::Null);
0183 }
0184 
0185 void SearchFieldRating::setValueWidgetsVisible(bool visible)
0186 {
0187     m_firstBox->setVisible(visible);
0188     m_secondBox->setVisible(visible);
0189     m_betweenLabel->setVisible(visible);
0190 }
0191 
0192 QList<QRect> SearchFieldRating::valueWidgetRects() const
0193 {
0194     QList<QRect> rects;
0195     rects << m_firstBox->geometry();
0196     rects << m_secondBox->geometry();
0197 
0198     return rects;
0199 }
0200 
0201 } // namespace Digikam