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

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  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "searchwindow.h"
0016 
0017 // Qt includes
0018 
0019 #include <QApplication>
0020 #include <QScrollArea>
0021 #include <QHBoxLayout>
0022 #include <QKeyEvent>
0023 #include <QScreen>
0024 #include <QWindow>
0025 
0026 // KDE includes
0027 
0028 #include <klocalizedstring.h>
0029 #include <ksharedconfig.h>
0030 #include <kconfiggroup.h>
0031 
0032 // Local includes
0033 
0034 #include "searchview.h"
0035 #include "coredbsearchxml.h"
0036 #include "thememanager.h"
0037 
0038 namespace Digikam
0039 {
0040 
0041 class Q_DECL_HIDDEN SearchWindow::Private
0042 {
0043 public:
0044 
0045     explicit Private()
0046       : scrollArea   (nullptr),
0047         searchView   (nullptr),
0048         bottomBar    (nullptr),
0049         currentId    (-1),
0050         hasTouchedXml(false)
0051     {
0052     }
0053 
0054     QScrollArea*         scrollArea;
0055     SearchView*          searchView;
0056     SearchViewBottomBar* bottomBar;
0057     int                  currentId;
0058     bool                 hasTouchedXml;
0059     QString              oldXml;
0060 };
0061 
0062 SearchWindow::SearchWindow()
0063     : QWidget(nullptr),
0064       d      (new Private)
0065 {
0066     QVBoxLayout* const layout = new QVBoxLayout;
0067 
0068     d->scrollArea             = new QScrollArea(this);
0069     d->scrollArea->setWidgetResizable(true);
0070     d->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
0071 
0072     d->searchView             = new SearchView;
0073     d->searchView->setup();
0074 
0075     d->bottomBar              = new SearchViewBottomBar(d->searchView);
0076     d->searchView->setBottomBar(d->bottomBar);
0077 
0078     d->scrollArea->setWidget(d->searchView);
0079     d->scrollArea->setFrameStyle(QFrame::NoFrame);
0080 
0081     layout->addWidget(d->scrollArea);
0082     layout->addWidget(d->bottomBar);
0083     layout->setContentsMargins(QMargins());
0084     layout->setSpacing(0);
0085     setLayout(layout);
0086 
0087     setVisible(false);
0088     setWindowTitle(i18nc("@title:window", "Advanced Search"));
0089 
0090     QScreen* screen = qApp->primaryScreen();
0091 
0092     if (QWidget* const widget = qApp->activeWindow())
0093     {
0094         if (QWindow* const window = widget->windowHandle())
0095         {
0096             screen = window->screen();
0097         }
0098     }
0099 
0100     QRect srect = screen->availableGeometry();
0101     QSize wsize = QSize(1024 <= srect.width()  ? 1024 : srect.width(),
0102                          800 <= srect.height() ?  800 : srect.height());
0103 
0104     KSharedConfigPtr config = KSharedConfig::openConfig();
0105     KConfigGroup group      = config->group(QLatin1String("AdvancedSearch Widget"));
0106 
0107     if (group.exists())
0108     {
0109         QSize confSize = group.readEntry(QLatin1String("Widget Size"), wsize);
0110         resize(confSize);
0111     }
0112     else
0113     {
0114         resize(wsize);
0115     }
0116 
0117     connect(d->searchView, SIGNAL(searchOk()),
0118             this, SLOT(searchOk()));
0119 
0120     connect(d->searchView, SIGNAL(searchCancel()),
0121             this, SLOT(searchCancel()));
0122 
0123     connect(d->searchView, SIGNAL(searchTryout()),
0124             this, SLOT(searchTryout()));
0125 }
0126 
0127 SearchWindow::~SearchWindow()
0128 {
0129     KSharedConfigPtr config = KSharedConfig::openConfig();
0130     KConfigGroup group      = config->group(QLatin1String("AdvancedSearch Widget"));
0131     group.writeEntry(QLatin1String("Widget Size"), size());
0132 
0133     delete d;
0134 }
0135 
0136 void SearchWindow::readSearch(int id, const QString& xml)
0137 {
0138     d->currentId     = id;
0139     d->hasTouchedXml = false;
0140     d->oldXml        = xml;
0141     d->searchView->read(xml);
0142 }
0143 
0144 void SearchWindow::reset()
0145 {
0146     d->currentId     = -1;
0147     d->hasTouchedXml = false;
0148     d->oldXml.clear();
0149     d->searchView->read(QString());
0150 }
0151 
0152 QString SearchWindow::search() const
0153 {
0154     return d->searchView->write();
0155 }
0156 
0157 void SearchWindow::searchOk()
0158 {
0159     d->hasTouchedXml = true;
0160     Q_EMIT searchEdited(d->currentId, search());
0161     hide();
0162 }
0163 
0164 void SearchWindow::searchCancel()
0165 {
0166     // redo changes by tryout
0167 
0168     if (d->hasTouchedXml)
0169     {
0170         Q_EMIT searchEdited(d->currentId, d->oldXml);
0171         d->hasTouchedXml = false;
0172     }
0173 
0174     hide();
0175 }
0176 
0177 void SearchWindow::searchTryout()
0178 {
0179     d->hasTouchedXml = true;
0180     Q_EMIT searchEdited(d->currentId, search());
0181 }
0182 
0183 void SearchWindow::keyPressEvent(QKeyEvent* e)
0184 {
0185     // Implement keys like in a dialog
0186 
0187     if (!e->modifiers() || ((e->modifiers() & Qt::KeypadModifier) && (e->key() == Qt::Key_Enter)))
0188     {
0189         switch (e->key())
0190         {
0191             case Qt::Key_Enter:
0192             case Qt::Key_Return:
0193             case Qt::Key_Select:
0194                 searchOk();
0195                 break;
0196 
0197             case Qt::Key_F4:
0198             case Qt::Key_Escape:
0199             case Qt::Key_Back:
0200                 searchCancel();
0201                 break;
0202 
0203             default:
0204                 break;
0205         }
0206     }
0207 }
0208 
0209 } // namespace Digikam
0210 
0211 #include "moc_searchwindow.cpp"