Fix Coding convention

Please download and use
https://qelectrotech.org/wiki_new/_media/doc/qtcreator-qet-style.xml
when you write code in Qt
This commit is contained in:
Simon De Backer
2020-09-07 21:41:45 +02:00
parent babf3d2cf2
commit 8ee38fab9e
7 changed files with 558 additions and 285 deletions

View File

@@ -43,7 +43,10 @@
@param project Le projet a exporter @param project Le projet a exporter
@param parent Le Widget parent de ce dialogue @param parent Le Widget parent de ce dialogue
*/ */
ExportDialog::ExportDialog(QETProject *project, QWidget *parent) : QDialog(parent) { ExportDialog::ExportDialog(
QETProject *project,
QWidget *parent) : QDialog(parent)
{
if (!project) return; if (!project) return;
// recupere le projet a exporter // recupere le projet a exporter
@@ -105,13 +108,15 @@ ExportDialog::ExportDialog(QETProject *project, QWidget *parent) : QDialog(paren
/** /**
Destructeur - ne fait rien Destructeur - ne fait rien
*/ */
ExportDialog::~ExportDialog() { ExportDialog::~ExportDialog()
{
} }
/** /**
@return le nombre de schemas coches (donc a exporter) @return le nombre de schemas coches (donc a exporter)
*/ */
int ExportDialog::diagramsToExportCount() const { int ExportDialog::diagramsToExportCount() const
{
int checked_diagrams_count = 0; int checked_diagrams_count = 0;
foreach(ExportDiagramLine *diagram_line, diagram_lines_.values()) { foreach(ExportDiagramLine *diagram_line, diagram_lines_.values()) {
if (diagram_line -> must_export -> isChecked()) ++ checked_diagrams_count; if (diagram_line -> must_export -> isChecked()) ++ checked_diagrams_count;
@@ -123,7 +128,8 @@ int ExportDialog::diagramsToExportCount() const {
Met en place la liste des schemas Met en place la liste des schemas
@return Le widget representant la liste des schemas @return Le widget representant la liste des schemas
*/ */
QWidget *ExportDialog::initDiagramsListPart() { QWidget *ExportDialog::initDiagramsListPart()
{
preview_mapper_ = new QSignalMapper(this); preview_mapper_ = new QSignalMapper(this);
width_mapper_ = new QSignalMapper(this); width_mapper_ = new QSignalMapper(this);
height_mapper_ = new QSignalMapper(this); height_mapper_ = new QSignalMapper(this);
@@ -186,13 +192,15 @@ QWidget *ExportDialog::initDiagramsListPart() {
return(scroll_diagrams_list); return(scroll_diagrams_list);
} }
void ExportDialog::slot_selectAllClicked() { void ExportDialog::slot_selectAllClicked()
{
foreach (ExportDiagramLine *diagramLine, diagram_lines_) { foreach (ExportDiagramLine *diagramLine, diagram_lines_) {
diagramLine -> must_export -> setChecked(true); diagramLine -> must_export -> setChecked(true);
} }
} }
void ExportDialog::slot_deSelectAllClicked() { void ExportDialog::slot_deSelectAllClicked()
{
foreach (ExportDiagramLine *diagramLine, diagram_lines_) { foreach (ExportDiagramLine *diagramLine, diagram_lines_) {
diagramLine -> must_export -> setChecked(false); diagramLine -> must_export -> setChecked(false);
} }
@@ -204,7 +212,8 @@ void ExportDialog::slot_deSelectAllClicked() {
@param diagram Un schema @param diagram Un schema
@return le rapport largeur / hauteur du schema @return le rapport largeur / hauteur du schema
*/ */
qreal ExportDialog::diagramRatio(Diagram *diagram) { qreal ExportDialog::diagramRatio(Diagram *diagram)
{
QSize diagram_size = diagramSize(diagram); QSize diagram_size = diagramSize(diagram);
qreal diagram_ratio = (qreal)diagram_size.width() / (qreal)diagram_size.height(); qreal diagram_ratio = (qreal)diagram_size.width() / (qreal)diagram_size.height();
return(diagram_ratio); return(diagram_ratio);
@@ -215,7 +224,8 @@ qreal ExportDialog::diagramRatio(Diagram *diagram) {
@return les dimensions du schema, en tenant compte du type d'export : cadre @return les dimensions du schema, en tenant compte du type d'export : cadre
ou elements ou elements
*/ */
QSize ExportDialog::diagramSize(Diagram *diagram) { QSize ExportDialog::diagramSize(Diagram *diagram)
{
// sauvegarde le parametre useBorder du schema // sauvegarde le parametre useBorder du schema
bool state_useBorder = diagram -> useBorder(); bool state_useBorder = diagram -> useBorder();
@@ -235,7 +245,8 @@ QSize ExportDialog::diagramSize(Diagram *diagram) {
activee pour ce schema. activee pour ce schema.
@param diagram_id numero du schema concerne @param diagram_id numero du schema concerne
*/ */
void ExportDialog::slot_correctWidth(int diagram_id) { void ExportDialog::slot_correctWidth(int diagram_id)
{
// recupere l'ExportDiagramLine concernee // recupere l'ExportDiagramLine concernee
ExportDialog::ExportDiagramLine *current_diagram = diagram_lines_[diagram_id]; ExportDialog::ExportDiagramLine *current_diagram = diagram_lines_[diagram_id];
if (!current_diagram) return; if (!current_diagram) return;
@@ -258,7 +269,8 @@ void ExportDialog::slot_correctWidth(int diagram_id) {
activee pour ce schema. activee pour ce schema.
@param diagram_id numero du schema concerne @param diagram_id numero du schema concerne
*/ */
void ExportDialog::slot_correctHeight(int diagram_id) { void ExportDialog::slot_correctHeight(int diagram_id)
{
// recupere l'ExportDiagramLine concernee // recupere l'ExportDiagramLine concernee
ExportDialog::ExportDiagramLine *current_diagram = diagram_lines_[diagram_id]; ExportDialog::ExportDiagramLine *current_diagram = diagram_lines_[diagram_id];
if (!current_diagram) return; if (!current_diagram) return;
@@ -280,7 +292,8 @@ void ExportDialog::slot_correctHeight(int diagram_id) {
proportions d'un des schemas proportions d'un des schemas
@param diagram_id numero du schema concerne @param diagram_id numero du schema concerne
*/ */
void ExportDialog::slot_keepRatioChanged(int diagram_id) { void ExportDialog::slot_keepRatioChanged(int diagram_id)
{
// recupere l'ExportDiagramLine concernee // recupere l'ExportDiagramLine concernee
ExportDialog::ExportDiagramLine *current_diagram = diagram_lines_[diagram_id]; ExportDialog::ExportDiagramLine *current_diagram = diagram_lines_[diagram_id];
if (!current_diagram) return; if (!current_diagram) return;
@@ -303,7 +316,8 @@ void ExportDialog::slot_keepRatioChanged(int diagram_id) {
Reinitialise les dimensions d'un des schemas Reinitialise les dimensions d'un des schemas
@param diagram_id numero du schema concerne @param diagram_id numero du schema concerne
*/ */
void ExportDialog::slot_resetSize(int diagram_id) { void ExportDialog::slot_resetSize(int diagram_id)
{
// recupere l'ExportDiagramLine concernee // recupere l'ExportDiagramLine concernee
ExportDialog::ExportDiagramLine *current_diagram = diagram_lines_[diagram_id]; ExportDialog::ExportDiagramLine *current_diagram = diagram_lines_[diagram_id];
if (!current_diagram) return; if (!current_diagram) return;
@@ -328,7 +342,12 @@ void ExportDialog::slot_resetSize(int diagram_id) {
@param keep_aspect_ratio True pour conserver le ratio, false sinon @param keep_aspect_ratio True pour conserver le ratio, false sinon
@return l'image a exporter @return l'image a exporter
*/ */
QImage ExportDialog::generateImage(Diagram *diagram, int width, int height, bool keep_aspect_ratio) { QImage ExportDialog::generateImage(
Diagram *diagram,
int width,
int height,
bool keep_aspect_ratio)
{
saveReloadDiagramParameters(diagram, true); saveReloadDiagramParameters(diagram, true);
QImage image(width, height, QImage::Format_RGB32); QImage image(width, height, QImage::Format_RGB32);
@@ -370,7 +389,13 @@ void ExportDialog::saveReloadDiagramParameters(Diagram *diagram, bool save) {
@param keep_aspect_ratio True pour conserver le ratio, false sinon @param keep_aspect_ratio True pour conserver le ratio, false sinon
@param io_device Peripherique de sortie pour le code SVG (souvent : un fichier) @param io_device Peripherique de sortie pour le code SVG (souvent : un fichier)
*/ */
void ExportDialog::generateSvg(Diagram *diagram, int width, int height, bool keep_aspect_ratio, QIODevice &io_device) { void ExportDialog::generateSvg(
Diagram *diagram,
int width,
int height,
bool keep_aspect_ratio,
QIODevice &io_device)
{
saveReloadDiagramParameters(diagram, true); saveReloadDiagramParameters(diagram, true);
// genere une QPicture a partir du schema // genere une QPicture a partir du schema
@@ -399,10 +424,12 @@ void ExportDialog::generateSvg(Diagram *diagram, int width, int height, bool kee
@param height Hauteur de l'export DXF @param height Hauteur de l'export DXF
@param file_path @param file_path
*/ */
void ExportDialog::generateDxf(Diagram *diagram, void ExportDialog::generateDxf(
Diagram *diagram,
int width, int width,
int height, int height,
QString &file_path) { QString &file_path)
{
saveReloadDiagramParameters(diagram, true); saveReloadDiagramParameters(diagram, true);
width -= 2*Diagram::margin; width -= 2*Diagram::margin;
@@ -696,7 +723,8 @@ QPointF ExportDialog::rotation_transformed(qreal px,
/** /**
Slot effectuant les exports apres la validation du dialogue. Slot effectuant les exports apres la validation du dialogue.
*/ */
void ExportDialog::slot_export() { void ExportDialog::slot_export()
{
// recupere la liste des schemas a exporter // recupere la liste des schemas a exporter
QList<ExportDiagramLine *> diagrams_to_export; QList<ExportDiagramLine *> diagrams_to_export;
foreach(ExportDiagramLine *diagram_line, diagram_lines_.values()) { foreach(ExportDiagramLine *diagram_line, diagram_lines_.values()) {
@@ -822,7 +850,8 @@ void ExportDialog::exportDiagram(ExportDiagramLine *diagram_line) {
Slot appele lorsque l'utilisateur change la zone du schema qui doit etre Slot appele lorsque l'utilisateur change la zone du schema qui doit etre
exportee. Il faut alors ajuster les dimensions des schemas. exportee. Il faut alors ajuster les dimensions des schemas.
*/ */
void ExportDialog::slot_changeUseBorder() { void ExportDialog::slot_changeUseBorder()
{
// parcourt les schemas a exporter // parcourt les schemas a exporter
foreach(int diagram_id, diagram_lines_.keys()) { foreach(int diagram_id, diagram_lines_.keys()) {
ExportDiagramLine *diagram_line = diagram_lines_[diagram_id]; ExportDiagramLine *diagram_line = diagram_lines_[diagram_id];
@@ -840,7 +869,8 @@ void ExportDialog::slot_changeUseBorder() {
schemas coches, et il garde au plus un schema coche si on exporte vers schemas coches, et il garde au plus un schema coche si on exporte vers
le presse-papier. le presse-papier.
*/ */
void ExportDialog::slot_checkDiagramsCount() { void ExportDialog::slot_checkDiagramsCount()
{
QPushButton *export_button = buttons -> button(QDialogButtonBox::Save); QPushButton *export_button = buttons -> button(QDialogButtonBox::Save);
export_button -> setEnabled(diagramsToExportCount()); export_button -> setEnabled(diagramsToExportCount());
} }
@@ -1027,14 +1057,16 @@ ExportDialog::ExportDiagramLine::ExportDiagramLine(Diagram *dia, QSize diagram_s
/** /**
Destructeur Destructeur
*/ */
ExportDialog::ExportDiagramLine::~ExportDiagramLine() { ExportDialog::ExportDiagramLine::~ExportDiagramLine()
{
} }
/** /**
@return un layout contenant les widgets necessaires a la gestion de la @return un layout contenant les widgets necessaires a la gestion de la
taille d'un schema avant son export. taille d'un schema avant son export.
*/ */
QBoxLayout *ExportDialog::ExportDiagramLine::sizeLayout() { QBoxLayout *ExportDialog::ExportDiagramLine::sizeLayout()
{
QHBoxLayout *layout = new QHBoxLayout(); QHBoxLayout *layout = new QHBoxLayout();
layout -> addWidget(width); layout -> addWidget(width);
layout -> addWidget(x_label); layout -> addWidget(x_label);

View File

@@ -30,20 +30,18 @@ class ExportPropertiesWidget;
class ExportDialog : public QDialog { class ExportDialog : public QDialog {
Q_OBJECT Q_OBJECT
// constructors, destructor
public: public:
// constructors, destructor
ExportDialog(QETProject *, QWidget * = nullptr); ExportDialog(QETProject *, QWidget * = nullptr);
~ExportDialog() override; ~ExportDialog() override;
private:
ExportDialog(const ExportDialog &);
// methods // methods
public:
int diagramsToExportCount() const; int diagramsToExportCount() const;
static QPointF rotation_transformed(qreal, qreal, qreal, qreal, qreal); static QPointF rotation_transformed(qreal, qreal, qreal, qreal, qreal);
private: private:
ExportDialog(const ExportDialog &);
class ExportDiagramLine { class ExportDiagramLine {
public: public:
ExportDiagramLine(Diagram *, QSize); ExportDiagramLine(Diagram *, QSize);
@@ -85,7 +83,6 @@ class ExportDialog : public QDialog {
QETProject *project_; QETProject *project_;
// methods // methods
private:
QWidget *initDiagramsListPart(); QWidget *initDiagramsListPart();
void saveReloadDiagramParameters(Diagram *, bool = true); void saveReloadDiagramParameters(Diagram *, bool = true);
void generateSvg(Diagram *, int, int, bool, QIODevice &); void generateSvg(Diagram *, int, int, bool, QIODevice &);

View File

@@ -44,7 +44,8 @@ ExportProperties::ExportProperties() :
/** /**
Destructeur Destructeur
*/ */
ExportProperties::~ExportProperties() { ExportProperties::~ExportProperties()
{
} }
/** /**
@@ -53,7 +54,8 @@ ExportProperties::~ExportProperties() {
@param prefix prefixe a ajouter devant les noms des parametres @param prefix prefixe a ajouter devant les noms des parametres
*/ */
void ExportProperties::toSettings(QSettings &settings, void ExportProperties::toSettings(QSettings &settings,
const QString &prefix) const { const QString &prefix) const
{
settings.setValue(prefix + "path", settings.setValue(prefix + "path",
QDir::toNativeSeparators( QDir::toNativeSeparators(
destination_directory.absolutePath())); destination_directory.absolutePath()));

View File

@@ -44,17 +44,23 @@ QetGraphicsHeaderItem::QetGraphicsHeaderItem(QGraphicsItem *parent) :
void QetGraphicsHeaderItem::setModel(QAbstractItemModel *model) void QetGraphicsHeaderItem::setModel(QAbstractItemModel *model)
{ {
if (m_model) { if (m_model) {
disconnect(m_model, &QAbstractItemModel::headerDataChanged, this, &QetGraphicsHeaderItem::headerDataChanged); disconnect(m_model, &QAbstractItemModel::headerDataChanged,
disconnect(m_model, &QAbstractItemModel::modelReset, this, &QetGraphicsHeaderItem::modelReseted); this, &QetGraphicsHeaderItem::headerDataChanged);
disconnect(m_model, &QAbstractItemModel::columnsInserted, this, &QetGraphicsHeaderItem::modelReseted); disconnect(m_model, &QAbstractItemModel::modelReset,
this, &QetGraphicsHeaderItem::modelReseted);
disconnect(m_model, &QAbstractItemModel::columnsInserted,
this, &QetGraphicsHeaderItem::modelReseted);
} }
m_model = model; m_model = model;
if (m_model) if (m_model)
{ {
connect(m_model, &QAbstractItemModel::headerDataChanged, this, &QetGraphicsHeaderItem::headerDataChanged); connect(m_model, &QAbstractItemModel::headerDataChanged,
connect(m_model, &QAbstractItemModel::modelReset, this, &QetGraphicsHeaderItem::modelReseted); this, &QetGraphicsHeaderItem::headerDataChanged);
connect(m_model, &QAbstractItemModel::columnsInserted, this, &QetGraphicsHeaderItem::modelReseted); connect(m_model, &QAbstractItemModel::modelReset, this,
&QetGraphicsHeaderItem::modelReseted);
connect(m_model, &QAbstractItemModel::columnsInserted,
this, &QetGraphicsHeaderItem::modelReseted);
setUpMinimumSectionsSize(); setUpMinimumSectionsSize();
m_current_sections_width.clear(); m_current_sections_width.clear();
m_current_sections_width.resize(m_sections_minimum_width.size()); m_current_sections_width.resize(m_sections_minimum_width.size());
@@ -69,7 +75,8 @@ void QetGraphicsHeaderItem::setModel(QAbstractItemModel *model)
@brief QetGraphicsHeaderItem::model @brief QetGraphicsHeaderItem::model
@return the model that this item is presenting @return the model that this item is presenting
*/ */
QAbstractItemModel *QetGraphicsHeaderItem::model() const { QAbstractItemModel *QetGraphicsHeaderItem::model() const
{
return m_model; return m_model;
} }
@@ -78,7 +85,8 @@ QAbstractItemModel *QetGraphicsHeaderItem::model() const {
Reimplemented from QGraphicsObject::boundingRect() const; Reimplemented from QGraphicsObject::boundingRect() const;
@return @return
*/ */
QRectF QetGraphicsHeaderItem::boundingRect() const { QRectF QetGraphicsHeaderItem::boundingRect() const
{
return m_bounding_rect; return m_bounding_rect;
} }
@@ -89,7 +97,10 @@ QRectF QetGraphicsHeaderItem::boundingRect() const {
@param option @param option
@param widget @param widget
*/ */
void QetGraphicsHeaderItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) void QetGraphicsHeaderItem::paint(
QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget)
{ {
Q_UNUSED(option) Q_UNUSED(option)
Q_UNUSED(widget) Q_UNUSED(widget)
@@ -113,7 +124,11 @@ void QetGraphicsHeaderItem::paint(QPainter *painter, const QStyleOptionGraphicsI
painter->restore(); painter->restore();
return; return;
} }
painter->setFont(m_model->headerData(0, Qt::Horizontal, Qt::FontRole).value<QFont>()); painter->setFont(
m_model->headerData(
0,
Qt::Horizontal,
Qt::FontRole).value<QFont>());
//Draw vertical lines //Draw vertical lines
auto offset= 0; auto offset= 0;
@@ -126,14 +141,32 @@ void QetGraphicsHeaderItem::paint(QPainter *painter, const QStyleOptionGraphicsI
} }
//Write text of each cell //Write text of each cell
auto margins_ = QETUtils::marginsFromString(m_model->headerData(0, Qt::Horizontal, Qt::UserRole+1).toString()); auto margins_ = QETUtils::marginsFromString(
m_model->headerData(
0,
Qt::Horizontal,
Qt::UserRole+1).toString());
QPointF top_left(margins_.left(), margins_.top()); QPointF top_left(margins_.left(), margins_.top());
for (auto i= 0 ; i<m_model->columnCount() ; ++i) for (auto i= 0 ; i<m_model->columnCount() ; ++i)
{ {
QSize size(m_current_sections_width.at(i) - margins_.left() - margins_.right(), m_section_height - margins_.top() - margins_.bottom()); QSize size(
painter->drawText(QRectF(top_left, size), m_current_sections_width.at(i)
m_model->headerData(0, Qt::Horizontal, Qt::TextAlignmentRole).toInt(), - margins_.left()
m_model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString()); - margins_.right(),
m_section_height
- margins_.top()
- margins_.bottom());
painter->drawText(
QRectF(top_left, size),
m_model->headerData(
0,
Qt::Horizontal,
Qt::TextAlignmentRole).toInt(),
m_model->headerData(
i,
Qt::Horizontal,
Qt::DisplayRole).toString());
top_left.setX(top_left.x() + m_current_sections_width.at(i)); top_left.setX(top_left.x() + m_current_sections_width.at(i));
} }
@@ -158,16 +191,26 @@ bool QetGraphicsHeaderItem::toDXF(const QString &filepath)
{ {
QPointF p1(offset+size, m_current_rect.top()); QPointF p1(offset+size, m_current_rect.top());
QPointF p2(offset+size, m_current_rect.bottom()); QPointF p2(offset+size, m_current_rect.bottom());
Createdxf::drawLine(filepath,QLineF(mapToScene(p1),mapToScene(p2)),0); Createdxf::drawLine(
filepath,
QLineF(
mapToScene(p1),
mapToScene(p2)),
0);
offset += size; offset += size;
} }
//Write text of each cell //Write text of each cell
auto margins_ = QETUtils::marginsFromString(m_model->headerData(0, Qt::Horizontal, Qt::UserRole+1).toString()); auto margins_ = QETUtils::marginsFromString(
m_model->headerData(
0,
Qt::Horizontal,
Qt::UserRole+1).toString());
QPointF top_left(margins_.left(), margins_.top()); QPointF top_left(margins_.left(), margins_.top());
for (auto i= 0 ; i<m_model->columnCount() ; ++i) for (auto i= 0 ; i<m_model->columnCount() ; ++i)
{ {
QSize size(m_current_sections_width.at(i) - margins_.left() - margins_.right(), m_section_height - margins_.top() - margins_.bottom()); QSize size(m_current_sections_width.at(i) - margins_.left() - margins_.right(),
m_section_height - margins_.top() - margins_.bottom());
QPointF qm = mapToScene(top_left); QPointF qm = mapToScene(top_left);
qreal h = size.height();// * Createdxf::yScale; qreal h = size.height();// * Createdxf::yScale;
@@ -177,7 +220,13 @@ bool QetGraphicsHeaderItem::toDXF(const QString &filepath)
int valign = 2; int valign = 2;
Createdxf::drawTextAligned(filepath,m_model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString(),x,y,h1,0,0,0,valign,x,0,0); Createdxf::drawTextAligned(
filepath,
m_model->headerData(
i,
Qt::Horizontal,
Qt::DisplayRole).toString(),
x,y,h1,0,0,0,valign,x,0,0);
top_left.setX(top_left.x() + m_current_sections_width.at(i)); top_left.setX(top_left.x() + m_current_sections_width.at(i));
} }
@@ -188,7 +237,8 @@ bool QetGraphicsHeaderItem::toDXF(const QString &filepath)
@brief QetGraphicsHeaderItem::rect @brief QetGraphicsHeaderItem::rect
@return the current rect of the item aka the size of rectangle painted. @return the current rect of the item aka the size of rectangle painted.
*/ */
QRect QetGraphicsHeaderItem::rect() const { QRect QetGraphicsHeaderItem::rect() const
{
return m_current_rect; return m_current_rect;
} }
@@ -223,7 +273,11 @@ void QetGraphicsHeaderItem::resizeSection(int logicalIndex, int size)
{ {
prepareGeometryChange(); prepareGeometryChange();
m_current_sections_width.replace(logicalIndex, size); m_current_sections_width.replace(logicalIndex, size);
m_current_rect.setWidth(std::accumulate(m_current_sections_width.begin(), m_current_sections_width.end(), 0)); m_current_rect.setWidth(
std::accumulate(
m_current_sections_width.begin(),
m_current_sections_width.end(),
0));
setUpBoundingRect(); setUpBoundingRect();
update(); update();
emit sectionResized(logicalIndex, size); emit sectionResized(logicalIndex, size);
@@ -254,7 +308,14 @@ QDomElement QetGraphicsHeaderItem::toXml(QDomDocument &document) const
{ {
auto dom_element = document.createElement(xmlTagName()); auto dom_element = document.createElement(xmlTagName());
if (m_model) { if (m_model) {
dom_element.appendChild(QETXML::marginsToXml(document, QETUtils::marginsFromString(m_model->headerData(0, Qt::Horizontal, Qt::UserRole+1).toString()))); dom_element.appendChild(
QETXML::marginsToXml(
document,
QETUtils::marginsFromString(
m_model->headerData(
0,
Qt::Horizontal,
Qt::UserRole+1).toString())));
} }
return dom_element; return dom_element;
@@ -271,8 +332,16 @@ void QetGraphicsHeaderItem::fromXml(const QDomElement &element)
return; return;
} }
auto margins_ = QETUtils::marginsToString(QETXML::marginsFromXml(element.firstChildElement("margins"))); auto margins_ = QETUtils::marginsToString(
m_model->setHeaderData(0, Qt::Horizontal, QETUtils::marginsToString(QETXML::marginsFromXml(element.firstChildElement("margins"))), Qt::UserRole+1); QETXML::marginsFromXml(
element.firstChildElement("margins")));
m_model->setHeaderData(
0,
Qt::Horizontal,
QETUtils::marginsToString(
QETXML::marginsFromXml(
element.firstChildElement("margins"))),
Qt::UserRole+1);
} }
/** /**
@@ -291,10 +360,19 @@ void QetGraphicsHeaderItem::setUpMinimumSectionsSize()
return; return;
} }
QFontMetrics metrics(m_model->headerData(0, Qt::Horizontal, Qt::FontRole).value<QFont>()); QFontMetrics metrics(
auto margins_ = QETUtils::marginsFromString(m_model->headerData(0, Qt::Horizontal, Qt::UserRole+1).toString()); m_model->headerData(
0,
Qt::Horizontal,
Qt::FontRole).value<QFont>());
auto margins_ = QETUtils::marginsFromString(
m_model->headerData(
0,
Qt::Horizontal,
Qt::UserRole+1).toString());
//Set the height of row; //Set the height of row;
m_minimum_section_height = metrics.boundingRect("HEIGHT TEST").height() + margins_.top() + margins_.bottom(); m_minimum_section_height = metrics.boundingRect("HEIGHT TEST").height()
+ margins_.top() + margins_.bottom();
m_sections_minimum_width.clear(); m_sections_minimum_width.clear();
m_sections_minimum_width.resize(m_model->columnCount()); m_sections_minimum_width.resize(m_model->columnCount());
@@ -302,17 +380,25 @@ void QetGraphicsHeaderItem::setUpMinimumSectionsSize()
for (auto i= 0 ; i<m_model->columnCount() ; ++i) for (auto i= 0 ; i<m_model->columnCount() ; ++i)
{ {
auto str = m_model->headerData(i, Qt::Horizontal).toString(); auto str = m_model->headerData(i, Qt::Horizontal).toString();
m_sections_minimum_width.replace(i, metrics.boundingRect(str).width() + margins_.left() + margins_.right()); m_sections_minimum_width.replace(
i,
metrics.boundingRect(str).width()
+ margins_.left()
+ margins_.right());
} }
m_minimum_width = std::accumulate(m_sections_minimum_width.begin(), m_sections_minimum_width.end(), 0); m_minimum_width = std::accumulate(
m_sections_minimum_width.begin(),
m_sections_minimum_width.end(),
0);
} }
/** /**
@brief QetGraphicsHeaderItem::setUpBoundingRect @brief QetGraphicsHeaderItem::setUpBoundingRect
Setup the bounding rect of the item Setup the bounding rect of the item
*/ */
void QetGraphicsHeaderItem::setUpBoundingRect() { void QetGraphicsHeaderItem::setUpBoundingRect()
{
m_bounding_rect = m_current_rect.adjusted(-10, -10, 10, 10); m_bounding_rect = m_current_rect.adjusted(-10, -10, 10, 10);
} }
@@ -323,7 +409,8 @@ void QetGraphicsHeaderItem::setUpBoundingRect() {
@param first @param first
@param last @param last
*/ */
void QetGraphicsHeaderItem::headerDataChanged(Qt::Orientations orientation, int first, int last) void QetGraphicsHeaderItem::headerDataChanged(
Qt::Orientations orientation, int first, int last)
{ {
Q_UNUSED(orientation) Q_UNUSED(orientation)
Q_UNUSED(first) Q_UNUSED(first)

View File

@@ -51,12 +51,17 @@ class QetGraphicsHeaderItem : public QGraphicsObject
QAbstractItemModel *model() const; QAbstractItemModel *model() const;
virtual QRectF boundingRect() const override; virtual QRectF boundingRect() const override;
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override; virtual void paint(
QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget) override;
QRect rect() const; QRect rect() const;
void resizeSection(int logicalIndex, int size); void resizeSection(int logicalIndex, int size);
int sectionSize(int logical_index) const; int sectionSize(int logical_index) const;
QVector<int> minimumSectionWidth() const {return m_sections_minimum_width;} QVector<int> minimumSectionWidth() const
int minimumWidth() const {return m_minimum_width;} {return m_sections_minimum_width;}
int minimumWidth() const
{return m_minimum_width;}
QDomElement toXml (QDomDocument &document) const; QDomElement toXml (QDomDocument &document) const;
void fromXml(const QDomElement &element); void fromXml(const QDomElement &element);
@@ -70,19 +75,25 @@ class QetGraphicsHeaderItem : public QGraphicsObject
private: private:
void setUpMinimumSectionsSize(); void setUpMinimumSectionsSize();
void setUpBoundingRect(); void setUpBoundingRect();
void headerDataChanged(Qt::Orientations orientation, int first, int last); void headerDataChanged(
Qt::Orientations orientation,
int first,
int last);
void adjustSize(); void adjustSize();
void modelReseted(); void modelReseted();
private: QRect m_current_rect;
QRectF m_bounding_rect; QRectF m_bounding_rect;
QAbstractItemModel *m_model = nullptr; QAbstractItemModel *m_model = nullptr;
QVector<int> m_sections_minimum_width,
QVector<int>
m_sections_minimum_width,
m_current_sections_width; m_current_sections_width;
int m_section_height=1,
m_minimum_section_height=1; int
int m_minimum_width=1; m_section_height=1,
QRect m_current_rect; m_minimum_section_height=1,
m_minimum_width=1;
}; };
#endif // QETGRAPHICSHEADERITEM_H #endif // QETGRAPHICSHEADERITEM_H

View File

@@ -40,7 +40,8 @@ static int no_model_width = 40;
@param table : table to adjust @param table : table to adjust
@param margins : margins between table and folio. @param margins : margins between table and folio.
*/ */
void QetGraphicsTableItem::adjustTableToFolio(QetGraphicsTableItem *table, void QetGraphicsTableItem::adjustTableToFolio(
QetGraphicsTableItem *table,
QMargins margins) QMargins margins)
{ {
if (!table->diagram()) { if (!table->diagram()) {
@@ -48,7 +49,11 @@ void QetGraphicsTableItem::adjustTableToFolio(QetGraphicsTableItem *table,
} }
auto drawable_rect = table->diagram()->border_and_titleblock.insideBorderRect(); auto drawable_rect = table->diagram()->border_and_titleblock.insideBorderRect();
table->setPos(drawable_rect.topLeft().x() + margins.left(), drawable_rect.topLeft().y() + margins.top() + table->headerItem()->rect().height()); table->setPos(
drawable_rect.topLeft().x() + margins.left(),
drawable_rect.topLeft().y()
+ margins.top()
+ table->headerItem()->rect().height());
auto size_ = table->size(); auto size_ = table->size();
size_.setWidth(int(drawable_rect.width() - (margins.left() + margins.right()))); size_.setWidth(int(drawable_rect.width() - (margins.left() + margins.right())));
@@ -134,12 +139,17 @@ QetGraphicsTableItem::QetGraphicsTableItem(QGraphicsItem *parent) :
//then user can already grab this item, even if model is not already set //then user can already grab this item, even if model is not already set
m_bounding_rect.setRect(m_br_margin/-2, m_br_margin/-2, 50, 50); m_bounding_rect.setRect(m_br_margin/-2, m_br_margin/-2, 50, 50);
connect(this, &QetGraphicsTableItem::xChanged, this, &QetGraphicsTableItem::adjustHandlerPos); connect(this, &QetGraphicsTableItem::xChanged,
connect(this, &QetGraphicsTableItem::yChanged, this, &QetGraphicsTableItem::adjustHandlerPos); this, &QetGraphicsTableItem::adjustHandlerPos);
connect(this, &QetGraphicsTableItem::yChanged,
this, &QetGraphicsTableItem::adjustHandlerPos);
m_header_item = new QetGraphicsHeaderItem(this); m_header_item = new QetGraphicsHeaderItem(this);
connect(m_header_item, &QetGraphicsHeaderItem::sectionResized, this, &QetGraphicsTableItem::headerSectionResized); connect(m_header_item, &QetGraphicsHeaderItem::sectionResized,
connect(m_header_item, &QetGraphicsHeaderItem::heightResized, this, [this]() { this, &QetGraphicsTableItem::headerSectionResized);
connect(m_header_item, &QetGraphicsHeaderItem::heightResized,
this, [this]()
{
m_header_item->setPos(0, 0-m_header_item->rect().height()); m_header_item->setPos(0, 0-m_header_item->rect().height());
}); });
//Init the size of table without a model //Init the size of table without a model
@@ -160,8 +170,10 @@ void QetGraphicsTableItem::setModel(QAbstractItemModel *model)
{ {
if (m_model) if (m_model)
{ {
disconnect(m_model, &QAbstractItemModel::dataChanged, this, &QetGraphicsTableItem::dataChanged); disconnect(m_model, &QAbstractItemModel::dataChanged,
disconnect(m_model, &QAbstractItemModel::modelReset, this, &QetGraphicsTableItem::modelReseted); this, &QetGraphicsTableItem::dataChanged);
disconnect(m_model, &QAbstractItemModel::modelReset,
this, &QetGraphicsTableItem::modelReseted);
} }
m_model = model; m_model = model;
m_header_item->setModel(model); m_header_item->setModel(model);
@@ -172,8 +184,10 @@ void QetGraphicsTableItem::setModel(QAbstractItemModel *model)
m_header_item->setPos(0, -m_header_item->rect().height()); m_header_item->setPos(0, -m_header_item->rect().height());
if (m_model) if (m_model)
{ {
connect(m_model, &QAbstractItemModel::dataChanged, this, &QetGraphicsTableItem::dataChanged); connect(m_model, &QAbstractItemModel::dataChanged,
connect(m_model, &QAbstractItemModel::modelReset, this, &QetGraphicsTableItem::modelReseted); this, &QetGraphicsTableItem::dataChanged);
connect(m_model, &QAbstractItemModel::modelReset,
this, &QetGraphicsTableItem::modelReseted);
} }
if (m_next_table) { if (m_next_table) {
@@ -185,7 +199,8 @@ void QetGraphicsTableItem::setModel(QAbstractItemModel *model)
@brief QetGraphicsTableItem::model @brief QetGraphicsTableItem::model
@return The model that this item is presenting @return The model that this item is presenting
*/ */
QAbstractItemModel *QetGraphicsTableItem::model() const { QAbstractItemModel *QetGraphicsTableItem::model() const
{
return m_model; return m_model;
} }
@@ -194,7 +209,8 @@ QAbstractItemModel *QetGraphicsTableItem::model() const {
Reimplemented from QGraphicsObject Reimplemented from QGraphicsObject
@return @return
*/ */
QRectF QetGraphicsTableItem::boundingRect() const { QRectF QetGraphicsTableItem::boundingRect() const
{
return m_bounding_rect; return m_bounding_rect;
} }
@@ -205,7 +221,10 @@ QRectF QetGraphicsTableItem::boundingRect() const {
@param option @param option
@param widget @param widget
*/ */
void QetGraphicsTableItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) void QetGraphicsTableItem::paint(
QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget)
{ {
Q_UNUSED(option) Q_UNUSED(option)
Q_UNUSED(widget) Q_UNUSED(widget)
@@ -272,16 +291,23 @@ void QetGraphicsTableItem::paint(QPainter *painter, const QStyleOptionGraphicsIt
for(auto j= 0 ; j<m_model->columnCount() ; ++j) for(auto j= 0 ; j<m_model->columnCount() ; ++j)
{ {
//In first iteration the top left X is margin left, in all other iteration the top left X is stored in m_column_size //In first iteration the top left X is margin left,
// in all other iteration the top left X is stored in m_column_size
if (j>0) { if (j>0) {
top_left.setX(top_left.x() + m_header_item->sectionSize(j-1)); top_left.setX(top_left.x() + m_header_item->sectionSize(j-1));
} }
QSize size(m_header_item->sectionSize(j) - margin_.left() - margin_.right(), QSize size(m_header_item->sectionSize(j) - margin_.left() - margin_.right(),
static_cast<int>(cell_height) - margin_.top() - margin_.bottom()); static_cast<int>(cell_height) - margin_.top() - margin_.bottom());
auto index_row = m_previous_table ? i + m_previous_table->displayNRowOffset() : i; auto index_row = m_previous_table ? i + m_previous_table->displayNRowOffset() : i;
painter->drawText(QRectF(top_left, size), painter->drawText(
m_model->data(m_model->index(0,0), Qt::TextAlignmentRole).toInt() | Qt::AlignVCenter, QRectF(top_left, size),
m_model->index(index_row, j).data().toString()); m_model->data(
m_model->index(0,0),
Qt::TextAlignmentRole
).toInt()
| Qt::AlignVCenter,
m_model->index(index_row, j)
.data().toString());
} }
} }
@@ -325,7 +351,8 @@ QSize QetGraphicsTableItem::size() const
/** /**
@brief QetGraphicsTableItem::minimumSize @brief QetGraphicsTableItem::minimumSize
@return the minimum size the table can be @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 The returned size take care of the table's minimum width,
but also the header item's minimum width
*/ */
QSize QetGraphicsTableItem::minimumSize() const QSize QetGraphicsTableItem::minimumSize() const
{ {
@@ -343,7 +370,12 @@ QSize QetGraphicsTableItem::minimumSize() const
//m_minimum_column_width already take in count the minimum size of header //m_minimum_column_width already take in count the minimum size of header
QSize size_(std::accumulate(m_minimum_column_width.begin(), m_minimum_column_width.end(), 0), m_minimum_row_height*row_count); QSize size_(
std::accumulate(
m_minimum_column_width.begin(),
m_minimum_column_width.end(),
0),
m_minimum_row_height*row_count);
//make sure that the width is a multiple of 10 //make sure that the width is a multiple of 10
while (size_.width()%10) { while (size_.width()%10) {
size_.rwidth()++; size_.rwidth()++;
@@ -356,7 +388,8 @@ QSize QetGraphicsTableItem::minimumSize() const
Limit the number of row to display Limit the number of row to display
@param number : set to 0 or less to disabled the limit of row to display @param number : set to 0 or less to disabled the limit of row to display
*/ */
void QetGraphicsTableItem::setDisplayNRow(const int &number) { void QetGraphicsTableItem::setDisplayNRow(const int &number)
{
m_number_of_displayed_row = number; m_number_of_displayed_row = number;
setToMinimumHeight(); setToMinimumHeight();
if (m_next_table) if (m_next_table)
@@ -368,7 +401,8 @@ void QetGraphicsTableItem::setDisplayNRow(const int &number) {
@return the number of row displayed. @return the number of row displayed.
A value of 0 or less mean there is no limit A value of 0 or less mean there is no limit
*/ */
int QetGraphicsTableItem::displayNRow() const { int QetGraphicsTableItem::displayNRow() const
{
return m_number_of_displayed_row; return m_number_of_displayed_row;
} }
@@ -439,11 +473,13 @@ void QetGraphicsTableItem::setNextTable(QetGraphicsTableItem *table)
} }
void QetGraphicsTableItem::setTableName(const QString &name) { void QetGraphicsTableItem::setTableName(const QString &name)
{
m_name = name; m_name = name;
} }
QString QetGraphicsTableItem::tableName() const { QString QetGraphicsTableItem::tableName() const
{
return m_name; return m_name;
} }
@@ -461,11 +497,13 @@ int QetGraphicsTableItem::displayNRowOffset() const
return offset_; return offset_;
} }
QetGraphicsTableItem *QetGraphicsTableItem::previousTable() const { QetGraphicsTableItem *QetGraphicsTableItem::previousTable() const
{
return m_previous_table; return m_previous_table;
} }
QetGraphicsTableItem *QetGraphicsTableItem::nextTable() const { QetGraphicsTableItem *QetGraphicsTableItem::nextTable() const
{
return m_next_table; return m_next_table;
} }
@@ -498,7 +536,8 @@ void QetGraphicsTableItem::initLink()
@brief QetGraphicsTableItem::minimumRowHeigth @brief QetGraphicsTableItem::minimumRowHeigth
@return the minimum height of a row @return the minimum height of a row
*/ */
int QetGraphicsTableItem::minimumRowHeigth() const { int QetGraphicsTableItem::minimumRowHeigth() const
{
return m_minimum_row_height; return m_minimum_row_height;
} }
@@ -554,30 +593,54 @@ void QetGraphicsTableItem::fromXml(const QDomElement &dom_element)
return; return;
} }
this->setPos(dom_element.attribute("x", QString::number(10)).toDouble(), this->setPos(
dom_element.attribute("y", QString::number(10)).toDouble()); dom_element.attribute(
"x",
QString::number(10)).toDouble(),
dom_element.attribute(
"y",
QString::number(10)).toDouble());
//Size is not set now because will change during the whole process of opening a project from the xml //Size is not set now because will change during the whole process of opening a project from the xml
m_pending_size = QSize(dom_element.attribute("width", QString::number(no_model_width)).toInt(), m_pending_size = QSize(
dom_element.attribute("height", QString::number(no_model_height)).toInt()); dom_element.attribute(
"width",
QString::number(no_model_width)).toInt(),
dom_element.attribute(
"height",
QString::number(no_model_height)).toInt());
m_uuid = QUuid(dom_element.attribute("uuid", QUuid::createUuid().toString())); m_uuid = QUuid(
dom_element.attribute(
"uuid",
QUuid::createUuid().toString()));
m_name = dom_element.attribute("name"); m_name = dom_element.attribute("name");
m_number_of_displayed_row = dom_element.attribute("display_n_row", QString::number(0)).toInt(); m_number_of_displayed_row = dom_element.attribute(
"display_n_row",
QString::number(0)).toInt();
auto vector_ = QETXML::directChild(dom_element, "previous_table"); auto vector_ = QETXML::directChild(dom_element, "previous_table");
if (vector_.size()) { //Table have a previous table if (vector_.size()) { //Table have a previous table
m_pending_previous_table_uuid = QUuid(vector_.first().attribute("uuid")); m_pending_previous_table_uuid = QUuid(
vector_.first().attribute("uuid"));
} }
else if (this->diagram()) //The table haven't got a previous table, so there should be a model save to xml else if (this->diagram()) //The table haven't got a previous table, so there should be a model save to xml
{ {
//Get table //Get table
auto model_ = new ProjectDBModel(this->diagram()->project(), this->diagram()->project()); auto model_ = new ProjectDBModel(
model_->fromXml(dom_element.firstChildElement("model").firstChildElement(ProjectDBModel::xmlTagName())); this->diagram()->project(),
this->diagram()->project());
model_->fromXml(
dom_element
.firstChildElement("model")
.firstChildElement(
ProjectDBModel::xmlTagName()));
this->setModel(model_); this->setModel(model_);
} }
//Restore the header from xml //Restore the header from xml
m_header_item->fromXml(dom_element.firstChildElement(QetGraphicsHeaderItem::xmlTagName())); m_header_item->fromXml(
dom_element.firstChildElement(
QetGraphicsHeaderItem::xmlTagName()));
} }
/** /**
@@ -601,8 +664,13 @@ bool QetGraphicsTableItem::toDXF(const QString &filepath)
for(auto i=0 ; i<m_model->columnCount() ; ++i) for(auto i=0 ; i<m_model->columnCount() ; ++i)
{ {
QPointF p1(offset+m_header_item->sectionSize(i), 0); QPointF p1(offset+m_header_item->sectionSize(i), 0);
QPointF p2(offset+m_header_item->sectionSize(i), m_current_size.height()); QPointF p2(
Createdxf::drawLine(filepath,QLineF(mapToScene(p1),mapToScene(p2)),0); offset+m_header_item->sectionSize(i),
m_current_size.height());
Createdxf::drawLine(
filepath,
QLineF(mapToScene(p1),mapToScene(p2)),
0);
offset += m_header_item->sectionSize(i); offset += m_header_item->sectionSize(i);
} }
//Calculate the number of rows to display. //Calculate the number of rows to display.
@@ -620,7 +688,10 @@ bool QetGraphicsTableItem::toDXF(const QString &filepath)
{ {
QPointF p1(m_header_item->rect().left(), cell_height*i); QPointF p1(m_header_item->rect().left(), cell_height*i);
QPointF p2(m_header_item->rect().right(), cell_height*i); QPointF p2(m_header_item->rect().right(), cell_height*i);
Createdxf::drawLine(filepath,QLineF(mapToScene(p1),mapToScene(p2)),0); Createdxf::drawLine(
filepath,
QLineF(mapToScene(p1),mapToScene(p2))
,0);
} }
//Write text of each cell //Write text of each cell
@@ -631,7 +702,8 @@ bool QetGraphicsTableItem::toDXF(const QString &filepath)
for(auto j= 0 ; j<m_model->columnCount() ; ++j) for(auto j= 0 ; j<m_model->columnCount() ; ++j)
{ {
//In first iteration the top left X is margin left, in all other iteration the top left X is stored in m_column_size //In first iteration the top left X is margin left,
// in all other iteration the top left X is stored in m_column_size
if (j>0) { if (j>0) {
top_left.setX(top_left.x() + m_header_item->sectionSize(j-1)); top_left.setX(top_left.x() + m_header_item->sectionSize(j-1));
} }
@@ -647,7 +719,10 @@ bool QetGraphicsTableItem::toDXF(const QString &filepath)
int valign = 2; int valign = 2;
Createdxf::drawTextAligned(filepath,m_model->index(index_row, j).data().toString(),x,y,h1,0,0,0,valign,x,0,0); Createdxf::drawTextAligned(
filepath,
m_model->index(index_row, j).data().toString(),
x,y,h1,0,0,0,valign,x,0,0);
} }
} }
return true; return true;
@@ -719,7 +794,8 @@ bool QetGraphicsTableItem::sceneEventFilter(QGraphicsItem *watched, QEvent *even
@param value @param value
@return @return
*/ */
QVariant QetGraphicsTableItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) QVariant QetGraphicsTableItem::itemChange(
QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{ {
//item was removed from scene, we remove the handler //item was removed from scene, we remove the handler
if (change == ItemSceneHasChanged) { if (change == ItemSceneHasChanged) {
@@ -731,7 +807,8 @@ QVariant QetGraphicsTableItem::itemChange(QGraphicsItem::GraphicsItemChange chan
return QetGraphicsItem::itemChange(change, value); return QetGraphicsItem::itemChange(change, value);
} }
void QetGraphicsTableItem::modelReseted() { void QetGraphicsTableItem::modelReseted()
{
dataChanged(m_model->index(0,0), m_model->index(0,0), QVector<int>()); dataChanged(m_model->index(0,0), m_model->index(0,0), QVector<int>());
setToMinimumHeight(); setToMinimumHeight();
@@ -768,7 +845,11 @@ void QetGraphicsTableItem::setUpColumnAndRowMinimumSize()
{ {
auto index = m_model->index(row, col); auto index = m_model->index(row, col);
auto width = metrics.boundingRect(index.data().toString()).width(); auto width = metrics.boundingRect(index.data().toString()).width();
m_minimum_column_width.replace(col, std::max(m_minimum_column_width.at(col), width + margin_.left() + margin_.right())); m_minimum_column_width.replace(
col,
std::max(
m_minimum_column_width.at(col),
width + margin_.left() + margin_.right()));
} }
} }
} }
@@ -779,15 +860,24 @@ void QetGraphicsTableItem::setUpColumnAndRowMinimumSize()
void QetGraphicsTableItem::setUpBoundingRect() void QetGraphicsTableItem::setUpBoundingRect()
{ {
QSize header_size = m_header_item->rect().size(); QSize header_size = m_header_item->rect().size();
QRect rect(0, -header_size.height(), header_size.width(), m_current_size.height() + header_size.height()); QRect rect(
m_bounding_rect = rect.adjusted(-m_br_margin, -m_br_margin, m_br_margin, m_br_margin); 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 @brief QetGraphicsTableItem::adjustHandlerPos
Adjust the pos of the handler item Adjust the pos of the handler item
*/ */
void QetGraphicsTableItem::adjustHandlerPos() { void QetGraphicsTableItem::adjustHandlerPos()
{
m_handler_item.setPos(mapToScene(QRect(QPoint(0,0), size()).bottomRight())); m_handler_item.setPos(mapToScene(QRect(QPoint(0,0), size()).bottomRight()));
} }
@@ -800,22 +890,29 @@ void QetGraphicsTableItem::setUpHandler()
m_handler_item.setZValue(this->zValue() + 1); m_handler_item.setZValue(this->zValue() + 1);
} }
void QetGraphicsTableItem::handlerMousePressEvent(QGraphicsSceneMouseEvent *event) void QetGraphicsTableItem::handlerMousePressEvent(
QGraphicsSceneMouseEvent *event)
{ {
Q_UNUSED(event) Q_UNUSED(event)
diagram()->clearSelection(); diagram()->clearSelection();
this->setSelected(true); this->setSelected(true);
m_old_size = size(); m_old_size = size();
//User start to resize the table, disconnect the signal to avoid double paint. //User start to resize the table, disconnect the signal to avoid double paint.
disconnect(m_header_item, &QetGraphicsHeaderItem::sectionResized, this, &QetGraphicsTableItem::headerSectionResized); disconnect(
m_header_item,
&QetGraphicsHeaderItem::sectionResized,
this,
&QetGraphicsTableItem::headerSectionResized);
} }
void QetGraphicsTableItem::handlerMouseMoveEvent(QGraphicsSceneMouseEvent *event) void QetGraphicsTableItem::handlerMouseMoveEvent(
QGraphicsSceneMouseEvent *event)
{ {
auto new_handler_pos = Diagram::snapToGrid(event->scenePos()); auto new_handler_pos = Diagram::snapToGrid(event->scenePos());
QSize size_ = QRectF(QPointF(0,0), mapFromScene(new_handler_pos)).size().toSize(); QSize size_ = QRectF(QPointF(0,0), mapFromScene(new_handler_pos)).size().toSize();
QPoint new_pos(std::max(minimumSize().width(), size_.width()), QPoint new_pos(
std::max(minimumSize().width(), size_.width()),
std::max(minimumSize().height(), size_.height())); std::max(minimumSize().height(), size_.height()));
m_handler_item.setPos(mapToScene(new_pos)); m_handler_item.setPos(mapToScene(new_pos));
@@ -825,7 +922,8 @@ void QetGraphicsTableItem::handlerMouseMoveEvent(QGraphicsSceneMouseEvent *event
} }
} }
void QetGraphicsTableItem::handlerMouseReleaseEvent(QGraphicsSceneMouseEvent *event) void QetGraphicsTableItem::handlerMouseReleaseEvent(
QGraphicsSceneMouseEvent *event)
{ {
Q_UNUSED(event) Q_UNUSED(event)
if (diagram()) if (diagram())
@@ -836,12 +934,17 @@ void QetGraphicsTableItem::handlerMouseReleaseEvent(QGraphicsSceneMouseEvent *ev
diagram()->undoStack().push(undo); diagram()->undoStack().push(undo);
} }
//User finish to resize the table, we can reconnect now //User finish to resize the table, we can reconnect now
connect(m_header_item, &QetGraphicsHeaderItem::sectionResized, this, &QetGraphicsTableItem::headerSectionResized); connect(
m_header_item,
&QetGraphicsHeaderItem::sectionResized,
this,
&QetGraphicsTableItem::headerSectionResized);
} }
/** /**
@brief QetGraphicsTableItem::adjustColumnsWidth @brief QetGraphicsTableItem::adjustColumnsWidth
Adjust the size of each column according to the current table width by setting the sectionSize of the header item Adjust the size of each column according to the current table width
by setting the sectionSize of the header item
*/ */
void QetGraphicsTableItem::adjustColumnsWidth() void QetGraphicsTableItem::adjustColumnsWidth()
{ {
@@ -857,38 +960,59 @@ void QetGraphicsTableItem::adjustColumnsWidth()
auto a = m_current_size.width() - minimumSize().width(); auto a = m_current_size.width() - minimumSize().width();
auto b = a/std::max(1,m_model->columnCount()); //avoid divide by 0 auto b = a/std::max(1,m_model->columnCount()); //avoid divide by 0
disconnect(m_header_item, &QetGraphicsHeaderItem::sectionResized, this, &QetGraphicsTableItem::headerSectionResized); //Avoid to resize //Avoid to resize
disconnect(
m_header_item,
&QetGraphicsHeaderItem::sectionResized,
this,
&QetGraphicsTableItem::headerSectionResized);
int sum_=0; int sum_=0;
for(auto i= 0 ; i<m_model->columnCount() ; ++i) for(auto i= 0 ; i<m_model->columnCount() ; ++i)
{ {
auto at_a = std::min(m_minimum_column_width.size()-1, i); //In case of the I is higher than m_minimum_column_width or auto at_a = std::min(m_minimum_column_width.size()-1, i); //In case of the I is higher than m_minimum_column_width or
auto at_b = std::min(m_header_item->minimumSectionWidth().size()-1, i); //m_header_item->minimumSectionWidth().size() auto at_b = std::min(m_header_item->minimumSectionWidth().size()-1, i); //m_header_item->minimumSectionWidth().size()
m_header_item->resizeSection(i, std::max(m_minimum_column_width.at(at_a), m_header_item->resizeSection(
i,
std::max(
m_minimum_column_width.at(at_a),
m_header_item->minimumSectionWidth().at(at_b))+b); m_header_item->minimumSectionWidth().at(at_b))+b);
sum_+= m_header_item->sectionSize(i); sum_+= m_header_item->sectionSize(i);
} }
//The sum of the header sections width can be less than width of @m_current_size we adjust it in order to have the same width //The sum of the header sections width can be less than width of
// @m_current_size we adjust it in order to have the same width
if (m_model->columnCount() > 0 && if (m_model->columnCount() > 0 &&
sum_ < m_current_size.width()) sum_ < m_current_size.width())
{ {
//add the quotient of the division to each columns //add the quotient of the division to each columns
auto result = (m_current_size.width()-sum_)/m_model->columnCount(); auto result = (m_current_size.width()-sum_)/m_model->columnCount();
for(auto i= 0 ; i<m_model->columnCount() ; ++i) { for(auto i= 0 ; i<m_model->columnCount() ; ++i) {
m_header_item->resizeSection(i, m_header_item->sectionSize(i) + result); m_header_item->resizeSection(
i,
m_header_item->sectionSize(i) + result);
} }
//add the rest of the division to the last column //add the rest of the division to the last column
auto last_section = m_model->columnCount()-1; auto last_section = m_model->columnCount()-1;
m_header_item->resizeSection(last_section, m_header_item->sectionSize(last_section) + ((m_current_size.width()-sum_)%m_model->columnCount())); m_header_item->resizeSection(
last_section,
m_header_item->sectionSize(last_section)
+ ((m_current_size.width()-sum_)%m_model->columnCount()));
} }
connect(m_header_item, &QetGraphicsHeaderItem::sectionResized, this, &QetGraphicsTableItem::headerSectionResized); connect(
m_header_item,
&QetGraphicsHeaderItem::sectionResized,
this,
&QetGraphicsTableItem::headerSectionResized);
update(); update();
} }
void QetGraphicsTableItem::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles) void QetGraphicsTableItem::dataChanged(
const QModelIndex &topLeft,
const QModelIndex &bottomRight,
const QVector<int> &roles)
{ {
Q_UNUSED(topLeft) Q_UNUSED(topLeft)
Q_UNUSED(bottomRight) Q_UNUSED(bottomRight)
@@ -947,7 +1071,8 @@ void QetGraphicsTableItem::adjustSize()
} }
} }
void QetGraphicsTableItem::previousTableDisplayRowChanged() { void QetGraphicsTableItem::previousTableDisplayRowChanged()
{
setToMinimumHeight(); setToMinimumHeight();
if (m_next_table) { if (m_next_table) {
m_next_table->previousTableDisplayRowChanged(); m_next_table->previousTableDisplayRowChanged();

View File

@@ -47,10 +47,12 @@ class QetGraphicsTableItem : public QetGraphicsItem
Q_PROPERTY(int displayNRow READ displayNRow WRITE setDisplayNRow) Q_PROPERTY(int displayNRow READ displayNRow WRITE setDisplayNRow)
public : public :
static void adjustTableToFolio(QetGraphicsTableItem *table, QMargins margins = QMargins(20,20,20,0)); static void adjustTableToFolio(
static void checkInsufficientRowsCount(QetGraphicsTableItem *first_table); QetGraphicsTableItem *table,
QMargins margins = QMargins(20,20,20,0));
static void checkInsufficientRowsCount(
QetGraphicsTableItem *first_table);
public:
QetGraphicsTableItem(QGraphicsItem *parent= nullptr); QetGraphicsTableItem(QGraphicsItem *parent= nullptr);
virtual ~QetGraphicsTableItem() override; virtual ~QetGraphicsTableItem() override;
@@ -61,8 +63,12 @@ class QetGraphicsTableItem : public QetGraphicsItem
QAbstractItemModel *model() const; QAbstractItemModel *model() const;
virtual QRectF boundingRect() const override; virtual QRectF boundingRect() const override;
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override; virtual void paint(
QetGraphicsHeaderItem *headerItem() const {return m_header_item;} QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget) override;
QetGraphicsHeaderItem *headerItem() const
{return m_header_item;}
void setSize(const QSize &size); void setSize(const QSize &size);
QSize size() const; QSize size() const;
QSize minimumSize() const; QSize minimumSize() const;
@@ -86,10 +92,16 @@ class QetGraphicsTableItem : public QetGraphicsItem
virtual bool toDXF (const QString &filepath); virtual bool toDXF (const QString &filepath);
protected: protected:
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override; virtual void hoverEnterEvent(
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override; QGraphicsSceneHoverEvent *event) override;
virtual bool sceneEventFilter(QGraphicsItem *watched, QEvent *event) override; virtual void hoverLeaveEvent(
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value) override; QGraphicsSceneHoverEvent *event) override;
virtual bool sceneEventFilter(
QGraphicsItem *watched,
QEvent *event) override;
virtual QVariant itemChange(
GraphicsItemChange change,
const QVariant &value) override;
private: private:
void modelReseted(); void modelReseted();
@@ -101,32 +113,39 @@ class QetGraphicsTableItem : public QetGraphicsItem
void handlerMouseMoveEvent (QGraphicsSceneMouseEvent *event); void handlerMouseMoveEvent (QGraphicsSceneMouseEvent *event);
void handlerMouseReleaseEvent (QGraphicsSceneMouseEvent *event); void handlerMouseReleaseEvent (QGraphicsSceneMouseEvent *event);
void adjustColumnsWidth(); void adjustColumnsWidth();
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles); void dataChanged(
const QModelIndex &topLeft,
const QModelIndex &bottomRight,
const QVector<int> &roles);
void headerSectionResized(); void headerSectionResized();
void adjustSize(); void adjustSize();
void previousTableDisplayRowChanged(); void previousTableDisplayRowChanged();
private:
QAbstractItemModel *m_model= nullptr; QAbstractItemModel *m_model= nullptr;
QVector<int> m_minimum_column_width; QVector<int> m_minimum_column_width;
int m_minimum_row_height; int
int m_number_of_displayed_row = 0; m_minimum_row_height,
QSize m_current_size, m_number_of_displayed_row = 0,
m_br_margin = 10;
QSize
m_current_size,
m_old_size, m_old_size,
m_pending_size; m_pending_size;
int m_br_margin= 10;
QRectF m_bounding_rect; QRectF m_bounding_rect;
QetGraphicsHandlerItem m_handler_item; QetGraphicsHandlerItem m_handler_item;
QetGraphicsHeaderItem *m_header_item = nullptr; QetGraphicsHeaderItem *m_header_item = nullptr;
QetGraphicsTableItem *m_previous_table = nullptr, QetGraphicsTableItem
*m_previous_table = nullptr,
*m_next_table = nullptr; *m_next_table = nullptr;
QString m_name; QString m_name;
QUuid m_uuid = QUuid::createUuid(), QUuid
m_uuid = QUuid::createUuid(),
m_pending_previous_table_uuid; m_pending_previous_table_uuid;
}; };