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("Item: "));
60 QLabel *descriptionLabel = new QLabel(tr("Description: "));
61 QLabel *imageFileLabel = new QLabel(tr("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(1));
72 imageFileEditor->setModelColumn(items->relationModel(1)->fieldIndex("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(imageFileEditor, 1);
81 mapper->addMapping(itemText, 2, "text");
82 mapper->addMapping(descriptionEditor, 3);
83 mapper->setCurrentIndex(id);
84//! [3]
85
86//! [4]
87 connect(descriptionEditor, &QTextEdit::textChanged, [=]() {
88 enableButtons();
89 });
90 connect(imageFileEditor, &QComboBox::currentIndexChanged, [=]() {
91 enableButtons();
92 });
93
94 QFormLayout *formLayout = new QFormLayout;
95 formLayout->addRow(itemLabel, itemText);
96 formLayout->addRow(imageFileLabel, imageFileEditor);
97 formLayout->addRow(descriptionLabel, descriptionEditor);
98
99 QVBoxLayout *layout = new QVBoxLayout;
100 layout->addLayout(formLayout);
101 layout->addWidget(buttonBox);
102 setLayout(layout);
103
104 itemId = id;
105 displayedImage = imageFileEditor->currentText();
106
107 setWindowFlags(Qt::Window);
108 enableButtons(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(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(itemId, newImage);
136 }
137
138 mapper->submit();
139 mapper->setCurrentIndex(itemId);
140
141 enableButtons(false);
142}
143//! [7]
144
145//! [8]
146void InformationWindow::createButtons()
147{
148 closeButton = new QPushButton(tr("&Close"));
149 revertButton = new QPushButton(tr("&Revert"));
150 submitButton = new QPushButton(tr("&Submit"));
151
152 closeButton->setDefault(true);
153
154 connect(closeButton, &QPushButton::clicked, this, &InformationWindow::close);
155 connect(revertButton, &QPushButton::clicked, this, &InformationWindow::revert);
156 connect(submitButton, &QPushButton::clicked, this, &InformationWindow::submit);
157//! [8]
158
159//! [9]
160 buttonBox = new QDialogButtonBox(this);
161 buttonBox->addButton(submitButton, QDialogButtonBox::AcceptRole);
162 buttonBox->addButton(revertButton, QDialogButtonBox::ResetRole);
163 buttonBox->addButton(closeButton, 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