From 83b592346ba98b3fba76cfac0b8662430b59e46b Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Thu, 27 Oct 2022 08:46:48 +0200 Subject: [PATCH] Modify the MDI manual test to allow multiple views of same document - editing same document in different views is possible? yes - resizing one QTextEdit calls QTextDocument::setTextWidth(), which affects the width in the other view too. You can resize either one, but you can't have two views with different layouts, of course. Added a categorized log message in QTextDocument::setTextWidth() for a sanity check. Task-nunber: QTBUG-35688 Change-Id: I59c4d10143fda3a66b946237832274d67f9d9d45 Reviewed-by: Axel Spoerl --- src/gui/text/qtextdocument.cpp | 5 +++++ .../widgets/mainwindows/mdi/mainwindow.cpp | 17 +++++++++++++++-- .../widgets/mainwindows/mdi/mainwindow.h | 5 ++++- .../widgets/mainwindows/mdi/mdichild.cpp | 12 ++++++++++-- .../examples/widgets/mainwindows/mdi/mdichild.h | 4 ++-- 5 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/gui/text/qtextdocument.cpp b/src/gui/text/qtextdocument.cpp index e5b271ee3c8..ea0cc27f590 100644 --- a/src/gui/text/qtextdocument.cpp +++ b/src/gui/text/qtextdocument.cpp @@ -11,6 +11,7 @@ #include "qtexttable.h" #include "qtextlist.h" #include +#include #if QT_CONFIG(regularexpression) #include #endif @@ -42,6 +43,8 @@ QT_BEGIN_NAMESPACE +Q_DECLARE_LOGGING_CATEGORY(lcLayout); + using namespace Qt::StringLiterals; Q_CORE_EXPORT Q_DECL_CONST_FUNCTION unsigned int qt_int_sqrt(unsigned int n); @@ -727,6 +730,8 @@ void QTextDocument::setTextWidth(qreal width) { Q_D(QTextDocument); QSizeF sz = d->pageSize; + + qCDebug(lcLayout) << "page size" << sz << "-> width" << width; sz.setWidth(width); sz.setHeight(-1); setPageSize(sz); diff --git a/tests/manual/examples/widgets/mainwindows/mdi/mainwindow.cpp b/tests/manual/examples/widgets/mainwindows/mdi/mainwindow.cpp index 89998f31603..c0f90910022 100644 --- a/tests/manual/examples/widgets/mainwindows/mdi/mainwindow.cpp +++ b/tests/manual/examples/widgets/mainwindows/mdi/mainwindow.cpp @@ -74,6 +74,12 @@ bool MainWindow::loadFile(const QString &fileName) return succeeded; } +void MainWindow::newSubWindow() +{ + MdiChild *child = createMdiChild(static_cast(mdiArea->activeSubWindow()->widget())); + child->show(); +} + static inline QString recentFilesKey() { return QStringLiteral("recentFileList"); } static inline QString fileKey() { return QStringLiteral("file"); } @@ -201,6 +207,7 @@ void MainWindow::updateMenus() #ifndef QT_NO_CLIPBOARD pasteAct->setEnabled(hasMdiChild); #endif + newWindowAct->setEnabled(hasMdiChild); closeAct->setEnabled(hasMdiChild); closeAllAct->setEnabled(hasMdiChild); tileAct->setEnabled(hasMdiChild); @@ -220,6 +227,7 @@ void MainWindow::updateMenus() void MainWindow::updateWindowMenu() { windowMenu->clear(); + windowMenu->addAction(newWindowAct); windowMenu->addAction(closeAct); windowMenu->addAction(closeAllAct); windowMenu->addSeparator(); @@ -253,9 +261,9 @@ void MainWindow::updateWindowMenu() } } -MdiChild *MainWindow::createMdiChild() +MdiChild *MainWindow::createMdiChild(MdiChild *other) { - MdiChild *child = new MdiChild; + MdiChild *child = new MdiChild(other); mdiArea->addSubWindow(child); #ifndef QT_NO_CLIPBOARD @@ -365,6 +373,11 @@ void MainWindow::createActions() windowMenu = menuBar()->addMenu(tr("&Window")); connect(windowMenu, &QMenu::aboutToShow, this, &MainWindow::updateWindowMenu); + newWindowAct = new QAction(tr("&New view"), this); + newWindowAct->setStatusTip(tr("Make another window viewing the same document")); + connect(newWindowAct, &QAction::triggered, + this, &MainWindow::newSubWindow); + closeAct = new QAction(tr("Cl&ose"), this); closeAct->setStatusTip(tr("Close the active window")); connect(closeAct, &QAction::triggered, diff --git a/tests/manual/examples/widgets/mainwindows/mdi/mainwindow.h b/tests/manual/examples/widgets/mainwindows/mdi/mainwindow.h index 8fec975c6ea..c8b720e2160 100644 --- a/tests/manual/examples/widgets/mainwindows/mdi/mainwindow.h +++ b/tests/manual/examples/widgets/mainwindows/mdi/mainwindow.h @@ -12,6 +12,7 @@ class QAction; class QMenu; class QMdiArea; class QMdiSubWindow; +class QTextDocument; QT_END_NAMESPACE class MainWindow : public QMainWindow @@ -41,8 +42,9 @@ private slots: void about(); void updateMenus(); void updateWindowMenu(); - MdiChild *createMdiChild(); + MdiChild *createMdiChild(MdiChild *other = nullptr); void switchLayoutDirection(); + void newSubWindow(); private: enum { MaxRecentFiles = 5 }; @@ -72,6 +74,7 @@ private: QAction *copyAct; QAction *pasteAct; #endif + QAction *newWindowAct; QAction *closeAct; QAction *closeAllAct; QAction *tileAct; diff --git a/tests/manual/examples/widgets/mainwindows/mdi/mdichild.cpp b/tests/manual/examples/widgets/mainwindows/mdi/mdichild.cpp index 8f96ec623dd..b922826ae3e 100644 --- a/tests/manual/examples/widgets/mainwindows/mdi/mdichild.cpp +++ b/tests/manual/examples/widgets/mainwindows/mdi/mdichild.cpp @@ -5,10 +5,18 @@ #include "mdichild.h" -MdiChild::MdiChild() +MdiChild::MdiChild(MdiChild *other) { setAttribute(Qt::WA_DeleteOnClose); - isUntitled = true; + if (other) { + auto *doc = other->document(); + setDocument(doc); + setWindowModified(other->isWindowModified()); + connect(doc, &QTextDocument::contentsChanged, + this, &MdiChild::documentWasModified); + curFile = other->curFile; + setWindowTitle(userFriendlyCurrentFile() + "[*]"); + } } void MdiChild::newFile() diff --git a/tests/manual/examples/widgets/mainwindows/mdi/mdichild.h b/tests/manual/examples/widgets/mainwindows/mdi/mdichild.h index ba9c3d8f1ca..813a8455948 100644 --- a/tests/manual/examples/widgets/mainwindows/mdi/mdichild.h +++ b/tests/manual/examples/widgets/mainwindows/mdi/mdichild.h @@ -11,7 +11,7 @@ class MdiChild : public QTextEdit Q_OBJECT public: - MdiChild(); + MdiChild(MdiChild *other = nullptr); void newFile(); bool loadFile(const QString &fileName); @@ -33,7 +33,7 @@ private: QString strippedName(const QString &fullFileName); QString curFile; - bool isUntitled; + bool isUntitled = true; }; #endif