Delete htmleditor, add ritchEdit

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@2095 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
cfdev
2013-04-09 11:26:53 +00:00
parent 4d0ad03e19
commit b35bb53512
45 changed files with 1140 additions and 1832 deletions

View File

@@ -63,11 +63,11 @@ DEPENDPATH += .
INCLUDEPATH += sources sources/editor sources/titleblock
# Fichiers sources
HEADERS += sources/*.h sources/ui/*.h sources/editor/*.h sources/titleblock/*.h sources/htmleditor/*.h
SOURCES += sources/*.cpp sources/editor/*.cpp sources/titleblock/*.cpp sources/htmleditor/*.cpp
HEADERS += sources/*.h sources/ui/*.h sources/editor/*.h sources/titleblock/*.h sources/richtext/*.h
SOURCES += sources/*.cpp sources/editor/*.cpp sources/titleblock/*.cpp sources/richtext/*.cpp
# Liste des fichiers qui seront incorpores au binaire en tant que ressources Qt
RESOURCES += qelectrotech.qrc sources/htmleditor/htmleditor.qrc
RESOURCES += qelectrotech.qrc
# Liste des ressources Windows
RC_FILE = ico/windows_icon/qelectrotech.rc
@@ -76,10 +76,10 @@ RC_FILE = ico/windows_icon/qelectrotech.rc
TRANSLATIONS += lang/qet_en.ts lang/qet_es.ts lang/qet_fr.ts lang/qet_ru.ts lang/qet_pt.ts lang/qet_cs.ts lang/qet_pl.ts lang/qet_de.ts lang/qet_ro.ts lang/qet_it.ts
# Modules Qt utilises par l'application
QT += xml svg network sql webkit
QT += xml svg network sql
# UI DESIGNER FILES AND GENERATION SOURCES FILES
FORMS = sources/htmleditor/htmleditor.ui sources/htmleditor/inserthtmldialog.ui
FORMS = sources/richtext/addlinkdialog.ui
UI_SOURCES_DIR = sources/ui/
UI_HEADERS_DIR = sources/ui/

View File

@@ -20,7 +20,8 @@
#include "qet.h"
#include "qetapp.h"
#include "htmleditor/htmleditor.h"
#include "richtext/richtexteditor_p.h"
/**
Constructeur
@param parent Le QGraphicsItem parent du champ de texte
@@ -313,13 +314,11 @@ void DiagramTextItem::setHtmlText(const QString &txt) {
*/
void DiagramTextItem::edit() {
//Open the HtmlEditor
HtmlEditor *editor = new HtmlEditor();
qdesigner_internal::RichTextEditorDialog *editor = new qdesigner_internal::RichTextEditorDialog();
// connect the in/out
connect(editor, SIGNAL(applyEditText(const QString &)), this, SLOT(setHtmlText(const QString &)));
// load the Html txt
editor->loadHtml( toHtml() );
// set the minimum controls
editor->setSimpleDisplay(true);
editor->setText( toHtml() );
// show
editor->show();
}

View File

@@ -38,7 +38,6 @@
#include "qetmessagebox.h"
#include "qtextorientationspinboxwidget.h"
#include "htmleditor/htmleditor.h"
/**
Constructeur

View File

@@ -1,269 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the Graphics Dojo project on Qt Labs.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 or 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file. Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
#include "highlighter.h"
#include <QtGui>
Highlighter::Highlighter(QTextDocument *document)
: QSyntaxHighlighter(document)
{
m_colors[DocType] = QColor(192, 192, 192);
m_colors[Entity] = QColor(128, 128, 128);
m_colors[Tag] = QColor(136, 18, 128);
m_colors[Comment] = QColor( 35, 110, 37);
m_colors[AttributeName] = QColor(153, 69, 0);
m_colors[AttributeValue] = QColor( 36, 36, 170);
}
void Highlighter::highlightBlock(const QString &text)
{
int state = previousBlockState();
int len = text.length();
int start = 0;
int pos = 0;
while (pos < len) {
switch (state) {
case State_Text:
default:
while (pos < len) {
QChar ch = text.at(pos);
if (ch == '<') {
if (text.mid(pos, 4) == "<!--") {
state = State_Comment;
} else {
if (text.mid(pos, 9).toUpper() == "<!DOCTYPE")
state = State_DocType;
else
state = State_TagStart;
}
break;
} else if (ch == '&') {
start = pos;
while (pos < len
&& text.at(pos++) != ';')
;
setFormat(start, pos - start, m_colors[Entity]);
} else {
++pos;
}
}
break;
case State_Comment:
start = pos;
while (pos < len) {
if (text.mid(pos, 3) == "-->") {
pos += 3;
state = State_Text;
break;
} else {
++pos;
}
}
setFormat(start, pos - start, m_colors[Comment]);
break;
case State_DocType:
start = pos;
while (pos < len) {
QChar ch = text.at(pos);
++pos;
if (ch == '>') {
state = State_Text;
break;
}
}
setFormat(start, pos - start, m_colors[DocType]);
break;
// at '<' in e.g. "<span>foo</span>"
case State_TagStart:
start = pos + 1;
while (pos < len) {
QChar ch = text.at(pos);
++pos;
if (ch == '>') {
state = State_Text;
break;
}
if (!ch.isSpace()) {
--pos;
state = State_TagName;
break;
}
}
break;
// at 'b' in e.g "<blockquote>foo</blockquote>"
case State_TagName:
start = pos;
while (pos < len) {
QChar ch = text.at(pos);
++pos;
if (ch.isSpace()) {
--pos;
state = State_InsideTag;
break;
}
if (ch == '>') {
state = State_Text;
break;
}
}
setFormat(start, pos - start, m_colors[Tag]);
break;
// anywhere after tag name and before tag closing ('>')
case State_InsideTag:
start = pos;
while (pos < len) {
QChar ch = text.at(pos);
++pos;
if (ch == '/')
continue;
if (ch == '>') {
state = State_Text;
break;
}
if (!ch.isSpace()) {
--pos;
state = State_AttributeName;
break;
}
}
break;
// at 's' in e.g. <img src=bla.png/>
case State_AttributeName:
start = pos;
while (pos < len) {
QChar ch = text.at(pos);
++pos;
if (ch == '=') {
state = State_AttributeValue;
break;
}
if (ch == '>' || ch == '/') {
state = State_InsideTag;
break;
}
}
setFormat(start, pos - start, m_colors[AttributeName]);
break;
// after '=' in e.g. <img src=bla.png/>
case State_AttributeValue:
start = pos;
// find first non-space character
while (pos < len) {
QChar ch = text.at(pos);
++pos;
// handle opening single quote
if (ch == '\'') {
state = State_SingleQuote;
break;
}
// handle opening double quote
if (ch == '"') {
state = State_DoubleQuote;
break;
}
if (!ch.isSpace())
break;
}
if (state == State_AttributeValue) {
// attribute value without quote
// just stop at non-space or tag delimiter
start = pos;
while (pos < len) {
QChar ch = text.at(pos);
if (ch.isSpace())
break;
if (ch == '>' || ch == '/')
break;
++pos;
}
state = State_InsideTag;
setFormat(start, pos - start, m_colors[AttributeValue]);
}
break;
// after the opening single quote in an attribute value
case State_SingleQuote:
start = pos;
while (pos < len) {
QChar ch = text.at(pos);
++pos;
if (ch == '\'')
break;
}
state = State_InsideTag;
setFormat(start, pos - start, m_colors[AttributeValue]);
break;
// after the opening double quote in an attribute value
case State_DoubleQuote:
start = pos;
while (pos < len) {
QChar ch = text.at(pos);
++pos;
if (ch == '"')
break;
}
state = State_InsideTag;
setFormat(start, pos - start, m_colors[AttributeValue]);
break;
}
}
setCurrentBlockState(state);
}

View File

@@ -1,70 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the Graphics Dojo project on Qt Labs.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 or 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file. Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
#ifndef HIGHLIGHTER_H
#define HIGHLIGHTER_H
#include <QHash>
#include <QSyntaxHighlighter>
// based on http://doc.trolltech.com/qq/qq21-syntaxhighlighter.html
class Highlighter : public QSyntaxHighlighter
{
Q_OBJECT
public:
Highlighter(QTextDocument *document);
enum Construct {
DocType,
Entity,
Tag,
Comment,
AttributeName,
AttributeValue
};
protected:
enum State {
State_Text = -1,
State_DocType,
State_Comment,
State_TagStart,
State_TagName,
State_InsideTag,
State_AttributeName,
State_SingleQuote,
State_DoubleQuote,
State_AttributeValue,
};
void highlightBlock(const QString &text);
private:
QHash<int, QColor> m_colors;
};
#endif // HIGHLIGHTER_H

View File

@@ -1,647 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the Graphics Dojo project on Qt Labs.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 or 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file. Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
/**
Integration : QElectroTech Team
Changelog:
- 26/03/2013 : Start integration...Compilation and object creation are successful
*/
#include "../ui/ui_htmleditor.h"
#include "../ui/ui_inserthtmldialog.h"
#include "htmleditor.h"
#include "highlighter.h"
#include <QtGui>
#include <QtWebKit>
#include <QFontDatabase>
#define FORWARD_ACTION(action1, action2) \
connect(action1, SIGNAL(triggered()), \
ui->webView->pageAction(action2), SLOT(trigger())); \
connect(ui->webView->pageAction(action2), \
SIGNAL(changed()), SLOT(adjustActions()));
HtmlEditor::HtmlEditor(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui_MainWindow)
, sourceDirty(true)
, highlighter(0)
, ui_dialog(0)
, insertHtmlDialog(0)
{
ui->setupUi(this);
ui->tabWidget->setTabText(0, "Normal View");
ui->tabWidget->setTabText(1, "HTML Source");
connect(ui->tabWidget, SIGNAL(currentChanged(int)), SLOT(changeTab(int)));
resize(600, 300);
highlighter = new Highlighter(ui->plainTextEdit->document());
QWidget *spacer = new QWidget(this);
spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
ui->standardToolBar->insertWidget(ui->actionZoomOut, spacer);
zoomLabel = new QLabel;
ui->standardToolBar->insertWidget(ui->actionZoomOut, zoomLabel);
zoomSlider = new QSlider(this);
zoomSlider->setOrientation(Qt::Horizontal);
zoomSlider->setMaximumWidth(150);
zoomSlider->setRange(25, 400);
zoomSlider->setSingleStep(25);
zoomSlider->setPageStep(100);
connect(zoomSlider, SIGNAL(valueChanged(int)), SLOT(changeZoom(int)));
ui->standardToolBar->insertWidget(ui->actionZoomIn, zoomSlider);
connect(ui->actionFileNew, SIGNAL(triggered()), SLOT(fileNew()));
connect(ui->actionFileOpen, SIGNAL(triggered()), SLOT(fileOpen()));
connect(ui->actionFileSave, SIGNAL(triggered()), SLOT(fileSave()));
connect(ui->actionFileSaveAs, SIGNAL(triggered()), SLOT(fileSaveAs()));
connect(ui->actionExit, SIGNAL(triggered()), SLOT(close()));
connect(ui->actionInsertImage, SIGNAL(triggered()), SLOT(insertImage()));
connect(ui->actionCreateLink, SIGNAL(triggered()), SLOT(createLink()));
connect(ui->actionInsertHtml, SIGNAL(triggered()), SLOT(insertHtml()));
connect(ui->actionZoomOut, SIGNAL(triggered()), SLOT(zoomOut()));
connect(ui->actionZoomIn, SIGNAL(triggered()), SLOT(zoomIn()));
// these are forward to internal QWebView
FORWARD_ACTION(ui->actionEditUndo, QWebPage::Undo);
FORWARD_ACTION(ui->actionEditRedo, QWebPage::Redo);
FORWARD_ACTION(ui->actionEditCut, QWebPage::Cut);
FORWARD_ACTION(ui->actionEditCopy, QWebPage::Copy);
FORWARD_ACTION(ui->actionEditPaste, QWebPage::Paste);
FORWARD_ACTION(ui->actionFormatBold, QWebPage::ToggleBold);
FORWARD_ACTION(ui->actionFormatItalic, QWebPage::ToggleItalic);
FORWARD_ACTION(ui->actionFormatUnderline, QWebPage::ToggleUnderline);
// Qt 4.5.0 has a bug: always returns 0 for QWebPage::SelectAll
connect(ui->actionEditSelectAll, SIGNAL(triggered()), SLOT(editSelectAll()));
connect(ui->actionStyleParagraph, SIGNAL(triggered()), SLOT(styleParagraph()));
connect(ui->actionStyleHeading1, SIGNAL(triggered()), SLOT(styleHeading1()));
connect(ui->actionStyleHeading2, SIGNAL(triggered()), SLOT(styleHeading2()));
connect(ui->actionStyleHeading3, SIGNAL(triggered()), SLOT(styleHeading3()));
connect(ui->actionStyleHeading4, SIGNAL(triggered()), SLOT(styleHeading4()));
connect(ui->actionStyleHeading5, SIGNAL(triggered()), SLOT(styleHeading5()));
connect(ui->actionStyleHeading6, SIGNAL(triggered()), SLOT(styleHeading6()));
connect(ui->actionStylePreformatted, SIGNAL(triggered()), SLOT(stylePreformatted()));
connect(ui->actionStyleAddress, SIGNAL(triggered()), SLOT(styleAddress()));
connect(ui->actionFormatFontName, SIGNAL(triggered()), SLOT(formatFontName()));
connect(ui->actionFormatFontSize, SIGNAL(triggered()), SLOT(formatFontSize()));
connect(ui->actionFormatTextColor, SIGNAL(triggered()), SLOT(formatTextColor()));
connect(ui->actionFormatBackgroundColor, SIGNAL(triggered()), SLOT(formatBackgroundColor()));
// no page action exists yet for these, so use execCommand trick
connect(ui->actionFormatStrikethrough, SIGNAL(triggered()), SLOT(formatStrikeThrough()));
connect(ui->actionFormatAlignLeft, SIGNAL(triggered()), SLOT(formatAlignLeft()));
connect(ui->actionFormatAlignCenter, SIGNAL(triggered()), SLOT(formatAlignCenter()));
connect(ui->actionFormatAlignRight, SIGNAL(triggered()), SLOT(formatAlignRight()));
connect(ui->actionFormatAlignJustify, SIGNAL(triggered()), SLOT(formatAlignJustify()));
connect(ui->actionFormatDecreaseIndent, SIGNAL(triggered()), SLOT(formatDecreaseIndent()));
connect(ui->actionFormatIncreaseIndent, SIGNAL(triggered()), SLOT(formatIncreaseIndent()));
connect(ui->actionFormatNumberedList, SIGNAL(triggered()), SLOT(formatNumberedList()));
connect(ui->actionFormatBulletedList, SIGNAL(triggered()), SLOT(formatBulletedList()));
// necessary to sync our actions
connect(ui->webView->page(), SIGNAL(selectionChanged()), SLOT(adjustActions()));
connect(ui->webView->page(), SIGNAL(contentsChanged()), SLOT(adjustSource()));
ui->webView->setFocus();
setCurrentFileName(QString());
QString initialFile = ":/example.html";
const QStringList args = QCoreApplication::arguments();
if (args.count() == 2)
initialFile = args.at(1);
if (!load(initialFile))
fileNew();
adjustActions();
adjustSource();
setWindowModified(false);
changeZoom(100);
}
HtmlEditor::~HtmlEditor()
{
delete ui;
delete ui_dialog;
}
bool HtmlEditor::maybeSave()
{
if (!isWindowModified())
return true;
QMessageBox::StandardButton ret;
ret = QMessageBox::warning(this, tr("HTML Editor"),
tr("The document has been modified.\n"
"Do you want to save your changes?"),
QMessageBox::Save | QMessageBox::Discard
| QMessageBox::Cancel);
if (ret == QMessageBox::Save)
return fileSave();
else if (ret == QMessageBox::Cancel)
return false;
return true;
}
void HtmlEditor::fileNew()
{
if (maybeSave()) {
ui->webView->setHtml("<p></p>");
ui->webView->setFocus();
ui->webView->page()->setContentEditable(true);
setCurrentFileName(QString());
setWindowModified(false);
// quirk in QWebView: need an initial mouse click to show the cursor
int mx = ui->webView->width() / 2;
int my = ui->webView->height() / 2;
QPoint center = QPoint(mx, my);
QMouseEvent *e1 = new QMouseEvent(QEvent::MouseButtonPress, center,
Qt::LeftButton, Qt::LeftButton,
Qt::NoModifier);
QMouseEvent *e2 = new QMouseEvent(QEvent::MouseButtonRelease, center,
Qt::LeftButton, Qt::LeftButton,
Qt::NoModifier);
QApplication::postEvent(ui->webView, e1);
QApplication::postEvent(ui->webView, e2);
}
}
void HtmlEditor::fileOpen()
{
QString fn = QFileDialog::getOpenFileName(this, tr("Open File..."),
QString(), tr("HTML-Files (*.htm *.html);;All Files (*)"));
if (!fn.isEmpty())
load(fn);
}
bool HtmlEditor::fileSave()
{
if (fileName.isEmpty() || fileName.startsWith(QLatin1String(":/")))
return fileSaveAs();
QFile file(fileName);
bool success = file.open(QIODevice::WriteOnly);
if (success) {
// FIXME: here we always use UTF-8 encoding
QString content = ui->webView->page()->mainFrame()->toHtml();
QByteArray data = content.toUtf8();
qint64 c = file.write(data);
success = (c >= data.length());
}
setWindowModified(false);
return success;
}
bool HtmlEditor::fileSaveAs()
{
QString fn = QFileDialog::getSaveFileName(this, tr("Save as..."),
QString(), tr("HTML-Files (*.htm *.html);;All Files (*)"));
if (fn.isEmpty())
return false;
if (!(fn.endsWith(".htm", Qt::CaseInsensitive) || fn.endsWith(".html", Qt::CaseInsensitive)))
fn += ".htm"; // default
setCurrentFileName(fn);
return fileSave();
}
void HtmlEditor::insertImage()
{
QString filters;
filters += tr("Common Graphics (*.png *.jpg *.jpeg *.gif);;");
filters += tr("Portable Network Graphics (PNG) (*.png);;");
filters += tr("JPEG (*.jpg *.jpeg);;");
filters += tr("Graphics Interchange Format (*.gif);;");
filters += tr("All Files (*)");
QString fn = QFileDialog::getOpenFileName(this, tr("Open image..."),
QString(), filters);
if (fn.isEmpty())
return;
if (!QFile::exists(fn))
return;
QUrl url = QUrl::fromLocalFile(fn);
execCommand("insertImage", url.toString());
}
// shamelessly copied from Qt Demo Browser
static QUrl guessUrlFromString(const QString &string)
{
QString urlStr = string.trimmed();
QRegExp test(QLatin1String("^[a-zA-Z]+\\:.*"));
// Check if it looks like a qualified URL. Try parsing it and see.
bool hasSchema = test.exactMatch(urlStr);
if (hasSchema) {
QUrl url(urlStr, QUrl::TolerantMode);
if (url.isValid())
return url;
}
// Might be a file.
if (QFile::exists(urlStr))
return QUrl::fromLocalFile(urlStr);
// Might be a shorturl - try to detect the schema.
if (!hasSchema) {
int dotIndex = urlStr.indexOf(QLatin1Char('.'));
if (dotIndex != -1) {
QString prefix = urlStr.left(dotIndex).toLower();
QString schema = (prefix == QLatin1String("ftp")) ? prefix : QLatin1String("http");
QUrl url(schema + QLatin1String("://") + urlStr, QUrl::TolerantMode);
if (url.isValid())
return url;
}
}
// Fall back to QUrl's own tolerant parser.
return QUrl(string, QUrl::TolerantMode);
}
void HtmlEditor::createLink()
{
QString link = QInputDialog::getText(this, tr("Create link"),
"Enter URL");
if (!link.isEmpty()) {
QUrl url = guessUrlFromString(link);
if (url.isValid())
execCommand("createLink", url.toString());
}
}
void HtmlEditor::insertHtml()
{
if (!insertHtmlDialog) {
insertHtmlDialog = new QDialog(this);
if (!ui_dialog)
ui_dialog = new Ui_Dialog;
ui_dialog->setupUi(insertHtmlDialog);
connect(ui_dialog->buttonBox, SIGNAL(accepted()),
insertHtmlDialog, SLOT(accept()));
connect(ui_dialog->buttonBox, SIGNAL(rejected()),
insertHtmlDialog, SLOT(reject()));
}
ui_dialog->plainTextEdit->clear();
ui_dialog->plainTextEdit->setFocus();
Highlighter *hilite = new Highlighter(ui_dialog->plainTextEdit->document());
if (insertHtmlDialog->exec() == QDialog::Accepted)
execCommand("insertHTML", ui_dialog->plainTextEdit->toPlainText());
delete hilite;
}
void HtmlEditor::zoomOut()
{
int percent = static_cast<int>(ui->webView->zoomFactor() * 100);
if (percent > 25) {
percent -= 25;
percent = 25 * (int((percent + 25 - 1) / 25));
qreal factor = static_cast<qreal>(percent) / 100;
ui->webView->setZoomFactor(factor);
ui->actionZoomOut->setEnabled(percent > 25);
ui->actionZoomIn->setEnabled(true);
zoomSlider->setValue(percent);
}
}
void HtmlEditor::zoomIn()
{
int percent = static_cast<int>(ui->webView->zoomFactor() * 100);
if (percent < 400) {
percent += 25;
percent = 25 * (int(percent / 25));
qreal factor = static_cast<qreal>(percent) / 100;
ui->webView->setZoomFactor(factor);
ui->actionZoomIn->setEnabled(percent < 400);
ui->actionZoomOut->setEnabled(true);
zoomSlider->setValue(percent);
}
}
void HtmlEditor::editSelectAll()
{
ui->webView->triggerPageAction(QWebPage::SelectAll);
}
void HtmlEditor::execCommand(const QString &cmd)
{
QWebFrame *frame = ui->webView->page()->mainFrame();
QString js = QString("document.execCommand(\"%1\", false, null)").arg(cmd);
frame->evaluateJavaScript(js);
}
void HtmlEditor::execCommand(const QString &cmd, const QString &arg)
{
QWebFrame *frame = ui->webView->page()->mainFrame();
QString js = QString("document.execCommand(\"%1\", false, \"%2\")").arg(cmd).arg(arg);
frame->evaluateJavaScript(js);
}
bool HtmlEditor::queryCommandState(const QString &cmd)
{
QWebFrame *frame = ui->webView->page()->mainFrame();
QString js = QString("document.queryCommandState(\"%1\", false, null)").arg(cmd);
QVariant result = frame->evaluateJavaScript(js);
return result.toString().simplified().toLower() == "true";
}
void HtmlEditor::styleParagraph()
{
execCommand("formatBlock", "p");
}
void HtmlEditor::styleHeading1()
{
execCommand("formatBlock", "h1");
}
void HtmlEditor::styleHeading2()
{
execCommand("formatBlock", "h2");
}
void HtmlEditor::styleHeading3()
{
execCommand("formatBlock", "h3");
}
void HtmlEditor::styleHeading4()
{
execCommand("formatBlock", "h4");
}
void HtmlEditor::styleHeading5()
{
execCommand("formatBlock", "h5");
}
void HtmlEditor::styleHeading6()
{
execCommand("formatBlock", "h6");
}
void HtmlEditor::stylePreformatted()
{
execCommand("formatBlock", "pre");
}
void HtmlEditor::styleAddress()
{
execCommand("formatBlock", "address");
}
void HtmlEditor::formatStrikeThrough()
{
execCommand("strikeThrough");
}
void HtmlEditor::formatAlignLeft()
{
execCommand("justifyLeft");
}
void HtmlEditor::formatAlignCenter()
{
execCommand("justifyCenter");
}
void HtmlEditor::formatAlignRight()
{
execCommand("justifyRight");
}
void HtmlEditor::formatAlignJustify()
{
execCommand("justifyFull");
}
void HtmlEditor::formatIncreaseIndent()
{
execCommand("indent");
}
void HtmlEditor::formatDecreaseIndent()
{
execCommand("outdent");
}
void HtmlEditor::formatNumberedList()
{
execCommand("insertOrderedList");
}
void HtmlEditor::formatBulletedList()
{
execCommand("insertUnorderedList");
}
void HtmlEditor::formatFontName()
{
QStringList families = QFontDatabase().families();
bool ok = false;
QString family = QInputDialog::getItem(this, tr("Font"), tr("Select font:"),
families, 0, false, &ok);
if (ok)
execCommand("fontName", family);
}
void HtmlEditor::formatFontSize()
{
QStringList sizes;
sizes << "xx-small";
sizes << "x-small";
sizes << "small";
sizes << "medium";
sizes << "large";
sizes << "x-large";
sizes << "xx-large";
bool ok = false;
QString size = QInputDialog::getItem(this, tr("Font Size"), tr("Select font size:"),
sizes, sizes.indexOf("medium"), false, &ok);
if (ok)
execCommand("fontSize", QString::number(sizes.indexOf(size)));
}
void HtmlEditor::formatTextColor()
{
QColor color = QColorDialog::getColor(Qt::black, this);
if (color.isValid())
execCommand("foreColor", color.name());
}
void HtmlEditor::formatBackgroundColor()
{
QColor color = QColorDialog::getColor(Qt::white, this);
if (color.isValid())
execCommand("hiliteColor", color.name());
}
#define FOLLOW_ENABLE(a1, a2) a1->setEnabled(ui->webView->pageAction(a2)->isEnabled())
#define FOLLOW_CHECK(a1, a2) a1->setChecked(ui->webView->pageAction(a2)->isChecked())
void HtmlEditor::adjustActions()
{
FOLLOW_ENABLE(ui->actionEditUndo, QWebPage::Undo);
FOLLOW_ENABLE(ui->actionEditRedo, QWebPage::Redo);
FOLLOW_ENABLE(ui->actionEditCut, QWebPage::Cut);
FOLLOW_ENABLE(ui->actionEditCopy, QWebPage::Copy);
FOLLOW_ENABLE(ui->actionEditPaste, QWebPage::Paste);
FOLLOW_CHECK(ui->actionFormatBold, QWebPage::ToggleBold);
FOLLOW_CHECK(ui->actionFormatItalic, QWebPage::ToggleItalic);
FOLLOW_CHECK(ui->actionFormatUnderline, QWebPage::ToggleUnderline);
ui->actionFormatStrikethrough->setChecked(queryCommandState("strikeThrough"));
ui->actionFormatNumberedList->setChecked(queryCommandState("insertOrderedList"));
ui->actionFormatBulletedList->setChecked(queryCommandState("insertUnorderedList"));
}
void HtmlEditor::adjustSource()
{
setWindowModified(true);
sourceDirty = true;
if (ui->tabWidget->currentIndex() == 1)
changeTab(1);
}
void HtmlEditor::changeTab(int index)
{
if (sourceDirty && (index == 1)) {
QString content = ui->webView->page()->mainFrame()->toHtml();
ui->plainTextEdit->setPlainText(content);
sourceDirty = false;
}
}
void HtmlEditor::openLink(const QUrl &url)
{
QString msg = QString(tr("Open %1 ?")).arg(url.toString());
if (QMessageBox::question(this, tr("Open link"), msg,
QMessageBox::Open | QMessageBox::Cancel) ==
QMessageBox::Open)
QDesktopServices::openUrl(url);
}
void HtmlEditor::changeZoom(int percent)
{
ui->actionZoomOut->setEnabled(percent > 25);
ui->actionZoomIn->setEnabled(percent < 400);
qreal factor = static_cast<qreal>(percent) / 100;
ui->webView->setZoomFactor(factor);
zoomLabel->setText(tr(" Zoom: %1% ").arg(percent));
zoomSlider->setValue(percent);
}
/**
* @brief HtmlEditor::loadHtml to the editor
* @param f
*/
void HtmlEditor::loadHtml(const QString &f) {
ui->webView->setContent(f.toAscii(), "text/html");
ui->webView->page()->setContentEditable(true);
ui->webView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
connect(ui->webView, SIGNAL(linkClicked(QUrl)), SLOT(openLink(QUrl)));
}
/**
* @brief HtmlEditor::setSimpleDisplay
* @param state
*/
void HtmlEditor::setSimpleDisplay(bool state) {
ui->standardToolBar->setVisible(!state);
ui->menubar->setVisible(!state);
}
bool HtmlEditor::load(const QString &f)
{
if (!QFile::exists(f))
return false;
QFile file(f);
if (!file.open(QFile::ReadOnly))
return false;
QByteArray data = file.readAll();
ui->webView->setContent(data, "text/html");
ui->webView->page()->setContentEditable(true);
ui->webView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
connect(ui->webView, SIGNAL(linkClicked(QUrl)), SLOT(openLink(QUrl)));
setCurrentFileName(f);
return true;
}
void HtmlEditor::setCurrentFileName(const QString &fileName)
{
this->fileName = fileName;
// setWindowModified(false);
QString shownName;
if (fileName.isEmpty())
shownName = "untitled";
else
shownName = QFileInfo(fileName).fileName();
setWindowTitle(tr("%1[*] - %2").arg("QET").arg(tr("HTML Editor")));
setWindowModified(false);
bool allowSave = true;
if (fileName.isEmpty() || fileName.startsWith(QLatin1String(":/")))
allowSave = false;
ui->actionFileSave->setEnabled(allowSave);
}
/**
* @brief HtmlEditor::on_buttonBox_accepted
*/
void HtmlEditor::on_buttonBox_accepted() {
emit applyEditText( ui->webView->page()->mainFrame()->toHtml() );
this->close();
}
/**
* @brief HtmlEditor::on_buttonBox_rejected
*/
void HtmlEditor::on_buttonBox_rejected() {
this->close();
}

View File

@@ -1,121 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the Graphics Dojo project on Qt Labs.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 or 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file. Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
#ifndef HTML_EDITOR_H
#define HTML_EDITOR_H
#include <QMainWindow>
#include "highlighter.h"
#if QT_VERSION < 0x040500
#error You must use Qt >= 4.5.0!
#endif
class Ui_MainWindow;
class Ui_Dialog;
class QLabel;
class QSlider;
class QUrl;
class HtmlEditor : public QMainWindow
{
Q_OBJECT
public:
HtmlEditor(QWidget *parent = 0);
~HtmlEditor();
void loadHtml(const QString &f);
void setSimpleDisplay(bool state);
private:
void setupActions();
void setupToolBar();
void setupMenu();
bool load(const QString &f);
bool maybeSave();
void setCurrentFileName(const QString &fileName);
void execCommand(const QString&);
void execCommand(const QString &cmd, const QString &arg);
bool queryCommandState(const QString&);
private slots:
void fileNew();
void fileOpen();
bool fileSave();
bool fileSaveAs();
void editSelectAll();
void styleParagraph();
void styleHeading1();
void styleHeading2();
void styleHeading3();
void styleHeading4();
void styleHeading5();
void styleHeading6();
void stylePreformatted();
void styleAddress();
void formatStrikeThrough();
void formatAlignLeft();
void formatAlignCenter();
void formatAlignRight();
void formatAlignJustify();
void formatIncreaseIndent();
void formatDecreaseIndent();
void formatNumberedList();
void formatBulletedList();
void formatFontName();
void formatFontSize();
void formatTextColor();
void formatBackgroundColor();
void insertImage();
void createLink();
void insertHtml();
void zoomOut();
void zoomIn();
void adjustActions();
void adjustSource();
void changeTab(int);
void openLink(const QUrl&);
void changeZoom(int);
signals:
void applyEditText(const QString &);
private:
Ui_MainWindow *ui;
QString fileName;
bool sourceDirty;
QLabel *zoomLabel;
QSlider *zoomSlider;
Highlighter *highlighter;
Ui_Dialog *ui_dialog;
QDialog *insertHtmlDialog;
private slots:
void on_buttonBox_accepted();
void on_buttonBox_rejected();
};
#endif // HTML_EDITOR_H

View File

@@ -1,33 +0,0 @@
<RCC>
<qresource prefix="/">
<file>images/document-new.png</file>
<file>images/document-open.png</file>
<file>images/document-save.png</file>
<file>images/edit-copy.png</file>
<file>images/edit-cut.png</file>
<file>images/edit-paste.png</file>
<file>images/edit-redo.png</file>
<file>images/edit-select-all.png</file>
<file>images/edit-undo.png</file>
<file>images/format-indent-less.png</file>
<file>images/format-indent-more.png</file>
<file>images/format-justify-center.png</file>
<file>images/format-justify-fill.png</file>
<file>images/format-justify-left.png</file>
<file>images/format-justify-right.png</file>
<file>images/format-text-bold.png</file>
<file>images/format-text-italic.png</file>
<file>images/format-text-underline.png</file>
<file>images/format-text-strikethrough.png</file>
<file>images/bulleted-list.png</file>
<file>images/numbered-list.png</file>
<file>images/image-x-generic.png</file>
<file>qtlogo.png</file>
<file>images/text-html.png</file>
<file>images/list-remove.png</file>
<file>images/list-add.png</file>
<file>images/insert-html.png</file>
<file>images/text.png</file>
<file>images/text_color.png</file>
</qresource>
</RCC>

View File

@@ -1,640 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>562</width>
<height>361</height>
</rect>
</property>
<property name="windowTitle">
<string>HTML Editor</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="tabPosition">
<enum>QTabWidget::South</enum>
</property>
<property name="tabShape">
<enum>QTabWidget::Rounded</enum>
</property>
<property name="currentIndex">
<number>0</number>
</property>
<property name="documentMode">
<bool>true</bool>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>Tab 1</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QWebView" name="webView">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="url">
<url>
<string>about:blank</string>
</url>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>Tab 2</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_3">
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="QPlainTextEdit" name="plainTextEdit">
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>562</width>
<height>31</height>
</rect>
</property>
<widget class="QMenu" name="menu_File">
<property name="title">
<string>&amp;File</string>
</property>
<addaction name="actionFileNew"/>
<addaction name="actionFileOpen"/>
<addaction name="actionFileSave"/>
<addaction name="actionFileSaveAs"/>
<addaction name="separator"/>
<addaction name="actionExit"/>
</widget>
<widget class="QMenu" name="menu_Edit">
<property name="title">
<string>&amp;Edit</string>
</property>
<addaction name="actionEditUndo"/>
<addaction name="actionEditRedo"/>
<addaction name="separator"/>
<addaction name="actionEditCut"/>
<addaction name="actionEditCopy"/>
<addaction name="actionEditPaste"/>
<addaction name="separator"/>
<addaction name="actionEditSelectAll"/>
<addaction name="separator"/>
<addaction name="actionInsertImage"/>
<addaction name="actionCreateLink"/>
<addaction name="actionInsertHtml"/>
</widget>
<widget class="QMenu" name="menuF_ormat">
<property name="title">
<string>F&amp;ormat</string>
</property>
<widget class="QMenu" name="menuSt_yle">
<property name="title">
<string>St&amp;yle</string>
</property>
<addaction name="actionStyleParagraph"/>
<addaction name="actionStyleHeading1"/>
<addaction name="actionStyleHeading2"/>
<addaction name="actionStyleHeading3"/>
<addaction name="actionStyleHeading4"/>
<addaction name="actionStyleHeading5"/>
<addaction name="actionStyleHeading6"/>
<addaction name="actionStylePreformatted"/>
<addaction name="actionStyleAddress"/>
</widget>
<widget class="QMenu" name="menu_Align">
<property name="title">
<string>&amp;Align</string>
</property>
<addaction name="actionFormatAlignLeft"/>
<addaction name="actionFormatAlignCenter"/>
<addaction name="actionFormatAlignRight"/>
<addaction name="actionFormatAlignJustify"/>
</widget>
<addaction name="menuSt_yle"/>
<addaction name="menu_Align"/>
<addaction name="separator"/>
<addaction name="actionFormatBold"/>
<addaction name="actionFormatItalic"/>
<addaction name="actionFormatUnderline"/>
<addaction name="actionFormatStrikethrough"/>
<addaction name="separator"/>
<addaction name="actionFormatIncreaseIndent"/>
<addaction name="actionFormatDecreaseIndent"/>
<addaction name="separator"/>
<addaction name="actionFormatNumberedList"/>
<addaction name="actionFormatBulletedList"/>
<addaction name="separator"/>
<addaction name="actionFormatFontName"/>
<addaction name="actionFormatFontSize"/>
<addaction name="separator"/>
<addaction name="actionFormatTextColor"/>
<addaction name="actionFormatBackgroundColor"/>
</widget>
<addaction name="menu_File"/>
<addaction name="menu_Edit"/>
<addaction name="menuF_ormat"/>
</widget>
<widget class="QToolBar" name="standardToolBar">
<property name="enabled">
<bool>false</bool>
</property>
<property name="windowTitle">
<string>Standard</string>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
<addaction name="actionFileNew"/>
<addaction name="actionFileOpen"/>
<addaction name="actionFileSave"/>
<addaction name="separator"/>
<addaction name="actionEditUndo"/>
<addaction name="actionEditRedo"/>
<addaction name="separator"/>
<addaction name="actionEditCut"/>
<addaction name="actionEditCopy"/>
<addaction name="actionEditPaste"/>
<addaction name="separator"/>
<addaction name="actionZoomOut"/>
<addaction name="actionZoomIn"/>
</widget>
<widget class="QToolBar" name="formatToolBar">
<property name="windowTitle">
<string>Formatting</string>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>true</bool>
</attribute>
<addaction name="actionFormatBold"/>
<addaction name="actionFormatItalic"/>
<addaction name="actionFormatUnderline"/>
<addaction name="actionFormatStrikethrough"/>
<addaction name="actionFormatFontSize"/>
<addaction name="actionFormatTextColor"/>
<addaction name="separator"/>
<addaction name="actionFormatAlignLeft"/>
<addaction name="actionFormatAlignCenter"/>
<addaction name="actionFormatAlignRight"/>
<addaction name="actionFormatAlignJustify"/>
<addaction name="separator"/>
<addaction name="actionFormatDecreaseIndent"/>
<addaction name="actionFormatIncreaseIndent"/>
<addaction name="separator"/>
<addaction name="actionFormatNumberedList"/>
<addaction name="actionFormatBulletedList"/>
<addaction name="separator"/>
<addaction name="actionInsertImage"/>
<addaction name="actionCreateLink"/>
<addaction name="actionInsertHtml"/>
</widget>
<action name="actionFileNew">
<property name="icon">
<iconset resource="htmleditor.qrc">
<normaloff>:/images/document-new.png</normaloff>:/images/document-new.png</iconset>
</property>
<property name="text">
<string>&amp;New</string>
</property>
<property name="shortcut">
<string>Ctrl+N</string>
</property>
</action>
<action name="actionFileOpen">
<property name="icon">
<iconset resource="htmleditor.qrc">
<normaloff>:/images/document-open.png</normaloff>:/images/document-open.png</iconset>
</property>
<property name="text">
<string>&amp;Open...</string>
</property>
<property name="shortcut">
<string>Ctrl+O</string>
</property>
</action>
<action name="actionFileSave">
<property name="icon">
<iconset resource="htmleditor.qrc">
<normaloff>:/images/document-save.png</normaloff>:/images/document-save.png</iconset>
</property>
<property name="text">
<string>&amp;Save</string>
</property>
<property name="shortcut">
<string>Ctrl+S</string>
</property>
</action>
<action name="actionFileSaveAs">
<property name="text">
<string>Save &amp;As...</string>
</property>
</action>
<action name="actionEditUndo">
<property name="icon">
<iconset resource="htmleditor.qrc">
<normaloff>:/images/edit-undo.png</normaloff>:/images/edit-undo.png</iconset>
</property>
<property name="text">
<string>&amp;Undo</string>
</property>
<property name="shortcut">
<string>Ctrl+Z</string>
</property>
</action>
<action name="actionEditRedo">
<property name="icon">
<iconset resource="htmleditor.qrc">
<normaloff>:/images/edit-redo.png</normaloff>:/images/edit-redo.png</iconset>
</property>
<property name="text">
<string>&amp;Redo</string>
</property>
<property name="shortcut">
<string>Ctrl+Y</string>
</property>
</action>
<action name="actionEditCut">
<property name="icon">
<iconset resource="htmleditor.qrc">
<normaloff>:/images/edit-cut.png</normaloff>:/images/edit-cut.png</iconset>
</property>
<property name="text">
<string>Cu&amp;t</string>
</property>
<property name="shortcut">
<string>Ctrl+X</string>
</property>
</action>
<action name="actionEditCopy">
<property name="icon">
<iconset resource="htmleditor.qrc">
<normaloff>:/images/edit-copy.png</normaloff>:/images/edit-copy.png</iconset>
</property>
<property name="text">
<string>&amp;Copy</string>
</property>
<property name="shortcut">
<string>Ctrl+C</string>
</property>
</action>
<action name="actionEditPaste">
<property name="icon">
<iconset resource="htmleditor.qrc">
<normaloff>:/images/edit-paste.png</normaloff>:/images/edit-paste.png</iconset>
</property>
<property name="text">
<string>&amp;Paste</string>
</property>
<property name="shortcut">
<string>Ctrl+V</string>
</property>
</action>
<action name="actionEditSelectAll">
<property name="icon">
<iconset resource="htmleditor.qrc">
<normaloff>:/images/edit-select-all.png</normaloff>:/images/edit-select-all.png</iconset>
</property>
<property name="text">
<string>Select A&amp;ll</string>
</property>
<property name="shortcut">
<string>Ctrl+A</string>
</property>
</action>
<action name="actionFormatBold">
<property name="checkable">
<bool>true</bool>
</property>
<property name="icon">
<iconset resource="htmleditor.qrc">
<normaloff>:/images/format-text-bold.png</normaloff>:/images/format-text-bold.png</iconset>
</property>
<property name="text">
<string>&amp;Bold</string>
</property>
<property name="shortcut">
<string>Ctrl+B</string>
</property>
</action>
<action name="actionFormatItalic">
<property name="checkable">
<bool>true</bool>
</property>
<property name="icon">
<iconset resource="htmleditor.qrc">
<normaloff>:/images/format-text-italic.png</normaloff>:/images/format-text-italic.png</iconset>
</property>
<property name="text">
<string>&amp;Italic</string>
</property>
<property name="shortcut">
<string>Ctrl+I</string>
</property>
</action>
<action name="actionFormatUnderline">
<property name="checkable">
<bool>true</bool>
</property>
<property name="icon">
<iconset resource="htmleditor.qrc">
<normaloff>:/images/format-text-underline.png</normaloff>:/images/format-text-underline.png</iconset>
</property>
<property name="text">
<string>&amp;Underline</string>
</property>
<property name="shortcut">
<string>Ctrl+U</string>
</property>
</action>
<action name="actionFormatStrikethrough">
<property name="checkable">
<bool>true</bool>
</property>
<property name="icon">
<iconset resource="htmleditor.qrc">
<normaloff>:/images/format-text-strikethrough.png</normaloff>:/images/format-text-strikethrough.png</iconset>
</property>
<property name="text">
<string>&amp;Strikethrough</string>
</property>
</action>
<action name="actionFormatAlignLeft">
<property name="icon">
<iconset resource="htmleditor.qrc">
<normaloff>:/images/format-justify-left.png</normaloff>:/images/format-justify-left.png</iconset>
</property>
<property name="text">
<string>Align &amp;Left</string>
</property>
</action>
<action name="actionFormatAlignCenter">
<property name="icon">
<iconset resource="htmleditor.qrc">
<normaloff>:/images/format-justify-center.png</normaloff>:/images/format-justify-center.png</iconset>
</property>
<property name="text">
<string>Align &amp;Center</string>
</property>
</action>
<action name="actionFormatAlignRight">
<property name="icon">
<iconset resource="htmleditor.qrc">
<normaloff>:/images/format-justify-right.png</normaloff>:/images/format-justify-right.png</iconset>
</property>
<property name="text">
<string>Align &amp;Right</string>
</property>
</action>
<action name="actionFormatAlignJustify">
<property name="icon">
<iconset resource="htmleditor.qrc">
<normaloff>:/images/format-justify-fill.png</normaloff>:/images/format-justify-fill.png</iconset>
</property>
<property name="text">
<string>Align &amp;Justify</string>
</property>
</action>
<action name="actionFormatIncreaseIndent">
<property name="icon">
<iconset resource="htmleditor.qrc">
<normaloff>:/images/format-indent-more.png</normaloff>:/images/format-indent-more.png</iconset>
</property>
<property name="text">
<string>I&amp;ncrease Indent</string>
</property>
</action>
<action name="actionFormatDecreaseIndent">
<property name="icon">
<iconset resource="htmleditor.qrc">
<normaloff>:/images/format-indent-less.png</normaloff>:/images/format-indent-less.png</iconset>
</property>
<property name="text">
<string>&amp;Decrease Indent</string>
</property>
</action>
<action name="actionFormatBulletedList">
<property name="checkable">
<bool>true</bool>
</property>
<property name="icon">
<iconset resource="htmleditor.qrc">
<normaloff>:/images/bulleted-list.png</normaloff>:/images/bulleted-list.png</iconset>
</property>
<property name="text">
<string>Bulle&amp;ted List</string>
</property>
</action>
<action name="actionFormatNumberedList">
<property name="checkable">
<bool>true</bool>
</property>
<property name="icon">
<iconset resource="htmleditor.qrc">
<normaloff>:/images/numbered-list.png</normaloff>:/images/numbered-list.png</iconset>
</property>
<property name="text">
<string>&amp;Numbered List</string>
</property>
</action>
<action name="actionInsertImage">
<property name="icon">
<iconset resource="htmleditor.qrc">
<normaloff>:/images/image-x-generic.png</normaloff>:/images/image-x-generic.png</iconset>
</property>
<property name="text">
<string>Insert &amp;Image...</string>
</property>
<property name="visible">
<bool>false</bool>
</property>
</action>
<action name="actionCreateLink">
<property name="icon">
<iconset resource="htmleditor.qrc">
<normaloff>:/images/text-html.png</normaloff>:/images/text-html.png</iconset>
</property>
<property name="text">
<string>Create Link...</string>
</property>
</action>
<action name="actionZoomOut">
<property name="icon">
<iconset resource="htmleditor.qrc">
<normaloff>:/images/list-remove.png</normaloff>:/images/list-remove.png</iconset>
</property>
<property name="text">
<string>Zoom Out</string>
</property>
</action>
<action name="actionZoomIn">
<property name="icon">
<iconset resource="htmleditor.qrc">
<normaloff>:/images/list-add.png</normaloff>:/images/list-add.png</iconset>
</property>
<property name="text">
<string>Zoom In</string>
</property>
</action>
<action name="actionExit">
<property name="text">
<string>E&amp;xit</string>
</property>
<property name="shortcut">
<string>Ctrl+Q</string>
</property>
</action>
<action name="actionStyleParagraph">
<property name="text">
<string>&amp;Paragraph</string>
</property>
</action>
<action name="actionStyleHeading1">
<property name="text">
<string>Heading &amp;1</string>
</property>
</action>
<action name="actionStyleHeading2">
<property name="text">
<string>Heading &amp;2</string>
</property>
</action>
<action name="actionStyleHeading3">
<property name="text">
<string>Heading &amp;3</string>
</property>
</action>
<action name="actionStyleHeading4">
<property name="text">
<string>Heading &amp;4</string>
</property>
</action>
<action name="actionStyleHeading5">
<property name="text">
<string>Heading &amp;5</string>
</property>
</action>
<action name="actionStyleHeading6">
<property name="text">
<string>Heading &amp;6</string>
</property>
</action>
<action name="actionStylePreformatted">
<property name="text">
<string>Pre&amp;formatted</string>
</property>
</action>
<action name="actionStyleAddress">
<property name="text">
<string>&amp;Address</string>
</property>
</action>
<action name="actionFormatFontName">
<property name="text">
<string>&amp;Font Name...</string>
</property>
</action>
<action name="actionFormatTextColor">
<property name="icon">
<iconset resource="htmleditor.qrc">
<normaloff>:/images/text_color.png</normaloff>:/images/text_color.png</iconset>
</property>
<property name="text">
<string>Text &amp;Color...</string>
</property>
</action>
<action name="actionFormatBackgroundColor">
<property name="text">
<string>Bac&amp;kground Color...</string>
</property>
</action>
<action name="actionFormatFontSize">
<property name="icon">
<iconset resource="htmleditor.qrc">
<normaloff>:/images/text.png</normaloff>:/images/text.png</iconset>
</property>
<property name="text">
<string>Font Si&amp;ze...</string>
</property>
</action>
<action name="actionInsertHtml">
<property name="icon">
<iconset resource="htmleditor.qrc">
<normaloff>:/images/insert-html.png</normaloff>:/images/insert-html.png</iconset>
</property>
<property name="text">
<string>Insert HTML...</string>
</property>
<property name="toolTip">
<string>Insert HTML</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
<class>QWebView</class>
<extends>QWidget</extends>
<header>QtWebKit/QWebView</header>
</customwidget>
</customwidgets>
<resources>
<include location="htmleditor.qrc"/>
</resources>
<connections/>
</ui>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 678 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 692 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 919 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 725 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 773 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 810 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 645 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1011 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 683 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 698 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 506 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 495 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 494 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 506 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 939 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 784 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 797 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 869 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 900 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 757 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 386 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 252 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 739 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 889 B

View File

@@ -1,41 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>426</width>
<height>288</height>
</rect>
</property>
<property name="windowTitle">
<string>Insert HTML</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>HTML Code:</string>
</property>
</widget>
</item>
<item>
<widget class="QPlainTextEdit" name="plainTextEdit"/>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.2 KiB

View File

@@ -0,0 +1 @@
Example running richtext through XSLT

View File

@@ -0,0 +1,112 @@
<ui version="4.0" >
<class>AddLinkDialog</class>
<widget class="QDialog" name="AddLinkDialog" >
<property name="windowTitle" >
<string>Insert Link</string>
</property>
<property name="sizeGripEnabled" >
<bool>false</bool>
</property>
<property name="modal" >
<bool>true</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout" >
<item>
<layout class="QFormLayout" >
<item row="0" column="0" >
<widget class="QLabel" name="label" >
<property name="text" >
<string>Title:</string>
</property>
</widget>
</item>
<item row="0" column="1" >
<widget class="QLineEdit" name="titleInput" >
<property name="minimumSize" >
<size>
<width>337</width>
<height>0</height>
</size>
</property>
</widget>
</item>
<item row="1" column="0" >
<widget class="QLabel" name="label_2" >
<property name="text" >
<string>URL:</string>
</property>
</widget>
</item>
<item row="1" column="1" >
<widget class="QLineEdit" name="urlInput" />
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer" >
<property name="orientation" >
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0" >
<size>
<width>0</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="Line" name="line" >
<property name="orientation" >
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox" >
<property name="orientation" >
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons" >
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>AddLinkDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel" >
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel" >
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>AddLinkDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel" >
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel" >
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@@ -0,0 +1,797 @@
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt Designer of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, 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.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
/**
Integration : QElectroTech Team
Changelog:
- 09/04/2013 : Start integration...Compilation and object creation are successful
*/
#include "richtexteditor_p.h"
#include "ui_addlinkdialog.h"
#include <QtDesigner/QDesignerFormEditorInterface>
#include <QtCore/QList>
#include <QtCore/QMap>
#include <QtCore/QPointer>
#include <QtCore/QProcess>
#include <QtCore/QTemporaryFile>
#include <QtCore/QDir>
#include <QtGui/QAction>
#include <QtGui/QColorDialog>
#include <QtGui/QComboBox>
#include <QtGui/QFontDatabase>
#include <QtGui/QTextCursor>
#include <QtGui/QPainter>
#include <QtGui/QIcon>
#include <QtGui/QMenu>
#include <QtGui/QMoveEvent>
#include <QtGui/QTabWidget>
#include <QtGui/QTextDocument>
#include <QtGui/QTextBlock>
#include <QtGui/QToolBar>
#include <QtGui/QToolButton>
#include <QtGui/QVBoxLayout>
#include <QtGui/QHBoxLayout>
#include <QtGui/QPushButton>
#include <QtGui/QDialogButtonBox>
QT_BEGIN_NAMESPACE
//static const char *RichTextDialogC = "RichTextDialog";
//static const char *Geometry = "Geometry";
namespace qdesigner_internal {
class RichTextEditor : public QTextEdit
{
Q_OBJECT
public:
RichTextEditor(QWidget *parent = 0);
void setDefaultFont(const QFont &font);
QToolBar *createToolBar(QWidget *parent = 0);
public slots:
void setFontBold(bool b);
void setFontPointSize(double);
void setText(const QString &text);
QString text(Qt::TextFormat format) const;
signals:
void stateChanged();
};
class AddLinkDialog : public QDialog
{
Q_OBJECT
public:
AddLinkDialog(RichTextEditor *editor, QWidget *parent = 0);
~AddLinkDialog();
int showDialog();
public slots:
void accept();
private:
RichTextEditor *m_editor;
Ui::AddLinkDialog *m_ui;
};
AddLinkDialog::AddLinkDialog(RichTextEditor *editor, QWidget *parent) :
QDialog(parent),
m_ui(new Ui::AddLinkDialog)
{
m_ui->setupUi(this);
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
m_editor = editor;
}
AddLinkDialog::~AddLinkDialog()
{
delete m_ui;
}
int AddLinkDialog::showDialog()
{
// Set initial focus
const QTextCursor cursor = m_editor->textCursor();
if (cursor.hasSelection()) {
m_ui->titleInput->setText(cursor.selectedText());
m_ui->urlInput->setFocus();
} else {
m_ui->titleInput->setFocus();
}
return exec();
}
void AddLinkDialog::accept()
{
const QString title = m_ui->titleInput->text();
const QString url = m_ui->urlInput->text();
if (!title.isEmpty()) {
QString html = QLatin1String("<a href=\"");
html += url;
html += QLatin1String("\">");
html += title;
html += QLatin1String("</a>");
m_editor->insertHtml(html);
}
m_ui->titleInput->clear();
m_ui->urlInput->clear();
QDialog::accept();
}
class HtmlTextEdit : public QTextEdit
{
Q_OBJECT
public:
HtmlTextEdit(QWidget *parent = 0)
: QTextEdit(parent)
{}
void contextMenuEvent(QContextMenuEvent *event);
private slots:
void actionTriggered(QAction *action);
};
void HtmlTextEdit::contextMenuEvent(QContextMenuEvent *event)
{
QMenu *menu = createStandardContextMenu();
QMenu *htmlMenu = new QMenu(tr("Insert HTML entity"), menu);
typedef struct {
const char *text;
const char *entity;
} Entry;
const Entry entries[] = {
{ "&&amp; (&&)", "&amp;" },
{ "&&nbsp;", "&nbsp;" },
{ "&&lt; (<)", "&lt;" },
{ "&&gt; (>)", "&gt;" },
{ "&&copy; (Copyright)", "&copy;" },
{ "&&reg; (Trade Mark)", "&reg;" },
};
for (int i = 0; i < 6; ++i) {
QAction *entityAction = new QAction(QLatin1String(entries[i].text),
htmlMenu);
entityAction->setData(QLatin1String(entries[i].entity));
htmlMenu->addAction(entityAction);
}
menu->addMenu(htmlMenu);
connect(htmlMenu, SIGNAL(triggered(QAction*)),
SLOT(actionTriggered(QAction*)));
menu->exec(event->globalPos());
delete menu;
}
void HtmlTextEdit::actionTriggered(QAction *action)
{
insertPlainText(action->data().toString());
}
class ColorAction : public QAction
{
Q_OBJECT
public:
ColorAction(QObject *parent);
const QColor& color() const { return m_color; }
void setColor(const QColor &color);
signals:
void colorChanged(const QColor &color);
private slots:
void chooseColor();
private:
QColor m_color;
};
ColorAction::ColorAction(QObject *parent):
QAction(parent)
{
setText(tr("Text Color"));
setColor(Qt::black);
connect(this, SIGNAL(triggered()), this, SLOT(chooseColor()));
}
void ColorAction::setColor(const QColor &color)
{
if (color == m_color)
return;
m_color = color;
QPixmap pix(24, 24);
QPainter painter(&pix);
painter.setRenderHint(QPainter::Antialiasing, false);
painter.fillRect(pix.rect(), m_color);
painter.setPen(m_color.darker());
painter.drawRect(pix.rect().adjusted(0, 0, -1, -1));
setIcon(pix);
}
void ColorAction::chooseColor()
{
const QColor col = QColorDialog::getColor(m_color, 0);
if (col.isValid() && col != m_color) {
setColor(col);
emit colorChanged(m_color);
}
}
class RichTextEditorToolBar : public QToolBar
{
Q_OBJECT
public:
RichTextEditorToolBar(RichTextEditor *editor,
QWidget *parent = 0);
public slots:
void updateActions();
private slots:
void alignmentActionTriggered(QAction *action);
void sizeInputActivated(const QString &size);
void colorChanged(const QColor &color);
void setVAlignSuper(bool super);
void setVAlignSub(bool sub);
void insertLink();
void insertImage();
void runXmlPatterns();
private:
QAction *m_bold_action;
QAction *m_italic_action;
QAction *m_underline_action;
QAction *m_valign_sup_action;
QAction *m_valign_sub_action;
QAction *m_align_left_action;
QAction *m_align_center_action;
QAction *m_align_right_action;
QAction *m_align_justify_action;
QAction *m_link_action;
QAction *m_image_action;
QAction *m_xmlPatterns;
ColorAction *m_color_action;
QComboBox *m_font_size_input;
QPointer<RichTextEditor> m_editor;
};
static QAction *createCheckableAction(const QIcon &icon, const QString &text,
QObject *receiver, const char *slot,
QObject *parent = 0)
{
QAction *result = new QAction(parent);
result->setIcon(icon);
result->setText(text);
result->setCheckable(true);
result->setChecked(false);
if (slot)
QObject::connect(result, SIGNAL(triggered(bool)), receiver, slot);
return result;
}
RichTextEditorToolBar::RichTextEditorToolBar(RichTextEditor *editor,
QWidget *parent) :
QToolBar(parent),
m_link_action(new QAction(this)),
m_image_action(new QAction(this)),
m_color_action(new ColorAction(this)),
m_font_size_input(new QComboBox),
m_editor(editor)
{
m_xmlPatterns = new QAction("Run XMLPatterns", this);
connect(m_xmlPatterns, SIGNAL(triggered()), this, SLOT(runXmlPatterns()));
addAction(m_xmlPatterns);
m_xmlPatterns -> setVisible( false );
// Font size combo box
m_font_size_input->setEditable(false);
const QList<int> font_sizes = QFontDatabase::standardSizes();
foreach (int font_size, font_sizes)
m_font_size_input->addItem(QString::number(font_size));
connect(m_font_size_input, SIGNAL(activated(QString)),
this, SLOT(sizeInputActivated(QString)));
addWidget(m_font_size_input);
addSeparator();
// Bold, italic and underline buttons
m_bold_action = createCheckableAction(QIcon(),
tr("Bold"), editor, SLOT(setFontBold(bool)), this);
m_bold_action->setShortcut(tr("CTRL+B"));
addAction(m_bold_action);
m_italic_action = createCheckableAction(
QIcon(),
tr("Italic"), editor, SLOT(setFontItalic(bool)), this);
m_italic_action->setShortcut(tr("CTRL+I"));
addAction(m_italic_action);
m_underline_action = createCheckableAction(
QIcon(),
tr("Underline"), editor, SLOT(setFontUnderline(bool)), this);
m_underline_action->setShortcut(tr("CTRL+U"));
addAction(m_underline_action);
addSeparator();
// Left, center, right and justified alignment buttons
QActionGroup *alignment_group = new QActionGroup(this);
connect(alignment_group, SIGNAL(triggered(QAction*)),
SLOT(alignmentActionTriggered(QAction*)));
m_align_left_action = createCheckableAction(
QIcon(),
tr("Left Align"), editor, 0, alignment_group);
addAction(m_align_left_action);
m_align_center_action = createCheckableAction(
QIcon(),
tr("Center"), editor, 0, alignment_group);
addAction(m_align_center_action);
m_align_right_action = createCheckableAction(
QIcon(),
tr("Right Align"), editor, 0, alignment_group);
addAction(m_align_right_action);
m_align_justify_action = createCheckableAction(
QIcon(),
tr("Justify"), editor, 0, alignment_group);
addAction(m_align_justify_action);
addSeparator();
// Superscript and subscript buttons
m_valign_sup_action = createCheckableAction(
QIcon(),
tr("Superscript"),
this, SLOT(setVAlignSuper(bool)), this);
addAction(m_valign_sup_action);
m_valign_sub_action = createCheckableAction(
QIcon(),
tr("Subscript"),
this, SLOT(setVAlignSub(bool)), this);
addAction(m_valign_sub_action);
m_valign_sup_action -> setVisible( false );
m_valign_sub_action -> setVisible( false );
/* addSeparator();
// Insert hyperlink and image buttons
m_link_action->setText(tr("Insert &Link"));
connect(m_link_action, SIGNAL(triggered()), SLOT(insertLink()));
addAction(m_link_action);
m_image_action->setText(tr("Insert &Image"));
connect(m_image_action, SIGNAL(triggered()), SLOT(insertImage()));
addAction(m_image_action);
addSeparator();*/
// Text color button
connect(m_color_action, SIGNAL(colorChanged(QColor)),
this, SLOT(colorChanged(QColor)));
addAction(m_color_action);
connect(editor, SIGNAL(textChanged()), this, SLOT(updateActions()));
connect(editor, SIGNAL(stateChanged()), this, SLOT(updateActions()));
updateActions();
}
void RichTextEditorToolBar::alignmentActionTriggered(QAction *action)
{
Qt::Alignment new_alignment;
if (action == m_align_left_action) {
new_alignment = Qt::AlignLeft;
} else if (action == m_align_center_action) {
new_alignment = Qt::AlignCenter;
} else if (action == m_align_right_action) {
new_alignment = Qt::AlignRight;
} else {
new_alignment = Qt::AlignJustify;
}
m_editor->setAlignment(new_alignment);
}
QString runXSLT(const QString &t)
{
QString pattern = QDir::tempPath();
if (!pattern.endsWith('/'))
pattern += '/';
pattern += "qt_tempXXXXXX.html";
QTemporaryFile tf(pattern);
if (!tf.open())
return QLatin1String("Open failure");
const QString tfName = tf.fileName();
tf.write(t.toUtf8());
tf.close();
QProcess p;
QStringList args;
args << "tohtml.xsl" << tfName;
p.start("xmlpatterns",args);
if (!p.waitForStarted() || !p.waitForFinished())
return QLatin1String("Run failure");
const QByteArray output = p.exitStatus() == QProcess::NormalExit &&
p.exitCode() == 0 ?
p.readAllStandardOutput() :
p.readAllStandardError();
return QString::fromUtf8(output);
}
void RichTextEditorToolBar::runXmlPatterns()
{
qWarning("%s", qPrintable(runXSLT(m_editor->text(Qt::RichText))));
}
void RichTextEditorToolBar::colorChanged(const QColor &color)
{
m_editor->setTextColor(color);
m_editor->setFocus();
}
void RichTextEditorToolBar::sizeInputActivated(const QString &size)
{
bool ok;
int i = size.toInt(&ok);
if (!ok)
return;
m_editor->setFontPointSize(i);
m_editor->setFocus();
}
void RichTextEditorToolBar::setVAlignSuper(bool super)
{
const QTextCharFormat::VerticalAlignment align = super ?
QTextCharFormat::AlignSuperScript : QTextCharFormat::AlignNormal;
QTextCharFormat charFormat = m_editor->currentCharFormat();
charFormat.setVerticalAlignment(align);
m_editor->setCurrentCharFormat(charFormat);
m_valign_sub_action->setChecked(false);
}
void RichTextEditorToolBar::setVAlignSub(bool sub)
{
const QTextCharFormat::VerticalAlignment align = sub ?
QTextCharFormat::AlignSubScript : QTextCharFormat::AlignNormal;
QTextCharFormat charFormat = m_editor->currentCharFormat();
charFormat.setVerticalAlignment(align);
m_editor->setCurrentCharFormat(charFormat);
m_valign_sup_action->setChecked(false);
}
void RichTextEditorToolBar::insertLink()
{
AddLinkDialog linkDialog(m_editor, this);
linkDialog.showDialog();
m_editor->setFocus();
}
void RichTextEditorToolBar::insertImage()
{
#ifdef hip
const QString path = IconSelector::choosePixmapResource(m_core, m_core->resourceModel(), QString(), this);
if (!path.isEmpty())
m_editor->insertHtml(QLatin1String("<img src=\"") + path + QLatin1String("\"/>"));
#endif
}
void RichTextEditorToolBar::updateActions()
{
if (m_editor == 0) {
setEnabled(false);
return;
}
const Qt::Alignment alignment = m_editor->alignment();
const QTextCursor cursor = m_editor->textCursor();
const QTextCharFormat charFormat = cursor.charFormat();
const QFont font = charFormat.font();
const QTextCharFormat::VerticalAlignment valign =
charFormat.verticalAlignment();
const bool superScript = valign == QTextCharFormat::AlignSuperScript;
const bool subScript = valign == QTextCharFormat::AlignSubScript;
if (alignment & Qt::AlignLeft) {
m_align_left_action->setChecked(true);
} else if (alignment & Qt::AlignRight) {
m_align_right_action->setChecked(true);
} else if (alignment & Qt::AlignHCenter) {
m_align_center_action->setChecked(true);
} else {
m_align_justify_action->setChecked(true);
}
m_bold_action->setChecked(font.bold());
m_italic_action->setChecked(font.italic());
m_underline_action->setChecked(font.underline());
m_valign_sup_action->setChecked(superScript);
m_valign_sub_action->setChecked(subScript);
const int size = font.pointSize();
const int idx = m_font_size_input->findText(QString::number(size));
if (idx != -1)
m_font_size_input->setCurrentIndex(idx);
m_color_action->setColor(m_editor->textColor());
}
RichTextEditor::RichTextEditor(QWidget *parent)
: QTextEdit(parent)
{
connect(this, SIGNAL(currentCharFormatChanged(QTextCharFormat)),
this, SIGNAL(stateChanged()));
connect(this, SIGNAL(cursorPositionChanged()),
this, SIGNAL(stateChanged()));
}
QToolBar *RichTextEditor::createToolBar(QWidget *parent)
{
return new RichTextEditorToolBar(this, parent);
}
void RichTextEditor::setFontBold(bool b)
{
if (b)
setFontWeight(QFont::Bold);
else
setFontWeight(QFont::Normal);
}
void RichTextEditor::setFontPointSize(double d)
{
QTextEdit::setFontPointSize(qreal(d));
}
void RichTextEditor::setText(const QString &text)
{
if (Qt::mightBeRichText(text))
setHtml(text);
else
setPlainText(text);
}
void RichTextEditor::setDefaultFont(const QFont &font)
{
document()->setDefaultFont(font);
if (font.pointSize() > 0)
setFontPointSize(font.pointSize());
else
setFontPointSize(QFontInfo(font).pointSize());
emit textChanged();
}
QString RichTextEditor::text(Qt::TextFormat format) const
{
switch (format) {
case Qt::LogText:
case Qt::PlainText:
return toPlainText();
case Qt::RichText:
return toHtml();
case Qt::AutoText:
break;
}
const QString html = toHtml();
const QString plain = toPlainText();
QTextEdit tester;
tester.setPlainText(plain);
return tester.toHtml() == html ? plain : html;
}
RichTextEditorDialog::RichTextEditorDialog(QWidget *parent) :
QDialog(parent),
m_editor(new RichTextEditor()),
m_text_edit(new HtmlTextEdit),
m_tab_widget(new QTabWidget),
m_state(Clean)
{
setWindowTitle(tr("Edit text"));
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
m_text_edit->setAcceptRichText(false);
connect(m_editor, SIGNAL(textChanged()), this, SLOT(richTextChanged()));
connect(m_text_edit, SIGNAL(textChanged()), this, SLOT(sourceChanged()));
// The toolbar needs to be created after the RichTextEditor
QToolBar *tool_bar = m_editor->createToolBar();
tool_bar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
QWidget *rich_edit = new QWidget;
QVBoxLayout *rich_edit_layout = new QVBoxLayout(rich_edit);
rich_edit_layout->addWidget(tool_bar);
rich_edit_layout->addWidget(m_editor);
QWidget *plain_edit = new QWidget;
QVBoxLayout *plain_edit_layout = new QVBoxLayout(plain_edit);
plain_edit_layout->addWidget(m_text_edit);
m_tab_widget->setTabPosition(QTabWidget::South);
m_tab_widget->addTab(rich_edit, tr("Rich Text"));
m_tab_widget->addTab(plain_edit, tr("Source"));
connect(m_tab_widget, SIGNAL(currentChanged(int)),
SLOT(tabIndexChanged(int)));
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal);
QPushButton *ok_button = buttonBox->button(QDialogButtonBox::Ok);
ok_button->setText(tr("&OK"));
ok_button->setDefault(true);
buttonBox->button(QDialogButtonBox::Cancel)->setText(tr("&Cancel"));
connect(buttonBox, SIGNAL(accepted()), this, SLOT(on_buttonBox_accepted()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(m_tab_widget);
layout->addWidget(buttonBox);
m_editor->setFocus();
}
RichTextEditorDialog::~RichTextEditorDialog()
{
}
/**
* @brief RichTextEditorDialog::on_buttonBox_accepted
*/
void RichTextEditorDialog::on_buttonBox_accepted() {
emit applyEditText( text(Qt::RichText) );
this->close();
}
int RichTextEditorDialog::showDialog()
{
m_tab_widget->setCurrentIndex(0);
m_editor->selectAll();
m_editor->setFocus();
return exec();
}
void RichTextEditorDialog::setDefaultFont(const QFont &font)
{
m_editor->setDefaultFont(font);
}
void RichTextEditorDialog::setText(const QString &text)
{
m_editor->setText(text);
m_text_edit->setPlainText(text);
m_state = Clean;
}
QString RichTextEditorDialog::text(Qt::TextFormat format) const
{
// In autotext mode, if the user has changed the source, use that
if (format == Qt::AutoText && (m_state == Clean || m_state == SourceChanged))
return m_text_edit->toPlainText();
// If the plain text HTML editor is selected, first copy its contents over
// to the rich text editor so that it is converted to Qt-HTML or actual
// plain text.
if (m_tab_widget->currentIndex() == SourceIndex && m_state == SourceChanged)
m_editor->setHtml(m_text_edit->toPlainText());
return m_editor->text(format);
}
void RichTextEditorDialog::tabIndexChanged(int newIndex)
{
// Anything changed, is there a need for a conversion?
if (newIndex == SourceIndex && m_state != RichTextChanged)
return;
if (newIndex == RichTextIndex && m_state != SourceChanged)
return;
const State oldState = m_state;
// Remember the cursor position, since it is invalidated by setPlainText
QTextEdit *new_edit = (newIndex == SourceIndex) ? m_text_edit : m_editor;
const int position = new_edit->textCursor().position();
if (newIndex == SourceIndex) {
const QString html = m_editor->text(Qt::RichText);
qWarning("%s", qPrintable(runXSLT(html)));
m_text_edit->setPlainText(html);
} else
m_editor->setHtml(m_text_edit->toPlainText());
QTextCursor cursor = new_edit->textCursor();
cursor.movePosition(QTextCursor::End);
if (cursor.position() > position) {
cursor.setPosition(position);
}
new_edit->setTextCursor(cursor);
m_state = oldState; // Changed is triggered by setting the text
}
void RichTextEditorDialog::richTextChanged()
{
m_state = RichTextChanged;
}
void RichTextEditorDialog::sourceChanged()
{
m_state = SourceChanged;
}
} // namespace qdesigner_internal
QT_END_NAMESPACE
#include "richtexteditor.moc"

View File

@@ -0,0 +1,104 @@
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt Designer of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, 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.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists for the convenience
// of Qt Designer. This header
// file may change from version to version without notice, or even be removed.
//
// We mean it.
//
#ifndef RICHTEXTEDITOR_H
#define RICHTEXTEDITOR_H
#include <QtGui/QTextEdit>
#include <QtGui/QDialog>
QT_BEGIN_NAMESPACE
class QTabWidget;
class QToolBar;
class QDesignerFormEditorInterface;
namespace qdesigner_internal {
class RichTextEditor;
class RichTextEditorDialog : public QDialog
{
Q_OBJECT
public:
explicit RichTextEditorDialog(QWidget *parent = 0);
~RichTextEditorDialog();
int showDialog();
void setDefaultFont(const QFont &font);
void setText(const QString &text);
QString text(Qt::TextFormat format = Qt::AutoText) const;
signals:
void applyEditText(const QString &);
private slots:
void tabIndexChanged(int newIndex);
void richTextChanged();
void sourceChanged();
void on_buttonBox_accepted();
private:
enum TabIndex { RichTextIndex, SourceIndex };
enum State { Clean, RichTextChanged, SourceChanged };
RichTextEditor *m_editor;
QTextEdit *m_text_edit;
QTabWidget *m_tab_widget;
State m_state;
};
} // namespace qdesigner_internal
QT_END_NAMESPACE
#endif // RITCHTEXTEDITOR_H

View File

@@ -0,0 +1,117 @@
/********************************************************************************
** Form generated from reading UI file 'addlinkdialog.ui'
**
** Created: Thu 4. Apr 17:13:59 2013
** by: Qt User Interface Compiler version 4.8.4
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_ADDLINKDIALOG_H
#define UI_ADDLINKDIALOG_H
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QDialog>
#include <QtGui/QDialogButtonBox>
#include <QtGui/QFormLayout>
#include <QtGui/QFrame>
#include <QtGui/QHeaderView>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QSpacerItem>
#include <QtGui/QVBoxLayout>
QT_BEGIN_NAMESPACE
class Ui_AddLinkDialog
{
public:
QVBoxLayout *verticalLayout;
QFormLayout *formLayout;
QLabel *label;
QLineEdit *titleInput;
QLabel *label_2;
QLineEdit *urlInput;
QSpacerItem *verticalSpacer;
QFrame *line;
QDialogButtonBox *buttonBox;
void setupUi(QDialog *AddLinkDialog)
{
if (AddLinkDialog->objectName().isEmpty())
AddLinkDialog->setObjectName(QString::fromUtf8("AddLinkDialog"));
AddLinkDialog->setSizeGripEnabled(false);
AddLinkDialog->setModal(true);
verticalLayout = new QVBoxLayout(AddLinkDialog);
verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
formLayout = new QFormLayout();
formLayout->setObjectName(QString::fromUtf8("formLayout"));
label = new QLabel(AddLinkDialog);
label->setObjectName(QString::fromUtf8("label"));
formLayout->setWidget(0, QFormLayout::LabelRole, label);
titleInput = new QLineEdit(AddLinkDialog);
titleInput->setObjectName(QString::fromUtf8("titleInput"));
titleInput->setMinimumSize(QSize(337, 0));
formLayout->setWidget(0, QFormLayout::FieldRole, titleInput);
label_2 = new QLabel(AddLinkDialog);
label_2->setObjectName(QString::fromUtf8("label_2"));
formLayout->setWidget(1, QFormLayout::LabelRole, label_2);
urlInput = new QLineEdit(AddLinkDialog);
urlInput->setObjectName(QString::fromUtf8("urlInput"));
formLayout->setWidget(1, QFormLayout::FieldRole, urlInput);
verticalLayout->addLayout(formLayout);
verticalSpacer = new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
verticalLayout->addItem(verticalSpacer);
line = new QFrame(AddLinkDialog);
line->setObjectName(QString::fromUtf8("line"));
line->setFrameShape(QFrame::HLine);
line->setFrameShadow(QFrame::Sunken);
verticalLayout->addWidget(line);
buttonBox = new QDialogButtonBox(AddLinkDialog);
buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
buttonBox->setOrientation(Qt::Horizontal);
buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
verticalLayout->addWidget(buttonBox);
retranslateUi(AddLinkDialog);
QObject::connect(buttonBox, SIGNAL(accepted()), AddLinkDialog, SLOT(accept()));
QObject::connect(buttonBox, SIGNAL(rejected()), AddLinkDialog, SLOT(reject()));
QMetaObject::connectSlotsByName(AddLinkDialog);
} // setupUi
void retranslateUi(QDialog *AddLinkDialog)
{
AddLinkDialog->setWindowTitle(QApplication::translate("AddLinkDialog", "Insert Link", 0, QApplication::UnicodeUTF8));
label->setText(QApplication::translate("AddLinkDialog", "Title:", 0, QApplication::UnicodeUTF8));
label_2->setText(QApplication::translate("AddLinkDialog", "URL:", 0, QApplication::UnicodeUTF8));
} // retranslateUi
};
namespace Ui {
class AddLinkDialog: public Ui_AddLinkDialog {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_ADDLINKDIALOG_H