From eba42324a4cc87f600f3933e371b27ddc2926c91 Mon Sep 17 00:00:00 2001 From: Felix Ernst Date: Tue, 17 Nov 2020 17:35:23 +0100 Subject: [PATCH 1/3] Allow having the UrlNavigators below the tab bar This commit restores the possibility to have the UrlNavigators below the tab bar. This will happen automatically whenever the UrlNavigator is removed from the toolbar. It is also now again possible to have the toolbar on the side. This option is disabled while the toolbar contains the UrlNavigators. This commit makes no changes to the new default which is having the UrlNavigators in the toolbar but makes sure that upgrading users won't be affected. --- src/dolphinmainwindow.cpp | 29 +++++++++++--- src/dolphinmainwindow.h | 12 ++++++ src/dolphinnavigatorswidgetaction.cpp | 58 +++++++++++++-------------- src/dolphinnavigatorswidgetaction.h | 23 +++++++---- src/dolphintabpage.cpp | 23 +++++++++-- src/dolphintabpage.h | 2 + src/main.cpp | 7 ---- 7 files changed, 103 insertions(+), 51 deletions(-) diff --git a/src/dolphinmainwindow.cpp b/src/dolphinmainwindow.cpp index 0c4c3c881..b53dfa666 100644 --- a/src/dolphinmainwindow.cpp +++ b/src/dolphinmainwindow.cpp @@ -169,11 +169,6 @@ DolphinMainWindow::DolphinMainWindow() : setupGUI(Keys | Save | Create | ToolBar); stateChanged(QStringLiteral("new_file")); - toolBar()->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea); - toolBar()->setFloatable(false); - if (!toolBar()->actions().contains(navigatorsWidgetAction)) { - navigatorsWidgetAction->addToToolbarAndSave(this); - } QClipboard* clipboard = QApplication::clipboard(); connect(clipboard, &QClipboard::dataChanged, this, &DolphinMainWindow::updatePasteAction); @@ -194,6 +189,8 @@ DolphinMainWindow::DolphinMainWindow() : createControlButton(); } + updateAllowedToolbarAreas(); + // enable middle-click on back/forward/up to open in a new tab auto *middleClickEventFilter = new MiddleClickActionEventFilter(this); connect(middleClickEventFilter, &MiddleClickActionEventFilter::actionMiddleClicked, this, &DolphinMainWindow::slotToolBarActionMiddleClicked); @@ -625,6 +622,17 @@ void DolphinMainWindow::closeEvent(QCloseEvent* event) KXmlGuiWindow::closeEvent(event); } +void DolphinMainWindow::saveNewToolbarConfig() +{ + KXmlGuiWindow::saveNewToolbarConfig(); + updateAllowedToolbarAreas(); + auto navigators = static_cast + (actionCollection()->action(QStringLiteral("url_navigators"))); + if (!toolBar()->actions().contains(navigators)) { + m_tabWidget->currentTabPage()->insertNavigatorsWidget(navigators); + } +} + void DolphinMainWindow::saveProperties(KConfigGroup& group) { m_tabWidget->saveProperties(group); @@ -2217,6 +2225,17 @@ void DolphinMainWindow::updateSplitAction() } } +void DolphinMainWindow::updateAllowedToolbarAreas() +{ + auto navigators = static_cast + (actionCollection()->action(QStringLiteral("url_navigators"))); + if (toolBar()->actions().contains(navigators)) { + toolBar()->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea); + } else { + toolBar()->setAllowedAreas(Qt::AllToolBarAreas); + } +} + bool DolphinMainWindow::isKompareInstalled() const { static bool initialized = false; diff --git a/src/dolphinmainwindow.h b/src/dolphinmainwindow.h index 29ab6326d..aef8d9e59 100644 --- a/src/dolphinmainwindow.h +++ b/src/dolphinmainwindow.h @@ -210,6 +210,13 @@ protected: /** @see QMainWindow::closeEvent() */ void closeEvent(QCloseEvent* event) override; + /** + * Calls the base method KXmlGuiWindow::saveNewToolbarConfig(). + * Is also used to set toolbar constraints and UrlNavigator position + * based on the newly changed toolbar configuration. + */ + void saveNewToolbarConfig() override; + /** @see KMainWindow::saveProperties() */ void saveProperties(KConfigGroup& group) override; @@ -593,6 +600,11 @@ private: */ void updateSplitAction(); + /** + * Sets the window sides the toolbar may be moved to based on toolbar contents. + */ + void updateAllowedToolbarAreas(); + bool isKompareInstalled() const; /** diff --git a/src/dolphinnavigatorswidgetaction.cpp b/src/dolphinnavigatorswidgetaction.cpp index e0ae3132a..a2370d25a 100644 --- a/src/dolphinnavigatorswidgetaction.cpp +++ b/src/dolphinnavigatorswidgetaction.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include @@ -35,7 +36,6 @@ DolphinNavigatorsWidgetAction::DolphinNavigatorsWidgetAction(QWidget *parent) : setIcon(QIcon::fromTheme(QStringLiteral("dialog-scripts"))); m_splitter->setChildrenCollapsible(false); - setDefaultWidget(m_splitter.get()); m_splitter->addWidget(createNavigatorWidget(Primary)); @@ -45,34 +45,6 @@ DolphinNavigatorsWidgetAction::DolphinNavigatorsWidgetAction(QWidget *parent) : this, &DolphinNavigatorsWidgetAction::adjustSpacing); } -bool DolphinNavigatorsWidgetAction::addToToolbarAndSave(KXmlGuiWindow *mainWindow) -{ - const QString rawXml = KXMLGUIFactory::readConfigFile(mainWindow->xmlFile()); - QDomDocument domDocument; - if (rawXml.isEmpty() || !domDocument.setContent(rawXml) || domDocument.isNull()) { - return false; - } - QDomNode toolbar = domDocument.elementsByTagName(QStringLiteral("ToolBar")).at(0); - if (toolbar.isNull()) { - return false; - } - - QDomElement urlNavigatorElement = domDocument.createElement(QStringLiteral("Action")); - urlNavigatorElement.setAttribute(QStringLiteral("name"), QStringLiteral("url_navigators")); - - QDomNode position = toolbar.firstChildElement(QStringLiteral("Spacer")); - if (position.isNull()) { - toolbar.appendChild(urlNavigatorElement); - } else { - toolbar.replaceChild(urlNavigatorElement, position); - } - - KXMLGUIFactory::saveConfigFile(domDocument, mainWindow->xmlFile()); - mainWindow->reloadXML(); - mainWindow->createGUI(); - return true; -} - void DolphinNavigatorsWidgetAction::createSecondaryUrlNavigator() { Q_ASSERT(m_splitter->count() == 1); @@ -109,6 +81,12 @@ void DolphinNavigatorsWidgetAction::followViewContainersGeometry( adjustSpacing(); } +bool DolphinNavigatorsWidgetAction::isInToolbar() const +{ + return qobject_cast(m_splitter->parentWidget()); +} + + DolphinUrlNavigator* DolphinNavigatorsWidgetAction::primaryUrlNavigator() const { Q_ASSERT(m_splitter); @@ -137,6 +115,28 @@ void DolphinNavigatorsWidgetAction::setSecondaryNavigatorVisible(bool visible) updateText(); } +QWidget *DolphinNavigatorsWidgetAction::createWidget(QWidget *parent) +{ + QWidget *oldParent = m_splitter->parentWidget(); + if (oldParent && oldParent->layout()) { + oldParent->layout()->removeWidget(m_splitter.get()); + QGridLayout *layout = qobject_cast(oldParent->layout()); + if (qobject_cast(parent) && layout) { + // in DolphinTabPage::insertNavigatorsWidget the minimumHeight of this row was + // set to fit the m_splitter. Since we are now removing it again, the + // minimumHeight can be reset to 0. + layout->setRowMinimumHeight(0, 0); + } + } + m_splitter->setParent(parent); + return m_splitter.get(); +} + +void DolphinNavigatorsWidgetAction::deleteWidget(QWidget *widget) +{ + m_splitter->setParent(nullptr); +} + void DolphinNavigatorsWidgetAction::adjustSpacing() { Q_ASSERT(m_globalXOfSplitter != INT_MIN); diff --git a/src/dolphinnavigatorswidgetaction.h b/src/dolphinnavigatorswidgetaction.h index f343e6a1c..bbd8cf127 100644 --- a/src/dolphinnavigatorswidgetaction.h +++ b/src/dolphinnavigatorswidgetaction.h @@ -41,13 +41,6 @@ class DolphinNavigatorsWidgetAction : public QWidgetAction public: DolphinNavigatorsWidgetAction(QWidget *parent = nullptr); - /** - * Adds this action to the mainWindow's toolbar and saves the change - * in the users ui configuration file. - * @return true if successful. Otherwise false. - */ - bool addToToolbarAndSave(KXmlGuiWindow *mainWindow); - /** * The secondary UrlNavigator is only created on-demand. Such an action is not necessary * for the primary UrlNavigator which is created preemptively. @@ -71,6 +64,8 @@ public: void followViewContainersGeometry(int globalXOfPrimary, int widthOfPrimary, int globalXOfSecondary, int widthOfSecondary); + bool isInToolbar() const; + /** * @return the primary UrlNavigator. */ @@ -87,6 +82,20 @@ public: */ void setSecondaryNavigatorVisible(bool visible); +protected: + /** + * There should always ever be one navigatorsWidget for this action so + * this method always returns the same widget and reparents it. + * You normally don't have to use this method directly because + * QWidgetAction::requestWidget() is used to obtain the navigatorsWidget + * and to steal it from whereever it was prior. + * @param parent the new parent of the navigatorsWidget. + */ + QWidget *createWidget(QWidget *parent) override; + + /** @see QWidgetAction::deleteWidget() */ + void deleteWidget(QWidget *widget) override; + private: /** * Adjusts the width of the spacings used to align the UrlNavigators with ViewContainers. diff --git a/src/dolphintabpage.cpp b/src/dolphintabpage.cpp index 7f945dce2..138822cfd 100644 --- a/src/dolphintabpage.cpp +++ b/src/dolphintabpage.cpp @@ -10,7 +10,8 @@ #include "dolphinviewcontainer.h" #include -#include +#include +#include DolphinTabPage::DolphinTabPage(const QUrl &primaryUrl, const QUrl &secondaryUrl, QWidget* parent) : QWidget(parent), @@ -18,7 +19,7 @@ DolphinTabPage::DolphinTabPage(const QUrl &primaryUrl, const QUrl &secondaryUrl, m_splitViewEnabled(false), m_active(true) { - QVBoxLayout* layout = new QVBoxLayout(this); + QGridLayout *layout = new QGridLayout(this); layout->setSpacing(0); layout->setContentsMargins(0, 0, 0, 0); @@ -26,7 +27,8 @@ DolphinTabPage::DolphinTabPage(const QUrl &primaryUrl, const QUrl &secondaryUrl, m_splitter->setChildrenCollapsible(false); connect(m_splitter, &QSplitter::splitterMoved, this, &DolphinTabPage::splitterMoved); - layout->addWidget(m_splitter); + layout->addWidget(m_splitter, 1, 0); + layout->setRowStretch(1, 1); // Create a new primary view m_primaryViewContainer = createViewContainer(primaryUrl); @@ -157,6 +159,7 @@ int DolphinTabPage::selectedItemsCount() const void DolphinTabPage::connectNavigators(DolphinNavigatorsWidgetAction *navigatorsWidget) { + insertNavigatorsWidget(navigatorsWidget); m_navigatorsWidget = navigatorsWidget; auto primaryNavigator = navigatorsWidget->primaryUrlNavigator(); m_primaryViewContainer->connectUrlNavigator(primaryNavigator); @@ -185,6 +188,20 @@ bool DolphinTabPage::eventFilter(QObject *watched, QEvent *event) return QWidget::eventFilter(watched, event); } +void DolphinTabPage::insertNavigatorsWidget(DolphinNavigatorsWidgetAction* navigatorsWidget) +{ + QGridLayout *gridLayout = static_cast(layout()); + if (navigatorsWidget->isInToolbar()) { + gridLayout->setRowMinimumHeight(0, 0); + } else { + // We set a row minimum height, so the height does not visibly change whenever + // navigatorsWidget is inserted which happens every time the current tab is changed. + gridLayout->setRowMinimumHeight(0, navigatorsWidget->primaryUrlNavigator()->height()); + gridLayout->addWidget(navigatorsWidget->requestWidget(this), 0, 0); + } +} + + void DolphinTabPage::resizeNavigators() const { if (!m_splitViewEnabled) { diff --git a/src/dolphintabpage.h b/src/dolphintabpage.h index b874d128f..63a246328 100644 --- a/src/dolphintabpage.h +++ b/src/dolphintabpage.h @@ -85,6 +85,8 @@ public: */ bool eventFilter(QObject *watched, QEvent *event) override; + void insertNavigatorsWidget(DolphinNavigatorsWidgetAction *navigatorsWidget); + /** * Notify the connected DolphinNavigatorsWidgetAction of geometry changes which it * needs for visual alignment. diff --git a/src/main.cpp b/src/main.cpp index a4b1b1963..ef2905d77 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,7 +21,6 @@ #include #include #include -#include #include #include @@ -214,12 +213,6 @@ extern "C" Q_DECL_EXPORT int kdemain(int argc, char **argv) } } } - Qt::ToolBarArea area = mainWindow->toolBarArea(mainWindow->toolBar()); - if (area != Qt::TopToolBarArea && area != Qt::BottomToolBarArea) { - // Migrate users with disabled tool bar positions. - // Remove this a few years from now (2020). - mainWindow->addToolBar(Qt::TopToolBarArea, mainWindow->toolBar()); - } #ifdef HAVE_KUSERFEEDBACK auto feedbackProvider = DolphinFeedbackProvider::instance(); -- GitLab From 51592372c2b736e69efedf46b1e3f8ebc7158f69 Mon Sep 17 00:00:00 2001 From: Felix Ernst Date: Wed, 18 Nov 2020 11:24:46 +0100 Subject: [PATCH 2/3] Adress review comments --- src/dolphinmainwindow.cpp | 26 +++++++++++++++----------- src/dolphinmainwindow.h | 15 ++++++++------- src/dolphinnavigatorswidgetaction.cpp | 2 +- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/dolphinmainwindow.cpp b/src/dolphinmainwindow.cpp index b53dfa666..24ab17c2a 100644 --- a/src/dolphinmainwindow.cpp +++ b/src/dolphinmainwindow.cpp @@ -622,17 +622,6 @@ void DolphinMainWindow::closeEvent(QCloseEvent* event) KXmlGuiWindow::closeEvent(event); } -void DolphinMainWindow::saveNewToolbarConfig() -{ - KXmlGuiWindow::saveNewToolbarConfig(); - updateAllowedToolbarAreas(); - auto navigators = static_cast - (actionCollection()->action(QStringLiteral("url_navigators"))); - if (!toolBar()->actions().contains(navigators)) { - m_tabWidget->currentTabPage()->insertNavigatorsWidget(navigators); - } -} - void DolphinMainWindow::saveProperties(KConfigGroup& group) { m_tabWidget->saveProperties(group); @@ -2231,6 +2220,10 @@ void DolphinMainWindow::updateAllowedToolbarAreas() (actionCollection()->action(QStringLiteral("url_navigators"))); if (toolBar()->actions().contains(navigators)) { toolBar()->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea); + if (toolBarArea(toolBar()) == Qt::LeftToolBarArea || + toolBarArea(toolBar()) == Qt::RightToolBarArea) { + addToolBar(Qt::TopToolBarArea, toolBar()); + } } else { toolBar()->setAllowedAreas(Qt::AllToolBarAreas); } @@ -2443,6 +2436,17 @@ bool DolphinMainWindow::eventFilter(QObject* obj, QEvent* event) return false; } +void DolphinMainWindow::saveNewToolbarConfig() +{ + auto navigators = static_cast + (actionCollection()->action(QStringLiteral("url_navigators"))); + if (!toolBar()->actions().contains(navigators)) { + m_tabWidget->currentTabPage()->insertNavigatorsWidget(navigators); + } + KXmlGuiWindow::saveNewToolbarConfig(); + updateAllowedToolbarAreas(); +} + void DolphinMainWindow::focusTerminalPanel() { if (m_terminalPanel->isVisible()) { diff --git a/src/dolphinmainwindow.h b/src/dolphinmainwindow.h index aef8d9e59..8d5eae344 100644 --- a/src/dolphinmainwindow.h +++ b/src/dolphinmainwindow.h @@ -210,13 +210,6 @@ protected: /** @see QMainWindow::closeEvent() */ void closeEvent(QCloseEvent* event) override; - /** - * Calls the base method KXmlGuiWindow::saveNewToolbarConfig(). - * Is also used to set toolbar constraints and UrlNavigator position - * based on the newly changed toolbar configuration. - */ - void saveNewToolbarConfig() override; - /** @see KMainWindow::saveProperties() */ void saveProperties(KConfigGroup& group) override; @@ -228,6 +221,14 @@ protected: /** Handles QWhatsThisClickedEvent and passes all others on. */ bool eventFilter(QObject*, QEvent*) override; +protected slots: + /** + * Calls the base method KXmlGuiWindow::saveNewToolbarConfig(). + * Is also used to set toolbar constraints and UrlNavigator position + * based on the newly changed toolbar configuration. + */ + void saveNewToolbarConfig() override; + private slots: /** * Refreshes the views of the main window by recreating them according to diff --git a/src/dolphinnavigatorswidgetaction.cpp b/src/dolphinnavigatorswidgetaction.cpp index a2370d25a..b8c77c69b 100644 --- a/src/dolphinnavigatorswidgetaction.cpp +++ b/src/dolphinnavigatorswidgetaction.cpp @@ -86,7 +86,6 @@ bool DolphinNavigatorsWidgetAction::isInToolbar() const return qobject_cast(m_splitter->parentWidget()); } - DolphinUrlNavigator* DolphinNavigatorsWidgetAction::primaryUrlNavigator() const { Q_ASSERT(m_splitter); @@ -134,6 +133,7 @@ QWidget *DolphinNavigatorsWidgetAction::createWidget(QWidget *parent) void DolphinNavigatorsWidgetAction::deleteWidget(QWidget *widget) { + Q_UNUSED(widget) m_splitter->setParent(nullptr); } -- GitLab From d09ee4801c52886f6cdef74b4ee7689de55bab60 Mon Sep 17 00:00:00 2001 From: Felix Ernst Date: Thu, 19 Nov 2020 11:14:53 +0100 Subject: [PATCH 3/3] Change call order and add a comment why --- src/dolphinmainwindow.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/dolphinmainwindow.cpp b/src/dolphinmainwindow.cpp index 24ab17c2a..b72f2eb90 100644 --- a/src/dolphinmainwindow.cpp +++ b/src/dolphinmainwindow.cpp @@ -2438,12 +2438,14 @@ bool DolphinMainWindow::eventFilter(QObject* obj, QEvent* event) void DolphinMainWindow::saveNewToolbarConfig() { + KXmlGuiWindow::saveNewToolbarConfig(); // Applies the new config. This has to be called first + // because the rest of this method decides things + // based on the new config. auto navigators = static_cast (actionCollection()->action(QStringLiteral("url_navigators"))); if (!toolBar()->actions().contains(navigators)) { m_tabWidget->currentTabPage()->insertNavigatorsWidget(navigators); } - KXmlGuiWindow::saveNewToolbarConfig(); updateAllowedToolbarAreas(); } -- GitLab