fix logic
[personal-kdelibs.git] / khtml / tests / testkhtmlnew.cpp
blob85b7fe0a2f0c5497aad3502a835782992d4f80d4
1 // program to test the new khtml implementation
3 #include "testkhtmlnew.h"
4 #include "testkhtmlnew.moc"
6 #include <QtGui/QCursor>
7 #include <QtGui/QBoxLayout>
8 #include <QtGui/QFrame>
9 #include <QtGui/QToolButton>
10 #include <QtGui/QComboBox>
11 #include <QtGui/QLineEdit>
12 #include <QtGui/QMovie>
13 #include <QtGui/QLabel>
14 #include <QtGui/QImage>
15 #include <QtXml/QDomDocument>
17 #include <kapplication.h>
18 #include <kxmlguifactory.h>
19 #include <kxmlguiwindow.h>
20 #include <kcmdlineargs.h>
21 #include <ktoggleaction.h>
22 #include <kcombobox.h>
23 #include <kpushbutton.h>
24 #include <kicon.h>
25 #include <kstandardaction.h>
26 #include <kactioncollection.h>
28 #include "css/cssstyleselector.h"
30 #include "dom/dom_string.h"
31 #include "dom/dom2_range.h"
32 #include "dom/html_document.h"
33 #include "dom/dom_exception.h"
35 #include "html/htmltokenizer.h"
36 #include "html/html_imageimpl.h"
38 #include "misc/loader.h"
40 #include "rendering/render_style.h"
42 #include "khtml_part.h"
43 #include "khtml_global.h"
45 TestKHTML::TestKHTML()
46 : KXmlGuiWindow()
48 #ifndef __KDE_HAVE_GCC_VISIBILITY
49 m_factory = new KHTMLGlobal();
50 #endif
52 m_movie = new QMovie(":/pics/progress.gif");
53 m_indicator = new QLabel;
54 m_indicator->setFixedSize(QSize(24, 24));
55 m_indicator->setMovie(m_movie);
57 m_part = new KHTMLPart( this, this, KHTMLPart::BrowserViewGUI );
58 connect( m_part->browserExtension(), SIGNAL(openUrlRequest(const KUrl&, const KParts::OpenUrlArguments&, const KParts::BrowserArguments &)),
59 this, SLOT(openUrl( const KUrl&, const KParts::OpenUrlArguments&, const KParts::BrowserArguments & )));
61 m_combo = new KComboBox;
62 m_combo->setEditable(true);
63 connect(m_combo, SIGNAL(returnPressed(const QString&)), this, SLOT(openUrl(const QString&)));
65 m_goButton = new QToolButton;
66 m_goButton->setText("Go");
67 connect(m_goButton, SIGNAL(clicked()), this, SLOT(openUrl()));
69 m_reloadButton = new QToolButton;
70 m_reloadButton->setIcon(KIcon("view-refresh"));
71 connect(m_reloadButton, SIGNAL(clicked()), this, SLOT(reload()));
73 QHBoxLayout *topLayout = new QHBoxLayout;
74 topLayout->addWidget(m_reloadButton);
75 topLayout->addWidget(m_combo);
76 topLayout->addWidget(m_goButton);
77 topLayout->addWidget(m_indicator);
78 m_indicator->show();
80 QFrame *mainWidget = new QFrame;
81 QVBoxLayout *mainLayout = new QVBoxLayout(mainWidget);
82 mainLayout->addLayout(topLayout);
83 mainLayout->addWidget(m_part->widget());
85 setCentralWidget(mainWidget);
86 resize(800, 600);
87 setupActions();
89 m_part->setJScriptEnabled(true);
90 m_part->setJavaEnabled(true);
91 m_part->setPluginsEnabled(true);
92 m_part->setURLCursor(QCursor(Qt::PointingHandCursor));
94 connect(m_part, SIGNAL(setWindowCaption(const QString &)),
95 m_part->widget()->topLevelWidget(), SLOT(setCaption(const QString &)));
97 connect(m_part, SIGNAL(started(KIO::Job *)), this, SLOT(startLoading()));
98 connect(m_part, SIGNAL(completed()), this, SLOT(finishedLoading()));
101 void TestKHTML::startLoading()
103 m_movie->start();
106 void TestKHTML::finishedLoading()
108 m_movie->stop();
111 TestKHTML::~TestKHTML()
113 #ifndef __KDE_HAVE_GCC_VISIBILITY
114 delete m_factory;
115 #endif
118 void TestKHTML::setupActions()
120 QDomDocument document = m_part->domDocument();
121 QDomElement fileMenu = document.documentElement().firstChild().childNodes().item( 0 ).toElement();
123 QDomElement quitElement = document.createElement("action");
124 quitElement.setAttribute("name", "quit");
125 fileMenu.appendChild(quitElement);
127 QDomElement viewMenu = document.documentElement().firstChild().childNodes().item( 2 ).toElement();
129 QDomElement element = document.createElement("action");
130 element.setAttribute("name", "debugRenderTree");
131 viewMenu.appendChild(element);
133 element = document.createElement("action");
134 element.setAttribute("name", "debugDOMTree");
135 viewMenu.appendChild(element);
137 QDomElement toolBar = document.documentElement().firstChild().nextSibling().toElement();
138 element = document.createElement("action");
139 element.setAttribute("name", "editable");
140 toolBar.insertBefore(element, toolBar.firstChild());
142 element = document.createElement("action");
143 element.setAttribute("name", "navigable");
144 toolBar.insertBefore(element, toolBar.firstChild());
146 element = document.createElement("action");
147 element.setAttribute("name", "reload");
148 toolBar.insertBefore(element, toolBar.firstChild());
150 element = document.createElement("action");
151 element.setAttribute("name", "print");
152 toolBar.insertBefore(element, toolBar.firstChild());
155 KAction *quitAction = new KAction( i18n( "Quit" ), this );
156 m_part->actionCollection()->addAction( "quit", quitAction );
157 connect( quitAction, SIGNAL( triggered( bool ) ), kapp, SLOT( quit() ) );
159 KAction *action = new KAction(KIcon("view-refresh"), "Reload", this );
160 m_part->actionCollection()->addAction( "reload", action );
161 connect(action, SIGNAL(triggered(bool)), this, SLOT(reload()));
162 action->setShortcut(Qt::Key_F5);
164 KAction *kprint = new KAction(KIcon("document-print"), "Print", this );
165 m_part->actionCollection()->addAction( "print", kprint );
166 connect(kprint, SIGNAL(triggered(bool)), m_part->browserExtension(), SLOT(print()));
167 kprint->setEnabled(true);
169 KToggleAction *ta = new KToggleAction( KIcon("edit-rename"), "Navigable", this );
170 actionCollection()->addAction( "navigable", ta );
171 ta->setShortcuts( KShortcut() );
172 ta->setChecked(m_part->isCaretMode());
173 connect(ta, SIGNAL(toggled(bool)), this, SLOT( toggleNavigable(bool) ));
175 ta = new KToggleAction( KIcon("document-properties"), "Editable", this );
176 actionCollection()->addAction( "editable", ta );
177 ta->setShortcuts( KShortcut() );
178 ta->setChecked(m_part->isEditable());
179 connect(ta, SIGNAL(toggled(bool)), this, SLOT(toggleEditable(bool)));
181 KStandardAction::quit( this, SLOT(kpp->quit()), m_part->actionCollection() );
183 guiFactory()->addClient(m_part);
186 KHTMLPart *TestKHTML::doc() const
188 if (m_part)
189 return m_part;
191 return 0;
194 void TestKHTML::openUrl( const KUrl &url, const KParts::OpenUrlArguments& args, const KParts::BrowserArguments &browserArgs )
196 m_part->setArguments(args);
197 m_part->browserExtension()->setBrowserArguments(browserArgs);
198 m_part->openUrl( url );
201 void TestKHTML::openUrl(const KUrl &url)
203 m_part->openUrl(url);
206 void TestKHTML::openUrl(const QString &url)
208 kDebug() << "url: " << url;
209 KUrl parsedUrl(url);
210 m_part->openUrl(parsedUrl);
213 void TestKHTML::openUrl()
215 QLineEdit *edit = m_combo->lineEdit();
216 if (!edit)
217 return;
219 QString url = edit->text();
220 KUrl parsedUrl(url);
221 m_part->openUrl(parsedUrl);
224 void TestKHTML::reload()
226 KParts::OpenUrlArguments args;
227 args.setReload( true );
228 m_part->setArguments( args );
229 m_part->openUrl( m_part->url() );
232 void TestKHTML::toggleNavigable(bool s)
234 m_part->setCaretMode(s);
237 void TestKHTML::toggleEditable(bool s)
239 kDebug() << "editable: " << s;
240 m_part->setEditable(s);
243 int main(int argc, char *argv[])
245 KCmdLineArgs::init(argc, argv, "testkhtml", 0, ki18n("Testkhtml"), "1.0",
246 ki18n("a basic web browser using the KHTML library"));
248 KCmdLineOptions options;
249 options.add("+file", ki18n("url to open"));
250 KCmdLineArgs::addCmdLineOptions(options);
252 KApplication app;
253 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
254 if (args->count() == 0)
256 KCmdLineArgs::usage();
257 ::exit( 1 );
260 TestKHTML *test = new TestKHTML;
261 if (args->url(0).url().right(4).toLower() == ".xml")
263 KParts::OpenUrlArguments args(test->doc()->arguments());
264 args.setMimeType("text/xml");
265 test->doc()->setArguments(args);
268 test->openUrl(args->url(0));
269 test->show();
271 return app.exec();