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

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 SearchField::SearchField(QObject* const parent)
0022     : QObject(parent)
0023 {
0024     m_label                = new QLabel;
0025     m_detailLabel          = new QLabel;
0026     m_clearButton          = new AnimatedClearButton;
0027     m_categoryLabelVisible = true;
0028     m_valueIsValid         = false;
0029 }
0030 
0031 void SearchField::setup(QGridLayout* const layout, int line)
0032 {
0033     if (line == -1)
0034     {
0035         line = layout->rowCount();
0036     }
0037 
0038     // 10px indent
0039 
0040     layout->setColumnMinimumWidth(0, 10);
0041 
0042     // set stretch for the value widget columns
0043 
0044     layout->setColumnStretch(3, 1);
0045     layout->setColumnStretch(5, 1);
0046 
0047     // push value widgets to the left
0048 
0049     layout->setColumnStretch(6, 1);
0050 
0051     setupLabels(layout, line);
0052 
0053     // value widgets can use columns 3,4,5.
0054     // In the case of "from ... to ..." fields, column 3 and 5 can contain spin boxes etc.,
0055     // and 4 can contain a label in between.
0056     // In other cases, a widget or sublayout spanning the three columns is recommended.
0057 
0058     setupValueWidgets(layout, line, 3);
0059 
0060     // setup the clear button that appears dynamically
0061 
0062     if (qApp->isLeftToRight())
0063     {
0064         m_clearButton->setPixmap(QIcon::fromTheme(QLatin1String("edit-clear-locationbar-rtl"))
0065                                  .pixmap(QApplication::style()->pixelMetric(QStyle::PM_SmallIconSize)));
0066     }
0067     else
0068     {
0069         m_clearButton->setPixmap(QIcon::fromTheme(QLatin1String("edit-clear-locationbar-ltr"))
0070                                  .pixmap(QApplication::style()->pixelMetric(QStyle::PM_SmallIconSize)));
0071     }
0072 
0073     // Important: Don't cause re-layouting when button gets hidden/shown!
0074 
0075     m_clearButton->stayVisibleWhenAnimatedOut(true);
0076     m_clearButton->setToolTip(i18n("Reset contents"));
0077 
0078     connect(m_clearButton, SIGNAL(clicked()),
0079             this, SLOT(clearButtonClicked()));
0080 
0081     layout->addWidget(m_clearButton, line, 7);
0082 }
0083 
0084 void SearchField::setupLabels(QGridLayout* layout, int line)
0085 {
0086     m_label->setObjectName(QLatin1String("SearchField_MainLabel"));
0087     m_detailLabel->setObjectName(QLatin1String("SearchField_DetailLabel"));
0088     layout->addWidget(m_label, line, 1);
0089     layout->addWidget(m_detailLabel, line, 2);
0090 }
0091 
0092 void SearchField::setFieldName(const QString& fieldName)
0093 {
0094     m_name = fieldName;
0095 }
0096 
0097 void SearchField::setText(const QString& label, const QString& detailLabel)
0098 {
0099     m_label->setText(label);
0100     m_detailLabel->setText(detailLabel);
0101 }
0102 
0103 bool SearchField::supportsField(const QString& fieldName)
0104 {
0105     return (m_name == fieldName);
0106 }
0107 
0108 void SearchField::setVisible(bool visible)
0109 {
0110     m_label->setVisible(visible && m_categoryLabelVisible);
0111     m_detailLabel->setVisible(visible);
0112     m_clearButton->setShallBeShown(visible);
0113     setValueWidgetsVisible(visible);
0114 }
0115 
0116 bool SearchField::isVisible()
0117 {
0118     // the detail label is considered representative for all widgets
0119 
0120     return m_detailLabel->isVisible();
0121 }
0122 
0123 void SearchField::setCategoryLabelVisible(bool visible)
0124 {
0125     if (m_categoryLabelVisible == visible)
0126     {
0127         return;
0128     }
0129 
0130     m_categoryLabelVisible = visible;
0131 
0132     // update status: compare setVisible() and isVisible()
0133 
0134     m_label->setVisible(m_detailLabel->isVisible() && m_categoryLabelVisible);
0135 }
0136 
0137 void SearchField::setCategoryLabelVisibleFromPreviousField(SearchField* previousField)
0138 {
0139     if (previousField->m_label->text() == m_label->text())
0140     {
0141         setCategoryLabelVisible(false);
0142     }
0143     else
0144     {
0145         setCategoryLabelVisible(true);
0146     }
0147 
0148     if ((previousField->m_name == QLatin1String("filesize")) &&
0149         (this->m_name          == QLatin1String("bytesize")))
0150     {
0151         connect(previousField, &SearchField::signalVisibilityChanged,
0152                 this, &SearchField::clearButtonClicked);
0153 
0154         connect(this, &SearchField::signalVisibilityChanged,
0155                 previousField, &SearchField::clearButtonClicked);
0156     }
0157 }
0158 
0159 QList<QRect> SearchField::widgetRects(WidgetRectType type) const
0160 {
0161     QList<QRect> rects;
0162 
0163     if (type == LabelAndValueWidgetRects)
0164     {
0165         rects << m_label->geometry();
0166         rects << m_detailLabel->geometry();
0167     }
0168 
0169     rects += valueWidgetRects();
0170 
0171     return rects;
0172 }
0173 
0174 void SearchField::clearButtonClicked()
0175 {
0176     reset();
0177 }
0178 
0179 void SearchField::setValidValueState(bool valueIsValid)
0180 {
0181     if (valueIsValid != m_valueIsValid)
0182     {
0183         m_valueIsValid = valueIsValid;
0184 
0185         // NOTE: setVisible visibility is independent from animateVisible visibility!
0186 
0187         m_clearButton->animateVisible(m_valueIsValid);
0188 
0189         Q_EMIT signalVisibilityChanged();
0190     }
0191 }
0192 
0193 } // namespace Digikam
0194 
0195 #include "moc_searchfields.cpp"