File indexing completed on 2025-01-19 03:53:23

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2005-06-16
0007  * Description : a dialog to select a target album to download
0008  *               pictures from camera
0009  *
0010  * SPDX-FileCopyrightText: 2005      by Renchi Raju <renchi dot raju at gmail dot com>
0011  * SPDX-FileCopyrightText: 2006-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0012  *
0013  * SPDX-License-Identifier: GPL-2.0-or-later
0014  *
0015  * ============================================================ */
0016 
0017 #include "albumselectdialog.h"
0018 
0019 // Qt includes
0020 
0021 #include <QLabel>
0022 #include <QGridLayout>
0023 #include <QPixmap>
0024 #include <QPointer>
0025 #include <QStandardPaths>
0026 #include <QApplication>
0027 #include <QStyle>
0028 #include <QDialogButtonBox>
0029 #include <QVBoxLayout>
0030 #include <QPushButton>
0031 
0032 // KDE includes
0033 
0034 #include <klocalizedstring.h>
0035 #include <ksharedconfig.h>
0036 #include <kconfiggroup.h>
0037 
0038 // Local includes
0039 
0040 #include "digikam_globals.h"
0041 #include "album.h"
0042 #include "albummanager.h"
0043 #include "albumselectwidget.h"
0044 #include "albumthumbnailloader.h"
0045 #include "collectionmanager.h"
0046 #include "dxmlguiwindow.h"
0047 
0048 namespace Digikam
0049 {
0050 
0051 class Q_DECL_HIDDEN AlbumSelectDialog::Private
0052 {
0053 
0054 public:
0055 
0056     explicit Private()
0057       : buttons     (nullptr),
0058         albumSel    (nullptr),
0059         searchBar   (nullptr)
0060     {
0061     }
0062 
0063     QDialogButtonBox*  buttons;
0064 
0065     AlbumSelectWidget* albumSel;
0066 
0067     SearchTextBar*     searchBar;
0068 };
0069 
0070 AlbumSelectDialog::AlbumSelectDialog(QWidget* const parent, PAlbum* const albumToSelect, const QString& header)
0071     : QDialog(parent),
0072       d      (new Private)
0073 {
0074     setWindowFlags((windowFlags() & ~Qt::Dialog) |
0075                    Qt::Window                    |
0076                    Qt::WindowCloseButtonHint     |
0077                    Qt::WindowMinMaxButtonsHint);
0078 
0079     setObjectName(QLatin1String("Select Album Dialog"));
0080     setWindowTitle(i18nc("@title:window", "Select Album"));
0081 
0082     d->buttons = new QDialogButtonBox(QDialogButtonBox::Help | QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0083     d->buttons->button(QDialogButtonBox::Ok)->setDefault(true);
0084 
0085     // -------------------------------------------------------------
0086 
0087     QWidget* const page     = new QWidget(this);
0088     QGridLayout* const grid = new QGridLayout(page);
0089     QLabel* const logo      = new QLabel(page);
0090     logo->setPixmap(QIcon::fromTheme(QLatin1String("digikam")).pixmap(QSize(48,48)));
0091 
0092     QLabel* const message   = new QLabel(page);
0093     message->setWordWrap(true);
0094 
0095     if (!header.isEmpty())
0096     {
0097         message->setText(header);
0098     }
0099 
0100     d->albumSel = new AlbumSelectWidget(page, albumToSelect, true);
0101 
0102     grid->addWidget(logo,        0, 0, 1, 1);
0103     grid->addWidget(message,     1, 0, 1, 1);
0104     grid->addWidget(d->albumSel, 0, 1, 3, 1);
0105     grid->setColumnStretch(0, 2);
0106     grid->setColumnStretch(1, 10);
0107     grid->setRowStretch(2, 10);
0108     grid->setContentsMargins(QMargins());
0109     grid->setSpacing(qMin(QApplication::style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing),
0110                           QApplication::style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing)));
0111 
0112     QVBoxLayout* const vbx = new QVBoxLayout(this);
0113     vbx->addWidget(page);
0114     vbx->addWidget(d->buttons);
0115     setLayout(vbx);
0116 
0117     // -------------------------------------------------------------
0118 
0119     connect(d->albumSel, SIGNAL(itemSelectionChanged()),
0120             this, SLOT(slotSelectionChanged()));
0121 
0122     connect(d->albumSel, SIGNAL(completerActivated()),
0123             this, SLOT(accept()));
0124 
0125     connect(d->buttons->button(QDialogButtonBox::Ok), SIGNAL(clicked()),
0126             this, SLOT(accept()));
0127 
0128     connect(d->buttons->button(QDialogButtonBox::Cancel), SIGNAL(clicked()),
0129             this, SLOT(reject()));
0130 
0131     connect(d->buttons->button(QDialogButtonBox::Help), SIGNAL(clicked()),
0132             this, SLOT(slotHelp()));
0133 
0134     // -------------------------------------------------------------
0135 
0136     KSharedConfigPtr config = KSharedConfig::openConfig();
0137     KConfigGroup group      = config->group(objectName());
0138 
0139     winId();
0140     DXmlGuiWindow::setGoodDefaultWindowSize(windowHandle());
0141     DXmlGuiWindow::restoreWindowSize(windowHandle(), group);
0142     resize(windowHandle()->size());
0143 
0144     slotSelectionChanged();
0145 }
0146 
0147 AlbumSelectDialog::~AlbumSelectDialog()
0148 {
0149     KSharedConfigPtr config = KSharedConfig::openConfig();
0150     KConfigGroup group      = config->group(objectName());
0151     DXmlGuiWindow::saveWindowSize(windowHandle(), group);
0152 
0153     delete d;
0154 }
0155 
0156 void AlbumSelectDialog::slotSelectionChanged()
0157 {
0158     PAlbum* const currentAlbum = d->albumSel->currentAlbum();
0159 
0160     bool enabled = !(!currentAlbum || currentAlbum->isRoot());
0161     d->buttons->button(QDialogButtonBox::Ok)->setEnabled(enabled);
0162 }
0163 
0164 PAlbum* AlbumSelectDialog::selectAlbum(QWidget* const parent, PAlbum* const albumToSelect, const QString& header)
0165 {
0166     QPointer<AlbumSelectDialog> dlg = new AlbumSelectDialog(parent, albumToSelect, header);
0167 
0168     if (dlg->exec() != QDialog::Accepted)
0169     {
0170         delete dlg;
0171         return nullptr;
0172     }
0173 
0174     PAlbum* const selectedAlbum = dlg->d->albumSel->currentAlbum();
0175 
0176     if (!selectedAlbum || (selectedAlbum->isRoot()))
0177     {
0178         delete dlg;
0179         return nullptr;
0180     }
0181 
0182     delete dlg;
0183 
0184     return selectedAlbum;
0185 }
0186 
0187 void AlbumSelectDialog::slotHelp()
0188 {
0189     openOnlineDocumentation(QLatin1String("main_window"), QLatin1String("albums_view"));
0190 }
0191 
0192 } // namespace Digikam
0193 
0194 #include "moc_albumselectdialog.cpp"