File indexing completed on 2025-01-19 03:57:57

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2010-09-12
0007  * Description : Widget for assignment and confirmation of names for faces
0008  *
0009  * SPDX-FileCopyrightText: 2010      by Aditya Bhatt <adityabhatt1991 at gmail dot com>
0010  * SPDX-FileCopyrightText: 2010-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0011  * SPDX-FileCopyrightText: 2012-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 "assignnamewidget_p.h"
0018 
0019 namespace Digikam
0020 {
0021 
0022 AssignNameWidget::AssignNameWidget(QWidget* const parent)
0023     : QFrame(parent),
0024       d     (new Private(this))
0025 {
0026     setObjectName(QLatin1String("assignNameWidget"));
0027     setVisualStyle(StyledFrame);
0028 }
0029 
0030 AssignNameWidget::~AssignNameWidget()
0031 {
0032     delete d;
0033 }
0034 
0035 void AssignNameWidget::setDefaultModel()
0036 {
0037     setAlbumModels(nullptr, nullptr, nullptr);
0038 }
0039 
0040 void AssignNameWidget::setAlbumModels(TagModel* const model,
0041                                       TagPropertiesFilterModel* const filteredModel,
0042                                       CheckableAlbumFilterModel* const filterModel)
0043 {
0044     // Restrict the tag properties filter model to people if configured.
0045 
0046     ApplicationSettings* const settings = ApplicationSettings::instance();
0047 
0048     if      (settings)
0049     {
0050         if (filteredModel && settings->showOnlyPersonTagsInPeopleSidebar())
0051         {
0052             filteredModel->listOnlyTagsWithProperty(TagPropertyName::person());
0053         }
0054     }
0055 
0056     if      (d->comboBox)
0057     {
0058         d->comboBox->setAlbumModels(model, filteredModel, filterModel);
0059     }
0060     else if (d->lineEdit)
0061     {
0062         d->lineEdit->setAlbumModels(model, filteredModel, filterModel);
0063     }
0064 
0065     if (model || filteredModel || filterModel)
0066     {
0067         // possibly set later on box
0068 
0069         d->modelsGiven      = true;
0070         d->tagModel         = model;
0071         d->tagFilterModel   = filterModel;
0072         d->tagFilteredModel = filteredModel;
0073     }
0074 }
0075 
0076 void AssignNameWidget::setParentTag(TAlbum* album)
0077 {
0078     d->parentTag = album;
0079 
0080     if      (d->comboBox)
0081     {
0082         d->comboBox->setParentTag(album);
0083     }
0084     else if (d->lineEdit)
0085     {
0086         d->lineEdit->setParentTag(album);
0087     }
0088 }
0089 
0090 AddTagsComboBox* AssignNameWidget::comboBox() const
0091 {
0092     return d->comboBox;
0093 }
0094 
0095 AddTagsLineEdit* AssignNameWidget::lineEdit() const
0096 {
0097     return d->lineEdit;
0098 }
0099 
0100 void AssignNameWidget::setMode(Mode mode)
0101 {
0102     /**
0103      * Reject tooltip and icon should be updated even if the
0104      * same mode is passed, because Unconfirmed and Unknown
0105      * Faces have the same mode but different tooltips and icons.
0106      */
0107     if ((d->layoutMode == Compact)                    &&
0108         (mode == AssignNameWidget::UnconfirmedEditMode))
0109     {
0110         d->updateRejectButton();
0111     }
0112 
0113     if (mode == d->mode)
0114     {
0115         return;
0116     }
0117 
0118     d->mode = mode;
0119     d->updateModes();
0120     d->updateContents();
0121 }
0122 
0123 AssignNameWidget::Mode AssignNameWidget::mode() const
0124 {
0125     return d->mode;
0126 }
0127 
0128 void AssignNameWidget::setTagEntryWidgetMode(TagEntryWidgetMode mode)
0129 {
0130     if (d->widgetMode == mode)
0131     {
0132         return;
0133     }
0134 
0135     d->widgetMode = mode;
0136     d->updateModes();
0137     d->updateContents();
0138 }
0139 
0140 AssignNameWidget::TagEntryWidgetMode AssignNameWidget::tagEntryWidgetMode() const
0141 {
0142     return d->widgetMode;
0143 }
0144 
0145 void AssignNameWidget::setLayoutMode(LayoutMode mode)
0146 {
0147     if (d->layoutMode == mode)
0148     {
0149         return;
0150     }
0151 
0152     d->layoutMode = mode;
0153     d->updateModes();
0154     d->updateContents();
0155 }
0156 
0157 AssignNameWidget::LayoutMode AssignNameWidget::layoutMode() const
0158 {
0159     return d->layoutMode;
0160 }
0161 
0162 void AssignNameWidget::setVisualStyle(VisualStyle style)
0163 {
0164     if (d->visualStyle == style)
0165     {
0166         return;
0167     }
0168 
0169     d->visualStyle = style;
0170     d->updateModes();
0171 }
0172 
0173 AssignNameWidget::VisualStyle AssignNameWidget::visualStyle() const
0174 {
0175     return d->visualStyle;
0176 }
0177 
0178 void AssignNameWidget::setUserData(const ItemInfo& info, const QVariant& faceIdentifier)
0179 {
0180     d->info            = info;
0181     d->faceIdentifier  = faceIdentifier;
0182 
0183     FaceTagsIface face = FaceTagsIface::fromVariant(faceIdentifier);
0184 
0185     /**
0186      * Ignored faces are drawn over with a different
0187      * overlay, as Reject button should be disabled.
0188      */
0189     if      (face.type() == FaceTagsIface::ConfirmedName)
0190     {
0191         setMode(AssignNameWidget::ConfirmedMode);
0192     }
0193     else if (face.type() == FaceTagsIface::IgnoredName)
0194     {
0195         setMode(AssignNameWidget::IgnoredMode);
0196     }
0197     else
0198     {
0199         setMode(AssignNameWidget::UnconfirmedEditMode);
0200     }
0201 }
0202 
0203 ItemInfo AssignNameWidget::info() const
0204 {
0205     return d->info;
0206 }
0207 
0208 QVariant AssignNameWidget::faceIdentifier() const
0209 {
0210     return d->faceIdentifier;
0211 }
0212 
0213 void AssignNameWidget::setCurrentFace(const FaceTagsIface& face)
0214 {
0215     TAlbum* album = nullptr;
0216 
0217     if (!face.isNull() && !face.isUnknownName() && !face.isIgnoredName())
0218     {
0219         album = AlbumManager::instance()->findTAlbum(face.tagId());
0220     }
0221 
0222     setCurrentTag(album);
0223 }
0224 
0225 void AssignNameWidget::setCurrentTag(int tagId)
0226 {
0227     setCurrentTag(AlbumManager::instance()->findTAlbum(tagId));
0228 }
0229 
0230 void AssignNameWidget::setCurrentTag(TAlbum* album)
0231 {
0232     if (d->currentTag == album)
0233     {
0234         if (d->clickLabel && (d->mode != IgnoredMode))
0235         {
0236             d->clickLabel->setText(d->currentTag ? d->currentTag->title()
0237                                                  : QString());
0238         }
0239 
0240         return;
0241     }
0242 
0243     d->currentTag = album;
0244     d->updateContents();
0245 }
0246 
0247 void AssignNameWidget::slotConfirm()
0248 {
0249     if      (d->comboBox)
0250     {
0251         Q_EMIT assigned(d->comboBox->currentTaggingAction(), d->info, d->faceIdentifier);
0252     }
0253     else if (d->lineEdit)
0254     {
0255         Q_EMIT assigned(d->lineEdit->currentTaggingAction(), d->info, d->faceIdentifier);
0256     }
0257 }
0258 
0259 void AssignNameWidget::slotReject()
0260 {
0261     Q_EMIT rejected(d->info, d->faceIdentifier);
0262 }
0263 
0264 void AssignNameWidget::slotIgnore()
0265 {
0266     Q_EMIT ignored(d->info, d->faceIdentifier);
0267 }
0268 
0269 void AssignNameWidget::slotActionActivated(const TaggingAction& action)
0270 {
0271     Q_EMIT assigned(action, d->info, d->faceIdentifier);
0272 }
0273 
0274 void AssignNameWidget::slotActionSelected(const TaggingAction& action)
0275 {
0276     if (d->confirmButton)
0277     {
0278         d->confirmButton->setEnabled(action.isValid());
0279     }
0280 
0281     Q_EMIT selected(action, d->info, d->faceIdentifier);
0282 }
0283 
0284 void AssignNameWidget::slotIgnoredClicked()
0285 {
0286     Q_EMIT ignoredClicked(d->info, d->faceIdentifier);
0287 }
0288 
0289 void AssignNameWidget::slotLabelClicked()
0290 {
0291     Q_EMIT labelClicked(d->info, d->faceIdentifier);
0292 }
0293 
0294 void AssignNameWidget::keyPressEvent(QKeyEvent* e)
0295 {
0296     switch (e->key())
0297     {
0298         case Qt::Key_Enter:
0299         case Qt::Key_Return:
0300         {
0301             return;
0302         }
0303     }
0304 
0305     QWidget::keyPressEvent(e);
0306 }
0307 
0308 void AssignNameWidget::showEvent(QShowEvent* e)
0309 {
0310     if ((d->mode == UnconfirmedEditMode) || (d->mode == ConfirmedEditMode))
0311     {
0312         if      (d->comboBox)
0313         {
0314             d->comboBox->setMinimumWidth(qMax(250, size().width() - 4));
0315             d->comboBox->lineEdit()->selectAll();
0316             d->comboBox->lineEdit()->setFocus();
0317         }
0318         else if (d->lineEdit)
0319         {
0320             d->lineEdit->selectAll();
0321             d->lineEdit->setFocus();
0322         }
0323     }
0324 
0325     QWidget::showEvent(e);
0326 }
0327 
0328 } // namespace Digikam
0329 
0330 #include "moc_assignnamewidget.cpp"