PageRenderTime 58ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/manual/qtexttableborders/widget.cpp

https://gitlab.com/f3822/qtbase
C++ | 125 lines | 79 code | 15 blank | 31 comment | 5 complexity | 352f2b0498664848be5bf8667766bfd8 MD5 | raw file
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2019 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the test suite of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU
  19. ** General Public License version 3 as published by the Free Software
  20. ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
  21. ** included in the packaging of this file. Please review the following
  22. ** information to ensure the GNU General Public License requirements will
  23. ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
  24. **
  25. ** $QT_END_LICENSE$
  26. **
  27. ****************************************************************************/
  28. #include "widget.h"
  29. #include "ui_widget.h"
  30. #include <QScrollBar>
  31. #include <QFile>
  32. #include <QDir>
  33. #include <QTemporaryFile>
  34. #ifndef QT_NO_DESKTOPSERVICES
  35. #include <QDesktopServices>
  36. #endif
  37. #ifndef QT_NO_PRINTER
  38. #include <QPrinter>
  39. #include <QPrintDialog>
  40. #include <QPrintPreviewDialog>
  41. #endif
  42. // This manual test allows checking the QTextTable border logic (QTBUG-36152)
  43. Widget::Widget(QWidget *parent) :
  44. QWidget(parent),
  45. ui(new Ui::Widget)
  46. {
  47. ui->setupUi(this);
  48. resize(1400, 800);
  49. connect(ui->docComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &Widget::onDocumentSelected);
  50. connect(ui->printButton, &QPushButton::clicked, this, &Widget::onPrint);
  51. connect(ui->previewButton, &QPushButton::clicked, this, &Widget::onPreview);
  52. connect(ui->openBrowserButton, &QPushButton::clicked, this, &Widget::onOpenBrowser);
  53. connect(ui->sourceEdit, &QTextEdit::textChanged, this,
  54. [this]() {
  55. // make this a world class HTML IDE
  56. auto pos = ui->htmlEdit->verticalScrollBar()->value();
  57. ui->htmlEdit->setHtml(ui->sourceEdit->toPlainText());
  58. ui->htmlEdit->verticalScrollBar()->setValue(pos);
  59. });
  60. ui->docComboBox->addItem(tr("Table Border Test"), ":/table-border-test.html");
  61. ui->docComboBox->addItem(tr("Table Border Header Test"), ":/table-border-test-header.html");
  62. ui->docComboBox->setCurrentIndex(0);
  63. }
  64. Widget::~Widget()
  65. {
  66. delete ui;
  67. }
  68. void Widget::onDocumentSelected()
  69. {
  70. QString url = ui->docComboBox->itemData(ui->docComboBox->currentIndex()).toString();
  71. QFile f(url);
  72. if (f.open(QFile::ReadOnly)) {
  73. ui->sourceEdit->setPlainText(QString::fromUtf8(f.readAll()));
  74. // preview HTML is set via textChanged signal
  75. }
  76. }
  77. void Widget::onPrint()
  78. {
  79. #ifndef QT_NO_PRINTER
  80. QPrinter printer(QPrinter::HighResolution);
  81. QPrintDialog dlg(&printer, this);
  82. if (ui->htmlEdit->textCursor().hasSelection())
  83. dlg.addEnabledOption(QAbstractPrintDialog::PrintSelection);
  84. dlg.setWindowTitle(tr("Print Document"));
  85. if (dlg.exec() == QDialog::Accepted) {
  86. ui->htmlEdit->print(&printer);
  87. }
  88. #endif
  89. }
  90. void Widget::onPreview()
  91. {
  92. #ifndef QT_NO_PRINTER
  93. QPrinter printer(QPrinter::HighResolution);
  94. QPrintPreviewDialog preview(&printer, this);
  95. connect(&preview, &QPrintPreviewDialog::paintRequested, ui->htmlEdit, &QTextEdit::print);
  96. preview.exec();
  97. #endif
  98. }
  99. void Widget::onOpenBrowser()
  100. {
  101. // write the current html to a temp file and open the system browser
  102. #ifndef QT_NO_DESKTOPSERVICES
  103. auto tf = new QTemporaryFile(QDir::tempPath() + "/XXXXXX.html", this);
  104. if (tf->open()) {
  105. tf->write(ui->sourceEdit->toPlainText().toUtf8());
  106. tf->close();
  107. QDesktopServices::openUrl(QUrl::fromLocalFile(tf->fileName()));
  108. }
  109. #endif
  110. }