PageRenderTime 62ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/bugfix-2-3/bibletime/src/frontend/htmldialogs/bttabhtmldialog.cpp

#
C++ | 124 lines | 96 code | 18 blank | 10 comment | 12 complexity | b15ac8bd99d2fe6aae6ef2f2939e0553 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0
  1. /*********
  2. *
  3. * This file is part of BibleTime's source code, http://www.bibletime.info/.
  4. *
  5. * Copyright 1999-2009 by the BibleTime developers.
  6. * The BibleTime source code is licensed under the GNU General Public License version 2.0.
  7. *
  8. **********/
  9. #include "bttabhtmldialog.h" // See this file for more documentation of BtTabHtmlDialog
  10. #include "util/dialogutil.h"
  11. #include "util/directoryutil.h"
  12. #include <QDialog>
  13. #include <QDialogButtonBox>
  14. #include <QVBoxLayout>
  15. #include <QWebView>
  16. #include <QMenu>
  17. #include <QTabWidget>
  18. #include <QDesktopServices>
  19. #include <QContextMenuEvent>
  20. BtTabHtmlDialog::BtTabHtmlDialog
  21. (const QString& title, int tabs, QWidget *parent, Qt::WindowFlags wflags )
  22. : QDialog(parent, wflags), m_webView(0), m_tabWidget(0), m_tabs(tabs) {
  23. //Set the flag to destroy when closed
  24. setAttribute(Qt::WA_DeleteOnClose);
  25. setWindowTitle(title);
  26. resize(400, 300);
  27. QVBoxLayout *vboxLayout = new QVBoxLayout(this);
  28. if (tabs == 0) {
  29. m_webView = new BtWebView(this);
  30. init_connections(m_webView);
  31. vboxLayout->addWidget(m_webView);
  32. m_webView->setHtml("Hi");
  33. }
  34. else {
  35. m_tabWidget = new QTabWidget(this);
  36. vboxLayout->addWidget(m_tabWidget);
  37. for (int i = 0; i < tabs; i++) {
  38. QWebView* webView = new BtWebView(this);
  39. init_connections(webView);
  40. webView->setObjectName("View" + QString::number(i));
  41. webView->setHtml(" ");
  42. m_tabWidget->addTab(webView, "Tab" + QString::number(i));
  43. m_tabWidget->show();
  44. }
  45. }
  46. QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, Qt::Horizontal, this);
  47. util::prepareDialogBox(buttonBox);
  48. vboxLayout->addWidget(buttonBox);
  49. bool ok;
  50. ok = connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
  51. Q_ASSERT(ok);
  52. ok = connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
  53. Q_ASSERT(ok);
  54. }
  55. BtTabHtmlDialog::~BtTabHtmlDialog() {
  56. }
  57. void BtTabHtmlDialog::init_connections(QWebView* webView) {
  58. webView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
  59. bool ok = connect(webView, SIGNAL(linkClicked(QUrl)), this, SLOT(linkClicked(QUrl)));
  60. Q_ASSERT(ok);
  61. }
  62. void BtTabHtmlDialog::linkClicked(const QUrl url) {
  63. QDesktopServices::openUrl(url);
  64. }
  65. void BtTabHtmlDialog::selectTab(int tab) {
  66. Q_ASSERT(tab >= 0 && tab < m_tabWidget->count());
  67. m_tabWidget->setCurrentIndex(tab);
  68. }
  69. QWebView* BtTabHtmlDialog::webView() {
  70. QWebView* webview = 0;
  71. if (m_tabs == 0)
  72. webview = m_webView;
  73. else {
  74. QWidget* widget = m_tabWidget->currentWidget();
  75. QString name = widget->objectName();
  76. webview = qobject_cast<QWebView*>(widget);
  77. }
  78. Q_ASSERT(webview != 0);
  79. return webview;
  80. }
  81. void BtTabHtmlDialog::setHtml(const QString& html, const QUrl& baseUrl) {
  82. QUrl url = baseUrl;
  83. if (url == QUrl()) {
  84. QString dir = "file://" + util::filesystem::DirectoryUtil::getIconDir().path();
  85. url.setUrl(dir);
  86. }
  87. webView()->setHtml(html, url);
  88. }
  89. void BtTabHtmlDialog::setUrl(const QUrl& url) {
  90. webView()->setUrl(url);
  91. }
  92. void BtTabHtmlDialog::setTabText(const QString& tabName) {
  93. Q_ASSERT(m_tabs != 0); // There are no tabs to name
  94. int index = m_tabWidget->currentIndex();
  95. m_tabWidget->setTabText(index, tabName);
  96. }
  97. // ******************* BtWebView *******************
  98. BtWebView::BtWebView(QWidget* parent)
  99. : QWebView(parent), m_popup(0) {
  100. m_popup = new QMenu(this);
  101. QAction* copyAction = pageAction(QWebPage::Copy);
  102. m_popup->addAction(copyAction);
  103. }
  104. void BtWebView::contextMenuEvent(QContextMenuEvent* event) {
  105. m_popup->exec(event->globalPos());
  106. }