File indexing completed on 2024-06-02 04:16:55

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2021-08-27
0007  * Description : Showfoto folder view bookmark edit dialog
0008  *
0009  * SPDX-FileCopyrightText: 2021-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "showfotofolderviewbookmarkdlg.h"
0016 
0017 // Qt includes
0018 
0019 #include <QGridLayout>
0020 #include <QLineEdit>
0021 #include <QLabel>
0022 #include <QPointer>
0023 #include <QApplication>
0024 #include <QStyle>
0025 #include <QDir>
0026 #include <QStandardPaths>
0027 #include <QDialogButtonBox>
0028 #include <QVBoxLayout>
0029 #include <QPushButton>
0030 
0031 // KDE includes
0032 
0033 #include <klocalizedstring.h>
0034 
0035 #ifdef HAVE_KICONTHEMES
0036 #   include <kicondialog.h>
0037 #endif
0038 
0039 // Local includes
0040 
0041 #include "digikam_debug.h"
0042 #include "digikam_globals.h"
0043 #include "dfileselector.h"
0044 #include "dlayoutbox.h"
0045 #include "dtextedit.h"
0046 #include "dxmlguiwindow.h"
0047 #include "showfotofolderviewbookmarklist.h"
0048 
0049 using namespace Digikam;
0050 
0051 namespace ShowFoto
0052 {
0053 
0054 class Q_DECL_HIDDEN ShowfotoFolderViewBookmarkDlg::Private
0055 {
0056 public:
0057 
0058     explicit Private()
0059       : create         (false),
0060         topLabel       (nullptr),
0061         icon           (QLatin1String("folder")),
0062         iconButton     (nullptr),
0063         resetIconButton(nullptr),
0064         buttons        (nullptr),
0065         titleEdit      (nullptr),
0066         pathEdit       (nullptr),
0067         list           (nullptr)
0068     {
0069     }
0070 
0071     bool                            create;
0072 
0073     QLabel*                         topLabel;
0074 
0075     QString                         icon;
0076 
0077     QPushButton*                    iconButton;
0078     QPushButton*                    resetIconButton;
0079 
0080     QDialogButtonBox*               buttons;
0081 
0082     DTextEdit*                      titleEdit;
0083     DFileSelector*                  pathEdit;
0084     ShowfotoFolderViewBookmarkList* list;
0085 };
0086 
0087 ShowfotoFolderViewBookmarkDlg::ShowfotoFolderViewBookmarkDlg(ShowfotoFolderViewBookmarkList* const parent,
0088                                                              bool create)
0089     : QDialog(parent),
0090       d      (new Private)
0091 {
0092     setModal(true);
0093 
0094     d->create  = create;
0095     d->list    = parent;
0096     d->buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Help, this);
0097     d->buttons->button(QDialogButtonBox::Ok)->setDefault(true);
0098 
0099     if (d->create)
0100     {
0101         setWindowTitle(i18nc("@title:window", "New Bookmark"));
0102     }
0103     else
0104     {
0105         setWindowTitle(i18nc("@title:window", "Edit Bookmark"));
0106     }
0107 
0108     QWidget* const page      = new QWidget(this);
0109 
0110     // --------------------------------------------------------
0111 
0112     QGridLayout* const grid  = new QGridLayout(page);
0113 
0114     d->topLabel              = new QLabel(page);
0115     d->topLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
0116     d->topLabel->setWordWrap(false);
0117 
0118     // --------------------------------------------------------
0119 
0120     QLabel* const titleLabel = new QLabel(page);
0121     titleLabel->setText(i18nc("@label: bookmark properties", "&Title:"));
0122 
0123     d->titleEdit             = new DTextEdit(page);
0124     d->titleEdit->setLinesVisible(1);
0125     d->titleEdit->setPlaceholderText(i18nc("@info", "Enter bookmark title here..."));
0126     d->titleEdit->setToolTip(i18nc("@info", "The bookmark title which must be unique and not empty"));
0127     titleLabel->setBuddy(d->titleEdit);
0128 
0129     // --------------------------------------------------------
0130 
0131     QLabel* const iconTextLabel = new QLabel(page);
0132     iconTextLabel->setText(i18nc("@label", "&Icon:"));
0133 
0134     d->iconButton               = new QPushButton(page);
0135     d->iconButton->setFixedSize(40, 40);
0136     d->iconButton->setIcon(QIcon::fromTheme(d->icon));
0137     iconTextLabel->setBuddy(d->iconButton);
0138 
0139     d->resetIconButton          = new QPushButton(QIcon::fromTheme(QLatin1String("view-refresh")),
0140                                                   i18nc("@action:button", "Reset"), page);
0141 
0142 #ifndef HAVE_KICONTHEMES
0143 
0144     iconTextLabel->hide();
0145     d->iconButton->hide();
0146     d->resetIconButton->hide();
0147 
0148 #endif
0149 
0150     // --------------------------------------------------------
0151 
0152     QLabel* const pathLabel = new QLabel(page);
0153     pathLabel->setText(i18nc("@label: bookmark properties", "&Path:"));
0154 
0155     d->pathEdit             = new DFileSelector(page);
0156     d->pathEdit->setFileDlgMode(QFileDialog::Directory);
0157     d->pathEdit->setFileDlgOptions(QFileDialog::ShowDirsOnly);
0158     d->pathEdit->lineEdit()->setPlaceholderText(i18nc("@info", "Enter bookmark path here..."));
0159     pathLabel->setBuddy(d->pathEdit);
0160 
0161     // --------------------------------------------------------
0162 
0163     grid->addWidget(titleLabel,         0, 0, 1, 1);
0164     grid->addWidget(d->titleEdit,       0, 1, 1, 3);
0165     grid->addWidget(iconTextLabel,      1, 0, 1, 1);
0166     grid->addWidget(d->iconButton,      1, 1, 1, 1);
0167     grid->addWidget(d->resetIconButton, 1, 2, 1, 1);
0168     grid->addWidget(pathLabel,          2, 0, 1, 1);
0169     grid->addWidget(d->pathEdit,        2, 1, 1, 3);
0170     grid->setRowStretch(3, 10);
0171     grid->setColumnStretch(3, 10);
0172 
0173     QVBoxLayout* const vbx = new QVBoxLayout(this);
0174     vbx->addWidget(page);
0175     vbx->addWidget(d->buttons);
0176     setLayout(vbx);
0177 
0178     // --------------------------------------------------------
0179 
0180     connect(d->titleEdit, SIGNAL(textChanged()),
0181             this, SLOT(slotModified()));
0182 
0183     connect(d->pathEdit->lineEdit(), SIGNAL(textChanged(QString)),
0184             this, SLOT(slotModified()));
0185 
0186     connect(d->pathEdit, SIGNAL(signalUrlSelected(QUrl)),
0187             this, SLOT(slotModified()));
0188 
0189     connect(d->iconButton, SIGNAL(clicked()),
0190             this, SLOT(slotIconChanged()));
0191 
0192     connect(d->resetIconButton, SIGNAL(clicked()),
0193             this, SLOT(slotIconResetClicked()));
0194 
0195     connect(d->buttons->button(QDialogButtonBox::Ok), SIGNAL(clicked()),
0196             this, SLOT(slotAccept()));
0197 
0198     connect(d->buttons->button(QDialogButtonBox::Cancel), SIGNAL(clicked()),
0199             this, SLOT(reject()));
0200 
0201     connect(d->buttons->button(QDialogButtonBox::Help), SIGNAL(clicked()),
0202             this, SLOT(slotHelp()));
0203 
0204     // --------------------------------------------------------
0205 
0206     d->titleEdit->setFocus();
0207     adjustSize();
0208 }
0209 
0210 ShowfotoFolderViewBookmarkDlg::~ShowfotoFolderViewBookmarkDlg()
0211 {
0212     delete d;
0213 }
0214 
0215 bool ShowfotoFolderViewBookmarkDlg::canAccept() const
0216 {
0217     bool b = true;
0218 
0219     if (d->create)
0220     {
0221         b = !d->list->bookmarkExists(title());
0222     }
0223 
0224     return (
0225             !title().isEmpty() &&
0226             !path().isEmpty()  &&
0227             b
0228            );
0229 }
0230 
0231 void ShowfotoFolderViewBookmarkDlg::slotAccept()
0232 {
0233     if (canAccept())
0234     {
0235         accept();
0236     }
0237 }
0238 
0239 void ShowfotoFolderViewBookmarkDlg::slotModified()
0240 {
0241     d->buttons->button(QDialogButtonBox::Ok)->setEnabled(canAccept());
0242 }
0243 
0244 QString ShowfotoFolderViewBookmarkDlg::title() const
0245 {
0246     return d->titleEdit->text();
0247 }
0248 
0249 QString ShowfotoFolderViewBookmarkDlg::icon() const
0250 {
0251     return d->icon;
0252 }
0253 
0254 QString ShowfotoFolderViewBookmarkDlg::path() const
0255 {
0256     return d->pathEdit->fileDlgPath();
0257 }
0258 
0259 void ShowfotoFolderViewBookmarkDlg::setTitle(const QString& title)
0260 {
0261     d->titleEdit->setText(title);
0262 }
0263 
0264 void ShowfotoFolderViewBookmarkDlg::setIcon(const QString& icon)
0265 {
0266     d->icon = icon;
0267 }
0268 
0269 void ShowfotoFolderViewBookmarkDlg::setPath(const QString& path)
0270 {
0271     d->pathEdit->setFileDlgPath(path);
0272 }
0273 
0274 void ShowfotoFolderViewBookmarkDlg::slotIconResetClicked()
0275 {
0276     d->icon = QLatin1String("folder");
0277     d->iconButton->setIcon(QIcon::fromTheme(d->icon));
0278 }
0279 
0280 void ShowfotoFolderViewBookmarkDlg::slotIconChanged()
0281 {
0282 
0283 #ifdef HAVE_KICONTHEMES
0284 
0285     QPointer<KIconDialog> dlg = new KIconDialog(this);
0286     dlg->setup(KIconLoader::NoGroup, KIconLoader::Application, false, 20, false, false, false);
0287     QString icon              = dlg->openDialog();
0288     delete dlg;
0289 
0290     if (icon.isEmpty() || (icon == d->icon))
0291     {
0292         return;
0293     }
0294 
0295     d->icon                   = icon;
0296     d->iconButton->setIcon(QIcon::fromTheme(d->icon));
0297 
0298 #endif
0299 
0300 }
0301 
0302 bool ShowfotoFolderViewBookmarkDlg::bookmarkDialog(ShowfotoFolderViewBookmarkList* const parent,
0303                                                    QString& title,
0304                                                    QString& icon,
0305                                                    QString& path,
0306                                                    bool create)
0307 {
0308     QPointer<ShowfotoFolderViewBookmarkDlg> dlg = new ShowfotoFolderViewBookmarkDlg(parent, create);
0309     dlg->setTitle(title);
0310     dlg->setIcon(icon);
0311     dlg->setPath(path);
0312 
0313     bool valRet = dlg->exec();
0314 
0315     if (valRet == QDialog::Accepted)
0316     {
0317         title = dlg->title();
0318         icon  = dlg->icon();
0319         path  = dlg->path();
0320 
0321         if (!path.endsWith(QDir::separator()))
0322         {
0323             path.append(QDir::separator());
0324         }
0325     }
0326 
0327     delete dlg;
0328 
0329     return valRet;
0330 }
0331 
0332 void ShowfotoFolderViewBookmarkDlg::slotHelp()
0333 {
0334     Digikam::openOnlineDocumentation(QLatin1String("showfoto_editor"),
0335                                      QLatin1String("showfoto_leftsidebar"),
0336                                      QLatin1String("showfoto-folderstab"));
0337 }
0338 
0339 } // namespace ShowFoto
0340 
0341 #include "moc_showfotofolderviewbookmarkdlg.cpp"