Qet graphics table item can be removed from diagram

This commit is contained in:
Claveau Joshua 2020-05-12 11:17:25 +02:00
parent 50f061697c
commit 0015f4a694
11 changed files with 154 additions and 52 deletions

View File

@ -17,7 +17,7 @@
*/
#include "renamedialog.h"
#include <utility>
#include <QDate>
#include "ui_renamedialog.h"
RenameDialog::RenameDialog(QString path, QWidget *parent) :

View File

@ -17,7 +17,7 @@
*/
#include "conductorproperties.h"
#include <QPainter>
#include <utility>
#include <QMetaEnum>
/**
Constructeur par defaut

View File

@ -20,6 +20,7 @@
#include "qet.h"
#include <QColor>
#include <QSettings>
class QPainter;

View File

@ -27,6 +27,7 @@
#include "diagram.h"
#include "terminal.h"
#include "conductortextitem.h"
#include "qetgraphicstableitem.h"
/**
* @brief DiagramContent::DiagramContent
@ -49,15 +50,15 @@ DiagramContent::DiagramContent(Diagram *diagram, bool selected) :
item_list = diagram->items();
}
for (QGraphicsItem *item : item_list)
for (auto item : item_list)
{
if (Element *elmt = qgraphicsitem_cast<Element *>(item))
m_elements << elmt;
else if (IndependentTextItem *iti = qgraphicsitem_cast<IndependentTextItem *>(item))
m_text_fields << iti;
else if (Conductor *c = qgraphicsitem_cast<Conductor *>(item))
switch (item->type())
{
case Element::Type: { m_elements << qgraphicsitem_cast<Element *>(item); break;}
case IndependentTextItem::Type: { m_text_fields << qgraphicsitem_cast<IndependentTextItem *>(item); break;}
case Conductor::Type:
{
auto c = qgraphicsitem_cast<Conductor *>(item);
//Get the isolated selected conductor (= not movable, but deletable)
if (!c->terminal1->parentItem()->isSelected() &&\
!c->terminal2->parentItem()->isSelected()) {
@ -66,22 +67,24 @@ DiagramContent::DiagramContent(Diagram *diagram, bool selected) :
if (m_potential_conductors.isEmpty()) {
m_potential_conductors << c;
}
else
{
if (!potentialIsManaged(c->relatedPotentialConductors(true).toList()))
} else {
if (!potentialIsManaged(c->relatedPotentialConductors(true).toList())) {
m_potential_conductors << c;
}
}
else if (DiagramImageItem *dii = qgraphicsitem_cast<DiagramImageItem *>(item))
m_images << dii;
else if (QetShapeItem *dsi = qgraphicsitem_cast<QetShapeItem *>(item))
m_shapes << dsi;
else if (DynamicElementTextItem *deti = qgraphicsitem_cast<DynamicElementTextItem *>(item))
m_element_texts << deti;
else if (QGraphicsItemGroup *group = qgraphicsitem_cast<QGraphicsItemGroup *>(item))
if(ElementTextItemGroup *etig = dynamic_cast<ElementTextItemGroup *>(group))
m_texts_groups << etig;
break;
}
case DiagramImageItem::Type: { m_images << qgraphicsitem_cast<DiagramImageItem *>(item); break;}
case QetShapeItem::Type: { m_shapes << qgraphicsitem_cast<QetShapeItem *>(item); break;}
case DynamicElementTextItem::Type: { m_element_texts << qgraphicsitem_cast<DynamicElementTextItem *>(item); break;}
case QGraphicsItemGroup::Type: {
if (auto *group = dynamic_cast<ElementTextItemGroup *>(item)) {
m_texts_groups << group;
}
break;
}
case QetGraphicsTableItem::Type: { m_tables << qgraphicsitem_cast<QetGraphicsTableItem *>(item); break;}
}
}
@ -129,7 +132,8 @@ DiagramContent::DiagramContent(const DiagramContent &other) :
m_potential_conductors(other.m_potential_conductors),
m_element_texts(other.m_element_texts),
m_texts_groups(other.m_texts_groups),
m_selected_items(other.m_selected_items)
m_selected_items(other.m_selected_items),
m_tables(other.m_tables)
{}
DiagramContent::~DiagramContent()
@ -211,7 +215,8 @@ bool DiagramContent::hasDeletableItems() const
qgi->type() == IndependentTextItem::Type ||
qgi->type() == QetShapeItem::Type ||
qgi->type() == DiagramImageItem::Type ||
qgi->type() == DynamicElementTextItem::Type)
qgi->type() == DynamicElementTextItem::Type ||
qgi->type() == QetGraphicsTableItem::Type)
return true;
if(qgi->type() == QGraphicsItemGroup::Type)
if(dynamic_cast<ElementTextItemGroup *>(qgi))
@ -250,6 +255,7 @@ void DiagramContent::clear()
m_element_texts.clear();
m_texts_groups.clear();
m_selected_items.clear();
m_tables.clear();
}
/**
@ -339,6 +345,10 @@ DiagramContent &DiagramContent::operator+=(const DiagramContent &other)
}
}
for (auto table : other.m_tables)
if (!m_tables.contains(table))
m_tables << table;
return *this;
}
@ -386,12 +396,13 @@ QList<QGraphicsItem *> DiagramContent::items(int filter) const
for(QGraphicsItem *qgi : conductors(filter)) items_list << qgi;
if (filter & Elements) for(QGraphicsItem *qgi : m_elements) items_list << qgi;
if (filter & TextFields) for(QGraphicsItem *qgi : m_text_fields) items_list << qgi;
if (filter & Images) for(QGraphicsItem *qgi : m_images) items_list << qgi;
if (filter & Shapes) for(QGraphicsItem *qgi : m_shapes) items_list << qgi;
if (filter & ElementTextFields) for(QGraphicsItem *qgi : m_element_texts) items_list << qgi;
if (filter & TextGroup) for(QGraphicsItem *qgi : m_texts_groups) items_list << qgi;
if (filter & Elements) for(auto qgi : m_elements) items_list << qgi;
if (filter & TextFields) for(auto qgi : m_text_fields) items_list << qgi;
if (filter & Images) for(auto qgi : m_images) items_list << qgi;
if (filter & Shapes) for(auto qgi : m_shapes) items_list << qgi;
if (filter & ElementTextFields) for(auto qgi : m_element_texts) items_list << qgi;
if (filter & TextGroup) for(auto qgi : m_texts_groups) items_list << qgi;
if (filter & Tables) for(auto qgi : m_tables) items_list << qgi;
if (filter & SelectedOnly) {
for(QGraphicsItem *qgi : items_list) {
@ -410,15 +421,16 @@ int DiagramContent::count(int filter) const
{
int count = 0;
if (filter & SelectedOnly) {
if (filter & Elements) for(Element *element : m_elements) { if (element -> isSelected()) ++ count; }
if (filter & TextFields) for(DiagramTextItem *dti : m_text_fields) { if (dti -> isSelected()) ++ count; }
if (filter & Images) for(DiagramImageItem *dii : m_images) { if (dii -> isSelected()) ++ count; }
if (filter & Shapes) for(QetShapeItem *dsi : m_shapes) { if (dsi -> isSelected()) ++ count; }
if (filter & ConductorsToMove) for(Conductor *conductor : m_conductors_to_move) { if (conductor -> isSelected()) ++ count; }
if (filter & ConductorsToUpdate) for(Conductor *conductor : m_conductors_to_update) { if (conductor -> isSelected()) ++ count; }
if (filter & OtherConductors) for(Conductor *conductor : m_other_conductors) { if (conductor -> isSelected()) ++ count; }
if (filter & ElementTextFields) for(DynamicElementTextItem *deti : m_element_texts) { if (deti -> isSelected()) ++ count; }
if (filter & TextGroup) for(ElementTextItemGroup *etig : m_texts_groups) { if (etig -> isSelected()) ++ count; }
if (filter & Elements) for(auto element : m_elements) { if (element -> isSelected()) ++ count; }
if (filter & TextFields) for(auto dti : m_text_fields) { if (dti -> isSelected()) ++ count; }
if (filter & Images) for(auto dii : m_images) { if (dii -> isSelected()) ++ count; }
if (filter & Shapes) for(auto dsi : m_shapes) { if (dsi -> isSelected()) ++ count; }
if (filter & ConductorsToMove) for(auto conductor : m_conductors_to_move) { if (conductor -> isSelected()) ++ count; }
if (filter & ConductorsToUpdate) for(auto conductor : m_conductors_to_update) { if (conductor -> isSelected()) ++ count; }
if (filter & OtherConductors) for(auto conductor : m_other_conductors) { if (conductor -> isSelected()) ++ count; }
if (filter & ElementTextFields) for(auto deti : m_element_texts) { if (deti -> isSelected()) ++ count; }
if (filter & TextGroup) for(auto etig : m_texts_groups) { if (etig -> isSelected()) ++ count; }
if (filter & Tables) for(auto table : m_tables) { if (table -> isSelected()) ++ count; }
}
else {
if (filter & Elements) count += m_elements.count();
@ -430,6 +442,7 @@ int DiagramContent::count(int filter) const
if (filter & OtherConductors) count += m_other_conductors.count();
if (filter & ElementTextFields) count += m_element_texts.count();
if (filter & TextGroup) count += m_texts_groups.count();
if (filter & Tables) count += m_tables.count();
}
return(count);
}
@ -448,6 +461,7 @@ QString DiagramContent::sentence(int filter) const
int images_count = (filter & Images) ? m_images.count() : 0;
int shapes_count = (filter & Shapes) ? m_shapes.count() : 0;
int elmt_text_count = (filter & ElementTextFields) ? m_element_texts.count() : 0;
int tables_count = (filter & Tables) ? m_tables.count() : 0;
return(
QET::ElementsAndConductorsSentence(
@ -456,7 +470,8 @@ QString DiagramContent::sentence(int filter) const
textfields_count,
images_count,
shapes_count,
elmt_text_count
elmt_text_count,
tables_count
)
);
}
@ -468,7 +483,7 @@ QString DiagramContent::sentence(int filter) const
* @return
*/
QDebug &operator<<(QDebug d, DiagramContent &content) {
Q_UNUSED(content);
Q_UNUSED(content)
d << "DiagramContent {" << "\n";
/*
FIXME Le double-heritage QObject / QGraphicsItem a casse cet operateur

View File

@ -23,6 +23,7 @@
#include <QAction>
#include <QFileInfo>
#include <QSaveFile>
#include <QTextStream>
/**
Permet de convertir une chaine de caracteres ("n", "s", "e" ou "w")
@ -237,7 +238,8 @@ bool QET::attributeIsAReal(const QDomElement &e, const QString& nom_attribut, qr
@return la proposition decrivant le nombre d'elements, de conducteurs et de
textes
*/
QString QET::ElementsAndConductorsSentence(int elements_count, int conductors_count, int texts_count, int images_count, int shapes_count, int element_text_count) {
QString QET::ElementsAndConductorsSentence(int elements_count, int conductors_count, int texts_count, int images_count, int shapes_count, int element_text_count, int tables_count)
{
QString text;
if (elements_count) {
text += QObject::tr(
@ -291,6 +293,14 @@ QString QET::ElementsAndConductorsSentence(int elements_count, int conductors_co
element_text_count);
}
if (tables_count) {
if (!text.isEmpty()) text += ", ";
text += QObject::tr(
"%n tableau(s)",
"part of a sentence listing the content of diagram",
tables_count);
}
return(text);
}

View File

@ -17,7 +17,9 @@
*/
#ifndef _QET_H
#define _QET_H
#include <QtXml>
#include <QDomElement>
#include <QFile>
#include <QObject>
class QActionGroup;
@ -160,7 +162,7 @@ namespace QET {
bool orthogonalProjection(const QPointF &, const QLineF &, QPointF * = nullptr);
bool attributeIsAnInteger(const QDomElement &, const QString& , int * = nullptr);
bool attributeIsAReal(const QDomElement &, const QString& , qreal * = nullptr);
QString ElementsAndConductorsSentence(int, int, int = 0, int = 0, int = 0, int = 0);
QString ElementsAndConductorsSentence(int elements=0, int conductors=0, int indi_texts=0, int images=0, int shapes=0, int element_text=0, int tables_count=0);
QList<QDomElement> findInDomElement(const QDomElement &, const QString &);
QList<QDomElement> findInDomElement(const QDomElement &, const QString &, const QString &);
QList<QChar> forbiddenCharacters();

View File

@ -544,6 +544,24 @@ bool QetGraphicsTableItem::sceneEventFilter(QGraphicsItem *watched, QEvent *even
return false;
}
/**
* @brief QetGraphicsTableItem::itemChange
* @param change
* @param value
* @return
*/
QVariant QetGraphicsTableItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{
//item was removed from scene, we remove the handler
if (change == ItemSceneHasChanged) {
if (!scene() && m_handler_item.scene()) {
m_handler_item.scene()->removeItem(&m_handler_item);
}
}
return QetGraphicsItem::itemChange(change, value);
}
void QetGraphicsTableItem::modelReseted() {
dataChanged(m_model->index(0,0), m_model->index(0,0), QVector<int>());
setToMinimumHeight();

View File

@ -83,6 +83,7 @@ class QetGraphicsTableItem : public QetGraphicsItem
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;
virtual bool sceneEventFilter(QGraphicsItem *watched, QEvent *event) override;
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
private:
void modelReseted();

View File

@ -19,6 +19,8 @@
#include "ui_aboutqetdialog.h"
#include "qet.h"
#include <QThread>
AboutQETDialog::AboutQETDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::AboutQETDialog)

View File

@ -25,6 +25,7 @@
#include "addelementtextcommand.h"
#include "terminal.h"
#include "diagramcommands.h"
#include "qetgraphicstableitem.h"
/**
* @brief DeleteQGraphicsItemCommand::DeleteQGraphicsItemCommand
@ -74,6 +75,15 @@ DeleteQGraphicsItemCommand::DeleteQGraphicsItemCommand(Diagram *diagram, const D
m_removed_contents.m_texts_groups.clear();
setPotentialsOfRemovedElements();
//Store some information about the tables
for (auto table : m_removed_contents.m_tables)
{
tableStatus status;
status.next = table->nextTable();
status.previous = table->previousTable();
m_tables_status.insert(table, status);
}
setText(QString(QObject::tr("supprimer %1", "undo caption - %1 is a sentence listing the removed content")).arg(m_removed_contents.sentence(DiagramContent::All)));
m_diagram->qgiManager().manage(m_removed_contents.items(DiagramContent::All));
}
@ -230,6 +240,27 @@ void DeleteQGraphicsItemCommand::undo()
}
}
for (auto table : m_removed_contents.m_tables)
{
auto pair = m_tables_status.value(table);
if(pair.next && pair.previous) // Table is between two tables
{
pair.next->setPreviousTable(nullptr);
table->setPreviousTable(pair.previous);
pair.next->setPreviousTable(table);
}
else if (pair.next) //Table is the first table of linked tables
{
auto model = pair.next->model();
pair.next->setPreviousTable(table);
table->setModel(model);
}
else if (pair.previous) { //Table is the last of linked tables
table->setPreviousTable(pair.previous);
}
}
QUndoCommand::undo();
}
@ -272,6 +303,20 @@ void DeleteQGraphicsItemCommand::redo()
deti->setParentItem(nullptr);
}
for (auto table : m_removed_contents.m_tables)
{
auto pair = m_tables_status.value(table);
if(pair.next && pair.previous) { // Table is between two tables
pair.next->setPreviousTable(pair.previous); //change the previous table of the current next table of @table
} else if (pair.next) { //Table is the first table of linked tables
pair.next->setPreviousTable(nullptr); //Next table haven't got model anymore
pair.next->setModel(table->model());
} else if (pair.previous) { //Table is the last of linked tables
table->setPreviousTable(nullptr); //Remove the previous table @table
}
}
for(QGraphicsItem *item : m_removed_contents.items())
m_diagram->removeItem(item);

View File

@ -24,9 +24,16 @@
class Diagram;
class ElementTextItemGroup;
class Terminal;
class QetGraphicsTableItem;
class DeleteQGraphicsItemCommand : public QUndoCommand
{
private :
struct tableStatus {
QetGraphicsTableItem *previous = nullptr;
QetGraphicsTableItem *next = nullptr;
};
public:
DeleteQGraphicsItemCommand(Diagram *diagram, const DiagramContent &content, QUndoCommand * parent = nullptr);
~DeleteQGraphicsItemCommand() override;
@ -49,6 +56,7 @@ class DeleteQGraphicsItemCommand : public QUndoCommand
QHash <DynamicElementTextItem *, Element *> m_elmt_text_hash; /// Keep the parent element of each deleted dynamic element text item
QHash <DynamicElementTextItem *, ElementTextItemGroup *> m_grp_texts_hash; ///Keep the parent group of each deleted element text item
QList <QPair<Terminal *, Terminal *>> m_connected_terminals;
QHash <QetGraphicsTableItem *, tableStatus> m_tables_status;
};
#endif // DELETEQGRAPHICSITEMCOMMAND_H