File indexing completed on 2025-01-05 03:59:47
0001 /* ============================================================ 0002 * 0003 * This file is a part of digiKam project 0004 * https://www.digikam.org 0005 * 0006 * Date : 2006-07-24 0007 * Description : a dialog to select a camera folders. 0008 * 0009 * SPDX-FileCopyrightText: 2006-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 "camerafolderdialog.h" 0016 0017 // Qt includes 0018 0019 #include <QLabel> 0020 #include <QFrame> 0021 #include <QGridLayout> 0022 #include <QStandardPaths> 0023 #include <QApplication> 0024 #include <QStyle> 0025 #include <QDialogButtonBox> 0026 #include <QVBoxLayout> 0027 #include <QPushButton> 0028 0029 // KDE includes 0030 0031 #include <klocalizedstring.h> 0032 0033 // Local includes 0034 0035 #include "digikam_debug.h" 0036 #include "digikam_globals.h" 0037 #include "camerafolderitem.h" 0038 #include "camerafolderview.h" 0039 #include "dxmlguiwindow.h" 0040 0041 namespace Digikam 0042 { 0043 0044 class Q_DECL_HIDDEN CameraFolderDialog::Private 0045 { 0046 public: 0047 0048 explicit Private() 0049 : buttons (nullptr), 0050 folderView(nullptr) 0051 { 0052 } 0053 0054 QString rootPath; 0055 QDialogButtonBox* buttons; 0056 0057 CameraFolderView* folderView; 0058 }; 0059 0060 CameraFolderDialog::CameraFolderDialog(QWidget* const parent, const QMap<QString, int>& map, 0061 const QString& cameraName, const QString& rootPath) 0062 : QDialog(parent), 0063 d (new Private) 0064 { 0065 setModal(true); 0066 setWindowTitle(i18nc("@title:window %1: name of the camera", "%1 - Select Camera Folder", cameraName)); 0067 0068 const int spacing = qMin(QApplication::style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing), 0069 QApplication::style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing)); 0070 0071 d->buttons = new QDialogButtonBox(QDialogButtonBox::Help | QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); 0072 d->buttons->button(QDialogButtonBox::Ok)->setDefault(true); 0073 d->buttons->button(QDialogButtonBox::Ok)->setEnabled(false); 0074 0075 d->rootPath = rootPath; 0076 QFrame* const page = new QFrame(this); 0077 0078 QGridLayout* const grid = new QGridLayout(page); 0079 d->folderView = new CameraFolderView(page); 0080 QLabel* const logo = new QLabel(page); 0081 QLabel* const message = new QLabel(page); 0082 0083 logo->setPixmap(QIcon::fromTheme(QLatin1String("digikam")).pixmap(QSize(48,48))); 0084 0085 message->setText(i18nc("@info", "Please select the camera folder " 0086 "where you want to upload the images.")); 0087 message->setWordWrap(true); 0088 0089 grid->addWidget(logo, 0, 0, 1, 1); 0090 grid->addWidget(message, 1, 0, 1, 1); 0091 grid->addWidget(d->folderView, 0, 1, 3, 1); 0092 grid->setColumnStretch(0, 2); 0093 grid->setColumnStretch(1, 10); 0094 grid->setRowStretch(2, 10); 0095 grid->setContentsMargins(spacing, spacing, spacing, spacing); 0096 grid->setSpacing(spacing); 0097 0098 QVBoxLayout* const vbx = new QVBoxLayout(this); 0099 vbx->addWidget(page); 0100 vbx->addWidget(d->buttons); 0101 setLayout(vbx); 0102 0103 d->folderView->addVirtualFolder(cameraName); 0104 d->folderView->addRootFolder(QLatin1String("/")); 0105 0106 for (QMap<QString, int>::const_iterator it = map.constBegin() ; 0107 it != map.constEnd() ; ++it) 0108 { 0109 QString folder(it.key()); 0110 0111 if (folder != QLatin1String("/")) 0112 { 0113 if (folder.startsWith(rootPath) && rootPath != QLatin1String("/")) 0114 { 0115 folder.remove(0, rootPath.length()); 0116 } 0117 0118 QString path(QLatin1Char('/')); 0119 0120 Q_FOREACH (const QString& sub, folder.split(QLatin1Char('/'), QT_SKIP_EMPTY_PARTS)) 0121 { 0122 qCDebug(DIGIKAM_IMPORTUI_LOG) << "Camera folder:" << path << "Subfolder:" << sub; 0123 d->folderView->addFolder(path, sub, it.value()); 0124 path += sub + QLatin1Char('/'); 0125 } 0126 } 0127 } 0128 0129 connect(d->folderView, SIGNAL(signalFolderChanged(CameraFolderItem*)), 0130 this, SLOT(slotFolderPathSelectionChanged(CameraFolderItem*))); 0131 0132 connect(d->buttons->button(QDialogButtonBox::Ok), SIGNAL(clicked()), 0133 this, SLOT(accept())); 0134 0135 connect(d->buttons->button(QDialogButtonBox::Cancel), SIGNAL(clicked()), 0136 this, SLOT(reject())); 0137 0138 connect(d->buttons->button(QDialogButtonBox::Help), SIGNAL(clicked()), 0139 this, SLOT(slotHelp())); 0140 0141 adjustSize(); 0142 0143 // make sure the ok button is properly set up 0144 0145 d->buttons->button(QDialogButtonBox::Ok)->setEnabled(d->folderView->currentItem() != nullptr); 0146 } 0147 0148 CameraFolderDialog::~CameraFolderDialog() 0149 { 0150 delete d; 0151 } 0152 0153 QString CameraFolderDialog::selectedFolderPath() const 0154 { 0155 QTreeWidgetItem* const item = d->folderView->currentItem(); 0156 0157 if (!item) 0158 { 0159 return QString(); 0160 } 0161 0162 CameraFolderItem* const folderItem = dynamic_cast<CameraFolderItem*>(item); 0163 0164 if (!folderItem) 0165 { 0166 return QString(); 0167 } 0168 0169 if (folderItem->isVirtualFolder()) 0170 { 0171 return QString(d->rootPath); 0172 } 0173 0174 // Case of Gphoto2 cameras. No need to duplicate root '/'. 0175 0176 if (d->rootPath == QLatin1String("/")) 0177 { 0178 return(folderItem->folderPath()); 0179 } 0180 0181 return (d->rootPath + folderItem->folderPath()); 0182 } 0183 0184 void CameraFolderDialog::slotFolderPathSelectionChanged(CameraFolderItem* item) 0185 { 0186 if (item) 0187 { 0188 d->buttons->button(QDialogButtonBox::Ok)->setEnabled(true); 0189 qCDebug(DIGIKAM_IMPORTUI_LOG) << "Camera folder path: " << selectedFolderPath(); 0190 } 0191 else 0192 { 0193 d->buttons->button(QDialogButtonBox::Ok)->setEnabled(false); 0194 } 0195 } 0196 0197 void CameraFolderDialog::slotHelp() 0198 { 0199 openOnlineDocumentation(QLatin1String("import_tools"), QLatin1String("camera_import"), QLatin1String("camera-upload")); 0200 } 0201 0202 } // namespace Digikam 0203 0204 #include "moc_camerafolderdialog.cpp"