Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickfontdialogimpl.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
6
7#include <QtQuickTemplates2/private/qquickdialogbuttonbox_p_p.h>
8#include <private/qfontdatabase_p.h>
9
10#include <QRegularExpression>
11
13
14Q_STATIC_LOGGING_CATEGORY(lcAttachedProperty, "qt.quick.dialogs.quickfontdialogimpl.attachedOrWarn")
15
16QQuickFontDialogImplPrivate::QQuickFontDialogImplPrivate()
17{
18}
19
20QQuickFontDialogImplAttached *QQuickFontDialogImplPrivate::attachedOrWarn()
21{
22 Q_Q(QQuickFontDialogImpl);
23 QQuickFontDialogImplAttached *attached = static_cast<QQuickFontDialogImplAttached *>(
24 qmlAttachedPropertiesObject<QQuickFontDialogImpl>(q));
25 if (!attached) {
26 qCWarning(lcAttachedProperty)
27 << "Expected FontDialogImpl attached object to be present on" << this;
28 }
29 return attached;
30}
31
33
34void QQuickFontDialogImplPrivate::handleClick(QQuickAbstractButton *button)
35{
36 Q_Q(QQuickFontDialogImpl);
37 if (buttonRole(button) == QPlatformDialogHelper::AcceptRole) {
38 q->accept();
39 QQuickDialogPrivate::handleClick(button);
40 }
41}
42
43QQuickFontDialogImpl::QQuickFontDialogImpl(QObject *parent)
44 : QQuickDialog(*(new QQuickFontDialogImplPrivate), parent)
45{
46 setPopupType(QQuickPopup::Window);
47}
48
49QQuickFontDialogImplAttached *QQuickFontDialogImpl::qmlAttachedProperties(QObject *object)
50{
51 return new QQuickFontDialogImplAttached(object);
52}
53
54QSharedPointer<QFontDialogOptions> QQuickFontDialogImpl::options() const
55{
56 Q_D(const QQuickFontDialogImpl);
57
58 return d->options;
59}
60
61void QQuickFontDialogImpl::setOptions(const QSharedPointer<QFontDialogOptions> &options)
62{
63 Q_D(QQuickFontDialogImpl);
64
65 if (options == d->options)
66 return;
67
68 d->options = options;
69
70 emit optionsChanged();
71}
72
73QFont QQuickFontDialogImpl::currentFont() const
74{
75 Q_D(const QQuickFontDialogImpl);
76 return d->currentFont;
77}
78
79void QQuickFontDialogImpl::setCurrentFont(const QFont &font, bool selectInListViews)
80{
81 Q_D(QQuickFontDialogImpl);
82
83 if (font == d->currentFont)
84 return;
85
86 d->currentFont = font;
87
88 emit currentFontChanged(font);
89
90 if (!selectInListViews)
91 return;
92
93 QQuickFontDialogImplAttached *attached = d->attachedOrWarn();
94 if (!attached)
95 return;
96
97 if (!attached->familyListView()->model().isValid()) {
98 const QSignalBlocker blocker(attached->sampleEdit());
99 attached->updateFamilies();
100 }
101
102 attached->selectFontInListViews(font);
103}
104
105void QQuickFontDialogImpl::init()
106{
107 Q_D(QQuickFontDialogImpl);
108 QQuickFontDialogImplAttached *attached = d->attachedOrWarn();
109 if (!attached)
110 return;
111
112 if (!attached->familyListView()->model().isValid())
113 attached->updateFamilies();
114
115 attached->buttonBox()->setVisible(!(options()->options() & QFontDialogOptions::NoButtons));
116}
117
118void QQuickFontDialogImpl::keyReleaseEvent(QKeyEvent *event)
119{
120 Q_D(QQuickFontDialogImpl);
121
122 QQuickDialog::keyReleaseEvent(event);
123
124 QQuickFontDialogImplAttached *attached = d->attachedOrWarn();
125 if (!attached)
126 return;
127
128 // The family and style text edits are read-only so that they
129 // can show the current selection but also allow key input to "search".
130 // This is why we handle just the release event, and don't accept it.
131 if (attached->familyEdit()->hasFocus())
132 attached->searchFamily(event->text());
133 else if (attached->styleEdit()->hasFocus())
134 attached->searchStyle(event->text());
135}
136
137void QQuickFontDialogImpl::focusOutEvent(QFocusEvent *event)
138{
139 Q_D(QQuickFontDialogImpl);
140
141 QQuickDialog::focusOutEvent(event);
142
143 QQuickFontDialogImplAttached *attached = d->attachedOrWarn();
144 if (!attached)
145 return;
146
147 attached->clearSearch();
148}
149
150QQuickFontDialogImplAttached::QQuickFontDialogImplAttached(QObject *parent)
151 : QObject(*(new QQuickFontDialogImplAttachedPrivate), parent),
152 m_writingSystem(QFontDatabase::Any),
153 m_selectedSize(-1),
154 m_smoothlyScalable(false),
155 m_ignoreFamilyUpdate(false),
156 m_ignoreStyleUpdate(false)
157{
158 if (!qobject_cast<QQuickFontDialogImpl *>(parent)) {
159 qmlWarning(this) << "FontDialogImpl attached properties should only be "
160 << "accessed through the root FileDialogImpl instance";
161 }
162}
163
164QQuickListView *QQuickFontDialogImplAttached::familyListView() const
165{
166 Q_D(const QQuickFontDialogImplAttached);
167 return d->familyListView;
168}
169
170void QQuickFontDialogImplAttached::setFamilyListView(QQuickListView *familyListView)
171{
172 Q_D(QQuickFontDialogImplAttached);
173 if (d->familyListView == familyListView)
174 return;
175
176 if (d->familyListView) {
177 disconnect(d->familyListView, &QQuickListView::currentIndexChanged,
178 this, &QQuickFontDialogImplAttached::_q_familyChanged);
179 }
180
181 d->familyListView = familyListView;
182
183 if (familyListView) {
184 connect(d->familyListView, &QQuickListView::currentIndexChanged,
185 this, &QQuickFontDialogImplAttached::_q_familyChanged);
186 }
187
188 emit familyListViewChanged();
189}
190
191QQuickListView *QQuickFontDialogImplAttached::styleListView() const
192{
193 Q_D(const QQuickFontDialogImplAttached);
194 return d->styleListView;
195}
196
197void QQuickFontDialogImplAttached::setStyleListView(QQuickListView *styleListView)
198{
199 Q_D(QQuickFontDialogImplAttached);
200 if (d->styleListView == styleListView)
201 return;
202
203 if (d->styleListView) {
204 disconnect(d->styleListView, &QQuickListView::currentIndexChanged,
205 this, &QQuickFontDialogImplAttached::_q_styleChanged);
206 }
207
208 d->styleListView = styleListView;
209
210 if (styleListView) {
211 connect(d->styleListView, &QQuickListView::currentIndexChanged,
212 this, &QQuickFontDialogImplAttached::_q_styleChanged);
213 }
214
215 emit styleListViewChanged();
216}
217
218QQuickListView *QQuickFontDialogImplAttached::sizeListView() const
219{
220 Q_D(const QQuickFontDialogImplAttached);
221 return d->sizeListView;
222}
223
224void QQuickFontDialogImplAttached::setSizeListView(QQuickListView *sizeListView)
225{
226 Q_D(QQuickFontDialogImplAttached);
227 if (d->sizeListView == sizeListView)
228 return;
229
230 if (d->sizeListView) {
231 disconnect(d->sizeListView, &QQuickListView::currentIndexChanged,
232 this, &QQuickFontDialogImplAttached::_q_sizeChanged);
233 }
234
235 d->sizeListView = sizeListView;
236
237 if (d->sizeListView) {
238 connect(d->sizeListView, &QQuickListView::currentIndexChanged,
239 this, &QQuickFontDialogImplAttached::_q_sizeChanged);
240 }
241
242 emit sizeListViewChanged();
243}
244
245QQuickTextEdit *QQuickFontDialogImplAttached::sampleEdit() const
246{
247 Q_D(const QQuickFontDialogImplAttached);
248 return d->sampleEdit;
249}
250
251void QQuickFontDialogImplAttached::setSampleEdit(QQuickTextEdit *sampleEdit)
252{
253 Q_D(QQuickFontDialogImplAttached);
254
255 if (d->sampleEdit == sampleEdit)
256 return;
257
258 if (d->sampleEdit) {
259 QObjectPrivate::disconnect(d->sampleEdit, &QQuickTextEdit::fontChanged,
260 d, &QQuickFontDialogImplAttachedPrivate::currentFontChanged);
261 }
262
263 d->sampleEdit = sampleEdit;
264
265 if (d->sampleEdit) {
266 QObjectPrivate::connect(d->sampleEdit, &QQuickTextEdit::fontChanged,
267 d, &QQuickFontDialogImplAttachedPrivate::currentFontChanged);
268
269 d->sampleEdit->setText(QFontDatabase::writingSystemSample(m_writingSystem));
270 }
271
272 emit sampleEditChanged();
273}
274
275QQuickDialogButtonBox *QQuickFontDialogImplAttached::buttonBox() const
276{
277 Q_D(const QQuickFontDialogImplAttached);
278 return d->buttonBox;
279}
280
281void QQuickFontDialogImplAttached::setButtonBox(QQuickDialogButtonBox *buttonBox)
282{
283 Q_D(QQuickFontDialogImplAttached);
284 if (buttonBox == d->buttonBox)
285 return;
286
287 if (d->buttonBox) {
288 QQuickFontDialogImpl *fontDialogImpl = qobject_cast<QQuickFontDialogImpl *>(parent());
289 if (fontDialogImpl) {
290 auto dialogPrivate = QQuickDialogPrivate::get(fontDialogImpl);
291 QObjectPrivate::disconnect(d->buttonBox, &QQuickDialogButtonBox::accepted,
292 dialogPrivate, &QQuickDialogPrivate::handleAccept);
293 QObjectPrivate::disconnect(d->buttonBox, &QQuickDialogButtonBox::rejected,
294 dialogPrivate, &QQuickDialogPrivate::handleReject);
295 QObjectPrivate::disconnect(d->buttonBox, &QQuickDialogButtonBox::clicked, dialogPrivate,
296 &QQuickDialogPrivate::handleClick);
297 }
298 }
299
300 d->buttonBox = buttonBox;
301
302 if (buttonBox) {
303 QQuickFontDialogImpl *fontDialogImpl = qobject_cast<QQuickFontDialogImpl *>(parent());
304 if (fontDialogImpl) {
305 auto dialogPrivate = QQuickDialogPrivate::get(fontDialogImpl);
306 QObjectPrivate::connect(d->buttonBox, &QQuickDialogButtonBox::accepted, dialogPrivate,
307 &QQuickDialogPrivate::handleAccept);
308 QObjectPrivate::connect(d->buttonBox, &QQuickDialogButtonBox::rejected, dialogPrivate,
309 &QQuickDialogPrivate::handleReject);
310 QObjectPrivate::connect(d->buttonBox, &QQuickDialogButtonBox::clicked, dialogPrivate,
311 &QQuickDialogPrivate::handleClick);
312 }
313 }
314
315 emit buttonBoxChanged();
316}
317
318QQuickComboBox *QQuickFontDialogImplAttached::writingSystemComboBox() const
319{
320 Q_D(const QQuickFontDialogImplAttached);
321 return d->writingSystemComboBox;
322}
323
324void QQuickFontDialogImplAttached::setWritingSystemComboBox(QQuickComboBox *writingSystemComboBox)
325{
326 Q_D(QQuickFontDialogImplAttached);
327
328 if (d->writingSystemComboBox == writingSystemComboBox)
329 return;
330
331 if (d->writingSystemComboBox) {
332 disconnect(d->writingSystemComboBox, &QQuickComboBox::activated,
333 this, &QQuickFontDialogImplAttached::_q_writingSystemChanged);
334 }
335
336 d->writingSystemComboBox = writingSystemComboBox;
337
338 if (d->writingSystemComboBox) {
339 QStringList writingSystemModel;
340 for (int i = 0; i < QFontDatabase::WritingSystemsCount; ++i) {
341 QFontDatabase::WritingSystem ws = QFontDatabase::WritingSystem(i);
342 QString wsName = QFontDatabase::writingSystemName(ws);
343 if (wsName.isEmpty())
344 break;
345 writingSystemModel.append(wsName);
346 }
347
348 d->writingSystemComboBox->setModel(writingSystemModel);
349
350 connect(d->writingSystemComboBox, &QQuickComboBox::activated,
351 this, &QQuickFontDialogImplAttached::_q_writingSystemChanged);
352 }
353
354 emit writingSystemComboBoxChanged();
355}
356
357QQuickCheckBox *QQuickFontDialogImplAttached::underlineCheckBox() const
358{
359 Q_D(const QQuickFontDialogImplAttached);
360 return d->underlineCheckBox;
361}
362
363void QQuickFontDialogImplAttached::setUnderlineCheckBox(QQuickCheckBox *underlineCheckBox)
364{
365 Q_D(QQuickFontDialogImplAttached);
366
367 if (d->underlineCheckBox == underlineCheckBox)
368 return;
369
370 if (d->underlineCheckBox) {
371 disconnect(d->underlineCheckBox, &QQuickCheckBox::checkStateChanged,
372 this, &QQuickFontDialogImplAttached::_q_updateSample);
373 }
374
375 d->underlineCheckBox = underlineCheckBox;
376
377 if (d->underlineCheckBox) {
378 connect(d->underlineCheckBox, &QQuickCheckBox::checkStateChanged,
379 this, &QQuickFontDialogImplAttached::_q_updateSample);
380 }
381
382 emit underlineCheckBoxChanged();
383}
384
385QQuickCheckBox *QQuickFontDialogImplAttached::strikeoutCheckBox() const
386{
387 Q_D(const QQuickFontDialogImplAttached);
388 return d->strikeoutCheckBox;
389}
390
391void QQuickFontDialogImplAttached::setStrikeoutCheckBox(QQuickCheckBox *strikeoutCheckBox)
392{
393 Q_D(QQuickFontDialogImplAttached);
394
395 if (d->strikeoutCheckBox == strikeoutCheckBox)
396 return;
397
398 if (d->strikeoutCheckBox) {
399 disconnect(d->strikeoutCheckBox, &QQuickCheckBox::checkStateChanged,
400 this, &QQuickFontDialogImplAttached::_q_updateSample);
401 }
402
403 d->strikeoutCheckBox = strikeoutCheckBox;
404
405 if (d->strikeoutCheckBox) {
406 connect(d->strikeoutCheckBox, &QQuickCheckBox::checkStateChanged,
407 this, &QQuickFontDialogImplAttached::_q_updateSample);
408 }
409
410 emit strikeoutCheckBoxChanged();
411}
412
413QQuickTextField *QQuickFontDialogImplAttached::familyEdit() const
414{
415 Q_D(const QQuickFontDialogImplAttached);
416 return d->familyEdit;
417}
418
419void QQuickFontDialogImplAttached::setFamilyEdit(QQuickTextField *familyEdit)
420{
421 Q_D(QQuickFontDialogImplAttached);
422
423 if (d->familyEdit == familyEdit)
424 return;
425
426 d->familyEdit = familyEdit;
427
428 emit familyEditChanged();
429}
430
431QQuickTextField *QQuickFontDialogImplAttached::styleEdit() const
432{
433 Q_D(const QQuickFontDialogImplAttached);
434 return d->styleEdit;
435}
436
437void QQuickFontDialogImplAttached::setStyleEdit(QQuickTextField *styleEdit)
438{
439 Q_D(QQuickFontDialogImplAttached);
440
441 if (d->styleEdit == styleEdit)
442 return;
443
444 d->styleEdit = styleEdit;
445
446 emit styleEditChanged();
447}
448
449QQuickTextField *QQuickFontDialogImplAttached::sizeEdit() const
450{
451 Q_D(const QQuickFontDialogImplAttached);
452 return d->sizeEdit;
453}
454
455void QQuickFontDialogImplAttached::setSizeEdit(QQuickTextField *sizeEdit)
456{
457 Q_D(QQuickFontDialogImplAttached);
458
459 if (d->sizeEdit == sizeEdit)
460 return;
461
462 if (d->sizeEdit) {
463 disconnect(d->sizeEdit, &QQuickTextField::textChanged,
464 this, &QQuickFontDialogImplAttached::_q_sizeEdited);
465 }
466
467 d->sizeEdit = sizeEdit;
468
469 if (d->sizeEdit) {
470 connect(d->sizeEdit, &QQuickTextField::textChanged,
471 this, &QQuickFontDialogImplAttached::_q_sizeEdited);
472 }
473
474 emit sizeEditChanged();
475}
476
477static int findFamilyInModel(const QString &selectedFamily, const QStringList &model)
478{
479 enum match_t { MATCH_NONE = 0, MATCH_LAST_RESORT = 1, MATCH_APP = 2, MATCH_FAMILY = 3 };
480 QString foundryName1, familyName1, foundryName2, familyName2;
481 int bestFamilyMatch = -1;
482 match_t bestFamilyType = MATCH_NONE;
483 const QFont f;
484
485 QFontDatabasePrivate::parseFontName(selectedFamily, foundryName1, familyName1);
486
487 int i = 0;
488 for (auto it = model.constBegin(); it != model.constEnd(); ++it, ++i) {
489 QFontDatabasePrivate::parseFontName(*it, foundryName2, familyName2);
490
491 if (familyName1 == familyName2) {
492 bestFamilyType = MATCH_FAMILY;
493 if (foundryName1 == foundryName2)
494 return i;
495 else
496 bestFamilyMatch = i;
497 }
498
499 // fallbacks
500 match_t type = MATCH_NONE;
501 if (bestFamilyType <= MATCH_NONE && familyName2 == QStringLiteral("helvetica"))
502 type = MATCH_LAST_RESORT;
503 if (bestFamilyType <= MATCH_LAST_RESORT && familyName2 == f.families().constFirst())
504 type = MATCH_APP;
505 if (type != MATCH_NONE) {
506 bestFamilyType = type;
507 bestFamilyMatch = i;
508 }
509 }
510
511 return bestFamilyMatch;
512}
513
514static int findStyleInModel(const QString &selectedStyle, const QStringList &model)
515{
516 if (model.isEmpty())
517 return -1;
518
519 if (!selectedStyle.isEmpty()) {
520 const int idx = model.indexOf(QRegularExpression(QRegularExpression::escape(selectedStyle), QRegularExpression::CaseInsensitiveOption));
521 if (idx >= 0)
522 return idx;
523
524 enum class StyleClass {Unknown, Normal, Italic};
525 auto classifyStyleFallback = [](const QString & style) {
526 if (style.toLower() == QLatin1String("italic") || style.toLower() == QLatin1String("oblique"))
527 return StyleClass::Italic;
528 if (style.toLower() == QLatin1String("normal") || style.toLower() == QLatin1String("regular"))
529 return StyleClass::Normal;
530 return StyleClass::Unknown;
531 };
532
533 auto styleClass = classifyStyleFallback(selectedStyle);
534
535 if (styleClass != StyleClass::Unknown)
536 for (int i = 0; i < model.size(); ++i)
537 if (classifyStyleFallback(model.at(i)) == styleClass)
538 return i;
539 }
540 return 0;
541}
542
543/*!
544 \internal
545
546 Updates the model for the family list view, and attempt
547 to reselect the previously selected font family.
548 */
549void QQuickFontDialogImplAttached::updateFamilies()
550{
551 const QFontDialogOptions::FontDialogOptions scalableMask(
552 QFontDialogOptions::ScalableFonts | QFontDialogOptions::NonScalableFonts);
553
554 const QFontDialogOptions::FontDialogOptions spacingMask(QFontDialogOptions::ProportionalFonts
555 | QFontDialogOptions::MonospacedFonts);
556
557 const auto p = qobject_cast<QQuickFontDialogImpl *>(parent());
558
559 const auto options = p->options()->options();
560
561 QStringList familyNames;
562 const auto families = QFontDatabase::families(m_writingSystem);
563 for (const auto &family : families) {
564 if (QFontDatabase::isPrivateFamily(family))
565 continue;
566
567 if ((options & scalableMask) && (options & scalableMask) != scalableMask) {
568 if (bool(options & QFontDialogOptions::ScalableFonts)
569 != QFontDatabase::isSmoothlyScalable(family))
570 continue;
571 }
572
573 if ((options & spacingMask) && (options & scalableMask) != spacingMask) {
574 if (bool(options & QFontDialogOptions::MonospacedFonts)
575 != QFontDatabase::isFixedPitch(family))
576 continue;
577 }
578
579 familyNames << family;
580 }
581
582 auto listView = familyListView();
583
584 // Index will be set to -1 on empty model, and 0 for non empty models.
585 m_ignoreFamilyUpdate = !m_selectedFamily.isEmpty();
586 listView->setModel(familyNames);
587 m_ignoreFamilyUpdate = false;
588
589 // Will overwrite selectedFamily and selectedStyle
590 listView->setCurrentIndex(findFamilyInModel(m_selectedFamily, familyNames));
591
592 if (familyNames.isEmpty())
593 _q_familyChanged();
594}
595
596/*!
597 \internal
598
599 Updates the model for the style list view, and
600 attempt to reselect the style that was previously selected.
601
602 Calls updateSizes()
603 */
604void QQuickFontDialogImplAttached::updateStyles()
605{
606 const QString family = familyListView()->currentIndex() >= 0 ? m_selectedFamily : QString();
607 const QStringList styles = QFontDatabase::styles(family);
608
609 auto listView = styleListView();
610
611 m_ignoreStyleUpdate = !m_selectedStyle.isEmpty();
612 listView->setModel(styles);
613
614 if (styles.isEmpty()) {
615 styleEdit()->clear();
616 m_smoothlyScalable = false;
617 } else {
618 int newIndex = findStyleInModel(m_selectedStyle, styles);
619
620 listView->setCurrentIndex(newIndex);
621
622 m_selectedStyle = styles.at(newIndex);
623 styleEdit()->setText(m_selectedStyle);
624
625 m_smoothlyScalable = QFontDatabase::isSmoothlyScalable(m_selectedFamily, m_selectedStyle);
626 }
627
628 m_ignoreStyleUpdate = false;
629
630 updateSizes();
631}
632
633/*!
634 \internal
635
636 Updates the model for the size list view,
637 and attempts to reselect the size that was previously selected
638 */
639void QQuickFontDialogImplAttached::updateSizes()
640{
641 if (!m_selectedFamily.isEmpty()) {
642 const QList<int> sizes = QFontDatabase::pointSizes(m_selectedFamily, m_selectedStyle);
643
644 QStringList str_sizes;
645
646 str_sizes.reserve(sizes.size());
647
648 int idx = 0, current = -1;
649 for (QList<int>::const_iterator it = sizes.constBegin(); it != sizes.constEnd(); it++) {
650 str_sizes.append(QString::number(*it));
651 if (current == -1 && m_selectedSize == *it) {
652 current = idx;
653 }
654 ++idx;
655 }
656
657 auto listView = sizeListView();
658
659 // only select the first element in the model when this function is first called and the new model isn't empty
660 listView->setModel(str_sizes);
661
662 if (current != -1)
663 listView->setCurrentIndex(current);
664
665 sizeEdit()->setText(!m_smoothlyScalable && listView->currentIndex() > 0
666 ? str_sizes.at(listView->currentIndex())
667 : QString::number(m_selectedSize));
668 } else {
669 qCWarning(lcAttachedProperty) << "Warning! selectedFamily is empty";
670 sizeEdit()->clear();
671 }
672
673 _q_updateSample();
674}
675
676void QQuickFontDialogImplAttached::_q_updateSample()
677{
678 if (m_selectedFamily.isEmpty())
679 return;
680
681 const int pSize = sizeEdit()->text().toInt();
682
683 QFont newFont = QFontDatabase::font(m_selectedFamily, m_selectedStyle, pSize);
684
685 newFont.setUnderline(underlineCheckBox()->isChecked());
686 newFont.setStrikeOut(strikeoutCheckBox()->isChecked());
687
688 sampleEdit()->setFont(newFont);
689}
690
691void QQuickFontDialogImplAttached::_q_writingSystemChanged(int index)
692{
693 m_writingSystem = QFontDatabase::WritingSystem(index);
694 sampleEdit()->setText(QFontDatabase::writingSystemSample(m_writingSystem));
695
696 updateFamilies();
697}
698
699void QQuickFontDialogImplAttached::searchListView(const QString &s, QQuickListView *listView)
700{
701 if (s.isEmpty())
702 return;
703
704 const QStringList model = listView->model().toStringList();
705
706 bool redo = false;
707
708 do {
709 m_search.append(s);
710
711 for (int i = 0; i < model.size(); ++i) {
712 if (model.at(i).startsWith(m_search, Qt::CaseInsensitive)) {
713 listView->setCurrentIndex(i);
714 return;
715 }
716 }
717
718 clearSearch();
719
720 redo = !redo;
721 } while (redo);
722}
723
724void QQuickFontDialogImplAttached::clearSearch()
725{
726 m_search.clear();
727}
728
729void QQuickFontDialogImplAttached::_q_familyChanged()
730{
731 if (m_ignoreFamilyUpdate)
732 return;
733
734 const int index = familyListView()->currentIndex();
735
736 if (index < 0) {
737 familyEdit()->clear();
738 } else {
739 m_selectedFamily = familyListView()->model().toStringList().at(index);
740 familyEdit()->setText(m_selectedFamily);
741 }
742
743 updateStyles();
744}
745
746void QQuickFontDialogImplAttached::_q_styleChanged()
747{
748 if (m_ignoreStyleUpdate)
749 return;
750
751 const int index = styleListView()->currentIndex();
752
753 if (index < 0) {
754 qCWarning(lcAttachedProperty) << "currentIndex changed to -1";
755 return;
756 }
757
758 m_selectedStyle = styleListView()->model().toStringList().at(index);
759 styleEdit()->setText(m_selectedStyle);
760 m_smoothlyScalable = QFontDatabase::isSmoothlyScalable(m_selectedFamily, m_selectedStyle);
761
762 updateSizes();
763}
764
765void QQuickFontDialogImplAttached::_q_sizeEdited()
766{
767 const int size = qAbs(sizeEdit()->text().toInt());
768
769 if (size == m_selectedSize)
770 return;
771
772 m_selectedSize = size;
773
774 if (sizeListView()->count()) {
775 auto model = sizeListView()->model().toStringList();
776
777 int i;
778 for (i = 0; i < model.size() - 1; ++i) {
779 if (model.at(i).toInt() >= size)
780 break;
781 }
782
783 QSignalBlocker blocker(sizeListView());
784 if (model.at(i).toInt() == size)
785 sizeListView()->setCurrentIndex(i);
786 else
787 sizeListView()->setCurrentIndex(-1);
788 }
789
790 _q_updateSample();
791}
792
793void QQuickFontDialogImplAttached::_q_sizeChanged()
794{
795 const int index = sizeListView()->currentIndex();
796
797 if (index < 0) {
798 qCWarning(lcAttachedProperty) << "currentIndex changed to -1";
799 return;
800 }
801
802 const QString s = sizeListView()->model().toStringList().at(index);
803 m_selectedSize = s.toInt();
804
805 sizeEdit()->setText(s);
806
807 _q_updateSample();
808}
809
810void QQuickFontDialogImplAttachedPrivate::currentFontChanged(const QFont &font)
811{
812 auto fontDialogImpl = qobject_cast<QQuickFontDialogImpl *>(parent);
813
814 if (!fontDialogImpl) {
815 return;
816 }
817
818 fontDialogImpl->setCurrentFont(font);
819}
820
821void QQuickFontDialogImplAttached::selectFontInListViews(const QFont &font)
822{
823 {
824 QSignalBlocker blocker(sampleEdit());
825 familyListView()->setCurrentIndex(findFamilyInModel(font.families().constFirst(), familyListView()->model().toStringList()));
826 styleListView()->setCurrentIndex(findStyleInModel(QFontDatabase::styleString(font), styleListView()->model().toStringList()));
827 sizeEdit()->setText(QString::number(font.pointSize()));
828
829 underlineCheckBox()->setChecked(font.underline());
830 strikeoutCheckBox()->setChecked(font.strikeOut());
831 }
832
833 _q_updateSample();
834}
835
836QT_END_NAMESPACE
837
838#include "moc_qquickfontdialogimpl_p.cpp"
void handleClick(QQuickAbstractButton *button) override
QQuickFontDialogImplAttached * attachedOrWarn()
Combined button and popup list for selecting options.
static int findFamilyInModel(const QString &selectedFamily, const QStringList &model)
static int findStyleInModel(const QString &selectedStyle, const QStringList &model)