#include #include "trackdelegate.h" #include "trackeditor.h" Track::Track(const QString &title, int duration) { this->title = title; this->duration = duration; } TrackEditor::TrackEditor(QList *tracks, QWidget *parent) : QDialog(parent) { this->tracks = tracks; tableWidget = new QTableWidget(tracks->count(), 2); tableWidget->setItemDelegate(new TrackDelegate(1)); tableWidget->setHorizontalHeaderLabels( QStringList() << tr("Track") << tr("Duration")); for (int row = 0; row < tracks->count(); ++row) { Track track = tracks->at(row); QTableWidgetItem *item0 = new QTableWidgetItem(track.title); tableWidget->setItem(row, 0, item0); QTableWidgetItem *item1 = new QTableWidgetItem(QString::number(track.duration)); item1->setTextAlignment(Qt::AlignRight); tableWidget->setItem(row, 1, item1); } tableWidget->resizeColumnToContents(0); addTrackButton = new QPushButton(tr("&Add Track")); okButton = new QPushButton(tr("OK")); okButton->setDefault(true); cancelButton = new QPushButton(tr("Cancel")); connect(addTrackButton, SIGNAL(clicked()), this, SLOT(addTrack())); connect(okButton, SIGNAL(clicked()), this, SLOT(accept())); connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject())); QHBoxLayout *buttonLayout = new QHBoxLayout; buttonLayout->addWidget(addTrackButton); buttonLayout->addStretch(); buttonLayout->addWidget(okButton); buttonLayout->addWidget(cancelButton); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addWidget(tableWidget); mainLayout->addLayout(buttonLayout); setLayout(mainLayout); setWindowTitle(tr("Track Editor")); } void TrackEditor::done(int result) { if (result == QDialog::Accepted) { tracks->clear(); for (int row = 0; row < tableWidget->rowCount(); ++row) { QString title = tableWidget->item(row, 0)->text(); int duration = tableWidget->item(row, 1)->text().toInt(); tracks->append(Track(title, duration)); } } QDialog::done(result); } void TrackEditor::addTrack() { tableWidget->insertRow(tableWidget->rowCount()); }