-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
listdialog.cpp
92 lines (75 loc) · 2.11 KB
/
listdialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <QDebug>
#include <QVariant>
#include "listdialog.h"
#include "ui_listdialog.h"
SpeakerListModel::SpeakerListModel(const QList<Speaker> &speakers, QWidget *parent) :
QAbstractListModel(parent)
{
this->speakers = speakers;
}
SpeakerListModel::~SpeakerListModel()
{
}
int SpeakerListModel::rowCount(const QModelIndex &parent) const
{
if (!parent.isValid())
return speakers.size();
if (parent.row() > 0)
return 0;
return speakers.size();
}
QVariant SpeakerListModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) return QVariant();
if (index.row() >= speakers.size()) return QVariant();
if (role == Qt::DisplayRole) {
return QVariant(speakers.at(index.row()).getVendor() + " - " + speakers.at(index.row()).getModel());
} else if (role == Qt::UserRole){
return QVariant::fromValue<Speaker>(speakers.at(index.row()));
} else {
return QVariant();
}
}
void SpeakerListModel::clear()
{
speakers.clear();
}
ListDialog::ListDialog(const QList<Speaker> &speakers, QWidget *parent) :
QDialog(parent),
ui(new Ui::ListDialog),
model(nullptr)
{
ui->setupUi(this);
setSpeakerItems(speakers);
connect(this, &ListDialog::accepted, this, &ListDialog::onAccepted);
connect(this, &ListDialog::rejected, this, &ListDialog::onRejected);
}
ListDialog::~ListDialog()
{
delete ui;
if (model != nullptr)
delete model;
}
void ListDialog::setSpeakerItems(const QList<Speaker> &speakers)
{
if (model != nullptr) {
model->clear();
delete model;
}
model = new SpeakerListModel(speakers, this);
ui->listDialogListView->setModel(model);
}
void ListDialog::clear()
{
model->clear();
}
void ListDialog::onAccepted()
{
QString title = model->data(QModelIndex(ui->listDialogListView->currentIndex()), Qt::DisplayRole).toString();
Speaker s = static_cast<Speaker> (model->data(QModelIndex(ui->listDialogListView->currentIndex()), Qt::UserRole).value<Speaker>());
emit speakerItemSelected(title, s);
}
void ListDialog::onRejected()
{
emit speakerItemCancelled();
}