forked from scribusproject/scribus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvasgesture_rowresize.cpp
More file actions
170 lines (139 loc) · 4.43 KB
/
canvasgesture_rowresize.cpp
File metadata and controls
170 lines (139 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
Copyright (C) 2011 Elvis Stansvik <elvstone@gmail.com>
For general Scribus (>=1.3.2) copyright and licensing information please refer
to the COPYING file provided with the program. Following this notice may exist
a copyright and/or license notice that predates the release of Scribus 1.3.2
for which a new license (GPL+exception) is in place.
*/
#include <QMouseEvent>
#include <QKeyEvent>
#include <QPainter>
#include <QPointF>
#include "canvas.h"
#include "pageitem.h"
#include "pageitem_table.h"
#include "scribusdoc.h"
#include "scribusview.h"
#include "undomanager.h"
#include "canvasgesture_rowresize.h"
void RowResize::keyPressEvent(QKeyEvent* event)
{
if (event->key() == Qt::Key_Escape)
{
// Cancel the resize.
event->accept();
m_view->stopGesture();
}
if (event->key() == Qt::Key_Shift)
{
// Re-copy row geometires since resizing behavior will change.
m_rowHeights = table()->rowHeights();
m_rowPositions = table()->rowPositions();
}
}
void RowResize::keyReleaseEvent(QKeyEvent* event)
{
if (event->key() == Qt::Key_Shift)
{
// Re-copy row geometires since resizing behavior will change.
m_rowHeights = table()->rowHeights();
m_rowPositions = table()->rowPositions();
}
}
void RowResize::mouseReleaseEvent(QMouseEvent* event)
{
event->accept();
QPointF gridPoint = globalToTableGrid(event->globalPosition());
// Perform the actual resize of the row.
PageItem_Table::ResizeStrategy strategy;
if (event->modifiers() & Qt::ShiftModifier)
strategy = PageItem_Table::MoveFollowing;
else
strategy = PageItem_Table::ResizeFollowing;
UndoTransaction activeTransaction;
if (UndoManager::undoEnabled())
activeTransaction = UndoManager::instance()->beginTransaction(table()->getUName(), table()->getUPixmap(), Um::TableRowHeight, QString(), Um::ITable);
table()->doc()->dontResize = true;
table()->resizeRow(m_row, gridPoint.y() - table()->rowPosition(m_row), strategy);
if (strategy == PageItem_Table::MoveFollowing)
{
table()->adjustTableToFrame();
table()->adjustFrameToTable();
}
table()->doc()->dontResize = false;
table()->adjustFrameToTable();
table()->doc()->setRedrawBounding(table());
table()->update();
if (activeTransaction)
activeTransaction.commit();
m_view->stopGesture();
}
void RowResize::mouseMoveEvent(QMouseEvent* event)
{
event->accept();
QPointF gridPoint = globalToTableGrid(event->globalPosition());
double requestedHeight = gridPoint.y() - m_rowPositions[m_row];
double actualHeight = 0.0;
if (event->modifiers() & Qt::ShiftModifier)
actualHeight = resizeRowMoveFollowing(requestedHeight);
else
actualHeight = resizeRowResizeFollowing(requestedHeight);
// Display height tooltip.
m_canvas->displayDoubleHUD(event->globalPosition(), tr("Height"), actualHeight);
// Update canvas.
m_canvas->update();
}
void RowResize::drawControls(QPainter* p)
{
p->save();
commonDrawControls(p, false);
p->restore();
// Paint the table outline using the changed row geometries.
paintTableOutline(m_rowHeights, m_rowPositions,
table()->columnWidths(), table()->columnPositions(), p);
}
void RowResize::setup(PageItem_Table* table, int row)
{
Q_ASSERT(table);
Q_ASSERT(row >= 0 && row < table->rows());
setTable(table);
m_row = row;
// Make copies of the row geometries to be used during resize.
m_rowHeights = table->rowHeights();
m_rowPositions = table->rowPositions();
}
double RowResize::resizeRowMoveFollowing(double height)
{
// Set row height.
double newHeight = m_rowHeights[m_row] = qMax(PageItem_Table::MinimumRowHeight, height);
// Move following rows.
double rowPosition = m_rowPositions[m_row];
for (int row = m_row; row < m_rowPositions.size(); ++row)
{
m_rowPositions[row] = rowPosition;
rowPosition += m_rowHeights[row];
}
return newHeight;
}
double RowResize::resizeRowResizeFollowing(double height)
{
double oldHeight = m_rowHeights[m_row];
double newHeight = 0.0;
if (m_row < table()->rows() - 1)
{
// Following row exists, so height is bounded at both ends.
newHeight = m_rowHeights[m_row] = qBound(
PageItem_Table::MinimumRowHeight, height,
oldHeight + m_rowHeights[m_row + 1] - PageItem_Table::MinimumRowHeight);
// Resize/move following row.
double heightChange = newHeight - oldHeight;
m_rowPositions[m_row + 1] += heightChange;
m_rowHeights[m_row + 1] -= heightChange;
}
else
{
// Last row, so height only bounded by MinimumRowHeight.
newHeight = m_rowHeights[m_row] = qMax(PageItem_Table::MinimumRowHeight, height);
}
return newHeight;
}