diff --git a/qelectrotech.pro b/qelectrotech.pro index f1ba0ed5a..18a806bdc 100644 --- a/qelectrotech.pro +++ b/qelectrotech.pro @@ -80,6 +80,7 @@ INCLUDEPATH += sources \ sources/ui \ sources/qetgraphicsitem \ sources/qetgraphicsitem/ViewItem \ + sources/qetgraphicsitem/ViewItem/ui \ sources/richtext \ sources/factory \ sources/properties \ @@ -110,6 +111,7 @@ HEADERS += $$files(sources/*.h) $$files(sources/ui/*.h) \ $$files(sources/richtext/*.h) \ $$files(sources/qetgraphicsitem/*.h) \ $$files(sources/qetgraphicsitem/ViewItem/*.h) \ + $$files(sources/qetgraphicsitem/ViewItem/ui/*.h) \ $$files(sources/factory/*.h) \ $$files(sources/properties/*.h) \ $$files(sources/editor/ui/*.h) \ @@ -137,6 +139,7 @@ SOURCES += $$files(sources/*.cpp) \ $$files(sources/ui/*.cpp) \ $$files(sources/qetgraphicsitem/*.cpp) \ $$files(sources/qetgraphicsitem/ViewItem/*.cpp) \ + $$files(sources/qetgraphicsitem/ViewItem/ui/*.cpp) \ $$files(sources/factory/*.cpp) \ $$files(sources/properties/*.cpp) \ $$files(sources/editor/ui/*.cpp) \ @@ -177,7 +180,8 @@ FORMS += $$files(sources/richtext/*.ui) \ $$files(sources/autoNum/ui/*.ui) \ $$files(sources/ui/configpage/*.ui) \ $$files(sources/SearchAndReplace/ui/*.ui) \ - $$files(sources/NameList/ui/*.ui) + $$files(sources/NameList/ui/*.ui) \ + $$files(sources/qetgraphicsitem/ViewItem/ui/*.ui) UI_SOURCES_DIR = sources/ui/ UI_HEADERS_DIR = sources/ui/ diff --git a/sources/qetdiagrameditor.cpp b/sources/qetdiagrameditor.cpp index ced39e30c..010cb619a 100644 --- a/sources/qetdiagrameditor.cpp +++ b/sources/qetdiagrameditor.cpp @@ -402,7 +402,7 @@ void QETDiagramEditor::setUpActions() }); //Add a nomenclature item - m_add_nomenclature = new QAction(QET::Icons::TableOfContent, tr("Ajouter une nomenclature"),this); + m_add_nomenclature = new QAction(QET::Icons::TableOfContent, tr("Ajouter un tableau lambda (Fonctionnalité en cours de devellopement)"),this); connect(m_add_nomenclature, &QAction::triggered, [this]() { if(this->currentDiagramView()) { this->currentDiagramView()->diagram()->addItem(new QetGraphicsTableItem()); diff --git a/sources/qetgraphicsitem/ViewItem/qetgraphicsheaderitem.cpp b/sources/qetgraphicsitem/ViewItem/qetgraphicsheaderitem.cpp index 4b9c2ba2e..ae47648d7 100644 --- a/sources/qetgraphicsitem/ViewItem/qetgraphicsheaderitem.cpp +++ b/sources/qetgraphicsitem/ViewItem/qetgraphicsheaderitem.cpp @@ -1,4 +1,4 @@ -/* +/* Copyright 2006-2019 QElectroTech Team This file is part of QElectroTech. @@ -40,8 +40,16 @@ QetGraphicsHeaderItem::QetGraphicsHeaderItem(QGraphicsItem *parent) : */ void QetGraphicsHeaderItem::setModel(QAbstractItemModel *model) { + if (m_model) { + disconnect(m_model, &QAbstractItemModel::headerDataChanged, this, &QetGraphicsHeaderItem::headerDataChanged); + } + m_model = model; - reset(); + connect(m_model, &QAbstractItemModel::headerDataChanged, this, &QetGraphicsHeaderItem::headerDataChanged); + setUpMinimumSectionsSize(); + m_current_sections_width.clear(); + m_current_sections_width.resize(m_sections_minimum_width.size()); + adjustSize(); } /** @@ -52,14 +60,6 @@ QAbstractItemModel *QetGraphicsHeaderItem::model() const { return m_model; } -/** - * @brief QetGraphicsHeaderItem::reset - * Reset the internal state of the item - */ -void QetGraphicsHeaderItem::reset() { - setUpMinimumSectionsSize(); -} - /** * @brief QetGraphicsHeaderItem::boundingRect * Reimplemented from QGraphicsObject::boundingRect() const; @@ -93,7 +93,7 @@ void QetGraphicsHeaderItem::paint(QPainter *painter, const QStyleOptionGraphicsI painter->setBrush(brush); painter->setPen(pen); - painter->setFont(m_font); + painter->setFont(m_model->headerData(0, Qt::Horizontal, Qt::FontRole).value()); painter->drawRect(m_current_rect); if (!m_model) @@ -116,9 +116,12 @@ void QetGraphicsHeaderItem::paint(QPainter *painter, const QStyleOptionGraphicsI QPointF top_left(m_margin.left(), m_margin.top()); for (auto i= 0 ; icolumnCount() ; ++i) { - QSize size(m_current_sections_width.at(i), m_section_height - m_margin.top() - m_margin.bottom()); - painter->drawText(QRectF(top_left, size), Qt::AlignCenter, m_model->headerData(i, Qt::Horizontal).toString()); - top_left.setX(top_left.x() + size.width()); + QSize size(m_current_sections_width.at(i) - m_margin.left() - m_margin.right(), m_section_height - m_margin.top() - m_margin.bottom()); + painter->drawText(QRectF(top_left, size), + m_model->headerData(0, Qt::Horizontal, Qt::TextAlignmentRole).toInt(), + m_model->headerData(i, Qt::Horizontal).toString()); + + top_left.setX(top_left.x() + m_current_sections_width.at(i)); } painter->restore(); @@ -132,14 +135,28 @@ QRect QetGraphicsHeaderItem::rect() const { return m_current_rect; } +/** + * @brief QetGraphicsHeaderItem::resizeSection + * @param logicalIndex + * @param size + */ void QetGraphicsHeaderItem::resizeSection(int logicalIndex, int size) -{ - if (m_model && logicalIndexcolumnCount()) +{ + if (logicalIndex >= m_current_sections_width.size() || + m_current_sections_width.at(logicalIndex) == size) { + return; + } + + if (m_model && + logicalIndexcolumnCount() && + size >= m_sections_minimum_width.at(logicalIndex)) { prepareGeometryChange(); m_current_sections_width.replace(logicalIndex, size); m_current_rect.setWidth(std::accumulate(m_current_sections_width.begin(), m_current_sections_width.end(), 0)); setUpBoundingRect(); + update(); + emit sectionResized(logicalIndex, size); } } @@ -157,9 +174,20 @@ int QetGraphicsHeaderItem::sectionSize(int logical_index) const } } +/** + * @brief QetGraphicsHeaderItem::setMargins + * @param margins + */ +void QetGraphicsHeaderItem::setMargins(const QMargins &margins) +{ + m_margin = margins; + headerDataChanged(Qt::Horizontal, 0,1); +} + /** * @brief QetGraphicsHeaderItem::setUpMinimumSectionsSize - * Setup the minimum section size of the item + * Setup the minimum section size and height of the item. + * Not that this function doesn't change the current size of this item. */ void QetGraphicsHeaderItem::setUpMinimumSectionsSize() { @@ -167,9 +195,9 @@ void QetGraphicsHeaderItem::setUpMinimumSectionsSize() return; } - QFontMetrics metrics(m_font); + QFontMetrics metrics(m_model->headerData(0, Qt::Horizontal, Qt::FontRole).value()); //Set the height of row; - m_section_height = metrics.boundingRect("HEIGHT TEST").height() + m_margin.top() + m_margin.bottom(); + m_minimum_section_height = metrics.boundingRect("HEIGHT TEST").height() + m_margin.top() + m_margin.bottom(); m_sections_minimum_width.clear(); m_sections_minimum_width.resize(m_model->columnCount()); @@ -180,12 +208,7 @@ void QetGraphicsHeaderItem::setUpMinimumSectionsSize() m_sections_minimum_width.replace(i, metrics.boundingRect(str).width() + m_margin.left() + m_margin.right()); } - m_current_sections_width = m_sections_minimum_width; - - m_minimum_rect.setRect(0,0, std::accumulate(m_sections_minimum_width.begin(), m_sections_minimum_width.end(), 0), m_section_height); - m_current_rect = m_minimum_rect; - - setUpBoundingRect(); + m_minimum_width = std::accumulate(m_sections_minimum_width.begin(), m_sections_minimum_width.end(), 0); } /** @@ -195,3 +218,49 @@ void QetGraphicsHeaderItem::setUpMinimumSectionsSize() void QetGraphicsHeaderItem::setUpBoundingRect() { m_bounding_rect = m_current_rect.adjusted(-10, -10, 10, 10); } + +/** + * @brief QetGraphicsHeaderItem::headerDataChanged + * Update the header when data of displayed model change + * @param orientation + * @param first + * @param last + */ +void QetGraphicsHeaderItem::headerDataChanged(Qt::Orientations orientation, int first, int last) +{ + Q_UNUSED(orientation) + Q_UNUSED(first) + Q_UNUSED(last) + + setUpMinimumSectionsSize(); + adjustSize(); +} + +/** + * @brief QetGraphicsHeaderItem::adjustSize + * If needed, this function resize the current height and section + * according to there minimum + */ +void QetGraphicsHeaderItem::adjustSize() +{ + if (m_section_height != m_minimum_section_height) + { + m_section_height = m_minimum_section_height; + m_current_rect.setHeight(m_section_height); + emit heightResized(); + } + + if(m_current_sections_width.size() == m_sections_minimum_width.size()) + { + auto old_sections_width = m_current_sections_width; + + for (int i=0 ; i minimumSectionWidth() const {return m_sections_minimum_width;} + int minimumWidth() const {return m_minimum_width;} + + signals: + void sectionResized(int logicalIndex, int size); + void heightResized(); private: void setUpMinimumSectionsSize(); void setUpBoundingRect(); + void headerDataChanged(Qt::Orientations orientation, int first, int last); + void adjustSize(); private: QRectF m_bounding_rect; QAbstractItemModel *m_model = nullptr; - QFont m_font = QETApp::diagramTextsFont(); QMargins m_margin; QVector m_sections_minimum_width, m_current_sections_width; - int m_section_height=1; - QRect m_minimum_rect, - m_current_rect; + int m_section_height=1, + m_minimum_section_height=1; + int m_minimum_width=1; + QRect m_current_rect; }; #endif // QETGRAPHICSHEADERITEM_H diff --git a/sources/qetgraphicsitem/ViewItem/qetgraphicstableitem.cpp b/sources/qetgraphicsitem/ViewItem/qetgraphicstableitem.cpp index edfb7e19d..7be9155f8 100644 --- a/sources/qetgraphicsitem/ViewItem/qetgraphicstableitem.cpp +++ b/sources/qetgraphicsitem/ViewItem/qetgraphicstableitem.cpp @@ -1,5 +1,5 @@ /* - Copyright 2006-2019 QElectroTech Team + Copyright 2006-2020 QElectroTech Team This file is part of QElectroTech. QElectroTech is free software: you can redistribute it and/or modify @@ -18,6 +18,7 @@ #include "qetgraphicstableitem.h" #include "diagram.h" #include "qetgraphicsheaderitem.h" +#include "QPropertyUndoCommand/qpropertyundocommand.h" #include #include @@ -47,12 +48,14 @@ QetGraphicsTableItem::QetGraphicsTableItem(QGraphicsItem *parent) : connect(this, &QetGraphicsTableItem::yChanged, this, &QetGraphicsTableItem::adjustHandlerPos); m_header_item = new QetGraphicsHeaderItem(this); - - + connect(m_header_item, &QetGraphicsHeaderItem::sectionResized, this, &QetGraphicsTableItem::headerSectionResized); + connect(m_header_item, &QetGraphicsHeaderItem::heightResized, this, [this]() { + m_header_item->setPos(0, 0-m_header_item->rect().height()); + }); /*******ONLY FOR TEST DURING DEVEL*********/ auto model = new QStandardItemModel(this); - int r = 10; + int r = 20; int c = 5; for (int row = 0; row < r; ++row) @@ -62,6 +65,10 @@ QetGraphicsTableItem::QetGraphicsTableItem(QGraphicsItem *parent) : model->setItem(row, column, item); } } + model->setData(model->index(0,0), Qt::AlignLeft, Qt::TextAlignmentRole); + model->setData(model->index(0,0), QETApp::diagramTextsFont(), Qt::FontRole); + model->setHeaderData(0, Qt::Horizontal, Qt::AlignHCenter, Qt::TextAlignmentRole); + model->setHeaderData(0, Qt::Horizontal, QETApp::diagramTextsFont(), Qt::FontRole); model->setHeaderData(0, Qt::Horizontal, "Label"); model->setHeaderData(1, Qt::Horizontal, "Folio"); model->setHeaderData(2, Qt::Horizontal, "Fonction"); @@ -69,11 +76,11 @@ QetGraphicsTableItem::QetGraphicsTableItem(QGraphicsItem *parent) : model->setHeaderData(4, Qt::Horizontal, "Installation"); this->setModel(model); this->setPos(50,50); + /******************************************/ } QetGraphicsTableItem::~QetGraphicsTableItem() -{ -} +{} /** * @brief QetGraphicsTableItem::setModel @@ -84,12 +91,17 @@ QetGraphicsTableItem::~QetGraphicsTableItem() */ void QetGraphicsTableItem::setModel(QAbstractItemModel *model) { + if (m_model) { + disconnect(m_model, &QAbstractItemModel::dataChanged, this, &QetGraphicsTableItem::dataChanged); + } m_model = model; - reset(); - m_header_item->setModel(model); - adjustColumnsWidth(); + + setUpColumnAndRowMinimumSize(); + adjustSize(); + m_header_item->setPos(0, -m_header_item->rect().height()); + connect(m_model, &QAbstractItemModel::dataChanged, this, &QetGraphicsTableItem::dataChanged); } /** @@ -100,17 +112,6 @@ QAbstractItemModel *QetGraphicsTableItem::model() const { return m_model; } -/** - * @brief QetGraphicsTableItem::reset - * Reset the internal state of the item - */ -void QetGraphicsTableItem::reset() -{ - setUpColumnAndRowMinimumSize(); - setUpBoundingRect(); - adjustColumnsWidth(); -} - /** * @brief QetGraphicsTableItem::boundingRect * Reimplemented from QGraphicsObject @@ -138,9 +139,9 @@ void QetGraphicsTableItem::paint(QPainter *painter, const QStyleOptionGraphicsIt pen.setWidthF(0.7); pen.setColor(Qt::black); painter->setPen(pen); - painter->setFont(m_font); + painter->setFont(m_model->data(model()->index(0,0), Qt::FontRole).value()); - painter->drawRect(m_current_rect); + painter->drawRect(0,0, m_header_item->rect().width(), m_current_size.height()); if(!m_model) { @@ -152,25 +153,25 @@ void QetGraphicsTableItem::paint(QPainter *painter, const QStyleOptionGraphicsIt auto offset= 0; for(auto i=0 ; icolumnCount() ; ++i) { - QPointF p1(offset+m_header_item->sectionSize(i), m_current_rect.top()); - QPointF p2(offset+m_header_item->sectionSize(i), m_current_rect.bottom()); + QPointF p1(offset+m_header_item->sectionSize(i), 0); + QPointF p2(offset+m_header_item->sectionSize(i), m_current_size.height()); painter->drawLine(p1, p2); offset += m_header_item->sectionSize(i); } //Draw horizontal lines - auto cell_height = m_current_rect.height()/m_model->rowCount(); + auto cell_height = static_cast(m_current_size.height())/static_cast(m_model->rowCount()); for(auto i= 1 ; i-1rowCount() ; ++i) { - QPointF p1(m_current_rect.left(), cell_height*i); - QPointF p2(m_current_rect.right(), cell_height*i); + QPointF p1(m_header_item->rect().left(), cell_height*i); + QPointF p2(m_header_item->rect().right(), cell_height*i); painter->drawLine(p1, p2); } //Write text of each cell for (auto i= 0 ; irowCount() ; ++i) { - QPointF top_left(m_margin.left(), i==0? 0 : cell_height*i); + QPointF top_left(m_margin.left(), i==0? m_margin.top() : cell_height*i + m_margin.top()); for(auto j= 0 ; jcolumnCount() ; ++j) { @@ -178,15 +179,71 @@ void QetGraphicsTableItem::paint(QPainter *painter, const QStyleOptionGraphicsIt if (j>0) { top_left.setX(top_left.x() + m_header_item->sectionSize(j-1)); } - QSize size(m_header_item->sectionSize(j), - cell_height - m_margin.top() - m_margin.bottom()); - painter->drawText(QRectF(top_left, size), Qt::AlignVCenter|Qt::AlignLeft, m_model->index(i, j).data().toString()); + QSize size(m_header_item->sectionSize(j) - m_margin.left() - m_margin.right(), + static_cast(cell_height) - m_margin.top() - m_margin.bottom()); + painter->drawText(QRectF(top_left, size), + m_model->data(m_model->index(0,0), Qt::TextAlignmentRole).toInt(), + m_model->index(i, j).data().toString()); } } painter->restore(); } +/** + * @brief QetGraphicsTableItem::setMargins + * @param margins + */ +void QetGraphicsTableItem::setMargins(const QMargins &margins) +{ + m_margin = margins; + setUpColumnAndRowMinimumSize(); + adjustSize(); + update(); +} + +/** + * @brief QetGraphicsTableItem::setSize + * Set the current size of the table to @size + * @param size + */ +void QetGraphicsTableItem::setSize(const QSize &size) +{ + auto new_size = size; + if (new_size.width() < minimumSize().width()) { + new_size.setWidth(minimumSize().width()); + } + if (new_size.height() < minimumSize().height()) { + new_size.setHeight(minimumSize().height()); + } + + prepareGeometryChange(); + m_current_size = new_size; + adjustColumnsWidth(); + setUpBoundingRect(); +} + +/** + * @brief QetGraphicsTableItem::size + * @return The current size of the table + */ +QSize QetGraphicsTableItem::size() const +{ + QSize size_(m_header_item->rect().width(), m_current_size.height()); + return size_; +} + +/** + * @brief QetGraphicsTableItem::minimumSize + * @return the minimum size the table can be + * The returned size take care of the table's minimum width, but also the header item's minimum width + */ +QSize QetGraphicsTableItem::minimumSize() const +{ + QSize s(std::accumulate(m_minimum_column_width.begin(), m_minimum_column_width.end(), 0), m_minimum_row_height*m_model->rowCount()); + return s; +} + /** * @brief QetGraphicsTableItem::hoverEnterEvent * Reimplemented from QetGraphicsItem @@ -245,6 +302,7 @@ bool QetGraphicsTableItem::sceneEventFilter(QGraphicsItem *watched, QEvent *even /** * @brief QetGraphicsTableItem::setUpColumnAndRowMinimumSize * Calcule the minimum row height and the minimum column width for each columns + * this function doesn't change the geometry of the table. */ void QetGraphicsTableItem::setUpColumnAndRowMinimumSize() { @@ -252,48 +310,40 @@ void QetGraphicsTableItem::setUpColumnAndRowMinimumSize() return; } - QFontMetrics metrics(m_font); + QFontMetrics metrics(m_model->data(model()->index(0,0), Qt::FontRole).value()); //Set the height of row; - m_row_height = metrics.boundingRect("HEIGHT TEST").height() + m_margin.top() + m_margin.bottom(); + m_minimum_row_height = metrics.boundingRect("HEIGHT TEST").height() + m_margin.top() + m_margin.bottom(); - m_minimum_column_width.clear(); - m_minimum_column_width.resize(m_model->columnCount()); + m_minimum_column_width = m_header_item->minimumSectionWidth(); //Get the maximum width of each columns - for (auto i= 0 ; irowCount() ; ++i) + for (auto row= 0 ; rowrowCount() ; ++row) { - for(auto j= 0 ; jcolumnCount() ; ++j) + for(auto col= 0 ; colcolumnCount() ; ++col) { - auto index = m_model->index(i, j); + auto index = m_model->index(row, col); auto width = metrics.boundingRect(index.data().toString()).width(); - m_minimum_column_width.replace(j, std::max(m_minimum_column_width.at(j), width+m_margin.left() + m_margin.right())); + m_minimum_column_width.replace(col, std::max(m_minimum_column_width.at(col), width + m_margin.left() + m_margin.right())); } } - - //Set the minimum size of the table - m_minimum_rect.setRect(0, 0, - std::accumulate(m_minimum_column_width.begin(), m_minimum_column_width.end(), 0), - m_row_height*m_model->rowCount()); - - m_current_rect = m_minimum_rect; } /** * @brief QetGraphicsTableItem::setUpBoundingRect */ -void QetGraphicsTableItem::setUpBoundingRect() { - m_bounding_rect = m_current_rect.adjusted(-m_br_margin, -m_br_margin, m_br_margin, m_br_margin); +void QetGraphicsTableItem::setUpBoundingRect() +{ + QSize header_size = m_header_item->rect().size(); + QRect rect(0, -header_size.height(), header_size.width(), m_current_size.height() + header_size.height()); + m_bounding_rect = rect.adjusted(-m_br_margin, -m_br_margin, m_br_margin, m_br_margin); } /** * @brief QetGraphicsTableItem::adjustHandlerPos * Adjust the pos of the handler item */ -void QetGraphicsTableItem::adjustHandlerPos() -{ - if(m_handler_item.scene()) { - m_handler_item.setPos(mapToScene(m_current_rect.bottomRight())); - } +void QetGraphicsTableItem::adjustHandlerPos() { + m_handler_item.setPos(mapToScene(QRect(QPoint(0,0), size()).bottomRight())); } /** @@ -305,31 +355,41 @@ void QetGraphicsTableItem::setUpHandler() m_handler_item.setZValue(this->zValue() + 1); } -void QetGraphicsTableItem::handlerMousePressEvent(QGraphicsSceneMouseEvent *event) { +void QetGraphicsTableItem::handlerMousePressEvent(QGraphicsSceneMouseEvent *event) +{ Q_UNUSED(event) + m_old_size = size(); + //User start to resize the table, disconnect the signal to avoid double paint. + disconnect(m_header_item, &QetGraphicsHeaderItem::sectionResized, this, &QetGraphicsTableItem::headerSectionResized); } void QetGraphicsTableItem::handlerMouseMoveEvent(QGraphicsSceneMouseEvent *event) { auto new_handler_pos = Diagram::snapToGrid(event->scenePos()); - QSize size = QRectF(m_current_rect.topLeft(), mapFromScene(new_handler_pos)).size().toSize(); + QSize size_ = QRectF(QPointF(0,0), mapFromScene(new_handler_pos)).size().toSize(); - m_handler_item.setPos(mapToScene(std::max(m_minimum_rect.width(), size.width()), - std::max(m_minimum_rect.height(), size.height()))); + QPoint new_pos(std::max(minimumSize().width(), size_.width()), + std::max(minimumSize().height(), size_.height())); + m_handler_item.setPos(mapToScene(new_pos)); - QRect new_rect = QRectF(QPointF(0,0), mapFromScene(m_handler_item.scenePos())).toRect(); - - if (new_rect != m_current_rect) - { - prepareGeometryChange(); - m_current_rect = new_rect; - setUpBoundingRect(); - adjustColumnsWidth(); + QSize new_size(new_pos.x(), new_pos.y()); + if (new_size != size()) { + setSize(new_size); } } -void QetGraphicsTableItem::handlerMouseReleaseEvent(QGraphicsSceneMouseEvent *event) { +void QetGraphicsTableItem::handlerMouseReleaseEvent(QGraphicsSceneMouseEvent *event) +{ Q_UNUSED(event) + if (diagram()) + { + auto undo = new QPropertyUndoCommand(this, "size", m_old_size, size()); + undo->setAnimated(); + undo->setText(tr("Modifier la géometrie d'un tableau")); + diagram()->undoStack().push(undo); + } + //User finish to resize the table, we can reconnect now + connect(m_header_item, &QetGraphicsHeaderItem::sectionResized, this, &QetGraphicsTableItem::headerSectionResized); } /** @@ -338,11 +398,62 @@ void QetGraphicsTableItem::handlerMouseReleaseEvent(QGraphicsSceneMouseEvent *ev */ void QetGraphicsTableItem::adjustColumnsWidth() { - auto minimum_width = std::accumulate(m_minimum_column_width.begin(), m_minimum_column_width.end(), 0); - auto a = m_current_rect.width() - minimum_width; + auto a = m_current_size.width() - minimumSize().width(); auto b = a/m_model->columnCount(); - for(auto i= 0 ; iresizeSection(i, m_minimum_column_width.at(i) + b); + for(auto i= 0 ; icolumnCount() ; ++i) { + m_header_item->resizeSection(i, std::max(m_minimum_column_width.at(i), m_header_item->minimumSectionWidth().at(i)) + b); + } +} + +void QetGraphicsTableItem::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector &roles) +{ + Q_UNUSED(topLeft) + Q_UNUSED(bottomRight) + Q_UNUSED(roles) + + auto size_ = size(); + setUpColumnAndRowMinimumSize(); + adjustSize(); + setSize(size_); +} + +/** + * @brief QetGraphicsTableItem::headerSectionResized + * Connected to the header signal QetGraphicsTableItem sectionResized + */ +void QetGraphicsTableItem::headerSectionResized() +{ + auto header_size = m_header_item->rect().size(); + auto size_ = size(); + size_.setWidth(header_size.width()); + + m_current_size = size_; + prepareGeometryChange(); + setUpBoundingRect(); +} + +/** + * @brief QetGraphicsTableItem::adjustSize + * If needed, this function resize the current height and width of table + * according to there minimum + */ +void QetGraphicsTableItem::adjustSize() +{ + if (m_current_size.height() < minimumSize().height()) + { + prepareGeometryChange(); + m_current_size.setHeight(minimumSize().height()); + setUpBoundingRect(); + update(); + } + + if (m_current_size.width() < minimumSize().width()) + { + prepareGeometryChange(); + m_current_size.setWidth(minimumSize().width()); + adjustColumnsWidth(); + setUpBoundingRect(); + update(); } } diff --git a/sources/qetgraphicsitem/ViewItem/qetgraphicstableitem.h b/sources/qetgraphicsitem/ViewItem/qetgraphicstableitem.h index 8fe6a9069..96df2db67 100644 --- a/sources/qetgraphicsitem/ViewItem/qetgraphicstableitem.h +++ b/sources/qetgraphicsitem/ViewItem/qetgraphicstableitem.h @@ -1,5 +1,5 @@ /* - Copyright 2006-2019 QElectroTech Team + Copyright 2006-2020 QElectroTech Team This file is part of QElectroTech. QElectroTech is free software: you can redistribute it and/or modify @@ -27,8 +27,22 @@ class QAbstractItemModel; class QetGraphicsHeaderItem; +/** + * @brief The QetGraphicsTableItem class + * This item display a table destined to represent the content of a QAbstractItemModel + * The table have a few parameters to edit her visual aspect. + * Margins, to edit the margin between the cell and the text. + * Text font. + * Text alignment in the cell + * These two last parameters are not settable directly with the table but trough the model to be displayed by the table. + */ class QetGraphicsTableItem : public QetGraphicsItem { + Q_OBJECT + + Q_PROPERTY(QMargins margins READ margins WRITE setMargins) + Q_PROPERTY(QSize size READ size WRITE setSize) + public: QetGraphicsTableItem(QGraphicsItem *parent= nullptr); virtual ~QetGraphicsTableItem() override; @@ -38,10 +52,15 @@ class QetGraphicsTableItem : public QetGraphicsItem void setModel(QAbstractItemModel *model); QAbstractItemModel *model() const; - void reset(); virtual QRectF boundingRect() const override; virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override; + QMargins margins() const {return m_margin;} + void setMargins(const QMargins &margins); + QetGraphicsHeaderItem *headerItem() const {return m_header_item;} + void setSize(const QSize &size); + QSize size() const; + QSize minimumSize() const; protected: virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override; @@ -58,20 +77,21 @@ class QetGraphicsTableItem : public QetGraphicsItem void handlerMouseMoveEvent (QGraphicsSceneMouseEvent *event); void handlerMouseReleaseEvent (QGraphicsSceneMouseEvent *event); void adjustColumnsWidth(); + void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector &roles); + void headerSectionResized(); + void adjustSize(); private: QAbstractItemModel *m_model= nullptr; - QFont m_font = QETApp::diagramTextsFont(); - QVector m_minimum_column_width; - - int m_row_height; QMargins m_margin; - QRect m_minimum_rect, - m_current_rect; + QVector m_minimum_column_width; + int m_minimum_row_height; + QSize m_current_size, + m_old_size; - QRectF m_bounding_rect; int m_br_margin= 10; + QRectF m_bounding_rect; QetGraphicsHandlerItem m_handler_item; QetGraphicsHeaderItem *m_header_item = nullptr; diff --git a/sources/qetgraphicsitem/ViewItem/ui/graphicstablepropertieseditor.cpp b/sources/qetgraphicsitem/ViewItem/ui/graphicstablepropertieseditor.cpp new file mode 100644 index 000000000..f1aa0e309 --- /dev/null +++ b/sources/qetgraphicsitem/ViewItem/ui/graphicstablepropertieseditor.cpp @@ -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 . +*/ +#include "graphicstablepropertieseditor.h" +#include "ui_graphicstablepropertieseditor.h" +#include "qetgraphicstableitem.h" +#include "qetgraphicsheaderitem.h" +#include "diagram.h" +#include "QPropertyUndoCommand/qpropertyundocommand.h" +#include "itemmodelcommand.h" + +#include +#include + +/** + * @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(), + 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(); + 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::of(&QSpinBox::valueChanged), this, &GraphicsTablePropertiesEditor::apply); + m_edit_connection << connect(ui->m_y_pos, QOverload::of(&QSpinBox::valueChanged), this, &GraphicsTablePropertiesEditor::apply); + m_edit_connection << connect(ui->m_header_top_margin, QOverload::of(&QSpinBox::valueChanged), this, &GraphicsTablePropertiesEditor::apply); + m_edit_connection << connect(ui->m_header_left_margin, QOverload::of(&QSpinBox::valueChanged), this, &GraphicsTablePropertiesEditor::apply); + m_edit_connection << connect(ui->m_header_right_margin, QOverload::of(&QSpinBox::valueChanged), this, &GraphicsTablePropertiesEditor::apply); + m_edit_connection << connect(ui->m_header_bottom_margin, QOverload::of(&QSpinBox::valueChanged), this, &GraphicsTablePropertiesEditor::apply); + m_edit_connection << connect(ui->m_table_top_margin, QOverload::of(&QSpinBox::valueChanged), this, &GraphicsTablePropertiesEditor::apply); + m_edit_connection << connect(ui->m_table_left_margin, QOverload::of(&QSpinBox::valueChanged), this, &GraphicsTablePropertiesEditor::apply); + m_edit_connection << connect(ui->m_table_right_margin, QOverload::of(&QSpinBox::valueChanged), this, &GraphicsTablePropertiesEditor::apply); + m_edit_connection << connect(ui->m_table_bottom_margin, QOverload::of(&QSpinBox::valueChanged), this, &GraphicsTablePropertiesEditor::apply); + m_edit_connection << connect(m_table_button_group, QOverload::of(&QButtonGroup::buttonClicked), this, &GraphicsTablePropertiesEditor::apply); + m_edit_connection << connect(m_header_button_group, QOverload::of(&QButtonGroup::buttonClicked), this, &GraphicsTablePropertiesEditor::apply); + } +} diff --git a/sources/qetgraphicsitem/ViewItem/ui/graphicstablepropertieseditor.h b/sources/qetgraphicsitem/ViewItem/ui/graphicstablepropertieseditor.h new file mode 100644 index 000000000..f3b770eab --- /dev/null +++ b/sources/qetgraphicsitem/ViewItem/ui/graphicstablepropertieseditor.h @@ -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 . +*/ +#ifndef GRAPHICSTABLEPROPERTIESEDITOR_H +#define GRAPHICSTABLEPROPERTIESEDITOR_H + +#include "PropertiesEditor/propertieseditorwidget.h" +#include + +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 m_table_item; + QList m_connect_list, + m_edit_connection; + QButtonGroup *m_header_button_group = nullptr, + *m_table_button_group = nullptr; +}; + +Q_DECLARE_METATYPE(QMargins) + +#endif // GRAPHICSTABLEPROPERTIESEDITOR_H diff --git a/sources/qetgraphicsitem/ViewItem/ui/graphicstablepropertieseditor.ui b/sources/qetgraphicsitem/ViewItem/ui/graphicstablepropertieseditor.ui new file mode 100644 index 000000000..e7f53285d --- /dev/null +++ b/sources/qetgraphicsitem/ViewItem/ui/graphicstablepropertieseditor.ui @@ -0,0 +1,253 @@ + + + GraphicsTablePropertiesEditor + + + + 0 + 0 + 331 + 484 + + + + Form + + + + + + Position + + + + + + X : + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + 10000 + + + + + + + Y : + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + 10000 + + + + + + + + + + En tête + + + + + + + + Marge + + + Qt::AlignCenter + + + + + + + + + + + + + + + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Aligement : + + + + + + + Gauche + + + + + + + Centré + + + + + + + Droite + + + + + + + + + + Police + + + + + + + + + + Tableau + + + + + + + + + + + + + + + + + + + + Marge + + + Qt::AlignCenter + + + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Alignement : + + + + + + + Gauche + + + + + + + Centré + + + + + + + Droite + + + + + + + + + + Police + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + diff --git a/sources/ui/diagrampropertieseditordockwidget.cpp b/sources/ui/diagrampropertieseditordockwidget.cpp index f4df9c7e0..cca4cab86 100644 --- a/sources/ui/diagrampropertieseditordockwidget.cpp +++ b/sources/ui/diagrampropertieseditordockwidget.cpp @@ -27,6 +27,8 @@ #include "elementtextitemgroup.h" #include "independenttextitem.h" #include "inditextpropertieswidget.h" +#include "qetgraphicstableitem.h" +#include "graphicstablepropertieseditor.h" /** * @brief DiagramPropertiesEditorDockWidget::DiagramPropertiesEditorDockWidget @@ -230,6 +232,20 @@ void DiagramPropertiesEditorDockWidget::selectionChanged() } break; } + case QetGraphicsTableItem::Type: + { + if (count_ > 1) + { + clear(); + m_edited_qgi_type = -1; + return; + } + + clear(); + m_edited_qgi_type = type_; + addEditor(new GraphicsTablePropertiesEditor(static_cast(item), this)); + break; + } default: m_edited_qgi_type = -1; clear(); diff --git a/sources/undocommand/itemmodelcommand.cpp b/sources/undocommand/itemmodelcommand.cpp new file mode 100644 index 000000000..e12f64a2f --- /dev/null +++ b/sources/undocommand/itemmodelcommand.cpp @@ -0,0 +1,120 @@ +/* + Copyright 2006-2020 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 . +*/ +#include "itemmodelcommand.h" + +/** + * @brief ModelIndexCommand::ModelIndexCommand + * @param model + * @param index + * @param parent + */ +ModelIndexCommand::ModelIndexCommand(QAbstractItemModel *model, const QModelIndex &index, QUndoCommand *parent): + QUndoCommand(parent), + m_model(model), + m_index(index) +{} + +/** + * @brief ModelIndexCommand::setData + * @param value + * @param role + */ +void ModelIndexCommand::setData(const QVariant &value, int role) +{ + m_new_value = value; + if (m_model) { + m_old_value = m_model->data(m_index, role); + } else { + m_old_value = value; + } + + m_role = role; +} + +/** + * @brief ModelIndexCommand::redo + * Reimplemented from QUndoCommand + */ +void ModelIndexCommand::redo() { + if (m_model && m_index.isValid()) { + m_model->setData(m_index, m_new_value, m_role); + } +} + +/** + * @brief ModelIndexCommand::undo + * Reimplemented from QUndoCommand + */ +void ModelIndexCommand::undo() { + if (m_model && m_index.isValid()) { + m_model->setData(m_index, m_old_value, m_role); + } +} + +/** + * @brief ModelHeaderDataCommand::ModelHeaderDataCommand + * @param model + * @param parent + */ +ModelHeaderDataCommand::ModelHeaderDataCommand(QAbstractItemModel *model, QUndoCommand *parent) : + QUndoCommand(parent), + m_model(model) +{} + +/** + * @brief ModelHeaderDataCommand::setData + * See QAbstractItemModel::setHeaderData + * @param section + * @param orientation + * @param value + * @param role + */ +void ModelHeaderDataCommand::setData(int section, Qt::Orientation orientation, const QVariant &value, int role) +{ + m_section = section; + m_orientation = orientation; + m_new_value = value; + if (m_model) { + m_old_value = m_model->headerData(section, orientation, role); + } else { + m_old_value = m_new_value; + } + m_role = role; +} + +/** + * @brief ModelHeaderDataCommand::redo + * Reimplemented from QUndoCommand + */ +void ModelHeaderDataCommand::redo() +{ + if (m_model) { + m_model->setHeaderData(m_section, m_orientation, m_new_value, m_role); + } +} + +/** + * @brief ModelHeaderDataCommand::undo + * Reimplemented from QUndoCommand + */ +void ModelHeaderDataCommand::undo() +{ + if (m_model) { + m_model->setHeaderData(m_section, m_orientation, m_old_value, m_role); + } +} diff --git a/sources/undocommand/itemmodelcommand.h b/sources/undocommand/itemmodelcommand.h new file mode 100644 index 000000000..b66e16e80 --- /dev/null +++ b/sources/undocommand/itemmodelcommand.h @@ -0,0 +1,70 @@ +/* + Copyright 2006-2020 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 . +*/ +#ifndef ITEMMODELCOMMAND_H +#define ITEMMODELCOMMAND_H + +#include +#include +#include + +class QAbstractItemModel; + +/** + * @brief The ModelIndexCommand class + * Change a data of an index of QAbstractItemModel + */ +class ModelIndexCommand : public QUndoCommand +{ + public: + ModelIndexCommand(QAbstractItemModel *model, const QModelIndex &index, QUndoCommand *parent = nullptr); + void setData(const QVariant &value, int role = Qt::DisplayRole); + + virtual void redo() override; + virtual void undo() override; + + private: + QPointer m_model; + QModelIndex m_index; + QVariant m_old_value, + m_new_value; + int m_role=0; +}; + +/** + * @brief The ModelHeaderDataCommand class + * Change the data of a header + */ +class ModelHeaderDataCommand : public QUndoCommand +{ + public: + ModelHeaderDataCommand(QAbstractItemModel *model, QUndoCommand *parent = nullptr); + void setData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::DisplayRole); + + virtual void redo() override; + virtual void undo() override; + + private: + QPointer m_model; + Qt::Orientation m_orientation; + QVariant m_old_value, + m_new_value; + int m_section, + m_role; +}; + +#endif // ITEMMODELCOMMAND_H