| 1 | /* |
| 2 | This file is part of the KDE Libraries |
| 3 | SPDX-FileCopyrightText: 2006 Tobias Koenig <tokoe@kde.org> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #include "kpagewidget.h" |
| 9 | #include "kpagewidget_p.h" |
| 10 | |
| 11 | #include "kpagewidgetmodel.h" |
| 12 | |
| 13 | KPageWidgetPrivate::KPageWidgetPrivate(KPageWidget *qq) |
| 14 | : KPageViewPrivate(qq) |
| 15 | { |
| 16 | } |
| 17 | |
| 18 | void KPageWidgetPrivate::slotCurrentPageChanged(const QModelIndex ¤t, const QModelIndex &before) |
| 19 | { |
| 20 | KPageWidgetItem *currentItem = nullptr; |
| 21 | if (current.isValid()) { |
| 22 | currentItem = model()->item(index: current); |
| 23 | } |
| 24 | |
| 25 | KPageWidgetItem *beforeItem = nullptr; |
| 26 | if (before.isValid()) { |
| 27 | beforeItem = model()->item(index: before); |
| 28 | } |
| 29 | |
| 30 | Q_Q(KPageWidget); |
| 31 | Q_EMIT q->currentPageChanged(current: currentItem, before: beforeItem); |
| 32 | } |
| 33 | |
| 34 | KPageWidget::KPageWidget(KPageWidgetPrivate &dd, QWidget *parent) |
| 35 | : KPageView(dd, parent) |
| 36 | { |
| 37 | Q_D(KPageWidget); |
| 38 | connect(sender: this, signal: &KPageView::currentPageChanged, context: this, slot: [d](const QModelIndex ¤t, const QModelIndex &before) { |
| 39 | d->slotCurrentPageChanged(current, before); |
| 40 | }); |
| 41 | |
| 42 | if (!d->KPageViewPrivate::model) { |
| 43 | setModel(new KPageWidgetModel(this)); |
| 44 | } else { |
| 45 | Q_ASSERT(qobject_cast<KPageWidgetModel *>(d->KPageViewPrivate::model)); |
| 46 | } |
| 47 | |
| 48 | connect(sender: d->model(), signal: &KPageWidgetModel::toggled, context: this, slot: &KPageWidget::pageToggled); |
| 49 | } |
| 50 | |
| 51 | KPageWidget::KPageWidget(QWidget *parent) |
| 52 | : KPageWidget(*new KPageWidgetPrivate(this), parent) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | KPageWidget::~KPageWidget() |
| 57 | { |
| 58 | } |
| 59 | |
| 60 | KPageWidgetItem *KPageWidget::addPage(QWidget *widget, const QString &name) |
| 61 | { |
| 62 | Q_D(KPageWidget); |
| 63 | |
| 64 | return d->model()->addPage(widget, name); |
| 65 | } |
| 66 | |
| 67 | void KPageWidget::addPage(KPageWidgetItem *item) |
| 68 | { |
| 69 | Q_D(KPageWidget); |
| 70 | |
| 71 | d->model()->addPage(item); |
| 72 | } |
| 73 | |
| 74 | KPageWidgetItem *KPageWidget::insertPage(KPageWidgetItem *before, QWidget *widget, const QString &name) |
| 75 | { |
| 76 | Q_D(KPageWidget); |
| 77 | |
| 78 | return d->model()->insertPage(before, widget, name); |
| 79 | } |
| 80 | |
| 81 | void KPageWidget::insertPage(KPageWidgetItem *before, KPageWidgetItem *item) |
| 82 | { |
| 83 | Q_D(KPageWidget); |
| 84 | |
| 85 | d->model()->insertPage(before, item); |
| 86 | } |
| 87 | |
| 88 | KPageWidgetItem *KPageWidget::addSubPage(KPageWidgetItem *parent, QWidget *widget, const QString &name) |
| 89 | { |
| 90 | Q_D(KPageWidget); |
| 91 | |
| 92 | return d->model()->addSubPage(parent, widget, name); |
| 93 | } |
| 94 | |
| 95 | void KPageWidget::addSubPage(KPageWidgetItem *parent, KPageWidgetItem *item) |
| 96 | { |
| 97 | Q_D(KPageWidget); |
| 98 | |
| 99 | d->model()->addSubPage(parent, item); |
| 100 | } |
| 101 | |
| 102 | void KPageWidget::removePage(KPageWidgetItem *item) |
| 103 | { |
| 104 | Q_D(KPageWidget); |
| 105 | |
| 106 | Q_EMIT pageRemoved(page: item); // Q_EMIT signal before we remove it, because the item will be deleted in the model |
| 107 | d->model()->removePage(item); |
| 108 | } |
| 109 | |
| 110 | void KPageWidget::setCurrentPage(KPageWidgetItem *item) |
| 111 | { |
| 112 | Q_D(KPageWidget); |
| 113 | |
| 114 | const QModelIndex index = d->model()->index(item); |
| 115 | if (!index.isValid()) { |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | KPageView::setCurrentPage(index); |
| 120 | } |
| 121 | |
| 122 | KPageWidgetItem *KPageWidget::currentPage() const |
| 123 | { |
| 124 | Q_D(const KPageWidget); |
| 125 | |
| 126 | const QModelIndex index = KPageView::currentPage(); |
| 127 | |
| 128 | if (!index.isValid()) { |
| 129 | return nullptr; |
| 130 | } |
| 131 | |
| 132 | return d->model()->item(index); |
| 133 | } |
| 134 | |
| 135 | #include "moc_kpagewidget.cpp" |
| 136 | |