-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6569e08
commit 1f43723
Showing
8 changed files
with
570 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#------------------------------------------------- | ||
# | ||
# Project created by QtCreator 2014-11-24T22:57:26 | ||
# | ||
#------------------------------------------------- | ||
|
||
QT += core gui | ||
|
||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets | ||
|
||
TARGET = TrackEditor | ||
TEMPLATE = app | ||
|
||
|
||
SOURCES += main.cpp\ | ||
trackeditor.cpp \ | ||
trackdelegate.cpp | ||
|
||
HEADERS += trackeditor.h \ | ||
trackdelegate.h | ||
|
||
FORMS += trackeditor.ui |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include "trackeditor.h" | ||
#include <QApplication> | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
QApplication a(argc, argv); | ||
QList<Track> tracks; | ||
tracks << Track("The Flying Dutchman: Overture", 630) | ||
<< Track("The Flying Dutchman: Wie aus der Fern laengst " | ||
"vergangner Zeiten", 374) | ||
<< Track("The Flying Dutchman: Steuermann, lass die Wacht", | ||
152) | ||
<< Track("Die Walkuere: Ride of the Valkyries", 286) | ||
<< Track("Tannhaeuser: Freudig begruessen wir die edle " | ||
"Halle", 384) | ||
<< Track("Tannhaeuser: Wie Todesahnung - O du mein holder " | ||
"Abendstern", 257) | ||
<< Track("Lohengrin: Treulich gefuert ziehet dahnin", 294) | ||
<< Track("Lohengrin: In fernem Land", 383) | ||
<< Track("Die Meistersinger von Nuernberg: Overture", 543) | ||
<< Track("Die Meistersinger von Nuernberg: Verachtet mir " | ||
"die Meister nicht", 200) | ||
<< Track("Die Meistersinger von Nuernberg: Ehrt eure " | ||
"deutschen Meister", 112) | ||
<< Track("Goetterdaemmerung: Funeral Music", 469) | ||
<< Track("Tristan und Isolde: Mild und leise, wie er " | ||
"laechelt", 375); | ||
|
||
TrackEditor editor(&tracks); | ||
editor.resize(600, 300); | ||
editor.show(); | ||
|
||
return a.exec(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
|
||
#include <QtGui> | ||
|
||
#include "trackdelegate.h" | ||
|
||
TrackDelegate::TrackDelegate(int durationColumn, QObject *parent) | ||
: QItemDelegate(parent) | ||
{ | ||
this->durationColumn = durationColumn; | ||
} | ||
|
||
void TrackDelegate::paint(QPainter *painter, | ||
const QStyleOptionViewItem &option, | ||
const QModelIndex &index) const | ||
{ | ||
if (index.column() == durationColumn) { | ||
int secs = index.model()->data(index, Qt::DisplayRole).toInt(); | ||
QString text = QString("%1:%2") | ||
.arg(secs / 60, 2, 10, QChar('0')) | ||
.arg(secs % 60, 2, 10, QChar('0')); | ||
|
||
QStyleOptionViewItem myOption = option; | ||
myOption.displayAlignment = Qt::AlignRight | Qt::AlignVCenter; | ||
|
||
//重新进行绘制 | ||
drawDisplay(painter, myOption, myOption.rect, text); | ||
drawFocus(painter, myOption, myOption.rect); | ||
} else{ | ||
QItemDelegate::paint(painter, option, index); | ||
} | ||
} | ||
|
||
//createEditor用来创建一个编辑器 | ||
QWidget *TrackDelegate::createEditor(QWidget *parent, | ||
const QStyleOptionViewItem &option, | ||
const QModelIndex &index) const | ||
{ | ||
if (index.column() == durationColumn) { | ||
QTimeEdit *timeEdit = new QTimeEdit(parent); | ||
//设定正确的时间格式 | ||
timeEdit->setDisplayFormat("mm:ss"); | ||
//当完成编辑的时候(即按下enter键后--由editingFinished发射信号),会触发commitAndCloseEditor信号 | ||
connect(timeEdit, SIGNAL(editingFinished()), | ||
this, SLOT(commitAndCloseEditor())); | ||
return timeEdit; | ||
} else { | ||
return QItemDelegate::createEditor(parent, option, index); | ||
} | ||
} | ||
|
||
//setEditorData用来初始化编辑器 | ||
void TrackDelegate::setEditorData(QWidget *editor, | ||
const QModelIndex &index) const | ||
{ | ||
if (index.column() == durationColumn) { | ||
int secs = index.model()->data(index, Qt::DisplayRole).toInt(); | ||
QTimeEdit *timeEdit = qobject_cast<QTimeEdit *>(editor); | ||
timeEdit->setTime(QTime(0, secs / 60, secs % 60)); | ||
} else { | ||
QItemDelegate::setEditorData(editor, index); | ||
} | ||
} | ||
|
||
void TrackDelegate::setModelData(QWidget *editor, | ||
QAbstractItemModel *model, | ||
const QModelIndex &index) const | ||
{ | ||
if (index.column() == durationColumn) { | ||
QTimeEdit *timeEdit = qobject_cast<QTimeEdit *>(editor); | ||
QTime time = timeEdit->time(); | ||
int secs = (time.minute() * 60) + time.second(); | ||
model->setData(index, secs); | ||
} else { | ||
QItemDelegate::setModelData(editor, model, index); | ||
} | ||
} | ||
|
||
//提交修改并且关闭编辑器 | ||
void TrackDelegate::commitAndCloseEditor() | ||
{ | ||
QTimeEdit *editor = qobject_cast<QTimeEdit *>(sender()); | ||
emit commitData(editor); | ||
emit closeEditor(editor); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#ifndef TRACKDELEGATE_H | ||
#define TRACKDELEGATE_H | ||
|
||
#include <QItemDelegate> | ||
#include <QTimeEdit> | ||
|
||
class TrackDelegate : public QItemDelegate | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
TrackDelegate(int durationColumn, QObject *parent = 0); | ||
|
||
//我们想要实现分:秒这种格式的时间,所以必须重新编写paint函数 | ||
void paint(QPainter *painter, const QStyleOptionViewItem &option, | ||
const QModelIndex &index) const; | ||
//为了实现可编辑的委托,则必须实现以下三个函数:createEditor,setEditorData和setModelData | ||
QWidget *createEditor(QWidget *parent, | ||
const QStyleOptionViewItem &option, | ||
const QModelIndex &index) const; | ||
void setEditorData(QWidget *editor, const QModelIndex &index) const; | ||
void setModelData(QWidget *editor, QAbstractItemModel *model, | ||
const QModelIndex &index) const; | ||
|
||
private slots: | ||
void commitAndCloseEditor(); | ||
|
||
private: | ||
int durationColumn; | ||
}; | ||
|
||
#endif // TRACKDELEGATE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include "trackeditor.h" | ||
#include "ui_trackeditor.h" | ||
|
||
Track::Track(const QString &title, int duration) | ||
{ | ||
this->title = title; | ||
this->duration = duration; | ||
} | ||
|
||
TrackEditor::TrackEditor(QList<Track> *tracks, QWidget *parent) : | ||
QDialog(parent), | ||
ui(new Ui::TrackEditor) | ||
{ | ||
ui->setupUi(this); | ||
|
||
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); | ||
|
||
buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | ||
| QDialogButtonBox::Cancel); | ||
QPushButton *addTrackButton = buttonBox->addButton(tr("&Add Track"), | ||
QDialogButtonBox::ActionRole); | ||
|
||
connect(addTrackButton, SIGNAL(clicked()), this, SLOT(addTrack())); | ||
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())); | ||
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); | ||
|
||
QVBoxLayout *mainLayout = new QVBoxLayout; | ||
mainLayout->addWidget(tableWidget); | ||
mainLayout->addWidget(buttonBox); | ||
setLayout(mainLayout); | ||
|
||
setWindowTitle(tr("Track Editor")); | ||
} | ||
|
||
TrackEditor::~TrackEditor() | ||
{ | ||
delete ui; | ||
} | ||
|
||
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(); | ||
QTableWidgetItem *item = tableWidget->item(row, 1); | ||
int duration = item ? item->text().toInt() : 0; | ||
tracks->append(Track(title, duration)); | ||
} | ||
} | ||
QDialog::done(result); | ||
} | ||
|
||
void TrackEditor::addTrack() | ||
{ | ||
tableWidget->insertRow(tableWidget->rowCount()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#ifndef TRACKEDITOR_H | ||
#define TRACKEDITOR_H | ||
|
||
#include <QDialog> | ||
#include <QDialogButtonBox> | ||
#include <QTableWidget> | ||
#include <QList> | ||
#include <QPushButton> | ||
#include <QVBoxLayout> | ||
#include "trackdelegate.h" | ||
|
||
namespace Ui { | ||
class TrackEditor; | ||
} | ||
|
||
class Track | ||
{ | ||
public: | ||
Track(const QString &title = "", int duration = 0); | ||
|
||
QString title; | ||
int duration; | ||
}; | ||
|
||
class TrackEditor : public QDialog | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit TrackEditor(QList<Track> *tracks, QWidget *parent = 0); | ||
~TrackEditor(); | ||
|
||
void done(int result); | ||
|
||
private slots: | ||
void addTrack(); | ||
|
||
private: | ||
QTableWidget *tableWidget; | ||
QDialogButtonBox *buttonBox; | ||
QList<Track> *tracks; | ||
|
||
private: | ||
Ui::TrackEditor *ui; | ||
}; | ||
|
||
#endif // TRACKEDITOR_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<ui version="4.0"> | ||
<class>TrackEditor</class> | ||
<widget class="QDialog" name="TrackEditor" > | ||
<property name="geometry" > | ||
<rect> | ||
<x>0</x> | ||
<y>0</y> | ||
<width>400</width> | ||
<height>300</height> | ||
</rect> | ||
</property> | ||
<property name="windowTitle" > | ||
<string>TrackEditor</string> | ||
</property> | ||
</widget> | ||
<layoutDefault spacing="6" margin="11" /> | ||
<pixmapfunction></pixmapfunction> | ||
<resources/> | ||
<connections/> | ||
</ui> |