diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..258bec7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +make +.swp +.git diff --git a/README.md b/README.md index f6ef021..688e137 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,18 @@ # slide Simple slideshow showing random images from specified directory + +## Usage + +``` +slide /path/to/images +``` + +## Build + +``` +mkdir -p make +cd make +qmake ../slide.pro +make +sudo make install +``` diff --git a/imageselector.cpp b/imageselector.cpp new file mode 100644 index 0000000..f4cf6b2 --- /dev/null +++ b/imageselector.cpp @@ -0,0 +1,34 @@ +#include "imageselector.h" +#include "mainwindow.h" +#include +#include +#include +#include +#include /* srand, rand */ +#include /* time */ + +ImageSelector::ImageSelector(MainWindow& w, unsigned int timeout, std::string path): + QObject::QObject(), + window(w), + timeout(timeout), + path(path), + timer(this) +{ + srand (time(NULL)); +} + +void ImageSelector::updateImage() +{ + QDir directory(path.c_str()); + QStringList images = directory.entryList(QStringList() << "*.jpg" << "*.JPG",QDir::Files); + unsigned int selectedImage = rand() % images.size(); + std::string filename = directory.filePath(images.at(selectedImage)).toStdString(); + std::cout << "updating image: " << filename << std::endl; + window.setImage(filename); +} + +void ImageSelector::start(){ + updateImage(); + connect(&timer, SIGNAL(timeout()), this, SLOT(updateImage())); + timer.start(timeout); +} diff --git a/imageselector.h b/imageselector.h new file mode 100644 index 0000000..f9bffd4 --- /dev/null +++ b/imageselector.h @@ -0,0 +1,24 @@ +#ifndef IMAGESELECTOR_H +#define IMAGESELECTOR_H + +#include +#include +#include +class MainWindow; +class ImageSelector : public QObject +{ + Q_OBJECT +public: + ImageSelector(MainWindow& w, unsigned int timeout, std::string path); + void start(); + +public slots: + void updateImage(); +private: + MainWindow& window; + unsigned int timeout; + std::string path; + QTimer timer; +}; + +#endif // IMAGESELECTOR_H diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..e5320b0 --- /dev/null +++ b/main.cpp @@ -0,0 +1,31 @@ +#include "mainwindow.h" +#include "imageselector.h" +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + + int pid_file = open("/tmp/slide.pid", O_CREAT | O_RDWR, 0666); + int rc = flock(pid_file, LOCK_EX | LOCK_NB); + if(rc) { + if(EWOULDBLOCK == errno) { + std::cout << "Already running. Exiting" << std::endl; + return 0; + } + } + if (argc < 2) { + std::cout << "Error: Path expected." << std::endl; + return 1; + } + QApplication a(argc, argv); + + MainWindow w; + w.show(); + + ImageSelector is(w, 30000, argv[1]); + is.start(); + return a.exec(); +} diff --git a/mainwindow.cpp b/mainwindow.cpp new file mode 100644 index 0000000..308a5e6 --- /dev/null +++ b/mainwindow.cpp @@ -0,0 +1,82 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" +#include +#include +#include +#include +#include + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow), + image(new QLabel) +{ + ui->setupUi(this); + setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint); + + QMainWindow::showFullScreen(); + QApplication::setOverrideCursor(Qt::BlankCursor); + QLabel *label = this->findChild("image"); + setCentralWidget(label); + label->setStyleSheet("QLabel { background-color: rgba(0, 0, 0, 230);}"); + label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); + update(); + +} + +MainWindow::~MainWindow() +{ + delete ui; +} + +void MainWindow::keyPressEvent(QKeyEvent* event) +{ + if(event->key() == Qt::Key_Escape) + { + QCoreApplication::quit(); + } + else + QWidget::keyPressEvent(event); +} + +void MainWindow::resizeEvent(QResizeEvent* event) +{ + QMainWindow::resizeEvent(event); + updateImage(); +} + +void MainWindow::setImage(std::string path) +{ + currentImage = path; + updateImage(); +} + +void MainWindow::updateImage() +{ + if (currentImage == "") + { + return; + } + QLabel *label = this->findChild("image"); + QPixmap p( currentImage.c_str() ); + int w = label->width(); + int h = label->height(); + QPixmap scaled = p.scaled(w,h,Qt::KeepAspectRatio); + label->setPixmap(scaled); + + + QPalette palette; + if (scaled.width() < width()) { + QPixmap background = p.scaledToWidth(width()); + QRect rect(0, (background.height() - height())/2, width(), height()); + background = background.copy(rect); + palette.setBrush(QPalette::Background, background); + } else { + QPixmap background = p.scaledToHeight(height()); + QRect rect((background.width() - width())/2, 0, width(), height()); + background = background.copy(rect); + palette.setBrush(QPalette::Background, background); + } + this->setPalette(palette); + update(); +} diff --git a/mainwindow.h b/mainwindow.h new file mode 100644 index 0000000..4c24c07 --- /dev/null +++ b/mainwindow.h @@ -0,0 +1,30 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +namespace Ui { +class MainWindow; +} +class QLabel; +class QKeyEvent; + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = 0); + void keyPressEvent(QKeyEvent* event); + void resizeEvent(QResizeEvent* event); + ~MainWindow(); + void setImage(std::string path); + +private: + Ui::MainWindow *ui; + QLabel *image; + std::string currentImage; + void updateImage(); +}; + +#endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui new file mode 100644 index 0000000..caa5029 --- /dev/null +++ b/mainwindow.ui @@ -0,0 +1,37 @@ + + + MainWindow + + + + 0 + 0 + 1500 + 945 + + + + + 0 + 0 + + + + MainWindow + + + + + + + TextLabel + + + + + + + + + + diff --git a/run_once.sh b/run_once.sh new file mode 100755 index 0000000..335870d --- /dev/null +++ b/run_once.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +me="$(basename "$0")"; +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +running=$(ps h -C "$me" | grep -wv $$ | wc -l); +[[ $running > 1 ]] && exit; +slide $@ diff --git a/slide.pro b/slide.pro new file mode 100644 index 0000000..5142755 --- /dev/null +++ b/slide.pro @@ -0,0 +1,39 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2019-01-20T10:45:40 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = slide +TEMPLATE = app + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which has been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + + +SOURCES += \ + main.cpp \ + mainwindow.cpp \ + imageselector.cpp + +HEADERS += \ + mainwindow.h \ + imageselector.h + +FORMS += \ + mainwindow.ui + +target.path = /usr/local/bin/ +INSTALLS += target