Hi everybody,
I'm busy for a long time on this project, and currently it's killing me.
The goal is to print a excel sheet look-a-like this is the code that I wrote.
The function that invokes the proces
Code:
void MainWindow::printSurvey() { QPrinter printer; { TablePrint *print = new TablePrint(printer, this); print->setOffsetTop(10); print->setOffsetBottom(10); print->setOffsetLeft(10); print->setOffsetRight(10); QMap<int, QMap<QString, QString> > header; QMap<int, QMap<int, QMap<QString, QString> > > data; //header format header[columnNumber][identifier] = value header[0]["width"] = "100"; header[0]["height"] = "20"; header[0]["fillColor"] = "blue"; header[0]["text"] = "column1"; header[1]["width"] = "100"; header[1]["height"] = "20"; header[1]["fillColor"] = "blue"; header[1]["text"] = "column2"; header[2]["width"] = "100"; header[2]["height"] = "20"; header[2]["fillColor"] = "blue"; header[2]["text"] = "column3"; //data format data[rowNumber][columnNumber][identifier] = value data[0][0]["text"] = "column_1_row_1_artikel"; data[0][0]["fillColor"] = "white"; data[0][1]["text"] = "column_2_row_1_artikel"; data[0][1]["fillColor"] = "red"; data[0][2]["text"] = "column_3_row_1_artikel"; data[0][2]["fillColor"] = "green"; data[1][0]["text"] = "column_1_row_2_artikel"; data[1][0]["fillColor"] = "white"; data[1][1]["text"] = "column_2_row_2_artikel"; data[1][1]["fillColor"] = "red"; data[1][2]["text"] = "column_3_row_2_artikel"; data[1][2]["fillColor"] = "green"; data[2][0]["text"] = "column_1_row_3_artikel"; data[2][0]["fillColor"] = "white"; data[2][1]["text"] = "column_2_row_3_artikel"; data[2][1]["fillColor"] = "red"; data[2][2]["text"] = "column_3_row_3_artikel"; data[2][2]["fillColor"] = "green"; data[3][0]["text"] = "column_1_row_4_artikel"; data[3][0]["fillColor"] = "white"; data[3][1]["text"] = "column_2_row_4_artikel"; data[3][1]["fillColor"] = "red"; data[3][2]["text"] = "column_3_row_4_artikel"; data[3][2]["fillColor"] = "green"; print->dataPrint(header, data); delete print; } }
I divided the responsibility to several scripts:
- Tableprint
- Tablerow
- Tablecolumn
- Tablecell
Maybe you all ready noticed that you don't need 3 things to receive the coordinates of a 2D frame.
So what I did was delegating the responsibility to table row. So you can see tablerow as the controller. The row controls the columns which controls the cells.
A simple example, when you want to retrieve row 2 column 1 than you will access the table row, which can access the column in this case 1 and the column contains all the cell beneath him, so we pass the parameter 2 so the column knows that we want to retrieve cell 2.
I thought that this idea would be the solution to my problem, but it's giving me troubles.
Here are the other scripts:
Tableprint.h
Code:
#ifndef TABLEPRINT_H #define TABLEPRINT_H #include <QObject> #include <QPainter> #include <QStringList> #include <QPrinter> #include <QString> #include <QList> class TableRow; { Q_OBJECT public: //setters void setOffsetTop(int offset); void setOffsetLeft(int offset); void setOffsetRight(int offset); void setOffsetBottom(int offset); void setDynamicHeight(bool b); //print functions void dataPrint(QMap<int, QMap<QString, QString> > header, QMap<int, QMap<int, QMap<QString, QString> > > data); ~TablePrint(); private: int offsetTop; int offsetLeft; int offsetRight; int offsetBottom; int width; int height; bool dynamicHeight; int rows; TableRow *tableRow; QPainter painter; }; #endif
Tableprint.cpp
Code:
#include "tablePrint.h" #include <QtGui> #include <QPainter> #include <QPrinter> #include "tableRow.h" #include <QMap> #include <QMapIterator> qDebug("TablePrint::TablePrint():start printing"); this->rows = 0; this->offsetTop = 0; this->offsetRight = 0; this->offsetBottom = 0; this->offsetLeft = 0; this->dynamicHeight = false; this->width = 0; this->height = 0; this->width = printer.pageRect().width() - this->offsetLeft - this->offsetRight; this->height = printer.pageRect().height() - this->offsetBottom - this->offsetTop; } void TablePrint::dataPrint(QMap<int, QMap<QString, QString> > header, QMap<int, QMap<int, QMap<QString, QString> > > data) { qDebug("TablePrint::dataPrint():printing row data"); this->tableRow = new TableRow(QRect(this->offsetLeft, this->offsetTop, (this->width - this->offsetLeft - this->offsetRight), (this->height - this->offsetTop - this->offsetBottom)), this); //set the header data this->tableRow->setHeaderData(header); //sets the dynamic height to false, so all the cells will have the same height this->tableRow->setDynamicHeight(false); QMap<int, QMap<int, QMap<QString, QString> > >::const_iterator i = data.constBegin(); //loop the iterator while(i != data.constEnd() ) { QMap<int, QMap<QString, QString> > rowData = data.value(i.key()); this->tableRow->setRowData(rowData); this->rows++; i++; } //column print this->tableRow->draw(0, this->painter); for( int i = 2; i < (this->rows+2); i++) { this->tableRow->draw(i, this->painter); } this->painter.end(); } void TablePrint::setOffsetTop(int offset){this->offsetTop = offset; if(!this->offsetBottom) this->offsetBottom = offset;}; void TablePrint::setOffsetLeft(int offset){this->offsetLeft = offset; if(!this->offsetRight) this->offsetRight = offset;}; void TablePrint::setOffsetRight(int offset){this->offsetRight = offset;}; void TablePrint::setOffsetBottom(int offset){this->offsetBottom = offset;}; void TablePrint::setDynamicHeight(bool b){this->dynamicHeight = b;};
Tablerow.h
Code:
#ifndef TABLEROW_H #define TABLEROW_H #include <QObject> #include <QPainter> #include <QStringList> #include <QRect> class TableCell; class TableColumn; { Q_OBJECT public: void setHeaderData(QMap<int, QMap<QString, QString> > data); void setRowData(QMap<int, QMap<QString, QString> > data); void draw(int row, QPainter&); void setRemember(bool rememberLocal){this->remember = rememberLocal;}; void setDynamicHeight(bool b) {this->dynamicHeight = b;}; int getMaxColumnHeight(){return this->maxColumnHeight;}; ~TableRow(); private: int columns; int currentRow; int maxColumnHeight; TableColumn *tableColumns[]; QRect rectArea; bool remember; int offsetTop; int offsetLeft; int width; bool dynamicHeight; QObject *parent; }; #endif
To be continued in the next post