| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the examples of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:BSD$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** BSD License Usage |
| 18 | ** Alternatively, you may use this file under the terms of the BSD license |
| 19 | ** as follows: |
| 20 | ** |
| 21 | ** "Redistribution and use in source and binary forms, with or without |
| 22 | ** modification, are permitted provided that the following conditions are |
| 23 | ** met: |
| 24 | ** * Redistributions of source code must retain the above copyright |
| 25 | ** notice, this list of conditions and the following disclaimer. |
| 26 | ** * Redistributions in binary form must reproduce the above copyright |
| 27 | ** notice, this list of conditions and the following disclaimer in |
| 28 | ** the documentation and/or other materials provided with the |
| 29 | ** distribution. |
| 30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 | ** contributors may be used to endorse or promote products derived |
| 32 | ** from this software without specific prior written permission. |
| 33 | ** |
| 34 | ** |
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 | ** |
| 47 | ** $QT_END_LICENSE$ |
| 48 | ** |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | #include <QComboBox> |
| 52 | #include <QVBoxLayout> |
| 53 | #include <QStackedWidget> |
| 54 | |
| 55 | #include "multipagewidget.h" |
| 56 | |
| 57 | MultiPageWidget::MultiPageWidget(QWidget *parent) |
| 58 | : QWidget(parent) |
| 59 | , stackWidget(new QStackedWidget) |
| 60 | , comboBox(new QComboBox) |
| 61 | { |
| 62 | comboBox->setObjectName(QStringLiteral("__qt__passive_comboBox" )); |
| 63 | |
| 64 | connect(sender: comboBox, signal: QOverload<int>::of(ptr: &QComboBox::activated), |
| 65 | receiver: this, slot: &MultiPageWidget::setCurrentIndex); |
| 66 | |
| 67 | QVBoxLayout *layout = new QVBoxLayout(this); |
| 68 | layout->addWidget(comboBox); |
| 69 | layout->addWidget(stackWidget); |
| 70 | } |
| 71 | |
| 72 | QSize MultiPageWidget::sizeHint() const |
| 73 | { |
| 74 | return QSize(200, 150); |
| 75 | } |
| 76 | |
| 77 | void MultiPageWidget::addPage(QWidget *page) |
| 78 | { |
| 79 | insertPage(index: count(), page); |
| 80 | } |
| 81 | |
| 82 | void MultiPageWidget::removePage(int index) |
| 83 | { |
| 84 | QWidget *widget = stackWidget->widget(index); |
| 85 | stackWidget->removeWidget(w: widget); |
| 86 | |
| 87 | comboBox->removeItem(index); |
| 88 | } |
| 89 | |
| 90 | int MultiPageWidget::count() const |
| 91 | { |
| 92 | return stackWidget->count(); |
| 93 | } |
| 94 | |
| 95 | int MultiPageWidget::currentIndex() const |
| 96 | { |
| 97 | return stackWidget->currentIndex(); |
| 98 | } |
| 99 | |
| 100 | void MultiPageWidget::insertPage(int index, QWidget *page) |
| 101 | { |
| 102 | page->setParent(stackWidget); |
| 103 | |
| 104 | stackWidget->insertWidget(index, w: page); |
| 105 | |
| 106 | QString title = page->windowTitle(); |
| 107 | if (title.isEmpty()) { |
| 108 | title = tr(s: "Page %1" ).arg(a: comboBox->count() + 1); |
| 109 | page->setWindowTitle(title); |
| 110 | } |
| 111 | connect(sender: page, signal: &QWidget::windowTitleChanged, |
| 112 | receiver: this, slot: &MultiPageWidget::pageWindowTitleChanged); |
| 113 | comboBox->insertItem(aindex: index, atext: title); |
| 114 | } |
| 115 | |
| 116 | void MultiPageWidget::setCurrentIndex(int index) |
| 117 | { |
| 118 | if (index != currentIndex()) { |
| 119 | stackWidget->setCurrentIndex(index); |
| 120 | comboBox->setCurrentIndex(index); |
| 121 | emit currentIndexChanged(index); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | void MultiPageWidget::pageWindowTitleChanged() |
| 126 | { |
| 127 | QWidget *page = qobject_cast<QWidget *>(o: sender()); |
| 128 | const int index = stackWidget->indexOf(page); |
| 129 | comboBox->setItemText(index, text: page->windowTitle()); |
| 130 | } |
| 131 | |
| 132 | QWidget* MultiPageWidget::widget(int index) |
| 133 | { |
| 134 | return stackWidget->widget(index); |
| 135 | } |
| 136 | |
| 137 | QString MultiPageWidget::pageTitle() const |
| 138 | { |
| 139 | if (const QWidget *currentWidget = stackWidget->currentWidget()) |
| 140 | return currentWidget->windowTitle(); |
| 141 | return QString(); |
| 142 | } |
| 143 | |
| 144 | void MultiPageWidget::setPageTitle(QString const &newTitle) |
| 145 | { |
| 146 | if (QWidget *currentWidget = stackWidget->currentWidget()) |
| 147 | currentWidget->setWindowTitle(newTitle); |
| 148 | emit pageTitleChanged(title: newTitle); |
| 149 | } |
| 150 | |