Dynamic element text item : add new feature -> alignment

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@5353 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2018-05-11 18:14:41 +00:00
parent bf12d337c9
commit 609fcc351f
10 changed files with 480 additions and 6 deletions

View File

@@ -0,0 +1,94 @@
/*
Copyright 2006-2017 The QElectroTech Team
This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
QElectroTech is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/
#include "alignmenttextdialog.h"
#include "ui_alignmenttextdialog.h"
#include "dynamicelementtextitem.h"
AlignmentTextDialog::AlignmentTextDialog(DynamicElementTextItem *text, QWidget *parent) :
QDialog(parent),
ui(new Ui::AlignmentTextDialog)
{
ui->setupUi(this);
Qt::Alignment align = text->alignment();
if(align == (Qt::AlignTop|Qt::AlignLeft))
ui->top_left->setChecked(true);
else if(align == (Qt::AlignTop|Qt::AlignHCenter))
ui->top->setChecked(true);
else if(align == (Qt::AlignTop|Qt::AlignRight))
ui->top_right->setChecked(true);
else if(align == (Qt::AlignVCenter|Qt::AlignLeft))
ui->left->setChecked(true);
else if(align == Qt::AlignCenter)
ui->center->setChecked(true);
else if(align == (Qt::AlignVCenter|Qt::AlignRight))
ui->right->setChecked(true);
else if(align == (Qt::AlignBottom|Qt::AlignLeft))
ui->bottom_left->setChecked(true);
else if(align == (Qt::AlignBottom|Qt::AlignHCenter))
ui->bottom->setChecked(true);
else if(align == (Qt::AlignBottom|Qt::AlignRight))
ui->bottom_right->setChecked(true);
}
AlignmentTextDialog::~AlignmentTextDialog()
{
delete ui;
}
/**
* @brief AlignmentTextDialog::alignment
* @return the selected alignment
*/
Qt::Alignment AlignmentTextDialog::alignment() const
{
if(ui->top_left->isChecked())
return (Qt::AlignTop|Qt::AlignLeft);
else if(ui->top->isChecked())
return (Qt::AlignTop|Qt::AlignHCenter);
else if(ui->top_right->isChecked())
return (Qt::AlignTop|Qt::AlignRight);
else if(ui->left->isChecked())
return (Qt::AlignVCenter|Qt::AlignLeft);
else if (ui->center->isChecked())
return Qt::AlignCenter;
else if(ui->right->isChecked())
return (Qt::AlignVCenter|Qt::AlignRight);
else if(ui->bottom_left->isChecked())
return (Qt::AlignBottom|Qt::AlignLeft);
else if(ui->bottom->isChecked())
return (Qt::AlignBottom|Qt::AlignHCenter);
else if(ui->bottom_right->isChecked())
return (Qt::AlignBottom|Qt::AlignRight);
else
return (Qt::AlignTop|Qt::AlignLeft);
}
bool AlignmentTextDialog::event(QEvent *event)
{
//Little hack to set focus to a radio button
//if we not do that, when the user click on the title bar (for move the dialog) or try to resize the dialog,
//the dialog lose focus and close.
if(event->type() == QEvent::Show && m_first_show)
{
QTimer::singleShot(50, [this](){ui->top_left->setFocus();});
m_first_show = false;
}
return QDialog::event(event);
}

View File

@@ -0,0 +1,47 @@
/*
Copyright 2006-2017 The QElectroTech Team
This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
QElectroTech is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ALIGNMENTTEXTDIALOG_H
#define ALIGNMENTTEXTDIALOG_H
#include <QDialog>
class DynamicElementTextItem;
namespace Ui {
class AlignmentTextDialog;
}
class AlignmentTextDialog : public QDialog
{
Q_OBJECT
public:
explicit AlignmentTextDialog(DynamicElementTextItem *text, QWidget *parent = nullptr);
~AlignmentTextDialog();
Qt::Alignment alignment() const;
protected:
bool event(QEvent *event);
private:
bool m_first_show = true;
Ui::AlignmentTextDialog *ui;
};
#endif // ALIGNMENTTEXTDIALOG_H

View File

@@ -0,0 +1,185 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>AlignmentTextDialog</class>
<widget class="QDialog" name="AlignmentTextDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>160</width>
<height>158</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>200</horstretch>
<verstretch>150</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>160</width>
<height>158</height>
</size>
</property>
<property name="windowTitle">
<string>Alignement du texte</string>
</property>
<property name="modal">
<bool>true</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="3">
<widget class="QRadioButton" name="right">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="1" column="4">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="2">
<widget class="QRadioButton" name="top">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="2" column="2">
<widget class="QRadioButton" name="bottom">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="0" column="3">
<widget class="QRadioButton" name="top_right">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QRadioButton" name="left">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="2" column="3">
<widget class="QRadioButton" name="bottom_right">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QRadioButton" name="bottom_left">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QRadioButton" name="top_left">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QRadioButton" name="center">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="1" column="0">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>AlignmentTextDialog</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>AlignmentTextDialog</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

@@ -33,6 +33,7 @@
#include "qeticons.h"
#include "diagram.h"
#include "addelementtextcommand.h"
#include "alignmenttextdialog.h"
int src_txt_row = 0;
int usr_txt_row = 1;
@@ -45,6 +46,7 @@ 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;
int align_grp_row = 0;
int rot_grp_row = 1;
@@ -276,6 +278,19 @@ QList<QStandardItem *> DynamicElementTextModel::itemsForText(DynamicElementTextI
qsi_list.clear();;
qsi_list << rot << rot_a;
qsi->appendRow(qsi_list);
//Alignment
QStandardItem *alignment = new QStandardItem(tr("Alignement"));
alignment->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
QStandardItem *alignmenta = new QStandardItem(tr("Éditer"));
alignmenta->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable);
alignmenta->setData(DynamicElementTextModel::txtAlignment, Qt::UserRole+1);
alignmenta->setData(QVariant::fromValue(deti->alignment()), Qt::UserRole+2);
qsi_list.clear();
qsi_list << alignment << alignmenta;
qsi->appendRow(qsi_list);
}
@@ -451,6 +466,13 @@ QUndoCommand *DynamicElementTextModel::undoForEditedText(DynamicElementTextItem
new QPropertyUndoCommand(deti, "compositeText", QVariant(deti->compositeText()), QVariant(composite_text), undo);
}
Qt::Alignment alignment = text_qsi->child(align_txt_row, 1)->data(Qt::UserRole+2).value<Qt::Alignment>();
if (alignment != deti->alignment())
{
QPropertyUndoCommand *quc = new QPropertyUndoCommand(deti, "alignment", QVariant(deti->alignment()), QVariant(alignment), undo);
quc->setText(tr("Modifier l'alignement d'un texte d'élément"));
}
int fs = text_qsi->child(size_txt_row,1)->data(Qt::EditRole).toInt();
if (fs != deti->fontSize())
{
@@ -1368,6 +1390,22 @@ QWidget *DynamicTextItemDelegate::createEditor(QWidget *parent, const QStyleOpti
cted->setObjectName("composite_text");
return cted;
}
case DynamicElementTextModel::txtAlignment:
{
const DynamicElementTextModel *detm = static_cast<const DynamicElementTextModel *>(index.model());
QStandardItem *qsi = detm->itemFromIndex(index);
if(!qsi)
break;
DynamicElementTextItem *deti = detm->textFromIndex(index);
if(!deti)
break;
AlignmentTextDialog *atd = new AlignmentTextDialog(deti, parent);
atd->setObjectName("alignment_text");
return atd;
}
case DynamicElementTextModel::size:
{
QSpinBox *sb = new QSpinBox(parent);
@@ -1502,6 +1540,19 @@ void DynamicTextItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *
}
}
}
else if (editor->objectName() == "alignment_text")
{
if(QStandardItemModel *qsim = dynamic_cast<QStandardItemModel *>(model))
{
if(QStandardItem *qsi = qsim->itemFromIndex(index))
{
AlignmentTextDialog *atd = static_cast<AlignmentTextDialog *>(editor);
Qt::Alignment align = atd->alignment();
qsi->setData(QVariant::fromValue(align), Qt::UserRole+2);
return;
}
}
}
else if (editor->objectName() == "group_alignment")
{
if(QStandardItemModel *qsim = dynamic_cast<QStandardItemModel *>(model))

View File

@@ -42,6 +42,7 @@ class DynamicElementTextModel : public QStandardItemModel
userText,
infoText,
compositeText,
txtAlignment,
size,
color,
pos,