dolphinnavigatorswidgetaction.cpp 9.55 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
    This file is part of the KDE project
    SPDX-FileCopyrightText: 2020 Felix Ernst <fe.a.ernst@gmail.com>

    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/

#include "dolphinnavigatorswidgetaction.h"

#include "trash/dolphintrash.h"

#include <KLocalizedString>
#include <KXMLGUIFactory>
#include <KXmlGuiWindow>

#include <QDomDocument>
#include <QHBoxLayout>
#include <QPushButton>
#include <QSplitter>

21
22
#include <limits>

23
24
25
26
DolphinNavigatorsWidgetAction::DolphinNavigatorsWidgetAction(QWidget *parent) :
    QWidgetAction{parent},
    m_splitter{new QSplitter(Qt::Horizontal)},
    m_adjustSpacingTimer{new QTimer(this)},
27
28
29
30
31
    m_globalXOfSplitter{INT_MIN},
    m_globalXOfPrimary{INT_MIN},
    m_widthOfPrimary{INT_MIN},
    m_globalXOfSecondary{INT_MIN},
    m_widthOfSecondary{INT_MIN}
32
{
33
    updateText();
Felix Ernst's avatar
Felix Ernst committed
34
    setIcon(QIcon::fromTheme(QStringLiteral("dialog-scripts")));
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

    m_splitter->setChildrenCollapsible(false);
    setDefaultWidget(m_splitter.get());

    m_splitter->addWidget(createNavigatorWidget(Primary));

    m_adjustSpacingTimer->setInterval(100);
    m_adjustSpacingTimer->setSingleShot(true);
    connect(m_adjustSpacingTimer.get(), &QTimer::timeout,
            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"));

Felix Ernst's avatar
Felix Ernst committed
62
    QDomNode position = toolbar.firstChildElement(QStringLiteral("Spacer"));
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    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);
    m_splitter->addWidget(createNavigatorWidget(Secondary));
    Q_ASSERT(m_splitter->count() == 2);
80
    updateText();
81
82
83
84
85
}

void DolphinNavigatorsWidgetAction::followViewContainerGeometry(
                                    int globalXOfPrimary,   int widthOfPrimary)
{
86
    followViewContainersGeometry(globalXOfPrimary, widthOfPrimary, INT_MIN, INT_MIN);
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
}

void DolphinNavigatorsWidgetAction::followViewContainersGeometry(
                                    int globalXOfPrimary,   int widthOfPrimary,
                                    int globalXOfSecondary, int widthOfSecondary)
{
    m_globalXOfSplitter = m_splitter->mapToGlobal(QPoint(0,0)).x();
    m_globalXOfPrimary = globalXOfPrimary;
    m_widthOfPrimary = widthOfPrimary;
    m_globalXOfSecondary = globalXOfSecondary;
    m_widthOfSecondary = widthOfSecondary;
    adjustSpacing();
}

DolphinUrlNavigator* DolphinNavigatorsWidgetAction::primaryUrlNavigator() const
{
    Q_ASSERT(m_splitter);
104
    return m_splitter->widget(0)->findChild<DolphinUrlNavigator *>();
105
106
107
108
109
110
111
112
}

DolphinUrlNavigator* DolphinNavigatorsWidgetAction::secondaryUrlNavigator() const
{
    Q_ASSERT(m_splitter);
    if (m_splitter->count() < 2) {
        return nullptr;
    }
113
    return m_splitter->widget(1)->findChild<DolphinUrlNavigator *>();
114
115
116
117
118
119
120
121
122
123
124
125
}

void DolphinNavigatorsWidgetAction::setSecondaryNavigatorVisible(bool visible)
{
    if (visible) {
        Q_ASSERT(m_splitter->count() == 2);
        m_splitter->widget(1)->setVisible(true);
    } else if (m_splitter->count() > 1) {
        m_splitter->widget(1)->setVisible(false);
        // Fix an unlikely event of wrong trash button visibility.
        emptyTrashButton(Secondary)->setVisible(false);
    }
126
    updateText();
127
128
129
130
}

void DolphinNavigatorsWidgetAction::adjustSpacing()
{
131
132
133
    Q_ASSERT(m_globalXOfSplitter != INT_MIN);
    Q_ASSERT(m_globalXOfPrimary  != INT_MIN);
    Q_ASSERT(m_widthOfPrimary    != INT_MIN);
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
    const int widthOfSplitterPrimary = m_globalXOfPrimary + m_widthOfPrimary - m_globalXOfSplitter;
    const QList<int> splitterSizes = {widthOfSplitterPrimary,
                                      m_splitter->width() - widthOfSplitterPrimary};
    m_splitter->setSizes(splitterSizes);

    // primary side of m_splitter
    int leadingSpacing = m_globalXOfPrimary - m_globalXOfSplitter;
    if (leadingSpacing < 0) {
        leadingSpacing = 0;
    }
    int trailingSpacing = (m_globalXOfSplitter + m_splitter->width())
                          - (m_globalXOfPrimary + m_widthOfPrimary);
    if (trailingSpacing < 0 || emptyTrashButton(Primary)->isVisible()) {
        trailingSpacing = 0;
    }
    const int widthLeftForUrlNavigator = m_splitter->widget(0)->width() - leadingSpacing - trailingSpacing;
    const int widthNeededForUrlNavigator = primaryUrlNavigator()->sizeHint().width() - widthLeftForUrlNavigator;
    if (widthNeededForUrlNavigator > 0) {
        trailingSpacing -= widthNeededForUrlNavigator;
        if (trailingSpacing < 0) {
            leadingSpacing += trailingSpacing;
            trailingSpacing = 0;
        }
        if (leadingSpacing < 0) {
            leadingSpacing = 0;
        }
    }
    spacing(Primary, Leading)->setMinimumWidth(leadingSpacing);
    spacing(Primary, Trailing)->setFixedWidth(trailingSpacing);

    // secondary side of m_splitter
165
166
    if (m_globalXOfSecondary == INT_MIN) {
        Q_ASSERT(m_widthOfSecondary == INT_MIN);
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
        return;
    }
    spacing(Primary, Trailing)->setFixedWidth(0);

    trailingSpacing = (m_globalXOfSplitter + m_splitter->width())
                      - (m_globalXOfSecondary + m_widthOfSecondary);
    if (trailingSpacing < 0 || emptyTrashButton(Secondary)->isVisible()) {
        trailingSpacing = 0;
    } else {
        const int widthLeftForUrlNavigator2 = m_splitter->widget(1)->width() - trailingSpacing;
        const int widthNeededForUrlNavigator2 = secondaryUrlNavigator()->sizeHint().width() - widthLeftForUrlNavigator2;
        if (widthNeededForUrlNavigator2 > 0) {
            trailingSpacing -= widthNeededForUrlNavigator2;
            if (trailingSpacing < 0) {
                trailingSpacing = 0;
            }
        }
    }
    spacing(Secondary, Trailing)->setMinimumWidth(trailingSpacing);
}

QWidget *DolphinNavigatorsWidgetAction::createNavigatorWidget(Side side) const
{
    auto navigatorWidget = new QWidget(m_splitter.get());
    auto layout = new QHBoxLayout{navigatorWidget};
    layout->setSpacing(0);
    layout->setContentsMargins(0, 0, 0, 0);
    if (side == Primary) {
        auto leadingSpacing = new QWidget{navigatorWidget};
        layout->addWidget(leadingSpacing);
    }
    auto urlNavigator = new DolphinUrlNavigator(navigatorWidget);
    layout->addWidget(urlNavigator);

    auto emptyTrashButton = newEmptyTrashButton(urlNavigator, navigatorWidget);
    layout->addWidget(emptyTrashButton);

    connect(urlNavigator, &KUrlNavigator::urlChanged, [this]() {
Felix Ernst's avatar
Felix Ernst committed
205
206
207
208
        // We have to wait for DolphinUrlNavigator::sizeHint() to update which
        // happens a little bit later than when urlChanged is emitted.
        this->m_adjustSpacingTimer->start();
    });
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252

    auto trailingSpacing = new QWidget{navigatorWidget};
    layout->addWidget(trailingSpacing);
    return navigatorWidget;
}

QPushButton * DolphinNavigatorsWidgetAction::emptyTrashButton(DolphinNavigatorsWidgetAction::Side side)
{
    int sideIndex = (side == Primary ? 0 : 1);
    if (side == Primary) {
        return static_cast<QPushButton *>(m_splitter->widget(sideIndex)->layout()->itemAt(2)->widget());
    }
    return static_cast<QPushButton *>(m_splitter->widget(sideIndex)->layout()->itemAt(1)->widget());
}

QPushButton *DolphinNavigatorsWidgetAction::newEmptyTrashButton(const DolphinUrlNavigator *urlNavigator, QWidget *parent) const
{
    auto emptyTrashButton = new QPushButton(QIcon::fromTheme(QStringLiteral("user-trash")),
                                        i18nc("@action:button", "Empty Trash"), parent);
    emptyTrashButton->setFlat(true);
    connect(emptyTrashButton, &QPushButton::clicked,
            this, [parent]() { Trash::empty(parent); });
    connect(&Trash::instance(), &Trash::emptinessChanged,
            emptyTrashButton, &QPushButton::setDisabled);
    emptyTrashButton->hide();
    connect(urlNavigator, &KUrlNavigator::urlChanged, [emptyTrashButton, urlNavigator]() {
        emptyTrashButton->setVisible(urlNavigator->locationUrl().scheme() == QLatin1String("trash"));
    });
    emptyTrashButton->setDisabled(Trash::isEmpty());
    return emptyTrashButton;
}

QWidget *DolphinNavigatorsWidgetAction::spacing(Side side, Position position) const
{
    int sideIndex = (side == Primary ? 0 : 1);
    if (position == Leading) {
        Q_ASSERT(side == Primary); // The secondary side of the splitter has no leading spacing.
        return m_splitter->widget(sideIndex)->layout()->itemAt(0)->widget();
    }
    if (side == Primary) {
        return m_splitter->widget(sideIndex)->layout()->itemAt(3)->widget();
    }
    return m_splitter->widget(sideIndex)->layout()->itemAt(2)->widget();
}
253
254
255
256
257
258
259

void DolphinNavigatorsWidgetAction::updateText()
{
    const int urlNavigatorsAmount = m_splitter->count() > 1 && m_splitter->widget(1)->isVisible() ?
                                    2 : 1;
    setText(i18ncp("@action:inmenu", "Url Navigator", "Url Navigators", urlNavigatorsAmount));
}