1 /*
2 	Copyright 2006-2019 The QElectroTech Team
3 	This file is part of QElectroTech.
4 
5 	QElectroTech is free software: you can redistribute it and/or modify
6 	it under the terms of the GNU General Public License as published by
7 	the Free Software Foundation, either version 2 of the License, or
8 	(at your option) any later version.
9 
10 	QElectroTech is distributed in the hope that it will be useful,
11 	but WITHOUT ANY WARRANTY; without even the implied warranty of
12 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 	GNU General Public License for more details.
14 
15 	You should have received a copy of the GNU General Public License
16 	along with QElectroTech.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "diagramfoliolist.h"
19 #include <QPainter>
20 #include "qetapp.h"
21 
22 qreal DiagramFolioList::colWidths[7] = {0.05, 0.50, 0.10, 0.10, 0.05, 0.10, 0.10};
23 
24 /**
25  * @brief DiagramFolioList::DiagramFolioList
26  * Constructor
27  * @param project : The project of this diagram and also parent QObject
28  */
DiagramFolioList(QETProject * project)29 DiagramFolioList::DiagramFolioList( QETProject *project) :
30 	Diagram(project)
31 {
32 	id = project -> getFolioSheetsQuantity();
33 }
34 
35 /**
36  * @brief DiagramFolioList::~DiagramFolioList
37  * Destructor
38  */
~DiagramFolioList()39 DiagramFolioList::~DiagramFolioList()
40 {
41 	qDeleteAll (list_lines_);
42 	qDeleteAll (list_rectangles_);
43 	int folioSheetQty = project() -> getFolioSheetsQuantity();
44 	if (folioSheetQty > 0)
45 		project() -> setFolioSheetsQuantity(folioSheetQty-1);
46 }
47 
48 /**
49  * @brief DiagramFolioList::drawBackground
50  * Draw background, and call method to draw the folio list (grid)
51  * @param p painter to use
52  * @param r rectangle where we paint
53  */
drawBackground(QPainter * p,const QRectF & r)54 void DiagramFolioList::drawBackground(QPainter *p, const QRectF &r)
55 {
56 	p -> save();
57 	QSettings settings;
58 	// disable all antialiasing, except for the texts
59 	p -> setRenderHint(QPainter::Antialiasing, false);
60 	p -> setRenderHint(QPainter::TextAntialiasing, true);
61 	p -> setRenderHint(QPainter::SmoothPixmapTransform, false);
62 
63 	// draw white background
64 	p -> setPen(Qt::NoPen);
65 	p -> setBrush(Diagram::background_color);
66 	p -> drawRect(r);
67 	p -> setPen(Qt::black);
68 
69 	qreal width  = border_and_titleblock.columnsTotalWidth();
70 	qreal height = border_and_titleblock.rowsTotalHeight();
71 
72 	//top left corner of drawable area
73 	qreal x0 = border_and_titleblock.rowsHeaderWidth();
74 	qreal y0 = border_and_titleblock.columnsHeaderHeight();
75 	QRectF row_RectBorder(x0, y0, width, height);
76 
77 	buildGrid(p, row_RectBorder,30,1,colWidths);
78 
79 	x0 = list_rectangles_[0] -> topLeft().x();
80 	y0 = list_rectangles_[0] -> topLeft().y();
81 
82 	qreal rowHeight = (list_rectangles_[0] -> height())/30;
83 	QRectF row_RectF(x0, y0, list_rectangles_[0] -> width(), rowHeight);
84 
85 	fillHeader(p, row_RectF);
86 	QList<Diagram *> diagram_list = project() -> diagrams();
87 
88 	int startDiagram = id * 29;
89 	for (int i = startDiagram; i < startDiagram+29 && i < diagram_list.size(); ++i) {
90 		y0 += rowHeight;
91 		QRectF row_rect(x0, y0, list_rectangles_[0] -> width(), rowHeight);
92 		if (settings.value("genericpanel/folio", true).toBool()){
93 		fillRow(p, row_rect, diagram_list[i] -> border_and_titleblock.author(),
94 				diagram_list[i] -> title(),
95 				diagram_list[i] -> border_and_titleblock.finalfolio(),
96 				diagram_list[i] -> border_and_titleblock.plant(),
97 				diagram_list[i] -> border_and_titleblock.locmach(),
98 				diagram_list[i] -> border_and_titleblock.indexrev(),
99 				diagram_list[i] -> border_and_titleblock.date().toString(Qt::SystemLocaleShortDate));
100 	}else{
101 			fillRow(p, row_rect, diagram_list[i] -> border_and_titleblock.author(),
102 				diagram_list[i] -> title(),
103 				QString::number(diagram_list[i] ->folioIndex()+1),
104 				diagram_list[i] -> border_and_titleblock.plant(),
105 				diagram_list[i] -> border_and_titleblock.locmach(),
106 				diagram_list[i] -> border_and_titleblock.indexrev(),
107 				diagram_list[i] -> border_and_titleblock.date().toString(Qt::SystemLocaleShortDate));
108 	}
109 
110 	}
111 
112 	border_and_titleblock.draw(p);
113 	p -> restore();
114 }
115 
116 /**
117  * @brief DiagramFolioList::fillRow
118  * Add new row and fill it with the given information.
119  * @param qp Qpainter to use
120  * @param row_rect rectangle where we must draw the new row
121  */
fillRow(QPainter * qp,const QRectF & row_rect,const QString & author,const QString & title,const QString & folio,const QString & plant,const QString & loc,const QString & indexrev,const QString & date)122 void DiagramFolioList::fillRow(QPainter *qp, const QRectF &row_rect, const QString& author, const QString& title,
123 							   const QString& folio, const QString& plant, const QString& loc, const QString& indexrev, const QString& date)
124 {
125 	qreal x = row_rect.topLeft().x();
126 	qreal y = row_rect.topLeft().y();
127 	QSettings settings;
128 
129 
130 	QFontMetrics origFontMetrics(QETApp::foliolistTextsFont());
131 	qreal origFontSize = QETApp::foliolistTextsFont().pointSizeF();
132 	QFont workingFont(QETApp::foliolistTextsFont());
133 
134 	if (settings.value("genericpanel/folio", true).toBool()){
135 	// reduce the font size if the text entry is long
136 	if (origFontMetrics.width(folio) > 0.95*colWidths[0]*row_rect.width())
137 		workingFont.setPointSizeF(origFontSize * 0.95*colWidths[0]*row_rect.width() / origFontMetrics.width(folio));
138 	else
139 		workingFont.setPointSizeF(origFontSize);
140 	qp -> setFont(workingFont);
141 	qp -> drawText(QRectF(x, y, colWidths[0]*row_rect.width(), row_rect.height()), Qt::AlignCenter, folio);
142 
143 	x += colWidths[0]*row_rect.width();
144 	}else{
145 	if (origFontMetrics.width(folio) > 0.95*colWidths[0]*row_rect.width())
146 		workingFont.setPointSizeF(origFontSize * 0.95*colWidths[0]*row_rect.width() / origFontMetrics.width(folio));
147 	else
148 		workingFont.setPointSizeF(origFontSize);
149 	qp -> setFont(workingFont);
150 	qp -> drawText(QRectF(x, y, colWidths[0]*row_rect.width(), row_rect.height()), Qt::AlignCenter, folio);
151 	x += colWidths[0]*row_rect.width();
152 	}
153 
154 
155 	if (origFontMetrics.width(title) > 0.95*colWidths[1]*row_rect.width())
156 		workingFont.setPointSizeF(origFontSize * 0.95*colWidths[1]*row_rect.width() / origFontMetrics.width(title));
157 	else
158 		workingFont.setPointSizeF(origFontSize);
159 	qp -> setFont(workingFont);
160 	QString heading = tr("Titre");
161 	if (title == heading)
162 		qp -> drawText(QRectF(x, y, colWidths[1]*row_rect.width(),row_rect.height()), Qt::AlignCenter, title);
163 	else
164 		qp -> drawText(QRectF(x+0.01*row_rect.width(), y, colWidths[1]*row_rect.width()*10.2,
165 					   row_rect.height()), Qt::AlignLeft | Qt::AlignVCenter, title);
166 	x += colWidths[1]*row_rect.width();
167 
168 
169 	if (origFontMetrics.width(plant) > 0.95*colWidths[2]*row_rect.width())
170 		workingFont.setPointSizeF(origFontSize * 0.95*colWidths[2]*row_rect.width() / origFontMetrics.width(plant));
171 	else
172 		workingFont.setPointSizeF(origFontSize);
173 	qp -> setFont(workingFont);
174 	qp -> drawText(QRectF(x, y, colWidths[3]*row_rect.width(), row_rect.height()), Qt::AlignCenter, plant);
175 	x += colWidths[2]*row_rect.width();
176 
177 
178 	if (origFontMetrics.width(loc) > 0.95*colWidths[3]*row_rect.width())
179 		workingFont.setPointSizeF(origFontSize * 0.95*colWidths[3]*row_rect.width() / origFontMetrics.width(loc));
180 	else
181 		workingFont.setPointSizeF(origFontSize);
182 	qp -> setFont(workingFont);
183 	qp -> drawText(QRectF(x, y, colWidths[3]*row_rect.width(), row_rect.height()), Qt::AlignCenter, loc);
184 	x += colWidths[3]*row_rect.width();
185 
186 
187 	if (origFontMetrics.width(indexrev) > 0.95*colWidths[4]*row_rect.width())
188 		workingFont.setPointSizeF(origFontSize * 0.95*colWidths[4]*row_rect.width() / origFontMetrics.width(indexrev));
189 	else
190 		workingFont.setPointSizeF(origFontSize);
191 	qp -> setFont(workingFont);
192 	qp -> drawText(QRectF(x, y, colWidths[4]*row_rect.width(), row_rect.height()), Qt::AlignCenter, indexrev);
193 	x += colWidths[4]*row_rect.width();
194 
195 
196 
197 
198 	if (origFontMetrics.width(author) > 0.95*colWidths[5]*row_rect.width())
199 		workingFont.setPointSizeF(origFontSize * 0.95*colWidths[5]*row_rect.width() / origFontMetrics.width(author));
200 	else
201 		workingFont.setPointSizeF(origFontSize);
202 	qp -> setFont(workingFont);
203 	qp -> drawText(QRectF(x, y, colWidths[6]*row_rect.width(), row_rect.height()), Qt::AlignCenter, author);
204 	x += colWidths[5]*row_rect.width();
205 
206 
207 	if (origFontMetrics.width(date) > 0.95*colWidths[6]*row_rect.width())
208 		workingFont.setPointSizeF(origFontSize * 0.95*colWidths[6]*row_rect.width() / origFontMetrics.width(date));
209 	else
210 		workingFont.setPointSizeF(origFontSize);
211 	qp -> setFont(workingFont);
212 	qp -> drawText(QRectF(x, y, colWidths[6]*row_rect.width(), row_rect.height()), Qt::AlignCenter, date);
213 }
214 
buildGrid(QPainter * qp,const QRectF & rect,int rows,int tables,qreal colWidths[])215 void DiagramFolioList::buildGrid(QPainter *qp, const QRectF &rect, int rows, int tables, qreal colWidths[])
216 {
217 	qDeleteAll (list_lines_);
218 	list_lines_.clear();
219 	qDeleteAll (list_rectangles_);
220 	list_rectangles_.clear();
221 
222 	qreal sum = 0;
223 	for (int i = 0; i < 7; i++ )
224 		sum += colWidths[i];
225 	if ( sum < 0.99 || sum > 1.01 ) {
226 		qDebug() << "Invalid input: Column widths do not sum to 1";
227 		return;
228 	}
229 
230 	qreal tablesSpacing = rect.height() * 0.02;
231 	qreal tableWidth = (rect.width() - tablesSpacing*(tables+1) ) / tables;
232 	qreal rowHeight = (rect.height() - 2*tablesSpacing) / rows;
233 	int cols = 7;//colWidths.size();
234 
235 	qreal x0 = tablesSpacing + rect.topLeft().x();
236 	qreal y0 = tablesSpacing + rect.topLeft().y();
237 
238 	for (int i = 0; i < tables; ++i) {
239 		QRectF *tableRect = new QRectF(x0, y0, tableWidth, rect.height() - 2*tablesSpacing);
240 		qp->drawRect(*tableRect);
241 		list_rectangles_.push_back(tableRect);
242 		for (int j = 1; j < rows; ++j) {
243 			QLineF *line = new QLineF(x0, y0 + j*rowHeight, x0 + tableWidth,y0 + j*rowHeight);
244 			qp->drawLine(*line);
245 			list_lines_.push_back(line);
246 		}
247 		for (int j = 0; j < cols-1; ++j) {
248 			QLineF *line = new QLineF(x0 + colWidths[j]*tableWidth, y0, x0 + colWidths[j]*tableWidth,y0 + rows*rowHeight);
249 			qp->drawLine(*line);
250 			list_lines_.push_back(line);
251 			x0 += colWidths[j]*tableWidth;
252 		}
253 		x0 += colWidths[cols-1]*tableWidth + tablesSpacing;
254 	}
255 }
256 
257 /**
258  * @brief DiagramFolioList::fillHeader
259  * Fill the header with bigger font
260  * @param qp the painter to use
261  * @param row_RectF rectangle of header
262  */
fillHeader(QPainter * qp,const QRectF & row_RectF)263 void DiagramFolioList::fillHeader(QPainter *qp, const QRectF &row_RectF) {
264 	QString authorTranslatable(QObject::tr("Auteur"));
265 	QString titleTranslatable(QObject::tr("Titre"));
266 	QString folioTranslatable(QObject::tr("Folio"));
267 	QString plantTranslatable(QObject::tr("Installation"));
268 	QString locTranslatable(QObject::tr("Localisation"));
269 	QString indexrevTranslatable(QObject::tr("Rev"));
270 	QString dateTranslatable(QObject::tr("Date"));
271 
272 	qp->save();
273 	qp->setFont(QETApp::diagramTextsFont(13));
274 	fillRow(qp, row_RectF, authorTranslatable, titleTranslatable, folioTranslatable, plantTranslatable, locTranslatable, indexrevTranslatable, dateTranslatable);
275 	qp->restore();
276 }
277