Skip to content

Commit

Permalink
Merge pull request #21 from NautiluX/sort-by-name
Browse files Browse the repository at this point in the history
sorted mode
  • Loading branch information
NautiluX authored Aug 14, 2020
2 parents 7b77e62 + 05e5bb9 commit fa579f7
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ slide [-t rotation_seconds] [-o background_opacity(0..255)] [-b blur_radius] -p
* `image_folder`: where to search for images (.jpg files)
* `-r` for recursive traversal of `image_folder`
* `-s` for shuffle instead of random image rotation
* `-S` for sorted rotation (files ordered by name, first images then subfolders)
* `rotation_seconds(default=30)`: time until next random image is chosen from the given folder
* `background_opacity(default=150)`: opacity of the background filling image between 0 (black background) and 255
* `blur_radius(default=20)`: blur radius of the background filling image
Expand Down
49 changes: 49 additions & 0 deletions src/imageselector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,52 @@ std::string ShuffleImageSelector::getNextImage()
current_image_shuffle = current_image_shuffle + 1;
return filename;
}

SortedImageSelector::SortedImageSelector(std::unique_ptr<PathTraverser>& pathTraverser):
ImageSelector(pathTraverser),
images()
{
srand (time(NULL));
}

SortedImageSelector::~SortedImageSelector()
{
}

bool operator<(const QString& lhs, const QString& rhs) noexcept{
if (lhs.count(QLatin1Char('/')) < rhs.count(QLatin1Char('/'))) {
return true;
}
if (lhs.count(QLatin1Char('/')) > rhs.count(QLatin1Char('/'))) {
return false;
}

return lhs.toStdString() < rhs.toStdString();

}

std::string SortedImageSelector::getNextImage()
{
if (images.size() == 0)
{
images = pathTraverser->getImages();
std::sort(images.begin(), images.end());
std::cout << "read " << images.size() << " images." << std::endl;
for (int i = 0;i <images.size();i++){

std::cout << images[i].toStdString() << std::endl;
}
}
if (images.size() == 0)
{
return "";
}
std::string filename = pathTraverser->getImagePath(images.takeFirst().toStdString());
if(!QFileInfo::exists(QString(filename.c_str())))
{
std::cout << "file not found: " << filename << std::endl;
return getNextImage();
}
std::cout << "updating image: " << filename << std::endl;
return filename;
}
10 changes: 10 additions & 0 deletions src/imageselector.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,14 @@ class ShuffleImageSelector : public ImageSelector
QStringList images;
};

class SortedImageSelector : public ImageSelector
{
public:
SortedImageSelector(std::unique_ptr<PathTraverser>& pathTraverser);
virtual ~SortedImageSelector();
virtual std::string getNextImage();

private:
QStringList images;
};
#endif // IMAGESELECTOR_H
12 changes: 10 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ int main(int argc, char *argv[])
int opt;
bool recursive = false;
bool shuffle = false;
while ((opt = getopt(argc, argv, "b:p:t:o:rs")) != -1) {
bool sorted = false;
while ((opt = getopt(argc, argv, "b:p:t:o:rsS")) != -1) {
switch (opt) {
case 'p':
path = optarg;
Expand All @@ -48,6 +49,9 @@ int main(int argc, char *argv[])
shuffle = true;
std::cout << "Shuffle mode is on." << std::endl;
break;
case 'S':
sorted = true;
break;
default: /* '?' */
usage(argv[0]);
return 1;
Expand All @@ -72,7 +76,11 @@ int main(int argc, char *argv[])
}

std::unique_ptr<ImageSelector> selector;
if (shuffle)
if (sorted)
{
selector = std::unique_ptr<ImageSelector>(new SortedImageSelector(pathTraverser));
}
else if (shuffle)
{
selector = std::unique_ptr<ImageSelector>(new ShuffleImageSelector(pathTraverser));
}
Expand Down

0 comments on commit fa579f7

Please sign in to comment.