Markdown: Add option for hiding preview

Make it possible to hide the preview, by having a toggle button for
"Show Preview" and "Show Editor", and ensure that at least one is always
on and they are in the order as the views themselves. Disable the "Swap
Views" button if only one is shown.

Change-Id: I0ec1e06c2a8ec94e34bf52eae45ba009fd8cd1b5
Reviewed-by: David Schulz <david.schulz@qt.io>
Reviewed-by: André Hartmann <aha_1980@gmx.de>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Eike Ziller
2023-04-24 13:10:08 +02:00
parent e849c19a33
commit ec9680d42e

View File

@@ -65,46 +65,76 @@ public:
setContext(Core::Context(MARKDOWNVIEWER_ID)); setContext(Core::Context(MARKDOWNVIEWER_ID));
setWidget(&m_widget); setWidget(&m_widget);
auto togglePreviewVisible = new QToolButton;
togglePreviewVisible->setText(Tr::tr("Show Preview"));
togglePreviewVisible->setCheckable(true);
togglePreviewVisible->setChecked(true);
auto toggleEditorVisible = new QToolButton; auto toggleEditorVisible = new QToolButton;
toggleEditorVisible->setText(Tr::tr("Hide Editor")); toggleEditorVisible->setText(Tr::tr("Show Editor"));
toggleEditorVisible->setCheckable(true); toggleEditorVisible->setCheckable(true);
toggleEditorVisible->setChecked(true); toggleEditorVisible->setChecked(true);
auto swapViews = new QToolButton; auto swapViews = new QToolButton;
swapViews->setText(Tr::tr("Swap Views")); swapViews->setText(Tr::tr("Swap Views"));
auto layout = new QHBoxLayout(&m_toolbar); auto toolbarLayout = new QHBoxLayout(&m_toolbar);
layout->setSpacing(0); toolbarLayout->setSpacing(0);
layout->setContentsMargins(0, 0, 0, 0); toolbarLayout->setContentsMargins(0, 0, 0, 0);
layout->addStretch(); toolbarLayout->addStretch();
layout->addWidget(toggleEditorVisible); if (textEditorRight) {
layout->addWidget(swapViews); toolbarLayout->addWidget(togglePreviewVisible);
toolbarLayout->addWidget(toggleEditorVisible);
} else {
toolbarLayout->addWidget(toggleEditorVisible);
toolbarLayout->addWidget(togglePreviewVisible);
}
toolbarLayout->addWidget(swapViews);
connect(m_document.data(), &TextDocument::mimeTypeChanged, connect(m_document.data(), &TextDocument::mimeTypeChanged,
m_document.data(), &TextDocument::changed); m_document.data(), &TextDocument::changed);
const auto viewToggled =
[swapViews](QWidget *view, bool visible, QWidget *otherView, QToolButton *otherButton) {
if (view->isVisible() == visible)
return;
view->setVisible(visible);
if (visible) {
view->setFocus();
} else if (otherView->isVisible()) {
otherView->setFocus();
} else {
// make sure at least one view is visible
otherButton->toggle();
}
swapViews->setEnabled(view->isVisible() && otherView->isVisible());
};
connect(toggleEditorVisible, connect(toggleEditorVisible,
&QToolButton::toggled, &QToolButton::toggled,
m_textEditorWidget, this,
[this, browser, toggleEditorVisible](bool editorVisible) { [this, browser, togglePreviewVisible, viewToggled](bool visible) {
if (m_textEditorWidget->isVisible() == editorVisible) viewToggled(m_textEditorWidget, visible, browser, togglePreviewVisible);
return; });
m_textEditorWidget->setVisible(editorVisible); connect(togglePreviewVisible,
if (editorVisible) &QToolButton::toggled,
m_textEditorWidget->setFocus(); this,
else [this, browser, toggleEditorVisible, viewToggled](bool visible) {
browser->setFocus(); viewToggled(browser, visible, m_textEditorWidget, toggleEditorVisible);
toggleEditorVisible->setText(editorVisible ? Tr::tr("Hide Editor")
: Tr::tr("Show Editor"));
}); });
connect(swapViews, &QToolButton::clicked, m_textEditorWidget, [this] { connect(swapViews, &QToolButton::clicked, m_textEditorWidget, [this, toolbarLayout] {
QTC_ASSERT(m_widget.count() > 1, return); QTC_ASSERT(m_widget.count() > 1, return);
// switch views
auto placeholder = std::make_unique<QWidget>(); auto placeholder = std::make_unique<QWidget>();
auto second = m_widget.replaceWidget(1, placeholder.get()); auto second = m_widget.replaceWidget(1, placeholder.get());
auto first = m_widget.replaceWidget(0, second); auto first = m_widget.replaceWidget(0, second);
m_widget.replaceWidget(1, first); m_widget.replaceWidget(1, first);
// switch buttons
const int rightIndex = toolbarLayout->count() - 2;
QLayoutItem *right = toolbarLayout->takeAt(rightIndex);
toolbarLayout->insertItem(rightIndex - 1, right);
// save settings
Utils::QtcSettings *s = Core::ICore::settings(); Utils::QtcSettings *s = Core::ICore::settings();
s->setValueWithDefault(MARKDOWNVIEWER_TEXTEDITOR_RIGHT, s->setValueWithDefault(MARKDOWNVIEWER_TEXTEDITOR_RIGHT,
!s->value(MARKDOWNVIEWER_TEXTEDITOR_RIGHT, !s->value(MARKDOWNVIEWER_TEXTEDITOR_RIGHT,