File: paneltabbar.cpp

package info (click to toggle)
krusader 2%3A2.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,448 kB
  • sloc: cpp: 56,112; ansic: 1,187; xml: 811; sh: 23; makefile: 3
file content (407 lines) | stat: -rw-r--r-- 12,112 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
62
63
64
65
66
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
165
166
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
205
206
207
208
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
    SPDX-FileCopyrightText: 2002 Shie Erlich <erlich@users.sourceforge.net>
    SPDX-FileCopyrightText: 2002 Rafi Yanai <yanai@users.sourceforge.net>
    SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org>

    Based on original code from Sebastian Trueg <trueg@kde.org>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "paneltabbar.h"

#include "../icon.h"
#include "../krglobal.h"
#include "Panel/listpanel.h"
#include "Panel/panelfunc.h"
#include "abstractpanelmanager.h"
#include "compat.h"
#include "defaults.h"
#include "tabactions.h"

// QtCore
#include <QEvent>
// QtGui
#include <QFontMetrics>
#include <QMouseEvent>
#include <QResizeEvent>
// QtWidgets
#include <QAction>
#include <QMenu>

#include <KActionMenu>
#include <KLocalizedString>
#include <KSharedConfig>

static const int sDragEnterDelay = 500; // msec

PanelTabBar::PanelTabBar(QWidget *parent, TabActions *actions)
    : QTabBar(parent)
    , _maxTabLength(0)
    , _tabClicked(false)
    , _tabDoubleClicked(false)
    , _draggingTab(false)
    , _dragTabIndex(-1)
{
    const KConfigGroup cfg(krConfig, "Look&Feel");
    const bool expandingTabs = cfg.readEntry("Expanding Tabs", true);
    const bool showCloseButtons = cfg.readEntry("Show Close Tab Buttons", true);
    _doubleClickClose = cfg.readEntry("Close Tab By Double Click", false);

    _panelActionMenu = new KActionMenu(i18n("Panel"), this);

    setAcceptDrops(true);
    setExpanding(expandingTabs);
    setTabsClosable(showCloseButtons);

    insertAction(actions->actNewTab);
    insertAction(actions->actLockTab);
    insertAction(actions->actPinTab);
    insertAction(actions->actDupTab);
    insertAction(actions->actMoveTabToOtherSide);
    insertAction(actions->actMoveTabToLeft);
    insertAction(actions->actMoveTabToRight);
    insertAction(actions->actCloseTab);
    insertAction(actions->actUndoCloseTab);
    insertAction(actions->actCloseInactiveTabs);
    insertAction(actions->actCloseDuplicatedTabs);

    setMovable(true); // enable drag'n'drop
    _dragTimer = new QTimer(this);
    _dragTimer->setSingleShot(true);
    _dragTimer->setInterval(sDragEnterDelay);
    connect(_dragTimer, &QTimer::timeout, this, [=]() {
        if (_dragTabIndex != -1 && _dragTabIndex != currentIndex()) {
            setCurrentIndex(_dragTabIndex);
        }
        _dragTabIndex = -1;
    });

    setShape(QTabBar::TriangularSouth);
}

void PanelTabBar::insertAction(QAction *action)
{
    _panelActionMenu->addAction(action);
}

int PanelTabBar::addPanel(ListPanel *panel, bool setCurrent, int insertIndex)
{
    // If the position where to place the new tab is not specified,
    // take the settings into account
    if (insertIndex == -1) {
        insertIndex = KConfigGroup(krConfig, "Look&Feel").readEntry("Insert Tabs After Current", false) ? currentIndex() + 1 : count();
    }

    QUrl virtualPath = panel->virtualPath();
    panel->setPinnedUrl(virtualPath);
    const QString text = squeeze(virtualPath);
    // In the help about `insertTab()` it's written that it inserts a new tab at
    // position `index`. If `index` is out of range, the new tab is appened. Returns
    // the new tab's index
    insertIndex = insertTab(insertIndex, text);

    setTabData(insertIndex, QVariant(reinterpret_cast<long long>(panel)));

    setIcon(insertIndex, panel);

    // make sure all tabs lengths are correct
    layoutTabs();

    if (setCurrent)
        setCurrentIndex(insertIndex);

    return insertIndex;
}

ListPanel *PanelTabBar::getPanel(int tabIdx)
{
    QVariant v = tabData(tabIdx);
    if (v.isNull())
        return nullptr;
    return (ListPanel *)v.toLongLong();
}

void PanelTabBar::changePanel(int tabIdx, ListPanel *panel)
{
    setTabData(tabIdx, QVariant((long long)panel));
}

ListPanel *PanelTabBar::removePanel(int index, ListPanel *&panelToDelete)
{
    panelToDelete = getPanel(index); // old panel to kill later
    disconnect(panelToDelete, nullptr, this, nullptr);

    removeTab(index);
    layoutTabs();

    return getPanel(currentIndex());
}

ListPanel *PanelTabBar::removeCurrentPanel(ListPanel *&panelToDelete)
{
    return removePanel(currentIndex(), panelToDelete);
}

void PanelTabBar::updateTab(ListPanel *panel)
{
    // find which is the correct tab
    for (int i = 0; i < count(); i++) {
        if ((ListPanel *)tabData(i).toLongLong() == panel) {
            setPanelTextToTab(i, panel);
            setIcon(i, panel);
            break;
        }
    }
}

void PanelTabBar::duplicateTab()
{
    int id = currentIndex();
    emit newTab(((ListPanel *)tabData(id).toLongLong())->virtualPath());
}

void PanelTabBar::setIcon(int index, ListPanel *panel)
{
    Icon tabIcon;
    if (panel->isLocked()) {
        tabIcon = Icon("lock");
    } else if (panel->isPinned()) {
        tabIcon = Icon("pin");
    }
    setTabIcon(index, tabIcon);
}

QString PanelTabBar::squeeze(const QUrl &url, int tabIndex)
{
    const QString longText = url.isEmpty() ? i18n("[invalid]") : url.isLocalFile() ? url.path() : url.toDisplayString();
    if (tabIndex >= 0)
        setTabToolTip(tabIndex, longText);

    const KConfigGroup group(krConfig, "Look&Feel");
    const bool showLongNames = group.readEntry("Fullpath Tab Names", _FullPathTabNames);

    QString text;
    if (!showLongNames) {
        const QString scheme = url.scheme().isEmpty() || url.isLocalFile() ? QString("") : (url.scheme() + ':');
        const QString host = url.host().isEmpty() ? QString("") : (url.host() + ':');
        const QString name = url.isLocalFile() && url.fileName().isEmpty() ? QString("/") : url.fileName();
        text = scheme + host + name;
    } else {
        text = longText;
    }

    if (text.isEmpty())
        text = i18nc("invalid URL path", "?");

    // set the real max length
    QFontMetrics fm(fontMetrics());
    _maxTabLength = (dynamic_cast<QWidget *>(parent())->width() - (6 * fm.horizontalAdvance("W"))) / fm.horizontalAdvance("W");
    // each tab gets a fair share of the max tab length
    const int effectiveTabLength = _maxTabLength / (count() == 0 ? 1 : count());
    const int labelWidth = fm.horizontalAdvance("W") * effectiveTabLength;
    const int textWidth = fm.horizontalAdvance(text);
    if (textWidth <= labelWidth)
        return text;

    // squeeze text - start with the dots only
    QString squeezedText = "...";
    int squeezedWidth = fm.horizontalAdvance(squeezedText);

    qsizetype letters = text.length() * (labelWidth - squeezedWidth) / textWidth / 2;
    if (labelWidth < squeezedWidth)
        letters = 1;
    squeezedText = text.left(letters) + "..." + text.right(letters);
    squeezedWidth = fm.horizontalAdvance(squeezedText);

    if (squeezedWidth < labelWidth) {
        // we estimated too short
        // add letters while text < label
        do {
            letters++;
            squeezedText = text.left(letters) + "..." + text.right(letters);
            squeezedWidth = fm.horizontalAdvance(squeezedText);
        } while (squeezedWidth < labelWidth);
        letters--;
        squeezedText = text.left(letters) + "..." + text.right(letters);
    } else if (squeezedWidth > labelWidth) {
        // we estimated too long
        // remove letters while text > label
        do {
            letters--;
            squeezedText = text.left(letters) + "..." + text.right(letters);
            squeezedWidth = fm.horizontalAdvance(squeezedText);
        } while (letters && squeezedWidth > labelWidth);
    }

    return squeezedText;
}

void PanelTabBar::resizeEvent(QResizeEvent *e)
{
    QTabBar::resizeEvent(e);

    layoutTabs();
}

void PanelTabBar::mouseMoveEvent(QMouseEvent *e)
{
    QTabBar::mouseMoveEvent(e);
    if (_tabClicked) {
        _draggingTab = true;
        emit draggingTab(e);
    }
}

void PanelTabBar::mousePressEvent(QMouseEvent *e)
{
    int clickedTabIndex = tabAt(e->pos());

    // don't handle clicks outside of tabs
    if (clickedTabIndex < 0) {
        QTabBar::mousePressEvent(e);
        return;
    }

    bool isActiveTab = currentIndex() == clickedTabIndex;
    KrPanel *tabPane = getPanel(clickedTabIndex)->manager()->currentPanel();
    KrPanel *activePane = ACTIVE_PANEL;

    _tabClicked = true;

    // activate the pane with the clicked tab unless it's active already
    if (tabPane && tabPane != activePane) {
        // slotFocusOnMe() is an expensive call, make it only when necessary
        // If current tab is not active, the QTabBar::mousePressEvent will trigger
        // current tab index change signal and the slot will call slotFocusOnMe().
        // This way we avoid extra calls.
        if (isActiveTab)
            tabPane->gui->slotFocusOnMe();
        else
            emit tabPane->gui->activate();
    }

    if (e->button() == Qt::RightButton) {
        if (!isActiveTab)
            setCurrentIndex(clickedTabIndex);

        // show the popup menu
        _panelActionMenu->menu()->popup(e->globalPosition().toPoint());
    } else if (e->button() == Qt::LeftButton && !_tabDoubleClicked) {
        bool isDuplicationEvent = false;

        if (e->modifiers() == Qt::ControlModifier) {
            KConfigGroup group(krConfig, "Look&Feel");
            if (group.readEntry("Duplicate Tab Click", "disabled") == "ctrl_click") {
                isDuplicationEvent = true;
            }
        } else if (e->modifiers() == Qt::AltModifier) {
            KConfigGroup group(krConfig, "Look&Feel");
            if (group.readEntry("Duplicate Tab Click", "disabled") == "alt_click") {
                isDuplicationEvent = true;
            }
        }

        if (isDuplicationEvent) {
            // Duplicate only the active tab, otherwise dismiss the click,
            // because an inactive tab may not be properly initialized
            // and duplication will be incomplete in this case.
            if (isActiveTab)
                emit duplicateCurrentTab();
            else
                return;
        }
    } else if (e->button() == Qt::MiddleButton) {
        if (!isActiveTab)
            setCurrentIndex(clickedTabIndex);

        // close the current tab
        emit closeCurrentTab();
    }

    QTabBar::mousePressEvent(e);
}

void PanelTabBar::mouseDoubleClickEvent(QMouseEvent *e)
{
    _tabDoubleClicked = true;

    if (!_doubleClickClose) {
        QTabBar::mouseDoubleClickEvent(e);
        return;
    }

    int clickedTabIndex = tabAt(e->pos());

    if (-1 == clickedTabIndex) { // clicked on nothing ...
        QTabBar::mouseDoubleClickEvent(e);
        return;
    }

    _tabClicked = true;

    // close the current tab
    if (e->button() == Qt::LeftButton && e->modifiers() == Qt::NoModifier) {
        emit closeCurrentTab();
    }
    QTabBar::mouseDoubleClickEvent(e);
}

void PanelTabBar::mouseReleaseEvent(QMouseEvent *e)
{
    QTabBar::mouseReleaseEvent(e);
    if (_draggingTab)
        emit draggingTabFinished(e);
    _draggingTab = false;
    _tabClicked = false;
    _tabDoubleClicked = false;
}

void PanelTabBar::dragEnterEvent(QDragEnterEvent *e)
{
    e->accept();
    handleDragEvent(tabAt(e->position().toPoint()));
    QTabBar::dragEnterEvent(e);
}

void PanelTabBar::dragLeaveEvent(QDragLeaveEvent *)
{
    handleDragEvent(-1);
}

void PanelTabBar::dragMoveEvent(QDragMoveEvent *e)
{
    e->ignore();
    handleDragEvent(tabAt(e->position().toPoint()));
    QTabBar::dragMoveEvent(e);
}

void PanelTabBar::handleDragEvent(int tabIndex)
{
    if (_dragTabIndex == tabIndex)
        return;

    _dragTabIndex = tabIndex;
    if (_dragTabIndex == -1) {
        _dragTimer->stop();
    } else {
        _dragTimer->start();
    }
}

void PanelTabBar::layoutTabs()
{
    for (int i = 0; i < count(); i++) {
        setPanelTextToTab(i, (ListPanel *)tabData(i).toLongLong());
    }
}

void PanelTabBar::setPanelTextToTab(int tabIndex, ListPanel *panel)
{
    // update tab text from pinnedUrl in case the tab is pinned
    if (panel->isPinned()) {
        setTabText(tabIndex, squeeze(panel->pinnedUrl(), tabIndex));
    } else {
        setTabText(tabIndex, squeeze(panel->virtualPath(), tabIndex));
    }
}