1 /*
2 Copyright (C) 2011 Elvis Stansvik <elvstone@gmail.com>
3 
4 For general Scribus (>=1.3.2) copyright and licensing information please refer
5 to the COPYING file provided with the program. Following this notice may exist
6 a copyright and/or license notice that predates the release of Scribus 1.3.2
7 for which a new license (GPL+exception) is in place.
8 */
9 
10 #include <algorithm>
11 
12 #include <QMouseEvent>
13 #include <QKeyEvent>
14 #include <QPainter>
15 #include <QPointF>
16 
17 #include "canvas.h"
18 #include "fpoint.h"
19 #include "pageitem.h"
20 #include "pageitem_table.h"
21 #include "scribusdoc.h"
22 #include "scribusview.h"
23 
24 #include "canvasgesture_tableresize.h"
25 
keyPressEvent(QKeyEvent * event)26 void TableResize::keyPressEvent(QKeyEvent* event)
27 {
28 	if (event->key() == Qt::Key_Escape)
29 	{
30 		// Cancel the resize.
31 		event->accept();
32 		m_view->stopGesture();
33 	}
34 }
35 
mouseReleaseEvent(QMouseEvent * event)36 void TableResize::mouseReleaseEvent(QMouseEvent* event)
37 {
38 	event->accept();
39 
40 	QPointF gridPoint = globalToTableGrid(event->globalPos());
41 
42 	// Perform the actual resize of the table.
43 	table()->resize(gridPoint.x(), gridPoint.y());
44 	table()->doc()->dontResize = true;
45 	table()->adjustFrameToTable();
46 	table()->doc()->dontResize = false;
47 	table()->update();
48 
49 	m_view->stopGesture();
50 }
51 
mouseMoveEvent(QMouseEvent * event)52 void TableResize::mouseMoveEvent(QMouseEvent* event)
53 {
54 	event->accept();
55 
56 	QPointF gridPoint = globalToTableGrid(event->globalPos());
57 
58 	// Set the column  geometries for the table outline.
59 	double requestedWidthFactor = gridPoint.x() / table()->tableWidth();
60 	double newMinWidth = qMax(m_minWidth * requestedWidthFactor, PageItem_Table::MinimumColumnWidth);
61 	double actualWidthFactor = newMinWidth / m_minWidth;
62 	for (int col = 0; col < m_columnWidths.size(); ++col)
63 	{
64 		m_columnWidths[col] = table()->columnWidth(col) * actualWidthFactor;
65 		m_columnPositions[col] = table()->columnPosition(col) * actualWidthFactor;
66 	}
67 
68 	// Set the row geometries for the table outline.
69 	double requestedHeightFactor = gridPoint.y() / table()->tableHeight();
70 	double newMinHeight = qMax(m_minHeight * requestedHeightFactor, PageItem_Table::MinimumRowHeight);
71 	double actualHeightFactor = newMinHeight / m_minHeight;
72 	for (int row = 0; row < m_rowHeights.size(); ++row)
73 	{
74 		m_rowHeights[row] = table()->rowHeight(row) * actualHeightFactor;
75 		m_rowPositions[row] = table()->rowPosition(row) * actualHeightFactor;
76 	}
77 
78 	// Display size tooltip.
79 	double actualTableWidth = table()->tableWidth() * actualWidthFactor;
80 	double actualTableHeight = table()->tableHeight() * actualHeightFactor;
81 	m_canvas->displaySizeHUD(event->globalPos(), actualTableWidth, actualTableHeight, false);
82 
83 	m_canvas->update();
84 }
85 
drawControls(QPainter * p)86 void TableResize::drawControls(QPainter* p)
87 {
88 	p->save();
89 	commonDrawControls(p, false);
90 	p->restore();
91 
92 	// Paint the table outline using the changed table geometries.
93 	paintTableOutline(m_rowHeights, m_rowPositions, m_columnWidths, m_columnPositions, p);
94 }
95 
setup(PageItem_Table * table)96 void TableResize::setup(PageItem_Table* table)
97 {
98 	Q_ASSERT(table);
99 
100 	setTable(table);
101 
102 	// Make copies of the row and column geometries to be used during resize.
103 	m_rowHeights = table->rowHeights();
104 	m_rowPositions = table->rowPositions();
105 	m_columnWidths = table->columnWidths();
106 	m_columnPositions = table->columnPositions();
107 
108 	// Save the minimum row height and column width.
109 	m_minHeight = *std::min_element(m_rowHeights.begin(), m_rowHeights.end());
110 	m_minWidth = *std::min_element(m_columnWidths.begin(), m_columnWidths.end());
111 }
112