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
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
if there is a parse error.
\snippet serialization/streambookmarks/mainwindow.cpp 1
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
the file cannot be written to.

View File

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

View File

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

View File

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