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 <axel.spoerl@qt.io>
This commit is contained in:
Shawn Rutledge 2022-10-27 08:46:48 +02:00
parent a0a29695ab
commit 83b592346b
5 changed files with 36 additions and 7 deletions

View File

@ -11,6 +11,7 @@
#include "qtexttable.h"
#include "qtextlist.h"
#include <qdebug.h>
#include <qloggingcategory.h>
#if QT_CONFIG(regularexpression)
#include <qregularexpression.h>
#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);

View File

@ -74,6 +74,12 @@ bool MainWindow::loadFile(const QString &fileName)
return succeeded;
}
void MainWindow::newSubWindow()
{
MdiChild *child = createMdiChild(static_cast<MdiChild *>(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,

View File

@ -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;

View File

@ -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()

View File

@ -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