/
rulesviewer.cpp
195 lines (173 loc) · 5.7 KB
/
rulesviewer.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "dialogconfigure.h"
#include "rulesviewer.h"
#include <QDebug>
#include <QGraphicsItem>
#include <QCursor>
#include <QKeyEvent>
#include <QPoint>
#include <QSize>
#include "rules.h"
int FormRulesViewer::current_page = 1;
FormRulesViewer::FormRulesViewer(QWidget* parent, Qt::WindowFlags flags )
: QWidget(parent, flags)
, ui(new Ui::FormRulesViewer)
{
if (Rules::sample()->doc()) {
close();
}
ui->setupUi(this);
ui->graphicsView->setScene(Rules::sample());
ui->lineEditPageLimit->setText(QString::number(Rules::sample()->doc()->numPages()));
ui->spinBoxCurrentPage->setMaximum(Rules::sample()->doc()->numPages());
setWindowTitle(tr("Competition rules"));
for (int id = 0; id< Rules::sample()->doc()->infoKeys().count(); id++) {
if (Rules::sample()->doc()->infoKeys().at(id) == "Title") {
if (Rules::sample()->doc()->info("Title").length() >= 3) setWindowTitle(Rules::sample()->doc()->info("Title"));
}
}
connect(ui->spinBoxCurrentPage, SIGNAL(valueChanged(int)), SLOT(setPage(int)));
ui->graphicsView->installEventFilter(this);
ui->graphicsView->adjustSize();
setPage(current_page);
ui->lineEditFind->setText(Rules::sample()->searchText());
if (Rules::sample()->searched()->isEmpty()) {
ui->widgetItemsInfo->setHidden(true);
ui->toolButtonNext->setDisabled(true);
ui->toolButtonPrevious->setDisabled(true);
} else {
ui->toolButtonNext->setEnabled(true);
ui->toolButtonPrevious->setEnabled(true);
ui->widgetItemsInfo->setShown(true);
ui->labelCountItem->setText(QString::number(Rules::sample()->searched()->count()));
ui->labelCurrentItem->setText(QString::number(Rules::sample()->currentResult()+1));
}
}
FormRulesViewer::~FormRulesViewer()
{
}
void FormRulesViewer::setPage(const int& page)
{
if ((page > 0)&(page <= Rules::sample()->doc()->numPages())) {
ui->graphicsView->centerOn(Rules::sample()->pager()->at(page-1));
current_page = page-1;
}
}
void FormRulesViewer::pageResize(const int& scale)
{
ui->graphicsView->resetMatrix();
ui->graphicsView->scale(((qreal)scale)/100, ((qreal)scale)/100);
}
bool FormRulesViewer::eventFilter(QObject* sender, QEvent* event)
{
bool ret = true;
bool is_recived = false;
int pos_x, pos_y;
if (sender == ui->graphicsView) {
switch (event->type()) {
case QEvent::Wheel :
pos_x = ((QWheelEvent*)event)->x();
pos_y = ((QWheelEvent*)event)->y();
pageNum(pos_x, pos_y);
event->ignore();
break;
case QEvent::HoverMove :
pos_x = ((QHoverEvent*)event)->pos().x();
pos_y = ((QHoverEvent*)event)->pos().y();
pageNum(pos_x, pos_y);
break;
case QEvent::KeyPress :
switch (((QKeyEvent*)event)->key()) {
case Qt::Key_PageDown :
ui->spinBoxCurrentPage->setValue(ui->spinBoxCurrentPage->value()+1);
is_recived = true;
event->accept();
break;
case Qt::Key_PageUp :
ui->spinBoxCurrentPage->setValue(ui->spinBoxCurrentPage->value()-1);
is_recived = true;
event->accept();
break;
case Qt::Key_Home :
ui->spinBoxCurrentPage->setValue(1);
is_recived = true;
event->accept();
break;
default:
break;
}
break;
/* case QEvent::KeyRelease :
switch (((QKeyEvent*)event)->key()) {
default:
break;
}*/
break;
default:
//qDebug() << event->type();
break;
}
}
if (!is_recived) ret = QObject::eventFilter(sender, event);
return ret;
}
void FormRulesViewer::findNext()
{
int current;
if (!Rules::sample()->searched()->isEmpty()) {
if (Rules::sample()->currentResult() >= 0) current = Rules::sample()->currentResult()+1;
if (Rules::sample()->currentResult() < 0) current = 0;
if (current >= Rules::sample()->searched()->count()) current = 0;
Rules::sample()->setCurrentResult(current);
ui->graphicsView->centerOn(Rules::sample()->searched()->at(current));
ui->labelCurrentItem->setText(QString::number(current+1));
}
}
void FormRulesViewer::findPrevious()
{
int current;
if (!Rules::sample()->searched()->isEmpty()) {
if (Rules::sample()->currentResult() >= 0) current = Rules::sample()->currentResult()-1;
if (Rules::sample()->currentResult() < 0) current = Rules::sample()->searched()->count()-1;
if (current < 0) current = Rules::sample()->searched()->count()-1;;
Rules::sample()->setCurrentResult(current);
ui->graphicsView->centerOn(Rules::sample()->searched()->at(current));
ui->labelCurrentItem->setText(QString::number(current+1));
}
}
void FormRulesViewer::startSearch()
{
if (ui->lineEditFind->text().simplified().isEmpty()) {
ui->widgetItemsInfo->setShown(false);
Rules::sample()->searchClean();
}
else {
ui->labelCountItem->setText(QString::number(Rules::sample()->search (ui->lineEditFind->text())->count()));
if (!Rules::sample()->searched()->isEmpty()) {
ui->widgetItemsInfo->setShown(true);
ui->toolButtonNext->setEnabled(true);
ui->toolButtonPrevious->setEnabled(true);
// TODO get ferst result from bottom of the current point
Rules::sample()->setCurrentResult(0);
ui->graphicsView->centerOn(Rules::sample()->searched()->at(0));
ui->labelCurrentItem->setText(QString::number(Rules::sample()->currentResult()+1));
} else {
ui->toolButtonNext->setDisabled(true);
ui->toolButtonPrevious->setDisabled(true);
ui->widgetItemsInfo->setShown(false);
}
}
}
void FormRulesViewer::pageNum(int pos_x, int pos_y)
{
for (int id = 0; id < ui->graphicsView->items(pos_x, pos_y).count(); id++) {
for (int j = 0; j < Rules::sample()->doc()->numPages(); j++) {
if (ui->graphicsView->items(pos_x, pos_y).at(id) == Rules::sample()->pager()->at(j)) {
current_page = j+1;
ui->spinBoxCurrentPage->blockSignals(true);
ui->spinBoxCurrentPage->setValue(current_page);
ui->spinBoxCurrentPage->blockSignals(false);
}
}
}
}
#include "rulesviewer.moc"