-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
129 lines (101 loc) · 2.84 KB
/
mainwindow.cpp
File metadata and controls
129 lines (101 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "mainwindow.hpp"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QDir>
#include <QMessageBox>
#include <QTextStream>
#include <QFileInfo>
#include <time.h>
MainWindow::MainWindow(QWidget * parent) :
QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
initialize();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::initialize()
{
mImageIndex = mQuoteIndex = -1;
srand(time(nullptr));
scan();
generate();
}
void MainWindow::generateButtonClicked()
{
generate();
}
void MainWindow::aboutButtonClicked()
{
QMessageBox::information(this, "About", "Hello world!");
}
void MainWindow::scan()
{
mImagePaths.clear();
mQuotes.clear();
// Load images
QDir imageDirectory(QDir::currentPath() + QDir::separator() + "Memes");
QStringList images = imageDirectory.entryList(QStringList() << "*.*", QDir::Files);
if(images.size() > 0)
{
for(const auto & image : images)
mImagePaths.push_back(imageDirectory.absolutePath() + QDir::separator() + image);
// Load facts
QFile inputFile("KarlFacts.txt");
if (inputFile.open(QIODevice::ReadOnly))
{
QTextStream in(&inputFile);
while (!in.atEnd())
{
mQuotes.push_back(in.readLine());
}
inputFile.close();
}
qDebug() << "Image base folder: " << imageDirectory.absolutePath() << '\n'
<< "Images count:" << mImagePaths.size() << '\n'
<< "Quotes count:" << mQuotes.size();
}
}
void MainWindow::load()
{
int width = std::max(ui->ImageLabel->width(), 300);
int height = std::max(ui->ImageLabel->height(), 282);
QString quote = mQuotes[mQuoteIndex];
QPixmap image = mImagePaths[mImageIndex];
QPixmap scaledImage = image.scaled(QSize(width, height), Qt::KeepAspectRatio);
ui->ImageLabel->setPixmap(scaledImage);
ui->QuoteLabel->setText(quote);
}
void MainWindow::generate()
{
if(mImagePaths.size() > 0 && mQuotes.size() > 0)
{
mImageIndex = rand() % mImagePaths.size();
mQuoteIndex = rand() % mQuotes.size();
QString imagePath = mImagePaths[mImageIndex];
qDebug() << "Selected image" << imagePath.section("/", -1);
if(!QFileInfo::exists(imagePath))
{
qDebug() << "File not found" << imagePath;
QMessageBox::warning(this, "File is moved or deleted", tr("File '%1' is moved or deleted!").arg(imagePath));
scan();
}
else
{
load();
}
}
else
{
qDebug() << "Containers are empty!";
QMessageBox::warning(this, "Warning", "No images & quotes found!");
scan();
}
}
void MainWindow::resizeEvent(QResizeEvent *)
{
if(mImageIndex != -1 && mQuoteIndex != -1)
load();
}