2009-02-25 09:15:00 +01:00
|
|
|
/**************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2012-01-26 18:33:46 +01:00
|
|
|
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2011-11-02 15:59:12 +01:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** This file may be used under the terms of the GNU Lesser General Public
|
|
|
|
|
** License version 2.1 as published by the Free Software Foundation and
|
|
|
|
|
** appearing in the file LICENSE.LGPL included in the packaging of this file.
|
|
|
|
|
** Please review the following information to ensure the GNU Lesser General
|
|
|
|
|
** Public License version 2.1 requirements will be met:
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Other Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
|
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** If you have questions regarding the use of this file, please contact
|
2011-11-02 15:59:12 +01:00
|
|
|
** Nokia at qt-info@nokia.com.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
**************************************************************************/
|
2008-12-02 15:08:31 +01:00
|
|
|
|
2010-03-09 10:26:20 +01:00
|
|
|
#include "formwindoweditor.h"
|
2010-03-10 10:21:52 +01:00
|
|
|
#include "formwindowfile.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "designerconstants.h"
|
2010-03-10 10:21:52 +01:00
|
|
|
#include "resourcehandler.h"
|
|
|
|
|
#include "designerxmleditor.h"
|
|
|
|
|
#include <widgethost.h>
|
|
|
|
|
|
|
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
|
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
#include <coreplugin/coreconstants.h>
|
2010-05-21 14:18:34 +02:00
|
|
|
#include <coreplugin/mimedatabase.h>
|
2010-03-10 10:21:52 +01:00
|
|
|
#include <texteditor/basetextdocument.h>
|
|
|
|
|
#include <texteditor/plaintexteditor.h>
|
2010-03-09 10:26:20 +01:00
|
|
|
|
2009-05-13 15:10:09 +02:00
|
|
|
#include <utils/qtcassert.h>
|
2011-04-04 15:24:13 +02:00
|
|
|
#include <utils/fileutils.h>
|
2009-05-13 15:10:09 +02:00
|
|
|
|
2011-05-19 11:30:38 +02:00
|
|
|
#if QT_VERSION >= 0x050000
|
2012-02-15 10:42:41 +01:00
|
|
|
# include <QDesignerFormWindowInterface>
|
|
|
|
|
# include <QBuffer>
|
2011-05-19 11:30:38 +02:00
|
|
|
#else
|
|
|
|
|
# include "qt_private/formwindowbase_p.h"
|
|
|
|
|
#endif
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QFile>
|
|
|
|
|
#include <QApplication>
|
2010-03-10 10:21:52 +01:00
|
|
|
|
|
|
|
|
namespace Designer {
|
|
|
|
|
|
2010-06-25 12:56:16 +02:00
|
|
|
struct FormWindowEditorPrivate
|
|
|
|
|
{
|
2011-04-13 13:00:30 +02:00
|
|
|
FormWindowEditorPrivate(Internal::DesignerXmlEditor *editor,
|
|
|
|
|
QDesignerFormWindowInterface *form)
|
|
|
|
|
: m_textEditor(editor), m_file(form)
|
|
|
|
|
{}
|
2010-03-10 10:21:52 +01:00
|
|
|
|
2011-02-21 19:30:43 +01:00
|
|
|
TextEditor::PlainTextEditor m_textEditor;
|
2010-03-10 10:21:52 +01:00
|
|
|
Internal::FormWindowFile m_file;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
FormWindowEditor::FormWindowEditor(Internal::DesignerXmlEditor *editor,
|
2011-02-21 19:30:43 +01:00
|
|
|
QDesignerFormWindowInterface *form,
|
|
|
|
|
QObject *parent) :
|
2010-03-10 10:21:52 +01:00
|
|
|
Core::IEditor(parent),
|
|
|
|
|
d(new FormWindowEditorPrivate(editor, form))
|
|
|
|
|
{
|
2011-04-13 13:00:30 +02:00
|
|
|
setContext(Core::Context(Designer::Constants::K_DESIGNER_XML_EDITOR_ID,
|
|
|
|
|
Designer::Constants::C_DESIGNER_XML_EDITOR));
|
|
|
|
|
setWidget(d->m_textEditor.widget());
|
|
|
|
|
|
2010-03-10 10:21:52 +01:00
|
|
|
connect(form, SIGNAL(changed()), this, SIGNAL(changed()));
|
2011-02-21 19:30:43 +01:00
|
|
|
// Revert to saved/load externally modified files.
|
2011-04-04 15:24:13 +02:00
|
|
|
connect(&d->m_file, SIGNAL(reload(QString*,QString)), this, SLOT(slotOpen(QString*,QString)));
|
2010-03-12 16:54:32 +01:00
|
|
|
// Force update of open editors model.
|
2011-02-21 19:30:43 +01:00
|
|
|
connect(&d->m_file, SIGNAL(saved()), this, SIGNAL(changed()));
|
|
|
|
|
connect(&d->m_file, SIGNAL(changed()), this, SIGNAL(changed()));
|
2010-03-10 10:21:52 +01:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-03-10 10:21:52 +01:00
|
|
|
FormWindowEditor::~FormWindowEditor()
|
|
|
|
|
{
|
|
|
|
|
delete d;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FormWindowEditor::createNew(const QString &contents)
|
|
|
|
|
{
|
|
|
|
|
if (Designer::Constants::Internal::debug)
|
|
|
|
|
qDebug() << "FormWindowEditor::createNew" << contents.size();
|
|
|
|
|
|
|
|
|
|
syncXmlEditor(QString());
|
|
|
|
|
|
|
|
|
|
QDesignerFormWindowInterface *form = d->m_file.formWindow();
|
|
|
|
|
QTC_ASSERT(form, return false);
|
|
|
|
|
|
|
|
|
|
if (contents.isEmpty())
|
|
|
|
|
return false;
|
|
|
|
|
|
2010-09-24 15:19:14 +02:00
|
|
|
// If we have an override cursor, reset it over Designer loading,
|
|
|
|
|
// should it pop up messages about missing resources or such.
|
|
|
|
|
const bool hasOverrideCursor = QApplication::overrideCursor();
|
|
|
|
|
QCursor overrideCursor;
|
|
|
|
|
if (hasOverrideCursor) {
|
|
|
|
|
overrideCursor = QCursor(*QApplication::overrideCursor());
|
|
|
|
|
QApplication::restoreOverrideCursor();
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-19 11:30:38 +02:00
|
|
|
#if QT_VERSION >= 0x050000
|
|
|
|
|
const bool success = form->setContents(contents);
|
|
|
|
|
#else
|
2010-03-10 10:21:52 +01:00
|
|
|
form->setContents(contents);
|
2011-05-19 11:30:38 +02:00
|
|
|
const bool success = form->mainContainer() != 0;
|
|
|
|
|
#endif
|
2010-09-24 15:19:14 +02:00
|
|
|
|
|
|
|
|
if (hasOverrideCursor)
|
|
|
|
|
QApplication::setOverrideCursor(overrideCursor);
|
|
|
|
|
|
2011-05-19 11:30:38 +02:00
|
|
|
if (!success)
|
2010-03-10 10:21:52 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
syncXmlEditor(contents);
|
|
|
|
|
d->m_file.setFileName(QString());
|
2011-05-10 20:43:03 +02:00
|
|
|
d->m_file.setShouldAutoSave(false);
|
2010-03-10 10:21:52 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2011-04-04 15:24:13 +02:00
|
|
|
void FormWindowEditor::slotOpen(QString *errorString, const QString &fileName)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2011-05-10 20:43:03 +02:00
|
|
|
open(errorString, fileName, fileName);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2011-05-10 20:43:03 +02:00
|
|
|
bool FormWindowEditor::open(QString *errorString, const QString &fileName, const QString &realFileName)
|
2009-10-01 16:38:08 +02:00
|
|
|
{
|
2010-03-09 10:26:20 +01:00
|
|
|
if (Designer::Constants::Internal::debug)
|
2010-03-10 10:21:52 +01:00
|
|
|
qDebug() << "FormWindowEditor::open" << fileName;
|
2009-10-01 16:38:08 +02:00
|
|
|
|
2010-03-10 10:21:52 +01:00
|
|
|
QDesignerFormWindowInterface *form = d->m_file.formWindow();
|
|
|
|
|
QTC_ASSERT(form, return false);
|
|
|
|
|
|
|
|
|
|
if (fileName.isEmpty()) {
|
|
|
|
|
setDisplayName(tr("untitled"));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QFileInfo fi(fileName);
|
|
|
|
|
const QString absfileName = fi.absoluteFilePath();
|
|
|
|
|
|
2011-08-25 12:54:20 +02:00
|
|
|
QString contents;
|
|
|
|
|
if (d->m_file.read(absfileName, &contents, errorString) != Utils::TextFileFormat::ReadSuccess)
|
2010-03-10 10:21:52 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
form->setFileName(absfileName);
|
2011-05-19 11:30:38 +02:00
|
|
|
#if QT_VERSION >= 0x050000
|
2011-08-25 12:54:20 +02:00
|
|
|
const QByteArray contentsBA = contents.toUtf8();
|
2011-11-09 13:51:02 +01:00
|
|
|
QBuffer str;
|
|
|
|
|
str.setData(contentsBA);
|
2011-05-19 11:30:38 +02:00
|
|
|
str.open(QIODevice::ReadOnly);
|
|
|
|
|
if (!form->setContents(&str, errorString))
|
|
|
|
|
return false;
|
|
|
|
|
#else
|
2011-08-25 12:54:20 +02:00
|
|
|
form->setContents(contents);
|
2010-03-10 10:21:52 +01:00
|
|
|
if (!form->mainContainer())
|
|
|
|
|
return false;
|
2011-05-19 11:30:38 +02:00
|
|
|
#endif
|
2011-05-10 20:43:03 +02:00
|
|
|
form->setDirty(fileName != realFileName);
|
2010-03-10 10:21:52 +01:00
|
|
|
syncXmlEditor(contents);
|
|
|
|
|
|
|
|
|
|
setDisplayName(fi.fileName());
|
|
|
|
|
d->m_file.setFileName(absfileName);
|
2011-05-10 20:43:03 +02:00
|
|
|
d->m_file.setShouldAutoSave(false);
|
2010-03-10 10:21:52 +01:00
|
|
|
|
|
|
|
|
if (Internal::ResourceHandler *rh = qFindChild<Designer::Internal::ResourceHandler*>(form))
|
|
|
|
|
rh->updateResources();
|
|
|
|
|
|
|
|
|
|
emit changed();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FormWindowEditor::syncXmlEditor()
|
|
|
|
|
{
|
|
|
|
|
if (Designer::Constants::Internal::debug)
|
|
|
|
|
qDebug() << "FormWindowEditor::syncXmlEditor" << d->m_file.fileName();
|
|
|
|
|
syncXmlEditor(contents());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FormWindowEditor::syncXmlEditor(const QString &contents)
|
|
|
|
|
{
|
2011-02-21 19:30:43 +01:00
|
|
|
d->m_textEditor.editorWidget()->setPlainText(contents);
|
|
|
|
|
d->m_textEditor.editorWidget()->setReadOnly(true);
|
2011-06-29 17:45:52 +02:00
|
|
|
static_cast<TextEditor::PlainTextEditorWidget *>
|
2012-02-14 16:43:51 +01:00
|
|
|
(d->m_textEditor.editorWidget())->configure(document()->mimeType());
|
2010-03-10 10:21:52 +01:00
|
|
|
}
|
|
|
|
|
|
2012-02-14 16:43:51 +01:00
|
|
|
Core::IDocument *FormWindowEditor::document()
|
2010-03-10 10:21:52 +01:00
|
|
|
{
|
|
|
|
|
return &d->m_file;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-10 11:36:51 +01:00
|
|
|
Core::Id FormWindowEditor::id() const
|
2010-03-10 10:21:52 +01:00
|
|
|
{
|
2011-11-10 11:36:51 +01:00
|
|
|
return Designer::Constants::K_DESIGNER_XML_EDITOR_ID;
|
2009-10-01 16:38:08 +02:00
|
|
|
}
|
2010-03-10 10:21:52 +01:00
|
|
|
|
|
|
|
|
QString FormWindowEditor::displayName() const
|
|
|
|
|
{
|
2011-02-21 19:30:43 +01:00
|
|
|
return d->m_textEditor.displayName();
|
2010-03-10 10:21:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FormWindowEditor::setDisplayName(const QString &title)
|
|
|
|
|
{
|
2011-02-21 19:30:43 +01:00
|
|
|
d->m_textEditor.setDisplayName(title);
|
2010-11-30 12:55:41 +01:00
|
|
|
emit changed();
|
2010-03-10 10:21:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FormWindowEditor::duplicateSupported() const
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Core::IEditor *FormWindowEditor::duplicate(QWidget *)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QByteArray FormWindowEditor::saveState() const
|
|
|
|
|
{
|
2011-02-21 19:30:43 +01:00
|
|
|
return d->m_textEditor.saveState();
|
2010-03-10 10:21:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FormWindowEditor::restoreState(const QByteArray &state)
|
|
|
|
|
{
|
2011-02-21 19:30:43 +01:00
|
|
|
return d->m_textEditor.restoreState(state);
|
2010-03-10 10:21:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FormWindowEditor::isTemporary() const
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QWidget *FormWindowEditor::toolBar()
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString FormWindowEditor::contents() const
|
|
|
|
|
{
|
2011-05-19 11:30:38 +02:00
|
|
|
#if QT_VERSION >= 0x050000 // TODO: No warnings about spacers here
|
|
|
|
|
const QDesignerFormWindowInterface *fw = d->m_file.formWindow();
|
|
|
|
|
QTC_ASSERT(fw, return QString());
|
|
|
|
|
return fw->contents();
|
|
|
|
|
#else
|
|
|
|
|
// No warnings about spacers here
|
2010-03-10 10:21:52 +01:00
|
|
|
const qdesigner_internal::FormWindowBase *fw = qobject_cast<const qdesigner_internal::FormWindowBase *>(d->m_file.formWindow());
|
|
|
|
|
QTC_ASSERT(fw, return QString());
|
2011-05-19 11:30:38 +02:00
|
|
|
return fw->fileContents();
|
|
|
|
|
#endif
|
2010-03-10 10:21:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextEditor::BaseTextDocument *FormWindowEditor::textDocument()
|
|
|
|
|
{
|
2012-02-14 16:43:51 +01:00
|
|
|
return qobject_cast<TextEditor::BaseTextDocument*>(d->m_textEditor.document());
|
2010-03-10 10:21:52 +01:00
|
|
|
}
|
|
|
|
|
|
2011-02-21 19:30:43 +01:00
|
|
|
TextEditor::PlainTextEditor *FormWindowEditor::textEditor()
|
2010-03-10 10:21:52 +01:00
|
|
|
{
|
2011-02-21 19:30:43 +01:00
|
|
|
return &d->m_textEditor;
|
2010-03-10 10:21:52 +01:00
|
|
|
}
|
|
|
|
|
|
2010-09-14 15:15:57 +02:00
|
|
|
QString FormWindowEditor::preferredModeType() const
|
2010-03-16 16:51:45 +01:00
|
|
|
{
|
2010-09-14 15:15:57 +02:00
|
|
|
return QLatin1String(Core::Constants::MODE_DESIGN_TYPE);
|
2010-03-16 16:51:45 +01:00
|
|
|
}
|
2010-03-10 10:21:52 +01:00
|
|
|
|
|
|
|
|
} // namespace Designer
|
|
|
|
|
|