summaryrefslogtreecommitdiff
path: root/src/tabbar/tabbar.cpp
blob: fb5c0444887000b760cad2493721fb207f47ebb9 (plain)
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/* ============================================================
 *     The rekonq project
 * ============================================================
 * SPDX-License-Identifier: GPL-2.0-or-later
 * Copyright (C) 2008-2012 by Andrea Diamantini <adjam7 at gmail dot com>
 * Copyright (C) 2009-2011 by Lionel Chauvin <megabigbug@yahoo.fr>
 * SPDX-License-Identifier: GPL-3.0-only
 * Copyright (C) 2022 aqua <aqua@iserlohn-fortress.net>
 * ============================================================ */

#include "tabbar.h"

static inline QByteArray highlightPropertyName(int index)
{
    return QByteArray("hAnim").append(QByteArray::number(index));
}

// ------------------------------------------------------------------------------------

TabBar::TabBar(QWidget *parent) : QTabBar(parent)
//, m_tabHighlightEffect(new TabHighlightEffect(this))
//, m_animationMapper(new QSignalMapper(this))
{
    setElideMode(Qt::ElideRight);

    setTabsClosable(true);
    setMovable(true);
    setAcceptDrops(true);

    connect(this, &QTabBar::currentChanged, this, [this](int index) {
      if (index == -1) {
        emit currentChanged(nullptr);
        return;
      }
      auto view = m_views.at(index);
      Q_ASSERT(!view.isNull());
      emit currentChanged(view);
    });
    connect(this, &QTabBar::tabMoved, this, [this](int from, int to) { m_views.move(from, to); });

    connect(this, &QTabBar::tabCloseRequested, this, [this](int index) {
      removeTab(index);
      emit removeView(m_views.at(index));
      m_views.removeAt(index);
    });

    /*
        // context menu(s)
        setContextMenuPolicy(Qt::CustomContextMenu);

        connect(this, SIGNAL(contextMenu(int,QPoint)), this, SLOT(contextMenu(int,QPoint)));
        connect(this, SIGNAL(emptyAreaContextMenu(QPoint)), this, SLOT(emptyAreaContextMenu(QPoint)));

        // Highlight effect
        connect(m_animationMapper, SIGNAL(mapped(int)), this, SLOT(removeAnimation(int)));
        setGraphicsEffect(m_tabHighlightEffect);
        m_tabHighlightEffect->setEnabled(true);
        */
}

int TabBar::addTab(RekonqView *view)
{
  m_views.append(view);
  const auto id = QTabBar::addTab(view->title());

  connect(view, &RekonqView::titleChanged, this, [this, view](const QString &title) {
    const int index = m_views.indexOf(view);
    setTabText(index, title);
  });

  return id;
}

// ------------------------------------------------------------------------------------

QSize TabBar::tabSizeHint(int index) const
{
    QWidget* p = qobject_cast<QWidget *>(parent());

    int w;
    // if tab is pinned...
    if (tabData(index).toBool())
    {
        w = 36;
    }
    else
    {
        // available width is tab widget width minus addTabButton width
        int tabWidgetWidth = p->size().width() - 24;
        w = c_baseTabWidth;
        if (w * count() > tabWidgetWidth)
        {
            w = tabWidgetWidth / count();
            if (w < c_minTabWidth)
            {
                w = c_minTabWidth;
            }
        }
    }

    int h = size().height();

    // this because it may happen sometimes (eg: exiting fullscreen)
    // that tabbar height is set to ZERO. And this is NOT good...
    if (h == 0) h = 30;

    return {w, h};
}

// ------------------------------------------------------------------------------------
// slots
/*
void TabBar::cloneTab()
{
    KAction *a = qobject_cast<KAction *>(sender());
    if (a)
    {
        int index = a->data().toInt();
        emit cloneTab(index);
    }
}


void TabBar::closeTab()
{
    KAction *a = qobject_cast<KAction *>(sender());
    if (a)
    {
        int index = a->data().toInt();
        emit closeTab(index);
    }
}


void TabBar::closeOtherTabs()
{
    KAction *a = qobject_cast<KAction *>(sender());
    if (a)
    {
        int index = a->data().toInt();
        emit closeOtherTabs(index);
    }
}


void TabBar::reloadTab()
{
    KAction *a = qobject_cast<KAction *>(sender());
    if (a)
    {
        int index = a->data().toInt();
        emit reloadTab(index);
    }
}


void TabBar::detachTab()
{
    KAction *a = qobject_cast<KAction *>(sender());
    if (a)
    {
        int index = a->data().toInt();
        emit detachTab(index);
    }
}


void TabBar::contextMenu(int tabIndex, const QPoint &pos)
{
    TabWidget *w = qobject_cast<TabWidget *>(parent());

    QAction *a;

    KMenu menu;

    a = w->actionByName(QL1S("new_tab"));
    menu.addAction(a);

    menu.addSeparator();    // ----------------------------------------------------------------

    a = new KAction(KIcon("tab-duplicate"), i18n("Clone"), this);
    a->setData(tabIndex);
    connect(a, SIGNAL(triggered(bool)), this, SLOT(cloneTab()));
    menu.addAction(a);

    a = new KAction(KIcon("view-refresh"), i18n("Reload"), this);
    connect(a, SIGNAL(triggered(bool)), this, SLOT(reloadTab()));
    a->setData(tabIndex);
    menu.addAction(a);

    a = new KAction(KIcon("view-refresh"), i18n("Reload All"), this);
    connect(a, SIGNAL(triggered(bool)), w, SLOT(reloadAllTabs()));
    menu.addAction(a);

    if (count() > 1)
    {
        a = new KAction(KIcon("tab-detach"), i18n("Detach"), this);
        connect(a, SIGNAL(triggered(bool)), this, SLOT(detachTab()));
        a->setData(tabIndex);
        menu.addAction(a);
    }

    if (tabData(tabIndex).toBool())
    {
        a = new KAction(i18n("Unpin Tab"), this);
        connect(a, SIGNAL(triggered(bool)), this, SLOT(unpinTab()));
        a->setData(tabIndex);
        menu.addAction(a);
    }
    else
    {
        a = new KAction(i18n("Pin Tab"), this);
        connect(a, SIGNAL(triggered(bool)), this, SLOT(pinTab()));
        a->setData(tabIndex);
        menu.addAction(a);
    }
    menu.addSeparator();    // ----------------------------------------------------------------

    a = new KAction(KIcon("tab-close"), i18n("&Close"), this);
    a->setData(tabIndex);
    connect(a, SIGNAL(triggered(bool)), this, SLOT(closeTab()));
    menu.addAction(a);

    if (count() > 1)
    {
        a = new KAction(KIcon("tab-close-other"), i18n("Close &Other Tabs"), this);
        connect(a, SIGNAL(triggered(bool)), this, SLOT(closeOtherTabs()));
        a->setData(tabIndex);
        menu.addAction(a);
    }

    menu.addSeparator();


    a = w->actionByName(QL1S("open_last_closed_tab"));
    menu.addAction(a);

    if (count() > 1)
    {
        a = w->actionByName(QL1S("bookmark_all_tabs"));
        menu.addAction(a);
    }

    menu.exec(pos);
}


void TabBar::emptyAreaContextMenu(const QPoint &pos)
{
    TabWidget *w = qobject_cast<TabWidget *>(parent());

    QAction *a;

    KMenu menu;

    a = w->actionByName(QL1S("new_tab"));
    menu.addAction(a);

    a = w->actionByName(QL1S("open_last_closed_tab"));
    menu.addAction(a);

    if (count() > 1)
    {
        a = w->actionByName(QL1S("bookmark_all_tabs"));
        menu.addAction(a);
    }

    menu.exec(pos);
}


void TabBar::setTabHighlighted(int index, bool b)
{
    if (!b)
    {
        removeAnimation(index);
        setTabTextColor(index, KColorScheme(QPalette::Active,
KColorScheme::Window).foreground(KColorScheme::NormalText).color()); return;
    }

    const QByteArray propertyName = highlightPropertyName(index);
    const QColor highlightColor = KColorScheme(QPalette::Active,
KColorScheme::Window).foreground(KColorScheme::PositiveText).color();

    if (tabTextColor(index) != highlightColor)
    {
        m_tabHighlightEffect->setEnabled(true);
        m_tabHighlightEffect->setProperty(propertyName, qreal(0.9));
        QPropertyAnimation *anim = new QPropertyAnimation(m_tabHighlightEffect, propertyName);
        m_highlightAnimation.insert(propertyName, anim);

        //setup the animation
        anim->setStartValue(0.9);
        anim->setEndValue(0.0);
        anim->setDuration(500);
        anim->setLoopCount(2);
        anim->start(QAbstractAnimation::DeleteWhenStopped);

        m_animationMapper->setMapping(anim, index);
        connect(anim, SIGNAL(finished()), m_animationMapper, SLOT(map()));

        setTabTextColor(index, highlightColor);
    }
}


QRect TabBar::tabTextRect(int index)
{
    QStyleOptionTabV3 option;
    initStyleOption(&option, index);
    return style()->subElementRect(QStyle::SE_TabBarTabText, &option, this);
}


void TabBar::removeAnimation(int index)
{
    const QByteArray propertyName = highlightPropertyName(index);
    m_tabHighlightEffect->setProperty(propertyName, QVariant()); //destroy the property

    QPropertyAnimation *anim = m_highlightAnimation.take(propertyName);
    m_animationMapper->removeMappings(anim);
    delete anim;

    if (m_highlightAnimation.isEmpty())
        m_tabHighlightEffect->setEnabled(false);
}


void TabBar::tabInserted(int index)
{
    // Find the available index to move
    int availableIndex = index;
    for (int i = index; i < count(); i++)
    {
        if (tabData(i).toBool())
        {
            availableIndex++;
            break;
        }
    }

    if (index < availableIndex)
    {
        TabWidget *w = qobject_cast<TabWidget *>(parent());
        w->moveTab(index, availableIndex);
    }

    KTabBar::tabInserted(index);
}


void TabBar::tabRemoved(int index)
{
    hideTabPreview();
    removeAnimation(index);

    KTabBar::tabRemoved(index);
}


void TabBar::mouseMoveEvent(QMouseEvent *event)
{
    KTabBar::mouseMoveEvent(event);

    if (count() == 1)
    {
        return;
    }

    if (ReKonfig::hoveringTabOption() != 0)
        return;

    // Find the tab under the mouse
    const int tabIndex = tabAt(event->pos());

    // if found and not the current tab then show tab preview
    if (tabIndex != -1
            && tabIndex != currentIndex()
            && m_currentTabPreviewIndex != tabIndex
            && event->buttons() == Qt::NoButton
       )
    {
        m_currentTabPreviewIndex = tabIndex;

        // if first time over tab, apply a small delay. If not, show it now!
        m_isFirstTimeOnTab
        ? QTimer::singleShot(200, this, SLOT(showTabPreview()))
        : showTabPreview();
    }

    // if current tab or not found then hide previous tab preview
    if (tabIndex == currentIndex() || tabIndex == -1)
    {
        hideTabPreview();
    }
}


void TabBar::leaveEvent(QEvent *event)
{
    hideTabPreview();
    m_isFirstTimeOnTab = true;

    KTabBar::leaveEvent(event);
}


void TabBar::mousePressEvent(QMouseEvent *event)
{
    hideTabPreview();

    // just close tab on middle mouse click
    if (event->button() == Qt::MidButton)
        return;

    KTabBar::mousePressEvent(event);
}


void TabBar::mouseReleaseEvent(QMouseEvent *event)
{
    // count pinned tabs
    int pinnedTabs = 0;
    for (int i = 0; i < count(); i++)
    {
        if (tabData(i).toBool())
            pinnedTabs++;
    }

    // fix unpinned ones
    for (int i = 0; i < pinnedTabs; i++)
    {
        if (!tabData(i).toBool())
        {
            TabWidget *w = qobject_cast<TabWidget *>(parent());
            w->moveTab(i, pinnedTabs);
            w->setCurrentIndex(pinnedTabs);
        }
    }

    // fix pinned ones
    for (int i = pinnedTabs; i < count(); i++)
    {
        if (tabData(i).toBool())
        {
            TabWidget *w = qobject_cast<TabWidget *>(parent());
            w->moveTab(i, pinnedTabs - 1);
            w->setCurrentIndex(pinnedTabs - 1);
        }
    }
    KTabBar::mouseReleaseEvent(event);
}


void TabBar::tabLayoutChange()
{
    KTabBar::tabLayoutChange();
    emit tabLayoutChanged();
}


void TabBar::showTabPreview()
{
    if (m_isFirstTimeOnTab)
        m_isFirstTimeOnTab = false;

    //delete previous tab preview
    delete m_previewPopup.data();
    m_previewPopup.clear();

    TabWidget *tabW = qobject_cast<TabWidget *>(parent());

    WebWindow *indexedTab = tabW->webWindow(m_currentTabPreviewIndex);
    WebWindow *currentTab = tabW->webWindow(currentIndex());

    // check if view && currentView exist before using them :)
    if (!currentTab || !indexedTab)
        return;

    // no previews during load
    if (indexedTab->isLoading())
        return;

    int w = c_baseTabWidth;
    int h = w * tabW->size().height() / tabW->size().width();

    m_previewPopup = new TabPreviewPopup(indexedTab->tabPreview(w, h), indexedTab->url().url() , this);

    int tabBarWidth = tabW->size().width();
    int leftIndex = tabRect(m_currentTabPreviewIndex).x() + (tabRect(m_currentTabPreviewIndex).width() - w) / 2;

    if (leftIndex < 0)
    {
        leftIndex = 0;
    }
    else if (leftIndex + w > tabBarWidth)
    {
        leftIndex = tabBarWidth - w;
    }

    QPoint pos(leftIndex, tabRect(m_currentTabPreviewIndex).y() + tabRect(m_currentTabPreviewIndex).height());
    m_previewPopup.data()->show(mapToGlobal(pos));
}


void TabBar::hideTabPreview()
{
    if (!m_previewPopup.isNull())
    {
        m_previewPopup.data()->hide();
    }
    m_currentTabPreviewIndex = -1;
}


void TabBar::pinTab()
{
    KAction *a = qobject_cast<KAction *>(sender());
    if (!a)
        return;

    int index = a->data().toInt();

    // Find the available index to move
    int availableIndex = 0;
    for (int i = 0; i < count(); i++)
    {
        if (!tabData(i).toBool())
        {
            availableIndex = i;
            break;
        }
    }

    TabWidget *w = qobject_cast<TabWidget *>(parent());
    w->moveTab(index, availableIndex);
    index = availableIndex;

    // set this tab data true to know this has been pinned
    setTabData(index, true);

    tabButton(index, QTabBar::RightSide)->hide();
    setTabText(index, QString());

    // workaround: "fix" the icon (or at least, try to...)
    QLabel *label = qobject_cast<QLabel* >(tabButton(index, QTabBar::LeftSide));
    if (!label)
        label = new QLabel(this);

    label->resize(QSize(16,16));

    setTabButton(index, QTabBar::LeftSide, 0);
    setTabButton(index, QTabBar::LeftSide, label);

    KIcon ic = IconManager::self()->iconForUrl(w->webWindow(index)->url());
    label->setPixmap(ic.pixmap(16, 16));

    SessionManager::self()->saveSession();
}


void TabBar::unpinTab()
{
    KAction *a = qobject_cast<KAction *>(sender());
    if (!a)
        return;

    int index = a->data().toInt();

    // Find the available index to move
    int availableIndex = 0;
    for (int i = 1; i < count(); i++)
    {
        if (!tabData(i).toBool())
        {
            availableIndex = i - 1;
            break;
        }
        availableIndex++;
    }

    TabWidget *w = qobject_cast<TabWidget *>(parent());
    w->moveTab(index, availableIndex);
    index = availableIndex;

    tabButton(index, QTabBar::RightSide)->show();
    setTabText(index, w->webWindow(index)->title());

    // set the tab data false to forget this pinned tab
    setTabData(index, false);

    // workaround: "fix" the icon (or at least, try to...)
    QLabel *label = qobject_cast<QLabel* >(tabButton(index, QTabBar::LeftSide));
    if (!label)
        label = new QLabel(this);

    label->resize(QSize(16,16));

    setTabButton(index, QTabBar::LeftSide, 0);
    setTabButton(index, QTabBar::LeftSide, label);

    KIcon ic = IconManager::self()->iconForUrl(w->webWindow(index)->url());
    label->setPixmap(ic.pixmap(16, 16));

    SessionManager::self()->saveSession();
}
*/