Tablerow.cpp
#include "tableRow.h"
#include <QMap>
#include <QList>
#include <QMapIterator>
#include <QDebug>
#include "tableCellPrinter.h"
#include "tableColumnPrint.h"
{
qDebug("TableRow::TableRow(): Start drawing the tables");
this->maxColumnHeight = 0;
qDebug("rect.width:%i", rect.width());
this->offsetLeft = rect.x();
this->offsetTop = rect.y();
this->width = rect.width();
this->currentRow = 0;
}
void TableRow::setHeaderData(QMap<int, QMap<QString, QString> > data)
{
qDebug("TableRow::setHeaderData(): Set the header data");
this->columns = data.count();
QMap<int, QMap<QString, QString> >::const_iterator i = data.constBegin();
int totalOffsetLeft = 0;
//loop the iterator
while(i != data.constEnd() )
{
QMap<QString, QString> rowData = data.value(i.key());
QRect rectColumn
(totalOffsetLeft, this
->offsetTop, rowData
["width"].
toInt(),
0);
//remember the highest height, so we can apply it to all columns!
if(rowData["height"].toInt() > this->maxColumnHeight)
this->maxColumnHeight = rowData["height"].toInt();
qDebug("keeeeyyy:%i", i.key());
this
->tableColumns
[i.
key()] = new TableColumn
(QColor(rowData
["fillColor"]), rowData
["text"], rectColumn,
this);
i++;
totalOffsetLeft += rowData["width"].toInt();
}
}
void TableRow::setRowData(QMap<int, QMap<QString, QString> > data)
{
qDebug("TableRow::setRowData(): set the row data");
// add cell to the matching table column!
qDebug("this->width:%i", this->width);
QMap<int, QMap<QString, QString> >::const_iterator i = data.constBegin();
//loop the iterator
while(i != data.constEnd() )
{
QMap<QString, QString> rowData = data.value(i.key());
if(rowData["height"].toInt() > this->maxColumnHeight)
this->maxColumnHeight = rowData["height"].toInt();
qDebug("column number to add a cell to:%i", i.key());
this
->tableColumns
[i.
key()]->addCell
(this
->currentRow,
QColor(rowData
["fillColor"]),
QString(rowData
["text"]));
i++;
}
this->currentRow++;
}
void TableRow
::draw(int row,
QPainter &painter
) {
//lets start drawing!
qDebug("TableRow:draw(): start drawing");
qDebug("this->width:%i", this->width);
int offsetLeftColumn = this->offsetLeft;
int offsetTopColumn = (row == 0)?this->offsetTop : this->offsetTop + this->maxColumnHeight*(row-1);
//loop each column
for(int i = 0; i < this->columns; i++)
{
//check if the column stays on the page
if(offsetLeftColumn + this->tableColumns[i]->columnWidth < this->width ){
this->tableColumns[i]->draw(row, offsetLeftColumn, offsetTopColumn, painter);
offsetLeftColumn += this->tableColumns[i]->columnWidth;
}else{
qDebug("column width:%i width:%i", offsetLeftColumn + this->tableColumns[i]->columnWidth, this->width);
}
}
offsetLeftColumn = 0;
offsetTopColumn = 0;
}
TableRow::~TableRow()
{
qDebug("Destruct rows");
for(int i = 0; i < this->columns; i++)
{
delete this->tableColumns[i];
}
}
#include "tableRow.h"
#include <QMap>
#include <QList>
#include <QMapIterator>
#include <QDebug>
#include "tableCellPrinter.h"
#include "tableColumnPrint.h"
TableRow::TableRow(QRect rect, QObject *parent)
: QObject(parent), parent(parent)
{
qDebug("TableRow::TableRow(): Start drawing the tables");
this->maxColumnHeight = 0;
qDebug("rect.width:%i", rect.width());
this->offsetLeft = rect.x();
this->offsetTop = rect.y();
this->width = rect.width();
this->currentRow = 0;
}
void TableRow::setHeaderData(QMap<int, QMap<QString, QString> > data)
{
qDebug("TableRow::setHeaderData(): Set the header data");
this->columns = data.count();
QMap<int, QMap<QString, QString> >::const_iterator i = data.constBegin();
int totalOffsetLeft = 0;
//loop the iterator
while(i != data.constEnd() )
{
QMap<QString, QString> rowData = data.value(i.key());
QRect rectColumn(totalOffsetLeft, this->offsetTop, rowData["width"].toInt(), 0);
//remember the highest height, so we can apply it to all columns!
if(rowData["height"].toInt() > this->maxColumnHeight)
this->maxColumnHeight = rowData["height"].toInt();
qDebug("keeeeyyy:%i", i.key());
this->tableColumns[i.key()] = new TableColumn(QColor(rowData["fillColor"]), rowData["text"], rectColumn, this);
i++;
totalOffsetLeft += rowData["width"].toInt();
}
}
void TableRow::setRowData(QMap<int, QMap<QString, QString> > data)
{
qDebug("TableRow::setRowData(): set the row data");
// add cell to the matching table column!
qDebug("this->width:%i", this->width);
QMap<int, QMap<QString, QString> >::const_iterator i = data.constBegin();
//loop the iterator
while(i != data.constEnd() )
{
QMap<QString, QString> rowData = data.value(i.key());
if(rowData["height"].toInt() > this->maxColumnHeight)
this->maxColumnHeight = rowData["height"].toInt();
qDebug("column number to add a cell to:%i", i.key());
this->tableColumns[i.key()]->addCell(this->currentRow, QColor(rowData["fillColor"]), QString(rowData["text"]));
i++;
}
this->currentRow++;
}
void TableRow::draw(int row, QPainter &painter)
{
//lets start drawing!
qDebug("TableRow:draw(): start drawing");
qDebug("this->width:%i", this->width);
int offsetLeftColumn = this->offsetLeft;
int offsetTopColumn = (row == 0)?this->offsetTop : this->offsetTop + this->maxColumnHeight*(row-1);
//loop each column
for(int i = 0; i < this->columns; i++)
{
//check if the column stays on the page
if(offsetLeftColumn + this->tableColumns[i]->columnWidth < this->width ){
this->tableColumns[i]->draw(row, offsetLeftColumn, offsetTopColumn, painter);
offsetLeftColumn += this->tableColumns[i]->columnWidth;
}else{
qDebug("column width:%i width:%i", offsetLeftColumn + this->tableColumns[i]->columnWidth, this->width);
}
}
offsetLeftColumn = 0;
offsetTopColumn = 0;
}
TableRow::~TableRow()
{
qDebug("Destruct rows");
for(int i = 0; i < this->columns; i++)
{
delete this->tableColumns[i];
}
}
To copy to clipboard, switch view to plain text mode
Tablecolumn.h
#ifndef TABLECOLUMNPRINT_H
#define TABLECOLUMNPRINT_H
#include <QObject>
#include "tableCellPrinter.h"
{
Q_OBJECT
public:
void draw
(int row,
int offsetLeft,
int offsetTop,
QPainter &painter
);
int columnWidth;
~TableColumn();
TableCell *cells[];
private:
int columnHeight;
int cellsCount;
};
#endif
#ifndef TABLECOLUMNPRINT_H
#define TABLECOLUMNPRINT_H
#include <QObject>
#include "tableCellPrinter.h"
class QRect;
class QPainter;
class TableColumn : public QObject
{
Q_OBJECT
public:
TableColumn(QColor colorfill, QString text, QRect &rectColumn, QObject *parent);
void addCell(int row, QColor colorfill, QString text);
void draw(int row, int offsetLeft, int offsetTop, QPainter &painter);
int columnWidth;
~TableColumn();
TableCell *cells[];
private:
int columnHeight;
int cellsCount;
QObject *parent;
};
#endif
To copy to clipboard, switch view to plain text mode
Tablecolumn.cpp
#include "tableColumnPrint.h"
#include "tableCellPrinter.h"
#include <QColor>
#include <QString>
#include <QRect>
#include <QPainter>
#include <QDebug>
{
qDebug("TableColumn::TableColumn(): Define header column");
this->cellsCount = 0;
this->columnWidth = rectColumn.width();
this->columnHeight = rectColumn.height();
this->cells[0] = new TableCell(colorfill, text, this);
this->parent = parent;
}
void TableColumn
::addCell(int row,
QColor colorfill,
QString text
) {
qDebug("TableColumn::addCell(): add a cell to the column");
row += 2;
this->cells[row] = new TableCell( colorfill, text, this);
qDebug("###########################################");
qDebug() << this->cells[row]->text;
qDebug("column:");
qDebug() << this->cells[0]->text;
qDebug("cel number for this column:%i", (row));
qDebug("###########################################");
//the plus one is there so we don't overwrite the header column
this->cellsCount = row;
}
void TableColumn
::draw(int row,
int offsetLeft,
int offsetTop,
QPainter &painter
) {
//draw the row
qDebug("TableColumn:draw(): start drawing");
painter.setBrush(this->cells[row]->fillColor);
painter.setPen(Qt::black);
qDebug("Drawing area: offsetLeft:%i offsetTop:%i width:%i height:%i", offsetLeft, offsetTop, this->columnWidth, this->columnHeight);
QRect rowRect
(offsetLeft, offsetTop , this
->columnWidth,
20);
qDebug("row for this column:%i", row);
painter.drawRect(rowRect);
this->cells[row]->draw(painter, this->cells[row]->text, offsetLeft, offsetTop , this->columnWidth, 20);
}
TableColumn::~TableColumn()
{
qDebug("Destruct column");
delete this->cells;
}
#include "tableColumnPrint.h"
#include "tableCellPrinter.h"
#include <QColor>
#include <QString>
#include <QRect>
#include <QPainter>
#include <QDebug>
TableColumn::TableColumn(QColor colorfill, QString text, QRect &rectColumn, QObject *parent)
:QObject(parent), cellsCount(0)
{
qDebug("TableColumn::TableColumn(): Define header column");
this->cellsCount = 0;
this->columnWidth = rectColumn.width();
this->columnHeight = rectColumn.height();
this->cells[0] = new TableCell(colorfill, text, this);
this->parent = parent;
}
void TableColumn::addCell(int row, QColor colorfill, QString text)
{
qDebug("TableColumn::addCell(): add a cell to the column");
row += 2;
this->cells[row] = new TableCell( colorfill, text, this);
qDebug("###########################################");
qDebug() << this->cells[row]->text;
qDebug("column:");
qDebug() << this->cells[0]->text;
qDebug("cel number for this column:%i", (row));
qDebug("###########################################");
//the plus one is there so we don't overwrite the header column
this->cellsCount = row;
}
void TableColumn::draw(int row, int offsetLeft, int offsetTop, QPainter &painter)
{
//draw the row
qDebug("TableColumn:draw(): start drawing");
painter.setBrush(this->cells[row]->fillColor);
painter.setPen(Qt::black);
qDebug("Drawing area: offsetLeft:%i offsetTop:%i width:%i height:%i", offsetLeft, offsetTop, this->columnWidth, this->columnHeight);
QRect rowRect(offsetLeft, offsetTop , this->columnWidth, 20);
qDebug("row for this column:%i", row);
painter.drawRect(rowRect);
this->cells[row]->draw(painter, this->cells[row]->text, offsetLeft, offsetTop , this->columnWidth, 20);
}
TableColumn::~TableColumn()
{
qDebug("Destruct column");
delete this->cells;
}
To copy to clipboard, switch view to plain text mode
Tablecell.h
#ifndef TABLECELLPRINTER_H
#define TABLECELLPRINTER_H
#include <QObject>
#include <QColor>
#include <QString>
#include <QRect>
{
Q_OBJECT
public:
void draw
(QPainter
&,
QString &txt,
int offsetLeft,
int offsetTop ,
int width,
int height
);
~TableCell();
};
#endif
#ifndef TABLECELLPRINTER_H
#define TABLECELLPRINTER_H
#include <QObject>
#include <QColor>
#include <QString>
#include <QRect>
class QPainter;
class TableCell : public QObject
{
Q_OBJECT
public:
TableCell(QColor fillColor, QString &txt, QObject *parent =0);
void draw(QPainter&, QString &txt, int offsetLeft, int offsetTop , int width, int height);
~TableCell();
QColor fillColor;
QString text;
};
#endif
To copy to clipboard, switch view to plain text mode
Tablecell.cpp
#include "tableCellPrinter.h"
#include <QDebug>
#include <QPainter>
#include <QString>
#include <QObject>
{
qDebug("TableCell::TableCell(): Initialize a new cell");
this->fillColor = fillColor;
}
void TableCell
::draw(QPainter &painter,
QString &txt,
int offsetLeft,
int offsetTop ,
int width,
int height
) {
//start drawing the text
qDebug("TableCell:draw(): start drawing");
painter.
drawText(QRect(offsetLeft, offsetTop, width, height
), Qt
::AlignCenter, txt
);
}
TableCell::~TableCell()
{
qDebug("destruct cell");
qDebug() << this->text;
}
#include "tableCellPrinter.h"
#include <QDebug>
#include <QPainter>
#include <QString>
#include <QObject>
TableCell::TableCell(QColor fillColor, QString &txt, QObject *parent)
:QObject(parent)
{
qDebug("TableCell::TableCell(): Initialize a new cell");
this->fillColor = fillColor;
this->text = QString(txt);
}
void TableCell::draw(QPainter &painter, QString &txt, int offsetLeft, int offsetTop , int width, int height)
{
//start drawing the text
qDebug("TableCell:draw(): start drawing");
painter.drawText(QRect(offsetLeft, offsetTop, width, height), Qt::AlignCenter, txt);
}
TableCell::~TableCell()
{
qDebug("destruct cell");
qDebug() << this->text;
}
To copy to clipboard, switch view to plain text mode
The script works fine with some test data inserted in mainWindow.cpp but when I add more test data, let's say 4 rows and 1 column row.
The whole program crashes, and in my debugger I saw that it crashed when the destructor of the QObject is called!
Can somebody help me please, I'm desperate.
Many many thanks,
Cyberboy
Bookmarks