-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.cpp
117 lines (93 loc) · 3.02 KB
/
view.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
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
#include <QFile>
#include <QTextStream>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGraphicsOpacityEffect>
#include <QPropertyAnimation>
#include <QCompleter>
#include <QDebug>
#include <iostream>
#include <cassert>
#include "view.hpp"
View::View() {
cities = new QStringListModel();
search = new QLineEdit();
search->setCompleter([this]() {
QCompleter* completer = new QCompleter(cities);
completer->setCaseSensitivity(Qt::CaseInsensitive);
return completer;
}());
connect(search, SIGNAL(textEdited(const QString&)),
this, SLOT(text_handler(const QString&)));
go = new QPushButton(tr("Поиск"));
connect(go, SIGNAL(clicked()), this, SLOT(go_handler()));
// the next code is all about initializing stack of widgets
QFile file(":resources/no_data.html");
assert(file.open(QIODevice::ReadOnly | QIODevice::Text));
no_data = new QWebView();
no_data->setHtml(QTextStream(&file).readAll());
no_data->setGraphicsEffect([]() {
QGraphicsOpacityEffect* effect = new QGraphicsOpacityEffect();
effect->setOpacity(1.0);
return effect;
}());
weather = new QWebView();
weather->setGraphicsEffect([]() {
QGraphicsOpacityEffect* effect = new QGraphicsOpacityEffect();
effect->setOpacity(1.0);
return effect;
}());
info = new QStackedWidget();
info->addWidget(no_data);
info->addWidget(weather);
info->setCurrentIndex(0);
place_it();
}
View* View::satisfy_weather(const QString& data) {
//show weather
weather->setHtml(data);
info->setCurrentWidget(weather);
//slighty animated
animate_it(weather);
return this;
}
View* View::satisfy_cities(const QStringList& data) {
cities->setStringList(data);
search->completer()->complete();
return this;
}
View* View::damn() {
//show damn
info->setCurrentWidget(no_data);
//slighty animated
animate_it(no_data);
return this;
}
void View::place_it() {
QHBoxLayout* horizontal = new QHBoxLayout();
horizontal->addWidget(search);
horizontal->addWidget(go);
QVBoxLayout* vertical = new QVBoxLayout();
vertical->addLayout(horizontal);
vertical->addWidget(info);
QWidget* content = new QWidget();
content->setLayout(vertical);
setCentralWidget(content);
resize(600, 200);
}
void View::animate_it(QWidget* widget) {
QGraphicsOpacityEffect* effect = static_cast<QGraphicsOpacityEffect*>(widget->graphicsEffect());
QPropertyAnimation* animation = new QPropertyAnimation(effect, "opacity");
animation->setDuration(1000);
animation->setStartValue(0.0);
animation->setEndValue(1.0);
animation->start(QAbstractAnimation::DeleteWhenStopped);
}
void View::go_handler() {
if (search->text().trimmed().length() > 0)
emit desire_weather(search->text().trimmed());
}
void View::text_handler(const QString& city) {
if (city.trimmed().length() > 0)
emit desire_cities(city.trimmed());
}