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 "informationwindow.h"
52
53//! [0]
54InformationWindow::InformationWindow(int id, QSqlRelationalTableModel *items,
55 QWidget *parent)
56 : QDialog(parent)
57{
58//! [0] //! [1]
59 QLabel *itemLabel = new QLabel(tr(s: "Item: "));
60 QLabel *descriptionLabel = new QLabel(tr(s: "Description: "));
61 QLabel *imageFileLabel = new QLabel(tr(s: "Image file: "));
62
63 createButtons();
64
65 itemText = new QLabel;
66 descriptionEditor = new QTextEdit;
67//! [1]
68
69//! [2]
70 imageFileEditor = new QComboBox;
71 imageFileEditor->setModel(items->relationModel(column: 1));
72 imageFileEditor->setModelColumn(items->relationModel(column: 1)->fieldIndex(fieldName: "file"));
73//! [2]
74
75//! [3]
76 mapper = new QDataWidgetMapper(this);
77 mapper->setModel(items);
78 mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
79 mapper->setItemDelegate(new QSqlRelationalDelegate(mapper));
80 mapper->addMapping(widget: imageFileEditor, section: 1);
81 mapper->addMapping(widget: itemText, section: 2, propertyName: "text");
82 mapper->addMapping(widget: descriptionEditor, section: 3);
83 mapper->setCurrentIndex(id);
84//! [3]
85
86//! [4]
87 connect(sender: descriptionEditor, signal: &QTextEdit::textChanged, slot: [=]() {
88 enableButtons();
89 });
90 connect(sender: imageFileEditor, signal: QOverload<int>::of(ptr: &QComboBox::currentIndexChanged), slot: [=]() {
91 enableButtons();
92 });
93
94 QFormLayout *formLayout = new QFormLayout;
95 formLayout->addRow(label: itemLabel, field: itemText);
96 formLayout->addRow(label: imageFileLabel, field: imageFileEditor);
97 formLayout->addRow(label: descriptionLabel, field: descriptionEditor);
98
99 QVBoxLayout *layout = new QVBoxLayout;
100 layout->addLayout(layout: formLayout);
101 layout->addWidget(buttonBox);
102 setLayout(layout);
103
104 itemId = id;
105 displayedImage = imageFileEditor->currentText();
106
107 setWindowFlags(Qt::Window);
108 enableButtons(enable: false);
109 setWindowTitle(itemText->text());
110}
111//! [4]
112
113//! [5]
114int InformationWindow::id() const
115{
116 return itemId;
117}
118//! [5]
119
120//! [6]
121void InformationWindow::revert()
122{
123 mapper->revert();
124 enableButtons(enable: false);
125}
126//! [6]
127
128//! [7]
129void InformationWindow::submit()
130{
131 QString newImage(imageFileEditor->currentText());
132
133 if (displayedImage != newImage) {
134 displayedImage = newImage;
135 emit imageChanged(id: itemId, fileName: newImage);
136 }
137
138 mapper->submit();
139 mapper->setCurrentIndex(itemId);
140
141 enableButtons(enable: false);
142}
143//! [7]
144
145//! [8]
146void InformationWindow::createButtons()
147{
148 closeButton = new QPushButton(tr(s: "&Close"));
149 revertButton = new QPushButton(tr(s: "&Revert"));
150 submitButton = new QPushButton(tr(s: "&Submit"));
151
152 closeButton->setDefault(true);
153
154 connect(sender: closeButton, signal: &QPushButton::clicked, receiver: this, slot: &InformationWindow::close);
155 connect(sender: revertButton, signal: &QPushButton::clicked, receiver: this, slot: &InformationWindow::revert);
156 connect(sender: submitButton, signal: &QPushButton::clicked, receiver: this, slot: &InformationWindow::submit);
157//! [8]
158
159//! [9]
160 buttonBox = new QDialogButtonBox(this);
161 buttonBox->addButton(button: submitButton, role: QDialogButtonBox::AcceptRole);
162 buttonBox->addButton(button: revertButton, role: QDialogButtonBox::ResetRole);
163 buttonBox->addButton(button: closeButton, role: QDialogButtonBox::RejectRole);
164}
165//! [9]
166
167//! [10]
168void InformationWindow::enableButtons(bool enable)
169{
170 revertButton->setEnabled(enable);
171 submitButton->setEnabled(enable);
172}
173//! [10]
174
175
176

source code of qtbase/examples/sql/drilldown/informationwindow.cpp