summaryrefslogtreecommitdiffstats
path: root/tests/manual/qtexttableborders/widget.cpp
blob: 21d958ce818f16c9e03216ce7270046cb4bb2b0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/****************************************************************************
**
** Copyright (C) 2019 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "widget.h"
#include "ui_widget.h"

#include <QScrollBar>
#include <QFile>
#include <QDir>
#include <QTemporaryFile>

#ifndef QT_NO_DESKTOPSERVICES
#include <QDesktopServices>
#endif

#ifndef QT_NO_PRINTER
#include <QPrinter>
#include <QPrintDialog>
#include <QPrintPreviewDialog>
#endif

// This manual test allows checking the QTextTable border logic (QTBUG-36152)

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    resize(1400, 800);

    connect(ui->docComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &Widget::onDocumentSelected);
    connect(ui->printButton, &QPushButton::clicked, this, &Widget::onPrint);
    connect(ui->previewButton, &QPushButton::clicked, this, &Widget::onPreview);
    connect(ui->openBrowserButton, &QPushButton::clicked, this, &Widget::onOpenBrowser);

    connect(ui->sourceEdit, &QTextEdit::textChanged, this,
            [this]() {
                // make this a world class HTML IDE
                auto pos = ui->htmlEdit->verticalScrollBar()->value();
                ui->htmlEdit->setHtml(ui->sourceEdit->toPlainText());
                ui->htmlEdit->verticalScrollBar()->setValue(pos);
            });

    ui->docComboBox->addItem(tr("Table Border Test"), ":/table-border-test.html");
    ui->docComboBox->addItem(tr("Table Border Header Test"), ":/table-border-test-header.html");

    ui->docComboBox->setCurrentIndex(0);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::onDocumentSelected()
{
    QString url = ui->docComboBox->itemData(ui->docComboBox->currentIndex()).toString();
    QFile f(url);
    if (f.open(QFile::ReadOnly)) {
        ui->sourceEdit->setPlainText(QString::fromUtf8(f.readAll()));
        // preview HTML is set via textChanged signal
    }
}

void Widget::onPrint()
{
#ifndef QT_NO_PRINTER
    QPrinter printer(QPrinter::HighResolution);
    QPrintDialog dlg(&printer, this);
    if (ui->htmlEdit->textCursor().hasSelection())
        dlg.addEnabledOption(QAbstractPrintDialog::PrintSelection);
    dlg.setWindowTitle(tr("Print Document"));
    if (dlg.exec() == QDialog::Accepted) {
        ui->htmlEdit->print(&printer);
    }
#endif
}

void Widget::onPreview()
{
#ifndef QT_NO_PRINTER
    QPrinter printer(QPrinter::HighResolution);
    QPrintPreviewDialog preview(&printer, this);
    connect(&preview, &QPrintPreviewDialog::paintRequested, ui->htmlEdit, &QTextEdit::print);
    preview.exec();
#endif
}

void Widget::onOpenBrowser()
{
    // write the current html to a temp file and open the system browser
#ifndef QT_NO_DESKTOPSERVICES
    auto tf = new QTemporaryFile(QDir::tempPath() + "/XXXXXX.html", this);
    if (tf->open()) {
        tf->write(ui->sourceEdit->toPlainText().toUtf8());
        tf->close();
        QDesktopServices::openUrl(QUrl::fromLocalFile(tf->fileName()));
    }
#endif
}