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 Qt Designer of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
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** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include "orderdialog_p.h"
30#include "iconloader_p.h"
31#include "ui_orderdialog.h"
32
33#include <QtDesigner/qextensionmanager.h>
34#include <QtDesigner/abstractformeditor.h>
35#include <QtDesigner/container.h>
36#include <QtCore/qabstractitemmodel.h>
37#include <QtWidgets/qpushbutton.h>
38
39QT_BEGIN_NAMESPACE
40
41// OrderDialog: Used to reorder the pages of QStackedWidget and QToolBox.
42// Provides up and down buttons as well as DnD via QAbstractItemView::InternalMove mode
43namespace qdesigner_internal {
44
45OrderDialog::OrderDialog(QWidget *parent) :
46 QDialog(parent),
47 m_ui(new Ui::OrderDialog),
48 m_format(PageOrderFormat)
49{
50 m_ui->setupUi(this);
51 setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
52 m_ui->upButton->setIcon(createIconSet(name: QString::fromUtf8(str: "up.png")));
53 m_ui->downButton->setIcon(createIconSet(name: QString::fromUtf8(str: "down.png")));
54 m_ui->buttonBox->button(which: QDialogButtonBox::Ok)->setDefault(true);
55 connect(sender: m_ui->buttonBox->button(which: QDialogButtonBox::Reset), signal: &QAbstractButton::clicked,
56 receiver: this, slot: &OrderDialog::slotReset);
57 // Catch the remove operation of a DnD operation in QAbstractItemView::InternalMove mode to enable buttons
58 // Selection mode is 'contiguous' to enable DnD of groups
59 connect(sender: m_ui->pageList->model(), signal: &QAbstractItemModel::rowsRemoved,
60 receiver: this, slot: &OrderDialog::slotEnableButtonsAfterDnD);
61
62 m_ui->upButton->setEnabled(false);
63 m_ui->downButton->setEnabled(false);
64}
65
66OrderDialog::~OrderDialog()
67{
68 delete m_ui;
69}
70
71void OrderDialog::setDescription(const QString &d)
72{
73 m_ui->groupBox->setTitle(d);
74}
75
76void OrderDialog::setPageList(const QWidgetList &pages)
77{
78 // The QWidget* are stored in a map indexed by the old index.
79 // The old index is set as user data on the item instead of the QWidget*
80 // because DnD is enabled which requires the user data to serializable
81 m_orderMap.clear();
82 const int count = pages.count();
83 for (int i=0; i < count; ++i)
84 m_orderMap.insert(akey: i, avalue: pages.at(i));
85 buildList();
86}
87
88void OrderDialog::buildList()
89{
90 m_ui->pageList->clear();
91 const OrderMap::const_iterator cend = m_orderMap.constEnd();
92 for (OrderMap::const_iterator it = m_orderMap.constBegin(); it != cend; ++it) {
93 QListWidgetItem *item = new QListWidgetItem();
94 const int index = it.key();
95 switch (m_format) {
96 case PageOrderFormat:
97 item->setText(tr(s: "Index %1 (%2)").arg(a: index).arg(a: it.value()->objectName()));
98 break;
99 case TabOrderFormat:
100 item->setText(tr(s: "%1 %2").arg(a: index+1).arg(a: it.value()->objectName()));
101 break;
102 }
103 item->setData(role: Qt::UserRole, value: QVariant(index));
104 m_ui->pageList->addItem(aitem: item);
105 }
106
107 if (m_ui->pageList->count() > 0)
108 m_ui->pageList->setCurrentRow(0);
109}
110
111void OrderDialog::slotReset()
112{
113 buildList();
114}
115
116QWidgetList OrderDialog::pageList() const
117{
118 QWidgetList rc;
119 const int count = m_ui->pageList->count();
120 for (int i=0; i < count; ++i) {
121 const int oldIndex = m_ui->pageList->item(row: i)->data(role: Qt::UserRole).toInt();
122 rc.append(t: m_orderMap.value(akey: oldIndex));
123 }
124 return rc;
125}
126
127void OrderDialog::on_upButton_clicked()
128{
129 const int row = m_ui->pageList->currentRow();
130 if (row <= 0)
131 return;
132
133 m_ui->pageList->insertItem(row: row - 1, item: m_ui->pageList->takeItem(row));
134 m_ui->pageList->setCurrentRow(row - 1);
135}
136
137void OrderDialog::on_downButton_clicked()
138{
139 const int row = m_ui->pageList->currentRow();
140 if (row == -1 || row == m_ui->pageList->count() - 1)
141 return;
142
143 m_ui->pageList->insertItem(row: row + 1, item: m_ui->pageList->takeItem(row));
144 m_ui->pageList->setCurrentRow(row + 1);
145}
146
147void OrderDialog::slotEnableButtonsAfterDnD()
148{
149 enableButtons(r: m_ui->pageList->currentRow());
150}
151
152void OrderDialog::on_pageList_currentRowChanged(int r)
153{
154 enableButtons(r);
155}
156
157void OrderDialog::enableButtons(int r)
158{
159 m_ui->upButton->setEnabled(r > 0);
160 m_ui->downButton->setEnabled(r >= 0 && r < m_ui->pageList->count() - 1);
161}
162
163QWidgetList OrderDialog::pagesOfContainer(const QDesignerFormEditorInterface *core, QWidget *container)
164{
165 QWidgetList rc;
166 if (QDesignerContainerExtension* ce = qt_extension<QDesignerContainerExtension*>(manager: core->extensionManager(), object: container)) {
167 const int count = ce->count();
168 for (int i = 0; i < count ;i ++)
169 rc.push_back(t: ce->widget(index: i));
170 }
171 return rc;
172}
173
174}
175
176QT_END_NAMESPACE
177

source code of qttools/src/designer/src/lib/shared/orderdialog.cpp