File: browser.cpp

package info (click to toggle)
algobox 0.9%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 1,732 kB
  • sloc: cpp: 12,047; makefile: 11
file content (117 lines) | stat: -rw-r--r-- 3,586 bytes parent folder | download | duplicates (2)
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
/***************************************************************************
 *   copyright       : (C) 2003-2011 by Pascal Brachet                     *
 *   http://www.xm1math.net/algobox/                                       *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
 
#include "browser.h"
#include "geticon.h"

#include <QtGui>
#include <QPrinter>
#include <QMenuBar>
#include <QToolBar>
#include <QPrintDialog>
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
#include "QtWebKitWidgets/qwebframe.h"
#else
#include <QWebFrame>
#endif


Browser::Browser( const QString home, QWidget* parent)
    : QMainWindow( parent)
{
setWindowTitle("AlgoBox");
#if defined(Q_OS_MAC)
setWindowIcon(QIcon(":/images/algobox128.png"));
#else
setWindowIcon(getIcon(":/images/algobox22.png"));
#endif
setIconSize(QSize(22,22 ));
progress = 0;
view = new QWebView(this);
if ( !home.isEmpty()) view->load(QUrl(home));
connect(view, SIGNAL(titleChanged(QString)), SLOT(adjustTitle()));
connect(view, SIGNAL(loadProgress(int)), SLOT(setProgress(int)));
connect(view, SIGNAL(loadFinished(bool)), SLOT(finishLoading(bool)));

QMenu *fileMenu = menuBar()->addMenu(QString::fromUtf8("&Fichier"));
fileMenu->addAction(QString::fromUtf8("&Imprimer"), this, SLOT(Print()));
fileMenu->addSeparator();
fileMenu->addAction(QString::fromUtf8("&Quitter"), this, SLOT(close()));

QToolBar *toolBar = addToolBar("Navigation");
QAction *Act;
Act = new QAction(getIcon(":/images/home.png"), QString::fromUtf8("Sommaire"), this);
connect(Act, SIGNAL(triggered()), this, SLOT(Sommaire()));
toolBar->addAction(Act);
toolBar->addAction(view->pageAction(QWebPage::Back));
toolBar->addAction(view->pageAction(QWebPage::Forward));

QWidget* spacer = new QWidget();
spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
toolBar->addWidget(spacer);
searchLineEdit = new QLineEdit(toolBar);
connect(searchLineEdit, SIGNAL(returnPressed()), this, SLOT(Chercher()));
toolBar->addWidget(searchLineEdit);

findButton=new QPushButton(QString::fromUtf8("Chercher"),toolBar);
connect(findButton, SIGNAL(clicked()), this, SLOT(Chercher()));
toolBar->addWidget(findButton);

setCentralWidget(view);
setUnifiedTitleAndToolBarOnMac(true);
    
//resize(780,580 );
}

Browser::~Browser()
{

}

void Browser::adjustTitle()
{
if (progress <= 0 || progress >= 100)
    setWindowTitle(view->title());
else
    setWindowTitle(QString("%1 (%2%)").arg(view->title()).arg(progress));
}

void Browser::setProgress(int p)
{
progress = p;
adjustTitle();
}

void Browser::finishLoading(bool)
{
progress = 100;
adjustTitle();
}

void Browser::Sommaire()
{
view->page()->mainFrame()->evaluateJavaScript("window.location.href='#SOMMAIRE';");
}

void Browser::Print()
{
QPrinter printer;
QPrintDialog *dialog = new QPrintDialog(&printer, this);
dialog->setWindowTitle(QString::fromUtf8("Imprimer"));
if (dialog->exec() != QDialog::Accepted) return;
view->page()->mainFrame()->print(&printer);
}

void Browser::Chercher()
{
if (searchLineEdit->text().isEmpty()) return;
view->findText(searchLineEdit->text(),QWebPage::FindWrapsAroundDocument);
}