From 1f15d308b25439628d61aba64a24678df7bb98f2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Timo=20H=C3=A4rk=C3=B6nen?= Date: Tue, 28 Sep 2010 19:17:44 +0300 Subject: [PATCH] Get image paths from tracker --- src/settings.cpp | 10 ++++++++ src/settings.h | 4 +++ src/settingsdialog.cpp | 53 +++++++++++++++++++++------------------ src/settingsdialog.h | 4 +++ src/src.pro | 6 +++-- src/statistics.cpp | 18 ++++++++++++++ src/statistics.h | 18 ++++++++++++++ src/statisticsdialog.cpp | 18 ++++++++++++++ src/statisticsdialog.h | 18 ++++++++++++++ src/trackerfiles.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++ src/trackerfiles.h | 44 ++++++++++++++++++++++++++++++++ 11 files changed, 229 insertions(+), 26 deletions(-) create mode 100644 src/trackerfiles.cpp create mode 100644 src/trackerfiles.h diff --git a/src/settings.cpp b/src/settings.cpp index 3a418be..151c5f5 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -67,3 +67,13 @@ void Settings::setImagePath(const QString &path) { imagePath_ = path; } + +QStringList Settings::localImages() const +{ + return imageList_; +} + +void Settings::setLocalImages(const QStringList &files) +{ + imageList_ = files; +} diff --git a/src/settings.h b/src/settings.h index bb45e67..4a5a6bc 100644 --- a/src/settings.h +++ b/src/settings.h @@ -38,6 +38,9 @@ public: QString imagePath() const; void setImagePath(const QString &path); + QStringList localImages() const; + void setLocalImages(const QStringList &files); + private: Settings(QObject *parent = 0); @@ -46,5 +49,6 @@ private: int pieceCount_; QPixmap image_; QString imagePath_; + QStringList imageList_; }; #endif diff --git a/src/settingsdialog.cpp b/src/settingsdialog.cpp index 0f2a6cb..86834f0 100644 --- a/src/settingsdialog.cpp +++ b/src/settingsdialog.cpp @@ -19,6 +19,7 @@ #include "settingsdialog.h" #include "settings.h" #include "defines.h" +#include "trackerfiles.h" #include #include @@ -67,12 +68,15 @@ SettingsDialog::SettingsDialog(QWidget *parent) : selectedImageLabel_->setVisible(false); + trackerFiles_ = new TrackerFiles(this); + setLayout(mainLayout_); connect(easyButton_, SIGNAL(toggled(bool)), this, SLOT(difficultySelectionChanged(bool))); //connect(imageCombo_, SIGNAL(currentIndexChanged(QString)), this, SLOT(imageSelectionChanged(QString))); connect(imageCombo_, SIGNAL(activated(QString)), this, SLOT(imageSelectionChanged(QString))); connect(applyButton_, SIGNAL(clicked()), this, SLOT(applyClicked())); + connect(trackerFiles_, SIGNAL(filesRead(QStringList)), this, SLOT(trackerFilesRead(QStringList))); } int SettingsDialog::exec() @@ -98,32 +102,11 @@ void SettingsDialog::difficultySelectionChanged(bool value) void SettingsDialog::imageSelectionChanged(const QString &txt) { if(txt == RANDOM_IMAGE_TXT) { - qDebug() << "Random image selected"; - - // Get random image from ~/MyDocs/.images/ - //TODO: images from other directories - QStringList filters; - filters << "*.jpg" << "*.png" << "*.xpm"; - - QDir dir(QDir::homePath() + QLatin1String("/MyDocs/.images")); - //dir.setNameFilters(filters); - - QStringList pics = dir.entryList(filters, QDir::Files | QDir::NoSymLinks); - - //qDebug() << QString("pics list contains %1 entries").arg(pics.count()); - - if(!pics.isEmpty()) { - QString path = QDir::homePath() + QLatin1String("/MyDocs/.images/") + pics.at(qrand() % pics.count()); - Settings::instance()->setImage(QPixmap(path)); - Settings::instance()->setImagePath(path); + if(Settings::instance()->localImages().isEmpty()) { + trackerFiles_->readFiles(); } else { - Settings::instance()->setImage(QPixmap(":/images/default.jpg")); - Settings::instance()->setImagePath("default"); - } - - if(selectedImageLabel_->isVisible()) { - selectedImageLabel_->setVisible(false); + trackerFilesRead(Settings::instance()->localImages()); } } else if(txt == SELECT_IMAGE_TXT) { @@ -159,3 +142,25 @@ void SettingsDialog::applyClicked() { this->done(1); } + +void SettingsDialog::trackerFilesRead(const QStringList &files) +{ + if(!files.isEmpty()) { + QString path = files.at(qrand() % files.count()); + path = path.trimmed(); + Settings::instance()->setImage(QPixmap(path)); + Settings::instance()->setImagePath(path); + + if(Settings::instance()->localImages().isEmpty()) { + Settings::instance()->setLocalImages(files); + } + } + else { + Settings::instance()->setImage(QPixmap(":/images/default.jpg")); + Settings::instance()->setImagePath("default"); + } + + if(selectedImageLabel_->isVisible()) { + selectedImageLabel_->setVisible(false); + } +} diff --git a/src/settingsdialog.h b/src/settingsdialog.h index 23b82ea..70cf8c9 100644 --- a/src/settingsdialog.h +++ b/src/settingsdialog.h @@ -28,6 +28,7 @@ class QHBoxLayout; class QComboBox; class QLabel; class QPushButton; +class TrackerFiles; class SettingsDialog : public QDialog { @@ -43,6 +44,7 @@ private slots: void difficultySelectionChanged(bool value); void imageSelectionChanged(const QString &txt); void applyClicked(); + void trackerFilesRead(const QStringList &files); private: QVBoxLayout *mainLayout_; @@ -58,5 +60,7 @@ private: QLabel *selectedImageLabel_; QPushButton *applyButton_; + + TrackerFiles *trackerFiles_; }; #endif diff --git a/src/src.pro b/src/src.pro index 2b28d76..92b6ee4 100644 --- a/src/src.pro +++ b/src/src.pro @@ -16,7 +16,8 @@ HEADERS += gameview.h \ settingsdialog.h \ aboutdialog.h \ statistics.h \ - statisticsdialog.h + statisticsdialog.h \ + trackerfiles.h SOURCES += gameview.cpp \ main.cpp \ mainwindow.cpp \ @@ -27,7 +28,8 @@ SOURCES += gameview.cpp \ settingsdialog.cpp \ aboutdialog.cpp \ statistics.cpp \ - statisticsdialog.cpp + statisticsdialog.cpp \ + trackerfiles.cpp RESOURCES += resources.qrc desktop.files += impuzzle.desktop desktop.path = /usr/share/applications/hildon/ diff --git a/src/statistics.cpp b/src/statistics.cpp index c979da0..b81af23 100644 --- a/src/statistics.cpp +++ b/src/statistics.cpp @@ -1,3 +1,21 @@ +/* + Image Puzzle - A set your pieces straight game + Copyright (C) 2009 Timo Härkönen + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + #include "statistics.h" #include "defines.h" diff --git a/src/statistics.h b/src/statistics.h index 8b2733c..67c6370 100644 --- a/src/statistics.h +++ b/src/statistics.h @@ -1,3 +1,21 @@ +/* + Image Puzzle - A set your pieces straight game + Copyright (C) 2009 Timo Härkönen + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + #ifndef STATISTICS_H #define STATISTICS_H diff --git a/src/statisticsdialog.cpp b/src/statisticsdialog.cpp index 28ed7ba..c8915f9 100644 --- a/src/statisticsdialog.cpp +++ b/src/statisticsdialog.cpp @@ -1,3 +1,21 @@ +/* + Image Puzzle - A set your pieces straight game + Copyright (C) 2009 Timo Härkönen + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + #include "statisticsdialog.h" #include "statistics.h" diff --git a/src/statisticsdialog.h b/src/statisticsdialog.h index cabecdd..b2faf80 100644 --- a/src/statisticsdialog.h +++ b/src/statisticsdialog.h @@ -1,3 +1,21 @@ +/* + Image Puzzle - A set your pieces straight game + Copyright (C) 2009 Timo Härkönen + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + #ifndef STATISTICSDIALOG_H #define STATISTICSDIALOG_H diff --git a/src/trackerfiles.cpp b/src/trackerfiles.cpp new file mode 100644 index 0000000..1890500 --- /dev/null +++ b/src/trackerfiles.cpp @@ -0,0 +1,62 @@ +/* + Image Puzzle - A set your pieces straight game + Copyright (C) 2009 Timo Härkönen + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +#include "trackerfiles.h" + +#include + +TrackerFiles::TrackerFiles(QObject *parent) : + QObject(parent) +{ + tracker_ = new QProcess(this); + + connect(tracker_, SIGNAL(finished(int,QProcess::ExitStatus)), + this, SLOT(processFinished(int,QProcess::ExitStatus))); +} + +void TrackerFiles::readFiles() +{ + QString program("tracker-files"); + QStringList args; + args << "-m" << "image/png" << "-m" << "image/jpeg"; + tracker_->start(program, args); + // FIXME: this blocks execution + tracker_->waitForFinished(); +} + +void TrackerFiles::processFinished(int exitCode, QProcess::ExitStatus exitStatus) +{ + QStringList files; + + if(exitCode == 0 && exitStatus == QProcess::NormalExit) { + QString stdOut = tracker_->readAllStandardOutput(); + files = stdOut.split("\n"); + // first row is not a path + if(!files.isEmpty()) { + files.removeAt(0); + } + + files.removeAll(""); + qDebug() << QString("Found %1 images").arg(files.count()); + } + else { + qDebug() << tracker_->errorString(); + } + + emit filesRead(files); +} diff --git a/src/trackerfiles.h b/src/trackerfiles.h new file mode 100644 index 0000000..9e353b4 --- /dev/null +++ b/src/trackerfiles.h @@ -0,0 +1,44 @@ +/* + Image Puzzle - A set your pieces straight game + Copyright (C) 2009 Timo Härkönen + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +#ifndef TRACKERFILES_H +#define TRACKERFILES_H + +#include +#include + +class TrackerFiles : public QObject +{ +Q_OBJECT +public: + explicit TrackerFiles(QObject *parent = 0); + +signals: + void filesRead(const QStringList &files); + +public slots: + void readFiles(); + +private slots: + void processFinished(int exitCode, QProcess::ExitStatus exitStatus); + +private: + QProcess *tracker_; +}; + +#endif // TRACKERFILES_H -- 1.7.9.5