Debian Codegraph @

core/utilities/facemanagement/widgets/assignnamewidget_p.cpp

raw
1 /* ============================================================
2 *
3 * This file is a part of digiKam project
4 * https://www.digikam.org
5 *
6 * Date : 2010-09-12
7 * Description : Widget for assignment and confirmation of names for faces
8 *
9 * SPDX-FileCopyrightText: 2010 by Aditya Bhatt <adityabhatt1991 at gmail dot com>
10 * SPDX-FileCopyrightText: 2010-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
11 * SPDX-FileCopyrightText: 2012-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
12 *
13 * SPDX-License-Identifier: GPL-2.0-or-later
14 *
15 * ============================================================ */
16
17 #include "assignnamewidget_p.h"
18
19 namespace Digikam
20 {
21
22 AssignNameWidget::Private::Private(AssignNameWidget* const qq)
23 : q(qq)
24 {
25 }
26
27 QWidget* AssignNameWidget::Private::addTagsWidget() const
28 {
29 if (comboBox)
30 {
31 return comboBox;
32 }
33 else
34 {
35 return lineEdit;
36 }
37 }
38
39 bool AssignNameWidget::Private::isValid() const
40 {
41 return (
42 (mode != InvalidMode) &&
43 (layoutMode != InvalidLayout) &&
44 (visualStyle != InvalidVisualStyle) &&
45 (widgetMode != InvalidTagEntryWidgetMode)
46 );
47 }
48
49 void AssignNameWidget::Private::clearWidgets()
50 {
51 delete comboBox;
52 comboBox = nullptr;
53
54 delete lineEdit;
55 lineEdit = nullptr;
56
57 delete confirmButton;
58 confirmButton = nullptr;
59
60 delete rejectButton;
61 rejectButton = nullptr;
62
63 delete ignoreButton;
64 ignoreButton = nullptr;
65
66 delete clickLabel;
67 clickLabel = nullptr;
68 }
69
70 QToolButton* AssignNameWidget::Private::createToolButton(const QIcon& icon,
71 const QString& text,
72 const QString& tip) const
73 {
74 QToolButton* const b = new QToolButton;
75 b->setIcon(icon);
76 b->setText(text);
77 b->setToolTip(tip);
78 b->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
79
80 return b;
81 }
82
83 void AssignNameWidget::Private::updateModes()
84 {
85 if (isValid())
86 {
87 clearWidgets();
88 checkWidgets();
89 updateIgnoreButton();
90 updateLayout();
91 updateVisualStyle();
92 }
93 }
94
95 template <class T>
96 void AssignNameWidget::Private::setupAddTagsWidget(T* const widget)
97 {
98 if (modelsGiven)
99 {
100 widget->setAlbumModels(tagModel, tagFilteredModel, tagFilterModel);
101 }
102
103 if (parentTag)
104 {
105 widget->setParentTag(parentTag);
106 }
107
108 q->connect(widget, SIGNAL(taggingActionActivated(TaggingAction)),
109 q, SLOT(slotActionActivated(TaggingAction)));
110
111 q->connect(widget, SIGNAL(taggingActionSelected(TaggingAction)),
112 q, SLOT(slotActionSelected(TaggingAction)));
113 }
114
115 void AssignNameWidget::Private::checkWidgets()
116 {
117 if (!isValid())
118 {
119 return;
120 }
121
122 switch (mode)
123 {
124 case InvalidMode:
125 {
126 break;
127 }
128
129 case UnconfirmedEditMode:
130 case ConfirmedEditMode:
131 {
132 switch (widgetMode)
133 {
134 case InvalidTagEntryWidgetMode:
135 {
136 break;
137 }
138
139 case AddTagsComboBoxMode:
140 {
141 if (!comboBox)
142 {
143 comboBox = new AddTagsComboBox(q);
144 setupAddTagsWidget(comboBox);
145 }
146
147 break;
148 }
149
150 case AddTagsLineEditMode:
151 {
152 if (!lineEdit)
153 {
154 lineEdit = new AddTagsLineEdit(q);
155 setupAddTagsWidget(lineEdit);
156 }
157
158 break;
159 }
160 }
161
162 if (!confirmButton)
163 {
164 confirmButton = createToolButton(QIcon::fromTheme(QLatin1String("dialog-ok-apply")), i18nc("@action", "OK"));
165 confirmButton->setToolTip(i18nc("@info:tooltip", "Confirm that the selected person is shown here"));
166
167 if (mode == UnconfirmedEditMode)
168 {
169 confirmButton->setText(i18nc("@action", "Confirm"));
170 }
171
172 q->connect(confirmButton, SIGNAL(clicked()),
173 q, SLOT(slotConfirm()));
174 }
175
176 if (!rejectButton)
177 {
178 rejectButton = createToolButton(QIcon::fromTheme(QLatin1String("list-remove")), i18nc("@action", "Remove"));
179 rejectButton->setToolTip(i18nc("@info:tooltip", "Remove this face region"));
180
181 q->connect(rejectButton, SIGNAL(clicked()),
182 q, SLOT(slotReject()));
183 }
184
185 break;
186 }
187
188 case IgnoredMode:
189 {
190 if (layoutMode == Compact)
191 {
192 if (!confirmButton)
193 {
194 confirmButton = createToolButton(QIcon::fromTheme(QLatin1String("edit-undo")), i18nc("@action", "OK"));
195 confirmButton->setToolTip(i18nc("@info:tooltip", "Unmark this face as Ignored"));
196
197 q->connect(confirmButton, SIGNAL(clicked()),
198 q, SLOT(slotIgnoredClicked()));
199 }
200
201 if (!rejectButton)
202 {
203 rejectButton = createToolButton(QIcon::fromTheme(QLatin1String("list-remove")), i18nc("@action", "Remove"));
204 rejectButton->setToolTip(i18nc("@info:tooltip", "Remove this face region"));
205
206 q->connect(rejectButton, SIGNAL(clicked()),
207 q, SLOT(slotReject()));
208 }
209 }
210 else
211 {
212 clickLabel = new DClickLabel;
213 clickLabel->setAlignment(Qt::AlignCenter);
214
215 connect(clickLabel, SIGNAL(activated()),
216 q, SLOT(slotIgnoredClicked()));
217 }
218
219 break;
220 }
221
222 case ConfirmedMode:
223 {
224 clickLabel = new DClickLabel;
225 clickLabel->setAlignment(Qt::AlignCenter);
226
227 connect(clickLabel, SIGNAL(activated()),
228 q, SLOT(slotLabelClicked()));
229
230 break;
231 }
232 }
233 }
234
235 void AssignNameWidget::Private::layoutAddTagsWidget(bool exceedBounds, int minimumContentsLength)
236 {
237 if (comboBox)
238 {
239 comboBox->setMinimumContentsLength(minimumContentsLength);
240 comboBox->lineEdit()->setAllowExceedBound(exceedBounds);
241 }
242 else if (lineEdit)
243 {
244 lineEdit->setAllowExceedBound(exceedBounds);
245 }
246 }
247
248 void AssignNameWidget::Private::setSizePolicies(QSizePolicy::Policy h, QSizePolicy::Policy v)
249 {
250 confirmButton->setSizePolicy(h, v);
251 rejectButton->setSizePolicy(h, v);
252
253 if (ignoreButton)
254 {
255 ignoreButton->setSizePolicy(h, v);
256 }
257
258 addTagsWidget()->setSizePolicy(h, v);
259 }
260
261 void AssignNameWidget::Private::setToolButtonStyles(Qt::ToolButtonStyle style)
262 {
263 confirmButton->setToolButtonStyle(style);
264 rejectButton->setToolButtonStyle(style);
265
266 if (ignoreButton)
267 {
268 ignoreButton->setToolButtonStyle(style);
269 }
270 }
271
272 void AssignNameWidget::Private::updateLayout()
273 {
274 if (!isValid())
275 {
276 return;
277 }
278
279 delete layout;
280 layout = new QGridLayout;
281
282 switch (mode)
283 {
284 case InvalidMode:
285 {
286 break;
287 }
288
289 case UnconfirmedEditMode:
290 case ConfirmedEditMode:
291 {
292 switch (layoutMode)
293 {
294 case InvalidLayout:
295 {
296 break;
297 }
298
299 case FullLine:
300 {
301 layout->addWidget(addTagsWidget(), 0, 0);
302 layout->addWidget(confirmButton, 0, 1);
303 layout->addWidget(rejectButton, 0, 2);
304 layout->addWidget(ignoreButton, 0, 3);
305 layout->setColumnStretch(0, 1);
306
307 setSizePolicies(QSizePolicy::Expanding, QSizePolicy::Preferred);
308 setToolButtonStyles(Qt::ToolButtonTextBesideIcon);
309 layoutAddTagsWidget(false, 15);
310
311 break;
312 }
313
314 case TwoLines:
315 case Compact:
316 {
317 layout->addWidget(addTagsWidget(), 0, 0, 1, 3);
318 layout->addWidget(confirmButton, 1, 0);
319 layout->addWidget(rejectButton, 1, 1);
320 layout->addWidget(ignoreButton, 1, 2);
321
322 setSizePolicies(QSizePolicy::Expanding, QSizePolicy::Minimum);
323
324 if (layoutMode == AssignNameWidget::TwoLines)
325 {
326 setToolButtonStyles(Qt::ToolButtonTextBesideIcon);
327 layoutAddTagsWidget(true, 10);
328 }
329 else
330 {
331 setToolButtonStyles(Qt::ToolButtonIconOnly);
332 layoutAddTagsWidget(true, 0);
333 }
334
335 break;
336 }
337 }
338
339 break;
340 }
341
342 case IgnoredMode:
343 {
344 if (layoutMode == Compact)
345 {
346 layout->addWidget(confirmButton, 0, 0);
347 layout->addWidget(rejectButton, 0, 1);
348
349 confirmButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
350 rejectButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
351 setToolButtonStyles(Qt::ToolButtonIconOnly);
352 layoutAddTagsWidget(true, 0);
353 }
354 else
355 {
356 layout->addWidget(clickLabel, 0, 0);
357 }
358
359 break;
360 }
361
362 case ConfirmedMode:
363 {
364 layout->addWidget(clickLabel, 0, 0);
365
366 break;
367 }
368 }
369
370 layout->setContentsMargins(1, 1, 1, 1);
371 layout->setSpacing(1);
372 q->setLayout(layout);
373 }
374
375 QString AssignNameWidget::Private::styleSheetFontDescriptor(const QFont& font) const
376 {
377 QString s;
378
379 s += (font.pointSize() == -1) ? QString::fromUtf8("font-size: %1px; ").arg(font.pixelSize())
380 : QString::fromUtf8("font-size: %1pt; ").arg(font.pointSize());
381
382 s += QString::fromUtf8("font-family: \"%1\"; ").arg(font.family());
383
384 return s;
385 }
386
387 void AssignNameWidget::Private::updateVisualStyle()
388 {
389 if (!isValid())
390 {
391 return;
392 }
393
394 QFont appFont = ApplicationSettings::instance()->getApplicationFont();
395
396 if (appFont.pointSize() == -1)
397 {
398 appFont.setPixelSize(appFont.pixelSize() - 1);
399 }
400 else
401 {
402 appFont.setPointSize(appFont.pointSize() - 1);
403 }
404
405 switch (visualStyle)
406 {
407 case InvalidVisualStyle:
408 {
409 break;
410 }
411
412 case TranslucentDarkRound:
413 {
414 q->setStyleSheet(
415 QString::fromUtf8(
416 "QWidget { "
417 " %1 "
418 "} "
419
420 "QFrame {"
421 " background-color: rgba(0, 0, 0, 66%); "
422 " border: 1px solid rgba(100, 100, 100, 66%); "
423 " border-radius: %2px; "
424 "} "
425
426 "QToolButton { "
427 " color: rgba(255, 255, 255, 220); "
428 " padding: 1px; "
429 " background-color: "
430 " qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgba(255, 255, 255, 100), "
431 " stop:1 rgba(255, 255, 255, 0)); "
432 " border: 1px solid rgba(255, 255, 255, 127); "
433 " border-radius: 4px; "
434 "} "
435
436 "QToolButton:hover { "
437 " border-color: white; "
438 "} "
439
440 "QToolButton:pressed { "
441 " background-color: "
442 " qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgba(255, 255, 255, 0), "
443 " stop:1 rgba(255, 255, 255, 100)); "
444 " border-color: white; "
445 "} "
446
447 "QToolButton:disabled { "
448 " color: rgba(255, 255, 255, 120); "
449 " background-color: "
450 " qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgba(255, 255, 255, 50), "
451 " stop:1 rgba(255, 255, 255, 0)); "
452 "} "
453
454 "QComboBox { "
455 " color: white; "
456 " background-color: transparent; "
457 "} "
458
459 "QComboBox QAbstractItemView, QListView::item:!selected { "
460 " color: white; "
461 " background-color: rgba(0, 0, 0, 80%); "
462 "} "
463
464 "QLabel { "
465 " color: white; background-color: transparent; border: none; "
466 "}"
467 ).arg(styleSheetFontDescriptor(appFont))
468 .arg(((mode == ConfirmedMode) || (mode == IgnoredMode)) ? QLatin1String("8")
469 : QLatin1String("4"))
470 );
471
472 break;
473 }
474
475 case TranslucentThemedFrameless:
476 {
477 QColor bg = qApp->palette().color(QPalette::Base);
478
479 q->setStyleSheet(
480 QString::fromUtf8(
481 "QWidget { "
482 " %1 "
483 "} "
484
485 "QFrame#assignNameWidget {"
486 " background-color: "
487 " qradialgradient(cx:0.5 cy:0.5, fx:0, fy:0, radius: 1, stop:0 rgba(%2, %3, %4, 255), "
488 " stop:0.8 rgba(%2, %3, %4, 200), stop:1 rgba(%2, %3, %4, 0));"
489 " border: none; "
490 " border-radius: 8px; "
491 "}"
492 ).arg(styleSheetFontDescriptor(appFont))
493 .arg(bg.red())
494 .arg(bg.green())
495 .arg(bg.blue())
496 );
497
498 break;
499 }
500
501 case StyledFrame:
502 {
503 q->setStyleSheet(QString());
504 q->setFrameStyle(Raised | StyledPanel);
505
506 break;
507 }
508 }
509 }
510
511 template <class T>
512 void AssignNameWidget::Private::setAddTagsWidgetContents(T* const widget)
513 {
514 if (widget)
515 {
516 widget->setPlaceholderText((mode == UnconfirmedEditMode) ? i18nc("@label", "Who is this?")
517 : QString());
518 widget->setCurrentTag(currentTag);
519
520 if (confirmButton)
521 {
522 confirmButton->setEnabled(widget->currentTaggingAction().isValid());
523 }
524 }
525 }
526
527 void AssignNameWidget::Private::updateContents()
528 {
529 if (!isValid())
530 {
531 return;
532 }
533
534 if (comboBox)
535 {
536 setAddTagsWidgetContents(comboBox);
537 }
538 else if (lineEdit)
539 {
540 setAddTagsWidgetContents(lineEdit);
541 }
542
543 if (clickLabel)
544 {
545 if (mode != IgnoredMode)
546 {
547 clickLabel->setText(currentTag ? currentTag->title()
548 : QString());
549 }
550 else
551 {
552 int tagId = FaceTags::ignoredPersonTagId();
553 clickLabel->setText(FaceTags::faceNameForTag(tagId));
554 }
555 }
556 }
557
558 void AssignNameWidget::Private::updateIgnoreButton()
559 {
560 if (!ignoreButton)
561 {
562 ignoreButton = createToolButton(QIcon::fromTheme(QLatin1String("dialog-cancel")),
563 i18nc("@action", "Ignore"));
564 ignoreButton->setToolTip(i18nc("@info:tooltip", "Ignore this face"));
565
566 q->connect(ignoreButton, SIGNAL(clicked()),
567 q, SLOT(slotIgnore()));
568 }
569 else
570 {
571 FaceTagsIface face = FaceTagsIface::fromVariant(faceIdentifier);
572
573 if (
574 (face.type() == FaceTagsIface::IgnoredName) ||
575 (face.type() == FaceTagsIface::UnknownName)
576 )
577 {
578 ignoreButton->hide();
579 }
580 else
581 {
582 ignoreButton->show();
583 }
584 }
585 }
586
587 void AssignNameWidget::Private::updateRejectButton()
588 {
589 if (!rejectButton)
590 {
591 return;
592 }
593
594 FaceTagsIface face = FaceTagsIface::fromVariant(faceIdentifier);
595
596 if (face.type() == FaceTagsIface::UnknownName)
597 {
598 rejectButton->setToolTip(i18nc("@info:tooltip", "Mark this face as Ignored"));
599 rejectButton->setIcon(QIcon::fromTheme(QLatin1String("dialog-cancel")));
600 }
601 else if (face.type() == FaceTagsIface::UnconfirmedName)
602 {
603 rejectButton->setToolTip(i18nc("@info:tooltip", "Reject this suggestion"));
604 rejectButton->setIcon(QIcon::fromTheme(QLatin1String("list-remove")));
605 }
606
607 updateIgnoreButton();
608 }
609
610 } // namespace Digikam