From 476fc3fe17b842d7dc5e7484ea4beff9baa9e801 Mon Sep 17 00:00:00 2001 From: Ivan Ilin Date: Mon, 21 Nov 2011 11:08:22 +0200 Subject: [PATCH] some new UBGraphicsTextItem properties --- .../interactive/Barre_prof.wgt/config.xml | 16 +-- src/adaptors/UBCFFSubsetAdaptor.cpp | 117 +++++++++++++----- src/adaptors/UBCFFSubsetAdaptor.h | 5 + src/adaptors/UBSvgSubsetAdaptor.cpp | 1 - src/adaptors/UBSvgSubsetAdaptor.h | 4 +- src/core/UB.h | 4 +- src/domain/UBGraphicsItemDelegate.h | 5 +- src/domain/UBGraphicsScene.cpp | 20 +++ src/domain/UBGraphicsScene.h | 1 + src/domain/UBGraphicsTextItem.cpp | 23 ++-- src/domain/UBGraphicsTextItemDelegate.cpp | 59 +++++++-- src/domain/UBGraphicsTextItemDelegate.h | 7 +- 12 files changed, 198 insertions(+), 64 deletions(-) diff --git a/resources/library/interactive/Barre_prof.wgt/config.xml b/resources/library/interactive/Barre_prof.wgt/config.xml index 103098a0..b2eaab1e 100644 --- a/resources/library/interactive/Barre_prof.wgt/config.xml +++ b/resources/library/interactive/Barre_prof.wgt/config.xml @@ -1,14 +1,14 @@ - - - Rich Note - Allows the teacher to attach a rich note to the page. - + ub:resizable="false"> + + Rich Note + Allows the teacher to attach a rich note to the page. + diff --git a/src/adaptors/UBCFFSubsetAdaptor.cpp b/src/adaptors/UBCFFSubsetAdaptor.cpp index d07255ef..aea6bcd7 100644 --- a/src/adaptors/UBCFFSubsetAdaptor.cpp +++ b/src/adaptors/UBCFFSubsetAdaptor.cpp @@ -27,6 +27,7 @@ #include "domain/UBGraphicsVideoItem.h" #include "domain/UBGraphicsAudioItem.h" #include "domain/UBGraphicsWidgetItem.h" +#include "domain/UBGraphicsTextItem.h" #include "domain/UBW3CWidget.h" #include "frameworks/UBFileSystemUtils.h" @@ -440,6 +441,49 @@ void UBCFFSubsetAdaptor::UBCFFSubsetReader::parseTextAttributes(const QDomElemen if (!element.attribute(aTransform).isNull()) fontTransform = transformFromString(element.attribute(aTransform)); } +void UBCFFSubsetAdaptor::UBCFFSubsetReader::readTextBlockAttr(const QDomElement &element, QTextBlockFormat &format) +{ + QString fontStretchText = element.attribute(aFontstretch); + if (!fontStretchText.isNull()) format.setAlignment(Qt::AlignJustify); + + QString align = element.attribute(aTextalign); + if (!align.isNull()) { + if (align == "middle" || align == "center") format.setAlignment(Qt::AlignHCenter); + else if (align == "start") format.setAlignment(Qt::AlignLeft); + else if (align == "end") format.setAlignment(Qt::AlignRight); + else if (align == "justify") format.setAlignment(Qt::AlignJustify); + } +} +void UBCFFSubsetAdaptor::UBCFFSubsetReader::readTextCharAttr(const QDomElement &element, QTextCharFormat &format) +{ + QString fontSz = element.attribute(aFontSize); + if (!fontSz.isNull()) { + qreal fontSize = fontSz.toDouble() * 72 / QApplication::desktop()->physicalDpiY(); + format.setFontPointSize(fontSize); + } + QString fontColorText = element.attribute(aFill); + if (!fontColorText.isNull()) { + QColor fontColor = colorFromString(fontColorText); + if (fontColor.isValid()) format.setForeground(fontColor); + } + QString fontFamilyText = element.attribute(aFontfamily); + if (!fontFamilyText.isNull()) { + format.setFontFamily(fontFamilyText); + } + if (!element.attribute(aFontstyle).isNull()) { + bool italic = (element.attribute(aFontstyle) == "italic"); + format.setFontItalic(italic); + } + QString weight = element.attribute(aFontweight); + if (!weight.isNull()) { + if (weight == "normal") format.setFontWeight(QFont::Normal); + else if (weight == "light") format.setFontWeight(QFont::Light); + else if (weight == "demibold") format.setFontWeight(QFont::DemiBold); + else if (weight == "bold") format.setFontWeight(QFont::Bold); + else if (weight == "black") format.setFontWeight(QFont::Black); + } +} + bool UBCFFSubsetAdaptor::UBCFFSubsetReader::parseSvgText(const QDomElement &element) { qreal x = element.attribute(aX).toDouble(); @@ -538,53 +582,70 @@ void UBCFFSubsetAdaptor::UBCFFSubsetReader::parseTSpan(const QDomElement &parent bool UBCFFSubsetAdaptor::UBCFFSubsetReader::parseSvgTextarea(const QDomElement &element) { + qreal x = element.attribute(aX).toDouble(); qreal y = element.attribute(aY).toDouble(); qreal width = element.attribute(aWidth).toDouble(); qreal height = element.attribute(aHeight).toDouble(); - qreal fontSize = 12; - QColor fontColor(qApp->palette().foreground().color()); - QString fontFamily = "Arial"; - QString fontStretch = "normal"; - bool italic = false; - int fontWeight = QFont::Normal; - int textAlign = Qt::AlignLeft; + + +// qreal fontSize = 12; +// QColor fontColor(qApp->palette().foreground().color()); +// QString fontFamily = "Arial"; +// QString fontStretch = "normal"; +// bool italic = false; +// int fontWeight = QFont::Normal; +// int textAlign = Qt::AlignLeft; QTransform fontTransform; - parseTextAttributes(element, fontSize, fontColor, fontFamily, fontStretch, italic, fontWeight, textAlign, fontTransform); +// parseTextAttributes(element, fontSize, fontColor, fontFamily, fontStretch, italic, fontWeight, textAlign, fontTransform); - QSvgGenerator *generator = createSvgGenerator(width, height); - QPainter painter; - painter.begin(generator); +// QSvgGenerator *generator = createSvgGenerator(width, height); +// QPainter painter; +// painter.begin(generator); - painter.setFont(QFont(fontFamily, fontSize, fontWeight, italic)); +// painter.setFont(QFont(fontFamily, fontSize, fontWeight, italic)); - qreal curY = 0.0; - qreal curX = 0.0; - qreal linespacing = QFontMetricsF(painter.font()).leading(); +// qreal curY = 0.0; +// qreal curX = 0.0; +// qreal linespacing = QFontMetricsF(painter.font()).leading(); -// remember if text area has transform -// QString transformString; +//// remember if text area has transform +//// QString transformString; QTransform transform = fontTransform; - bool hasTransform = !fontTransform.isIdentity(); + bool hasTransform = false;//!fontTransform.isIdentity(); - QRectF lastDrawnTextBoundingRect; - //parse text area tags +// QRectF lastDrawnTextBoundingRect; +// //parse text area tags - //recursive call any tspan in text svg element - parseTSpan(element, painter - , curX, curY, width, height, linespacing, lastDrawnTextBoundingRect - , fontSize, fontColor, fontFamily, fontStretch, italic, fontWeight, textAlign, fontTransform); +// //recursive call any tspan in text svg element +// parseTSpan(element, painter +// , curX, curY, width, height, linespacing, lastDrawnTextBoundingRect +// , fontSize, fontColor, fontFamily, fontStretch, italic, fontWeight, textAlign, fontTransform); - painter.end(); +// painter.end(); //add resulting svg file to scene - UBGraphicsSvgItem *svgItem = mCurrentScene->addSvg(QUrl::fromLocalFile(generator->fileName())); +// UBGraphicsSvgItem *svgItem = mCurrentScene->addSvg(QUrl::fromLocalFile(generator->fileName())); + QFile file("/home/ilia/Documents/tmp/1/index.html"); + file.open(QIODevice::ReadOnly); + QByteArray barr = file.readAll(); + file.close(); + QString str(barr); + UBGraphicsTextItem *svgItem = mCurrentScene->addTextHtml(str); + svgItem->resize(width * mVBTransFactor, height * mVBTransFactor); + +// QTextCursor cursor; +// cursor.insertBlock(); +// cursor.insertText("way away"); +// cursor.insertBlock(); +// cursor.insertText("for the right"); +// svgItem->setTextCursor(cursor); repositionSvgItem(svgItem, width, height, x, y, hasTransform, transform); - hashSceneItem(element, svgItem); +// hashSceneItem(element, svgItem); - delete generator; +// delete generator; return true; } bool UBCFFSubsetAdaptor::UBCFFSubsetReader::parseSvgImage(const QDomElement &element) diff --git a/src/adaptors/UBCFFSubsetAdaptor.h b/src/adaptors/UBCFFSubsetAdaptor.h index d92dba52..82c8c5f3 100644 --- a/src/adaptors/UBCFFSubsetAdaptor.h +++ b/src/adaptors/UBCFFSubsetAdaptor.h @@ -32,6 +32,8 @@ class QTransform; class QPainter; class UBGraphicsItem; class QGraphicsItem; +class QTextBlockFormat; +class QTextCharFormat; class UBCFFSubsetAdaptor @@ -104,6 +106,9 @@ private: inline void parseTextAttributes(const QDomElement &element, qreal &fontSize, QColor &fontColor, QString &fontFamily, QString &fontStretch, bool &italic, int &fontWeight, int &textAlign, QTransform &fontTransform); + inline void parseTextAttributes(const QDomElement &element, QFont &font, QColor); + inline void readTextBlockAttr(const QDomElement &element, QTextBlockFormat &format); + inline void readTextCharAttr(const QDomElement &element, QTextCharFormat &format); //elements parsing methods bool parseDoc(); diff --git a/src/adaptors/UBSvgSubsetAdaptor.cpp b/src/adaptors/UBSvgSubsetAdaptor.cpp index 09c1fb8d..0d1bc0a3 100644 --- a/src/adaptors/UBSvgSubsetAdaptor.cpp +++ b/src/adaptors/UBSvgSubsetAdaptor.cpp @@ -76,7 +76,6 @@ QString UBSvgSubsetAdaptor::toSvgTransform(const QMatrix& matrix) .arg(matrix.m22(), 0 , 'g') .arg(matrix.dx(), 0 , 'g') .arg(matrix.dy(), 0 , 'g'); - } diff --git a/src/adaptors/UBSvgSubsetAdaptor.h b/src/adaptors/UBSvgSubsetAdaptor.h index cd57ebcc..4b52193e 100644 --- a/src/adaptors/UBSvgSubsetAdaptor.h +++ b/src/adaptors/UBSvgSubsetAdaptor.h @@ -46,8 +46,8 @@ class UBSvgSubsetAdaptor { private: - UBSvgSubsetAdaptor() {}; - virtual ~UBSvgSubsetAdaptor() {}; + UBSvgSubsetAdaptor() {;} + virtual ~UBSvgSubsetAdaptor() {;} public: diff --git a/src/core/UB.h b/src/core/UB.h index 3f60df17..fbe7dd77 100644 --- a/src/core/UB.h +++ b/src/core/UB.h @@ -75,7 +75,9 @@ struct UBGraphicsItemData { enum Enum { - ItemLayerType, ItemLocked + ItemLayerType + , ItemLocked + , ItemEditable//for text only }; }; diff --git a/src/domain/UBGraphicsItemDelegate.h b/src/domain/UBGraphicsItemDelegate.h index 055a5de3..9986db6b 100644 --- a/src/domain/UBGraphicsItemDelegate.h +++ b/src/domain/UBGraphicsItemDelegate.h @@ -165,12 +165,11 @@ class UBGraphicsItemDelegate : public QObject QList mButtons; - protected slots: +protected slots: virtual void gotoContentSource(bool checked); - - private: +private: QPointF mOffset; QTransform mPreviousTransform; diff --git a/src/domain/UBGraphicsScene.cpp b/src/domain/UBGraphicsScene.cpp index 99bea72f..c27734e7 100644 --- a/src/domain/UBGraphicsScene.cpp +++ b/src/domain/UBGraphicsScene.cpp @@ -1311,7 +1311,27 @@ UBGraphicsTextItem* UBGraphicsScene::addTextWithFont(const QString& pString, con return textItem; } +UBGraphicsTextItem *UBGraphicsScene::addTextHtml(const QString &pString) +{ + UBGraphicsTextItem *textItem = new UBGraphicsTextItem(); + textItem->setPlainText(""); + textItem->setHtml(pString); + + addItem(textItem); + textItem->show(); + UBGraphicsItemUndoCommand* uc = new UBGraphicsItemUndoCommand(this, 0, textItem); + UBApplication::undoStack->push(uc); + + connect(textItem, SIGNAL(textUndoCommandAdded(UBGraphicsTextItem *)), this, SLOT(textUndoCommandAdded(UBGraphicsTextItem *))); + + textItem->setSelected(true); + textItem->setFocus(); + + setDocumentUpdated(); + + return textItem; +} void UBGraphicsScene::addItem(QGraphicsItem* item) { diff --git a/src/domain/UBGraphicsScene.h b/src/domain/UBGraphicsScene.h index 5e6a9a3b..70ce9465 100644 --- a/src/domain/UBGraphicsScene.h +++ b/src/domain/UBGraphicsScene.h @@ -91,6 +91,7 @@ class UBGraphicsScene: public UBCoreGraphicsScene, public UBItem UBGraphicsTextItem* addTextWithFont(const QString& pString, const QPointF& pTopLeft = QPointF(0, 0) , int pointSize = -1, const QString& fontFamily = "", bool bold = false, bool italic = false); + UBGraphicsTextItem* addTextHtml(const QString &pString = QString()); UBGraphicsW3CWidgetItem* addOEmbed(const QUrl& pContentUrl, const QPointF& pPos = QPointF(0, 0)); diff --git a/src/domain/UBGraphicsTextItem.cpp b/src/domain/UBGraphicsTextItem.cpp index b2a5536f..6125997c 100644 --- a/src/domain/UBGraphicsTextItem.cpp +++ b/src/domain/UBGraphicsTextItem.cpp @@ -56,8 +56,10 @@ UBGraphicsTextItem::UBGraphicsTextItem(QGraphicsItem * parent) } setData(UBGraphicsItemData::ItemLayerType, UBItemLayerType::Object); +// setData(UBGraphicsItemData::ItemEditable, QVariant(true)); setFlag(QGraphicsItem::ItemIsSelectable, true); +// setFlag(QGraphicsItem::ItemIsMovable, true); setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); setTextInteractionFlags(Qt::TextEditorInteraction); @@ -65,11 +67,11 @@ UBGraphicsTextItem::UBGraphicsTextItem(QGraphicsItem * parent) connect(document(), SIGNAL(contentsChanged()), mDelegate, SLOT(contentsChanged())); connect(document(), SIGNAL(undoCommandAdded()), this, SLOT(undoCommandAdded())); - connect(document()->documentLayout(), SIGNAL(documentSizeChanged(const QSizeF &)), this, SLOT(documentSizeChanged(const QSizeF &))); + connect(document()->documentLayout(), SIGNAL(documentSizeChanged(const QSizeF &)), + this, SLOT(documentSizeChanged(const QSizeF &))); } - UBGraphicsTextItem::~UBGraphicsTextItem() { if (mDelegate) @@ -78,7 +80,6 @@ UBGraphicsTextItem::~UBGraphicsTextItem() } } - QVariant UBGraphicsTextItem::itemChange(GraphicsItemChange change, const QVariant &value) { if (QGraphicsItem::ItemSelectedChange == change) @@ -106,9 +107,14 @@ QVariant UBGraphicsTextItem::itemChange(GraphicsItemChange change, const QVarian return QGraphicsTextItem::itemChange(change, newValue); } - void UBGraphicsTextItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { + if (mDelegate) + mDelegate->mousePressEvent(event); + + if (!data(UBGraphicsItemData::ItemEditable).toBool()) + return; + int elapsed = mLastMousePressTime.msecsTo(QTime::currentTime()); if (elapsed < UBApplication::app()->doubleClickInterval()) @@ -126,12 +132,8 @@ void UBGraphicsTextItem::mousePressEvent(QGraphicsSceneMouseEvent *event) if (mMultiClickState == 1) { - if (mDelegate) - mDelegate->mousePressEvent(event); - +// setTextInteractionFlags(Qt::TextEditorInteraction); QGraphicsTextItem::mousePressEvent(event); - setTextInteractionFlags(Qt::TextEditorInteraction); - setFocus(); } else if (mMultiClickState == 2) @@ -152,7 +154,6 @@ void UBGraphicsTextItem::mousePressEvent(QGraphicsSceneMouseEvent *event) } } - void UBGraphicsTextItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { if (!mDelegate || !mDelegate->mouseMoveEvent(event)) @@ -161,7 +162,6 @@ void UBGraphicsTextItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) } } - void UBGraphicsTextItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { if (mMultiClickState == 1) @@ -177,7 +177,6 @@ void UBGraphicsTextItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) } } - void UBGraphicsTextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { QColor color = UBSettings::settings()->isDarkBackground() ? mColorOnDarkBackground : mColorOnLightBackground; diff --git a/src/domain/UBGraphicsTextItemDelegate.cpp b/src/domain/UBGraphicsTextItemDelegate.cpp index 1ab6dc3f..856919d6 100644 --- a/src/domain/UBGraphicsTextItemDelegate.cpp +++ b/src/domain/UBGraphicsTextItemDelegate.cpp @@ -27,6 +27,7 @@ #include "core/UBDisplayManager.h" // TODO UB 4.x clean that dependency #include "core/memcheck.h" +#include "board/UBBoardController.h" const int UBGraphicsTextItemDelegate::sMinPixelSize = 8; @@ -139,16 +140,21 @@ void UBGraphicsTextItemDelegate::pickFont() UBSettings::settings()->setFontPixelSize(mLastFontPixelSize); UBSettings::settings()->setBoldFont(selectedFont.bold()); UBSettings::settings()->setItalicFont(selectedFont.italic()); - delegated()->setFont(selectedFont); - delegated()->setSelected(true); + //setting format for selected item + QTextCursor curCursor = delegated()->textCursor(); + QTextCharFormat format; + format.setFont(selectedFont); + curCursor.mergeCharFormat(format); + delegated()->setTextCursor(curCursor); + + delegated()->setSelected(true); delegated()->document()->adjustSize(); delegated()->contentsChanged(); } } } - void UBGraphicsTextItemDelegate::pickColor() { if (mDelegated && mDelegated->scene() && mDelegated->scene()->views().size() > 0) @@ -164,8 +170,17 @@ void UBGraphicsTextItemDelegate::pickColor() { QColor selectedColor = colorDialog.selectedColor(); delegated()->setDefaultTextColor(selectedColor); - delegated()->setColorOnDarkBackground(selectedColor); - delegated()->setColorOnLightBackground(selectedColor); +// delegated()->setColorOnDarkBackground(selectedColor); +// delegated()->setColorOnLightBackground(selectedColor); + QTextCursor curCursor = delegated()->textCursor(); + QTextCharFormat format; + format.setForeground(QBrush(selectedColor)); +// format.setBackground(Qt::yellow); + curCursor.mergeCharFormat(format); + QTextBlockFormat blFormat; + blFormat.setAlignment(Qt::AlignCenter); + curCursor.setBlockFormat(blFormat); + delegated()->setTextCursor(curCursor); UBGraphicsTextItem::lastUsedTextColor = selectedColor; @@ -175,7 +190,6 @@ void UBGraphicsTextItemDelegate::pickColor() } } - void UBGraphicsTextItemDelegate::decreaseSize() { QFontInfo fi(delegated()->font()); @@ -203,7 +217,6 @@ void UBGraphicsTextItemDelegate::decreaseSize() } - void UBGraphicsTextItemDelegate::increaseSize() { QFontInfo fi(delegated()->font()); @@ -229,8 +242,38 @@ void UBGraphicsTextItemDelegate::increaseSize() qDebug() << newPixelSize; } - UBGraphicsTextItem* UBGraphicsTextItemDelegate::delegated() { return static_cast(mDelegated); } +void UBGraphicsTextItemDelegate::setEditable(bool editable) +{ + if (editable) { + delegated()->setTextInteractionFlags(Qt::TextEditorInteraction); + mDelegated->setData(UBGraphicsItemData::ItemEditable, QVariant(true)); + } else { + QTextCursor cursor(delegated()->document()); + cursor.clearSelection(); + delegated()->setTextCursor(cursor); + + delegated()->setTextInteractionFlags(Qt::NoTextInteraction); + mDelegated->setData(UBGraphicsItemData::ItemEditable, QVariant(false)); + } +} +bool UBGraphicsTextItemDelegate::isEditable() +{ + return mDelegated->data(UBGraphicsItemData::ItemEditable).toBool(); +} +void UBGraphicsTextItemDelegate::decorateMenu(QMenu *menu) +{ + UBGraphicsItemDelegate::decorateMenu(menu); + + mEditableAction = menu->addAction(tr("Editable"), this, SLOT(setEditable(bool))); + mEditableAction->setCheckable(true); + mEditableAction->setChecked(true); + +} +void UBGraphicsTextItemDelegate::updateMenuActionState() +{ + UBGraphicsItemDelegate::updateMenuActionState(); +} diff --git a/src/domain/UBGraphicsTextItemDelegate.h b/src/domain/UBGraphicsTextItemDelegate.h index 827e2b43..f29d1070 100644 --- a/src/domain/UBGraphicsTextItemDelegate.h +++ b/src/domain/UBGraphicsTextItemDelegate.h @@ -27,17 +27,21 @@ class UBGraphicsTextItem; class UBGraphicsTextItemDelegate : public UBGraphicsItemDelegate { - Q_OBJECT; + Q_OBJECT public: UBGraphicsTextItemDelegate(UBGraphicsTextItem* pDelegated, QObject * parent = 0); virtual ~UBGraphicsTextItemDelegate(); + bool isEditable(); public slots: void contentsChanged(); + virtual void setEditable(bool); protected: virtual void buildButtons(); + virtual void decorateMenu(QMenu *menu); + virtual void updateMenuActionState(); private: @@ -54,6 +58,7 @@ class UBGraphicsTextItemDelegate : public UBGraphicsItemDelegate private: void customize(QFontDialog &fontDialog); + QAction *mEditableAction; private slots: