From 9b1db44c2a94a8d4d56c85e97c391c5bdf762a95 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Fri, 22 Jul 2016 09:21:42 +0200 Subject: Polish charactermap example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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ö --- .../widgets/charactermap/characterwidget.cpp | 17 +-- .../widgets/widgets/charactermap/characterwidget.h | 2 + .../widgets/widgets/charactermap/mainwindow.cpp | 121 +++++++++++++++++---- examples/widgets/widgets/charactermap/mainwindow.h | 6 +- 4 files changed, 117 insertions(+), 29 deletions(-) (limited to 'examples/widgets') diff --git a/examples/widgets/widgets/charactermap/characterwidget.cpp b/examples/widgets/widgets/charactermap/characterwidget.cpp index 1b8cb226f7..47aff4f5be 100644 --- a/examples/widgets/widgets/charactermap/characterwidget.cpp +++ b/examples/widgets/widgets/charactermap/characterwidget.cpp @@ -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 { diff --git a/examples/widgets/widgets/charactermap/characterwidget.h b/examples/widgets/widgets/charactermap/characterwidget.h index d1c0f28bc4..e195c177d0 100644 --- a/examples/widgets/widgets/charactermap/characterwidget.h +++ b/examples/widgets/widgets/charactermap/characterwidget.h @@ -76,6 +76,8 @@ protected: void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; private: + void calculateSquareSize(); + QFont displayFont; int columns; int lastKey; diff --git a/examples/widgets/widgets/charactermap/mainwindow.cpp b/examples/widgets/widgets/charactermap/mainwindow.cpp index 14784e7d65..a49e74769e 100644 --- a/examples/widgets/widgets/charactermap/mainwindow.cpp +++ b/examples/widgets/widgets/charactermap/mainwindow.cpp @@ -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(&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(&QComboBox::currentIndexChanged), + characterWidget, &CharacterWidget::updateSize); + connect(styleCombo, static_cast(&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(); + 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] diff --git a/examples/widgets/widgets/charactermap/mainwindow.h b/examples/widgets/widgets/charactermap/mainwindow.h index 75e7e78fed..b24816418b 100644 --- a/examples/widgets/widgets/charactermap/mainwindow.h +++ b/examples/widgets/widgets/charactermap/mainwindow.h @@ -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; -- cgit v1.2.3 From d5be0d30588f6ccd971f73a3c030f9c10d8a6b4e Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Thu, 7 Jul 2016 15:30:00 +0200 Subject: Polish the codecs example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Port to Qt 5 connection syntax. - Remove unneeded member variables. - Adapt to screen size. - Add a tab widget with a hex dump view to the preview dialog. - Handle conversion errors in preview dialog, add status label displaying errors and warnings about failures and invalid characters encountered. - Fix translated messages. Change-Id: I916100c903e73d0d2326523753ed7398b1c34df0 Reviewed-by: Topi Reiniö --- examples/widgets/tools/codecs/mainwindow.cpp | 137 +++++++++----------- examples/widgets/tools/codecs/mainwindow.h | 16 +-- examples/widgets/tools/codecs/previewform.cpp | 176 ++++++++++++++++++++++---- examples/widgets/tools/codecs/previewform.h | 16 ++- 4 files changed, 227 insertions(+), 118 deletions(-) (limited to 'examples/widgets') diff --git a/examples/widgets/tools/codecs/mainwindow.cpp b/examples/widgets/tools/codecs/mainwindow.cpp index f25be7304e..3c8ec58881 100644 --- a/examples/widgets/tools/codecs/mainwindow.cpp +++ b/examples/widgets/tools/codecs/mainwindow.cpp @@ -45,8 +45,8 @@ MainWindow::MainWindow() { - textEdit = new QTextEdit; - textEdit->setLineWrapMode(QTextEdit::NoWrap); + textEdit = new QPlainTextEdit; + textEdit->setLineWrapMode(QPlainTextEdit::NoWrap); setCentralWidget(textEdit); findCodecs(); @@ -54,54 +54,57 @@ MainWindow::MainWindow() previewForm = new PreviewForm(this); previewForm->setCodecList(codecs); - createActions(); createMenus(); setWindowTitle(tr("Codecs")); - resize(500, 400); + + const QRect screenGeometry = QApplication::desktop()->screenGeometry(this); + resize(screenGeometry.width() / 2, screenGeometry.height() * 2 / 3); } void MainWindow::open() { - QString fileName = QFileDialog::getOpenFileName(this); - if (!fileName.isEmpty()) { - QFile file(fileName); - if (!file.open(QFile::ReadOnly)) { - QMessageBox::warning(this, tr("Codecs"), - tr("Cannot read file %1:\n%2") - .arg(fileName) - .arg(file.errorString())); - return; - } + const QString fileName = QFileDialog::getOpenFileName(this); + if (fileName.isEmpty()) + return; + QFile file(fileName); + if (!file.open(QFile::ReadOnly)) { + QMessageBox::warning(this, tr("Codecs"), + tr("Cannot read file %1:\n%2") + .arg(QDir::toNativeSeparators(fileName), + file.errorString())); + return; + } - QByteArray data = file.readAll(); + const QByteArray data = file.readAll(); - previewForm->setEncodedData(data); - if (previewForm->exec()) - textEdit->setPlainText(previewForm->decodedString()); - } + previewForm->setWindowTitle(tr("Choose Encoding for %1").arg(QFileInfo(fileName).fileName())); + previewForm->setEncodedData(data); + if (previewForm->exec()) + textEdit->setPlainText(previewForm->decodedString()); } void MainWindow::save() { - QString fileName = QFileDialog::getSaveFileName(this); - if (!fileName.isEmpty()) { - QFile file(fileName); - if (!file.open(QFile::WriteOnly | QFile::Text)) { - QMessageBox::warning(this, tr("Codecs"), - tr("Cannot write file %1:\n%2") - .arg(fileName) - .arg(file.errorString())); - return; - } - - QAction *action = qobject_cast(sender()); - QByteArray codecName = action->data().toByteArray(); - - QTextStream out(&file); - out.setCodec(codecName.constData()); - out << textEdit->toPlainText(); + const QAction *action = qobject_cast(sender()); + const QByteArray codecName = action->data().toByteArray(); + const QString title = tr("Save As (%1)").arg(QLatin1String(codecName)); + + QString fileName = QFileDialog::getSaveFileName(this, title); + if (fileName.isEmpty()) + return; + QFile file(fileName); + if (!file.open(QFile::WriteOnly | QFile::Text)) { + QMessageBox::warning(this, tr("Codecs"), + tr("Cannot write file %1:\n%2") + .arg(QDir::toNativeSeparators(fileName), + file.errorString())); + return; } + + QTextStream out(&file); + out.setCodec(codecName.constData()); + out << textEdit->toPlainText(); } void MainWindow::about() @@ -133,9 +136,9 @@ void MainWindow::findCodecs() QString sortKey = codec->name().toUpper(); int rank; - if (sortKey.startsWith("UTF-8")) { + if (sortKey.startsWith(QLatin1String("UTF-8"))) { rank = 1; - } else if (sortKey.startsWith("UTF-16")) { + } else if (sortKey.startsWith(QLatin1String("UTF-16"))) { rank = 2; } else if (iso8859RegExp.exactMatch(sortKey)) { if (iso8859RegExp.cap(1).size() == 1) @@ -145,58 +148,38 @@ void MainWindow::findCodecs() } else { rank = 5; } - sortKey.prepend(QChar('0' + rank)); + sortKey.prepend(QLatin1Char('0' + rank)); codecMap.insert(sortKey, codec); } codecs = codecMap.values(); } -void MainWindow::createActions() +void MainWindow::createMenus() { - openAct = new QAction(tr("&Open..."), this); + QMenu *fileMenu = menuBar()->addMenu(tr("&File")); + QAction *openAct = + fileMenu->addAction(tr("&Open..."), this, &MainWindow::open); openAct->setShortcuts(QKeySequence::Open); - connect(openAct, SIGNAL(triggered()), this, SLOT(open())); - - foreach (QTextCodec *codec, codecs) { - QString text = tr("%1...").arg(QString(codec->name())); - QAction *action = new QAction(text, this); - action->setData(codec->name()); - connect(action, SIGNAL(triggered()), this, SLOT(save())); + QMenu *saveAsMenu = fileMenu->addMenu(tr("&Save As")); + connect(saveAsMenu, &QMenu::aboutToShow, + this, &MainWindow::aboutToShowSaveAsMenu); + foreach (const QTextCodec *codec, codecs) { + const QByteArray name = codec->name(); + QAction *action = saveAsMenu->addAction(tr("%1...").arg(QLatin1String(name))); + action->setData(QVariant(name)); + connect(action, &QAction::triggered, this, &MainWindow::save); saveAsActs.append(action); } - exitAct = new QAction(tr("E&xit"), this); - exitAct->setShortcuts(QKeySequence::Quit); - connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); - - aboutAct = new QAction(tr("&About"), this); - connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); - - aboutQtAct = new QAction(tr("About &Qt"), this); - connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); -} - -void MainWindow::createMenus() -{ - saveAsMenu = new QMenu(tr("&Save As"), this); - foreach (QAction *action, saveAsActs) - saveAsMenu->addAction(action); - connect(saveAsMenu, SIGNAL(aboutToShow()), - this, SLOT(aboutToShowSaveAsMenu())); - - fileMenu = new QMenu(tr("&File"), this); - fileMenu->addAction(openAct); - fileMenu->addMenu(saveAsMenu); fileMenu->addSeparator(); - fileMenu->addAction(exitAct); - - helpMenu = new QMenu(tr("&Help"), this); - helpMenu->addAction(aboutAct); - helpMenu->addAction(aboutQtAct); + QAction *exitAct = fileMenu->addAction(tr("E&xit"), this, &QWidget::close); + exitAct->setShortcuts(QKeySequence::Quit); - menuBar()->addMenu(fileMenu); menuBar()->addSeparator(); - menuBar()->addMenu(helpMenu); + + QMenu *helpMenu = menuBar()->addMenu(tr("&Help")); + helpMenu->addAction(tr("&About"), this, &MainWindow::about); + helpMenu->addAction(tr("About &Qt"), qApp, &QApplication::aboutQt); } diff --git a/examples/widgets/tools/codecs/mainwindow.h b/examples/widgets/tools/codecs/mainwindow.h index 7f1d62627e..5455daeb84 100644 --- a/examples/widgets/tools/codecs/mainwindow.h +++ b/examples/widgets/tools/codecs/mainwindow.h @@ -46,9 +46,8 @@ QT_BEGIN_NAMESPACE class QAction; -class QMenu; class QTextCodec; -class QTextEdit; +class QPlainTextEdit; QT_END_NAMESPACE class PreviewForm; @@ -67,21 +66,12 @@ private slots: private: void findCodecs(); - void createActions(); void createMenus(); - QTextEdit *textEdit; + QList saveAsActs; + QPlainTextEdit *textEdit; PreviewForm *previewForm; QList codecs; - - QMenu *fileMenu; - QMenu *helpMenu; - QMenu *saveAsMenu; - QAction *openAct; - QList saveAsActs; - QAction *exitAct; - QAction *aboutAct; - QAction *aboutQtAct; }; #endif diff --git a/examples/widgets/tools/codecs/previewform.cpp b/examples/widgets/tools/codecs/previewform.cpp index f6199a9b35..fdfb90f56c 100644 --- a/examples/widgets/tools/codecs/previewform.cpp +++ b/examples/widgets/tools/codecs/previewform.cpp @@ -42,47 +42,159 @@ #include "previewform.h" +// Helpers for creating hex dumps +static void indent(QTextStream &str, int indent) +{ + for (int i = 0; i < indent; ++i) + str << ' '; +} + +static void formatHex(QTextStream &str, const QByteArray &data) +{ + const int fieldWidth = str.fieldWidth(); + const QTextStream::FieldAlignment alignment = str.fieldAlignment(); + const int base = str.integerBase(); + const QChar padChar = str.padChar(); + str.setIntegerBase(16); + str.setPadChar(QLatin1Char('0')); + str.setFieldAlignment(QTextStream::AlignRight); + + const unsigned char *p = reinterpret_cast(data.constBegin()); + for (const unsigned char *end = p + data.size(); p < end; ++p) { + str << ' '; + str.setFieldWidth(2); + str << unsigned(*p); + str.setFieldWidth(fieldWidth); + } + str.setFieldAlignment(alignment); + str.setPadChar(padChar); + str.setIntegerBase(base); +} + +static void formatPrintableCharacters(QTextStream &str, const QByteArray &data) +{ + for (int i = 0, size = data.size(); i < size; ++i) { + const char c = data.at(i); + switch (c) { + case '\0': + str << "\\0"; + break; + case '\t': + str << "\\t"; + break; + case '\r': + str << "\\r"; + break; + case '\n': + str << "\\n"; + break; + default: + if (c >= 32 && uchar(c) < 127) + str << ' ' << c; + else + str << ".."; + break; + } + } +} + +static QString formatHexDump(const QByteArray &data) +{ + enum { lineWidth = 16 }; + QString result; + QTextStream str(&result); + str.setIntegerBase(16); + str.setPadChar(QLatin1Char('0')); + const int fieldWidth = str.fieldWidth(); + const QTextStream::FieldAlignment alignment = str.fieldAlignment(); + for (int a = 0, size = data.size(); a < size; a += lineWidth) { + str.setFieldAlignment(QTextStream::AlignRight); + str.setFieldWidth(8); + str << a; + str.setFieldWidth(fieldWidth); + str.setFieldAlignment(alignment); + + const int end = qMin(a + lineWidth, size); + const QByteArray line = data.mid(a, end - a); + + formatHex(str, line); + indent(str, 3 * (lineWidth - line.size())); + + str << ' '; + formatPrintableCharacters(str, line); + indent(str, 2 * (lineWidth - line.size())); + str << '\n'; + } + return result; +} + PreviewForm::PreviewForm(QWidget *parent) : QDialog(parent) { + setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); encodingComboBox = new QComboBox; - encodingLabel = new QLabel(tr("&Encoding:")); + QLabel *encodingLabel = new QLabel(tr("&Encoding:")); encodingLabel->setBuddy(encodingComboBox); - textEdit = new QTextEdit; - textEdit->setLineWrapMode(QTextEdit::NoWrap); + textEdit = new QPlainTextEdit; + textEdit->setLineWrapMode(QPlainTextEdit::NoWrap); textEdit->setReadOnly(true); + hexDumpEdit = new QPlainTextEdit; + hexDumpEdit->setLineWrapMode(QPlainTextEdit::NoWrap); + hexDumpEdit->setReadOnly(true); + hexDumpEdit->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont)); - buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok - | QDialogButtonBox::Cancel); + QDialogButtonBox *buttonBox = + new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); + okButton = buttonBox->button(QDialogButtonBox::Ok); - connect(encodingComboBox, SIGNAL(activated(int)), - this, SLOT(updateTextEdit())); - connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())); - connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); + typedef void(QComboBox::*ComboBoxIntSignal)(int); + connect(encodingComboBox, static_cast(&QComboBox::activated), + this, &PreviewForm::updateTextEdit); + connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); + connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); - QGridLayout *mainLayout = new QGridLayout; + QGridLayout *mainLayout = new QGridLayout(this); mainLayout->addWidget(encodingLabel, 0, 0); mainLayout->addWidget(encodingComboBox, 0, 1); - mainLayout->addWidget(textEdit, 1, 0, 1, 2); - mainLayout->addWidget(buttonBox, 2, 0, 1, 2); - setLayout(mainLayout); + tabWidget = new QTabWidget; + tabWidget->addTab(textEdit, tr("Preview")); + tabWidget->addTab(hexDumpEdit, tr("Hex Dump")); + mainLayout->addWidget(tabWidget, 1, 0, 1, 2); + statusLabel = new QLabel; + mainLayout->addWidget(statusLabel, 2, 0, 1, 2); + mainLayout->addWidget(buttonBox, 3, 0, 1, 2); - setWindowTitle(tr("Choose Encoding")); - resize(400, 300); + const QRect screenGeometry = QApplication::desktop()->screenGeometry(this); + resize(screenGeometry.width() * 2 / 5, screenGeometry.height() / 2); } void PreviewForm::setCodecList(const QList &list) { encodingComboBox->clear(); - foreach (QTextCodec *codec, list) - encodingComboBox->addItem(codec->name(), codec->mibEnum()); + foreach (const QTextCodec *codec, list) { + encodingComboBox->addItem(QLatin1String(codec->name()), + QVariant(codec->mibEnum())); + } +} + +void PreviewForm::reset() +{ + decodedStr.clear(); + textEdit->clear(); + hexDumpEdit->clear(); + statusLabel->clear(); + statusLabel->setStyleSheet(QString()); + okButton->setEnabled(false); + tabWidget->setCurrentIndex(0); } void PreviewForm::setEncodedData(const QByteArray &data) { + reset(); encodedData = data; + hexDumpEdit->setPlainText(formatHexDump(data)); updateTextEdit(); } @@ -90,12 +202,30 @@ void PreviewForm::updateTextEdit() { int mib = encodingComboBox->itemData( encodingComboBox->currentIndex()).toInt(); - QTextCodec *codec = QTextCodec::codecForMib(mib); + const QTextCodec *codec = QTextCodec::codecForMib(mib); + const QString name = QLatin1String(codec->name()); - QTextStream in(&encodedData); - in.setAutoDetectUnicode(false); - in.setCodec(codec); - decodedStr = in.readAll(); + QTextCodec::ConverterState state; + decodedStr = codec->toUnicode(encodedData.constData(), encodedData.size(), &state); - textEdit->setPlainText(decodedStr); + bool success = true; + if (state.remainingChars) { + success = false; + const QString message = + tr("%1: conversion error at character %2") + .arg(name).arg(encodedData.size() - state.remainingChars + 1); + statusLabel->setText(message); + statusLabel->setStyleSheet(QStringLiteral("background-color: \"red\";")); + } else if (state.invalidChars) { + statusLabel->setText(tr("%1: %n invalid characters", 0, state.invalidChars).arg(name)); + statusLabel->setStyleSheet(QStringLiteral("background-color: \"yellow\";")); + } else { + statusLabel->setText(tr("%1: %n bytes converted", 0, encodedData.size()).arg(name)); + statusLabel->setStyleSheet(QString()); + } + if (success) + textEdit->setPlainText(decodedStr); + else + textEdit->clear(); + okButton->setEnabled(success); } diff --git a/examples/widgets/tools/codecs/previewform.h b/examples/widgets/tools/codecs/previewform.h index 93bfed3275..1047a50d80 100644 --- a/examples/widgets/tools/codecs/previewform.h +++ b/examples/widgets/tools/codecs/previewform.h @@ -48,8 +48,10 @@ QT_BEGIN_NAMESPACE class QComboBox; class QDialogButtonBox; class QLabel; +class QPlainTextEdit; +class QPushButton; +class QTabWidget; class QTextCodec; -class QTextEdit; QT_END_NAMESPACE class PreviewForm : public QDialog @@ -57,7 +59,7 @@ class PreviewForm : public QDialog Q_OBJECT public: - PreviewForm(QWidget *parent = 0); + explicit PreviewForm(QWidget *parent = Q_NULLPTR); void setCodecList(const QList &list); void setEncodedData(const QByteArray &data); @@ -67,13 +69,17 @@ private slots: void updateTextEdit(); private: + void reset(); + QByteArray encodedData; QString decodedStr; + QPushButton *okButton; + QTabWidget *tabWidget; QComboBox *encodingComboBox; - QLabel *encodingLabel; - QTextEdit *textEdit; - QDialogButtonBox *buttonBox; + QPlainTextEdit *textEdit; + QPlainTextEdit *hexDumpEdit; + QLabel *statusLabel; }; #endif -- cgit v1.2.3 From d1a30be5abcc2d5e5340866434b2691275a135a6 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 5 Jul 2016 13:26:53 +0200 Subject: Polish the findfiles example to be actually useful MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Simplify the code, remove unused members - Fix the translations of plurals to use %n - Add tooltip displaying full paths in list - Add context menu allowing to copy the name and open - Display the correct slashes on Windows - Connect the returnPressed() signals of the line edits - Make the search recursive - Do not search binary files by checking the mime type Change-Id: I3663799c88931db1f58c03ea35211e7ab03737ec Reviewed-by: Topi Reiniö --- examples/widgets/dialogs/findfiles/window.cpp | 139 +++++++++++++++++++------- examples/widgets/dialogs/findfiles/window.h | 6 +- examples/widgets/doc/src/findfiles.qdoc | 38 +++++-- 3 files changed, 135 insertions(+), 48 deletions(-) (limited to 'examples/widgets') diff --git a/examples/widgets/dialogs/findfiles/window.cpp b/examples/widgets/dialogs/findfiles/window.cpp index 780c398ad5..ce53dd8c83 100644 --- a/examples/widgets/dialogs/findfiles/window.cpp +++ b/examples/widgets/dialogs/findfiles/window.cpp @@ -42,51 +42,72 @@ #include "window.h" +//! [17] +enum { absoluteFileNameRole = Qt::UserRole + 1 }; +//! [17] + +//! [18] +static inline QString fileNameOfItem(const QTableWidgetItem *item) +{ + return item->data(absoluteFileNameRole).toString(); +} +//! [18] + +//! [14] +static inline void openFile(const QString &fileName) +{ + QDesktopServices::openUrl(QUrl::fromLocalFile(fileName)); +} +//! [14] + //! [0] Window::Window(QWidget *parent) : QWidget(parent) { - browseButton = new QPushButton(tr("&Browse..."), this); + QPushButton *browseButton = new QPushButton(tr("&Browse..."), this); connect(browseButton, &QAbstractButton::clicked, this, &Window::browse); findButton = new QPushButton(tr("&Find"), this); connect(findButton, &QAbstractButton::clicked, this, &Window::find); fileComboBox = createComboBox(tr("*")); + connect(fileComboBox->lineEdit(), &QLineEdit::returnPressed, + this, &Window::animateFindClick); textComboBox = createComboBox(); - directoryComboBox = createComboBox(QDir::currentPath()); + connect(textComboBox->lineEdit(), &QLineEdit::returnPressed, + this, &Window::animateFindClick); + directoryComboBox = createComboBox(QDir::toNativeSeparators(QDir::currentPath())); + connect(directoryComboBox->lineEdit(), &QLineEdit::returnPressed, + this, &Window::animateFindClick); - fileLabel = new QLabel(tr("Named:")); - textLabel = new QLabel(tr("Containing text:")); - directoryLabel = new QLabel(tr("In directory:")); filesFoundLabel = new QLabel; createFilesTable(); //! [0] //! [1] - QGridLayout *mainLayout = new QGridLayout; - mainLayout->addWidget(fileLabel, 0, 0); + QGridLayout *mainLayout = new QGridLayout(this); + mainLayout->addWidget(new QLabel(tr("Named:")), 0, 0); mainLayout->addWidget(fileComboBox, 0, 1, 1, 2); - mainLayout->addWidget(textLabel, 1, 0); + mainLayout->addWidget(new QLabel(tr("Containing text:")), 1, 0); mainLayout->addWidget(textComboBox, 1, 1, 1, 2); - mainLayout->addWidget(directoryLabel, 2, 0); + mainLayout->addWidget(new QLabel(tr("In directory:")), 2, 0); mainLayout->addWidget(directoryComboBox, 2, 1); mainLayout->addWidget(browseButton, 2, 2); mainLayout->addWidget(filesTable, 3, 0, 1, 3); mainLayout->addWidget(filesFoundLabel, 4, 0, 1, 2); mainLayout->addWidget(findButton, 4, 2); - setLayout(mainLayout); setWindowTitle(tr("Find Files")); - resize(700, 300); + const QRect screenGeometry = QApplication::desktop()->screenGeometry(this); + resize(screenGeometry.width() / 2, screenGeometry.height() / 3); } //! [1] //! [2] void Window::browse() { - QString directory = QFileDialog::getExistingDirectory(this, - tr("Find Files"), QDir::currentPath()); + QString directory = + QDir::toNativeSeparators(QFileDialog::getExistingDirectory(this, tr("Find Files"), QDir::currentPath())); if (!directory.isEmpty()) { if (directoryComboBox->findText(directory) == -1) @@ -102,14 +123,28 @@ static void updateComboBox(QComboBox *comboBox) comboBox->addItem(comboBox->currentText()); } +//! [13] + +static void findRecursion(const QString &path, const QString &pattern, QStringList *result) +{ + QDir currentDir(path); + const QString prefix = path + QLatin1Char('/'); + foreach (const QString &match, currentDir.entryList(QStringList(pattern), QDir::Files | QDir::NoSymLinks)) + result->append(prefix + match); + foreach (const QString &dir, currentDir.entryList(QDir::Dirs | QDir::NoSymLinks | QDir::NoDotAndDotDot)) + findRecursion(prefix + dir, pattern, result); +} + +//! [13] //! [3] + void Window::find() { filesTable->setRowCount(0); QString fileName = fileComboBox->currentText(); QString text = textComboBox->currentText(); - QString path = directoryComboBox->currentText(); + QString path = QDir::cleanPath(directoryComboBox->currentText()); //! [3] updateComboBox(fileComboBox); @@ -117,19 +152,21 @@ void Window::find() updateComboBox(directoryComboBox); //! [4] + currentDir = QDir(path); QStringList files; - if (fileName.isEmpty()) - fileName = "*"; - files = currentDir.entryList(QStringList(fileName), - QDir::Files | QDir::NoSymLinks); - + findRecursion(path, fileName.isEmpty() ? QStringLiteral("*") : fileName, &files); if (!text.isEmpty()) files = findFiles(files, text); showFiles(files); } //! [4] +void Window::animateFindClick() +{ + findButton->animateClick(); +} + //! [5] QStringList Window::findFiles(const QStringList &files, const QString &text) { @@ -139,21 +176,26 @@ QStringList Window::findFiles(const QStringList &files, const QString &text) progressDialog.setWindowTitle(tr("Find Files")); //! [5] //! [6] + QMimeDatabase mimeDatabase; QStringList foundFiles; for (int i = 0; i < files.size(); ++i) { progressDialog.setValue(i); - progressDialog.setLabelText(tr("Searching file number %1 of %2...") - .arg(i).arg(files.size())); - qApp->processEvents(); + progressDialog.setLabelText(tr("Searching file number %1 of %n...", 0, files.size()).arg(i)); + QCoreApplication::processEvents(); //! [6] if (progressDialog.wasCanceled()) break; //! [7] - QFile file(currentDir.absoluteFilePath(files[i])); - + const QString fileName = files.at(i); + const QMimeType mimeType = mimeDatabase.mimeTypeForFile(fileName); + if (mimeType.isValid() && !mimeType.inherits(QStringLiteral("text/plain"))) { + qWarning() << "Not searching binary file " << QDir::toNativeSeparators(fileName); + continue; + } + QFile file(fileName); if (file.open(QIODevice::ReadOnly)) { QString line; QTextStream in(&file); @@ -161,7 +203,7 @@ QStringList Window::findFiles(const QStringList &files, const QString &text) if (progressDialog.wasCanceled()) break; line = in.readLine(); - if (line.contains(text)) { + if (line.contains(text, Qt::CaseInsensitive)) { foundFiles << files[i]; break; } @@ -176,13 +218,18 @@ QStringList Window::findFiles(const QStringList &files, const QString &text) void Window::showFiles(const QStringList &files) { for (int i = 0; i < files.size(); ++i) { - QFile file(currentDir.absoluteFilePath(files[i])); - qint64 size = QFileInfo(file).size(); - - QTableWidgetItem *fileNameItem = new QTableWidgetItem(files[i]); + const QString &fileName = files.at(i); + const QString toolTip = QDir::toNativeSeparators(fileName); + const QString relativePath = QDir::toNativeSeparators(currentDir.relativeFilePath(fileName)); + const qint64 size = QFileInfo(fileName).size(); + QTableWidgetItem *fileNameItem = new QTableWidgetItem(relativePath); + fileNameItem->setData(absoluteFileNameRole, QVariant(fileName)); + fileNameItem->setToolTip(toolTip); fileNameItem->setFlags(fileNameItem->flags() ^ Qt::ItemIsEditable); QTableWidgetItem *sizeItem = new QTableWidgetItem(tr("%1 KB") .arg(int((size + 1023) / 1024))); + sizeItem->setData(absoluteFileNameRole, QVariant(fileName)); + sizeItem->setToolTip(toolTip); sizeItem->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter); sizeItem->setFlags(sizeItem->flags() ^ Qt::ItemIsEditable); @@ -191,8 +238,7 @@ void Window::showFiles(const QStringList &files) filesTable->setItem(row, 0, fileNameItem); filesTable->setItem(row, 1, sizeItem); } - filesFoundLabel->setText(tr("%1 file(s) found").arg(files.size()) + - (" (Double click on a file to open it)")); + filesFoundLabel->setText(tr("%n file(s) found (Double click on a file to open it)", 0, files.size())); filesFoundLabel->setWordWrap(true); } //! [8] @@ -220,20 +266,43 @@ void Window::createFilesTable() filesTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); filesTable->verticalHeader()->hide(); filesTable->setShowGrid(false); - +//! [15] + filesTable->setContextMenuPolicy(Qt::CustomContextMenu); + connect(filesTable, &QTableWidget::customContextMenuRequested, + this, &Window::contextMenu); connect(filesTable, &QTableWidget::cellActivated, this, &Window::openFileOfItem); +//! [15] } //! [11] + //! [12] void Window::openFileOfItem(int row, int /* column */) { - QTableWidgetItem *item = filesTable->item(row, 0); - - QDesktopServices::openUrl(QUrl::fromLocalFile(currentDir.absoluteFilePath(item->text()))); + const QTableWidgetItem *item = filesTable->item(row, 0); + openFile(fileNameOfItem(item)); } //! [12] +//! [16] +void Window::contextMenu(const QPoint &pos) +{ + const QTableWidgetItem *item = filesTable->itemAt(pos); + if (!item) + return; + QMenu menu; + QAction *copyAction = menu.addAction("Copy Name"); + QAction *openAction = menu.addAction("Open"); + QAction *action = menu.exec(filesTable->mapToGlobal(pos)); + if (!action) + return; + const QString fileName = fileNameOfItem(item); + if (action == copyAction) + QGuiApplication::clipboard()->setText(QDir::toNativeSeparators(fileName)); + else if (action == openAction) + openFile(fileName); +} +//! [16] diff --git a/examples/widgets/dialogs/findfiles/window.h b/examples/widgets/dialogs/findfiles/window.h index 89dd87b83b..119c0fa100 100644 --- a/examples/widgets/dialogs/findfiles/window.h +++ b/examples/widgets/dialogs/findfiles/window.h @@ -63,7 +63,9 @@ public: private slots: void browse(); void find(); + void animateFindClick(); void openFileOfItem(int row, int column); + void contextMenu(const QPoint &pos); private: QStringList findFiles(const QStringList &files, const QString &text); @@ -74,11 +76,7 @@ private: QComboBox *fileComboBox; QComboBox *textComboBox; QComboBox *directoryComboBox; - QLabel *fileLabel; - QLabel *textLabel; - QLabel *directoryLabel; QLabel *filesFoundLabel; - QPushButton *browseButton; QPushButton *findButton; QTableWidget *filesTable; diff --git a/examples/widgets/doc/src/findfiles.qdoc b/examples/widgets/doc/src/findfiles.qdoc index dd06ed8bb4..df2ef40768 100644 --- a/examples/widgets/doc/src/findfiles.qdoc +++ b/examples/widgets/doc/src/findfiles.qdoc @@ -120,10 +120,12 @@ \snippet dialogs/findfiles/window.cpp 4 We use the directory's path to create a QDir; the QDir class - provides access to directory structures and their contents. We - create a list of the files (contained in the newly created QDir) - that match the specified file name. If the file name is empty - the list will contain all the files in the directory. + provides access to directory structures and their contents. + + \snippet dialogs/findfiles/window.cpp 13 + + We recursively create a list of the files (contained in the newl + created QDir) that match the specified file name. Then we search through all the files in the list, using the private \c findFiles() function, eliminating the ones that don't contain @@ -173,9 +175,7 @@ \snippet dialogs/findfiles/window.cpp 7 - After updating the QProgressDialog, we create a QFile using the - QDir::absoluteFilePath() function which returns the absolute path - name of a file in the directory. We open the file in read-only + After updating the QProgressDialog, we open the file in read-only mode, and read one line at a time using QTextStream. The QTextStream class provides a convenient interface for reading @@ -194,9 +194,18 @@ Both the \c findFiles() and \c showFiles() functions are called from the \c find() slot. In the \c showFiles() function we run through - the provided list of file names, adding each file name to the + the provided list of file names, adding each relative file name to the first column in the table widget and retrieving the file's size using - QFile and QFileInfo for the second column. + QFileInfo for the second column. For later use, we set + the absolute path as a data on the QTableWidget using the + the role absoluteFileNameRole defined to be Qt::UserRole + 1. + + \snippet dialogs/findfiles/window.cpp 17 + + This allows for retrieving the name of an item using a + convenience function: + + \snippet dialogs/findfiles/window.cpp 18 We also update the total number of files found. @@ -236,8 +245,19 @@ \snippet dialogs/findfiles/window.cpp 12 + \snippet dialogs/findfiles/window.cpp 14 + The \c openFileOfItem() slot is invoked when the user double clicks on a cell in the table. The QDesktopServices::openUrl() knows how to open a file given the file name. + + \snippet dialogs/findfiles/window.cpp 15 + \snippet dialogs/findfiles/window.cpp 16 + + We set the context menu policy to of the table view to Qt::CustomContextMenu + and connect a slot contextMenu() to its signal + customContextMenuRequested(). We retrieve the absolute file name + from the data of the QTableWidgetItem and populate the context menu + with actions offering to copy the file name and to open the file. */ -- cgit v1.2.3