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>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
33 #include "mainwindow.h"
35 #include "locationedit.h"
39 #ifndef QT_NO_INPUTDIALOG
42 #ifndef QT_NO_FILEDIALOG
43 #include <QFileDialog>
46 MainWindow::MainWindow()
47 : m_page(new WebPage(this))
51 setAttribute(Qt::WA_DeleteOnClose);
52 if (qgetenv("QTTESTBROWSER_USE_ARGB_VISUALS").toInt() == 1)
53 setAttribute(Qt::WA_TranslucentBackground);
58 void MainWindow::buildUI()
62 m_toolBar = addToolBar("Navigation");
63 QAction* reloadAction = page()->action(QWebPage::Reload);
64 connect(reloadAction, SIGNAL(triggered()), this, SLOT(changeLocation()));
66 m_toolBar->addAction(page()->action(QWebPage::Back));
67 m_toolBar->addAction(page()->action(QWebPage::Forward));
68 m_toolBar->addAction(reloadAction);
69 m_toolBar->addAction(page()->action(QWebPage::Stop));
71 #ifndef QT_NO_INPUTDIALOG
72 urlEdit = new LocationEdit(m_toolBar);
73 urlEdit->setSizePolicy(QSizePolicy::Expanding, urlEdit->sizePolicy().verticalPolicy());
74 connect(urlEdit, SIGNAL(returnPressed()), SLOT(changeLocation()));
75 QCompleter* completer = new QCompleter(m_toolBar);
76 urlEdit->setCompleter(completer);
77 completer->setModel(&urlModel);
78 m_toolBar->addWidget(urlEdit);
80 connect(page()->mainFrame(), SIGNAL(urlChanged(QUrl)), this, SLOT(setAddressUrl(QUrl)));
81 connect(page(), SIGNAL(loadProgress(int)), urlEdit, SLOT(setProgress(int)));
84 connect(page()->mainFrame(), SIGNAL(loadStarted()), this, SLOT(onLoadStarted()));
85 connect(page()->mainFrame(), SIGNAL(iconChanged()), this, SLOT(onIconChanged()));
86 connect(page()->mainFrame(), SIGNAL(titleChanged(QString)), this, SLOT(onTitleChanged(QString)));
87 connect(page(), SIGNAL(windowCloseRequested()), this, SLOT(close()));
89 #ifndef QT_NO_SHORTCUT
91 page()->action(QWebPage::Back)->setShortcut(QKeySequence::Back);
92 page()->action(QWebPage::Stop)->setShortcut(Qt::Key_Escape);
93 page()->action(QWebPage::Forward)->setShortcut(QKeySequence::Forward);
94 page()->action(QWebPage::Reload)->setShortcut(QKeySequence::Refresh);
95 #ifndef QT_NO_UNDOSTACK
96 page()->action(QWebPage::Undo)->setShortcut(QKeySequence::Undo);
97 page()->action(QWebPage::Redo)->setShortcut(QKeySequence::Redo);
99 page()->action(QWebPage::Cut)->setShortcut(QKeySequence::Cut);
100 page()->action(QWebPage::Copy)->setShortcut(QKeySequence::Copy);
101 page()->action(QWebPage::Paste)->setShortcut(QKeySequence::Paste);
102 page()->action(QWebPage::SelectAll)->setShortcut(QKeySequence::SelectAll);
104 page()->action(QWebPage::ToggleBold)->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_B));
105 page()->action(QWebPage::ToggleItalic)->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_I));
106 page()->action(QWebPage::ToggleUnderline)->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_U));
110 void MainWindow::setPage(WebPage* page)
113 page->setUserAgent(m_page->userAgentForUrl(QUrl()));
121 WebPage* MainWindow::page() const
126 void MainWindow::setAddressUrl(const QUrl& url)
128 setAddressUrl(url.toString(QUrl::RemoveUserInfo));
131 void MainWindow::setAddressUrl(const QString& url)
133 #ifndef QT_NO_INPUTDIALOG
134 if (!url.contains("about:"))
135 urlEdit->setText(url);
139 void MainWindow::addCompleterEntry(const QUrl& url)
141 QUrl::FormattingOptions opts;
142 opts |= QUrl::RemoveScheme;
143 opts |= QUrl::RemoveUserInfo;
144 opts |= QUrl::StripTrailingSlash;
145 QString s = url.toString(opts);
150 if (!urlList.contains(s))
152 urlModel.setStringList(urlList);
155 void MainWindow::load(const QString& url)
157 QUrl qurl = urlFromUserInput(url);
158 if (qurl.scheme().isEmpty())
159 qurl = QUrl("http://" + url + "/");
163 void MainWindow::load(const QUrl& url)
168 setAddressUrl(url.toString());
169 page()->mainFrame()->load(url);
172 QString MainWindow::addressUrl() const
174 #ifndef QT_NO_INPUTDIALOG
175 return urlEdit->text();
180 void MainWindow::changeLocation()
182 #ifndef QT_NO_INPUTDIALOG
183 QString string = urlEdit->text();
184 QUrl mainFrameURL = page()->mainFrame()->url();
186 if (mainFrameURL.isValid() && string == mainFrameURL.toString()) {
187 page()->triggerAction(QWebPage::Reload);
195 void MainWindow::openFile()
197 #ifndef QT_NO_FILEDIALOG
198 static const QString filter("HTML Files (*.htm *.html);;Text Files (*.txt);;Image Files (*.gif *.jpg *.png);;All Files (*)");
200 QFileDialog fileDialog(this, tr("Open"), QString(), filter);
201 fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
202 fileDialog.setFileMode(QFileDialog::ExistingFile);
203 fileDialog.setOptions(QFileDialog::ReadOnly);
205 if (fileDialog.exec()) {
206 QString selectedFile = fileDialog.selectedFiles()[0];
207 if (!selectedFile.isEmpty())
208 load(QUrl::fromLocalFile(selectedFile));
213 void MainWindow::openLocation()
215 #ifndef QT_NO_INPUTDIALOG
216 urlEdit->selectAll();
221 void MainWindow::onIconChanged()
223 #ifndef QT_NO_INPUTDIALOG
224 urlEdit->setPageIcon(page()->mainFrame()->icon());
228 void MainWindow::onLoadStarted()
230 #ifndef QT_NO_INPUTDIALOG
231 urlEdit->setPageIcon(QIcon());
235 void MainWindow::onTitleChanged(const QString& title)
238 setWindowTitle(QCoreApplication::applicationName());
240 setWindowTitle(QString::fromLatin1("%1 - %2").arg(title).arg(QCoreApplication::applicationName()));