PageRenderTime 22ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/assistant/tools/assistant/aboutdialog.cpp

https://bitbucket.org/kasimling/qt
C++ | 184 lines | 120 code | 24 blank | 40 comment | 12 complexity | 7a59972e92aaba18d9b4e1f9e65f1f57 MD5 | raw file
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
  4. ** All rights reserved.
  5. ** Contact: Nokia Corporation (qt-info@nokia.com)
  6. **
  7. ** This file is part of the Qt Assistant of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** GNU Lesser General Public License Usage
  11. ** This file may be used under the terms of the GNU Lesser General Public
  12. ** License version 2.1 as published by the Free Software Foundation and
  13. ** appearing in the file LICENSE.LGPL included in the packaging of this
  14. ** file. Please review the following information to ensure the GNU Lesser
  15. ** General Public License version 2.1 requirements will be met:
  16. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  17. **
  18. ** In addition, as a special exception, Nokia gives you certain additional
  19. ** rights. These rights are described in the Nokia Qt LGPL Exception
  20. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  21. **
  22. ** GNU General Public License Usage
  23. ** Alternatively, this file may be used under the terms of the GNU General
  24. ** Public License version 3.0 as published by the Free Software Foundation
  25. ** and appearing in the file LICENSE.GPL included in the packaging of this
  26. ** file. Please review the following information to ensure the GNU General
  27. ** Public License version 3.0 requirements will be met:
  28. ** http://www.gnu.org/copyleft/gpl.html.
  29. **
  30. ** Other Usage
  31. ** Alternatively, this file may be used in accordance with the terms and
  32. ** conditions contained in a signed written agreement between you and Nokia.
  33. **
  34. **
  35. **
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include "aboutdialog.h"
  42. #include "helpviewer.h"
  43. #include "tracer.h"
  44. #include <QtCore/QBuffer>
  45. #include <QtGui/QLabel>
  46. #include <QtGui/QPushButton>
  47. #include <QtGui/QLayout>
  48. #include <QtGui/QApplication>
  49. #include <QtGui/QDesktopWidget>
  50. #include <QtGui/QMessageBox>
  51. #include <QtGui/QDesktopServices>
  52. QT_BEGIN_NAMESPACE
  53. AboutLabel::AboutLabel(QWidget *parent)
  54. : QTextBrowser(parent)
  55. {
  56. TRACE_OBJ
  57. setFrameStyle(QFrame::NoFrame);
  58. QPalette p;
  59. p.setColor(QPalette::Base, p.color(QPalette::Background));
  60. setPalette(p);
  61. }
  62. void AboutLabel::setText(const QString &text, const QByteArray &resources)
  63. {
  64. TRACE_OBJ
  65. QDataStream in(resources);
  66. in >> m_resourceMap;
  67. QTextBrowser::setText(text);
  68. }
  69. QSize AboutLabel::minimumSizeHint() const
  70. {
  71. TRACE_OBJ
  72. QTextDocument *doc = document();
  73. doc->adjustSize();
  74. return QSize(int(doc->size().width()), int(doc->size().height()));
  75. }
  76. QVariant AboutLabel::loadResource(int type, const QUrl &name)
  77. {
  78. TRACE_OBJ
  79. if (type == 2 || type == 3) {
  80. if (m_resourceMap.contains(name.toString())) {
  81. return m_resourceMap.value(name.toString());
  82. }
  83. }
  84. return QVariant();
  85. }
  86. void AboutLabel::setSource(const QUrl &url)
  87. {
  88. TRACE_OBJ
  89. if (url.isValid() && (!HelpViewer::isLocalUrl(url)
  90. || !HelpViewer::canOpenPage(url.path()))) {
  91. if (!QDesktopServices::openUrl(url)) {
  92. QMessageBox::warning(this, tr("Warning"),
  93. tr("Unable to launch external application.\n"), tr("OK"));
  94. }
  95. }
  96. }
  97. AboutDialog::AboutDialog(QWidget *parent)
  98. : QDialog(parent, Qt::MSWindowsFixedSizeDialogHint |
  99. Qt::WindowTitleHint|Qt::WindowSystemMenuHint)
  100. {
  101. TRACE_OBJ
  102. m_pixmapLabel = 0;
  103. m_aboutLabel = new AboutLabel();
  104. m_closeButton = new QPushButton();
  105. m_closeButton->setText(tr("&Close"));
  106. connect(m_closeButton, SIGNAL(clicked()), this, SLOT(close()));
  107. m_layout = new QGridLayout(this);
  108. m_layout->addWidget(m_aboutLabel, 1, 0, 1, -1);
  109. m_layout->addItem(new QSpacerItem(20, 10, QSizePolicy::Minimum,
  110. QSizePolicy::Fixed), 2, 1, 1, 1);
  111. m_layout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding), 3, 0, 1, 1);
  112. m_layout->addWidget(m_closeButton, 3, 1, 1, 1);
  113. m_layout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding), 3, 2, 1, 1);
  114. }
  115. void AboutDialog::setText(const QString &text, const QByteArray &resources)
  116. {
  117. TRACE_OBJ
  118. m_aboutLabel->setText(text, resources);
  119. updateSize();
  120. }
  121. void AboutDialog::setPixmap(const QPixmap &pixmap)
  122. {
  123. TRACE_OBJ
  124. if (!m_pixmapLabel) {
  125. m_pixmapLabel = new QLabel();
  126. m_layout->addWidget(m_pixmapLabel, 0, 0, 1, -1, Qt::AlignCenter);
  127. }
  128. m_pixmapLabel->setPixmap(pixmap);
  129. updateSize();
  130. }
  131. QString AboutDialog::documentTitle() const
  132. {
  133. TRACE_OBJ
  134. return m_aboutLabel->documentTitle();
  135. }
  136. void AboutDialog::updateSize()
  137. {
  138. TRACE_OBJ
  139. QSize screenSize = QApplication::desktop()->availableGeometry(QCursor::pos())
  140. .size();
  141. int limit = qMin(screenSize.width()/2, 500);
  142. #ifdef Q_WS_MAC
  143. limit = qMin(screenSize.width()/2, 420);
  144. #endif
  145. layout()->activate();
  146. int width = layout()->totalMinimumSize().width();
  147. if (width > limit)
  148. width = limit;
  149. QFontMetrics fm(qApp->font("QWorkspaceTitleBar"));
  150. int windowTitleWidth = qMin(fm.width(windowTitle()) + 50, limit);
  151. if (windowTitleWidth > width)
  152. width = windowTitleWidth;
  153. layout()->activate();
  154. int height = (layout()->hasHeightForWidth())
  155. ? layout()->totalHeightForWidth(width)
  156. : layout()->totalMinimumSize().height();
  157. setFixedSize(width, height);
  158. QCoreApplication::removePostedEvents(this, QEvent::LayoutRequest);
  159. }
  160. QT_END_NAMESPACE