Polish charactermap example

- Use Qt 5 connection syntax.
- Introduce helper function to calculate the square size, remove
  the existing 24 pixel limitation since that makes it impossible
  to render 20pt fonts.
- Add filter chooser for font filters.
- Add menu and info dialog showing DPI and default fonts.
- Streamline code

Change-Id: I0cd4d0475b5a7ed3c475de7a413abebbe688dfe2
Reviewed-by: Topi Reiniö <topi.reinio@theqtcompany.com>
This commit is contained in:
Friedemann Kleint 2016-07-22 09:21:42 +02:00
parent 879fd5bb5c
commit 9b1db44c2a
4 changed files with 117 additions and 29 deletions

View File

@ -44,11 +44,9 @@
//! [0]
CharacterWidget::CharacterWidget(QWidget *parent)
: QWidget(parent)
: QWidget(parent), columns(16), lastKey(-1)
{
squareSize = 24;
columns = 16;
lastKey = -1;
calculateSquareSize();
setMouseTracking(true);
}
//! [0]
@ -57,7 +55,7 @@ CharacterWidget::CharacterWidget(QWidget *parent)
void CharacterWidget::updateFont(const QFont &font)
{
displayFont.setFamily(font.family());
squareSize = qMax(24, QFontMetrics(displayFont).xHeight() * 3);
calculateSquareSize();
adjustSize();
update();
}
@ -67,7 +65,7 @@ void CharacterWidget::updateFont(const QFont &font)
void CharacterWidget::updateSize(const QString &fontSize)
{
displayFont.setPointSize(fontSize.toInt());
squareSize = qMax(24, QFontMetrics(displayFont).xHeight() * 3);
calculateSquareSize();
adjustSize();
update();
}
@ -79,7 +77,7 @@ void CharacterWidget::updateStyle(const QString &fontStyle)
const QFont::StyleStrategy oldStrategy = displayFont.styleStrategy();
displayFont = fontDatabase.font(displayFont.family(), fontStyle, displayFont.pointSize());
displayFont.setStyleStrategy(oldStrategy);
squareSize = qMax(24, QFontMetrics(displayFont).xHeight() * 3);
calculateSquareSize();
adjustSize();
update();
}
@ -94,6 +92,11 @@ void CharacterWidget::updateFontMerging(bool enable)
update();
}
void CharacterWidget::calculateSquareSize()
{
squareSize = qMax(16, 4 + QFontMetrics(displayFont, this).height());
}
//! [3]
QSize CharacterWidget::sizeHint() const
{

View File

@ -76,6 +76,8 @@ protected:
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
private:
void calculateSquareSize();
QFont displayFont;
int columns;
int lastKey;

View File

@ -44,10 +44,30 @@
#include "mainwindow.h"
//! [0]
Q_DECLARE_METATYPE(QFontComboBox::FontFilter)
MainWindow::MainWindow()
{
QMenu *fileMenu = menuBar()->addMenu(tr("File"));
fileMenu->addAction(tr("Quit"), this, &QWidget::close);
QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(tr("Show Font Info"), this, &MainWindow::showInfo);
helpMenu->addAction(tr("About &Qt"), qApp, &QApplication::aboutQt);
QWidget *centralWidget = new QWidget;
QLabel *filterLabel = new QLabel(tr("Filter:"));
filterCombo = new QComboBox;
filterCombo->addItem(tr("All"), QVariant::fromValue(QFontComboBox::AllFonts));
filterCombo->addItem(tr("Scalable"), QVariant::fromValue(QFontComboBox::ScalableFonts));
filterCombo->addItem(tr("Monospaced"), QVariant::fromValue(QFontComboBox::MonospacedFonts));
filterCombo->addItem(tr("Proportional"), QVariant::fromValue(QFontComboBox::ProportionalFonts));
filterCombo->setCurrentIndex(0);
typedef void (QComboBox::*QComboBoxIntSignal)(int);
connect(filterCombo, static_cast<QComboBoxIntSignal>(&QComboBox::currentIndexChanged),
this, &MainWindow::filterChanged);
QLabel *fontLabel = new QLabel(tr("Font:"));
fontCombo = new QFontComboBox;
QLabel *sizeLabel = new QLabel(tr("Size:"));
@ -70,38 +90,39 @@ MainWindow::MainWindow()
//! [2]
lineEdit = new QLineEdit;
lineEdit->setClearButtonEnabled(true);
#ifndef QT_NO_CLIPBOARD
QPushButton *clipboardButton = new QPushButton(tr("&To clipboard"));
//! [2]
//! [3]
clipboard = QApplication::clipboard();
//! [3]
#endif
//! [4]
connect(fontCombo, SIGNAL(currentFontChanged(QFont)),
this, SLOT(findStyles(QFont)));
connect(fontCombo, SIGNAL(currentFontChanged(QFont)),
this, SLOT(findSizes(QFont)));
connect(fontCombo, SIGNAL(currentFontChanged(QFont)),
characterWidget, SLOT(updateFont(QFont)));
connect(sizeCombo, SIGNAL(currentIndexChanged(QString)),
characterWidget, SLOT(updateSize(QString)));
connect(styleCombo, SIGNAL(currentIndexChanged(QString)),
characterWidget, SLOT(updateStyle(QString)));
connect(fontCombo, &QFontComboBox::currentFontChanged,
this, &MainWindow::findStyles);
connect(fontCombo, &QFontComboBox::currentFontChanged,
this, &MainWindow::findSizes);
connect(fontCombo, &QFontComboBox::currentFontChanged,
characterWidget, &CharacterWidget::updateFont);
typedef void (QComboBox::*QComboBoxStringSignal)(const QString &);
connect(sizeCombo, static_cast<QComboBoxStringSignal>(&QComboBox::currentIndexChanged),
characterWidget, &CharacterWidget::updateSize);
connect(styleCombo, static_cast<QComboBoxStringSignal>(&QComboBox::currentIndexChanged),
characterWidget, &CharacterWidget::updateStyle);
//! [4] //! [5]
connect(characterWidget, SIGNAL(characterSelected(QString)),
this, SLOT(insertCharacter(QString)));
connect(characterWidget, &CharacterWidget::characterSelected,
this, &MainWindow::insertCharacter);
#ifndef QT_NO_CLIPBOARD
connect(clipboardButton, SIGNAL(clicked()), this, SLOT(updateClipboard()));
connect(clipboardButton, &QAbstractButton::clicked, this, &MainWindow::updateClipboard);
#endif
//! [5]
connect(fontMerging, SIGNAL(toggled(bool)), characterWidget, SLOT(updateFontMerging(bool)));
connect(fontMerging, &QAbstractButton::toggled, characterWidget, &CharacterWidget::updateFontMerging);
//! [6]
QHBoxLayout *controlsLayout = new QHBoxLayout;
controlsLayout->addWidget(filterLabel);
controlsLayout->addWidget(filterCombo, 1);
controlsLayout->addWidget(fontLabel);
controlsLayout->addWidget(fontCombo, 1);
controlsLayout->addWidget(sizeLabel);
@ -153,6 +174,14 @@ void MainWindow::findStyles(const QFont &font)
}
//! [8]
void MainWindow::filterChanged(int f)
{
const QFontComboBox::FontFilter filter =
filterCombo->itemData(f).value<QFontComboBox::FontFilter>();
fontCombo->setFontFilters(filter);
statusBar()->showMessage(tr("%n font(s) found", 0, fontCombo->count()));
}
void MainWindow::findSizes(const QFont &font)
{
QFontDatabase fontDatabase;
@ -198,9 +227,63 @@ void MainWindow::insertCharacter(const QString &character)
void MainWindow::updateClipboard()
{
//! [11]
clipboard->setText(lineEdit->text(), QClipboard::Clipboard);
QGuiApplication::clipboard()->setText(lineEdit->text(), QClipboard::Clipboard);
//! [11]
clipboard->setText(lineEdit->text(), QClipboard::Selection);
QGuiApplication::clipboard()->setText(lineEdit->text(), QClipboard::Selection);
}
#endif
class FontInfoDialog : public QDialog
{
public:
explicit FontInfoDialog(QWidget *parent = Q_NULLPTR);
private:
QString text() const;
};
FontInfoDialog::FontInfoDialog(QWidget *parent) : QDialog(parent)
{
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
QVBoxLayout *mainLayout = new QVBoxLayout(this);
QPlainTextEdit *textEdit = new QPlainTextEdit(text(), this);
textEdit->setReadOnly(true);
textEdit->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
mainLayout->addWidget(textEdit);
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
mainLayout->addWidget(buttonBox);
}
QString FontInfoDialog::text() const
{
QString text;
QTextStream str(&text);
const QFont defaultFont = QFontDatabase::systemFont(QFontDatabase::GeneralFont);
const QFont fixedFont = QFontDatabase::systemFont(QFontDatabase::FixedFont);
const QFont titleFont = QFontDatabase::systemFont(QFontDatabase::TitleFont);
const QFont smallestReadableFont = QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont);
str << "Qt " << QT_VERSION_STR << " on " << QGuiApplication::platformName()
<< ", " << logicalDpiX() << "DPI";
if (!qFuzzyCompare(devicePixelRatioF(), qreal(1)))
str << ", device pixel ratio: " << devicePixelRatioF();
str << "\n\nDefault font : " << defaultFont.family() << ", " << defaultFont.pointSizeF() << "pt\n"
<< "Fixed font : " << fixedFont.family() << ", " << fixedFont.pointSizeF() << "pt\n"
<< "Title font : " << titleFont.family() << ", " << titleFont.pointSizeF() << "pt\n"
<< "Smallest font: " << smallestReadableFont.family() << ", " << smallestReadableFont.pointSizeF() << "pt\n";
return text;
}
void MainWindow::showInfo()
{
const QRect screenGeometry = QApplication::desktop()->screenGeometry(this);
FontInfoDialog *dialog = new FontInfoDialog(this);
dialog->setWindowTitle(tr("Fonts"));
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->resize(screenGeometry.width() / 4, screenGeometry.height() / 4);
dialog->show();
}
//! [10]

View File

@ -63,18 +63,18 @@ public:
MainWindow();
public slots:
void filterChanged(int);
void findStyles(const QFont &font);
void findSizes(const QFont &font);
void insertCharacter(const QString &character);
#ifndef QT_NO_CLIPBOARD
void updateClipboard();
#endif
void showInfo();
private:
CharacterWidget *characterWidget;
#ifndef QT_NO_CLIPBOARD
QClipboard *clipboard;
#endif
QComboBox *filterCombo;
QComboBox *styleCombo;
QComboBox *sizeCombo;
QFontComboBox *fontCombo;