summaryrefslogtreecommitdiffstats
blob: 3d80d95f24ec77de08b91d1af9883d6f4838088d (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
/*
 * Copyright (c) 2014-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swift/QtUI/Trellis/QtDynamicGridLayout.h>

#include <cassert>

#include <QApplication>
#include <QEvent>
#include <QGridLayout>
#include <QLayoutItem>
#include <QtDebug>

#include <Swiften/Base/Log.h>

#include <Swift/QtUI/QtSwiftUtil.h>
#include <Swift/QtUI/QtTabWidget.h>
#include <Swift/QtUI/QtTabbable.h>
#include <Swift/QtUI/Trellis/QtDNDTabBar.h>

namespace Swift {

QtDynamicGridLayout::QtDynamicGridLayout(QWidget* parent, bool enableDND) : QWidget(parent), dndEnabled_(enableDND), movingTab_(nullptr) {
    gridLayout_ = new QGridLayout(this);
    setContentsMargins(0,0,0,0);
    setDimensions(QSize(1,1));
    connect(qApp, SIGNAL(focusChanged(QWidget*, QWidget*)), this, SLOT(handleApplicationFocusChanged(QWidget*,QWidget*)));
}

QtDynamicGridLayout::~QtDynamicGridLayout() {
}

int QtDynamicGridLayout::addTab(QtTabbable* tab, const QString& title) {
    assert(gridLayout_->rowCount() > 0 && gridLayout_->columnCount() > 0);

    QPoint lastPos(0,0);
    if (tabPositions_.contains(P2QSTRING(tab->getID()))) {
        lastPos = tabPositions_[P2QSTRING(tab->getID())];
    }

    lastPos = QPoint(qMin(lastPos.x(), gridLayout_->columnCount() - 1), qMin(lastPos.y(), gridLayout_->rowCount() - 1));

    QLayoutItem* item = gridLayout_->itemAtPosition(lastPos.y(), lastPos.x());
    QtTabWidget* tabWidget = dynamic_cast<QtTabWidget*>(item ? item->widget() : nullptr);
    if (tabWidget) {
        tabWidget->addTab(tab, title);
    }
    return tabWidget ? indexOf(tab) : -1;
}

int QtDynamicGridLayout::count() const {
    int count = 0;
    for (int y = 0; y < gridLayout_->rowCount(); y++) {
        for (int x = 0; x < gridLayout_->columnCount(); x++) {
            QLayoutItem* layoutItem = gridLayout_->itemAtPosition(y, x);
            QtTabWidget* tabWidget = dynamic_cast<QtTabWidget*>(layoutItem->widget());
            if (tabWidget) {
                count += tabWidget->count();
            }
        }
    }
    return count;
}

QWidget* QtDynamicGridLayout::widget(int index) const {
    QWidget* widgetAtIndex = nullptr;
    for (int y = 0; y < gridLayout_->rowCount(); y++) {
        for (int x = 0; x < gridLayout_->columnCount(); x++) {
            QLayoutItem* layoutItem = gridLayout_->itemAtPosition(y, x);
            QtTabWidget* tabWidget = dynamic_cast<QtTabWidget*>(layoutItem ? layoutItem->widget() : nullptr);
            if (tabWidget) {
                if (index < tabWidget->count()) {
                    widgetAtIndex = tabWidget->widget(index);
                    return widgetAtIndex;
                }
                else {
                    index -= tabWidget->count();
                }
            }
        }
    }
    return widgetAtIndex;
}

int QtDynamicGridLayout::indexOf(const QWidget* widget) const {
    int index = 0;
    if (widget) {
        for (int y = 0; y < gridLayout_->rowCount(); y++) {
            for (int x = 0; x < gridLayout_->columnCount(); x++) {
                QLayoutItem* layoutItem = gridLayout_->itemAtPosition(y, x);
                QtTabWidget* tabWidget = dynamic_cast<QtTabWidget*>(layoutItem->widget());
                if (tabWidget) {
                    for (int n = 0; n < tabWidget->count(); n++) {
                        QWidget* nthWidget = tabWidget->widget(n);
                        if (nthWidget == widget) {
                            return index;
                        }
                        index++;
                    }
                }
            }
        }
    }
    return -1;
}

void QtDynamicGridLayout::handleApplicationFocusChanged(QWidget*, QWidget* newFocus) {
    if (movingTab_ || resizing_) {
        return;
    }

    if (newFocus) {
        if (isAncestorOf(newFocus)) {
            QtTabbable *newTab = dynamic_cast<QtTabbable*>(newFocus->parentWidget());
            if (newTab) {
                onCurrentIndexChanged(currentIndex());
            }
        }
    }
}

int QtDynamicGridLayout::currentIndex() const {
    return indexOf(currentWidget());
}

void QtDynamicGridLayout::setCurrentIndex(int index) {
    int tabIndex = -1;
    QtTabWidget* tabWidget = indexToTabWidget(index, tabIndex);
    if (tabIndex >= 0) {
        tabWidget->setCurrentIndex(tabIndex);
        if (!tabWidget->hasFocus()) {
            tabWidget->widget(tabIndex)->setFocus(Qt::TabFocusReason);
        }
    } else {
        assert(false);
    }
}

void QtDynamicGridLayout::removeTab(int index) {
    int tabIndex = -1;
    QtTabWidget* tabWidget = indexToTabWidget(index, tabIndex);
    if (tabWidget) {
        tabWidget->removeTab(tabIndex);
    }
}

/**
 * This event filter serves the purpose of filtering out all QEvent::Show events targeted at
 * all widgets excepts the currently moving widget.
 * It is required because of the way Qt internally implements the QTabBar::moveTab method.
 * It does not move the actual tab in the underlying structure, but instead removes it from
 * a stacked layout and later adds it again.
 * Both the remove and insert produce a lot signal emission and focus changes. Most of which
 * the application MUST NOT react on because of the QTabBar and the corresponding QTabWidget
 * being out of sync in an inconsistent state.
 */
bool QtDynamicGridLayout::eventFilter(QObject* object, QEvent* event) {
    QtTabbable* tab = qobject_cast<QtTabbable*>(object);
    if (!tab) {
        return false;
    }
    if (tab && (tab != movingTab_)) {
        if (event->type() == QEvent::Show) {
            return true;
        }
    }
    return false;
}

QWidget* QtDynamicGridLayout::currentWidget() const {
    QWidget* current = nullptr;
    current = focusWidget();
    while (current && !dynamic_cast<QtTabbable*>(current)) {
        if (current->parentWidget()) {
            current = current->parentWidget();
        } else {
            current = nullptr;
            break;
        }
    }
    if (!current) {
        current = widget(0);
    }
    return current;
}

void QtDynamicGridLayout::setCurrentWidget(QWidget* widget) {
    if (widget) {
        for (int y = 0; y < gridLayout_->rowCount(); y++) {
            for (int x = 0; x < gridLayout_->columnCount(); x++) {
                QLayoutItem* layoutItem = gridLayout_->itemAtPosition(y, x);
                QtTabWidget* tabWidget = dynamic_cast<QtTabWidget*>(layoutItem->widget());
                if (tabWidget) {
                    for (int n = 0; n < tabWidget->count(); n++) {
                        if (tabWidget->widget(n) == widget) {
                            tabWidget->setCurrentWidget(widget);
                        }
                    }
                }
            }
        }
    }
}

QtTabWidget* QtDynamicGridLayout::indexToTabWidget(int index, int& tabIndex) {
    for (int y = 0; y < gridLayout_->rowCount(); y++) {
        for (int x = 0; x < gridLayout_->columnCount(); x++) {
            QLayoutItem* layoutItem = gridLayout_->itemAtPosition(y, x);
            QtTabWidget* tabWidget = dynamic_cast<QtTabWidget*>(layoutItem->widget());
            if (tabWidget) {
                if (index < tabWidget->count()) {
                    tabIndex = index;
                    return tabWidget;
                }
                else {
                    index -= tabWidget->count();
                    if (index < 0) {
                        qWarning() << "Called QtDynamicGridLayout::setCurrentIndex with index out of bounds: index = " << index;
                        tabIndex = -1;
                        return nullptr;
                    }
                }
            }
        }
    }
    tabIndex = -1;
    return nullptr;
}

bool QtDynamicGridLayout::isDNDEnabled() const {
    return dndEnabled_;
}

QHash<QString, QPoint> QtDynamicGridLayout::getTabPositions() const {
    return tabPositions_;
}

void QtDynamicGridLayout::setTabPositions(const QHash<QString, QPoint> positions) {
    tabPositions_ = positions;
}

QSize QtDynamicGridLayout::getDimension() const {
    return QSize(gridLayout_->columnCount(), gridLayout_->rowCount());
}

void QtDynamicGridLayout::setDimensions(const QSize& dim) {
    resizing_ = true;
    assert(dim.width() > 0 && dim.height() > 0);
    setUpdatesEnabled(false);

    QWidget* restoredWidget = currentWidget();

    QGridLayout* oldLayout = dynamic_cast<QGridLayout*>(layout());
    QGridLayout* newLayout = new QGridLayout(this);
    newLayout->setSpacing(4);
    newLayout->setContentsMargins(0,0,0,0);

    int oldWidth = oldLayout->columnCount();
    int oldHeight = oldLayout->rowCount();
    int maxCol = qMax(oldWidth, dim.width());
    int minCol = qMin(oldWidth, dim.width());
    int maxRow = qMax(oldHeight, dim.height());
    int minRow = qMin(oldHeight, dim.height());

    for (int row = 0; row < maxRow; row++) {
        for (int col = 0; col < maxCol; col++) {
            QLayoutItem* oldItem = oldLayout->itemAtPosition(row, col);
            QLayoutItem* newItem = newLayout->itemAtPosition(row, col);
            bool removeRow = !(row < dim.height());
            bool removeCol = !(col < dim.width());

            if (removeCol || removeRow) {
                if (oldItem) {
                    int squeezeRow = removeRow ? (minRow - 1) : row;
                    int squeezeCol = removeCol ? (minCol - 1) : col;
                    newItem = newLayout->itemAtPosition(squeezeRow, squeezeCol);
                    if (!newItem) {
                        newLayout->addWidget(createDNDTabWidget(this), squeezeRow, squeezeCol);
                        newItem = newLayout->itemAtPosition(squeezeRow, squeezeCol);
                    }
                    QtTabWidget* oldTabWidget = dynamic_cast<QtTabWidget*>(oldItem->widget());
                    QtTabWidget* newTabWidget = dynamic_cast<QtTabWidget*>(newItem->widget());
                    assert(oldTabWidget && newTabWidget);

                    oldTabWidget->hide();
                    while(oldTabWidget->count()) {
                        QIcon icon = oldTabWidget->tabIcon(0);
                        QString text = oldTabWidget->tabText(0);
                        newTabWidget->addTab(oldTabWidget->widget(0), icon, text);
                    }
                    delete oldTabWidget;
                }
            } else {
                if (oldItem) {
                    newLayout->addWidget(oldItem->widget(), row, col);
                    newItem = newLayout->itemAtPosition(row, col);
                } else {
                    newLayout->addWidget(createDNDTabWidget(this), row, col);
                }
            }
        }
    }

    for (int col = 0; col < dim.width(); col++) {
        newLayout->setColumnStretch(col, 1);
    }
    for (int row = 0; row < dim.height(); row++) {
        newLayout->setRowStretch(row, 1);
    }

    setUpdatesEnabled(true);
    delete layout();
    setLayout(newLayout);
    gridLayout_ = newLayout;

    resizing_ = false;
    setCurrentWidget(restoredWidget);
}

void QtDynamicGridLayout::moveCurrentTabRight() {
    int index = currentIndex();
    if (index >= 0) {
        int tabIndex = -1;
        QtTabWidget* tabWidget = indexToTabWidget(index, tabIndex);
        assert(tabWidget);
        int newTabIndex = (tabIndex + 1) % tabWidget->count();
        moveTab(tabWidget, tabIndex, newTabIndex);
    }
}

void QtDynamicGridLayout::moveCurrentTabLeft() {
    int index = currentIndex();
    if (index >= 0) {
        int tabIndex = -1;
        QtTabWidget* tabWidget = indexToTabWidget(index, tabIndex);
        assert(tabWidget);
        int newTabIndex = (tabWidget->count() + tabIndex - 1) % tabWidget->count();
        moveTab(tabWidget, tabIndex, newTabIndex);
    }
}

void QtDynamicGridLayout::moveCurrentTabToNextGroup() {
    int index = currentIndex();
    if (index >= 0) {
        int tabIndex = -1;
        QtTabWidget* tabWidget = indexToTabWidget(index, tabIndex);

        int row = -1;
        int col = -1;
        int tmp;
        gridLayout_->getItemPosition(gridLayout_->indexOf(tabWidget), &row, &col, &tmp, &tmp);

        // calculate next cell
        col++;
        if (!(col < gridLayout_->columnCount())) {
            col = 0;
            row++;
            if (!(row < gridLayout_->rowCount())) {
                row = 0;
            }
        }

        QtTabWidget* targetTabWidget = dynamic_cast<QtTabWidget*>(gridLayout_->itemAtPosition(row, col)->widget());
        assert(tabWidget);
        assert(targetTabWidget);

        // fetch tab information
        QWidget* tab = tabWidget->widget(tabIndex);
        QString tabText = tabWidget->tabText(tabIndex);

        // move tab
        tab->blockSignals(true);
        targetTabWidget->addTab(tab, tabText);
        tab->blockSignals(false);
        tab->setFocus(Qt::TabFocusReason);

        updateTabPositions();
    }
}

void QtDynamicGridLayout::moveCurrentTabToPreviousGroup() {
    int index = currentIndex();
    if (index >= 0) {
        int tabIndex = -1;
        QtTabWidget* tabWidget = indexToTabWidget(index, tabIndex);

        int row = -1;
        int col = -1;
        int tmp;
        gridLayout_->getItemPosition(gridLayout_->indexOf(tabWidget), &row, &col, &tmp, &tmp);

        // calculate next cell
        col--;
        if (col < 0) {
            col = gridLayout_->columnCount() - 1;
            row--;
            if (row < 0) {
                row = gridLayout_->rowCount() - 1;
            }
        }

        QtTabWidget* targetTabWidget = dynamic_cast<QtTabWidget*>(gridLayout_->itemAtPosition(row, col)->widget());
        assert(tabWidget);
        assert(targetTabWidget);

        // fetch tab information
        QWidget* tab = tabWidget->widget(tabIndex);
        QString tabText = tabWidget->tabText(tabIndex);

        // move tab
        tab->blockSignals(true);
        targetTabWidget->addTab(tab, tabText);
        tab->blockSignals(false);
        tab->setFocus(Qt::TabFocusReason);

        updateTabPositions();
    }
}

void QtDynamicGridLayout::handleTabCloseRequested(int index) {
    updateTabPositions();
    QtTabWidget* tabWidgetSender = dynamic_cast<QtTabWidget*>(sender());
    for (int y = 0; y < gridLayout_->rowCount(); y++) {
        for (int x = 0; x < gridLayout_->columnCount(); x++) {
            QLayoutItem* layoutItem = gridLayout_->itemAtPosition(y, x);
            QtTabWidget* tabWidget = dynamic_cast<QtTabWidget*>(layoutItem->widget());
            if (tabWidget == tabWidgetSender) {
                tabCloseRequested(index);
            }
            else {
                index += tabWidget->count();
            }
        }
    }
}

void QtDynamicGridLayout::handleTabCurrentChanged(int index) {
    if (movingTab_) {
        return;
    }

    if (index >= 0) {
        QTabWidget* sendingTabWidget = dynamic_cast<QTabWidget*>(sender());
        assert(sendingTabWidget);
        sendingTabWidget->widget(index)->setFocus();
    }
}

void QtDynamicGridLayout::updateTabPositions() {
    for (int y = 0; y < gridLayout_->rowCount(); y++) {
        for (int x = 0; x < gridLayout_->columnCount(); x++) {
            QLayoutItem* layoutItem = gridLayout_->itemAtPosition(y, x);
            QtTabWidget* tabWidget = dynamic_cast<QtTabWidget*>(layoutItem->widget());
            assert(tabWidget);
            for (int index = 0; index < tabWidget->count(); index++) {
                QtTabbable* tab = dynamic_cast<QtTabbable*>(tabWidget->widget(index));
                assert(tab);
                tabPositions_.insert(P2QSTRING(tab->getID()), QPoint(x, y));
            }
        }
    }
}

void QtDynamicGridLayout::moveTab(QtTabWidget* tabWidget, int oldIndex, int newIndex) {
#if QT_VERSION >= 0x040500
    SWIFT_LOG_ASSERT(movingTab_ == nullptr, error) << std::endl;
    movingTab_ = qobject_cast<QtTabbable*>(tabWidget->widget(oldIndex));
    SWIFT_LOG_ASSERT(movingTab_ != nullptr, error) << std::endl;

    if (movingTab_) {
        // Install event filter that filters out events issued during the internal movement of the
        // tab but not targeted at the moving tab.
        qApp->installEventFilter(this);

        tabWidget->tabBar()->moveTab(oldIndex, newIndex);

        qApp->removeEventFilter(this);
        SWIFT_LOG_ASSERT(movingTab_ == tabWidget->widget(newIndex), error) << std::endl;
    }
    movingTab_ = nullptr;
    tabWidget->widget(newIndex)->setFocus();
#else
#warning Qt 4.5 or later is needed. Trying anyway, some things will be disabled.
#endif
}

QtTabWidget* QtDynamicGridLayout::createDNDTabWidget(QWidget* parent) {
    QtTabWidget* tab = new QtTabWidget(parent);
    if (dndEnabled_) {
        QtDNDTabBar* tabBar = new QtDNDTabBar(tab);
        connect(tabBar, SIGNAL(onDropSucceeded()), this, SLOT(updateTabPositions()));
        tab->setTabBar(tabBar);
    }
    tab->setUsesScrollButtons(true);
    tab->setElideMode(Qt::ElideRight);
#if QT_VERSION >= 0x040500
    /*For Macs, change the tab rendering.*/
    tab->setDocumentMode(true);
    /*Closable tabs are only in Qt4.5 and later*/
    tab->setTabsClosable(true);
    tab->setMovable(true);
    connect(tab, SIGNAL(tabCloseRequested(int)), this, SLOT(handleTabCloseRequested(int)));
    connect(tab, SIGNAL(currentChanged(int)), this, SLOT(handleTabCurrentChanged(int)));
#else
#warning Qt 4.5 or later is needed. Trying anyway, some things will be disabled.
#endif
    return tab;
}

}