Add widget to edit QetGraphicsTableItem and QetGraphicsHeaderItem

This commit is contained in:
Claveau Joshua
2020-03-08 10:38:49 +01:00
parent 2a29b4b240
commit f7a090c3ca
12 changed files with 1148 additions and 111 deletions

View File

@@ -0,0 +1,284 @@
/*
Copyright 2006-2020 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 "graphicstablepropertieseditor.h"
#include "ui_graphicstablepropertieseditor.h"
#include "qetgraphicstableitem.h"
#include "qetgraphicsheaderitem.h"
#include "diagram.h"
#include "QPropertyUndoCommand/qpropertyundocommand.h"
#include "itemmodelcommand.h"
#include <QAbstractItemModel>
#include <QFontDialog>
/**
* @brief GraphicsTablePropertiesEditor::GraphicsTablePropertiesEditor
* @param table
* @param parent
*/
GraphicsTablePropertiesEditor::GraphicsTablePropertiesEditor(QetGraphicsTableItem *table, QWidget *parent) :
PropertiesEditorWidget(parent),
ui(new Ui::GraphicsTablePropertiesEditor)
{
ui->setupUi(this);
m_header_button_group = new QButtonGroup(this);
m_header_button_group->addButton(ui->m_header_align_left_rb, Qt::AlignLeft);
m_header_button_group->addButton(ui->m_header_align_center_rb, Qt::AlignHCenter);
m_header_button_group->addButton(ui->m_header_align_right_rb, Qt::AlignRight);
m_table_button_group = new QButtonGroup(this);
m_table_button_group->addButton(ui->m_table_align_left_rb, Qt::AlignLeft);
m_table_button_group->addButton(ui->m_table_align_center_rb, Qt::AlignHCenter);
m_table_button_group->addButton(ui->m_table_align_right_rb, Qt::AlignRight);
if (table) {
setTable(table);
}
}
/**
* @brief GraphicsTablePropertiesEditor::~GraphicsTablePropertiesEditor
*/
GraphicsTablePropertiesEditor::~GraphicsTablePropertiesEditor() {
delete ui;
}
/**
* @brief GraphicsTablePropertiesEditor::setTable
* Set the table to be edited
* @param table
*/
void GraphicsTablePropertiesEditor::setTable(QetGraphicsTableItem *table)
{
if (m_table_item) {
for (auto c : m_connect_list) {
disconnect(c);
}
}
m_table_item = table;
m_connect_list.clear();
m_connect_list << connect(m_table_item.data(), &QetGraphicsTableItem::xChanged, this, &GraphicsTablePropertiesEditor::updateUi);
m_connect_list << connect(m_table_item.data(), &QetGraphicsTableItem::yChanged, this, &GraphicsTablePropertiesEditor::updateUi);
updateUi();
}
/**
* @brief GraphicsTablePropertiesEditor::apply
* Apply the current edition
*/
void GraphicsTablePropertiesEditor::apply()
{
if(!m_table_item && m_table_item->diagram()) {
return;
}
auto d = m_table_item->diagram();
auto undo = associatedUndo();
if (undo) {
d->undoStack().push(undo);
}
}
/**
* @brief GraphicsTablePropertiesEditor::associatedUndo
* @return the undo command associated to the edition
*/
QUndoCommand *GraphicsTablePropertiesEditor::associatedUndo() const
{
if (m_live_edit)
{
if (!qFuzzyCompare(ui->m_x_pos->value(), m_table_item->pos().x())) {
auto undo = new QPropertyUndoCommand(m_table_item.data(), "x", m_table_item->pos().x(), ui->m_x_pos->value());
undo->setAnimated(true, false);
undo->setText(tr("Déplacer un tableau"));
return undo;
}
if (!qFuzzyCompare(ui->m_y_pos->value(), m_table_item->pos().y())) {
auto undo = new QPropertyUndoCommand(m_table_item.data(), "y", m_table_item->pos().y(), ui->m_y_pos->value());
undo->setAnimated(true, false);
undo->setText(tr("Déplacer un tableau"));
return undo;
}
QMargins header_margins(ui->m_header_left_margin->value(),
ui->m_header_top_margin->value(),
ui->m_header_right_margin->value(),
ui->m_header_bottom_margin->value());
if (header_margins != m_table_item->headerItem()->margins())
{
QVariant old_; old_.setValue(m_table_item->headerItem()->margins());
QVariant new_; new_.setValue(header_margins);
auto undo = new QPropertyUndoCommand(m_table_item->headerItem(), "margins", old_, new_);
undo->setText(tr("Modifier les marges d'une en tête de tableau"));
return undo;
}
QMargins table_margins(ui->m_table_left_margin->value(),
ui->m_table_top_margin->value(),
ui->m_table_right_margin->value(),
ui->m_table_bottom_margin->value());
if (table_margins != m_table_item->margins())
{
QVariant old_; old_.setValue(m_table_item->margins());
QVariant new_; new_.setValue(table_margins);
auto undo = new QPropertyUndoCommand(m_table_item.data(), "margins", old_, new_);
undo->setText(tr("Modifier les marges d'un tableau"));
return undo;
}
if (m_header_button_group->checkedId() != m_table_item->model()->headerData(0, Qt::Horizontal, Qt::TextAlignmentRole).toInt())
{
auto undo = new ModelHeaderDataCommand(m_table_item->model());
undo->setData(0, Qt::Horizontal, m_header_button_group->checkedId(), Qt::TextAlignmentRole);
undo->setText(tr("Modifier l'alignement d'une en tête de tableau"));
return undo;
}
if (m_table_button_group->checkedId() != m_table_item->model()->index(0,0).data(Qt::TextAlignmentRole).toInt())
{
auto undo = new ModelIndexCommand(m_table_item->model(), m_table_item->model()->index(0,0));
undo->setData(m_table_button_group->checkedId(), Qt::TextAlignmentRole);
undo->setText(tr("Modifier l'alignement des textes d'un tableau"));
return undo;
}
}
return nullptr;
}
bool GraphicsTablePropertiesEditor::setLiveEdit(bool live_edit)
{
if (m_live_edit == live_edit) {
return true;
}
m_live_edit = live_edit;
setUpEditConnection();
return true;
}
/**
* @brief GraphicsTablePropertiesEditor::on_m_header_font_pb_clicked
*/
void GraphicsTablePropertiesEditor::on_m_header_font_pb_clicked()
{
if (m_table_item && m_table_item->model())
{
bool ok;
auto font = QFontDialog::getFont(&ok,
m_table_item->model()->headerData(0, Qt::Horizontal, Qt::FontRole).value<QFont>(),
this);
if (ok && m_table_item->model())
{
auto undo = new ModelHeaderDataCommand(m_table_item->model());
undo->setData(0, Qt::Horizontal, QVariant::fromValue(font), Qt::FontRole);
undo->setText(tr("Modifier la police d'une en tête de tableau"));
m_table_item->diagram()->undoStack().push(undo);
}
}
}
/**
* @brief GraphicsTablePropertiesEditor::on_m_table_font_pb_clicked
*/
void GraphicsTablePropertiesEditor::on_m_table_font_pb_clicked()
{
if (m_table_item && m_table_item->model())
{
bool ok;
auto index = m_table_item->model()->index(0,0);
auto old_font = m_table_item->model()->data(index, Qt::FontRole).value<QFont>();
auto new_font = QFontDialog::getFont(&ok, old_font, this);
if (ok && m_table_item->diagram())
{
auto undo = new ModelIndexCommand(m_table_item->model(), index);
undo->setData(QVariant::fromValue(new_font), Qt::FontRole);
undo->setText(tr("Changer la police d'un tableau"));
m_table_item->diagram()->undoStack().push(undo);
}
}
}
/**
* @brief GraphicsTablePropertiesEditor::updateUi
*/
void GraphicsTablePropertiesEditor::updateUi()
{
//Disconnect every connections of editor widgets
//to avoid an unwanted edition (QSpinBox emit valueChanged no matter if changer by user or by program)
for (QMetaObject::Connection c : m_edit_connection) {
disconnect(c);
}
m_edit_connection.clear();
ui->m_x_pos->setValue(m_table_item->pos().x());
ui->m_y_pos->setValue(m_table_item->pos().y());
auto margin = m_table_item->headerItem()->margins();
ui->m_header_top_margin ->setValue(margin.top());
ui->m_header_left_margin ->setValue(margin.left());
ui->m_header_right_margin ->setValue(margin.right());
ui->m_header_bottom_margin->setValue(margin.bottom());
margin = m_table_item->margins();
ui->m_table_top_margin ->setValue(margin.top());
ui->m_table_left_margin ->setValue(margin.left());
ui->m_table_right_margin ->setValue(margin.right());
ui->m_table_bottom_margin->setValue(margin.bottom());
if (!m_table_item->model()) {
return;
}
m_header_button_group->button(m_table_item->model()->headerData(0, Qt::Horizontal, Qt::TextAlignmentRole).toInt())->setChecked(true);
m_table_button_group->button(m_table_item->model()->data(m_table_item->model()->index(0,0), Qt::TextAlignmentRole).toInt())->setChecked(true);
setUpEditConnection();
}
/**
* @brief GraphicsTablePropertiesEditor::setUpEditConnection
*/
void GraphicsTablePropertiesEditor::setUpEditConnection()
{
for (QMetaObject::Connection c : m_edit_connection) {
disconnect(c);
}
m_edit_connection.clear();
if (m_live_edit)
{
m_edit_connection << connect(ui->m_x_pos, QOverload<int>::of(&QSpinBox::valueChanged), this, &GraphicsTablePropertiesEditor::apply);
m_edit_connection << connect(ui->m_y_pos, QOverload<int>::of(&QSpinBox::valueChanged), this, &GraphicsTablePropertiesEditor::apply);
m_edit_connection << connect(ui->m_header_top_margin, QOverload<int>::of(&QSpinBox::valueChanged), this, &GraphicsTablePropertiesEditor::apply);
m_edit_connection << connect(ui->m_header_left_margin, QOverload<int>::of(&QSpinBox::valueChanged), this, &GraphicsTablePropertiesEditor::apply);
m_edit_connection << connect(ui->m_header_right_margin, QOverload<int>::of(&QSpinBox::valueChanged), this, &GraphicsTablePropertiesEditor::apply);
m_edit_connection << connect(ui->m_header_bottom_margin, QOverload<int>::of(&QSpinBox::valueChanged), this, &GraphicsTablePropertiesEditor::apply);
m_edit_connection << connect(ui->m_table_top_margin, QOverload<int>::of(&QSpinBox::valueChanged), this, &GraphicsTablePropertiesEditor::apply);
m_edit_connection << connect(ui->m_table_left_margin, QOverload<int>::of(&QSpinBox::valueChanged), this, &GraphicsTablePropertiesEditor::apply);
m_edit_connection << connect(ui->m_table_right_margin, QOverload<int>::of(&QSpinBox::valueChanged), this, &GraphicsTablePropertiesEditor::apply);
m_edit_connection << connect(ui->m_table_bottom_margin, QOverload<int>::of(&QSpinBox::valueChanged), this, &GraphicsTablePropertiesEditor::apply);
m_edit_connection << connect(m_table_button_group, QOverload<int>::of(&QButtonGroup::buttonClicked), this, &GraphicsTablePropertiesEditor::apply);
m_edit_connection << connect(m_header_button_group, QOverload<int>::of(&QButtonGroup::buttonClicked), this, &GraphicsTablePropertiesEditor::apply);
}
}

View File

@@ -0,0 +1,69 @@
/*
Copyright 2006-2020 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 GRAPHICSTABLEPROPERTIESEDITOR_H
#define GRAPHICSTABLEPROPERTIESEDITOR_H
#include "PropertiesEditor/propertieseditorwidget.h"
#include <QPointer>
namespace Ui {
class GraphicsTablePropertiesEditor;
}
class QetGraphicsTableItem;
class QAbstractItemModel;
class QUndoStack;
class QButtonGroup;
/**
* @brief The GraphicsTablePropertiesEditor class
* This widget is used to edit the property of both QetGraphicsTableItem and QetGraphicsHeaderItem
*/
class GraphicsTablePropertiesEditor : public PropertiesEditorWidget
{
Q_OBJECT
public:
explicit GraphicsTablePropertiesEditor(QetGraphicsTableItem *table = nullptr, QWidget *parent = nullptr);
~GraphicsTablePropertiesEditor() override;
void setTable(QetGraphicsTableItem *table);
virtual void apply() override;
QUndoCommand * associatedUndo() const override;
virtual bool setLiveEdit(bool live_edit) override;
private slots:
void on_m_header_font_pb_clicked();
void on_m_table_font_pb_clicked();
virtual void updateUi() override;
private:
void setUpEditConnection();
private:
Ui::GraphicsTablePropertiesEditor *ui;
QPointer<QetGraphicsTableItem> m_table_item;
QList <QMetaObject::Connection> m_connect_list,
m_edit_connection;
QButtonGroup *m_header_button_group = nullptr,
*m_table_button_group = nullptr;
};
Q_DECLARE_METATYPE(QMargins)
#endif // GRAPHICSTABLEPROPERTIESEDITOR_H

View File

@@ -0,0 +1,253 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>GraphicsTablePropertiesEditor</class>
<widget class="QWidget" name="GraphicsTablePropertiesEditor">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>331</width>
<height>484</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Position</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QLabel" name="label_8">
<property name="text">
<string>X :</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="m_x_pos">
<property name="maximum">
<number>10000</number>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_7">
<property name="text">
<string>Y :</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="m_y_pos">
<property name="maximum">
<number>10000</number>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>En tête</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QGridLayout" name="gridLayout">
<item row="2" column="1">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Marge</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QSpinBox" name="m_header_left_margin"/>
</item>
<item row="3" column="1">
<widget class="QSpinBox" name="m_header_bottom_margin"/>
</item>
<item row="2" column="2">
<widget class="QSpinBox" name="m_header_right_margin"/>
</item>
<item row="1" column="1">
<widget class="QSpinBox" name="m_header_top_margin"/>
</item>
</layout>
</item>
<item>
<widget class="QWidget" name="widget" native="true">
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="label_5">
<property name="text">
<string>Aligement :</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="m_header_align_left_rb">
<property name="text">
<string>Gauche</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="m_header_align_center_rb">
<property name="text">
<string>Centré</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="m_header_align_right_rb">
<property name="text">
<string>Droite</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QPushButton" name="m_header_font_pb">
<property name="text">
<string>Police</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_3">
<property name="title">
<string>Tableau</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<layout class="QGridLayout" name="gridLayout_3">
<item row="1" column="2">
<widget class="QSpinBox" name="m_table_right_margin"/>
</item>
<item row="0" column="1">
<widget class="QSpinBox" name="m_table_top_margin"/>
</item>
<item row="2" column="1">
<widget class="QSpinBox" name="m_table_bottom_margin"/>
</item>
<item row="1" column="0">
<widget class="QSpinBox" name="m_table_left_margin"/>
</item>
<item row="1" column="1">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Marge</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QWidget" name="widget_2" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="label_6">
<property name="text">
<string>Alignement :</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="m_table_align_left_rb">
<property name="text">
<string>Gauche</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="m_table_align_center_rb">
<property name="text">
<string>Centré</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="m_table_align_right_rb">
<property name="text">
<string>Droite</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QPushButton" name="m_table_font_pb">
<property name="text">
<string>Police</string>
</property>
</widget>
</item>
</layout>
</widget>
</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>
</layout>
</widget>
<resources/>
<connections/>
</ui>