tests/qtp/qtp_masterdetail/dialog.cpp
changeset 0 1918ee327afb
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "dialog.h"
       
    43 
       
    44 int uniqueAlbumId;
       
    45 int uniqueArtistId;
       
    46 
       
    47 Dialog::Dialog(QSqlRelationalTableModel *albums, QDomDocument details,
       
    48                QFile *output, QWidget *parent)
       
    49      : QDialog(parent)
       
    50 {
       
    51     model = albums;
       
    52     albumDetails = details;
       
    53     outputFile = output;
       
    54 
       
    55     QGroupBox *inputWidgetBox = createInputWidgets();
       
    56     QDialogButtonBox *buttonBox = createButtons();
       
    57 
       
    58     QVBoxLayout *layout = new QVBoxLayout;
       
    59     layout->addWidget(inputWidgetBox);
       
    60     layout->addWidget(buttonBox);
       
    61     setLayout(layout);
       
    62 
       
    63     setWindowTitle(tr("Add Album"));
       
    64 }
       
    65 
       
    66 void Dialog::submit()
       
    67 {
       
    68     QString artist = artistEditor->text();
       
    69     QString title = titleEditor->text();
       
    70 
       
    71     if (artist.isEmpty() || title.isEmpty()) {
       
    72         QString message(tr("Please provide both the name of the artist "
       
    73                            "and the title of the album."));
       
    74         QMessageBox::information(this, tr("Add Album"), message);
       
    75     } else {
       
    76         int artistId = findArtistId(artist);
       
    77         int albumId = addNewAlbum(title, artistId);
       
    78 
       
    79         QStringList tracks;
       
    80         tracks = tracksEditor->text().split(',', QString::SkipEmptyParts);
       
    81         addTracks(albumId, tracks);
       
    82 
       
    83         increaseAlbumCount(indexOfArtist(artist));
       
    84         accept();
       
    85     }
       
    86 }
       
    87 
       
    88 int Dialog::findArtistId(const QString &artist)
       
    89 {
       
    90     QSqlTableModel *artistModel = model->relationModel(2);
       
    91     int row = 0;
       
    92 
       
    93     while (row < artistModel->rowCount()) {
       
    94         QSqlRecord record = artistModel->record(row);
       
    95         if (record.value("artist") == artist)
       
    96             return record.value("id").toInt();
       
    97         else
       
    98             row++;
       
    99     }
       
   100     return addNewArtist(artist);
       
   101 }
       
   102 
       
   103 
       
   104 int Dialog::addNewArtist(const QString &name)
       
   105 {
       
   106     QSqlTableModel *artistModel = model->relationModel(2);
       
   107     QSqlRecord record;
       
   108 
       
   109     int id = generateArtistId();
       
   110 
       
   111     QSqlField f1("id", QVariant::Int);
       
   112     QSqlField f2("artist", QVariant::String);
       
   113     QSqlField f3("albumcount", QVariant::Int);
       
   114 
       
   115     f1.setValue(QVariant(id));
       
   116     f2.setValue(QVariant(name));
       
   117     f3.setValue(QVariant(0));
       
   118     record.append(f1);
       
   119     record.append(f2);
       
   120     record.append(f3);
       
   121 
       
   122     artistModel->insertRecord(-1, record);
       
   123     return id;
       
   124 }
       
   125 
       
   126 int Dialog::addNewAlbum(const QString &title, int artistId)
       
   127 {
       
   128     int id = generateAlbumId();
       
   129     QSqlRecord record;
       
   130 
       
   131     QSqlField f1("albumid", QVariant::Int);
       
   132     QSqlField f2("title", QVariant::String);
       
   133     QSqlField f3("artistid", QVariant::Int);
       
   134     QSqlField f4("year", QVariant::Int);
       
   135 
       
   136     f1.setValue(QVariant(id));
       
   137     f2.setValue(QVariant(title));
       
   138     f3.setValue(QVariant(artistId));
       
   139     f4.setValue(QVariant(yearEditor->value()));
       
   140     record.append(f1);
       
   141     record.append(f2);
       
   142     record.append(f3);
       
   143     record.append(f4);
       
   144 
       
   145     model->insertRecord(-1, record);
       
   146     return id;
       
   147 }
       
   148 
       
   149 void Dialog::addTracks(int albumId, QStringList tracks)
       
   150 {
       
   151     QDomElement albumNode = albumDetails.createElement("album");
       
   152     albumNode.setAttribute("id", albumId);
       
   153 
       
   154     for (int i = 0; i < tracks.count(); i++) {
       
   155         QString trackNumber = QString::number(i);
       
   156         if (i < 10)
       
   157             trackNumber.prepend("0");
       
   158 
       
   159         QDomText textNode = albumDetails.createTextNode(tracks.at(i));
       
   160 
       
   161         QDomElement trackNode = albumDetails.createElement("track");
       
   162         trackNode.setAttribute("number", trackNumber);
       
   163         trackNode.appendChild(textNode);
       
   164 
       
   165         albumNode.appendChild(trackNode);
       
   166     }
       
   167 
       
   168     QDomNodeList archive = albumDetails.elementsByTagName("archive");
       
   169     archive.item(0).appendChild(albumNode);
       
   170 
       
   171 /*
       
   172     The following code is commented out since the example uses an in
       
   173     memory database, i.e., altering the XML file will bring the data
       
   174     out of sync.
       
   175 
       
   176     if (!outputFile->open(QIODevice::WriteOnly)) {
       
   177         return;
       
   178     } else {
       
   179         QTextStream stream(outputFile);
       
   180         archive.item(0).save(stream, 4);
       
   181         outputFile->close();
       
   182     }
       
   183 */
       
   184 }
       
   185 
       
   186 void Dialog::increaseAlbumCount(QModelIndex artistIndex)
       
   187 {
       
   188     QSqlTableModel *artistModel = model->relationModel(2);
       
   189 
       
   190     QModelIndex albumCountIndex;
       
   191     albumCountIndex = artistIndex.sibling(artistIndex.row(), 2);
       
   192 
       
   193     int albumCount = albumCountIndex.data().toInt();
       
   194     artistModel->setData(albumCountIndex, QVariant(albumCount + 1));
       
   195 }
       
   196 
       
   197 
       
   198 void Dialog::revert()
       
   199 {
       
   200     artistEditor->clear();
       
   201     titleEditor->clear();
       
   202     yearEditor->setValue(QDate::currentDate().year());
       
   203     tracksEditor->clear();
       
   204 }
       
   205 
       
   206 QGroupBox *Dialog::createInputWidgets()
       
   207 {
       
   208     QGroupBox *box = new QGroupBox(tr("Add Album"));
       
   209 
       
   210     QLabel *artistLabel = new QLabel(tr("Artist:"));
       
   211     QLabel *titleLabel = new QLabel(tr("Title:"));
       
   212     QLabel *yearLabel = new QLabel(tr("Year:"));
       
   213     QLabel *tracksLabel = new QLabel(tr("Tracks (separated by comma):"));
       
   214 
       
   215     artistEditor = new QLineEdit;
       
   216     titleEditor = new QLineEdit;
       
   217 
       
   218     yearEditor = new QSpinBox;
       
   219     yearEditor->setMinimum(1900);
       
   220     yearEditor->setMaximum(QDate::currentDate().year());
       
   221     yearEditor->setValue(yearEditor->maximum());
       
   222     yearEditor->setReadOnly(false);
       
   223 
       
   224     tracksEditor = new QLineEdit;
       
   225 
       
   226     QGridLayout *layout = new QGridLayout;
       
   227     layout->addWidget(artistLabel, 0, 0);
       
   228     layout->addWidget(artistEditor, 0, 1);
       
   229     layout->addWidget(titleLabel, 1, 0);
       
   230     layout->addWidget(titleEditor, 1, 1);
       
   231     layout->addWidget(yearLabel, 2, 0);
       
   232     layout->addWidget(yearEditor, 2, 1);
       
   233     layout->addWidget(tracksLabel, 3, 0, 1, 2);
       
   234     layout->addWidget(tracksEditor, 4, 0, 1, 2);
       
   235     box->setLayout(layout);
       
   236 
       
   237     return box;
       
   238 }
       
   239 
       
   240 QDialogButtonBox *Dialog::createButtons()
       
   241 {
       
   242     QPushButton *closeButton = new QPushButton(tr("&Close"));
       
   243     QPushButton *revertButton = new QPushButton(tr("&Revert"));
       
   244     QPushButton *submitButton = new QPushButton(tr("&Submit"));
       
   245 
       
   246     closeButton->setDefault(true);
       
   247 
       
   248     connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
       
   249     connect(revertButton, SIGNAL(clicked()), this, SLOT(revert()));
       
   250     connect(submitButton, SIGNAL(clicked()), this, SLOT(submit()));
       
   251 
       
   252     QDialogButtonBox *buttonBox = new QDialogButtonBox;
       
   253     buttonBox->addButton(submitButton, QDialogButtonBox::ResetRole);
       
   254     buttonBox->addButton(revertButton, QDialogButtonBox::ResetRole);
       
   255     buttonBox->addButton(closeButton, QDialogButtonBox::RejectRole);
       
   256 
       
   257     return buttonBox;
       
   258 }
       
   259 
       
   260 QModelIndex Dialog::indexOfArtist(const QString &artist)
       
   261 {
       
   262     QSqlTableModel *artistModel = model->relationModel(2);
       
   263 
       
   264     for (int i = 0; i < artistModel->rowCount(); i++) {
       
   265         QSqlRecord record =  artistModel->record(i);
       
   266         if (record.value("artist") == artist)
       
   267             return artistModel->index(i, 1);
       
   268     }
       
   269 
       
   270     return QModelIndex();
       
   271 }
       
   272 
       
   273 int Dialog::generateArtistId()
       
   274 {
       
   275     uniqueArtistId += 1;
       
   276     return uniqueArtistId;
       
   277 }
       
   278 
       
   279 int Dialog::generateAlbumId()
       
   280 {
       
   281     uniqueAlbumId += 1;
       
   282     return uniqueAlbumId;
       
   283 }