-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.cpp
More file actions
119 lines (91 loc) · 3.33 KB
/
Copy pathwindow.cpp
File metadata and controls
119 lines (91 loc) · 3.33 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
#include "window.h"
#include <QNetworkAccessManager>
#include <QPushButton>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QDateEdit>
#include <QTextEdit>
#include <QLabel>
Window::Window(QWidget *parent) :
QWidget(parent), _working(false)
{
_label = new QLabel("Номера страниц через пробелы", this);
_text = new QLineEdit(this);
_button = new QPushButton("Начать", this);
_edit = new QTextEdit(this);
_from = new QDateEdit(QDate::currentDate(), this);
_to = new QDateEdit(QDate::currentDate(), this);
_from->setDisplayFormat("MM.yyyy");
_to->setDisplayFormat("MM.yyyy");
auto line = new QHBoxLayout();
line->addWidget(_label);
line->addWidget(_text);
auto grid = new QGridLayout();
grid->addLayout(line, 0, 0, 1, 4);
grid->addWidget(new QLabel("Начало", this), 1, 0, 1, 1);
grid->addWidget(_from, 1, 1, 1, 1);
grid->addWidget(new QLabel("Конец", this), 1, 2, 1, 1);
grid->addWidget(_to, 1, 3, 1, 1);
grid->addWidget(_button, 2, 3, 1, 1);
grid->addWidget(_edit, 3, 0, 1, 4);
this->setLayout(grid);
connect(_button, SIGNAL(clicked()), this, SLOT(startParsing()));
}
void Window::log(int index, QString str)
{
QString text = QString("%1%4: %2 -> %3\n").arg(_edit->toPlainText()).arg(index)
.arg(str).arg(QTime::currentTime().toString());
QTextCursor tmpCursor = _edit->textCursor();
_edit->setText(text);
tmpCursor.movePosition(QTextCursor::End);
_edit->setTextCursor(tmpCursor);
}
void Window::startParsing()
{
if(_text->text().isEmpty())
return;
_edit->setPlainText("");
_button->setText("Отменить");
disconnect(_button, SIGNAL(clicked()), this, SLOT(startParsing()));
connect(_button, SIGNAL(clicked()), this, SLOT(stopParsing()));
auto list = _text->text().split(" ", QString::SkipEmptyParts);
for(int i = 0; i < _tasks.size(); i++)
{
_threads[i]->deleteLater();
_tasks[i]->deleteLater();
}
_tasks.clear();
_threads.clear();
for(int i = 0; i < list.size(); i++)
{
int index = list[i].toInt();
log(index, "Начало");
_threads.append(new QThread());
_tasks.append(new Task(index, qMakePair(_to->date().month(), _to->date().year()),
qMakePair(_from->date().month(), _from->date().year())));
connect(_threads.back(), SIGNAL(started()), _tasks.back(), SLOT(start()));
connect(_tasks.back(), SIGNAL(finished()), _threads.back(), SLOT(quit()));
connect(_tasks.back(), SIGNAL(finished()), this, SLOT(finished()));
connect(_tasks.back(), SIGNAL(log(int,QString)), this, SLOT(log(int,QString)));
_tasks.back()->moveToThread(_threads.back());
}
for(int i = 0; i < list.size(); i++)
{
_threads[i]->start();
}
}
void Window::stopParsing()
{
finished();
for(int i = 0; i < _tasks.size(); i++)
{
_tasks[i]->stop();
}
}
void Window::finished()
{
_button->setText("Начать");
disconnect(_button, SIGNAL(clicked()), this, SLOT(stopParsing()));
connect(_button, SIGNAL(clicked()), this, SLOT(startParsing()));
}