OSDN Git Service

ca1c5eba61ed717d606101fbab8a2e7565364f1b
[qt-creator-jp/qt-creator-jp.git] / doc / pluginhowto / examples / htmleditor / htmlfile.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 "htmlfile.h"
40 #include<QFile>
41 #include<QFileInfo>
42
43 namespace HTMLEditorConstants
44 {
45     const char* const C_HTMLEDITOR_MIMETYPE = "text/html";
46     const char* const C_HTMLEDITOR = "HTML Editor";
47 }
48 struct HTMLFileData
49 {
50     HTMLFileData(): mimeType(HTMLEditorConstants::C_HTMLEDITOR),
51     editorWidget(0), editor(0), modified(false) { }
52     const QString mimeType;
53     HTMLEditorWidget* editorWidget;
54     HTMLEditor* editor;
55     QString fileName;
56     bool modified;
57 };
58
59 HTMLFile::HTMLFile(HTMLEditor* editor, HTMLEditorWidget* editorWidget)
60     : Core::IFile(editor)
61 {
62     d = new HTMLFileData;
63     d->editor = editor;
64     d->editorWidget = editorWidget;
65 }
66
67
68 HTMLFile::~HTMLFile()
69 {
70     delete d;
71 }
72
73 void HTMLFile::setModified(bool val)
74 {
75     if(d->modified == val)
76         return;
77
78     d->modified = val;
79     emit changed();
80 }
81
82 bool HTMLFile::isModified() const
83 {
84     return d->modified;
85 }
86
87 QString HTMLFile::mimeType() const
88 {
89     return d->mimeType;
90 }
91
92 bool HTMLFile::save(const QString &fileName)
93 {
94     QFile file(fileName);
95
96     if(file.open(QFile::WriteOnly))
97     {
98         d->fileName = fileName;
99
100         QByteArray content = d->editorWidget->content();
101         file.write(content);
102
103         setModified(false);
104
105         return true;
106     }
107     return false;
108 }
109
110 bool HTMLFile::open(const QString &fileName)
111 {
112     QFile file(fileName);
113
114     if(file.open(QFile::ReadOnly))
115     {
116         d->fileName = fileName;
117
118         QString path = QFileInfo(fileName).absolutePath();
119         d->editorWidget->setContent(file.readAll(), path);
120         d->editor->setDisplayName(d->editorWidget->title());
121
122         return true;
123     }
124     return false;
125 }
126
127 void HTMLFile::setFilename(const QString& filename)
128 {
129     d->fileName = filename;
130 }
131
132 QString HTMLFile::fileName() const
133 {
134     return d->fileName;
135 }
136 QString HTMLFile::defaultPath() const
137 {
138     return QString();
139 }
140
141 QString HTMLFile::suggestedFileName() const
142 {
143     return QString();
144 }
145
146 QString HTMLFile::fileFilter() const
147 {
148     return QString();
149 }
150
151 QString HTMLFile::fileExtension() const
152 {
153     return QString();
154 }
155
156 bool HTMLFile::isReadOnly() const
157 {
158     return false;
159 }
160 bool HTMLFile::isSaveAsAllowed() const
161 {
162     return true;
163 }
164
165 void HTMLFile::modified(ReloadBehavior* behavior)
166 {
167     Q_UNUSED(behavior);
168 }