19 class SpeakersModelItem
28 static bool lessThan(
const SpeakersModelItem& one,
const SpeakersModelItem& two );
31 class SpeakersModelPrivate
34 SpeakersModel* m_parent;
38 NewstuffModel m_newstuffModel;
40 SpeakersModelPrivate( SpeakersModel* parent );
44 void handleInstallationProgress(
int row, qreal progress );
46 void handleInstallation(
int );
48 #if QT_VERSION >= 0x050000
53 SpeakersModelItem::SpeakersModelItem() : m_newstuffIndex( -1 )
58 bool SpeakersModelItem::lessThan(
const SpeakersModelItem& one,
const SpeakersModelItem& two )
60 return one.m_file.fileName() < two.m_file.fileName();
63 SpeakersModelPrivate::SpeakersModelPrivate( SpeakersModel* parent ) :
66 m_newstuffModel.setTargetDirectory( MarbleDirs::localPath() +
"/audio/speakers" );
67 m_newstuffModel.setProvider(
"http://edu.kde.org/marble/newstuff/speakers.xml" );
68 QObject::connect( &m_newstuffModel, SIGNAL(modelReset()), m_parent, SLOT(fillModel()) );
69 QObject::connect( &m_newstuffModel, SIGNAL(installationProgressed(
int,qreal)),
70 m_parent, SLOT(handleInstallationProgress(
int,qreal)) );
71 QObject::connect( &m_newstuffModel, SIGNAL(installationFinished(
int)), m_parent, SLOT(handleInstallation(
int)) );
74 void SpeakersModelPrivate::fillModel()
79 foreach (
const QString &baseDir, baseDirs ) {
80 QString base = baseDir +
"/audio/speakers/";
84 foreach(
const QFileInfo &file, subdirs ) {
85 SpeakersModelItem item;
91 for (
int i=0; i<m_newstuffModel.rowCount(); ++i ) {
92 QModelIndex const index = m_newstuffModel.index( i );
93 QString const name = m_newstuffModel.
data( index ).toString();
95 for (
int j=0; j<m_speakers.size() && !exists; ++j ) {
96 SpeakersModelItem &item = m_speakers[j];
97 if ( item.m_file.fileName() == name ) {
98 item.m_newstuffIndex = i;
104 SpeakersModelItem item;
105 QString const path =
"%1/audio/speakers/%2";
106 item.m_file =
QFileInfo( path.
arg( MarbleDirs::localPath() ).
arg( name ) );
107 item.m_newstuffIndex = i;
112 qSort(m_speakers.begin(), m_speakers.end(), SpeakersModelItem::lessThan);
113 m_parent->beginResetModel();
114 m_parent->endResetModel();
115 emit m_parent->countChanged();
118 void SpeakersModelPrivate::handleInstallation(
int row )
120 for (
int j=0; j<m_speakers.size(); ++j ) {
121 if ( m_speakers[j].m_newstuffIndex == row ) {
122 m_speakers[j].m_file.refresh();
124 emit m_parent->dataChanged( affected, affected );
125 emit m_parent->installationFinished( j );
130 void SpeakersModelPrivate::handleInstallationProgress(
int row, qreal progress )
132 for (
int j=0; j<m_speakers.size(); ++j ) {
133 if ( m_speakers[j].m_newstuffIndex == row ) {
134 emit m_parent->installationProgressed( j, progress );
139 SpeakersModel::SpeakersModel(
QObject *parent ) :
143 roles[
Path] =
"path";
144 roles[
Name] =
"name";
147 #if QT_VERSION < 0x050000
150 d->m_roleNames = roles;
164 return d->m_speakers.size();
172 if ( index.
isValid() && index.
row() >= 0 && index.
row() < d->m_speakers.size() ) {
174 case Qt::DisplayRole:
return d->m_speakers.at( index.
row() ).m_file.fileName();
175 case Path:
return d->m_speakers.at( index.
row() ).m_file.absoluteFilePath();
176 case Name:
return d->m_speakers.at( index.
row() ).m_file.fileName();
177 case IsLocal:
return d->m_speakers.at( index.
row() ).m_file.exists();
178 case IsRemote:
return d->m_speakers.at( index.
row() ).m_newstuffIndex >= 0;
185 #if QT_VERSION >= 0x050000
188 return d->m_roleNames;
194 for(
int i=0; i<d->m_speakers.size(); ++i ) {
195 if ( d->m_speakers[i].m_file.absoluteFilePath() == name || d->m_speakers[i].m_file.fileName() ==
name ) {
204 if ( index >= 0 && index <= d->m_speakers.size() ) {
205 return d->m_speakers[
index].m_file.absoluteFilePath();
212 if ( index >= 0 && index < d->m_speakers.size() ) {
213 if ( d->m_speakers[index].m_newstuffIndex >= 0 ) {
214 d->m_newstuffModel.install( d->m_speakers[index].m_newstuffIndex );
236 #include "SpeakersModel.moc"
bool isRemote(int index) const
void setRoleNames(const QHash< int, QByteArray > &roleNames)
int rowCount(const QModelIndex &parent=QModelIndex()) const
Overload of QAbstractListModel.
int indexOf(const QString &name)
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Overload of QAbstractListModel.
const char * name() const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
QFileInfoList entryInfoList(QFlags< QDir::Filter > filters, QFlags< QDir::SortFlag > sort) const
bool isLocal(int index) const
~SpeakersModel()
Destructor.
QFuture< void > filter(Sequence &sequence, FilterFunction filterFunction)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QHash< int, QByteArray > roleNames() const
Overload of QAbstractListModel.