2011-01-27 Jacob Dinu <dinu.jacob@nokia.com>
[WebKit-https.git] / Tools / QtTestBrowser / mainwindow.cpp
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()
39     : m_page(new WebPage(this))
40     , m_toolBar(0)
41     , urlEdit(0)
42 {
43     setAttribute(Qt::WA_DeleteOnClose);
44     if (qgetenv("QTTESTBROWSER_USE_ARGB_VISUALS").toInt() == 1)
45         setAttribute(Qt::WA_TranslucentBackground);
46
47     buildUI();
48 }
49
50 void MainWindow::buildUI()
51 {
52 #if defined(Q_OS_SYMBIAN)
53     delete urlEdit;
54 #endif
55     delete m_toolBar;
56
57     m_toolBar = addToolBar("Navigation");
58 #if defined(Q_OS_SYMBIAN)
59     m_toolBar->setIconSize(QSize(16, 16));
60 #endif
61     QAction* reloadAction = page()->action(QWebPage::Reload);
62     connect(reloadAction, SIGNAL(triggered()), this, SLOT(changeLocation()));
63
64     m_toolBar->addAction(page()->action(QWebPage::Back));
65     m_toolBar->addAction(page()->action(QWebPage::Forward));
66     m_toolBar->addAction(reloadAction);
67     m_toolBar->addAction(page()->action(QWebPage::Stop));
68
69 #ifndef QT_NO_INPUTDIALOG
70     urlEdit = new LocationEdit(m_toolBar);
71     urlEdit->setSizePolicy(QSizePolicy::Expanding, urlEdit->sizePolicy().verticalPolicy());
72     connect(urlEdit, SIGNAL(returnPressed()), SLOT(changeLocation()));
73     QCompleter* completer = new QCompleter(m_toolBar);
74     urlEdit->setCompleter(completer);
75     completer->setModel(&urlModel);
76 #if defined(Q_OS_SYMBIAN)
77     addToolBarBreak();
78     addToolBar("Location")->addWidget(urlEdit);
79 #else
80     m_toolBar->addWidget(urlEdit);
81 #endif
82
83     connect(page()->mainFrame(), SIGNAL(urlChanged(QUrl)), this, SLOT(setAddressUrl(QUrl)));
84     connect(page(), SIGNAL(loadProgress(int)), urlEdit, SLOT(setProgress(int)));
85 #endif
86
87     connect(page()->mainFrame(), SIGNAL(titleChanged(const QString&)),
88                 this, SLOT(setWindowTitle(const QString&)));
89     connect(page(), SIGNAL(windowCloseRequested()), this, SLOT(close()));
90
91 #ifndef QT_NO_SHORTCUT
92     // short-cuts
93     page()->action(QWebPage::Back)->setShortcut(QKeySequence::Back);
94     page()->action(QWebPage::Stop)->setShortcut(Qt::Key_Escape);
95     page()->action(QWebPage::Forward)->setShortcut(QKeySequence::Forward);
96     page()->action(QWebPage::Reload)->setShortcut(QKeySequence::Refresh);
97 #ifndef QT_NO_UNDOSTACK
98     page()->action(QWebPage::Undo)->setShortcut(QKeySequence::Undo);
99     page()->action(QWebPage::Redo)->setShortcut(QKeySequence::Redo);
100 #endif
101     page()->action(QWebPage::Cut)->setShortcut(QKeySequence::Cut);
102     page()->action(QWebPage::Copy)->setShortcut(QKeySequence::Copy);
103     page()->action(QWebPage::Paste)->setShortcut(QKeySequence::Paste);
104     page()->action(QWebPage::SelectAll)->setShortcut(QKeySequence::SelectAll);
105
106     page()->action(QWebPage::ToggleBold)->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_B));
107     page()->action(QWebPage::ToggleItalic)->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_I));
108     page()->action(QWebPage::ToggleUnderline)->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_U));
109 #endif
110 }
111
112 void MainWindow::setPage(WebPage* page)
113 {
114     if (page && m_page)
115         page->setUserAgent(m_page->userAgentForUrl(QUrl()));
116
117     delete m_page;
118     m_page = page;
119
120     buildUI();
121 }
122
123 WebPage* MainWindow::page() const
124 {
125     return m_page;
126 }
127
128 void MainWindow::setAddressUrl(const QUrl& url)
129 {
130     setAddressUrl(url.toString(QUrl::RemoveUserInfo));
131 }
132
133 void MainWindow::setAddressUrl(const QString& url)
134 {
135 #ifndef QT_NO_INPUTDIALOG
136     if (!url.contains("about:"))
137         urlEdit->setText(url);
138 #endif
139 }
140
141 void MainWindow::addCompleterEntry(const QUrl& url)
142 {
143     QUrl::FormattingOptions opts;
144     opts |= QUrl::RemoveScheme;
145     opts |= QUrl::RemoveUserInfo;
146     opts |= QUrl::StripTrailingSlash;
147     QString s = url.toString(opts);
148     s = s.mid(2);
149     if (s.isEmpty())
150         return;
151
152     if (!urlList.contains(s))
153         urlList += s;
154     urlModel.setStringList(urlList);
155 }
156
157 void MainWindow::load(const QString& url)
158 {
159     QUrl qurl = urlFromUserInput(url);
160     if (qurl.scheme().isEmpty())
161         qurl = QUrl("http://" + url + "/");
162     load(qurl);
163 }
164
165 void MainWindow::load(const QUrl& url)
166 {
167     if (!url.isValid())
168         return;
169
170     setAddressUrl(url.toString());
171     page()->mainFrame()->load(url);
172 }
173
174 QString MainWindow::addressUrl() const
175 {
176 #ifndef QT_NO_INPUTDIALOG
177     return urlEdit->text();
178 #endif
179     return QString();
180 }
181
182 void MainWindow::changeLocation()
183 {
184 #ifndef QT_NO_INPUTDIALOG
185     QString string = urlEdit->text();
186     QUrl mainFrameURL = page()->mainFrame()->url();
187
188     if (mainFrameURL.isValid() && string == mainFrameURL.toString()) {
189         page()->triggerAction(QWebPage::Reload);
190         return;
191     }
192
193     load(string);
194 #endif
195 }
196
197 void MainWindow::openFile()
198 {
199 #ifndef QT_NO_FILEDIALOG
200     static const QString filter("HTML Files (*.htm *.html);;Text Files (*.txt);;Image Files (*.gif *.jpg *.png);;All Files (*)");
201
202     QFileDialog fileDialog(this, tr("Open"), QString(), filter);
203     fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
204     fileDialog.setFileMode(QFileDialog::ExistingFile);
205     fileDialog.setOptions(QFileDialog::ReadOnly);
206
207     if (fileDialog.exec()) {
208         QString selectedFile = fileDialog.selectedFiles()[0];
209         if (!selectedFile.isEmpty())
210             load(QUrl::fromLocalFile(selectedFile));
211     }
212 #endif
213 }
214
215 void MainWindow::openLocation()
216 {
217 #ifndef QT_NO_INPUTDIALOG
218     urlEdit->selectAll();
219     urlEdit->setFocus();
220 #endif
221 }