[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
[2] | 11 | **
|
---|
[846] | 12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
| 13 | ** modification, are permitted provided that the following conditions are
|
---|
| 14 | ** met:
|
---|
| 15 | ** * Redistributions of source code must retain the above copyright
|
---|
| 16 | ** notice, this list of conditions and the following disclaimer.
|
---|
| 17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
| 18 | ** notice, this list of conditions and the following disclaimer in
|
---|
| 19 | ** the documentation and/or other materials provided with the
|
---|
| 20 | ** distribution.
|
---|
| 21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
| 22 | ** the names of its contributors may be used to endorse or promote
|
---|
| 23 | ** products derived from this software without specific prior written
|
---|
| 24 | ** permission.
|
---|
[2] | 25 | **
|
---|
[846] | 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
[2] | 37 | ** $QT_END_LICENSE$
|
---|
| 38 | **
|
---|
| 39 | ****************************************************************************/
|
---|
| 40 |
|
---|
| 41 | #include "dialog.h"
|
---|
| 42 |
|
---|
| 43 | int uniqueAlbumId;
|
---|
| 44 | int uniqueArtistId;
|
---|
| 45 |
|
---|
| 46 | Dialog::Dialog(QSqlRelationalTableModel *albums, QDomDocument details,
|
---|
| 47 | QFile *output, QWidget *parent)
|
---|
| 48 | : QDialog(parent)
|
---|
| 49 | {
|
---|
| 50 | model = albums;
|
---|
| 51 | albumDetails = details;
|
---|
| 52 | outputFile = output;
|
---|
| 53 |
|
---|
| 54 | QGroupBox *inputWidgetBox = createInputWidgets();
|
---|
| 55 | QDialogButtonBox *buttonBox = createButtons();
|
---|
| 56 |
|
---|
| 57 | QVBoxLayout *layout = new QVBoxLayout;
|
---|
| 58 | layout->addWidget(inputWidgetBox);
|
---|
| 59 | layout->addWidget(buttonBox);
|
---|
| 60 | setLayout(layout);
|
---|
| 61 |
|
---|
| 62 | setWindowTitle(tr("Add Album"));
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | void Dialog::submit()
|
---|
| 66 | {
|
---|
| 67 | QString artist = artistEditor->text();
|
---|
| 68 | QString title = titleEditor->text();
|
---|
| 69 |
|
---|
| 70 | if (artist.isEmpty() || title.isEmpty()) {
|
---|
| 71 | QString message(tr("Please provide both the name of the artist "
|
---|
| 72 | "and the title of the album."));
|
---|
| 73 | QMessageBox::information(this, tr("Add Album"), message);
|
---|
| 74 | } else {
|
---|
| 75 | int artistId = findArtistId(artist);
|
---|
| 76 | int albumId = addNewAlbum(title, artistId);
|
---|
| 77 |
|
---|
| 78 | QStringList tracks;
|
---|
| 79 | tracks = tracksEditor->text().split(',', QString::SkipEmptyParts);
|
---|
| 80 | addTracks(albumId, tracks);
|
---|
| 81 |
|
---|
| 82 | increaseAlbumCount(indexOfArtist(artist));
|
---|
| 83 | accept();
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | int Dialog::findArtistId(const QString &artist)
|
---|
| 88 | {
|
---|
| 89 | QSqlTableModel *artistModel = model->relationModel(2);
|
---|
| 90 | int row = 0;
|
---|
| 91 |
|
---|
| 92 | while (row < artistModel->rowCount()) {
|
---|
| 93 | QSqlRecord record = artistModel->record(row);
|
---|
| 94 | if (record.value("artist") == artist)
|
---|
| 95 | return record.value("id").toInt();
|
---|
| 96 | else
|
---|
| 97 | row++;
|
---|
| 98 | }
|
---|
| 99 | return addNewArtist(artist);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 |
|
---|
| 103 | int Dialog::addNewArtist(const QString &name)
|
---|
| 104 | {
|
---|
| 105 | QSqlTableModel *artistModel = model->relationModel(2);
|
---|
| 106 | QSqlRecord record;
|
---|
| 107 |
|
---|
| 108 | int id = generateArtistId();
|
---|
| 109 |
|
---|
| 110 | QSqlField f1("id", QVariant::Int);
|
---|
| 111 | QSqlField f2("artist", QVariant::String);
|
---|
| 112 | QSqlField f3("albumcount", QVariant::Int);
|
---|
| 113 |
|
---|
| 114 | f1.setValue(QVariant(id));
|
---|
| 115 | f2.setValue(QVariant(name));
|
---|
| 116 | f3.setValue(QVariant(0));
|
---|
| 117 | record.append(f1);
|
---|
| 118 | record.append(f2);
|
---|
| 119 | record.append(f3);
|
---|
| 120 |
|
---|
| 121 | artistModel->insertRecord(-1, record);
|
---|
| 122 | return id;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | int Dialog::addNewAlbum(const QString &title, int artistId)
|
---|
| 126 | {
|
---|
| 127 | int id = generateAlbumId();
|
---|
| 128 | QSqlRecord record;
|
---|
| 129 |
|
---|
| 130 | QSqlField f1("albumid", QVariant::Int);
|
---|
| 131 | QSqlField f2("title", QVariant::String);
|
---|
| 132 | QSqlField f3("artistid", QVariant::Int);
|
---|
| 133 | QSqlField f4("year", QVariant::Int);
|
---|
| 134 |
|
---|
| 135 | f1.setValue(QVariant(id));
|
---|
| 136 | f2.setValue(QVariant(title));
|
---|
| 137 | f3.setValue(QVariant(artistId));
|
---|
| 138 | f4.setValue(QVariant(yearEditor->value()));
|
---|
| 139 | record.append(f1);
|
---|
| 140 | record.append(f2);
|
---|
| 141 | record.append(f3);
|
---|
| 142 | record.append(f4);
|
---|
| 143 |
|
---|
| 144 | model->insertRecord(-1, record);
|
---|
| 145 | return id;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | void Dialog::addTracks(int albumId, QStringList tracks)
|
---|
| 149 | {
|
---|
| 150 | QDomElement albumNode = albumDetails.createElement("album");
|
---|
| 151 | albumNode.setAttribute("id", albumId);
|
---|
| 152 |
|
---|
| 153 | for (int i = 0; i < tracks.count(); i++) {
|
---|
| 154 | QString trackNumber = QString::number(i);
|
---|
| 155 | if (i < 10)
|
---|
| 156 | trackNumber.prepend("0");
|
---|
| 157 |
|
---|
| 158 | QDomText textNode = albumDetails.createTextNode(tracks.at(i));
|
---|
| 159 |
|
---|
| 160 | QDomElement trackNode = albumDetails.createElement("track");
|
---|
| 161 | trackNode.setAttribute("number", trackNumber);
|
---|
| 162 | trackNode.appendChild(textNode);
|
---|
| 163 |
|
---|
| 164 | albumNode.appendChild(trackNode);
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | QDomNodeList archive = albumDetails.elementsByTagName("archive");
|
---|
| 168 | archive.item(0).appendChild(albumNode);
|
---|
| 169 |
|
---|
| 170 | /*
|
---|
| 171 | The following code is commented out since the example uses an in
|
---|
| 172 | memory database, i.e., altering the XML file will bring the data
|
---|
| 173 | out of sync.
|
---|
| 174 |
|
---|
| 175 | if (!outputFile->open(QIODevice::WriteOnly)) {
|
---|
| 176 | return;
|
---|
| 177 | } else {
|
---|
| 178 | QTextStream stream(outputFile);
|
---|
| 179 | archive.item(0).save(stream, 4);
|
---|
| 180 | outputFile->close();
|
---|
| 181 | }
|
---|
| 182 | */
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | void Dialog::increaseAlbumCount(QModelIndex artistIndex)
|
---|
| 186 | {
|
---|
| 187 | QSqlTableModel *artistModel = model->relationModel(2);
|
---|
| 188 |
|
---|
| 189 | QModelIndex albumCountIndex;
|
---|
| 190 | albumCountIndex = artistIndex.sibling(artistIndex.row(), 2);
|
---|
| 191 |
|
---|
| 192 | int albumCount = albumCountIndex.data().toInt();
|
---|
| 193 | artistModel->setData(albumCountIndex, QVariant(albumCount + 1));
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 |
|
---|
| 197 | void Dialog::revert()
|
---|
| 198 | {
|
---|
| 199 | artistEditor->clear();
|
---|
| 200 | titleEditor->clear();
|
---|
| 201 | yearEditor->setValue(QDate::currentDate().year());
|
---|
| 202 | tracksEditor->clear();
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | QGroupBox *Dialog::createInputWidgets()
|
---|
| 206 | {
|
---|
| 207 | QGroupBox *box = new QGroupBox(tr("Add Album"));
|
---|
| 208 |
|
---|
| 209 | QLabel *artistLabel = new QLabel(tr("Artist:"));
|
---|
| 210 | QLabel *titleLabel = new QLabel(tr("Title:"));
|
---|
| 211 | QLabel *yearLabel = new QLabel(tr("Year:"));
|
---|
| 212 | QLabel *tracksLabel = new QLabel(tr("Tracks (separated by comma):"));
|
---|
| 213 |
|
---|
| 214 | artistEditor = new QLineEdit;
|
---|
| 215 | titleEditor = new QLineEdit;
|
---|
| 216 |
|
---|
| 217 | yearEditor = new QSpinBox;
|
---|
| 218 | yearEditor->setMinimum(1900);
|
---|
| 219 | yearEditor->setMaximum(QDate::currentDate().year());
|
---|
| 220 | yearEditor->setValue(yearEditor->maximum());
|
---|
| 221 | yearEditor->setReadOnly(false);
|
---|
| 222 |
|
---|
| 223 | tracksEditor = new QLineEdit;
|
---|
| 224 |
|
---|
| 225 | QGridLayout *layout = new QGridLayout;
|
---|
| 226 | layout->addWidget(artistLabel, 0, 0);
|
---|
| 227 | layout->addWidget(artistEditor, 0, 1);
|
---|
| 228 | layout->addWidget(titleLabel, 1, 0);
|
---|
| 229 | layout->addWidget(titleEditor, 1, 1);
|
---|
| 230 | layout->addWidget(yearLabel, 2, 0);
|
---|
| 231 | layout->addWidget(yearEditor, 2, 1);
|
---|
| 232 | layout->addWidget(tracksLabel, 3, 0, 1, 2);
|
---|
| 233 | layout->addWidget(tracksEditor, 4, 0, 1, 2);
|
---|
| 234 | box->setLayout(layout);
|
---|
| 235 |
|
---|
| 236 | return box;
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | QDialogButtonBox *Dialog::createButtons()
|
---|
| 240 | {
|
---|
| 241 | QPushButton *closeButton = new QPushButton(tr("&Close"));
|
---|
| 242 | QPushButton *revertButton = new QPushButton(tr("&Revert"));
|
---|
| 243 | QPushButton *submitButton = new QPushButton(tr("&Submit"));
|
---|
| 244 |
|
---|
| 245 | closeButton->setDefault(true);
|
---|
| 246 |
|
---|
| 247 | connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
|
---|
| 248 | connect(revertButton, SIGNAL(clicked()), this, SLOT(revert()));
|
---|
| 249 | connect(submitButton, SIGNAL(clicked()), this, SLOT(submit()));
|
---|
| 250 |
|
---|
| 251 | QDialogButtonBox *buttonBox = new QDialogButtonBox;
|
---|
| 252 | buttonBox->addButton(submitButton, QDialogButtonBox::ResetRole);
|
---|
| 253 | buttonBox->addButton(revertButton, QDialogButtonBox::ResetRole);
|
---|
| 254 | buttonBox->addButton(closeButton, QDialogButtonBox::RejectRole);
|
---|
| 255 |
|
---|
| 256 | return buttonBox;
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | QModelIndex Dialog::indexOfArtist(const QString &artist)
|
---|
| 260 | {
|
---|
| 261 | QSqlTableModel *artistModel = model->relationModel(2);
|
---|
| 262 |
|
---|
| 263 | for (int i = 0; i < artistModel->rowCount(); i++) {
|
---|
| 264 | QSqlRecord record = artistModel->record(i);
|
---|
| 265 | if (record.value("artist") == artist)
|
---|
| 266 | return artistModel->index(i, 1);
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | return QModelIndex();
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | int Dialog::generateArtistId()
|
---|
| 273 | {
|
---|
| 274 | uniqueArtistId += 1;
|
---|
| 275 | return uniqueArtistId;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | int Dialog::generateAlbumId()
|
---|
| 279 | {
|
---|
| 280 | uniqueAlbumId += 1;
|
---|
| 281 | return uniqueAlbumId;
|
---|
| 282 | }
|
---|