File indexing completed on 2024-04-28 15:25:15

0001 // program to test the new khtml implementation
0002 
0003 #include "testkhtmlnew.h"
0004 
0005 #include <QAction>
0006 #include <QCursor>
0007 #include <QBoxLayout>
0008 #include <QFrame>
0009 #include <QToolButton>
0010 #include <QComboBox>
0011 #include <QLineEdit>
0012 #include <QMovie>
0013 #include <QLabel>
0014 #include <QImage>
0015 #include <QDomDocument>
0016 #include <QApplication>
0017 
0018 #include <kxmlguifactory.h>
0019 #include <ktoggleaction.h>
0020 #include <kstandardaction.h>
0021 #include <kactioncollection.h>
0022 
0023 #include "css/cssstyleselector.h"
0024 
0025 #include "dom/dom_string.h"
0026 #include "dom/dom2_range.h"
0027 #include "dom/html_document.h"
0028 #include "dom/dom_exception.h"
0029 
0030 #include "html/htmltokenizer.h"
0031 #include "html/html_imageimpl.h"
0032 
0033 #include "misc/loader.h"
0034 
0035 #include "rendering/render_style.h"
0036 
0037 #include "khtml_part.h"
0038 #include "khtml_global.h"
0039 
0040 TestKHTML::TestKHTML()
0041     : KXmlGuiWindow()
0042 {
0043 #ifndef __KDE_HAVE_GCC_VISIBILITY
0044     m_factory = new KHTMLGlobal();
0045 #endif
0046 
0047     m_movie = new QMovie(":/pics/progress.gif");
0048     m_indicator = new QLabel;
0049     m_indicator->setFixedSize(QSize(24, 24));
0050     m_indicator->setMovie(m_movie);
0051 
0052     m_part = new KHTMLPart(this, this, KHTMLPart::BrowserViewGUI);
0053     connect(m_part->browserExtension(), SIGNAL(openUrlRequest(QUrl,KParts::OpenUrlArguments,KParts::BrowserArguments)),
0054             this, SLOT(openUrl(QUrl,KParts::OpenUrlArguments,KParts::BrowserArguments)));
0055 
0056     m_combo = new QComboBox;
0057     m_combo->setEditable(true);
0058     connect(m_combo, SIGNAL(returnPressed(QString)), this, SLOT(openUrl(QString)));
0059 
0060     m_goButton = new QToolButton;
0061     m_goButton->setText("Go");
0062     connect(m_goButton, SIGNAL(clicked()), this, SLOT(openUrl()));
0063 
0064     m_reloadButton = new QToolButton;
0065     m_reloadButton->setIcon(QIcon::fromTheme("view-refresh"));
0066     connect(m_reloadButton, SIGNAL(clicked()), this, SLOT(reload()));
0067 
0068     QHBoxLayout *topLayout = new QHBoxLayout;
0069     topLayout->addWidget(m_reloadButton);
0070     topLayout->addWidget(m_combo);
0071     topLayout->addWidget(m_goButton);
0072     topLayout->addWidget(m_indicator);
0073     m_indicator->show();
0074 
0075     QFrame *mainWidget = new QFrame;
0076     QVBoxLayout *mainLayout = new QVBoxLayout(mainWidget);
0077     mainLayout->addLayout(topLayout);
0078     mainLayout->addWidget(m_part->widget());
0079 
0080     setCentralWidget(mainWidget);
0081     resize(800, 600);
0082     setupActions();
0083 
0084     m_part->setJScriptEnabled(true);
0085     m_part->setJavaEnabled(true);
0086     m_part->setPluginsEnabled(true);
0087     m_part->setURLCursor(QCursor(Qt::PointingHandCursor));
0088 
0089     connect(m_part, SIGNAL(setWindowCaption(QString)),
0090             m_part->widget()->topLevelWidget(), SLOT(setCaption(QString)));
0091 
0092     connect(m_part, SIGNAL(started(KIO::Job*)), this, SLOT(startLoading()));
0093     connect(m_part, SIGNAL(completed()), this, SLOT(finishedLoading()));
0094 }
0095 
0096 void TestKHTML::startLoading()
0097 {
0098     m_movie->start();
0099 }
0100 
0101 void TestKHTML::finishedLoading()
0102 {
0103     m_movie->stop();
0104 }
0105 
0106 TestKHTML::~TestKHTML()
0107 {
0108 #ifndef __KDE_HAVE_GCC_VISIBILITY
0109     delete m_factory;
0110 #endif
0111 }
0112 
0113 void TestKHTML::setupActions()
0114 {
0115     QDomDocument document = m_part->domDocument();
0116     QDomElement fileMenu = document.documentElement().firstChild().childNodes().item(0).toElement();
0117 
0118     QDomElement quitElement = document.createElement("action");
0119     quitElement.setAttribute("name",
0120                              KStandardAction::name(KStandardAction::Quit));
0121     fileMenu.appendChild(quitElement);
0122 
0123     QDomElement viewMenu = document.documentElement().firstChild().childNodes().item(2).toElement();
0124 
0125     QDomElement element = document.createElement("action");
0126     element.setAttribute("name", "debugRenderTree");
0127     viewMenu.appendChild(element);
0128 
0129     element = document.createElement("action");
0130     element.setAttribute("name", "debugDOMTree");
0131     viewMenu.appendChild(element);
0132 
0133     QDomElement toolBar = document.documentElement().firstChild().nextSibling().toElement();
0134     element = document.createElement("action");
0135     element.setAttribute("name", "editable");
0136     toolBar.insertBefore(element, toolBar.firstChild());
0137 
0138     element = document.createElement("action");
0139     element.setAttribute("name", "navigable");
0140     toolBar.insertBefore(element, toolBar.firstChild());
0141 
0142     element = document.createElement("action");
0143     element.setAttribute("name", "reload");
0144     toolBar.insertBefore(element, toolBar.firstChild());
0145 
0146     element = document.createElement("action");
0147     element.setAttribute("name", "print");
0148     toolBar.insertBefore(element, toolBar.firstChild());
0149 
0150     QAction *action = new QAction(QIcon::fromTheme("view-refresh"), "Reload", this);
0151     m_part->actionCollection()->addAction("reload", action);
0152     connect(action, SIGNAL(triggered(bool)), this, SLOT(reload()));
0153     m_part->actionCollection()->setDefaultShortcut(action, Qt::Key_F5);
0154 
0155     QAction *kprint = new QAction(QIcon::fromTheme("document-print"), "Print", this);
0156     m_part->actionCollection()->addAction("print", kprint);
0157     connect(kprint, SIGNAL(triggered(bool)), m_part->browserExtension(), SLOT(print()));
0158     kprint->setEnabled(true);
0159 
0160     KToggleAction *ta = new KToggleAction(QIcon::fromTheme("edit-rename"), "Navigable", this);
0161     actionCollection()->addAction("navigable", ta);
0162     ta->setShortcuts(QList<QKeySequence>());
0163     ta->setChecked(m_part->isCaretMode());
0164     connect(ta, SIGNAL(toggled(bool)), this, SLOT(toggleNavigable(bool)));
0165 
0166     ta = new KToggleAction(QIcon::fromTheme("document-properties"), "Editable", this);
0167     actionCollection()->addAction("editable", ta);
0168     ta->setShortcuts(QList<QKeySequence>());
0169     ta->setChecked(m_part->isEditable());
0170     connect(ta, SIGNAL(toggled(bool)), this, SLOT(toggleEditable(bool)));
0171 
0172     KStandardAction::quit(qApp, SLOT(quit()), m_part->actionCollection());
0173 
0174     guiFactory()->addClient(m_part);
0175 }
0176 
0177 KHTMLPart *TestKHTML::doc() const
0178 {
0179     if (m_part) {
0180         return m_part;
0181     }
0182 
0183     return nullptr;
0184 }
0185 
0186 void TestKHTML::openUrl(const QUrl &url, const KParts::OpenUrlArguments &args, const KParts::BrowserArguments &browserArgs)
0187 {
0188     m_part->setArguments(args);
0189     m_part->browserExtension()->setBrowserArguments(browserArgs);
0190     m_part->openUrl(url);
0191 }
0192 
0193 void TestKHTML::openUrl(const QUrl &url)
0194 {
0195     m_part->openUrl(url);
0196 }
0197 
0198 void TestKHTML::openUrl(const QString &url)
0199 {
0200     qDebug() << "url: " << url;
0201     QUrl parsedUrl(url);
0202     m_part->openUrl(parsedUrl);
0203 }
0204 
0205 void TestKHTML::openUrl()
0206 {
0207     QLineEdit *edit = m_combo->lineEdit();
0208     if (!edit) {
0209         return;
0210     }
0211 
0212     QString url = edit->text();
0213     QUrl parsedUrl(url);
0214     m_part->openUrl(parsedUrl);
0215 }
0216 
0217 void TestKHTML::reload()
0218 {
0219     KParts::OpenUrlArguments args;
0220     args.setReload(true);
0221     m_part->setArguments(args);
0222     m_part->openUrl(m_part->url());
0223 }
0224 
0225 void TestKHTML::toggleNavigable(bool s)
0226 {
0227     m_part->setCaretMode(s);
0228 }
0229 
0230 void TestKHTML::toggleEditable(bool s)
0231 {
0232     qDebug() << "editable: " << s;
0233     m_part->setEditable(s);
0234 }
0235 
0236 // a basic web browser using the KHTML library
0237 int main(int argc, char *argv[])
0238 {
0239     QApplication app(argc, argv);
0240 
0241     if (app.arguments().count() <= 1) {
0242         qWarning() << "Argument expected: url to open";
0243         return 1;
0244     }
0245 
0246     TestKHTML *test = new TestKHTML;
0247     QUrl url = QUrl::fromUserInput(app.arguments().at(1)); // TODO support for relative paths
0248     if (url.path().right(4).toLower() == ".xml") {
0249         KParts::OpenUrlArguments args(test->doc()->arguments());
0250         args.setMimeType("text/xml");
0251         test->doc()->setArguments(args);
0252     }
0253 
0254     test->openUrl(url);
0255     test->show();
0256 
0257     return app.exec();
0258 }
0259 
0260 #include "moc_testkhtmlnew.cpp"