mainwindow.cpp revision f05b935882198ccf7d81675736e3aeb089c5113a
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 38MainWindow::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 50void 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 urlEdit = new LocationEdit(m_toolBar); 70 urlEdit->setSizePolicy(QSizePolicy::Expanding, urlEdit->sizePolicy().verticalPolicy()); 71 connect(urlEdit, SIGNAL(returnPressed()), SLOT(changeLocation())); 72 QCompleter* completer = new QCompleter(m_toolBar); 73 urlEdit->setCompleter(completer); 74 completer->setModel(&urlModel); 75#if defined(Q_OS_SYMBIAN) 76 addToolBarBreak(); 77 addToolBar("Location")->addWidget(urlEdit); 78#else 79 m_toolBar->addWidget(urlEdit); 80#endif 81 82 connect(page()->mainFrame(), SIGNAL(titleChanged(const QString&)), 83 this, SLOT(setWindowTitle(const QString&))); 84 connect(page()->mainFrame(), SIGNAL(urlChanged(QUrl)), this, SLOT(setAddressUrl(QUrl))); 85 connect(page(), SIGNAL(loadProgress(int)), urlEdit, SLOT(setProgress(int))); 86 connect(page(), SIGNAL(windowCloseRequested()), this, SLOT(close())); 87 88 // short-cuts 89 page()->action(QWebPage::Back)->setShortcut(QKeySequence::Back); 90 page()->action(QWebPage::Stop)->setShortcut(Qt::Key_Escape); 91 page()->action(QWebPage::Forward)->setShortcut(QKeySequence::Forward); 92 page()->action(QWebPage::Reload)->setShortcut(QKeySequence::Refresh); 93 page()->action(QWebPage::Undo)->setShortcut(QKeySequence::Undo); 94 page()->action(QWebPage::Redo)->setShortcut(QKeySequence::Redo); 95 page()->action(QWebPage::Cut)->setShortcut(QKeySequence::Cut); 96 page()->action(QWebPage::Copy)->setShortcut(QKeySequence::Copy); 97 page()->action(QWebPage::Paste)->setShortcut(QKeySequence::Paste); 98 99 page()->action(QWebPage::ToggleBold)->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_B)); 100 page()->action(QWebPage::ToggleItalic)->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_I)); 101 page()->action(QWebPage::ToggleUnderline)->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_U)); 102} 103 104void MainWindow::setPage(WebPage* page) 105{ 106 if (page && m_page) 107 page->setUserAgent(m_page->userAgentForUrl(QUrl())); 108 109 delete m_page; 110 m_page = page; 111 112 buildUI(); 113} 114 115WebPage* MainWindow::page() const 116{ 117 return m_page; 118} 119 120void MainWindow::setAddressUrl(const QUrl& url) 121{ 122 setAddressUrl(url.toString(QUrl::RemoveUserInfo)); 123} 124 125void MainWindow::setAddressUrl(const QString& url) 126{ 127 if (!url.contains("about:")) 128 urlEdit->setText(url); 129} 130 131void MainWindow::addCompleterEntry(const QUrl& url) 132{ 133 QUrl::FormattingOptions opts; 134 opts |= QUrl::RemoveScheme; 135 opts |= QUrl::RemoveUserInfo; 136 opts |= QUrl::StripTrailingSlash; 137 QString s = url.toString(opts); 138 s = s.mid(2); 139 if (s.isEmpty()) 140 return; 141 142 if (!urlList.contains(s)) 143 urlList += s; 144 urlModel.setStringList(urlList); 145} 146 147void MainWindow::load(const QString& url) 148{ 149 QUrl qurl = urlFromUserInput(url); 150 if (qurl.scheme().isEmpty()) 151 qurl = QUrl("http://" + url + "/"); 152 load(qurl); 153} 154 155void MainWindow::load(const QUrl& url) 156{ 157 if (!url.isValid()) 158 return; 159 160 setAddressUrl(url.toString()); 161 page()->mainFrame()->load(url); 162} 163 164void MainWindow::changeLocation() 165{ 166 QString string = urlEdit->text(); 167 QUrl mainFrameURL = page()->mainFrame()->url(); 168 169 if (mainFrameURL.isValid() && string == mainFrameURL.toString()) { 170 page()->triggerAction(QWebPage::Reload); 171 return; 172 } 173 174 load(string); 175} 176 177void MainWindow::openFile() 178{ 179 static const QString filter("HTML Files (*.htm *.html);;Text Files (*.txt);;Image Files (*.gif *.jpg *.png);;All Files (*)"); 180 181 QFileDialog fileDialog(this, tr("Open"), QString(), filter); 182 fileDialog.setAcceptMode(QFileDialog::AcceptOpen); 183 fileDialog.setFileMode(QFileDialog::ExistingFile); 184 fileDialog.setOptions(QFileDialog::ReadOnly); 185 186 if (fileDialog.exec()) { 187 QString selectedFile = fileDialog.selectedFiles()[0]; 188 if (!selectedFile.isEmpty()) 189 load(QUrl::fromLocalFile(selectedFile)); 190 } 191} 192 193void MainWindow::openLocation() 194{ 195 urlEdit->selectAll(); 196 urlEdit->setFocus(); 197} 198