forked from simulationcraft/simc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebView.cpp
236 lines (208 loc) · 5.82 KB
/
WebView.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// ==========================================================================
// Dedmonwakeen's Raid DPS/TPS Simulator.
// Send questions to [email protected]
// ==========================================================================
#include "WebView.hpp"
#include "MainWindow.hpp"
#include "WebPage.hpp"
#include "util/sc_searchbox.hpp"
#include <QtWidgets/QShortcut>
SC_WebView::SC_WebView( SC_MainWindow* mw, QWidget* parent, const QString& h )
: SC_WebEngineView( parent ),
searchBox( new SC_SearchBox() ),
previousSearch( "" ),
allow_mouse_navigation( false ),
allow_keyboard_navigation( false ),
allow_searching( false ),
mainWindow( mw ),
progress( 0 ),
html_str( h )
{
searchBox->hide();
QShortcut* ctrlF = new QShortcut( QKeySequence( Qt::CTRL + Qt::Key_F ), this );
QShortcut* escape = new QShortcut( QKeySequence( Qt::Key_Escape ), this );
connect( ctrlF, SIGNAL( activated() ), searchBox, SLOT( show() ) );
connect( escape, SIGNAL( activated() ), this, SLOT( hideSearchBox() ) );
connect( searchBox, SIGNAL( textChanged( const QString& ) ), this, SLOT( findSomeText( const QString& ) ) );
connect( searchBox, SIGNAL( findPrev() ), this, SLOT( findPrev() ) );
connect( searchBox, SIGNAL( findNext() ), this, SLOT( findNext() ) );
connect( this, SIGNAL( loadProgress( int ) ), this, SLOT( loadProgressSlot( int ) ) );
connect( this, SIGNAL( loadFinished( bool ) ), this, SLOT( loadFinishedSlot( bool ) ) );
connect( this, SIGNAL( urlChanged( const QUrl& ) ), this, SLOT( urlChangedSlot( const QUrl& ) ) );
SC_WebPage* page = new SC_WebPage( this );
setPage( page );
// Add QT Major Version to avoid "mysterious" problems resulting in qBadAlloc.
QDir dir( mainWindow->TmpDir + QDir::separator() + "simc_webcache_qt" +
std::string( QT_VERSION_STR ).substr( 0, 3 ).c_str() );
if ( !dir.exists() )
dir.mkpath( "." );
QFileInfo fi( dir.absolutePath() );
if ( fi.isDir() && fi.isWritable() )
{
QNetworkDiskCache* diskCache = new QNetworkDiskCache( this );
diskCache->setCacheDirectory( dir.absolutePath() );
QString test = diskCache->cacheDirectory();
}
else
{
qWarning() << "Can't create/write webcache.";
}
}
void SC_WebView::store_html( const QString& s )
{
html_str = s;
}
void SC_WebView::loadHtml()
{
setHtml( html_str );
}
void SC_WebView::enableMouseNavigation()
{
allow_mouse_navigation = true;
}
void SC_WebView::disableMouseNavigation()
{
allow_mouse_navigation = false;
}
void SC_WebView::enableKeyboardNavigation()
{
allow_keyboard_navigation = true;
}
void SC_WebView::disableKeyboardNavigation()
{
allow_keyboard_navigation = false;
}
void SC_WebView::update_progress( int p )
{
progress = p;
mainWindow->updateWebView( this );
}
void SC_WebView::mouseReleaseEvent( QMouseEvent* e )
{
if ( allow_mouse_navigation )
{
switch ( e->button() )
{
case Qt::XButton1:
back();
break;
case Qt::XButton2:
forward();
break;
default:
break;
}
}
SC_WebEngineView::mouseReleaseEvent( e );
}
void SC_WebView::keyReleaseEvent( QKeyEvent* e )
{
if ( allow_keyboard_navigation )
{
int k = e->key();
Qt::KeyboardModifiers m = e->modifiers();
switch ( k )
{
case Qt::Key_R:
{
if ( m.testFlag( Qt::ControlModifier ) == true )
reload();
}
break;
case Qt::Key_F5:
{
reload();
}
break;
case Qt::Key_Left:
{
if ( m.testFlag( Qt::AltModifier ) == true )
back();
}
break;
case Qt::Key_Right:
{
if ( m.testFlag( Qt::AltModifier ) == true )
forward();
}
break;
default:
break;
}
}
SC_WebEngineView::keyReleaseEvent( e );
}
void SC_WebView::resizeEvent( QResizeEvent* e )
{
searchBox->updateGeometry();
SC_WebEngineView::resizeEvent( e );
}
void SC_WebView::focusInEvent( QFocusEvent* e )
{
hideSearchBox();
SC_WebEngineView::focusInEvent( e );
}
void SC_WebView::loadProgressSlot( int p )
{
update_progress( p );
}
void SC_WebView::loadFinishedSlot( bool /* ok */ )
{
update_progress( 100 );
}
void SC_WebView::urlChangedSlot( const QUrl& url )
{
url_to_show = url.toString();
if ( url_to_show == "about:blank" )
{
url_to_show = "results.html";
}
mainWindow->updateWebView( this );
}
void SC_WebView::hideSearchBox()
{
previousSearch = ""; // disable clearing of highlighted text on next search
searchBox->hide();
}
void SC_WebView::findSomeText( const QString& text, SC_WebEnginePage::FindFlags options )
{
findText( text, options );
// If the QWebView scrolls due to searching with the SC_SearchBox visible
// The SC_SearchBox could still be visible, as the area the searchbox is covering
// is not redrawn, just moved
// *HACK*
// This just repaints the area directly above the search box to the top, in a huge rectangle
QRect searchBoxRect = searchBox->rect();
searchBoxRect.moveTopLeft( searchBox->geometry().topLeft() );
switch ( searchBox->getCorner() )
{
case Qt::TopLeftCorner:
case Qt::BottomLeftCorner:
searchBoxRect.setTopLeft( QPoint( 0, 0 ) );
searchBoxRect.setBottomLeft( rect().bottomLeft() );
break;
case Qt::TopRightCorner:
case Qt::BottomRightCorner:
searchBoxRect.setTopRight( rect().topRight() );
searchBoxRect.setBottomRight( rect().bottomRight() );
break;
}
repaint( searchBoxRect );
}
void SC_WebView::findSomeText( const QString& text )
{
SC_WebEnginePage::FindFlags flags = 0;
if ( searchBox->reverseSearch() )
{
flags |= SC_WebEnginePage::FindBackward;
}
findSomeText( text, flags );
}
void SC_WebView::findPrev()
{
findSomeText( searchBox->text(), SC_WebEnginePage::FindBackward );
}
void SC_WebView::findNext()
{
findSomeText( searchBox->text(), 0 );
}