forked from debugger22/comic-aggregator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainwindow.cpp
62 lines (53 loc) · 1.96 KB
/
mainwindow.cpp
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
#include "mainwindow.h"
#include "aboutbox.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){
ui->setupUi(this);
connect(ui->cmdFetch,SIGNAL(clicked()),this,SLOT(fetch()));
connect(ui->actionExit, SIGNAL(triggered()),this,SLOT(close()));
connect(ui->actionAbout,SIGNAL(triggered()),this,SLOT(showAboutBox()));
ui->statusBar->showMessage("Ready");
}
MainWindow::~MainWindow(){
delete ui;
}
void MainWindow::fetch(){
ui->cmdFetch->setText("Fetching...");
ui->statusBar->showMessage("Downloading image urls...");
QList<QString> list;
list.append(QString("http://9gag.com/"));
list.append(QString("http://www.lolhappens.com/"));
list.append(QString("http://www.memecenter.com/"));
list.append(QString("http://uberhumor.com/"));
list.append(QString("http://porkystuff.com/"));
m_pImgCtrl = new FileDownloader(QUrl(list[rand()%5]), this);
connect(m_pImgCtrl, SIGNAL(downloaded()), SLOT(getImageUrl()));
ui->cmdFetch->setEnabled(false);
}
void MainWindow::getImageUrl(){
QString data = m_pImgCtrl->downloadedData();
QRegExp rx("\\<img[^\\>]*src\\s*=\\s*\"([^\"]*)\"[^\\>]*\\>");
QStringList list;
int pos = 0;
while ((pos = rx.indexIn(data, pos)) != -1){
list << rx.cap(1);
pos += rx.matchedLength();
}
int randomNo = rand() % list.length();
ui->statusBar->showMessage("Fetching Image");
QUrl imageUrl(list[randomNo]);
imageDownloader = new FileDownloader(imageUrl,this);
connect(imageDownloader,SIGNAL(downloaded()), SLOT(loadImage()));
}
void MainWindow::loadImage(){
QImage image;
image = QImage::fromData(imageDownloader->downloadedData());
ui->cmdFetch->setText("Fetch");
ui->lblView->setPixmap(QPixmap::fromImage(image));
ui->cmdFetch->setEnabled(true);
ui->statusBar->showMessage("Ready");
}
void MainWindow::showAboutBox(){
ab = new aboutBox;
ab->show();
ui->statusBar->showMessage("casc");
}