1 /* |
|
2 * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies) |
|
3 * Copyright (C) 2009 Girish Ramakrishnan <girish@forwardbias.in> |
|
4 * Copyright (C) 2006 George Staikos <staikos@kde.org> |
|
5 * Copyright (C) 2006 Dirk Mueller <mueller@kde.org> |
|
6 * Copyright (C) 2006 Zack Rusin <zack@kde.org> |
|
7 * Copyright (C) 2006 Simon Hausmann <hausmann@kde.org> |
|
8 * |
|
9 * All rights reserved. |
|
10 * |
|
11 * Redistribution and use in source and binary forms, with or without |
|
12 * modification, are permitted provided that the following conditions |
|
13 * are met: |
|
14 * 1. Redistributions of source code must retain the above copyright |
|
15 * notice, this list of conditions and the following disclaimer. |
|
16 * 2. Redistributions in binary form must reproduce the above copyright |
|
17 * notice, this list of conditions and the following disclaimer in the |
|
18 * documentation and/or other materials provided with the distribution. |
|
19 * |
|
20 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
|
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
|
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
|
28 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
31 */ |
|
32 |
|
33 #include "mainwindow.h" |
|
34 |
|
35 #include "locationedit.h" |
|
36 #include "utils.h" |
|
37 |
|
38 MainWindow::MainWindow(const QString& url) |
|
39 : m_page(new WebPage(this)) |
|
40 { |
|
41 setAttribute(Qt::WA_DeleteOnClose); |
|
42 if (qgetenv("QTTESTBROWSER_USE_ARGB_VISUALS").toInt() == 1) |
|
43 setAttribute(Qt::WA_TranslucentBackground); |
|
44 |
|
45 buildUI(); |
|
46 } |
|
47 |
|
48 void MainWindow::buildUI() |
|
49 { |
|
50 QToolBar* bar = addToolBar("Navigation"); |
|
51 #if defined(Q_WS_S60) |
|
52 bar->setIconSize(QSize(16, 16)); |
|
53 #endif |
|
54 QAction* reloadAction = page()->action(QWebPage::Reload); |
|
55 connect(reloadAction, SIGNAL(triggered()), this, SLOT(changeLocation())); |
|
56 |
|
57 bar->addAction(page()->action(QWebPage::Back)); |
|
58 bar->addAction(page()->action(QWebPage::Forward)); |
|
59 bar->addAction(reloadAction); |
|
60 bar->addAction(page()->action(QWebPage::Stop)); |
|
61 |
|
62 urlEdit = new LocationEdit(this); |
|
63 urlEdit->setSizePolicy(QSizePolicy::Expanding, urlEdit->sizePolicy().verticalPolicy()); |
|
64 connect(urlEdit, SIGNAL(returnPressed()), SLOT(changeLocation())); |
|
65 QCompleter* completer = new QCompleter(this); |
|
66 urlEdit->setCompleter(completer); |
|
67 completer->setModel(&urlModel); |
|
68 #if defined(Q_WS_S60) |
|
69 addToolBarBreak(); |
|
70 addToolBar("Location")->addWidget(urlEdit); |
|
71 #else |
|
72 bar->addWidget(urlEdit); |
|
73 #endif |
|
74 |
|
75 connect(page()->mainFrame(), SIGNAL(titleChanged(const QString&)), |
|
76 this, SLOT(setWindowTitle(const QString&))); |
|
77 connect(page()->mainFrame(), SIGNAL(urlChanged(QUrl)), this, SLOT(setAddressUrl(QUrl))); |
|
78 connect(page(), SIGNAL(loadProgress(int)), urlEdit, SLOT(setProgress(int))); |
|
79 connect(page(), SIGNAL(windowCloseRequested()), this, SLOT(close())); |
|
80 |
|
81 // short-cuts |
|
82 page()->action(QWebPage::Back)->setShortcut(QKeySequence::Back); |
|
83 page()->action(QWebPage::Stop)->setShortcut(Qt::Key_Escape); |
|
84 page()->action(QWebPage::Forward)->setShortcut(QKeySequence::Forward); |
|
85 page()->action(QWebPage::Reload)->setShortcut(QKeySequence::Refresh); |
|
86 page()->action(QWebPage::Undo)->setShortcut(QKeySequence::Undo); |
|
87 page()->action(QWebPage::Redo)->setShortcut(QKeySequence::Redo); |
|
88 page()->action(QWebPage::Cut)->setShortcut(QKeySequence::Cut); |
|
89 page()->action(QWebPage::Copy)->setShortcut(QKeySequence::Copy); |
|
90 page()->action(QWebPage::Paste)->setShortcut(QKeySequence::Paste); |
|
91 |
|
92 page()->action(QWebPage::ToggleBold)->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_B)); |
|
93 page()->action(QWebPage::ToggleItalic)->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_I)); |
|
94 page()->action(QWebPage::ToggleUnderline)->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_U)); |
|
95 } |
|
96 |
|
97 WebPage* MainWindow::page() |
|
98 { |
|
99 return m_page; |
|
100 } |
|
101 |
|
102 void MainWindow::setAddressUrl(const QUrl& url) |
|
103 { |
|
104 urlEdit->setText(url.toString(QUrl::RemoveUserInfo)); |
|
105 } |
|
106 |
|
107 void MainWindow::setAddressUrl(const QString& url) |
|
108 { |
|
109 urlEdit->setText(url); |
|
110 } |
|
111 |
|
112 void MainWindow::addCompleterEntry(const QUrl& url) |
|
113 { |
|
114 QUrl::FormattingOptions opts; |
|
115 opts |= QUrl::RemoveScheme; |
|
116 opts |= QUrl::RemoveUserInfo; |
|
117 opts |= QUrl::StripTrailingSlash; |
|
118 QString s = url.toString(opts); |
|
119 s = s.mid(2); |
|
120 if (s.isEmpty()) |
|
121 return; |
|
122 |
|
123 if (!urlList.contains(s)) |
|
124 urlList += s; |
|
125 urlModel.setStringList(urlList); |
|
126 } |
|
127 |
|
128 void MainWindow::load(const QString& url) |
|
129 { |
|
130 QUrl qurl = urlFromUserInput(url); |
|
131 if (qurl.scheme().isEmpty()) |
|
132 qurl = QUrl("http://" + url + "/"); |
|
133 load(qurl); |
|
134 } |
|
135 |
|
136 void MainWindow::load(const QUrl& url) |
|
137 { |
|
138 if (!url.isValid()) |
|
139 return; |
|
140 |
|
141 setAddressUrl(url.toString()); |
|
142 page()->mainFrame()->load(url); |
|
143 } |
|
144 |
|
145 void MainWindow::changeLocation() |
|
146 { |
|
147 QString string = urlEdit->text(); |
|
148 QUrl mainFrameURL = page()->mainFrame()->url(); |
|
149 |
|
150 if (mainFrameURL.isValid() && string == mainFrameURL.toString()) { |
|
151 page()->triggerAction(QWebPage::Reload); |
|
152 return; |
|
153 } |
|
154 |
|
155 load(string); |
|
156 } |
|
157 |
|
158 void MainWindow::openFile() |
|
159 { |
|
160 static const QString filter("HTML Files (*.htm *.html);;Text Files (*.txt);;Image Files (*.gif *.jpg *.png);;All Files (*)"); |
|
161 |
|
162 QFileDialog fileDialog(this, tr("Open"), QString(), filter); |
|
163 fileDialog.setAcceptMode(QFileDialog::AcceptOpen); |
|
164 fileDialog.setFileMode(QFileDialog::ExistingFile); |
|
165 fileDialog.setOptions(QFileDialog::ReadOnly); |
|
166 |
|
167 if (fileDialog.exec()) { |
|
168 QString selectedFile = fileDialog.selectedFiles()[0]; |
|
169 if (!selectedFile.isEmpty()) |
|
170 load(QUrl::fromLocalFile(selectedFile)); |
|
171 } |
|
172 } |
|
173 |
|
174 void MainWindow::openLocation() |
|
175 { |
|
176 urlEdit->selectAll(); |
|
177 urlEdit->setFocus(); |
|
178 } |
|