From aa168ca2228f96a22f0c251b26f4ff162f62d1ce Mon Sep 17 00:00:00 2001 From: Manuel Date: Wed, 23 Jan 2019 22:26:36 +0100 Subject: [PATCH] Blur and Command line opts * blur background image * command line options for * background blur radius * background opacity * path to image folder * timeout for password rotation --- src/main.cpp | 50 ++++++++++++++++++++++++++++++++++------------ src/mainwindow.cpp | 43 ++++++++++++++++++++++++++++++++++----- src/mainwindow.h | 8 +++++++- src/slide.pro.user | 4 ++-- 4 files changed, 84 insertions(+), 21 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index e5320b0..dbc66d7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,27 +5,51 @@ #include #include +#include +#include +#include + +void usage(std::string programName) { + std::cerr << "Usage: " << programName << " [-t rotation_seconds] [-o background_opacity(0..255)] [-b blur_radius] -p image_folder" << std::endl; +} + int main(int argc, char *argv[]) { + unsigned int rotationSeconds = 30; + std::string path = ""; - 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; + int opt; + while ((opt = getopt(argc, argv, "b:p:t:o:")) != -1) { + switch (opt) { + case 'p': + path = optarg; + break; + case 't': + rotationSeconds = atoi(optarg); + break; + case 'b': + w.setBlurRadius(atoi(optarg)); + break; + case 'o': + w.setBackgroundOpacity(atoi(optarg)); + break; + default: /* '?' */ + usage(argv[0]); + return 1; + } + } + + if (path.empty()) { + std::cout << "Error: Path expected." << std::endl; + usage(argv[0]); + return 1; + } w.show(); - ImageSelector is(w, 30000, argv[1]); + ImageSelector is(w, rotationSeconds * 1000, path); is.start(); return a.exec(); } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 1fe18e7..6694f31 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -7,11 +7,14 @@ #include #include #include +#include +#include +#include +#include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), - ui(new Ui::MainWindow), - image(new QLabel) + ui(new Ui::MainWindow) { ui->setupUi(this); setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint); @@ -20,7 +23,6 @@ MainWindow::MainWindow(QWidget *parent) : QApplication::setOverrideCursor(Qt::BlankCursor); QLabel *label = this->findChild("image"); setCentralWidget(label); - label->setStyleSheet("QLabel { background-color: rgba(0, 0, 0, 180);}"); label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); update(); @@ -100,7 +102,12 @@ void MainWindow::updateImage() QLabel *label = this->findChild("image"); label->setPixmap(scaled); + std::stringstream style; + style << "QLabel { background-color: rgba(0, 0, 0, " << (255 - backgroundOpacity) << ");}"; + label->setStyleSheet(style.str().c_str()); + drawBackground(rotated, scaled); + update(); } @@ -122,15 +129,41 @@ void MainWindow::drawBackground(const QPixmap& originalSize, const QPixmap& scal { QPalette palette; if (scaled.width() < width()) { - QPixmap background = originalSize.scaledToWidth(width()); + QPixmap background = blur(originalSize.scaledToWidth(width())); QRect rect(0, (background.height() - height())/2, width(), height()); background = background.copy(rect); palette.setBrush(QPalette::Background, background); } else { - QPixmap background = originalSize.scaledToHeight(height()); + QPixmap background = blur(originalSize.scaledToHeight(height())); QRect rect((background.width() - width())/2, 0, width(), height()); background = background.copy(rect); palette.setBrush(QPalette::Background, background); } this->setPalette(palette); } + +QPixmap MainWindow::blur(const QPixmap& input) +{ + QGraphicsScene scene; + QGraphicsPixmapItem item; + item.setPixmap(input); + QGraphicsBlurEffect effect; + effect.setBlurRadius(blurRadius); + item.setGraphicsEffect(&effect); + scene.addItem(&item); + QImage res(input.size(), QImage::Format_ARGB32); + res.fill(Qt::transparent); + QPainter ptr(&res); + scene.render(&ptr, QRectF(), QRectF( 0, 0, input.width(), input.height()) ); + return QPixmap::fromImage(res); +} + +void MainWindow::setBlurRadius(unsigned int blurRadius) +{ + this->blurRadius = blurRadius; +} + +void MainWindow::setBackgroundOpacity(unsigned int backgroundOpacity) +{ + this->backgroundOpacity = backgroundOpacity; +} diff --git a/src/mainwindow.h b/src/mainwindow.h index e6d88db..cb8fc31 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -20,17 +20,23 @@ public: void resizeEvent(QResizeEvent* event); ~MainWindow(); void setImage(std::string path); + void setBlurRadius(unsigned int blurRadius); + void setBackgroundOpacity(unsigned int opacity); private: Ui::MainWindow *ui; - QLabel *image; + std::string currentImage; + unsigned int blurRadius = 20; + unsigned int backgroundOpacity = 150; + void updateImage(); int getImageRotation(); QPixmap getRotatedPixmap(const QPixmap& p); QPixmap getScaledPixmap(const QPixmap& p); void drawBackground(const QPixmap& originalSize, const QPixmap& scaled); + QPixmap blur(const QPixmap& input); }; #endif // MAINWINDOW_H diff --git a/src/slide.pro.user b/src/slide.pro.user index 4c24fa9..8bfa437 100644 --- a/src/slide.pro.user +++ b/src/slide.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -310,7 +310,7 @@ slide.pro false - + /home/manuel/slide/build-slide-Desktop-Debug 3768 false true