Skip to content

Commit

Permalink
change background every 10 secs.
Browse files Browse the repository at this point in the history
changed to how filenames of images are created.
load / save settings
Changed gui
  • Loading branch information
KoalaBR committed Nov 7, 2017
1 parent e4a0e73 commit 53ad5ed
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 80 deletions.
16 changes: 13 additions & 3 deletions imageitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,19 @@ QString ImageItem::filename(void)
QString filename = "portrait_";
if (!isPortrait())
filename = "landscape_";
QString dummy = QString(QCryptographicHash::hash(m_url.toUtf8(),QCryptographicHash::Md5).toHex());
dummy = dummy.left(20);
filename += dummy + ".png";
if (m_title != "")
{
QString name = m_title;
name = name.replace(',','_');
name = name.replace(" ", "");
filename += name + ".jpg";
}
else
{
QString dummy = QString(QCryptographicHash::hash(m_url.toUtf8(),QCryptographicHash::Md5).toHex());
dummy = dummy.left(20);
filename += dummy + ".jpg";
}
return filename;
}

Expand Down
91 changes: 81 additions & 10 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
#include <QJsonArray>
#include <QDir>
#include <QMap>
#include <QPainter>
#include <QSettings>

#include <time.h>

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "imageitem.h"

QMap<QString, ImageItem> imageMap;

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
Expand All @@ -19,14 +22,19 @@ MainWindow::MainWindow(QWidget *parent) :
connect(ui->pbSearch, SIGNAL(clicked()), this, SLOT(clickedSearch()));
connect(&m_downloader, SIGNAL(jsonDownloaded(QString)), this, SLOT(slotDownloadComplete(QString)));
connect(&m_downloader, SIGNAL(imageDownloaded(ImageItem)), this, SLOT(slotImageDownloadComplete(ImageItem)));
connect(&m_changeImgTimeout, SIGNAL(timeout()), this, SLOT(slotChangeBackgroundTimeout()));
m_changeImgTimeout.start(C_MW_TimeOut);
srand(static_cast<unsigned int>(time(NULL)));
loadSettings();
}

MainWindow::~MainWindow()
{
saveSettings();
delete ui;
}

void MainWindow::clickedSearch()
void MainWindow::clickedSearch(void)
{
// Start url erstellen
QUrl url = QUrl(createFirstRequest());
Expand All @@ -35,6 +43,40 @@ void MainWindow::clickedSearch()
m_downloader.downloadJSON(url);
}

void MainWindow::slotChangeBackgroundTimeout()
{
qDebug() << "Change background";
// Suchen wir nach Portrait oder Landscape?
QString path = "download";
path += QDir::separator() + QString("spotlight") + QDir::separator();
if (ui->cmbOrientation->currentIndex() == 0)
{
// Portrait
path += "portrait";
}
else
{
// Landscape
path += "landscape";
}
QDir dir(path);
qDebug() << path;
QStringList list = dir.entryList(QDir::Files);
qDebug() << "Size = " << list.size();
if (list.size() == 0)
return;
int index = random() % list.size();
qDebug() << "/" <<list[index] << "/";
if (m_img1.load(path + QDir::separator() + list[index]))
{
qDebug() << m_img1.width() << m_img1.height();
this->resize(m_img1.width(), m_img1.height());
this->centralWidget()->repaint();
}

}


/**
* @brief MainWindow::slotDownloadComplete
* Will be called once a none image file has been downloaded (probably JSON or
Expand Down Expand Up @@ -69,6 +111,24 @@ void MainWindow::printLine(QString line)
ui->teShowActions->insertPlainText(line+"\r");
}

void MainWindow::saveSettings()
{
QSettings settings(C_MW_IniFile, QSettings::NativeFormat);
settings.setValue("orientation", ui->cmbOrientation->currentIndex());
settings.setValue("title", ui->cmbTitle->currentIndex());
settings.setValue("geometry", this->geometry());

}

void MainWindow::loadSettings()
{
QSettings settings(C_MW_IniFile, QSettings::NativeFormat);
ui->cmbOrientation->setCurrentIndex(settings.value("orientation", 0).toInt());
ui->cmbOrientation->setCurrentIndex(settings.value("title", 0).toInt());
QRect rect = settings.value("geometry").toRect();
this->setGeometry(rect);
}

QList<ImageItem> MainWindow::getItemList(QByteArray data)
{
QList<ImageItem> result;
Expand Down Expand Up @@ -169,24 +229,35 @@ void MainWindow::createCacheDirs(void)
{
QString path = QString("download") + QDir::separator() + "spotlight";
QDir md = QDir();
md.mkpath(path);
md.mkpath(path + QDir::separator() + "portrait");
md.mkpath(path + QDir::separator() + "landscape");
path = QString("download") + QDir::separator() + "bing";
md.mkpath(path);

md.mkpath(path + QDir::separator() + "landscape");
md.mkpath(path + QDir::separator() + "portrait");
}

int count = 0; /// Nur zum Test!!!!!!!!

void MainWindow::slotImageDownloadComplete(ImageItem item)
{
qDebug() << "Download Ende: "<< item.url();
QString filename = QString("download");
if (item.source() == Source::SRC_SPOTLIGHT)
filename += QDir::separator() + QString("spotlight");
else filename += QDir::separator() + QString("bing");

// filename += QDir::separator() + item.filename();
filename = QString("C:\\tmp\\test%1.jpg").arg(count++);
filename += QDir::separator();
if (item.isPortrait())
filename += "portrait";
else filename += "landscape";
filename += QDir::separator() + item.filename();
qDebug() << filename;
item.image().save(filename);
}

void MainWindow::paintEvent(QPaintEvent *event)
{
if (!m_img1.isNull())
{
QPainter paint(this);
paint.drawImage(QPoint(0,0), m_img1);
}
QMainWindow::paintEvent(event);
}
14 changes: 14 additions & 0 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@

#include <QMainWindow>
#include <QList>
#include <QTimer>

#include "filedownloader.h"
#include "imageitem.h"

namespace Ui {
class MainWindow;
}

#define C_MW_TimeOut 10000 ///< Timeout in ms
#define C_MW_IniFile "settings.ini" ///< Where to store settings

enum class Orientation { PORTRAIT = 0, LANDSCAPE = 1, BOTH = 2};

class MainWindow : public QMainWindow
{
Q_OBJECT
Expand All @@ -19,18 +26,25 @@ class MainWindow : public QMainWindow
~MainWindow();
public slots:
void clickedSearch(void);
void slotChangeBackgroundTimeout(void);
void slotDownloadComplete(QString content);
void slotImageDownloadComplete(ImageItem item);
protected:
void paintEvent(QPaintEvent *event);
private:
QString createFirstRequest();
void printLine(QString line); ///< Fügt eine Zeile am Anfang ein.
void saveSettings(void);
void loadSettings(void);
QList<ImageItem> getItemList(QByteArray data);
QList<ImageItem> decodeJsonList(QString value);
QList<ImageItem> getImageItem(QJsonObject image);
void createCacheDirs();

Ui::MainWindow *ui;
DownloadManager m_downloader;
QTimer m_changeImgTimeout; ///< Timer, which changes background image if possible
QImage m_img1;
};

#endif // MAINWINDOW_H
92 changes: 25 additions & 67 deletions mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QGridLayout" name="gridLayout">
<item row="4" column="0" colspan="2">
<item row="3" column="0" colspan="2">
<widget class="QPushButton" name="pbSearch">
<property name="text">
<string>Suchen</string>
</property>
</widget>
</item>
<item row="4" column="2" colspan="2">
<item row="3" column="2" colspan="2">
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
Expand All @@ -35,7 +35,7 @@
</property>
</spacer>
</item>
<item row="5" column="0" colspan="5">
<item row="4" column="0" colspan="5">
<widget class="QPlainTextEdit" name="teShowActions">
<property name="readOnly">
<bool>true</bool>
Expand Down Expand Up @@ -81,91 +81,57 @@
</property>
</spacer>
</item>
<item row="0" column="4" rowspan="5">
<item row="0" column="4" rowspan="4">
<widget class="QTableWidget" name="tableWidget">
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
</widget>
</item>
<item row="2" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Auflösung</string>
<string>Titel anzeigen</string>
</property>
</widget>
</item>
<item row="1" column="1" colspan="2">
<widget class="QComboBox" name="comboBox_2">
<widget class="QComboBox" name="cmbTitle">
<item>
<property name="text">
<string>800x600</string>
<string>nie </string>
</property>
</item>
<item>
<property name="text">
<string>1920x1080</string>
<string>nur im Programm</string>
</property>
</item>
<item>
<property name="text">
<string>1920x1200</string>
<string>nur beim Speichern</string>
</property>
</item>
<item>
<property name="text">
<string>2560x1440</string>
<string>Immer</string>
</property>
</item>
</widget>
</item>
<item row="1" column="3">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>77</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Suche ab</string>
</property>
</widget>
</item>
<item row="3" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="1" colspan="2">
<widget class="QLineEdit" name="lineEdit">
<property name="inputMask">
<string>0000-00-00</string>
</property>
</widget>
</item>
<item row="2" column="3">
<widget class="QToolButton" name="toolButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
Expand All @@ -174,18 +140,10 @@
<x>0</x>
<y>0</y>
<width>447</width>
<height>26</height>
<height>27</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
Expand Down

0 comments on commit 53ad5ed

Please sign in to comment.