Changeset View
Standalone View
src/kfontchooserdialog.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright (C) 1996 Bernd Johannes Wuebben <wuebben@kde.org> | ||||
3 | Copyright (c) 1999 Preston Brown <pbrown@kde.org> | ||||
4 | Copyright (c) 1999 Mario Weilguni <mweilguni@kde.org> | ||||
5 | | ||||
6 | This library is free software; you can redistribute it and/or | ||||
7 | modify it under the terms of the GNU Library General Public | ||||
8 | License as published by the Free Software Foundation; either | ||||
9 | version 2 of the License, or (at your option) any later version. | ||||
10 | | ||||
11 | This library is distributed in the hope that it will be useful, | ||||
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
14 | Library General Public License for more details. | ||||
15 | | ||||
16 | You should have received a copy of the GNU Library General Public License | ||||
17 | along with this library; see the file COPYING.LIB. If not, write to | ||||
18 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
19 | Boston, MA 02110-1301, USA. | ||||
20 | */ | ||||
21 | | ||||
22 | #include "kfontchooserdialog.h" | ||||
23 | | ||||
24 | #include <QDialogButtonBox> | ||||
25 | #include <QPushButton> | ||||
26 | #include <QVBoxLayout> | ||||
27 | | ||||
28 | class KFontChooserDialogPrivate | ||||
29 | { | ||||
30 | public: | ||||
31 | KFontChooserDialogPrivate() | ||||
32 | : chooser(nullptr) | ||||
33 | { | ||||
34 | } | ||||
35 | | ||||
36 | KFontChooser *chooser; | ||||
37 | }; | ||||
38 | | ||||
39 | KFontChooserDialog::KFontChooserDialog(const KFontChooser::DisplayFlags &flags, QWidget *parent) | ||||
dfaure: Should be static. Even file-static (move the implementation further up in the file). | |||||
40 | : QDialog(parent), | ||||
41 | d(new KFontChooserDialogPrivate) | ||||
42 | { | ||||
43 | setWindowTitle(tr("Select Font")); | ||||
44 | d->chooser = new KFontChooser(this, flags, QStringList(), 8, nullptr); | ||||
45 | d->chooser->setObjectName(QStringLiteral("fontChooser")); | ||||
46 | | ||||
47 | connect(d->chooser, &KFontChooser::fontSelected, this, &KFontChooserDialog::fontSelected); | ||||
48 | | ||||
49 | QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); | ||||
50 | QVBoxLayout *mainLayout = new QVBoxLayout; | ||||
51 | setLayout(mainLayout); | ||||
52 | mainLayout->addWidget(d->chooser); | ||||
53 | mainLayout->addWidget(buttonBox); | ||||
54 | | ||||
55 | connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); | ||||
56 | connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); | ||||
57 | } | ||||
58 | | ||||
59 | KFontChooserDialog::~KFontChooserDialog() | ||||
60 | { | ||||
61 | delete d; | ||||
62 | } | ||||
63 | | ||||
64 | void KFontChooserDialog::setFont(const QFont &font, bool onlyFixed) | ||||
65 | { | ||||
66 | d->chooser->setFont(font, onlyFixed); | ||||
67 | } | ||||
68 | | ||||
69 | QFont KFontChooserDialog::font() const | ||||
70 | { | ||||
71 | return d->chooser->font(); | ||||
72 | } | ||||
73 | | ||||
74 | // If the styleName property is set for a QFont, using setBold(true) would | ||||
75 | // lead to Qt using an "emboldended"/synthetic font style instead of using | ||||
76 | // the bold style provided by the font itself; the latter looks better than | ||||
77 | // the former, also accorgin to upstream, the styleName property is useful | ||||
78 | // for fancy font styles, so there is no point in setting it for "Regular" | ||||
79 | // fonts. For more details see: | ||||
80 | // https://bugreports.qt.io/browse/QTBUG-63792 | ||||
81 | // https://bugs.kde.org/show_bug.cgi?id=378523 | ||||
82 | static void stripRegularStyleName(QFont &font) | ||||
dfaure: not needed, exec() makes it modal anyway | |||||
83 | { | ||||
84 | if (font.weight() == QFont::Normal | ||||
85 | && (font.styleName() == QLatin1String("Regular") | ||||
86 | || font.styleName() == QLatin1String("Normal") | ||||
dfaure: const | |||||
87 | || font.styleName() == QLatin1String("Book") | ||||
88 | || font.styleName() == QLatin1String("Roman"))) { | ||||
89 | font.setStyleName(QString()); | ||||
90 | } | ||||
91 | } | ||||
92 | | ||||
93 | // static | ||||
94 | int KFontChooserDialog::getFontDiff(QFont &theFont, KFontChooser::FontDiffFlags &diffFlags, | ||||
meven: Shouldn't this be not commented | |||||
IIUC, if a function is declared static inside the class body (e.g. in the header file), the static keyword can't be repeated when it's defined outside the class body. ahmadsamir: IIUC, if a function is declared static inside the class body (e.g. in the header file), the… | |||||
95 | const KFontChooser::DisplayFlags &flags, QWidget *parent) | ||||
96 | { | ||||
97 | KFontChooserDialog dlg(flags | KFontChooser::ShowDifferences, parent); | ||||
98 | dlg.setObjectName(QStringLiteral("Font Selector")); | ||||
Is having dialog objects on the stack a good idea? What if the application gets closed while the dialog is open, would that not crash? At least at some point people thought it was not a good idea, see https://blogs.kde.org/node/3919 kossebau: Is having dialog objects on the stack a good idea?
What if the application gets closed while… | |||||
hmmm..... dfaure knows more about exec() and event loops more than me. @dfaure WDYT? ahmadsamir: hmmm..... dfaure knows more about exec() and event loops more than me. @dfaure WDYT? | |||||
The theoretical answer is yes, this would crash. But note that the user cannot just close the application by clicking somewhere while a modal dialog is up. This requires much more subtle interaction like a DBus call. There are a million modal dialogs out there created on the stack, but sure, if you want to be pedantic, use QPointer, new (and manual delete, for lack of a proper smart pointer for this)... (or we could set WA_DeleteOnClose instead of the manual delete? I just added a comment to the blog about that) dfaure: The theoretical answer is yes, this would crash. But note that the user cannot just close the… | |||||
OK, given how many modal dialogs are out there, I'll leave it as is. ahmadsamir: OK, given how many modal dialogs are out there, I'll leave it as is. | |||||
99 | dlg.setFont(theFont, flags & KFontChooser::FixedFontsOnly); | ||||
dfaure: same | |||||
100 | | ||||
101 | const int result = dlg.exec(); | ||||
102 | if (result == Accepted) { | ||||
103 | theFont = dlg.d->chooser->font(); | ||||
104 | diffFlags = dlg.d->chooser->fontDiffFlags(); | ||||
105 | stripRegularStyleName(theFont); | ||||
106 | } | ||||
107 | return result; | ||||
108 | } | ||||
109 | | ||||
110 | // static | ||||
111 | int KFontChooserDialog::getFont(QFont &theFont, const KFontChooser::DisplayFlags &flags, QWidget *parent) | ||||
meven: Shouldn't this be not commented | |||||
112 | { | ||||
113 | KFontChooserDialog dlg(flags, parent); | ||||
114 | dlg.setObjectName(QStringLiteral("Font Selector")); | ||||
115 | dlg.setFont(theFont, flags & KFontChooser::FixedFontsOnly); | ||||
116 | | ||||
117 | const int result = dlg.exec(); | ||||
118 | if (result == Accepted) { | ||||
119 | theFont = dlg.d->chooser->font(); | ||||
120 | stripRegularStyleName(theFont); | ||||
121 | } | ||||
122 | return result; | ||||
123 | } |
Should be static. Even file-static (move the implementation further up in the file).