XBEL stream reader: Brush up

- Use mime types in the file dialog handling
- Use per class includes
- Use the configure system instead of QT_NO... defines

Pick-to: 6.6 6.5
Task-number: QTBUG-111228
Change-Id: Iea915604e89d3005270f0eb83eca882855589a44
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Friedemann Kleint 2023-06-12 13:28:09 +02:00
parent 195c893424
commit 723e331f0a
4 changed files with 37 additions and 23 deletions

View File

@ -122,14 +122,14 @@
\snippet serialization/streambookmarks/mainwindow.cpp 0 \snippet serialization/streambookmarks/mainwindow.cpp 0
The \c open() function enables the user to open an XBEL file using The \c open() function enables the user to open an XBEL file using
QFileDialog::getOpenFileName(). A warning message is displayed along QFileDialog. A warning message is displayed along
with the \c fileName and \c errorString if the file cannot be read or with the \c fileName and \c errorString if the file cannot be read or
if there is a parse error. if there is a parse error.
\snippet serialization/streambookmarks/mainwindow.cpp 1 \snippet serialization/streambookmarks/mainwindow.cpp 1
The \c saveAs() function displays a QFileDialog, prompting the user for The \c saveAs() function displays a QFileDialog, prompting the user for
a \c fileName using QFileDialog::getSaveFileName(). Similar to the a \c fileName. Similar to the
\c open() function, this function also displays a warning message if \c open() function, this function also displays a warning message if
the file cannot be written to. the file cannot be written to.

View File

@ -1,19 +1,34 @@
// Copyright (C) 2016 The Qt Company Ltd. // Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include <QtWidgets>
#include "mainwindow.h" #include "mainwindow.h"
#include "xbelreader.h" #include "xbelreader.h"
#include "xbelwriter.h" #include "xbelwriter.h"
#include <QFileDialog>
#include <QHeaderView>
#include <QMenuBar>
#include <QMessageBox>
#include <QStatusBar>
#include <QTreeWidget>
#include <QAction>
#if QT_CONFIG(clipboard)
# include <QClipboard>
#endif
#include <QDesktopServices>
#include <QGuiApplication>
#include <QScreen>
using namespace Qt::StringLiterals;
//! [0] //! [0]
MainWindow::MainWindow() MainWindow::MainWindow()
{ {
treeWidget = new QTreeWidget; treeWidget = new QTreeWidget;
treeWidget->header()->setSectionResizeMode(QHeaderView::Stretch); treeWidget->header()->setSectionResizeMode(QHeaderView::Stretch);
treeWidget->setHeaderLabels(QStringList{ tr("Title"), tr("Location") }); treeWidget->setHeaderLabels(QStringList{ tr("Title"), tr("Location") });
#if !defined(QT_NO_CONTEXTMENU) && !defined(QT_NO_CLIPBOARD) #if QT_CONFIG(clipboard) && QT_CONFIG(contextmenu)
treeWidget->setContextMenuPolicy(Qt::CustomContextMenu); treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
connect(treeWidget, &QWidget::customContextMenuRequested, connect(treeWidget, &QWidget::customContextMenuRequested,
this, &MainWindow::onCustomContextMenuRequested); this, &MainWindow::onCustomContextMenuRequested);
@ -30,7 +45,7 @@ MainWindow::MainWindow()
} }
//! [0] //! [0]
#if !defined(QT_NO_CONTEXTMENU) && !defined(QT_NO_CLIPBOARD) #if QT_CONFIG(clipboard) && QT_CONFIG(contextmenu)
void MainWindow::onCustomContextMenuRequested(const QPoint &pos) void MainWindow::onCustomContextMenuRequested(const QPoint &pos)
{ {
const QTreeWidgetItem *item = treeWidget->itemAt(pos); const QTreeWidgetItem *item = treeWidget->itemAt(pos);
@ -46,21 +61,19 @@ void MainWindow::onCustomContextMenuRequested(const QPoint &pos)
else if (action == openAction) else if (action == openAction)
QDesktopServices::openUrl(QUrl(url)); QDesktopServices::openUrl(QUrl(url));
} }
#endif // !QT_NO_CONTEXTMENU && !QT_NO_CLIPBOARD #endif // QT_CONFIG(clipboard) && QT_CONFIG(contextmenu)
//! [1] //! [1]
void MainWindow::open() void MainWindow::open()
{ {
QString fileName = QFileDialog fileDialog(this, tr("Open Bookmark File"), QDir::currentPath());
QFileDialog::getOpenFileName(this, tr("Open Bookmark File"), fileDialog.setMimeTypeFilters({"application/x-xbel"_L1});
QDir::currentPath(), if (fileDialog.exec() != QDialog::Accepted)
tr("XBEL Files (*.xbel *.xml)"));
if (fileName.isEmpty())
return; return;
treeWidget->clear(); treeWidget->clear();
const QString fileName = fileDialog.selectedFiles().constFirst();
QFile file(fileName); QFile file(fileName);
if (!file.open(QFile::ReadOnly | QFile::Text)) { if (!file.open(QFile::ReadOnly | QFile::Text)) {
QMessageBox::warning(this, tr("QXmlStream Bookmarks"), QMessageBox::warning(this, tr("QXmlStream Bookmarks"),
@ -86,13 +99,14 @@ void MainWindow::open()
//! [2] //! [2]
void MainWindow::saveAs() void MainWindow::saveAs()
{ {
QString fileName = QFileDialog fileDialog(this, tr("Save Bookmark File"), QDir::currentPath());
QFileDialog::getSaveFileName(this, tr("Save Bookmark File"), fileDialog.setAcceptMode(QFileDialog::AcceptSave);
QDir::currentPath(), fileDialog.setDefaultSuffix("xbel"_L1);
tr("XBEL Files (*.xbel *.xml)")); fileDialog.setMimeTypeFilters({"application/x-xbel"_L1});
if (fileName.isEmpty()) if (fileDialog.exec() != QDialog::Accepted)
return; return;
const QString fileName = fileDialog.selectedFiles().constFirst();
QFile file(fileName); QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text)) { if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, tr("QXmlStream Bookmarks"), QMessageBox::warning(this, tr("QXmlStream Bookmarks"),

View File

@ -1,10 +1,11 @@
// Copyright (C) 2016 The Qt Company Ltd. // Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include <QtWidgets>
#include "xbelreader.h" #include "xbelreader.h"
#include <QStyle>
#include <QTreeWidget>
using namespace Qt::StringLiterals; using namespace Qt::StringLiterals;
//! [0] //! [0]

View File

@ -1,10 +1,9 @@
// Copyright (C) 2016 The Qt Company Ltd. // Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include <QtWidgets>
#include "xbelwriter.h" #include "xbelwriter.h"
#include "xbelreader.h"
#include <QTreeWidget>
using namespace Qt::StringLiterals; using namespace Qt::StringLiterals;