-
Notifications
You must be signed in to change notification settings - Fork 14
/
webview.cpp
596 lines (536 loc) · 20.8 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
* Copyright 2013 KanMemo Project.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "webview.h"
#include <QtCore/QDebug>
#include <QtWebKitWidgets/QWebFrame>
#include <QtWebKit/QWebElement>
#include <QMenu>
#include <QWebHitTestResult>
#include <QContextMenuEvent>
class WebView::Private
{
public:
Private(WebView *parent);
private:
void showNormal();
void showFullScreen();
void showOptionalSize(int width, int height, bool isfullscreen);
private:
WebView *q;
public:
ViewMode viewMode;
QRect defaultRect;
QWebFrame *searchGameFrame(QWebFrame *frame);
private:
QHash<QString, QString> naviApp;
QHash<QString, QString> foot;
QHash<QString, QString> body;
QHash<QString, QString> gameFrame;
QHash<QString, QString> flashWrap;
QHash<QString, QString> embed;
QHash<QString, QString> sectionWrap;
QHash<QString, QString> globalNavi;
QHash<QString, QString> dmmNtgnavi;
QHash<QString, QString> dmmNtgnaviInner;
bool setElementProperty(QWebElement &element, QHash<QString, QString> &properties, QHash<QString, QString> &backup);
};
WebView::Private::Private(WebView *parent)
: q(parent)
, viewMode(NormalMode)
{
//表示モードの変更
connect(q, &WebView::viewModeChanged, [this](ViewMode viewMode) {
switch (viewMode) {
case WebView::NormalMode:
showNormal();
break;
case WebView::FullScreenMode:
showFullScreen();
break;
}
});
//ズームの変更
connect(q, &WebView::gameSizeFactorChanged, [this](qreal factor) {
if(!defaultRect.isValid()){
defaultRect = q->getGameRect();
}
showOptionalSize(defaultRect.width() * factor, defaultRect.height() * factor, false);
});
}
void WebView::Private::showFullScreen()
{
//normal -> full
showOptionalSize(q->window()->width(), q->window()->height(), true);
}
void WebView::Private::showOptionalSize(int width, int height, bool isfull)
{
//ゲーム画面有るか
if(!q->gameExists()) return;
QHash<QString, QString> properties;
QWebFrame *frame = q->page()->mainFrame();
///////////////////////////////////////
//スクロールバー非表示
QWebElement element = frame->findFirstElement(QStringLiteral("body"));
if(!isfull){
properties.insert(QStringLiteral("overflow"), QStringLiteral(""));
properties.insert(QStringLiteral("min-width"), QString("%1px").arg(width+188));
}else{
//フルスクリーンのみ
properties.insert(QStringLiteral("overflow"), QStringLiteral("hidden"));
properties.insert(QStringLiteral("min-width"), QString("%1px").arg(width+188));
}
if(!setElementProperty(element, properties, body)){
qDebug() << "failed find target body";
return;
}
properties.clear();
/////////////////////////////////////////
//上のバー消す
element = frame->findFirstElement(QStringLiteral("#main-ntg"));
if(!isfull){
properties.insert(QStringLiteral("position"), QStringLiteral("relative"));
// properties.insert(QStringLiteral("margin"), QStringLiteral("0 -12px 20px"));
// properties.insert(QStringLiteral("visibility"), QStringLiteral(""));
properties.insert(QStringLiteral("top"), QStringLiteral(""));
properties.insert(QStringLiteral("left"), QStringLiteral(""));
}else{
//フルスクリーンのみ
properties.insert(QStringLiteral("position"), QStringLiteral("absolute"));
// properties.insert(QStringLiteral("margin"), QStringLiteral("0"));
// properties.insert(QStringLiteral("visibility"), QStringLiteral("hidden"));
properties.insert(QStringLiteral("top"), QStringLiteral("0px"));
properties.insert(QStringLiteral("left"), QStringLiteral("0px"));
}
if(!setElementProperty(element, properties, dmmNtgnavi)){
qDebug() << "failed find target #dmm-ntgnavi";
return;
}
properties.clear();
/////////////////////////////////////////
//フレームを最大化
element = frame->findFirstElement(QStringLiteral("#game_frame"));
if (element.isNull()) {
qDebug() << "failed find target #game_frame";
// ui.statusBar->showMessage(tr("failed find target"), STATUS_BAR_MSG_TIME);
return;
}
if(!isfull){
//フルスクリーンじゃない
properties.insert(QStringLiteral("position"), QStringLiteral(""));
properties.insert(QStringLiteral("top"), QStringLiteral(""));
properties.insert(QStringLiteral("left"), QStringLiteral(""));
properties.insert(QStringLiteral("width"), QString("%1px").arg(width+100));
properties.insert(QStringLiteral("height"), QString("%1px").arg(height+100));
properties.insert(QStringLiteral("z-index"), QStringLiteral(""));
}else{
properties.insert(QStringLiteral("position"), QStringLiteral("absolute"));
properties.insert(QStringLiteral("top"), QStringLiteral("0px"));
properties.insert(QStringLiteral("left"), QStringLiteral("0px"));
properties.insert(QStringLiteral("width"), QString("%1px").arg(width+100));
properties.insert(QStringLiteral("height"), QString("%1px").arg(height+100));
properties.insert(QStringLiteral("z-index"), QStringLiteral("10"));
}
if(!setElementProperty(element, properties, gameFrame)){
qDebug() << "failed find target game_frame tag";
return;
}
properties.clear();
/////////////////////////////////////////
//フレームの子供からflashの入ったdivを探して、さらにその中のembedタグを調べる
frame = searchGameFrame(frame);
element = frame->findFirstElement(QStringLiteral("#flashWrap"));
if(!isfull){
//フルスクリーンじゃない
properties.insert(QStringLiteral("position"), QStringLiteral(""));
properties.insert(QStringLiteral("top"), QStringLiteral(""));
properties.insert(QStringLiteral("left"), QStringLiteral(""));
properties.insert(QStringLiteral("width"), QString("%1px").arg(width));
properties.insert(QStringLiteral("height"), QString("%1px").arg(height));
}else{
properties.insert(QStringLiteral("position"), QStringLiteral("absolute"));
properties.insert(QStringLiteral("top"), QStringLiteral("0px"));
properties.insert(QStringLiteral("left"), QStringLiteral("0px"));
properties.insert(QStringLiteral("width"), QString("%1px").arg(width));
properties.insert(QStringLiteral("height"), QString("%1px").arg(height));
}
if(!setElementProperty(element, properties, flashWrap)){
qDebug() << "failed find target flashWrap";
return;
}
properties.clear();
/////////////////////////////////////////
//embedタグを探す
element = element.findFirst(QStringLiteral("embed"));
if (element.isNull()) {
qDebug() << "failed find target embed";
return;
}
properties.insert(QStringLiteral("width"), QString::number(width));
properties.insert(QStringLiteral("height"), QString::number(height));
if(embed.isEmpty()){
foreach (const QString &key, properties.keys()) {
embed.insert(key, element.attribute(key));
}
qDebug() << element.attribute(QStringLiteral("width"))
<< "," << element.attribute(QStringLiteral("height"))
<< "->" << width << "," << height;
}
foreach (const QString &key, properties.keys()) {
element.evaluateJavaScript(QString("this.%1='%2'").arg(key).arg(properties.value(key)));
}
properties.clear();
/////////////////////////////////////////
//解説とか消す
element = frame->findFirstElement(QStringLiteral("#sectionWrap"));
if(!isfull){
// properties.insert(QStringLiteral("width"), QString("%1px").arg(width));
properties.insert(QStringLiteral("visibility"), QStringLiteral(""));
}else{
//フルスクリーンのみ
// properties.insert(QStringLiteral("width"), QString("%1px").arg(width));
properties.insert(QStringLiteral("visibility"), QStringLiteral("hidden"));
}
if(!setElementProperty(element, properties, sectionWrap)){
qDebug() << "failed find target #sectionWrap";
return;
}
properties.clear();
/////////////////////////////////////////
//フレームの中の上の隙間
element = frame->findFirstElement(QStringLiteral("#spacing_top"));
if(!isfull){
properties.insert(QStringLiteral("height"), QStringLiteral("16px"));
}else{
//フルスクリーンのみ
properties.insert(QStringLiteral("height"), QStringLiteral("0px"));
}
if(!setElementProperty(element, properties, globalNavi)){
qDebug() << "failed find target spacing_top";
return;
}
properties.clear();
}
//ゲームフレームを検索する
QWebFrame *WebView::Private::searchGameFrame(QWebFrame *frame)
{
for(int i=0;i<frame->childFrames().count();i++){
qDebug() << "frame name:" << i << ":" << frame->childFrames().at(i)->frameName();
if(frame->childFrames().at(i)->frameName() == "game_frame"){
//発見
frame = frame->childFrames().at(i);
break;
}
}
return frame;
}
//エレメントにプロパティを設定する(初回はバックアップする)
bool WebView::Private::setElementProperty(QWebElement &element, QHash<QString, QString> &properties, QHash<QString, QString> &backup)
{
if(element.isNull()){
qDebug() << "failed find target";
return false;
}
QString text;
if(backup.isEmpty()){
foreach (const QString &key, properties.keys()) {
backup.insert(key, element.styleProperty(key, QWebElement::InlineStyle));
text.append(QString("%1(%2) ").arg(key).arg(element.styleProperty(key, QWebElement::InlineStyle)));
}
qDebug() << text;
}
foreach (const QString &key, properties.keys()) {
if(properties.value(key).length() > 0){
element.setStyleProperty(key, properties.value(key));
}
}
return true;
}
void WebView::Private::showNormal()
{
//full -> normal
QWebFrame *frame = q->page()->mainFrame();
/////////////////////////////////////////
//スクロールバー表示
QWebElement element = frame->findFirstElement(QStringLiteral("body"));
if (element.isNull()) {
qDebug() << "failed find target";
return;
}
//もとに戻す
foreach (const QString &key, body.keys()) {
element.setStyleProperty(key, body.value(key));
}
/////////////////////////////////////////
//フレーム
element = frame->findFirstElement(QStringLiteral("#game_frame"));
if (element.isNull()) {
qDebug() << "failed find target";
return;
}
//もとに戻す
foreach (const QString &key, gameFrame.keys()) {
element.setStyleProperty(key, gameFrame.value(key));
}
/////////////////////////////////////////
//フレームの子供からflashの入ったdivを探して、さらにその中のembedタグを調べる
frame = searchGameFrame(frame);
element = frame->findFirstElement(QStringLiteral("#flashWrap"));
if (element.isNull()) {
qDebug() << "failed find target";
return;
}
//もとに戻す
foreach (const QString &key, flashWrap.keys()) {
element.setStyleProperty(key, flashWrap.value(key));
}
/////////////////////////////////////////
element = element.findFirst(QStringLiteral("embed"));
if (element.isNull()) {
qDebug() << "failed find target";
return;
}
//もとに戻す
foreach (const QString &key, embed.keys()) {
element.evaluateJavaScript(QStringLiteral("this.%1='%2'").arg(key).arg(embed.value(key)));
}
/////////////////////////////////////////
//解説とか
element = frame->findFirstElement(QStringLiteral("#sectionWrap"));
if (element.isNull()) {
qDebug() << "failed find target";
return;
}
//もとに戻す
foreach (const QString &key, sectionWrap.keys()) {
element.setStyleProperty(key, sectionWrap.value(key));
}
/////////////////////////////////////////
//友達招待のバルーンを移動させるためにサイズ調整
element = frame->findFirstElement(QStringLiteral("#globalNavi"));
element = element.findFirst(QStringLiteral("p"));
if (element.isNull()) {
qDebug() << "failed find target";
return;
}
//もとに戻す
foreach (const QString &key, globalNavi.keys()) {
element.setStyleProperty(key, globalNavi.value(key));
}
}
WebView::WebView(QWidget *parent)
: QWebView(parent)
, d(new Private(this))
, m_disableContextMenu(false)
{
setAttribute(Qt::WA_AcceptTouchEvents, false);
connect(this, &QObject::destroyed, [this]() { delete d; });
#if 0 //
connect(this, &QWebView::loadFinished, [this](bool ok) {
if(ok){
qDebug() << "2 loadFinished " << url() << " game exist " << gameExists();
QWebFrame *frame = page()->mainFrame();
connect(frame->page(), &QWebPage::contentsChanged, [this](){
qDebug() << "2 frame contentsChanged " << gameExists();
});
if(frame->childFrames().isEmpty()){
qDebug() << "2 child frame empty";
return;
}
frame = frame->childFrames().first();
connect(frame, &QWebFrame::loadFinished, [this](bool ok){
qDebug() << "3 frame loadFinished " << ok << " game exist " << gameExists();
});
connect(frame->page(), &QWebPage::contentsChanged, [this](){
qDebug() << "3 frame contentsChanged " << gameExists();
});
connect(frame->page(), &QWebPage::loadFinished, [this](bool ok){
qDebug() << " frame loadFinished " << gameExists();
});
QWebElement element = frame->findFirstElement(QStringLiteral("#flashWrap"));
if (element.isNull()) {
qDebug() << "none flashWrap";
return;
}
qDebug() << "flashWrap : " << element.toPlainText();
}
});
connect(page(), &QWebPage::contentsChanged, [this](){
qDebug() << "1 contentsChanged " << gameExists();
});
connect(page(), &QWebPage::frameCreated, [this](QWebFrame *frame){
qDebug() << "1 frameCreated " << frame->url() << "," << frame->childFrames().length();
connect(frame, &QWebFrame::loadFinished, [this](bool ok){
qDebug() << "1 loadFinished " << ok << " game exist " << gameExists();
});
if(frame->childFrames().isEmpty()){
qDebug() << "1 child frame empty";
return;
}
frame = frame->childFrames().first();
connect(frame, &QWebFrame::loadFinished, [this](bool ok){
qDebug() << "1 frame loadFinished " << ok << " game exist " << gameExists();
});
connect(frame->page(), &QWebPage::contentsChanged, [this](){
qDebug() << "1 frame contentsChanged " << gameExists();
});
});
#endif
}
bool WebView::gameExists() const
{
return getGameRect().isValid();
}
//ゲームの領域を調べる
QRect WebView::getGameRect() const
{
//フレームを取得
QWebFrame *frame = page()->mainFrame();
if (frame->childFrames().isEmpty()) {
return QRect();
}
//フレームの子供からflashの入ったdivを探す。さらにその中のembedタグを調べる
frame = d->searchGameFrame(frame);
QWebElement element = frame->findFirstElement(QStringLiteral("#flashWrap"));
if (element.isNull()) {
qDebug() << "not found #flashWrap";
return QRect();
}
element = element.findFirst(QStringLiteral("embed"));
if (element.isNull()) {
qDebug() << "not found embed";
return QRect();
}
//見つけたタグの座標を取得
QRect geometry = element.geometry();
geometry.moveTopLeft(geometry.topLeft() + frame->geometry().topLeft());
qDebug() << "flash geo:" << geometry;
return geometry;
}
//ゲーム画面をキャプチャ
QImage WebView::capture(bool adjustScrollPosition)
{
QImage ret;
QImage temp;
//スクロール位置の保存
QPoint currentPos = page()->mainFrame()->scrollPosition();
//表示位置を一番上へ強制移動
if(adjustScrollPosition){
page()->mainFrame()->setScrollPosition(QPoint(0, 0));
}
QRect geometry = getGameRect();
if (!geometry.isValid()) {
emit error(tr("failed find target"));
goto finally;
}
if(!adjustScrollPosition){
geometry.moveTo(geometry.x() - currentPos.x(), geometry.y() - currentPos.y());
}
{
QRect image_geo(geometry);
if(d->defaultRect.isValid()){
if(image_geo.width() != d->defaultRect.width() || image_geo.height() != d->defaultRect.height()){
image_geo.setWidth(d->defaultRect.width());
image_geo.setHeight(d->defaultRect.height());
}
}
ret = QImage(image_geo.size(), QImage::Format_ARGB32); //最終形
temp = QImage(geometry.size(), QImage::Format_ARGB32); //Webviewのコピー
QPainter painter(&ret);
QPainter painterTemp(&temp);
if(image_geo.width() != geometry.width() || image_geo.height() != geometry.height()){
//テンポラリに全体を描画
render(&painterTemp, QPoint(0,0), geometry);
painter.drawImage(0, 0, temp.scaled(image_geo.width(), image_geo.height(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
}else{
//そのまま本番に描画
render(&painter, QPoint(0,0), geometry);
}
}
finally:
//スクロールの位置を戻す
if(adjustScrollPosition){
page()->mainFrame()->setScrollPosition(currentPos);
}
return ret;
}
WebView::ViewMode WebView::viewMode() const
{
return d->viewMode;
}
void WebView::setViewMode(ViewMode viewMode)
{
if (d->viewMode == viewMode) return;
d->viewMode = viewMode;
emit viewModeChanged(viewMode);
}
//リンクをクリックした時のメニューをつくる
void WebView::contextMenuEvent(QContextMenuEvent *event)
{
QWebHitTestResult r = page()->mainFrame()->hitTestContent(event->pos());
//背景をクリック
if(!r.boundingRect().isEmpty()){
//右クリックメニュー無効
if(disableContextMenu()){
return;
}
}
//リンクをクリック
if(!r.linkUrl().isEmpty()){
QMenu menu(this);
menu.addAction(tr("Open in New Tab"), this, SLOT(openLinkInNewTab()));
menu.addSeparator();
menu.addAction(pageAction(QWebPage::CopyLinkToClipboard));
menu.exec(mapToGlobal(event->pos()));
return;
}
QWebView::contextMenuEvent(event);
}
//WebViewへのクリックイベント
void WebView::mousePressEvent(QMouseEvent *event)
{
emit mousePressed(event);
QWebView::mousePressEvent(event);
}
//タブで開くのトリガー
void WebView::openLinkInNewTab()
{
pageAction(QWebPage::OpenLinkInNewWindow)->trigger();
}
//ゲームの画面サイズ
qreal WebView::getGameSizeFactor() const
{
return gameSizeFactor;
}
//ゲームの画面サイズ
void WebView::setGameSizeFactor(const qreal &factor)
{
if(gameSizeFactor == factor) return;
if(factor < 0){
gameSizeFactor = 0;
}else{
gameSizeFactor = factor;
}
emit gameSizeFactorChanged(factor);
}
//右クリックメニューを無効化する
bool WebView::disableContextMenu() const
{
return m_disableContextMenu;
}
void WebView::setDisableContextMenu(bool disableContextMenu)
{
m_disableContextMenu = disableContextMenu;
}