~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Tools/QtTestBrowser/mainwindow.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#include <QAction>
 
39
#ifndef QT_NO_INPUTDIALOG
 
40
#include <QCompleter>
 
41
#endif
 
42
#ifndef QT_NO_FILEDIALOG
 
43
#include <QFileDialog>
 
44
#endif
 
45
 
 
46
MainWindow::MainWindow()
 
47
    : m_page(new WebPage(this))
 
48
    , m_toolBar(0)
 
49
    , urlEdit(0)
 
50
{
 
51
    setAttribute(Qt::WA_DeleteOnClose);
 
52
    if (qgetenv("QTTESTBROWSER_USE_ARGB_VISUALS").toInt() == 1)
 
53
        setAttribute(Qt::WA_TranslucentBackground);
 
54
 
 
55
    buildUI();
 
56
}
 
57
 
 
58
void MainWindow::buildUI()
 
59
{
 
60
    delete m_toolBar;
 
61
 
 
62
    m_toolBar = addToolBar("Navigation");
 
63
    QAction* reloadAction = page()->action(QWebPage::Reload);
 
64
    connect(reloadAction, SIGNAL(triggered()), this, SLOT(changeLocation()));
 
65
 
 
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));
 
70
 
 
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);
 
79
 
 
80
    connect(page()->mainFrame(), SIGNAL(urlChanged(QUrl)), this, SLOT(setAddressUrl(QUrl)));
 
81
    connect(page(), SIGNAL(loadProgress(int)), urlEdit, SLOT(setProgress(int)));
 
82
#endif
 
83
 
 
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()));
 
88
 
 
89
#ifndef QT_NO_SHORTCUT
 
90
    // short-cuts
 
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);
 
98
#endif
 
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);
 
103
 
 
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));
 
107
#endif
 
108
}
 
109
 
 
110
void MainWindow::setPage(WebPage* page)
 
111
{
 
112
    if (page && m_page)
 
113
        page->setUserAgent(m_page->userAgentForUrl(QUrl()));
 
114
 
 
115
    delete m_page;
 
116
    m_page = page;
 
117
 
 
118
    buildUI();
 
119
}
 
120
 
 
121
WebPage* MainWindow::page() const
 
122
{
 
123
    return m_page;
 
124
}
 
125
 
 
126
void MainWindow::setAddressUrl(const QUrl& url)
 
127
{
 
128
    setAddressUrl(url.toString(QUrl::RemoveUserInfo));
 
129
}
 
130
 
 
131
void MainWindow::setAddressUrl(const QString& url)
 
132
{
 
133
#ifndef QT_NO_INPUTDIALOG
 
134
    if (!url.contains("about:"))
 
135
        urlEdit->setText(url);
 
136
#endif
 
137
}
 
138
 
 
139
void MainWindow::addCompleterEntry(const QUrl& url)
 
140
{
 
141
    QUrl::FormattingOptions opts;
 
142
    opts |= QUrl::RemoveScheme;
 
143
    opts |= QUrl::RemoveUserInfo;
 
144
    opts |= QUrl::StripTrailingSlash;
 
145
    QString s = url.toString(opts);
 
146
    s = s.mid(2);
 
147
    if (s.isEmpty())
 
148
        return;
 
149
 
 
150
    if (!urlList.contains(s))
 
151
        urlList += s;
 
152
    urlModel.setStringList(urlList);
 
153
}
 
154
 
 
155
void MainWindow::load(const QString& url)
 
156
{
 
157
    QUrl qurl = urlFromUserInput(url);
 
158
    if (qurl.scheme().isEmpty())
 
159
        qurl = QUrl("http://" + url + "/");
 
160
    load(qurl);
 
161
}
 
162
 
 
163
void MainWindow::load(const QUrl& url)
 
164
{
 
165
    if (!url.isValid())
 
166
        return;
 
167
 
 
168
    setAddressUrl(url.toString());
 
169
    page()->mainFrame()->load(url);
 
170
}
 
171
 
 
172
QString MainWindow::addressUrl() const
 
173
{
 
174
#ifndef QT_NO_INPUTDIALOG
 
175
    return urlEdit->text();
 
176
#endif
 
177
    return QString();
 
178
}
 
179
 
 
180
void MainWindow::changeLocation()
 
181
{
 
182
#ifndef QT_NO_INPUTDIALOG
 
183
    QString string = urlEdit->text();
 
184
    QUrl mainFrameURL = page()->mainFrame()->url();
 
185
 
 
186
    if (mainFrameURL.isValid() && string == mainFrameURL.toString()) {
 
187
        page()->triggerAction(QWebPage::Reload);
 
188
        return;
 
189
    }
 
190
 
 
191
    load(string);
 
192
#endif
 
193
}
 
194
 
 
195
void MainWindow::openFile()
 
196
{
 
197
#ifndef QT_NO_FILEDIALOG
 
198
    static const QString filter("HTML Files (*.htm *.html);;Text Files (*.txt);;Image Files (*.gif *.jpg *.png);;All Files (*)");
 
199
 
 
200
    QFileDialog fileDialog(this, tr("Open"), QString(), filter);
 
201
    fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
 
202
    fileDialog.setFileMode(QFileDialog::ExistingFile);
 
203
    fileDialog.setOptions(QFileDialog::ReadOnly);
 
204
 
 
205
    if (fileDialog.exec()) {
 
206
        QString selectedFile = fileDialog.selectedFiles()[0];
 
207
        if (!selectedFile.isEmpty())
 
208
            load(QUrl::fromLocalFile(selectedFile));
 
209
    }
 
210
#endif
 
211
}
 
212
 
 
213
void MainWindow::openLocation()
 
214
{
 
215
#ifndef QT_NO_INPUTDIALOG
 
216
    urlEdit->selectAll();
 
217
    urlEdit->setFocus();
 
218
#endif
 
219
}
 
220
 
 
221
void MainWindow::onIconChanged()
 
222
{
 
223
#ifndef QT_NO_INPUTDIALOG
 
224
    urlEdit->setPageIcon(page()->mainFrame()->icon());
 
225
#endif
 
226
}
 
227
 
 
228
void MainWindow::onLoadStarted()
 
229
{
 
230
#ifndef QT_NO_INPUTDIALOG
 
231
    urlEdit->setPageIcon(QIcon());
 
232
#endif
 
233
}
 
234
 
 
235
void MainWindow::onTitleChanged(const QString& title)
 
236
{
 
237
    if (title.isEmpty())
 
238
        setWindowTitle(QCoreApplication::applicationName());
 
239
    else
 
240
        setWindowTitle(QString::fromLatin1("%1 - %2").arg(title).arg(QCoreApplication::applicationName()));
 
241
}