2 * Copyright 2020 Felix Ernst <fe.a.ernst@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) version 3, or any
8 * later version accepted by the membership of KDE e.V. (or its
9 * successor approved by the membership of KDE e.V.), which shall
10 * act as a proxy defined in Section 6 of version 3 of the license.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library. If not, see <https://www.gnu.org/licenses/>.
21 #include "dolphinurlnavigatorwidgetaction.h"
23 #include "dolphin_generalsettings.h"
24 #include "dolphinviewcontainer.h"
26 #include <KLocalizedString>
27 #include <KXMLGUIFactory>
28 #include <KXmlGuiWindow>
30 #include <QDomDocument>
31 #include <QStackedWidget>
33 DolphinUrlNavigatorWidgetAction::DolphinUrlNavigatorWidgetAction(QWidget
*parent
) :
36 setText(i18nc("@action:inmenu", "Url navigator"));
38 m_stackedWidget
= new QStackedWidget(parent
);
40 auto expandingSpacer
= new QWidget(m_stackedWidget
);
41 expandingSpacer
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::MinimumExpanding
);
42 m_stackedWidget
->addWidget(expandingSpacer
); // index 0 of QStackedWidget
44 auto urlNavigator
= new DolphinUrlNavigator(m_stackedWidget
);
45 m_stackedWidget
->addWidget(urlNavigator
); // index 1 of QStackedWidget
47 setDefaultWidget(m_stackedWidget
);
48 setUrlNavigatorVisible(GeneralSettings::locationInToolbar());
51 DolphinUrlNavigator
* DolphinUrlNavigatorWidgetAction::urlNavigator() const
53 return static_cast<DolphinUrlNavigator
*>(m_stackedWidget
->widget(1));
56 void DolphinUrlNavigatorWidgetAction::setUrlNavigatorVisible(bool visible
)
59 m_stackedWidget
->setCurrentIndex(0); // expandingSpacer
61 m_stackedWidget
->setCurrentIndex(1); // urlNavigator
65 bool DolphinUrlNavigatorWidgetAction::addToToolbarAndSave(KXmlGuiWindow
*mainWindow
)
67 const QString rawXml
= KXMLGUIFactory::readConfigFile(mainWindow
->xmlFile());
68 QDomDocument domDocument
;
69 if (rawXml
.isEmpty() || !domDocument
.setContent(rawXml
) || domDocument
.isNull()) {
72 QDomNode toolbar
= domDocument
.elementsByTagName(QStringLiteral("ToolBar")).at(0);
73 if (toolbar
.isNull()) {
77 QDomElement urlNavigatorElement
= domDocument
.createElement(QStringLiteral("Action"));
78 urlNavigatorElement
.setAttribute(QStringLiteral("name"), QStringLiteral("url_navigator"));
80 QDomNode position
= toolbar
.lastChildElement(QStringLiteral("Spacer"));
81 if (position
.isNull()) {
82 toolbar
.appendChild(urlNavigatorElement
);
84 toolbar
.replaceChild(urlNavigatorElement
, position
);
87 KXMLGUIFactory::saveConfigFile(domDocument
, mainWindow
->xmlFile());
88 mainWindow
->reloadXML();
89 mainWindow
->createGUI();