1 /* 2 * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies) 3 * Copyright (C) 2009 Girish Ramakrishnan <girish (at) forwardbias.in> 4 * Copyright (C) 2006 George Staikos <staikos (at) kde.org> 5 * Copyright (C) 2006 Dirk Mueller <mueller (at) kde.org> 6 * Copyright (C) 2006 Zack Rusin <zack (at) kde.org> 7 * Copyright (C) 2006 Simon Hausmann <hausmann (at) 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 QT_VERSION >= QT_VERSION_CHECK(4, 5, 0) 43 if (qgetenv("QTLAUNCHER_USE_ARGB_VISUALS").toInt() == 1) 44 setAttribute(Qt::WA_TranslucentBackground); 45 #endif 46 47 buildUI(); 48 } 49 50 void MainWindow::buildUI() 51 { 52 QToolBar* bar = addToolBar("Navigation"); 53 #if defined(Q_WS_S60) 54 bar->setIconSize(QSize(16, 16)); 55 #endif 56 bar->addAction(page()->action(QWebPage::Back)); 57 bar->addAction(page()->action(QWebPage::Forward)); 58 bar->addAction(page()->action(QWebPage::Reload)); 59 bar->addAction(page()->action(QWebPage::Stop)); 60 61 urlEdit = new LocationEdit(this); 62 urlEdit->setSizePolicy(QSizePolicy::Expanding, urlEdit->sizePolicy().verticalPolicy()); 63 connect(urlEdit, SIGNAL(returnPressed()), SLOT(changeLocation())); 64 QCompleter* completer = new QCompleter(this); 65 urlEdit->setCompleter(completer); 66 completer->setModel(&urlModel); 67 #if defined(Q_WS_S60) 68 addToolBarBreak(); 69 addToolBar("Location")->addWidget(urlEdit); 70 #else 71 bar->addWidget(urlEdit); 72 #endif 73 74 connect(page()->mainFrame(), SIGNAL(titleChanged(const QString&)), 75 this, SLOT(setWindowTitle(const QString&))); 76 connect(page(), SIGNAL(loadProgress(int)), urlEdit, SLOT(setProgress(int))); 77 connect(page(), SIGNAL(windowCloseRequested()), this, SLOT(close())); 78 79 // short-cuts 80 page()->action(QWebPage::Back)->setShortcut(QKeySequence::Back); 81 page()->action(QWebPage::Stop)->setShortcut(Qt::Key_Escape); 82 page()->action(QWebPage::Forward)->setShortcut(QKeySequence::Forward); 83 page()->action(QWebPage::Reload)->setShortcut(QKeySequence::Refresh); 84 page()->action(QWebPage::Undo)->setShortcut(QKeySequence::Undo); 85 page()->action(QWebPage::Redo)->setShortcut(QKeySequence::Redo); 86 page()->action(QWebPage::Cut)->setShortcut(QKeySequence::Cut); 87 page()->action(QWebPage::Copy)->setShortcut(QKeySequence::Copy); 88 page()->action(QWebPage::Paste)->setShortcut(QKeySequence::Paste); 89 90 page()->action(QWebPage::ToggleBold)->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_B)); 91 page()->action(QWebPage::ToggleItalic)->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_I)); 92 page()->action(QWebPage::ToggleUnderline)->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_U)); 93 } 94 95 WebPage* MainWindow::page() 96 { 97 return m_page; 98 } 99 100 void MainWindow::setAddressUrl(const QString& url) 101 { 102 urlEdit->setText(url); 103 } 104 105 void MainWindow::addCompleterEntry(const QUrl& url) 106 { 107 QUrl::FormattingOptions opts; 108 opts |= QUrl::RemoveScheme; 109 opts |= QUrl::RemoveUserInfo; 110 opts |= QUrl::StripTrailingSlash; 111 QString s = url.toString(opts); 112 s = s.mid(2); 113 if (s.isEmpty()) 114 return; 115 116 if (!urlList.contains(s)) 117 urlList += s; 118 urlModel.setStringList(urlList); 119 } 120 121 void MainWindow::load(const QString& url) 122 { 123 QUrl qurl = urlFromUserInput(url); 124 if (qurl.scheme().isEmpty()) 125 qurl = QUrl("http://" + url + "/"); 126 load(qurl); 127 } 128 129 void MainWindow::load(const QUrl& url) 130 { 131 if (!url.isValid()) 132 return; 133 134 setAddressUrl(url.toString()); 135 page()->mainFrame()->load(url); 136 } 137 138 void MainWindow::changeLocation() 139 { 140 QString string = urlEdit->text(); 141 load(string); 142 } 143 144 void MainWindow::openFile() 145 { 146 static const QString filter("HTML Files (*.htm *.html);;Text Files (*.txt);;Image Files (*.gif *.jpg *.png);;All Files (*)"); 147 148 QFileDialog fileDialog(this, tr("Open"), QString(), filter); 149 fileDialog.setAcceptMode(QFileDialog::AcceptOpen); 150 fileDialog.setFileMode(QFileDialog::ExistingFile); 151 fileDialog.setOptions(QFileDialog::ReadOnly); 152 153 if (fileDialog.exec()) { 154 QString selectedFile = fileDialog.selectedFiles()[0]; 155 if (!selectedFile.isEmpty()) 156 load(QUrl::fromLocalFile(selectedFile)); 157 } 158 } 159 160