]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinurlnavigatorwidgetaction.cpp
108a5de13f10033caf06beec086ea4ce2b6be427
[dolphin.git] / src / views / dolphinurlnavigatorwidgetaction.cpp
1 /*
2 * Copyright 2020 Felix Ernst <fe.a.ernst@gmail.com>
3 *
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.
11 *
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.
16 *
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/>.
19 */
20
21 #include "dolphinurlnavigatorwidgetaction.h"
22
23 #include "dolphin_generalsettings.h"
24 #include "dolphinviewcontainer.h"
25
26 #include <KLocalizedString>
27 #include <KXMLGUIFactory>
28 #include <KXmlGuiWindow>
29
30 #include <QDomDocument>
31 #include <QStackedWidget>
32
33 DolphinUrlNavigatorWidgetAction::DolphinUrlNavigatorWidgetAction(QWidget *parent) :
34 QWidgetAction(parent)
35 {
36 setText(i18nc("@action:inmenu", "Url navigator"));
37
38 m_stackedWidget = new QStackedWidget(parent);
39
40 auto expandingSpacer = new QWidget(m_stackedWidget);
41 expandingSpacer->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
42 m_stackedWidget->addWidget(expandingSpacer); // index 0 of QStackedWidget
43
44 auto urlNavigator = new DolphinUrlNavigator(m_stackedWidget);
45 m_stackedWidget->addWidget(urlNavigator); // index 1 of QStackedWidget
46
47 setDefaultWidget(m_stackedWidget);
48 setUrlNavigatorVisible(GeneralSettings::locationInToolbar());
49 }
50
51 DolphinUrlNavigator* DolphinUrlNavigatorWidgetAction::urlNavigator() const
52 {
53 return static_cast<DolphinUrlNavigator *>(m_stackedWidget->widget(1));
54 }
55
56 void DolphinUrlNavigatorWidgetAction::setUrlNavigatorVisible(bool visible)
57 {
58 if (!visible) {
59 m_stackedWidget->setCurrentIndex(0); // expandingSpacer
60 } else {
61 m_stackedWidget->setCurrentIndex(1); // urlNavigator
62 }
63 }
64
65 bool DolphinUrlNavigatorWidgetAction::addToToolbarAndSave(KXmlGuiWindow *mainWindow)
66 {
67 const QString rawXml = KXMLGUIFactory::readConfigFile(mainWindow->xmlFile());
68 QDomDocument domDocument;
69 if (rawXml.isEmpty() || !domDocument.setContent(rawXml) || domDocument.isNull()) {
70 return false;
71 }
72 QDomNode toolbar = domDocument.elementsByTagName(QStringLiteral("ToolBar")).at(0);
73 if (toolbar.isNull()) {
74 return false;
75 }
76
77 QDomElement urlNavigatorElement = domDocument.createElement(QStringLiteral("Action"));
78 urlNavigatorElement.setAttribute(QStringLiteral("name"), QStringLiteral("url_navigator"));
79
80 QDomNode position = toolbar.lastChildElement(QStringLiteral("Spacer"));
81 if (position.isNull()) {
82 toolbar.appendChild(urlNavigatorElement);
83 } else {
84 toolbar.replaceChild(urlNavigatorElement, position);
85 }
86
87 KXMLGUIFactory::saveConfigFile(domDocument, mainWindow->xmlFile());
88 mainWindow->reloadXML();
89 mainWindow->createGUI();
90 return true;
91 }