[qet] qet/qet: [5764] Dynamic element text item : The font of the dynamic texts can be individually be setted. |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/qet Archives
]
Revision: 5764
Author: blacksun
Date: 2019-03-08 11:27:33 +0100 (Fri, 08 Mar 2019)
Log Message:
-----------
Dynamic element text item : The font of the dynamic texts can be individually be setted.
Modified Paths:
--------------
trunk/sources/qetgraphicsitem/diagramtextitem.cpp
trunk/sources/qetgraphicsitem/diagramtextitem.h
trunk/sources/qetgraphicsitem/dynamicelementtextitem.cpp
trunk/sources/ui/dynamicelementtextmodel.cpp
trunk/sources/ui/dynamicelementtextmodel.h
Modified: trunk/sources/qetgraphicsitem/diagramtextitem.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/diagramtextitem.cpp 2019-03-07 18:11:51 UTC (rev 5763)
+++ trunk/sources/qetgraphicsitem/diagramtextitem.cpp 2019-03-08 10:27:33 UTC (rev 5764)
@@ -162,9 +162,23 @@
int DiagramTextItem::fontSize() const
{
- return font().pointSize();
+ return font().pointSize();
}
+void DiagramTextItem::setFont(const QFont &font)
+{
+ if (this->font() == font) {
+ return;
+ }
+ else
+ {
+ prepareAlignment();
+ QGraphicsTextItem::setFont(font);
+ finishAlignment();
+ emit fontChanged(font);
+ }
+}
+
void DiagramTextItem::setColor(const QColor& color)
{
setDefaultTextColor(color);
Modified: trunk/sources/qetgraphicsitem/diagramtextitem.h
===================================================================
--- trunk/sources/qetgraphicsitem/diagramtextitem.h 2019-03-07 18:11:51 UTC (rev 5763)
+++ trunk/sources/qetgraphicsitem/diagramtextitem.h 2019-03-08 10:27:33 UTC (rev 5764)
@@ -19,6 +19,7 @@
#define DIAGRAM_TEXT_ITEM_H
#include <QGraphicsTextItem>
+#include <QFont>
class Diagram;
class QDomElement;
@@ -37,6 +38,7 @@
Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged)
Q_PROPERTY(QString plainText READ toPlainText WRITE setPlainText)
+ Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
signals:
@@ -44,6 +46,7 @@
void colorChanged(QColor color);
void alignmentChanged(Qt::Alignment alignment);
void textEdited(const QString &old_str, const QString &new_str);
+ void fontChanged(QFont font);
public:
DiagramTextItem(QGraphicsItem * = nullptr);
@@ -68,6 +71,8 @@
void setFontSize(int s);
int fontSize()const;
+
+ void setFont(const QFont &font);
void setColor(const QColor& color);
QColor color() const;
Modified: trunk/sources/qetgraphicsitem/dynamicelementtextitem.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/dynamicelementtextitem.cpp 2019-03-07 18:11:51 UTC (rev 5763)
+++ trunk/sources/qetgraphicsitem/dynamicelementtextitem.cpp 2019-03-08 10:27:33 UTC (rev 5764)
@@ -92,12 +92,10 @@
root_element.setAttribute("x", QString::number(pos().x()));
root_element.setAttribute("y", QString::number(pos().y()));
root_element.setAttribute("rotation", QString::number(QET::correctAngle(rotation())));
- root_element.setAttribute("font_size", font().pointSize());
root_element.setAttribute("uuid", m_uuid.toString());
- root_element.setAttribute("font_family", font().family());
- root_element.setAttribute("dynamicitemstyle", font().styleName());
root_element.setAttribute("frame", m_frame? "true" : "false");
root_element.setAttribute("text_width", QString::number(m_text_width));
+ root_element.setAttribute("font", font().toString());
QMetaEnum me = textFromMetaEnum();
root_element.setAttribute("text_from", me.valueToKey(m_text_from));
@@ -162,10 +160,21 @@
}
QGraphicsTextItem::setRotation(dom_elmt.attribute("rotation", QString::number(0)).toDouble());
- QFont font_(dom_elmt.attribute("font_family", font().family()),
- dom_elmt.attribute("font_size", QString::number(9)).toInt());
- font_.setStyleName(dom_elmt.attribute("dynamicitemstyle", font().styleName()));
- setFont(font_);
+
+ if (dom_elmt.hasAttribute("font"))
+ {
+ QFont font;
+ font.fromString(dom_elmt.attribute("font"));
+ setFont(font);
+ }
+ else //Retrocompatibility during the 0.7 dev because the font property was added lately. TODO remove this part in futur
+ {
+ QFont font_(dom_elmt.attribute("font_family", font().family()),
+ dom_elmt.attribute("font_size", QString::number(9)).toInt());
+ font_.setStyleName(dom_elmt.attribute("dynamicitemstyle", font().styleName()));
+ setFont(font_);
+ }
+
m_uuid = QUuid(dom_elmt.attribute("uuid", QUuid::createUuid().toString()));
setFrame(dom_elmt.attribute("frame", "false") == "true"? true : false);
setTextWidth(dom_elmt.attribute("text_width", QString::number(-1)).toDouble());
@@ -643,13 +652,13 @@
if (m_frame)
{
painter->save();
- painter->setFont(QETApp::dynamicTextsItemFont(fontSize()));
+ painter->setFont(QETApp::dynamicTextsItemFont(font().pointSize()));
//Adjust the thickness according to the font size,
qreal w=0.3;
- if(fontSize() >= 5)
+ if(font().pointSize() >= 5)
{
- w = (qreal)fontSize()*0.1;
+ w = font().pointSizeF()*0.1;
if(w > 2.5)
w = 2.5;
}
@@ -659,9 +668,9 @@
pen.setWidthF(w);
painter->setPen(pen);
painter->setRenderHint(QPainter::Antialiasing);
-
+
//Adjust the rounding of the rectangle according to the size of the font
- qreal ro = (qreal)fontSize()/3;
+ qreal ro = font().pointSizeF()/3;
painter->drawRoundedRect(frameRect(), ro, ro);
painter->restore();
Modified: trunk/sources/ui/dynamicelementtextmodel.cpp
===================================================================
--- trunk/sources/ui/dynamicelementtextmodel.cpp 2019-03-07 18:11:51 UTC (rev 5763)
+++ trunk/sources/ui/dynamicelementtextmodel.cpp 2019-03-08 10:27:33 UTC (rev 5764)
@@ -35,26 +35,27 @@
#include "addelementtextcommand.h"
#include "alignmenttextdialog.h"
-int src_txt_row = 0;
-int usr_txt_row = 1;
-int info_txt_row = 2;
-int compo_txt_row = 3;
-int size_txt_row = 4;
-int color_txt_row = 5;
-int frame_txt_row = 6;
-int width_txt_row = 7;
-int x_txt_row = 8;
-int y_txt_row = 9;
-int rot_txt_row = 10;
-int align_txt_row = 11;
+static int src_txt_row = 0;
+static int usr_txt_row = 1;
+static int info_txt_row = 2;
+static int compo_txt_row = 3;
+static int size_txt_row = 4;
+static int font_txt_row = 5;
+static int color_txt_row = 6;
+static int frame_txt_row = 7;
+static int width_txt_row = 8;
+static int x_txt_row = 9;
+static int y_txt_row = 10;
+static int rot_txt_row = 11;
+static int align_txt_row = 12;
-int align_grp_row = 0;
-int x_grp_row = 1;
-int y_grp_row = 2;
-int rot_grp_row = 3;
-int adjust_grp_row = 4;
-int frame_grp_row = 5;
-int hold_to_bottom_grp_row = 6;
+static int align_grp_row = 0;
+static int x_grp_row = 1;
+static int y_grp_row = 2;
+static int rot_grp_row = 3;
+static int adjust_grp_row = 4;
+static int frame_grp_row = 5;
+static int hold_to_bottom_grp_row = 6;
DynamicElementTextModel::DynamicElementTextModel(Element *element, QObject *parent) :
QStandardItemModel(parent),
@@ -114,7 +115,7 @@
/**
* @brief DynamicElementTextModel::itemsForText
* @param deti
- * @return The items for the text @deti, if the text@deti is already managed by this model
+ * @return The items for the text @deti, if the text @deti is already managed by this model
* the returned list is empty
* The returned items haven't got the same number of childs if the text is in a group or not.
*/
@@ -193,7 +194,7 @@
size->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
QStandardItem *siza = new QStandardItem();
- siza->setData(deti->fontSize(), Qt::EditRole);
+ siza->setData(deti->font().pointSize(), Qt::EditRole);
siza->setData(DynamicElementTextModel::size, Qt::UserRole+1);
siza->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable);
@@ -201,6 +202,21 @@
qsi_list << size << siza;
qsi->appendRow(qsi_list);
+ //Font
+ QStandardItem *font = new QStandardItem(tr("Police"));
+ font->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
+
+ QStandardItem *fonta = new QStandardItem();
+ fonta->setFont(deti->font());
+ fonta->setData(deti->font().family(), Qt::EditRole);
+ fonta->setData(DynamicElementTextModel::font, Qt::UserRole+1);
+ fonta->setData(deti->font(), Qt::UserRole+2);
+ fonta->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable);
+
+ qsi_list.clear();
+ qsi_list << font << fonta;
+ qsi->appendRow(qsi_list);
+
//Color
QStandardItem *color = new QStandardItem(tr("Couleur"));
color->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
@@ -470,12 +486,19 @@
}
int fs = text_qsi->child(size_txt_row,1)->data(Qt::EditRole).toInt();
- if (fs != deti->fontSize())
+ if (fs != deti->font().pointSize())
{
- QPropertyUndoCommand *quc = new QPropertyUndoCommand(deti, "fontSize", QVariant(deti->fontSize()), QVariant(fs), undo);
+ QPropertyUndoCommand *quc = new QPropertyUndoCommand(deti, "fontSize", QVariant(deti->font().pointSize()), QVariant(fs), undo);
quc->setAnimated(true, false);
quc->setText(tr("Modifier la taille d'un texte d'élément"));
}
+
+ QFont font = text_qsi->child(font_txt_row, 1)->data(Qt::UserRole+2).value<QFont>();
+ if (font != deti->font())
+ {
+ QPropertyUndoCommand *quc = new QPropertyUndoCommand(deti, "font", QVariant(deti->font()), QVariant(font), undo);
+ quc->setText(tr("Modifier la police d'un texte d'élément"));
+ }
QColor color = text_qsi->child(color_txt_row,1)->data(Qt::EditRole).value<QColor>();
if(color != deti->color())
@@ -1209,7 +1232,7 @@
QList<QMetaObject::Connection> connection_list;
connection_list << connect(deti, &DynamicElementTextItem::colorChanged, [deti,this](){this->updateDataFromText(deti, color);});
- connection_list << connect(deti, &DynamicElementTextItem::fontSizeChanged, [deti,this](){this->updateDataFromText(deti, size);});
+ connection_list << connect(deti, &DynamicElementTextItem::fontChanged, [deti,this](){this->updateDataFromText(deti, font);});
connection_list << connect(deti, &DynamicElementTextItem::textFromChanged, [deti,this](){this->updateDataFromText(deti, textFrom);});
connection_list << connect(deti, &DynamicElementTextItem::textChanged, [deti,this](){this->updateDataFromText(deti, userText);});
connection_list << connect(deti, &DynamicElementTextItem::infoNameChanged, [deti,this](){this->updateDataFromText(deti, infoText);});
@@ -1317,8 +1340,17 @@
break;
}
case size:
- qsi->child(size_txt_row,1)->setData(deti->fontSize(), Qt::EditRole);
+ //qsi->child(size_txt_row,1)->setData(deti->fontSize(), Qt::EditRole);
break;
+ case font:
+ {
+ QFont f(deti->font());
+ qsi->child(font_txt_row,1)->setFont(f);
+ qsi->child(font_txt_row,1)->setData(f.family(), Qt::EditRole);
+ qsi->child(font_txt_row,1)->setData(f, Qt::UserRole+2);
+ qsi->child(size_txt_row,1)->setData(f.pointSize(), Qt::EditRole);
+ break;
+ }
case color:
{
qsi->child(color_txt_row,1)->setData(deti->color(), Qt::EditRole);
@@ -1380,6 +1412,7 @@
{
qsi->child(x_grp_row,1)->setData(group->pos().x(), Qt::EditRole);
qsi->child(y_grp_row,1)->setData(group->pos().y(), Qt::EditRole);
+ break;
}
case grpRotation:
qsi->child(rot_grp_row,1)->setData(group->rotation(), Qt::EditRole);
@@ -1493,9 +1526,15 @@
sb->setFrame(false);
return sb;
}
+ case DynamicElementTextModel::font:
+ {
+ QFontDialog *fd = new QFontDialog(index.data(Qt::UserRole+2).value<QFont>(), parent);
+ fd->setObjectName("font_dialog");
+ return fd;
+ }
case DynamicElementTextModel::color:
{
- QColorDialog *cd = new QColorDialog(index.data(Qt::EditRole).value<QColor>());
+ QColorDialog *cd = new QColorDialog(index.data(Qt::EditRole).value<QColor>(), parent);
cd->setObjectName("color_dialog");
return cd;
}
@@ -1573,15 +1612,41 @@
{
if (index.isValid())
{
- if(editor->objectName() == "color_dialog")
+ if (editor->objectName() == "font_dialog")
{
if (QStandardItemModel *qsim = dynamic_cast<QStandardItemModel *>(model))
{
if(QStandardItem *qsi = qsim->itemFromIndex(index))
{
+ QFontDialog *fd = static_cast<QFontDialog *>(editor);
+ if (fd->result() == QDialog::Accepted)
+ {
+ //qsi->setData(fd->selectedFont().family(), Qt::EditRole);
+ //qsi->setData(fd->selectedFont(), Qt::UserRole+2);
+ /**For unknow reason, call selectedFont doesn't work,
+ * (always return the same font, no matter what you select)
+ * Instead we use curentFont
+ **/
+ qsi->setData(fd->currentFont().family(), Qt::EditRole);
+ qsi->setData(fd->currentFont(), Qt::UserRole+2);
+ qsi->setFont(fd->currentFont());
+ }
+ return;
+ }
+ }
+ }
+ else if(editor->objectName() == "color_dialog")
+ {
+ if (QStandardItemModel *qsim = dynamic_cast<QStandardItemModel *>(model))
+ {
+ if(QStandardItem *qsi = qsim->itemFromIndex(index))
+ {
QColorDialog *cd = static_cast<QColorDialog *> (editor);
- qsi->setData(cd->selectedColor(), Qt::EditRole);
- qsi->setData(cd->selectedColor(), Qt::ForegroundRole);
+ if (cd->result() == QDialog::Accepted)
+ {
+ qsi->setData(cd->selectedColor(), Qt::EditRole);
+ qsi->setData(cd->selectedColor(), Qt::ForegroundRole);
+ }
return;
}
Modified: trunk/sources/ui/dynamicelementtextmodel.h
===================================================================
--- trunk/sources/ui/dynamicelementtextmodel.h 2019-03-07 18:11:51 UTC (rev 5763)
+++ trunk/sources/ui/dynamicelementtextmodel.h 2019-03-08 10:27:33 UTC (rev 5764)
@@ -44,6 +44,7 @@
compositeText,
txtAlignment,
size,
+ font,
color,
pos,
frame,