This commit is contained in:
Manuel
2019-01-21 20:58:05 +01:00
parent 621681dd05
commit 08e159eb8e
10 changed files with 303 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
make
.swp
.git

View File

@@ -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
```

34
imageselector.cpp Normal file
View File

@@ -0,0 +1,34 @@
#include "imageselector.h"
#include "mainwindow.h"
#include <QTimer>
#include <QApplication>
#include <QDir>
#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* 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);
}

24
imageselector.h Normal file
View File

@@ -0,0 +1,24 @@
#ifndef IMAGESELECTOR_H
#define IMAGESELECTOR_H
#include <QObject>
#include <QTimer>
#include <iostream>
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

31
main.cpp Normal file
View File

@@ -0,0 +1,31 @@
#include "mainwindow.h"
#include "imageselector.h"
#include <QApplication>
#include <iostream>
#include <sys/file.h>
#include <errno.h>
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();
}

82
mainwindow.cpp Normal file
View File

@@ -0,0 +1,82 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLabel>
#include <QPixmap>
#include <QBitmap>
#include <QKeyEvent>
#include <QGraphicsBlurEffect>
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<QLabel*>("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<QLabel*>("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();
}

30
mainwindow.h Normal file
View File

@@ -0,0 +1,30 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
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

37
mainwindow.ui Normal file
View File

@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1500</width>
<height>945</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="image">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

7
run_once.sh Executable file
View File

@@ -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 $@

39
slide.pro Normal file
View File

@@ -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