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: 2012-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 "searchfieldgroup.h"
0017 
0018 // Qt includes
0019 
0020 #include <QGridLayout>
0021 #include <QLabel>
0022 #include <QVBoxLayout>
0023 
0024 // Local includes
0025 
0026 #include "searchfields.h"
0027 #include "searchgroup.h"
0028 #include "searchutilities.h"
0029 #include "thememanager.h"
0030 
0031 namespace Digikam
0032 {
0033 
0034 SearchFieldGroup::SearchFieldGroup(SearchGroup* const parent)
0035     : QWidget(parent),
0036       m_label(nullptr)
0037 {
0038     m_layout     = new QGridLayout;
0039     setLayout(m_layout);
0040     m_controller = new VisibilityController(this);
0041     m_controller->setContainerWidget(this);
0042 }
0043 
0044 void SearchFieldGroup::addField(SearchField* const field)
0045 {
0046     //FIXME: When all fields added in searchgroup.cpp are implemented, remove
0047 
0048     if (!field)
0049     {
0050         return;
0051     }
0052 
0053     // initialize widgets in field
0054 
0055     field->setup(m_layout);
0056 
0057     // take care that category labels are not duplicated in two subsequent rows
0058 
0059     if (!m_fields.isEmpty())
0060     {
0061         field->setCategoryLabelVisibleFromPreviousField(m_fields.last());
0062     }
0063 
0064     // add to our list
0065 
0066     m_fields << field;
0067 
0068     // add to visibility controller
0069 
0070     m_controller->addObject(field);
0071 }
0072 
0073 void SearchFieldGroup::setLabel(SearchFieldGroupLabel* const label)
0074 {
0075     m_label = label;
0076 
0077     connect(m_label, SIGNAL(clicked()),
0078             this, SLOT(slotLabelClicked()));
0079 }
0080 
0081 SearchField* SearchFieldGroup::fieldForName(const QString& fieldName) const
0082 {
0083     Q_FOREACH (SearchField* const field, m_fields)
0084     {
0085         if (field->supportsField(fieldName))
0086         {    // cppcheck-suppress useStlAlgorithm
0087             return field;
0088         }
0089     }
0090 
0091     return nullptr;
0092 }
0093 
0094 void SearchFieldGroup::write(SearchXmlWriter& writer)
0095 {
0096     Q_FOREACH (SearchField* const field, m_fields)
0097     {
0098         field->write(writer);
0099     }
0100 }
0101 
0102 void SearchFieldGroup::reset()
0103 {
0104     clearMarkedFields();
0105 
0106     Q_FOREACH (SearchField* const field, m_fields)
0107     {
0108         field->reset();
0109     }
0110 }
0111 
0112 void SearchFieldGroup::setFieldsVisible(bool visible)
0113 {
0114     m_controller->setVisible(visible);
0115 }
0116 
0117 void SearchFieldGroup::slotLabelClicked()
0118 {
0119     m_controller->triggerVisibility();
0120 }
0121 
0122 void SearchFieldGroup::markField(SearchField* const field)
0123 {
0124     m_markedFields << field;
0125 }
0126 
0127 void SearchFieldGroup::clearMarkedFields()
0128 {
0129     m_markedFields.clear();
0130 }
0131 
0132 QList<QRect> SearchFieldGroup::areaOfMarkedFields() const
0133 {
0134     QList<QRect> rects;
0135 
0136     if (!m_controller->isVisible())
0137     {
0138         return rects;
0139     }
0140 
0141     Q_FOREACH (SearchField* const field, m_markedFields)
0142     {
0143         if (field->isVisible())
0144         {
0145             // cppcheck-suppress useStlAlgorithm
0146             rects += field->widgetRects(SearchField::ValueWidgetRectsOnly);
0147         }
0148     }
0149 
0150     // adjust position relative to parent
0151 
0152     for (QList<QRect>::iterator it = rects.begin() ; it != rects.end() ; ++it)
0153     {
0154         (*it).translate(pos());
0155     }
0156 
0157     return rects;
0158 }
0159 
0160 // -------------------------------------------------------------------------
0161 
0162 SearchFieldGroupLabel::SearchFieldGroupLabel(QWidget* const parent)
0163     : QWidget(parent)
0164 {
0165     QGridLayout* const layout = new QGridLayout;
0166 
0167     m_titleLabel              = new DClickLabel;
0168     m_titleLabel->setObjectName(QLatin1String("SearchFieldGroupLabel_Label"));
0169     m_expandLabel             = new QLabel;
0170     QFrame* const hline       = new QFrame;
0171     hline->setFrameStyle(QFrame::HLine | QFrame::Raised);
0172 
0173     layout->addWidget(m_titleLabel,  0, 0);
0174     layout->addWidget(m_expandLabel, 0, 1);
0175     layout->addWidget(hline,         1, 0, 1, 3);
0176     layout->setColumnStretch(2, 1);
0177     layout->setSpacing(2);
0178     setLayout(layout);
0179 
0180     connect(m_titleLabel, SIGNAL(activated()),
0181             this, SIGNAL(clicked()));
0182 }
0183 
0184 void SearchFieldGroupLabel::setTitle(const QString& title)
0185 {
0186     m_title = title;
0187     m_titleLabel->setText(title);
0188 }
0189 
0190 void SearchFieldGroupLabel::displayExpanded()
0191 {
0192 }
0193 
0194 void SearchFieldGroupLabel::displayFolded()
0195 {
0196 }
0197 
0198 } // namespace Digikam
0199 
0200 #include "moc_searchfieldgroup.cpp"