OSDN Git Service

a2a94513633d140f0bc6e637580e2ca4deb22fd7
[qt-creator-jp/qt-creator-jp.git] / doc / pluginhowto / examples / htmleditor / htmleditorwidget.cpp
1 /***************************************************************************
2 **
3 ** Copyright (C) 2011 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 documentation of Qt Creator.
8 **
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
21 **     the names of its contributors may be used to endorse or promote
22 **     products derived from this software without specific prior written
23 **     permission.
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ****************************************************************************/
38
39 #include "htmleditorwidget.h"
40
41 #include <QTextEdit>
42 #include <QPlainTextEdit>
43 #include <QtWebKit>
44 #include <QDebug>
45
46 struct HTMLEditorWidgetData
47 {
48     QWebView* webView;
49     QPlainTextEdit* textEdit;
50     bool modified;
51     QString path;
52 };
53
54 HTMLEditorWidget::HTMLEditorWidget(QWidget* parent):QTabWidget(parent)
55 {
56     d = new HTMLEditorWidgetData;
57
58     d->webView = new QWebView;
59     d->textEdit = new QPlainTextEdit;
60
61     addTab(d->webView, "Preview");
62     addTab(d->textEdit, "Source");
63     setTabPosition(QTabWidget::South);
64     setTabShape(QTabWidget::Triangular);
65
66     d->textEdit->setFont( QFont("Courier", 12) );
67
68     connect(this, SIGNAL(currentChanged(int)),
69             this, SLOT(slotCurrentTabChanged(int)));
70
71     connect(d->textEdit, SIGNAL(textChanged()),
72             this, SLOT(slotContentModified()));
73
74     connect(d->webView, SIGNAL(titleChanged(QString)),
75             this, SIGNAL(titleChanged(QString)));
76
77     d->modified = false;
78 }
79
80
81 HTMLEditorWidget::~HTMLEditorWidget()
82 {
83     delete d;
84 }
85
86 void HTMLEditorWidget::setContent(const QByteArray& ba, const QString& path)
87 {
88     if(path.isEmpty())
89         d->webView->setHtml(ba);
90     else
91         d->webView->setHtml(ba, "file:///" + path);
92     d->textEdit->setPlainText(ba);
93     d->modified = false;
94     d->path = path;
95 }
96
97 QByteArray HTMLEditorWidget::content() const
98 {
99     QString HTMLText = d->textEdit->toPlainText();
100     return HTMLText.toAscii();
101 }
102
103 QString HTMLEditorWidget::title() const
104 {
105     return d->webView->title();
106 }
107
108 void HTMLEditorWidget::slotCurrentTabChanged(int tab)
109 {
110     if(tab == 0 && d->modified)
111         setContent( content(), d->path );
112 }
113
114 void HTMLEditorWidget::slotContentModified()
115 {
116     d->modified = true;
117     emit contentModified();
118 }
119
120