Commit 6151a7ae authored by Felix Ernst's avatar Felix Ernst Committed by Elvis Angelaccio
Browse files

Add the UrlNavigator to the toolbar automatically if needed

This commit adds the DolphinUrlNavigatorWidgetAction::addToToolbarAndSave()
method which changes the users toolbar configuration to contain an
Url Navigator. This way the user doesn't need to do anything manually.

Aside from that a bunch of minor fixes like renaming and reordering
parent b6fc58c3
......@@ -358,7 +358,7 @@ void DolphinMainWindow::slotSelectionChanged(const KFileItemList& selection)
void DolphinMainWindow::updateHistory()
{
const KUrlNavigator *urlNavigator = m_activeViewContainer->urlNavigatorInternal();
const KUrlNavigator *urlNavigator = m_activeViewContainer->urlNavigatorInternalWithHistory();
const int index = urlNavigator->historyIndex();
QAction* backAction = actionCollection()->action(KStandardAction::name(KStandardAction::Back));
......@@ -729,7 +729,7 @@ void DolphinMainWindow::slotToolBarActionMiddleClicked(QAction *action)
void DolphinMainWindow::slotAboutToShowBackPopupMenu()
{
const KUrlNavigator *urlNavigator = m_activeViewContainer->urlNavigatorInternal();
const KUrlNavigator *urlNavigator = m_activeViewContainer->urlNavigatorInternalWithHistory();
int entries = 0;
m_backAction->menu()->clear();
for (int i = urlNavigator->historyIndex() + 1; i < urlNavigator->historySize() && entries < MaxNumberOfNavigationentries; ++i, ++entries) {
......@@ -742,7 +742,7 @@ void DolphinMainWindow::slotAboutToShowBackPopupMenu()
void DolphinMainWindow::slotGoBack(QAction* action)
{
int gotoIndex = action->data().value<int>();
const KUrlNavigator *urlNavigator = m_activeViewContainer->urlNavigatorInternal();
const KUrlNavigator *urlNavigator = m_activeViewContainer->urlNavigatorInternalWithHistory();
for (int i = gotoIndex - urlNavigator->historyIndex(); i > 0; --i) {
goBack();
}
......@@ -751,14 +751,14 @@ void DolphinMainWindow::slotGoBack(QAction* action)
void DolphinMainWindow::slotBackForwardActionMiddleClicked(QAction* action)
{
if (action) {
const KUrlNavigator *urlNavigator = activeViewContainer()->urlNavigatorInternal();
const KUrlNavigator *urlNavigator = activeViewContainer()->urlNavigatorInternalWithHistory();
openNewTabAfterCurrentTab(urlNavigator->locationUrl(action->data().value<int>()));
}
}
void DolphinMainWindow::slotAboutToShowForwardPopupMenu()
{
const KUrlNavigator *urlNavigator = m_activeViewContainer->urlNavigatorInternal();
const KUrlNavigator *urlNavigator = m_activeViewContainer->urlNavigatorInternalWithHistory();
int entries = 0;
m_forwardAction->menu()->clear();
for (int i = urlNavigator->historyIndex() - 1; i >= 0 && entries < MaxNumberOfNavigationentries; --i, ++entries) {
......@@ -771,7 +771,7 @@ void DolphinMainWindow::slotAboutToShowForwardPopupMenu()
void DolphinMainWindow::slotGoForward(QAction* action)
{
int gotoIndex = action->data().value<int>();
const KUrlNavigator *urlNavigator = m_activeViewContainer->urlNavigatorInternal();
const KUrlNavigator *urlNavigator = m_activeViewContainer->urlNavigatorInternalWithHistory();
for (int i = urlNavigator->historyIndex() - gotoIndex; i > 0; --i) {
goForward();
}
......@@ -858,19 +858,21 @@ void DolphinMainWindow::toggleLocationInToolbar()
const int selectionStart = lineEdit->selectionStart();
const int selectionLength = lineEdit->selectionLength();
// prevent the switching if it would leave the user without a visible UrlNavigator
if (locationInToolbar && !toolBar()->actions().contains(urlNavigatorWidgetAction)) {
QAction *configureToolbars = actionCollection()->action(KStandardAction::name(KStandardAction::ConfigureToolbars));
KMessageWidget *messageWidget = m_activeViewContainer->showMessage(
// There is no UrlNavigator on the toolbar. Try to fix it. Otherwise show an error.
if (!urlNavigatorWidgetAction->addToToolbarAndSave(this)) {
QAction *configureToolbars = actionCollection()->action(KStandardAction::name(KStandardAction::ConfigureToolbars));
KMessageWidget *messageWidget = m_activeViewContainer->showMessage(
xi18nc("@info 2 is the visible text on a button just below the message",
"The location could not be moved onto the toolbar because there is currently "
"no \"%1\" item on the toolbar. Select <interface>%2</interface> and add the "
"\"%1\" item. Then this will work.", urlNavigatorWidgetAction->iconText(),
configureToolbars->iconText()), DolphinViewContainer::Information);
messageWidget->addAction(configureToolbars);
messageWidget->addAction(locationInToolbarAction);
locationInToolbarAction->setChecked(false);
return;
messageWidget->addAction(configureToolbars);
messageWidget->addAction(locationInToolbarAction);
locationInToolbarAction->setChecked(false);
return;
}
}
// do the switching
......@@ -949,7 +951,7 @@ void DolphinMainWindow::slotTerminalPanelVisibilityChanged()
void DolphinMainWindow::goBack()
{
DolphinUrlNavigator *urlNavigator = m_activeViewContainer->urlNavigatorInternal();
DolphinUrlNavigator *urlNavigator = m_activeViewContainer->urlNavigatorInternalWithHistory();
urlNavigator->goBack();
if (urlNavigator->locationState().isEmpty()) {
......@@ -976,14 +978,14 @@ void DolphinMainWindow::goHome()
void DolphinMainWindow::goBackInNewTab()
{
KUrlNavigator* urlNavigator = activeViewContainer()->urlNavigatorInternal();
KUrlNavigator* urlNavigator = activeViewContainer()->urlNavigatorInternalWithHistory();
const int index = urlNavigator->historyIndex() + 1;
openNewTabAfterCurrentTab(urlNavigator->locationUrl(index));
}
void DolphinMainWindow::goForwardInNewTab()
{
KUrlNavigator* urlNavigator = activeViewContainer()->urlNavigatorInternal();
KUrlNavigator* urlNavigator = activeViewContainer()->urlNavigatorInternalWithHistory();
const int index = urlNavigator->historyIndex() - 1;
openNewTabAfterCurrentTab(urlNavigator->locationUrl(index));
}
......@@ -2252,7 +2254,7 @@ void DolphinMainWindow::connectViewSignals(DolphinViewContainer* container)
connect(navigator, &KUrlNavigator::tabRequested,
this, &DolphinMainWindow::openNewTabAfterLastTab);
connect(container->urlNavigatorInternal(), &KUrlNavigator::historyChanged,
connect(container->urlNavigatorInternalWithHistory(), &KUrlNavigator::historyChanged,
this, &DolphinMainWindow::updateHistory);
}
......
......@@ -165,12 +165,12 @@ QByteArray DolphinTabPage::saveState() const
stream << m_splitViewEnabled;
stream << m_primaryViewContainer->url();
stream << m_primaryViewContainer->urlNavigatorInternal()->isUrlEditable();
stream << m_primaryViewContainer->urlNavigatorInternalWithHistory()->isUrlEditable();
m_primaryViewContainer->view()->saveState(stream);
if (m_splitViewEnabled) {
stream << m_secondaryViewContainer->url();
stream << m_secondaryViewContainer->urlNavigatorInternal()->isUrlEditable();
stream << m_secondaryViewContainer->urlNavigatorInternalWithHistory()->isUrlEditable();
m_secondaryViewContainer->view()->saveState(stream);
}
......@@ -206,7 +206,7 @@ void DolphinTabPage::restoreState(const QByteArray& state)
m_primaryViewContainer->setUrl(primaryUrl);
bool primaryUrlEditable;
stream >> primaryUrlEditable;
m_primaryViewContainer->urlNavigatorInternal()->setUrlEditable(primaryUrlEditable);
m_primaryViewContainer->urlNavigatorInternalWithHistory()->setUrlEditable(primaryUrlEditable);
m_primaryViewContainer->view()->restoreState(stream);
if (isSplitViewEnabled) {
......@@ -215,7 +215,7 @@ void DolphinTabPage::restoreState(const QByteArray& state)
m_secondaryViewContainer->setUrl(secondaryUrl);
bool secondaryUrlEditable;
stream >> secondaryUrlEditable;
m_secondaryViewContainer->urlNavigatorInternal()->setUrlEditable(secondaryUrlEditable);
m_secondaryViewContainer->urlNavigatorInternalWithHistory()->setUrlEditable(secondaryUrlEditable);
m_secondaryViewContainer->view()->restoreState(stream);
}
......@@ -250,7 +250,7 @@ void DolphinTabPage::restoreStateV1(const QByteArray& state)
m_primaryViewContainer->setUrl(primaryUrl);
bool primaryUrlEditable;
stream >> primaryUrlEditable;
m_primaryViewContainer->urlNavigatorInternal()->setUrlEditable(primaryUrlEditable);
m_primaryViewContainer->urlNavigatorInternalWithHistory()->setUrlEditable(primaryUrlEditable);
if (isSplitViewEnabled) {
QUrl secondaryUrl;
......@@ -258,7 +258,7 @@ void DolphinTabPage::restoreStateV1(const QByteArray& state)
m_secondaryViewContainer->setUrl(secondaryUrl);
bool secondaryUrlEditable;
stream >> secondaryUrlEditable;
m_secondaryViewContainer->urlNavigatorInternal()->setUrlEditable(secondaryUrlEditable);
m_secondaryViewContainer->urlNavigatorInternalWithHistory()->setUrlEditable(secondaryUrlEditable);
}
stream >> m_primaryViewActive;
......
......@@ -290,12 +290,12 @@ DolphinUrlNavigator* DolphinViewContainer::urlNavigator()
return m_urlNavigatorConnected;
}
const DolphinUrlNavigator *DolphinViewContainer::urlNavigatorInternal() const
const DolphinUrlNavigator *DolphinViewContainer::urlNavigatorInternalWithHistory() const
{
return m_urlNavigator;
}
DolphinUrlNavigator *DolphinViewContainer::urlNavigatorInternal()
DolphinUrlNavigator *DolphinViewContainer::urlNavigatorInternalWithHistory()
{
return m_urlNavigator;
}
......
......@@ -91,8 +91,8 @@ public:
* @see connectUrlNavigator()
* @see disconnectUrlNavigator()
*
* Use urlNavigatorInternal() if you want to access the history.
* @see urlNavigatorInternal()
* Use urlNavigatorInternalWithHistory() if you want to access the history.
* @see urlNavigatorInternalWithHistory()
*/
const DolphinUrlNavigator *urlNavigator() const;
/**
......@@ -101,8 +101,8 @@ public:
* @see connectUrlNavigator()
* @see disconnectUrlNavigator()
*
* Use urlNavigatorInternal() if you want to access the history.
* @see urlNavigatorInternal()
* Use urlNavigatorInternalWithHistory() if you want to access the history.
* @see urlNavigatorInternalWithHistory()
*/
DolphinUrlNavigator *urlNavigator();
......@@ -110,12 +110,12 @@ public:
* @return An UrlNavigator that contains this view's history.
* Use urlNavigator() instead when not accessing the history.
*/
const DolphinUrlNavigator *urlNavigatorInternal() const;
const DolphinUrlNavigator *urlNavigatorInternalWithHistory() const;
/**
* @return An UrlNavigator that contains this view's history.
* Use urlNavigator() instead when not accessing the history.
*/
DolphinUrlNavigator *urlNavigatorInternal();
DolphinUrlNavigator *urlNavigatorInternalWithHistory();
const DolphinView* view() const;
DolphinView* view();
......
......@@ -24,6 +24,11 @@
#include "dolphinviewcontainer.h"
#include <KLocalizedString>
#include <KXMLGUIFactory>
#include <KXmlGuiWindow>
#include <QDomDocument>
#include <QStackedWidget>
DolphinUrlNavigatorWidgetAction::DolphinUrlNavigatorWidgetAction(QWidget *parent) :
QWidgetAction(parent)
......@@ -56,3 +61,31 @@ void DolphinUrlNavigatorWidgetAction::setUrlNavigatorVisible(bool visible)
m_stackedWidget->setCurrentIndex(1); // urlNavigator
}
}
bool DolphinUrlNavigatorWidgetAction::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_navigator"));
QDomNode position = toolbar.lastChildElement(QStringLiteral("Spacer"));
if (position.isNull()) {
toolbar.appendChild(urlNavigatorElement);
} else {
toolbar.replaceChild(urlNavigatorElement, position);
}
KXMLGUIFactory::saveConfigFile(domDocument, mainWindow->xmlFile());
mainWindow->reloadXML();
mainWindow->createGUI();
return true;
}
......@@ -24,7 +24,9 @@
#include "dolphinurlnavigator.h"
#include <QWidgetAction>
#include <QStackedWidget>
class KXmlGuiWindow;
class QStackedWidget;
/**
* @brief QWidgetAction that allows to use a KUrlNavigator in a toolbar.
......@@ -45,11 +47,20 @@ public:
DolphinUrlNavigator *urlNavigator() const;
/**
* Set the QStackedWidget which is the defaultWidget() to either
* Sets the QStackedWidget which is the defaultWidget() to either
* show a KUrlNavigator or an expanding spacer.
*/
void setUrlNavigatorVisible(bool visible);
/**
* Adds this action to the mainWindow's toolbar and saves the change
* in the users ui configuration file.
* @return true if successful. Otherwise false.
* @note This method does multiple things which are discouraged in
* the API documentation.
*/
bool addToToolbarAndSave(KXmlGuiWindow *mainWindow);
private:
QStackedWidget *m_stackedWidget;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment