/[qspeakers]/listdialog.cpp
ViewVC logotype

Contents of /listdialog.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (show annotations)
Sat Oct 11 19:34:58 2014 UTC (2 years, 8 months ago) by ben
File size: 2122 byte(s)
implemented speaker selection from db search
1 #include <QDebug>
2 #include <QVariant>
3
4 #include "listdialog.h"
5 #include "ui_listdialog.h"
6
7 SpeakerListModel::SpeakerListModel(const QList<Speaker> &speakers, QWidget *parent) :
8 QAbstractListModel(parent)
9 {
10 this->speakers = speakers;
11 }
12
13 SpeakerListModel::~SpeakerListModel()
14 {
15 }
16
17 int SpeakerListModel::rowCount(const QModelIndex &parent) const
18 {
19 if (!parent.isValid())
20 return speakers.size();
21
22 if (parent.row() > 0)
23 return 0;
24
25 return speakers.size();
26 }
27
28 QVariant SpeakerListModel::data(const QModelIndex &index, int role) const
29 {
30 if (!index.isValid()) return QVariant();
31 if (index.row() >= speakers.size()) return QVariant();
32
33 if (role == Qt::DisplayRole) {
34 return QVariant(speakers.at(index.row()).getVendor() + " - " + speakers.at(index.row()).getModel());
35 } else if (role == Qt::UserRole){
36 return QVariant::fromValue<Speaker>(speakers.at(index.row()));
37 } else {
38 return QVariant();
39 }
40 }
41
42 void SpeakerListModel::clear()
43 {
44 speakers.clear();
45 }
46
47 ListDialog::ListDialog(const QList<Speaker> &speakers, QWidget *parent) :
48 QDialog(parent),
49 ui(new Ui::ListDialog),
50 model(NULL)
51 {
52 ui->setupUi(this);
53
54 setSpeakerItems(speakers);
55 connect(this, SIGNAL(accepted()), this, SLOT(onAccepted()));
56 connect(this, SIGNAL(rejected()), this, SLOT(onRejected()));
57 }
58
59 ListDialog::~ListDialog()
60 {
61 delete ui;
62 if (model != NULL)
63 delete model;
64 }
65
66 void ListDialog::setSpeakerItems(const QList<Speaker> &speakers)
67 {
68 if (model != NULL) {
69 model->clear();
70 delete model;
71 }
72
73 model = new SpeakerListModel(speakers, this);
74 ui->listDialogListView->setModel(model);
75 }
76
77 void ListDialog::clear()
78 {
79 model->clear();
80 }
81
82 void ListDialog::onAccepted()
83 {
84 QString title = model->data(QModelIndex(ui->listDialogListView->currentIndex()), Qt::DisplayRole).toString();
85 Speaker s = (Speaker) model->data(QModelIndex(ui->listDialogListView->currentIndex()), Qt::UserRole).value<Speaker>();
86 emit speakerItemSelected(title, s);
87 }
88
89 void ListDialog::onRejected()
90 {
91 emit speakerItemCancelled();
92 }

  ViewVC Help
Powered by ViewVC 1.1.26