PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
C++ | 140 lines | 112 code | 18 blank | 10 comment | 10 complexity | 6af730f9111e8338daa87ede51e3f814 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. {
  24. //Set the flag to destroy when closed
  25. setAttribute(Qt::WA_DeleteOnClose);
  26. setWindowTitle(title);
  27. resize(400, 300);
  28. QVBoxLayout *vboxLayout = new QVBoxLayout(this);
  29. if (tabs == 0)
  30. {
  31. m_webView = new BtWebView(this);
  32. init_connections(m_webView);
  33. vboxLayout->addWidget(m_webView);
  34. m_webView->setHtml("Hi");
  35. }
  36. else
  37. {
  38. m_tabWidget = new QTabWidget(this);
  39. vboxLayout->addWidget(m_tabWidget);
  40. for (int i=0; i<tabs; i++)
  41. {
  42. QWebView* webView = new BtWebView(this);
  43. init_connections(webView);
  44. webView->setObjectName("View" + QString::number(i));
  45. webView->setHtml(" ");
  46. m_tabWidget->addTab(webView,"Tab" + QString::number(i));
  47. m_tabWidget->show();
  48. }
  49. }
  50. QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, Qt::Horizontal, this);
  51. util::prepareDialogBox(buttonBox);
  52. vboxLayout->addWidget(buttonBox);
  53. bool ok;
  54. ok = connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
  55. Q_ASSERT(ok);
  56. ok = connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
  57. Q_ASSERT(ok);
  58. }
  59. BtTabHtmlDialog::~BtTabHtmlDialog()
  60. {
  61. }
  62. void BtTabHtmlDialog::init_connections(QWebView* webView)
  63. {
  64. webView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
  65. bool ok = connect(webView, SIGNAL(linkClicked(QUrl)), this, SLOT(linkClicked(QUrl)));
  66. Q_ASSERT(ok);
  67. }
  68. void BtTabHtmlDialog::linkClicked(const QUrl url)
  69. {
  70. QDesktopServices::openUrl(url);
  71. }
  72. void BtTabHtmlDialog::selectTab(int tab)
  73. {
  74. Q_ASSERT(tab >= 0 && tab < m_tabWidget->count());
  75. m_tabWidget->setCurrentIndex(tab);
  76. }
  77. QWebView* BtTabHtmlDialog::webView()
  78. {
  79. QWebView* webview = 0;
  80. if (m_tabs == 0)
  81. webview = m_webView;
  82. else
  83. {
  84. QWidget* widget = m_tabWidget->currentWidget();
  85. QString name = widget->objectName();
  86. webview = qobject_cast<QWebView*>(widget);
  87. }
  88. Q_ASSERT(webview != 0);
  89. return webview;
  90. }
  91. void BtTabHtmlDialog::setHtml(const QString& html, const QUrl& baseUrl)
  92. {
  93. QUrl url = baseUrl;
  94. if (url == QUrl())
  95. {
  96. QString dir = "file://" + util::filesystem::DirectoryUtil::getIconDir().path();
  97. url.setUrl(dir);
  98. }
  99. webView()->setHtml(html,url);
  100. }
  101. void BtTabHtmlDialog::setUrl(const QUrl& url)
  102. {
  103. webView()->setUrl(url);
  104. }
  105. void BtTabHtmlDialog::setTabText(const QString& tabName)
  106. {
  107. Q_ASSERT(m_tabs != 0); // There are no tabs to name
  108. int index = m_tabWidget->currentIndex();
  109. m_tabWidget->setTabText(index,tabName);
  110. }
  111. // ******************* BtWebView *******************
  112. BtWebView::BtWebView(QWidget* parent)
  113. : QWebView(parent), m_popup(0)
  114. {
  115. m_popup = new QMenu(this);
  116. QAction* copyAction = pageAction(QWebPage::Copy);
  117. m_popup->addAction(copyAction);
  118. }
  119. void BtWebView::contextMenuEvent(QContextMenuEvent* event)
  120. {
  121. m_popup->exec(event->globalPos());
  122. }