| 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 "mainwindow.h" |
| 52 | #include "dialog.h" |
| 53 | |
| 54 | #include <QtWidgets> |
| 55 | #include <QtSql> |
| 56 | #include <QtXml> |
| 57 | |
| 58 | extern int uniqueAlbumId; |
| 59 | extern int uniqueArtistId; |
| 60 | |
| 61 | MainWindow::MainWindow(const QString &artistTable, const QString &albumTable, |
| 62 | QFile *albumDetails, QWidget *parent) |
| 63 | : QMainWindow(parent) |
| 64 | { |
| 65 | file = albumDetails; |
| 66 | readAlbumData(); |
| 67 | |
| 68 | model = new QSqlRelationalTableModel(this); |
| 69 | model->setTable(albumTable); |
| 70 | model->setRelation(2, QSqlRelation(artistTable, "id" , "artist" )); |
| 71 | model->select(); |
| 72 | |
| 73 | QGroupBox *artists = createArtistGroupBox(); |
| 74 | QGroupBox *albums = createAlbumGroupBox(); |
| 75 | QGroupBox *details = createDetailsGroupBox(); |
| 76 | |
| 77 | artistView->setCurrentIndex(0); |
| 78 | uniqueAlbumId = model->rowCount(); |
| 79 | uniqueArtistId = artistView->count(); |
| 80 | |
| 81 | connect(model, &QSqlRelationalTableModel::rowsInserted, |
| 82 | this, &MainWindow::updateHeader); |
| 83 | connect(model, &QSqlRelationalTableModel::rowsRemoved, |
| 84 | this, &MainWindow::updateHeader); |
| 85 | |
| 86 | QGridLayout *layout = new QGridLayout; |
| 87 | layout->addWidget(artists, 0, 0); |
| 88 | layout->addWidget(albums, 1, 0); |
| 89 | layout->addWidget(details, 0, 1, 2, 1); |
| 90 | layout->setColumnStretch(1, 1); |
| 91 | layout->setColumnMinimumWidth(0, 500); |
| 92 | |
| 93 | QWidget *widget = new QWidget; |
| 94 | widget->setLayout(layout); |
| 95 | setCentralWidget(widget); |
| 96 | createMenuBar(); |
| 97 | |
| 98 | showImageLabel(); |
| 99 | resize(850, 400); |
| 100 | setWindowTitle(tr("Music Archive" )); |
| 101 | } |
| 102 | |
| 103 | void MainWindow::changeArtist(int row) |
| 104 | { |
| 105 | if (row > 0) { |
| 106 | QModelIndex index = model->relationModel(2)->index(row, 1); |
| 107 | model->setFilter("artist = '" + index.data().toString() + '\'') ; |
| 108 | showArtistProfile(index); |
| 109 | } else if (row == 0) { |
| 110 | model->setFilter(QString()); |
| 111 | showImageLabel(); |
| 112 | } else { |
| 113 | return; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void MainWindow::showArtistProfile(QModelIndex index) |
| 118 | { |
| 119 | QSqlRecord record = model->relationModel(2)->record(index.row()); |
| 120 | |
| 121 | QString name = record.value("artist" ).toString(); |
| 122 | QString count = record.value("albumcount" ).toString(); |
| 123 | profileLabel->setText(tr("Artist : %1 \n" \ |
| 124 | "Number of Albums: %2" ).arg(name).arg(count)); |
| 125 | |
| 126 | profileLabel->show(); |
| 127 | iconLabel->show(); |
| 128 | |
| 129 | titleLabel->hide(); |
| 130 | trackList->hide(); |
| 131 | imageLabel->hide(); |
| 132 | } |
| 133 | |
| 134 | void MainWindow::showAlbumDetails(QModelIndex index) |
| 135 | { |
| 136 | QSqlRecord record = model->record(index.row()); |
| 137 | |
| 138 | QString artist = record.value("artist" ).toString(); |
| 139 | QString title = record.value("title" ).toString(); |
| 140 | QString year = record.value("year" ).toString(); |
| 141 | QString albumId = record.value("albumid" ).toString(); |
| 142 | |
| 143 | showArtistProfile(indexOfArtist(artist)); |
| 144 | titleLabel->setText(tr("Title: %1 (%2)" ).arg(title).arg(year)); |
| 145 | titleLabel->show(); |
| 146 | |
| 147 | QDomNodeList albums = albumData.elementsByTagName("album" ); |
| 148 | for (int i = 0; i < albums.count(); ++i) { |
| 149 | QDomNode album = albums.item(i); |
| 150 | if (album.toElement().attribute("id" ) == albumId) { |
| 151 | getTrackList(album.toElement()); |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | if (trackList->count() != 0) |
| 156 | trackList->show(); |
| 157 | } |
| 158 | |
| 159 | void MainWindow::getTrackList(QDomNode album) |
| 160 | { |
| 161 | trackList->clear(); |
| 162 | |
| 163 | QDomNodeList tracks = album.childNodes(); |
| 164 | QDomNode track; |
| 165 | QString trackNumber; |
| 166 | |
| 167 | for (int i = 0; i < tracks.count(); ++i) { |
| 168 | |
| 169 | track = tracks.item(i); |
| 170 | trackNumber = track.toElement().attribute("number" ); |
| 171 | |
| 172 | QListWidgetItem *item = new QListWidgetItem(trackList); |
| 173 | item->setText(trackNumber + ": " + track.toElement().text()); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | void MainWindow::addAlbum() |
| 178 | { |
| 179 | Dialog *dialog = new Dialog(model, albumData, file, this); |
| 180 | int accepted = dialog->exec(); |
| 181 | |
| 182 | if (accepted == 1) { |
| 183 | int lastRow = model->rowCount() - 1; |
| 184 | albumView->selectRow(lastRow); |
| 185 | albumView->scrollToBottom(); |
| 186 | showAlbumDetails(model->index(lastRow, 0)); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | void MainWindow::deleteAlbum() |
| 191 | { |
| 192 | QModelIndexList selection = albumView->selectionModel()->selectedRows(0); |
| 193 | |
| 194 | if (!selection.empty()) { |
| 195 | QModelIndex idIndex = selection.at(0); |
| 196 | int id = idIndex.data().toInt(); |
| 197 | QString title = idIndex.sibling(idIndex.row(), 1).data().toString(); |
| 198 | QString artist = idIndex.sibling(idIndex.row(), 2).data().toString(); |
| 199 | |
| 200 | QMessageBox::StandardButton button; |
| 201 | button = QMessageBox::question(this, tr("Delete Album" ), |
| 202 | tr("Are you sure you want to " |
| 203 | "delete '%1' by '%2'?" ) |
| 204 | .arg(title, artist), |
| 205 | QMessageBox::Yes | QMessageBox::No); |
| 206 | |
| 207 | if (button == QMessageBox::Yes) { |
| 208 | removeAlbumFromFile(id); |
| 209 | removeAlbumFromDatabase(idIndex); |
| 210 | decreaseAlbumCount(indexOfArtist(artist)); |
| 211 | |
| 212 | showImageLabel(); |
| 213 | } |
| 214 | } else { |
| 215 | QMessageBox::information(this, tr("Delete Album" ), |
| 216 | tr("Select the album you want to delete." )); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | void MainWindow::removeAlbumFromFile(int id) |
| 221 | { |
| 222 | |
| 223 | QDomNodeList albums = albumData.elementsByTagName("album" ); |
| 224 | |
| 225 | for (int i = 0; i < albums.count(); ++i) { |
| 226 | QDomNode node = albums.item(i); |
| 227 | if (node.toElement().attribute("id" ).toInt() == id) { |
| 228 | albumData.elementsByTagName("archive" ).item(0).removeChild(node); |
| 229 | break; |
| 230 | } |
| 231 | } |
| 232 | /* |
| 233 | The following code is commented out since the example uses an in |
| 234 | memory database, i.e., altering the XML file will bring the data |
| 235 | out of sync. |
| 236 | |
| 237 | if (!file->open(QIODevice::WriteOnly)) { |
| 238 | return; |
| 239 | } else { |
| 240 | QTextStream stream(file); |
| 241 | albumData.elementsByTagName("archive").item(0).save(stream, 4); |
| 242 | file->close(); |
| 243 | } |
| 244 | */ |
| 245 | } |
| 246 | |
| 247 | void MainWindow::removeAlbumFromDatabase(QModelIndex index) |
| 248 | { |
| 249 | model->removeRow(index.row()); |
| 250 | } |
| 251 | |
| 252 | void MainWindow::decreaseAlbumCount(QModelIndex artistIndex) |
| 253 | { |
| 254 | int row = artistIndex.row(); |
| 255 | QModelIndex albumCountIndex = artistIndex.sibling(row, 2); |
| 256 | int albumCount = albumCountIndex.data().toInt(); |
| 257 | |
| 258 | QSqlTableModel *artists = model->relationModel(2); |
| 259 | |
| 260 | if (albumCount == 1) { |
| 261 | artists->removeRow(row); |
| 262 | showImageLabel(); |
| 263 | } else { |
| 264 | artists->setData(albumCountIndex, QVariant(albumCount - 1)); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | void MainWindow::readAlbumData() |
| 269 | { |
| 270 | if (!file->open(QIODevice::ReadOnly)) |
| 271 | return; |
| 272 | |
| 273 | if (!albumData.setContent(file)) { |
| 274 | file->close(); |
| 275 | return; |
| 276 | } |
| 277 | file->close(); |
| 278 | } |
| 279 | |
| 280 | QGroupBox* MainWindow::createArtistGroupBox() |
| 281 | { |
| 282 | artistView = new QComboBox; |
| 283 | artistView->setModel(model->relationModel(2)); |
| 284 | artistView->setModelColumn(1); |
| 285 | |
| 286 | connect(artistView, &QComboBox::currentIndexChanged, |
| 287 | this, &MainWindow::changeArtist); |
| 288 | |
| 289 | QGroupBox *box = new QGroupBox(tr("Artist" )); |
| 290 | |
| 291 | QGridLayout *layout = new QGridLayout; |
| 292 | layout->addWidget(artistView, 0, 0); |
| 293 | box->setLayout(layout); |
| 294 | |
| 295 | return box; |
| 296 | } |
| 297 | |
| 298 | QGroupBox* MainWindow::createAlbumGroupBox() |
| 299 | { |
| 300 | QGroupBox *box = new QGroupBox(tr("Album" )); |
| 301 | |
| 302 | albumView = new QTableView; |
| 303 | albumView->setEditTriggers(QAbstractItemView::NoEditTriggers); |
| 304 | albumView->setSortingEnabled(true); |
| 305 | albumView->setSelectionBehavior(QAbstractItemView::SelectRows); |
| 306 | albumView->setSelectionMode(QAbstractItemView::SingleSelection); |
| 307 | albumView->setShowGrid(false); |
| 308 | albumView->verticalHeader()->hide(); |
| 309 | albumView->setAlternatingRowColors(true); |
| 310 | albumView->setModel(model); |
| 311 | adjustHeader(); |
| 312 | |
| 313 | QLocale locale = albumView->locale(); |
| 314 | locale.setNumberOptions(QLocale::OmitGroupSeparator); |
| 315 | albumView->setLocale(locale); |
| 316 | |
| 317 | connect(albumView, &QTableView::clicked, |
| 318 | this, &MainWindow::showAlbumDetails); |
| 319 | connect(albumView, &QTableView::activated, |
| 320 | this, &MainWindow::showAlbumDetails); |
| 321 | |
| 322 | QVBoxLayout *layout = new QVBoxLayout; |
| 323 | layout->addWidget(albumView, 0, { }); |
| 324 | box->setLayout(layout); |
| 325 | |
| 326 | return box; |
| 327 | } |
| 328 | |
| 329 | QGroupBox* MainWindow::createDetailsGroupBox() |
| 330 | { |
| 331 | QGroupBox *box = new QGroupBox(tr("Details" )); |
| 332 | |
| 333 | profileLabel = new QLabel; |
| 334 | profileLabel->setWordWrap(true); |
| 335 | profileLabel->setAlignment(Qt::AlignBottom); |
| 336 | |
| 337 | titleLabel = new QLabel; |
| 338 | titleLabel->setWordWrap(true); |
| 339 | titleLabel->setAlignment(Qt::AlignBottom); |
| 340 | |
| 341 | iconLabel = new QLabel(); |
| 342 | iconLabel->setAlignment(Qt::AlignBottom | Qt::AlignRight); |
| 343 | iconLabel->setPixmap(QPixmap(":/images/icon.png" )); |
| 344 | |
| 345 | imageLabel = new QLabel; |
| 346 | imageLabel->setWordWrap(true); |
| 347 | imageLabel->setAlignment(Qt::AlignCenter); |
| 348 | imageLabel->setPixmap(QPixmap(":/images/image.png" )); |
| 349 | |
| 350 | trackList = new QListWidget; |
| 351 | |
| 352 | QGridLayout *layout = new QGridLayout; |
| 353 | layout->addWidget(imageLabel, 0, 0, 3, 2); |
| 354 | layout->addWidget(profileLabel, 0, 0); |
| 355 | layout->addWidget(iconLabel, 0, 1); |
| 356 | layout->addWidget(titleLabel, 1, 0, 1, 2); |
| 357 | layout->addWidget(trackList, 2, 0, 1, 2); |
| 358 | layout->setRowStretch(2, 1); |
| 359 | box->setLayout(layout); |
| 360 | |
| 361 | return box; |
| 362 | } |
| 363 | |
| 364 | void MainWindow::createMenuBar() |
| 365 | { |
| 366 | QAction *addAction = new QAction(tr("&Add album..." ), this); |
| 367 | QAction *deleteAction = new QAction(tr("&Delete album..." ), this); |
| 368 | QAction *quitAction = new QAction(tr("&Quit" ), this); |
| 369 | QAction *aboutAction = new QAction(tr("&About" ), this); |
| 370 | QAction *aboutQtAction = new QAction(tr("About &Qt" ), this); |
| 371 | |
| 372 | addAction->setShortcut(tr("Ctrl+A" )); |
| 373 | deleteAction->setShortcut(tr("Ctrl+D" )); |
| 374 | quitAction->setShortcuts(QKeySequence::Quit); |
| 375 | |
| 376 | QMenu * = menuBar()->addMenu(tr("&File" )); |
| 377 | fileMenu->addAction(addAction); |
| 378 | fileMenu->addAction(deleteAction); |
| 379 | fileMenu->addSeparator(); |
| 380 | fileMenu->addAction(quitAction); |
| 381 | |
| 382 | QMenu * = menuBar()->addMenu(tr("&Help" )); |
| 383 | helpMenu->addAction(aboutAction); |
| 384 | helpMenu->addAction(aboutQtAction); |
| 385 | |
| 386 | connect(addAction, &QAction::triggered, |
| 387 | this, &MainWindow::addAlbum); |
| 388 | connect(deleteAction, &QAction::triggered, |
| 389 | this, &MainWindow::deleteAlbum); |
| 390 | connect(quitAction, &QAction::triggered, |
| 391 | this, &MainWindow::close); |
| 392 | connect(aboutAction, &QAction::triggered, |
| 393 | this, &MainWindow::about); |
| 394 | connect(aboutQtAction, &QAction::triggered, |
| 395 | qApp, &QApplication::aboutQt); |
| 396 | } |
| 397 | |
| 398 | void MainWindow::showImageLabel() |
| 399 | { |
| 400 | profileLabel->hide(); |
| 401 | titleLabel->hide(); |
| 402 | iconLabel->hide(); |
| 403 | trackList->hide(); |
| 404 | |
| 405 | imageLabel->show(); |
| 406 | } |
| 407 | |
| 408 | QModelIndex MainWindow::indexOfArtist(const QString &artist) |
| 409 | { |
| 410 | QSqlTableModel *artistModel = model->relationModel(2); |
| 411 | |
| 412 | for (int i = 0; i < artistModel->rowCount(); i++) { |
| 413 | QSqlRecord record = artistModel->record(i); |
| 414 | if (record.value("artist" ) == artist) |
| 415 | return artistModel->index(i, 1); |
| 416 | } |
| 417 | return QModelIndex(); |
| 418 | } |
| 419 | |
| 420 | void MainWindow::updateHeader(QModelIndex, int, int) |
| 421 | { |
| 422 | adjustHeader(); |
| 423 | } |
| 424 | |
| 425 | void MainWindow::adjustHeader() |
| 426 | { |
| 427 | albumView->hideColumn(0); |
| 428 | albumView->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch); |
| 429 | albumView->resizeColumnToContents(2); |
| 430 | albumView->resizeColumnToContents(3); |
| 431 | } |
| 432 | |
| 433 | void MainWindow::about() |
| 434 | { |
| 435 | QMessageBox::about(this, tr("About Music Archive" ), |
| 436 | tr("<p>The <b>Music Archive</b> example shows how to present " |
| 437 | "data from different data sources in the same application. " |
| 438 | "The album titles, and the corresponding artists and release dates, " |
| 439 | "are kept in a database, while each album's tracks are stored " |
| 440 | "in an XML file. </p><p>The example also shows how to add as " |
| 441 | "well as remove data from both the database and the " |
| 442 | "associated XML file using the API provided by the Qt SQL and " |
| 443 | "Qt XML modules, respectively.</p>" )); |
| 444 | } |
| 445 | |