void MainWindow::deleteAlbum() { QModelIndexList selection = albumView->selectionModel()->selectedRows(0); if (!selection.empty()) { QModelIndex idIndex = selection.at(0); int id = idIndex.data().toInt(); QString title = idIndex.sibling(idIndex.row(), 1).data().toString(); QString artist = idIndex.sibling(idIndex.row(), 2).data().toString(); QMessageBox::StandardButton button; button = QMessageBox::question(this, tr("Delete Album"), tr("Are you sure you want to " "delete '%1' by '%2'?") .arg(title, artist), QMessageBox::Yes | QMessageBox::No); if (button == QMessageBox::Yes) { removeAlbumFromFile(id); removeAlbumFromDatabase(idIndex); decreaseAlbumCount(indexOfArtist(artist)); showImageLabel(); } } else { QMessageBox::information(this, tr("Delete Album"), tr("Select the album you want to delete.")); } }
void MainWindow::changeArtist(int row) { if (row > 0) { QModelIndex index = model->relationModel(2)->index(row, 1); model->setFilter("artist = '" + index.data().toString() + '\'') ; showArtistProfile(index); } else if (row == 0) { model->setFilter(QString()); showImageLabel(); } else { return; } }
void MainWindow::decreaseAlbumCount(QModelIndex artistIndex) { int row = artistIndex.row(); QModelIndex albumCountIndex = artistIndex.sibling(row, 2); int albumCount = albumCountIndex.data().toInt(); QSqlTableModel *artists = model->relationModel(2); if (albumCount == 1) { artists->removeRow(row); showImageLabel(); } else { artists->setData(albumCountIndex, QVariant(albumCount - 1)); } }
MainWindow::MainWindow(const QString &artistTable, const QString &albumTable, QFile *albumDetails, QWidget *parent) : QMainWindow(parent) { file = albumDetails; readAlbumData(); model = new QSqlRelationalTableModel(this); model->setTable(albumTable); model->setRelation(2, QSqlRelation(artistTable, "id", "artist")); model->select(); QGroupBox *artists = createArtistGroupBox(); QGroupBox *albums = createAlbumGroupBox(); QGroupBox *details = createDetailsGroupBox(); artistView->setCurrentIndex(0); uniqueAlbumId = model->rowCount(); uniqueArtistId = artistView->count(); connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(updateHeader(QModelIndex,int,int))); connect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(updateHeader(QModelIndex,int,int))); QGridLayout *layout = new QGridLayout; layout->addWidget(artists, 0, 0); layout->addWidget(albums, 1, 0); layout->addWidget(details, 0, 1, 2, 1); #if !defined(Q_OS_SYMBIAN) && !defined(Q_WS_MAEMO_5) layout->setColumnStretch(1, 1); layout->setColumnMinimumWidth(0, 500); #endif QWidget *widget = new QWidget; widget->setLayout(layout); setCentralWidget(widget); createMenuBar(); showImageLabel(); #if !defined(Q_OS_SYMBIAN) && !defined(Q_WS_MAEMO_5) resize(850, 400); #endif setWindowTitle(tr("Music Archive")); }