| 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 | |
| 4 | #include "qquickfontdialogimpl_p.h" |
| 5 | #include "qquickfontdialogimpl_p_p.h" |
| 6 | |
| 7 | #include <QtQuickTemplates2/private/qquickdialogbuttonbox_p_p.h> |
| 8 | #include <private/qfontdatabase_p.h> |
| 9 | |
| 10 | #include <QRegularExpression> |
| 11 | |
| 12 | QT_BEGIN_NAMESPACE |
| 13 | |
| 14 | Q_LOGGING_CATEGORY(lcAttachedProperty, "qt.quick.dialogs.quickfontdialogimpl.attachedOrWarn" ) |
| 15 | |
| 16 | QQuickFontDialogImplPrivate::QQuickFontDialogImplPrivate() |
| 17 | { |
| 18 | } |
| 19 | |
| 20 | QQuickFontDialogImplAttached *QQuickFontDialogImplPrivate::attachedOrWarn() |
| 21 | { |
| 22 | Q_Q(QQuickFontDialogImpl); |
| 23 | QQuickFontDialogImplAttached *attached = static_cast<QQuickFontDialogImplAttached *>( |
| 24 | qmlAttachedPropertiesObject<QQuickFontDialogImpl>(obj: q)); |
| 25 | if (!attached) { |
| 26 | qCWarning(lcAttachedProperty) |
| 27 | << "Expected FontDialogImpl attached object to be present on" << this; |
| 28 | } |
| 29 | return attached; |
| 30 | } |
| 31 | |
| 32 | void QQuickFontDialogImplPrivate::handleAccept() { } |
| 33 | |
| 34 | void 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 | |
| 43 | QQuickFontDialogImpl::QQuickFontDialogImpl(QObject *parent) |
| 44 | : QQuickDialog(*(new QQuickFontDialogImplPrivate), parent) |
| 45 | { |
| 46 | setPopupType(QQuickPopup::Window); |
| 47 | } |
| 48 | |
| 49 | QQuickFontDialogImplAttached *QQuickFontDialogImpl::qmlAttachedProperties(QObject *object) |
| 50 | { |
| 51 | return new QQuickFontDialogImplAttached(object); |
| 52 | } |
| 53 | |
| 54 | QSharedPointer<QFontDialogOptions> QQuickFontDialogImpl::options() const |
| 55 | { |
| 56 | Q_D(const QQuickFontDialogImpl); |
| 57 | |
| 58 | return d->options; |
| 59 | } |
| 60 | |
| 61 | void 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 | |
| 73 | QFont QQuickFontDialogImpl::currentFont() const |
| 74 | { |
| 75 | Q_D(const QQuickFontDialogImpl); |
| 76 | return d->currentFont; |
| 77 | } |
| 78 | |
| 79 | void 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 | |
| 105 | void 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 | |
| 118 | void 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(s: event->text()); |
| 133 | else if (attached->styleEdit()->hasFocus()) |
| 134 | attached->searchStyle(s: event->text()); |
| 135 | } |
| 136 | |
| 137 | void 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 | |
| 150 | QQuickFontDialogImplAttached::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 *>(object: parent)) { |
| 159 | qmlWarning(me: this) << "FontDialogImpl attached properties should only be " |
| 160 | << "accessed through the root FileDialogImpl instance" ; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | QQuickListView *QQuickFontDialogImplAttached::familyListView() const |
| 165 | { |
| 166 | Q_D(const QQuickFontDialogImplAttached); |
| 167 | return d->familyListView; |
| 168 | } |
| 169 | |
| 170 | void QQuickFontDialogImplAttached::setFamilyListView(QQuickListView *familyListView) |
| 171 | { |
| 172 | Q_D(QQuickFontDialogImplAttached); |
| 173 | if (d->familyListView == familyListView) |
| 174 | return; |
| 175 | |
| 176 | if (d->familyListView) { |
| 177 | disconnect(sender: d->familyListView, signal: &QQuickListView::currentIndexChanged, |
| 178 | receiver: this, slot: &QQuickFontDialogImplAttached::_q_familyChanged); |
| 179 | } |
| 180 | |
| 181 | d->familyListView = familyListView; |
| 182 | |
| 183 | if (familyListView) { |
| 184 | connect(sender: d->familyListView, signal: &QQuickListView::currentIndexChanged, |
| 185 | context: this, slot: &QQuickFontDialogImplAttached::_q_familyChanged); |
| 186 | } |
| 187 | |
| 188 | emit familyListViewChanged(); |
| 189 | } |
| 190 | |
| 191 | QQuickListView *QQuickFontDialogImplAttached::styleListView() const |
| 192 | { |
| 193 | Q_D(const QQuickFontDialogImplAttached); |
| 194 | return d->styleListView; |
| 195 | } |
| 196 | |
| 197 | void QQuickFontDialogImplAttached::setStyleListView(QQuickListView *styleListView) |
| 198 | { |
| 199 | Q_D(QQuickFontDialogImplAttached); |
| 200 | if (d->styleListView == styleListView) |
| 201 | return; |
| 202 | |
| 203 | if (d->styleListView) { |
| 204 | disconnect(sender: d->styleListView, signal: &QQuickListView::currentIndexChanged, |
| 205 | receiver: this, slot: &QQuickFontDialogImplAttached::_q_styleChanged); |
| 206 | } |
| 207 | |
| 208 | d->styleListView = styleListView; |
| 209 | |
| 210 | if (styleListView) { |
| 211 | connect(sender: d->styleListView, signal: &QQuickListView::currentIndexChanged, |
| 212 | context: this, slot: &QQuickFontDialogImplAttached::_q_styleChanged); |
| 213 | } |
| 214 | |
| 215 | emit styleListViewChanged(); |
| 216 | } |
| 217 | |
| 218 | QQuickListView *QQuickFontDialogImplAttached::sizeListView() const |
| 219 | { |
| 220 | Q_D(const QQuickFontDialogImplAttached); |
| 221 | return d->sizeListView; |
| 222 | } |
| 223 | |
| 224 | void QQuickFontDialogImplAttached::setSizeListView(QQuickListView *sizeListView) |
| 225 | { |
| 226 | Q_D(QQuickFontDialogImplAttached); |
| 227 | if (d->sizeListView == sizeListView) |
| 228 | return; |
| 229 | |
| 230 | if (d->sizeListView) { |
| 231 | disconnect(sender: d->sizeListView, signal: &QQuickListView::currentIndexChanged, |
| 232 | receiver: this, slot: &QQuickFontDialogImplAttached::_q_sizeChanged); |
| 233 | } |
| 234 | |
| 235 | d->sizeListView = sizeListView; |
| 236 | |
| 237 | if (d->sizeListView) { |
| 238 | connect(sender: d->sizeListView, signal: &QQuickListView::currentIndexChanged, |
| 239 | context: this, slot: &QQuickFontDialogImplAttached::_q_sizeChanged); |
| 240 | } |
| 241 | |
| 242 | emit sizeListViewChanged(); |
| 243 | } |
| 244 | |
| 245 | QQuickTextEdit *QQuickFontDialogImplAttached::sampleEdit() const |
| 246 | { |
| 247 | Q_D(const QQuickFontDialogImplAttached); |
| 248 | return d->sampleEdit; |
| 249 | } |
| 250 | |
| 251 | void 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(sender: d->sampleEdit, signal: &QQuickTextEdit::fontChanged, |
| 260 | receiverPrivate: d, slot: &QQuickFontDialogImplAttachedPrivate::currentFontChanged); |
| 261 | } |
| 262 | |
| 263 | d->sampleEdit = sampleEdit; |
| 264 | |
| 265 | if (d->sampleEdit) { |
| 266 | QObjectPrivate::connect(sender: d->sampleEdit, signal: &QQuickTextEdit::fontChanged, |
| 267 | receiverPrivate: d, slot: &QQuickFontDialogImplAttachedPrivate::currentFontChanged); |
| 268 | |
| 269 | d->sampleEdit->setText(QFontDatabase::writingSystemSample(writingSystem: m_writingSystem)); |
| 270 | } |
| 271 | |
| 272 | emit sampleEditChanged(); |
| 273 | } |
| 274 | |
| 275 | QQuickDialogButtonBox *QQuickFontDialogImplAttached::buttonBox() const |
| 276 | { |
| 277 | Q_D(const QQuickFontDialogImplAttached); |
| 278 | return d->buttonBox; |
| 279 | } |
| 280 | |
| 281 | void 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 *>(object: parent()); |
| 289 | if (fontDialogImpl) { |
| 290 | auto dialogPrivate = QQuickDialogPrivate::get(dialog: fontDialogImpl); |
| 291 | QObjectPrivate::disconnect(sender: d->buttonBox, signal: &QQuickDialogButtonBox::accepted, |
| 292 | receiverPrivate: dialogPrivate, slot: &QQuickDialogPrivate::handleAccept); |
| 293 | QObjectPrivate::disconnect(sender: d->buttonBox, signal: &QQuickDialogButtonBox::rejected, |
| 294 | receiverPrivate: dialogPrivate, slot: &QQuickDialogPrivate::handleReject); |
| 295 | QObjectPrivate::disconnect(sender: d->buttonBox, signal: &QQuickDialogButtonBox::clicked, receiverPrivate: dialogPrivate, |
| 296 | slot: &QQuickDialogPrivate::handleClick); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | d->buttonBox = buttonBox; |
| 301 | |
| 302 | if (buttonBox) { |
| 303 | QQuickFontDialogImpl *fontDialogImpl = qobject_cast<QQuickFontDialogImpl *>(object: parent()); |
| 304 | if (fontDialogImpl) { |
| 305 | auto dialogPrivate = QQuickDialogPrivate::get(dialog: fontDialogImpl); |
| 306 | QObjectPrivate::connect(sender: d->buttonBox, signal: &QQuickDialogButtonBox::accepted, receiverPrivate: dialogPrivate, |
| 307 | slot: &QQuickDialogPrivate::handleAccept); |
| 308 | QObjectPrivate::connect(sender: d->buttonBox, signal: &QQuickDialogButtonBox::rejected, receiverPrivate: dialogPrivate, |
| 309 | slot: &QQuickDialogPrivate::handleReject); |
| 310 | QObjectPrivate::connect(sender: d->buttonBox, signal: &QQuickDialogButtonBox::clicked, receiverPrivate: dialogPrivate, |
| 311 | slot: &QQuickDialogPrivate::handleClick); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | emit buttonBoxChanged(); |
| 316 | } |
| 317 | |
| 318 | QQuickComboBox *QQuickFontDialogImplAttached::writingSystemComboBox() const |
| 319 | { |
| 320 | Q_D(const QQuickFontDialogImplAttached); |
| 321 | return d->writingSystemComboBox; |
| 322 | } |
| 323 | |
| 324 | void QQuickFontDialogImplAttached::setWritingSystemComboBox(QQuickComboBox *writingSystemComboBox) |
| 325 | { |
| 326 | Q_D(QQuickFontDialogImplAttached); |
| 327 | |
| 328 | if (d->writingSystemComboBox == writingSystemComboBox) |
| 329 | return; |
| 330 | |
| 331 | if (d->writingSystemComboBox) { |
| 332 | disconnect(sender: d->writingSystemComboBox, signal: &QQuickComboBox::activated, |
| 333 | receiver: this, slot: &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(writingSystem: ws); |
| 343 | if (wsName.isEmpty()) |
| 344 | break; |
| 345 | writingSystemModel.append(t: wsName); |
| 346 | } |
| 347 | |
| 348 | d->writingSystemComboBox->setModel(writingSystemModel); |
| 349 | |
| 350 | connect(sender: d->writingSystemComboBox, signal: &QQuickComboBox::activated, |
| 351 | context: this, slot: &QQuickFontDialogImplAttached::_q_writingSystemChanged); |
| 352 | } |
| 353 | |
| 354 | emit writingSystemComboBoxChanged(); |
| 355 | } |
| 356 | |
| 357 | QQuickCheckBox *QQuickFontDialogImplAttached::underlineCheckBox() const |
| 358 | { |
| 359 | Q_D(const QQuickFontDialogImplAttached); |
| 360 | return d->underlineCheckBox; |
| 361 | } |
| 362 | |
| 363 | void QQuickFontDialogImplAttached::setUnderlineCheckBox(QQuickCheckBox *underlineCheckBox) |
| 364 | { |
| 365 | Q_D(QQuickFontDialogImplAttached); |
| 366 | |
| 367 | if (d->underlineCheckBox == underlineCheckBox) |
| 368 | return; |
| 369 | |
| 370 | if (d->underlineCheckBox) { |
| 371 | disconnect(sender: d->underlineCheckBox, signal: &QQuickCheckBox::checkStateChanged, |
| 372 | receiver: this, slot: &QQuickFontDialogImplAttached::_q_updateSample); |
| 373 | } |
| 374 | |
| 375 | d->underlineCheckBox = underlineCheckBox; |
| 376 | |
| 377 | if (d->underlineCheckBox) { |
| 378 | connect(sender: d->underlineCheckBox, signal: &QQuickCheckBox::checkStateChanged, |
| 379 | context: this, slot: &QQuickFontDialogImplAttached::_q_updateSample); |
| 380 | } |
| 381 | |
| 382 | emit underlineCheckBoxChanged(); |
| 383 | } |
| 384 | |
| 385 | QQuickCheckBox *QQuickFontDialogImplAttached::strikeoutCheckBox() const |
| 386 | { |
| 387 | Q_D(const QQuickFontDialogImplAttached); |
| 388 | return d->strikeoutCheckBox; |
| 389 | } |
| 390 | |
| 391 | void QQuickFontDialogImplAttached::setStrikeoutCheckBox(QQuickCheckBox *strikeoutCheckBox) |
| 392 | { |
| 393 | Q_D(QQuickFontDialogImplAttached); |
| 394 | |
| 395 | if (d->strikeoutCheckBox == strikeoutCheckBox) |
| 396 | return; |
| 397 | |
| 398 | if (d->strikeoutCheckBox) { |
| 399 | disconnect(sender: d->strikeoutCheckBox, signal: &QQuickCheckBox::checkStateChanged, |
| 400 | receiver: this, slot: &QQuickFontDialogImplAttached::_q_updateSample); |
| 401 | } |
| 402 | |
| 403 | d->strikeoutCheckBox = strikeoutCheckBox; |
| 404 | |
| 405 | if (d->strikeoutCheckBox) { |
| 406 | connect(sender: d->strikeoutCheckBox, signal: &QQuickCheckBox::checkStateChanged, |
| 407 | context: this, slot: &QQuickFontDialogImplAttached::_q_updateSample); |
| 408 | } |
| 409 | |
| 410 | emit strikeoutCheckBoxChanged(); |
| 411 | } |
| 412 | |
| 413 | QQuickTextField *QQuickFontDialogImplAttached::familyEdit() const |
| 414 | { |
| 415 | Q_D(const QQuickFontDialogImplAttached); |
| 416 | return d->familyEdit; |
| 417 | } |
| 418 | |
| 419 | void 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 | |
| 431 | QQuickTextField *QQuickFontDialogImplAttached::styleEdit() const |
| 432 | { |
| 433 | Q_D(const QQuickFontDialogImplAttached); |
| 434 | return d->styleEdit; |
| 435 | } |
| 436 | |
| 437 | void 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 | |
| 449 | QQuickTextField *QQuickFontDialogImplAttached::sizeEdit() const |
| 450 | { |
| 451 | Q_D(const QQuickFontDialogImplAttached); |
| 452 | return d->sizeEdit; |
| 453 | } |
| 454 | |
| 455 | void QQuickFontDialogImplAttached::setSizeEdit(QQuickTextField *sizeEdit) |
| 456 | { |
| 457 | Q_D(QQuickFontDialogImplAttached); |
| 458 | |
| 459 | if (d->sizeEdit == sizeEdit) |
| 460 | return; |
| 461 | |
| 462 | if (d->sizeEdit) { |
| 463 | disconnect(sender: d->sizeEdit, signal: &QQuickTextField::textChanged, |
| 464 | receiver: this, slot: &QQuickFontDialogImplAttached::_q_sizeEdited); |
| 465 | } |
| 466 | |
| 467 | d->sizeEdit = sizeEdit; |
| 468 | |
| 469 | if (d->sizeEdit) { |
| 470 | connect(sender: d->sizeEdit, signal: &QQuickTextField::textChanged, |
| 471 | context: this, slot: &QQuickFontDialogImplAttached::_q_sizeEdited); |
| 472 | } |
| 473 | |
| 474 | emit sizeEditChanged(); |
| 475 | } |
| 476 | |
| 477 | static 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(name: selectedFamily, foundry&: foundryName1, family&: familyName1); |
| 486 | |
| 487 | int i = 0; |
| 488 | for (auto it = model.constBegin(); it != model.constEnd(); ++it, ++i) { |
| 489 | QFontDatabasePrivate::parseFontName(name: *it, foundry&: foundryName2, family&: 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 | |
| 514 | static 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(re: QRegularExpression(QRegularExpression::escape(str: 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 | */ |
| 549 | void 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 *>(object: parent()); |
| 558 | |
| 559 | const auto options = p->options()->options(); |
| 560 | |
| 561 | QStringList familyNames; |
| 562 | const auto families = QFontDatabase::families(writingSystem: 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(selectedFamily: m_selectedFamily, model: 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 | */ |
| 604 | void 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(selectedStyle: m_selectedStyle, model: styles); |
| 619 | |
| 620 | listView->setCurrentIndex(newIndex); |
| 621 | |
| 622 | m_selectedStyle = styles.at(i: newIndex); |
| 623 | styleEdit()->setText(m_selectedStyle); |
| 624 | |
| 625 | m_smoothlyScalable = QFontDatabase::isSmoothlyScalable(family: m_selectedFamily, style: 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 | */ |
| 639 | void QQuickFontDialogImplAttached::updateSizes() |
| 640 | { |
| 641 | if (!m_selectedFamily.isEmpty()) { |
| 642 | const QList<int> sizes = QFontDatabase::pointSizes(family: m_selectedFamily, style: m_selectedStyle); |
| 643 | |
| 644 | QStringList str_sizes; |
| 645 | |
| 646 | str_sizes.reserve(size: 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(t: 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(i: 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 | |
| 676 | void 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(family: m_selectedFamily, style: m_selectedStyle, pointSize: pSize); |
| 684 | |
| 685 | newFont.setUnderline(underlineCheckBox()->isChecked()); |
| 686 | newFont.setStrikeOut(strikeoutCheckBox()->isChecked()); |
| 687 | |
| 688 | sampleEdit()->setFont(newFont); |
| 689 | } |
| 690 | |
| 691 | void QQuickFontDialogImplAttached::_q_writingSystemChanged(int index) |
| 692 | { |
| 693 | m_writingSystem = QFontDatabase::WritingSystem(index); |
| 694 | sampleEdit()->setText(QFontDatabase::writingSystemSample(writingSystem: m_writingSystem)); |
| 695 | |
| 696 | updateFamilies(); |
| 697 | } |
| 698 | |
| 699 | void 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(s: m_search, cs: Qt::CaseInsensitive)) { |
| 713 | listView->setCurrentIndex(i); |
| 714 | return; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | clearSearch(); |
| 719 | |
| 720 | redo = !redo; |
| 721 | } while (redo); |
| 722 | } |
| 723 | |
| 724 | void QQuickFontDialogImplAttached::clearSearch() |
| 725 | { |
| 726 | m_search.clear(); |
| 727 | } |
| 728 | |
| 729 | void 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(i: index); |
| 740 | familyEdit()->setText(m_selectedFamily); |
| 741 | } |
| 742 | |
| 743 | updateStyles(); |
| 744 | } |
| 745 | |
| 746 | void 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(i: index); |
| 759 | styleEdit()->setText(m_selectedStyle); |
| 760 | m_smoothlyScalable = QFontDatabase::isSmoothlyScalable(family: m_selectedFamily, style: m_selectedStyle); |
| 761 | |
| 762 | updateSizes(); |
| 763 | } |
| 764 | |
| 765 | void QQuickFontDialogImplAttached::_q_sizeEdited() |
| 766 | { |
| 767 | const int size = qAbs(t: 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 | |
| 793 | void 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(i: index); |
| 803 | m_selectedSize = s.toInt(); |
| 804 | |
| 805 | sizeEdit()->setText(s); |
| 806 | |
| 807 | _q_updateSample(); |
| 808 | } |
| 809 | |
| 810 | void QQuickFontDialogImplAttachedPrivate::currentFontChanged(const QFont &font) |
| 811 | { |
| 812 | auto fontDialogImpl = qobject_cast<QQuickFontDialogImpl *>(object: parent); |
| 813 | |
| 814 | if (!fontDialogImpl) { |
| 815 | return; |
| 816 | } |
| 817 | |
| 818 | fontDialogImpl->setCurrentFont(font); |
| 819 | } |
| 820 | |
| 821 | void QQuickFontDialogImplAttached::selectFontInListViews(const QFont &font) |
| 822 | { |
| 823 | { |
| 824 | QSignalBlocker blocker(sampleEdit()); |
| 825 | familyListView()->setCurrentIndex(findFamilyInModel(selectedFamily: font.families().constFirst(), model: familyListView()->model().toStringList())); |
| 826 | styleListView()->setCurrentIndex(findStyleInModel(selectedStyle: QFontDatabase::styleString(font), model: 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 | |
| 836 | QT_END_NAMESPACE |
| 837 | |
| 838 | #include "moc_qquickfontdialogimpl_p.cpp" |
| 839 | |