dolphinnavigatorswidgetaction.cpp 15.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
/*
    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>
13
14
#include <KNotificationJobUiDelegate>
#include <KService>
15
16
17
#include <KXMLGUIFactory>
#include <KXmlGuiWindow>

18
19
#include <KIO/ApplicationLauncherJob>

20
#include <QApplication>
21
22
23
24
#include <QDomDocument>
#include <QHBoxLayout>
#include <QPushButton>
#include <QSplitter>
25
#include <QToolBar>
26

27
28
#include <limits>

29
30
31
32
DolphinNavigatorsWidgetAction::DolphinNavigatorsWidgetAction(QWidget *parent) :
    QWidgetAction{parent},
    m_splitter{new QSplitter(Qt::Horizontal)},
    m_adjustSpacingTimer{new QTimer(this)},
33
    m_viewGeometriesHelper{m_splitter.get(), this}
34
{
35
    updateText();
Felix Ernst's avatar
Felix Ernst committed
36
    setIcon(QIcon::fromTheme(QStringLiteral("dialog-scripts")));
37
38
39
40
41
42
43
44
45
46
47

    m_splitter->setChildrenCollapsible(false);

    m_splitter->addWidget(createNavigatorWidget(Primary));

    m_adjustSpacingTimer->setInterval(100);
    m_adjustSpacingTimer->setSingleShot(true);
    connect(m_adjustSpacingTimer.get(), &QTimer::timeout,
            this, &DolphinNavigatorsWidgetAction::adjustSpacing);
}

48
49
void DolphinNavigatorsWidgetAction::adjustSpacing()
{
50
    m_previousWindowWidth = parentWidget()->window()->width();
51
52
53
54
55
56
57
58
59
60
61
62
63
    auto viewGeometries = m_viewGeometriesHelper.viewGeometries();
    const int widthOfSplitterPrimary = viewGeometries.globalXOfPrimary + viewGeometries.widthOfPrimary - viewGeometries.globalXOfNavigatorsWidget;
    const QList<int> splitterSizes = {widthOfSplitterPrimary,
                                      m_splitter->width() - widthOfSplitterPrimary};
    m_splitter->setSizes(splitterSizes);

    // primary side of m_splitter
    int leadingSpacing = viewGeometries.globalXOfPrimary - viewGeometries.globalXOfNavigatorsWidget;
    if (leadingSpacing < 0) {
        leadingSpacing = 0;
    }
    int trailingSpacing = (viewGeometries.globalXOfNavigatorsWidget + m_splitter->width())
                          - (viewGeometries.globalXOfPrimary + viewGeometries.widthOfPrimary);
64
65
66
    if (trailingSpacing < 0 || emptyTrashButton(Primary)->isVisible()
                            || networkFolderButton(Primary)->isVisible()
    ) {
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
        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
    if (viewGeometries.globalXOfSecondary == INT_MIN) {
        Q_ASSERT(viewGeometries.widthOfSecondary == INT_MIN);
        return;
    }
    spacing(Primary, Trailing)->setFixedWidth(0);

    trailingSpacing = (viewGeometries.globalXOfNavigatorsWidget + m_splitter->width())
                      - (viewGeometries.globalXOfSecondary + viewGeometries.widthOfSecondary);
93
94
95
    if (trailingSpacing < 0 || emptyTrashButton(Secondary)->isVisible()
                            || networkFolderButton(Secondary)->isVisible()
    ) {
96
97
98
99
100
101
102
103
104
105
106
107
108
109
        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);
}

110
111
112
113
114
void DolphinNavigatorsWidgetAction::createSecondaryUrlNavigator()
{
    Q_ASSERT(m_splitter->count() == 1);
    m_splitter->addWidget(createNavigatorWidget(Secondary));
    Q_ASSERT(m_splitter->count() == 2);
115
    updateText();
116
117
}

118
119
void DolphinNavigatorsWidgetAction::followViewContainersGeometry(QWidget *primaryViewContainer,
                                                                 QWidget *secondaryViewContainer)
120
{
121
    m_viewGeometriesHelper.setViewContainers(primaryViewContainer, secondaryViewContainer);
122
123
124
    adjustSpacing();
}

125
126
127
128
129
bool DolphinNavigatorsWidgetAction::isInToolbar() const
{
    return qobject_cast<QToolBar *>(m_splitter->parentWidget());
}

130
131
132
DolphinUrlNavigator* DolphinNavigatorsWidgetAction::primaryUrlNavigator() const
{
    Q_ASSERT(m_splitter);
133
    return m_splitter->widget(0)->findChild<DolphinUrlNavigator *>();
134
135
136
137
138
139
140
141
}

DolphinUrlNavigator* DolphinNavigatorsWidgetAction::secondaryUrlNavigator() const
{
    Q_ASSERT(m_splitter);
    if (m_splitter->count() < 2) {
        return nullptr;
    }
142
    return m_splitter->widget(1)->findChild<DolphinUrlNavigator *>();
143
144
145
146
147
148
149
150
151
152
153
154
}

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);
    }
155
    updateText();
156
157
}

158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
QWidget *DolphinNavigatorsWidgetAction::createWidget(QWidget *parent)
{
    QWidget *oldParent = m_splitter->parentWidget();
    if (oldParent && oldParent->layout()) {
        oldParent->layout()->removeWidget(m_splitter.get());
        QGridLayout *layout = qobject_cast<QGridLayout *>(oldParent->layout());
        if (qobject_cast<QToolBar *>(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)
{
    Q_UNUSED(widget)
    m_splitter->setParent(nullptr);
}

181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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);

197
198
199
    auto networkFolderButton = newNetworkFolderButton(urlNavigator, navigatorWidget);
    layout->addWidget(networkFolderButton);

200
201
202
203
204
205
206
207
208
209
210
211
212
213
    connect(urlNavigator, &KUrlNavigator::urlChanged, this, [urlNavigator, this]() {
        // Update URL navigator to show a server URL entry placeholder text if we
        // just loaded the remote:/ page, to make it easier for users to figure out
        // that they can enter arbitrary remote URLs. See bug 414670
        if (urlNavigator->locationUrl().scheme() == QLatin1String("remote")) {
            if (!urlNavigator->isUrlEditable()) {
                urlNavigator->setUrlEditable(true);
            }
            urlNavigator->clearText();
            urlNavigator->setPlaceholderText(i18n("Enter server URL (e.g. smb://[ip address])"));
        } else {
            urlNavigator->setPlaceholderText(QString());
        }

Felix Ernst's avatar
Felix Ernst committed
214
215
216
        // We have to wait for DolphinUrlNavigator::sizeHint() to update which
        // happens a little bit later than when urlChanged is emitted.
        this->m_adjustSpacingTimer->start();
217
    }, Qt::QueuedConnection);
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236

    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);
237
238
    emptyTrashButton->setToolTip(QLatin1String("Empties Trash to create free space"));

239
240
241
242
243
244
    emptyTrashButton->setFlat(true);
    connect(emptyTrashButton, &QPushButton::clicked,
            this, [parent]() { Trash::empty(parent); });
    connect(&Trash::instance(), &Trash::emptinessChanged,
            emptyTrashButton, &QPushButton::setDisabled);
    emptyTrashButton->hide();
245
    connect(urlNavigator, &KUrlNavigator::urlChanged, this, [emptyTrashButton, urlNavigator]() {
246
247
248
249
250
251
        emptyTrashButton->setVisible(urlNavigator->locationUrl().scheme() == QLatin1String("trash"));
    });
    emptyTrashButton->setDisabled(Trash::isEmpty());
    return emptyTrashButton;
}

252
253
254
255
256
257
258
259
260
261
262
263
264
265
QPushButton *DolphinNavigatorsWidgetAction::networkFolderButton(DolphinNavigatorsWidgetAction::Side side)
{
    int sideIndex = (side == Primary ? 0 : 1);
    if (side == Primary) {
        return static_cast<QPushButton *>(m_splitter->widget(sideIndex)->layout()->itemAt(3)->widget());
    }
    return static_cast<QPushButton *>(m_splitter->widget(sideIndex)->layout()->itemAt(2)->widget());
}

QPushButton *DolphinNavigatorsWidgetAction::newNetworkFolderButton(const DolphinUrlNavigator *urlNavigator, QWidget *parent) const
{
    auto networkFolderButton = new QPushButton(QIcon::fromTheme(QStringLiteral("folder-add")),
                                        i18nc("@action:button", "Add Network Folder"), parent);
    networkFolderButton->setFlat(true);
266
    KService::Ptr service = KService::serviceByDesktopName(QStringLiteral("org.kde.knetattach"));
267
    connect(networkFolderButton, &QPushButton::clicked,
268
            this, [networkFolderButton, service]() {
269
270
271
272
273
274
275
                auto *job = new KIO::ApplicationLauncherJob(service, networkFolderButton);
                auto *delegate = new KNotificationJobUiDelegate;
                delegate->setAutoErrorHandlingEnabled(true);
                job->setUiDelegate(delegate);
                job->start();
            });
    networkFolderButton->hide();
276
277
    connect(urlNavigator, &KUrlNavigator::urlChanged, this, [networkFolderButton, urlNavigator, service]() {
        networkFolderButton->setVisible(service && urlNavigator->locationUrl().scheme() == QLatin1String("remote"));
278
279
280
281
    });
    return networkFolderButton;
}

282
283
284
285
286
287
288
289
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) {
290
        return m_splitter->widget(sideIndex)->layout()->itemAt(4)->widget();
291
    }
292
    return m_splitter->widget(sideIndex)->layout()->itemAt(3)->widget();
293
}
294
295
296
297
298

void DolphinNavigatorsWidgetAction::updateText()
{
    const int urlNavigatorsAmount = m_splitter->count() > 1 && m_splitter->widget(1)->isVisible() ?
                                    2 : 1;
299
    setText(i18ncp("@action:inmenu", "Location Bar", "Location Bars", urlNavigatorsAmount));
300
}
301
302
303
304
305
306
307
308
309
310
311
312
313

DolphinNavigatorsWidgetAction::ViewGeometriesHelper::ViewGeometriesHelper
    (QWidget *navigatorsWidget, DolphinNavigatorsWidgetAction *navigatorsWidgetAction) :
    m_navigatorsWidget{navigatorsWidget},
    m_navigatorsWidgetAction{navigatorsWidgetAction}
{
    Q_CHECK_PTR(navigatorsWidget);
    Q_CHECK_PTR(navigatorsWidgetAction);
}

bool DolphinNavigatorsWidgetAction::ViewGeometriesHelper::eventFilter(QObject *watched, QEvent *event)
{
    if (event->type() == QEvent::Resize) {
314
315
316
317
318
319
320
321
322
323
324
        if (m_navigatorsWidgetAction->parentWidget()->window()->width() != m_navigatorsWidgetAction->m_previousWindowWidth) {
            // The window is being resized which means not all widgets have gotten their new sizes yet.
            // Let's wait a bit so the sizes of the navigatorsWidget and the viewContainers have all
            // had a chance to be updated.
            m_navigatorsWidgetAction->m_adjustSpacingTimer->start();
        } else {
            m_navigatorsWidgetAction->adjustSpacing();
            // We could always use the m_adjustSpacingTimer instead of calling adjustSpacing() directly
            // here but then the navigatorsWidget doesn't fluently align with the viewContainers when
            // the DolphinTabPage::m_expandViewAnimation is animating.
        }
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
        return false;
    }
    return QObject::eventFilter(watched, event);
}

void DolphinNavigatorsWidgetAction::ViewGeometriesHelper::setViewContainers(QWidget *primaryViewContainer,
                                                                            QWidget *secondaryViewContainer)
{
    Q_CHECK_PTR(primaryViewContainer);
    if (m_primaryViewContainer) {
        m_primaryViewContainer->removeEventFilter(this);
    }
    primaryViewContainer->installEventFilter(this);
    m_primaryViewContainer = primaryViewContainer;

    // It is not possible to resize the secondaryViewContainer without simultaneously
    // resizing the primaryViewContainer so we don't have to installEventFilter() here.
    m_secondaryViewContainer = secondaryViewContainer;
}

DolphinNavigatorsWidgetAction::ViewGeometriesHelper::Geometries
        DolphinNavigatorsWidgetAction::ViewGeometriesHelper::viewGeometries()
{
    Q_ASSERT(m_primaryViewContainer);
    Geometries geometries;

    // width
    geometries.widthOfPrimary = m_primaryViewContainer->width();
    if (m_secondaryViewContainer) {
        geometries.widthOfSecondary = m_secondaryViewContainer->width();
    } else {
        geometries.widthOfSecondary = INT_MIN;
    }

    // globalX
    if (QApplication::layoutDirection() == Qt::LeftToRight) {
        geometries.globalXOfNavigatorsWidget = m_navigatorsWidget->mapToGlobal(QPoint(0,0)).x();
        geometries.globalXOfPrimary = m_primaryViewContainer->mapToGlobal(QPoint(0,0)).x();
        geometries.globalXOfSecondary = !m_secondaryViewContainer ? INT_MIN :
                m_secondaryViewContainer->mapToGlobal(QPoint(0,0)).x();
    } else {
        // When the direction is reversed, globalX does not change.
        // For the adjustSpacing() code to work we need globalX to measure from right to left
        // and to measure up to the rightmost point of a widget instead of the leftmost.
        geometries.globalXOfNavigatorsWidget =
                (-1) * (m_navigatorsWidget->mapToGlobal(QPoint(0,0)).x() + m_navigatorsWidget->width());
        geometries.globalXOfPrimary =
                (-1) * (m_primaryViewContainer->mapToGlobal(QPoint(0,0)).x() + geometries.widthOfPrimary);
        geometries.globalXOfSecondary = !m_secondaryViewContainer ? INT_MIN :
                (-1) * (m_secondaryViewContainer->mapToGlobal(QPoint(0,0)).x() + geometries.widthOfSecondary);
    }
    return geometries;
}