summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/gui/rasterwindow/rasterwindow.cpp2
-rw-r--r--examples/widgets/dialogs/findfiles/window.cpp139
-rw-r--r--examples/widgets/dialogs/findfiles/window.h6
-rw-r--r--examples/widgets/doc/src/findfiles.qdoc38
-rw-r--r--examples/widgets/tools/codecs/mainwindow.cpp137
-rw-r--r--examples/widgets/tools/codecs/mainwindow.h16
-rw-r--r--examples/widgets/tools/codecs/previewform.cpp176
-rw-r--r--examples/widgets/tools/codecs/previewform.h16
-rw-r--r--examples/widgets/widgets/charactermap/characterwidget.cpp17
-rw-r--r--examples/widgets/widgets/charactermap/characterwidget.h2
-rw-r--r--examples/widgets/widgets/charactermap/mainwindow.cpp121
-rw-r--r--examples/widgets/widgets/charactermap/mainwindow.h6
12 files changed, 480 insertions, 196 deletions
diff --git a/examples/gui/rasterwindow/rasterwindow.cpp b/examples/gui/rasterwindow/rasterwindow.cpp
index c44222b13d..eb34bec252 100644
--- a/examples/gui/rasterwindow/rasterwindow.cpp
+++ b/examples/gui/rasterwindow/rasterwindow.cpp
@@ -55,8 +55,8 @@ RasterWindow::RasterWindow(QWindow *parent)
: QWindow(parent)
, m_update_pending(false)
{
- m_backingStore = new QBackingStore(this);
create();
+ m_backingStore = new QBackingStore(this);
setGeometry(100, 100, 300, 200);
diff --git a/examples/widgets/dialogs/findfiles/window.cpp b/examples/widgets/dialogs/findfiles/window.cpp
index 2f9b187bf0..05e160ed87 100644
--- a/examples/widgets/dialogs/findfiles/window.cpp
+++ b/examples/widgets/dialogs/findfiles/window.cpp
@@ -52,51 +52,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)
@@ -112,14 +133,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);
@@ -127,19 +162,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)
{
@@ -149,21 +186,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);
@@ -171,7 +213,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;
}
@@ -186,13 +228,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);
@@ -201,8 +248,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]
@@ -230,20 +276,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 ae4f49b726..fe217381e2 100644
--- a/examples/widgets/dialogs/findfiles/window.h
+++ b/examples/widgets/dialogs/findfiles/window.h
@@ -73,7 +73,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);
@@ -84,11 +86,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 700105e100..b8363dc698 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.
*/
diff --git a/examples/widgets/tools/codecs/mainwindow.cpp b/examples/widgets/tools/codecs/mainwindow.cpp
index fe29eeffa3..5e4810dd4d 100644
--- a/examples/widgets/tools/codecs/mainwindow.cpp
+++ b/examples/widgets/tools/codecs/mainwindow.cpp
@@ -55,8 +55,8 @@
MainWindow::MainWindow()
{
- textEdit = new QTextEdit;
- textEdit->setLineWrapMode(QTextEdit::NoWrap);
+ textEdit = new QPlainTextEdit;
+ textEdit->setLineWrapMode(QPlainTextEdit::NoWrap);
setCentralWidget(textEdit);
findCodecs();
@@ -64,54 +64,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<QAction *>(sender());
- QByteArray codecName = action->data().toByteArray();
-
- QTextStream out(&file);
- out.setCodec(codecName.constData());
- out << textEdit->toPlainText();
+ const QAction *action = qobject_cast<const QAction *>(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()
@@ -143,9 +146,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)
@@ -155,58 +158,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 1e8b440a02..7121c0b122 100644
--- a/examples/widgets/tools/codecs/mainwindow.h
+++ b/examples/widgets/tools/codecs/mainwindow.h
@@ -56,9 +56,8 @@
QT_BEGIN_NAMESPACE
class QAction;
-class QMenu;
class QTextCodec;
-class QTextEdit;
+class QPlainTextEdit;
QT_END_NAMESPACE
class PreviewForm;
@@ -77,21 +76,12 @@ private slots:
private:
void findCodecs();
- void createActions();
void createMenus();
- QTextEdit *textEdit;
+ QList<QAction *> saveAsActs;
+ QPlainTextEdit *textEdit;
PreviewForm *previewForm;
QList<QTextCodec *> codecs;
-
- QMenu *fileMenu;
- QMenu *helpMenu;
- QMenu *saveAsMenu;
- QAction *openAct;
- QList<QAction *> 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 82203175ca..e5ca13f011 100644
--- a/examples/widgets/tools/codecs/previewform.cpp
+++ b/examples/widgets/tools/codecs/previewform.cpp
@@ -52,47 +52,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<const unsigned char *>(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<ComboBoxIntSignal>(&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<QTextCodec *> &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();
}
@@ -100,12 +212,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 f3e13233b4..1846b976de 100644
--- a/examples/widgets/tools/codecs/previewform.h
+++ b/examples/widgets/tools/codecs/previewform.h
@@ -58,8 +58,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
@@ -67,7 +69,7 @@ class PreviewForm : public QDialog
Q_OBJECT
public:
- PreviewForm(QWidget *parent = 0);
+ explicit PreviewForm(QWidget *parent = Q_NULLPTR);
void setCodecList(const QList<QTextCodec *> &list);
void setEncodedData(const QByteArray &data);
@@ -77,13 +79,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
diff --git a/examples/widgets/widgets/charactermap/characterwidget.cpp b/examples/widgets/widgets/charactermap/characterwidget.cpp
index 07912da6ff..55d45501f1 100644
--- a/examples/widgets/widgets/charactermap/characterwidget.cpp
+++ b/examples/widgets/widgets/charactermap/characterwidget.cpp
@@ -54,11 +54,9 @@
//! [0]
CharacterWidget::CharacterWidget(QWidget *parent)
- : QWidget(parent)
+ : QWidget(parent), columns(16), lastKey(-1)
{
- squareSize = 24;
- columns = 16;
- lastKey = -1;
+ calculateSquareSize();
setMouseTracking(true);
}
//! [0]
@@ -67,7 +65,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();
}
@@ -77,7 +75,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();
}
@@ -89,7 +87,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();
}
@@ -104,6 +102,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 8a33d69628..53add51e6f 100644
--- a/examples/widgets/widgets/charactermap/characterwidget.h
+++ b/examples/widgets/widgets/charactermap/characterwidget.h
@@ -86,6 +86,8 @@ protected:
void paintEvent(QPaintEvent *event) 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 2141850f16..8b406ba1ca 100644
--- a/examples/widgets/widgets/charactermap/mainwindow.cpp
+++ b/examples/widgets/widgets/charactermap/mainwindow.cpp
@@ -54,10 +54,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:"));
@@ -80,38 +100,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);
@@ -163,6 +184,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;
@@ -208,9 +237,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 2865eacc00..eac16b35fa 100644
--- a/examples/widgets/widgets/charactermap/mainwindow.h
+++ b/examples/widgets/widgets/charactermap/mainwindow.h
@@ -73,18 +73,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;