mainwindow.cpp 7.5 KB

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