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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2009-11-03
 * Description : A dialog base class which can handle multiple pages.
 *
 * SPDX-FileCopyrightText: 2009-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
 * SPDX-FileCopyrightText: 2007      by Rafael Fernández López <ereslibre at kde dot org>
 * SPDX-FileCopyrightText: 2006      by Tobias Koenig <tokoe at kde dot org>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

#include "dconfigdlgview_p.h"

// Qt includes

#include <QApplication>
#include <QHeaderView>
#include <QPainter>
#include <QTextLayout>
#include <QVBoxLayout>

// Local includes

#include "dconfigdlgmodels.h"

namespace Digikam
{

using namespace DConfigDlgInternal;

DConfigDlgPlainView::DConfigDlgPlainView(QWidget* const parent)
    : QAbstractItemView(parent)
{
    hide();
}

QModelIndex DConfigDlgPlainView::indexAt(const QPoint&) const
{
    return QModelIndex();
}

void DConfigDlgPlainView::scrollTo(const QModelIndex&, ScrollHint)
{
}

QRect DConfigDlgPlainView::visualRect(const QModelIndex&) const
{
    return QRect();
}

QModelIndex DConfigDlgPlainView::moveCursor(QAbstractItemView::CursorAction, Qt::KeyboardModifiers)
{
    return QModelIndex();
}

int DConfigDlgPlainView::horizontalOffset() const
{
    return 0;
}

int DConfigDlgPlainView::verticalOffset() const
{
    return 0;
}

bool DConfigDlgPlainView::isIndexHidden(const QModelIndex&) const
{
    return false;
}

void DConfigDlgPlainView::setSelection(const QRect&, QFlags<QItemSelectionModel::SelectionFlag>)
{
}

QRegion DConfigDlgPlainView::visualRegionForSelection(const QItemSelection&) const
{
    return QRegion();
}

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

DConfigDlgListView::DConfigDlgListView(QWidget* const parent)
    : QListView(parent)
{
    setViewMode(QListView::ListMode);
    setMovement(QListView::Static);
    setVerticalScrollMode(QListView::ScrollPerPixel);

    QFont boldFont(font());
    boldFont.setBold(true);
    setFont(boldFont);

    setItemDelegate(new DConfigDlgListViewDelegate(this));
}

void DConfigDlgListView::setModel(QAbstractItemModel* model)
{
/*
      DConfigDlgListViewProxy* const proxy = new DConfigDlgListViewProxy( this );
      proxy->setSourceModel( model );
      proxy->rebuildMap();

      connect(model, SIGNAL(layoutChanged()),
              proxy, SLOT(rebuildMap()) );
*/

    connect(model, &QAbstractItemModel::layoutChanged,
            this, &DConfigDlgListView::updateWidth);

/*
    QListView::setModel(proxy);
*/
    QListView::setModel(model);

    // Set our own selection model, which won't allow our current selection to be cleared

    setSelectionModel(new DConfigDlgInternal::SelectionModel(model, this));

    updateWidth();
}

void DConfigDlgListView::updateWidth()
{
    if (!model())
    {
        return;
    }

    int rows = model()->rowCount();

    int width = 0;

    for (int i = 0 ; i < rows ; ++i)
    {
        width = qMax(width, sizeHintForIndex(model()->index(i, 0)).width());
    }

    setFixedWidth(width + 25);
}

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

DConfigDlgTreeView::DConfigDlgTreeView(QWidget* const parent)
    : QTreeView(parent)
{
    setUniformRowHeights(true);
    header()->hide();
}

void DConfigDlgTreeView::setModel(QAbstractItemModel* model)
{
    connect(model, &QAbstractItemModel::layoutChanged,
            this, &DConfigDlgTreeView::updateWidth);

    QTreeView::setModel(model);

    // Set our own selection model, which won't allow our current selection to be cleared

    setSelectionModel(new DConfigDlgInternal::SelectionModel(model, this));

    updateWidth();
}

void DConfigDlgTreeView::updateWidth()
{
    if (!model())
    {
        return;
    }

    int columns = model()->columnCount();

    expandItems();

    int width = 0;

    for (int i = 0 ; i < columns ; ++i)
    {
        resizeColumnToContents(i);
        width = qMax(width, sizeHintForColumn(i));
    }

    setFixedWidth(width + 25);
}

void DConfigDlgTreeView::expandItems(const QModelIndex& index)
{
    setExpanded(index, true);

    const int count = model()->rowCount(index);

    for (int i = 0 ; i < count ; ++i)
    {
        expandItems(model()->index(i, 0, index));
    }
}

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

DConfigDlgTabbedView::DConfigDlgTabbedView(QWidget* const parent)
    : QAbstractItemView(parent)
{
    // hide the viewport of the QAbstractScrollArea

    const QList<QWidget *> list = findChildren<QWidget *>();

    for (int i = 0 ; i < list.count() ; ++i)
    {
        list[i]->hide();
    }

    setFrameShape(NoFrame);

    QVBoxLayout* const layout = new QVBoxLayout(this);
    layout->setContentsMargins(QMargins());

    mTabWidget = new QTabWidget(this);

    connect(mTabWidget, &QTabWidget::currentChanged, this,
            &DConfigDlgTabbedView::currentPageChanged);

    layout->addWidget(mTabWidget);
}

DConfigDlgTabbedView::~DConfigDlgTabbedView()
{
    if (model())
    {
        for (int i = 0 ; i < mTabWidget->count() ; ++i)
        {
            QWidget* const page = qvariant_cast<QWidget *>(model()->data(model()->index(i, 0), DConfigDlgModel::WidgetRole));

            if (page)
            {
                page->setVisible(false);
                page->setParent(nullptr); // reparent our children before they are deleted
            }
        }
    }
}

void DConfigDlgTabbedView::setModel(QAbstractItemModel* model)
{
    QAbstractItemView::setModel(model);

    connect(model, &QAbstractItemModel::layoutChanged, this,
            &DConfigDlgTabbedView::layoutChanged);

    layoutChanged();
}

QModelIndex DConfigDlgTabbedView::indexAt(const QPoint&) const
{
    if (model())
    {
        return model()->index(0, 0);
    }
    else
    {
        return QModelIndex();
    }
}

void DConfigDlgTabbedView::scrollTo(const QModelIndex& index, ScrollHint)
{
    if (!index.isValid())
    {
        return;
    }

    mTabWidget->setCurrentIndex(index.row());
}

QRect DConfigDlgTabbedView::visualRect(const QModelIndex&) const
{
    return QRect();
}

QSize DConfigDlgTabbedView::minimumSizeHint() const
{
    return mTabWidget->minimumSizeHint();
}

QModelIndex DConfigDlgTabbedView::moveCursor(QAbstractItemView::CursorAction, Qt::KeyboardModifiers)
{
    return QModelIndex();
}

int DConfigDlgTabbedView::horizontalOffset() const
{
    return 0;
}

int DConfigDlgTabbedView::verticalOffset() const
{
    return 0;
}

bool DConfigDlgTabbedView::isIndexHidden(const QModelIndex& index) const
{
    return (mTabWidget->currentIndex() != index.row());
}

void DConfigDlgTabbedView::setSelection(const QRect& , QFlags<QItemSelectionModel::SelectionFlag>)
{
}

QRegion DConfigDlgTabbedView::visualRegionForSelection(const QItemSelection&) const
{
    return QRegion();
}

void DConfigDlgTabbedView::currentPageChanged(int index)
{
    if (!model())
    {
        return;
    }

    QModelIndex modelIndex = model()->index(index, 0);

    selectionModel()->setCurrentIndex(modelIndex, QItemSelectionModel::ClearAndSelect);
}

void DConfigDlgTabbedView::layoutChanged()
{
    // save old position

    int pos = mTabWidget->currentIndex();

    // clear tab bar

    int count = mTabWidget->count();

    for (int i = 0 ; i < count ; ++i)
    {
        mTabWidget->removeTab(0);
    }

    if (!model())
    {
        return;
    }

    // add new tabs

    for (int i = 0 ; i < model()->rowCount() ; ++i)
    {
        const QString title = model()->data(model()->index(i, 0)).toString();
        const QIcon icon    = model()->data(model()->index(i, 0), Qt::DecorationRole).value<QIcon>();
        QWidget* const page = qvariant_cast<QWidget *>(model()->data(model()->index(i, 0), DConfigDlgModel::WidgetRole));

        if (page)
        {
            QWidget* const widget     = new QWidget(this);
            QVBoxLayout* const layout = new QVBoxLayout(widget);
            widget->setLayout(layout);
            layout->addWidget(page);
            page->setVisible(true);
            mTabWidget->addTab(widget, icon, title);
        }
    }

    mTabWidget->setCurrentIndex(pos);
}

void DConfigDlgTabbedView::dataChanged(const QModelIndex& index, const QModelIndex&, const QVector<int>& roles)
{
    if (!index.isValid())
    {
        return;
    }

    if ((index.row() < 0) || (index.row() >= mTabWidget->count()))
    {
        return;
    }

    if (roles.isEmpty() || roles.contains(Qt::DisplayRole) || roles.contains(Qt::DecorationRole))
    {
        const QString title = model()->data(index).toString();
        const QIcon icon    = model()->data(index, Qt::DecorationRole).value<QIcon>();

        mTabWidget->setTabText(index.row(), title);
        mTabWidget->setTabIcon(index.row(), icon);
    }
}

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

DConfigDlgListViewDelegate::DConfigDlgListViewDelegate(QObject* const parent)
    : QAbstractItemDelegate(parent)
{
}

static int layoutText(QTextLayout* layout, int maxWidth)
{
    qreal height  = 0;
    int textWidth = 0;
    layout->beginLayout();

    while (true)
    {
        QTextLine line = layout->createLine();

        if (!line.isValid())
        {
            break;
        }

        line.setLineWidth(maxWidth);
        line.setPosition(QPointF(0, height));
        height    += line.height();
        textWidth  = qMax(textWidth, qRound(line.naturalTextWidth() + 0.5));
    }

    layout->endLayout();

    return textWidth;
}

void DConfigDlgListViewDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    if (!index.isValid())
    {
        return;
    }

    QStyleOptionViewItem opt(option);
    opt.showDecorationSelected = true;
    QStyle* const style        = opt.widget ? opt.widget->style() : QApplication::style();

    int iconSize               = style->pixelMetric(QStyle::PM_IconViewIconSize);
    const QString text         = index.model()->data(index, Qt::DisplayRole).toString();
    const QIcon icon           = index.model()->data(index, Qt::DecorationRole).value<QIcon>();
    const QPixmap pixmap       = icon.pixmap(iconSize, iconSize);

    QFontMetrics fm            = painter->fontMetrics();
    int wp                     = pixmap.width()  / pixmap.devicePixelRatio();
    int hp                     = pixmap.height() / pixmap.devicePixelRatio();

    QTextLayout iconTextLayout(text, option.font);
    QTextOption textOption(Qt::AlignHCenter);
    iconTextLayout.setTextOption(textOption);
    int maxWidth               = qMax(3 * wp, 8 * fm.height());
    layoutText(&iconTextLayout, maxWidth);

    QPen pen                   = painter->pen();
    QPalette::ColorGroup cg    = (option.state & QStyle::State_Enabled) ? QPalette::Normal
                                                                        : QPalette::Disabled;

    if ((cg == QPalette::Normal) && !(option.state & QStyle::State_Active))
    {
        cg = QPalette::Inactive;
    }

    style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget);

    if (option.state & QStyle::State_Selected)
    {
        painter->setPen(option.palette.color(cg, QPalette::HighlightedText));
    }
    else
    {
        painter->setPen(option.palette.color(cg, QPalette::Text));
    }

    painter->drawPixmap(option.rect.x() + (option.rect.width() / 2) - (wp / 2), option.rect.y() + 5, pixmap);

    if (!text.isEmpty())
    {
        iconTextLayout.draw(painter, QPoint(option.rect.x() + (option.rect.width() / 2) - (maxWidth / 2), option.rect.y() + hp + 7));
    }

    painter->setPen(pen);

    drawFocus(painter, option, option.rect);
}

QSize DConfigDlgListViewDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    if (!index.isValid())
    {
        return QSize(0, 0);
    }

    QStyleOptionViewItem opt(option);
    opt.showDecorationSelected = true;
    QStyle* const style        = opt.widget ? opt.widget->style() : QApplication::style();

    int iconSize               = style->pixelMetric(QStyle::PM_IconViewIconSize);
    const QString text         = index.model()->data(index, Qt::DisplayRole).toString();
    const QIcon icon           = index.model()->data(index, Qt::DecorationRole).value<QIcon>();
    const QPixmap pixmap       = icon.pixmap(iconSize, iconSize);

    QFontMetrics fm            = option.fontMetrics;
    int gap                    = fm.height();
    int wp                     = pixmap.width() / pixmap.devicePixelRatio();
    int hp                     = pixmap.height() / pixmap.devicePixelRatio();

    if (hp == 0)
    {
        /**
         * No pixmap loaded yet, we'll use the default icon size in this case.
         */
        hp = iconSize;
        wp = iconSize;
    }

    QTextLayout iconTextLayout(text, option.font);
    int wt = layoutText(&iconTextLayout, qMax(3 * wp, 8 * fm.height()));
    int ht = iconTextLayout.boundingRect().height();

    int width  = 0;
    int height = 0;

    if (text.isEmpty())
    {
        height = hp;
    }
    else
    {
        height = hp + ht + 10;
    }

    width = qMax(wt, wp) + gap;

    return QSize(width, height);
}

void DConfigDlgListViewDelegate::drawFocus(QPainter* painter, const QStyleOptionViewItem& option, const QRect& rect) const
{
    if (option.state & QStyle::State_HasFocus)
    {
        QStyleOptionFocusRect o;

        o.QStyleOption::operator = (option);
        o.rect                   = rect;
        o.state                 |= QStyle::State_KeyboardFocusChange;
        QPalette::ColorGroup cg  = (option.state & QStyle::State_Enabled) ? QPalette::Normal
                                                                          : QPalette::Disabled;
        o.backgroundColor        = option.palette.color(
                                                        cg, (option.state & QStyle::State_Selected) ? QPalette::Highlight
                                                                                                    : QPalette::Window
                                                       );

        QApplication::style()->drawPrimitive(QStyle::PE_FrameFocusRect, &o, painter);
    }
}

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

DConfigDlgListViewProxy::DConfigDlgListViewProxy(QObject* const parent)
    : QAbstractProxyModel(parent)
{
}

int DConfigDlgListViewProxy::rowCount(const QModelIndex&) const
{
    return mList.count();
}

int DConfigDlgListViewProxy::columnCount(const QModelIndex&) const
{
    return 1;
}

QModelIndex DConfigDlgListViewProxy::index(int row, int column, const QModelIndex&) const
{
    if ((column > 1) || (row >= mList.count()))
    {
        return QModelIndex();
    }
    else
    {
        return createIndex(row, column, mList[ row ].internalPointer());
    }
}

QModelIndex DConfigDlgListViewProxy::parent(const QModelIndex&) const
{
    return QModelIndex();
}

QVariant DConfigDlgListViewProxy::data(const QModelIndex& index, int role) const<--- Shadow argument
{
    if (!index.isValid())
    {
        return QVariant();
    }

    if (index.row() >= mList.count())
    {
        return QVariant();
    }

    return sourceModel()->data(mList[ index.row() ], role);
}

QModelIndex DConfigDlgListViewProxy::mapFromSource(const QModelIndex& index) const<--- Shadow argument
{
    if (!index.isValid())
    {
        return QModelIndex();
    }

    for (int i = 0 ; i < mList.count() ; ++i)
    {
        if (mList[i] == index)
        {
            return createIndex(i, 0, index.internalPointer());
        }
    }

    return QModelIndex();
}

QModelIndex DConfigDlgListViewProxy::mapToSource(const QModelIndex& index) const<--- Shadow argument
{
    if (!index.isValid())
    {
        return QModelIndex();
    }

    return mList[index.row()];
}

void DConfigDlgListViewProxy::rebuildMap()
{
    mList.clear();

    const QAbstractItemModel* model = sourceModel();

    if (!model)
    {
        return;
    }

    for (int i = 0 ; i < model->rowCount() ; ++i)
    {
        addMapEntry(model->index(i, 0));
    }

    for (int i = 0 ; i < mList.count() ; ++i)
    {
        qDebug("%d:0 -> %d:%d", i, mList[ i ].row(), mList[ i ].column());
    }

    Q_EMIT layoutChanged();
}

void DConfigDlgListViewProxy::addMapEntry(const QModelIndex& index)<--- Shadow argument
{
    if (sourceModel()->rowCount(index) == 0)
    {
        mList.append(index);
    }
    else
    {
        const int count = sourceModel()->rowCount(index);

        for (int i = 0 ; i < count ; ++i)
        {
            addMapEntry(sourceModel()->index(i, 0, index));
        }
    }
}

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

SelectionModel::SelectionModel(QAbstractItemModel* const model, QObject* const parent)
    : QItemSelectionModel(model, parent)
{
}

void SelectionModel::clear()
{
    // Don't allow the current selection to be cleared
}

void SelectionModel::select(const QModelIndex& index, QItemSelectionModel::SelectionFlags command)
{
    // Don't allow the current selection to be cleared

    if (!index.isValid() && (command & QItemSelectionModel::Clear))
    {
        return;
    }

    QItemSelectionModel::select(index, command);
}

void SelectionModel::select(const QItemSelection& selection, QItemSelectionModel::SelectionFlags command)
{
    // Don't allow the current selection to be cleared

    if (!selection.count() && (command & QItemSelectionModel::Clear))
    {
        return;
    }

    QItemSelectionModel::select(selection, command);
}

} // namespace Digikam

#include "moc_dconfigdlgview_p.cpp"