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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
|
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import Spreadsheets
ApplicationWindow {
width: 960
height: 720
visible: true
title: qsTr("Spreadsheets")
header: HeaderToolBar {
id: toolbar
onHelpRequested: helpDialog.open()
onPasteRequested: tableView.pasteFromClipboard()
onCopyRequested: tableView.copyToClipboard()
onCutRequested: tableView.cutToClipboard()
}
background: Rectangle {
// to make contrast with the cells of the TableView,
// HorizontalHeaderView and VerticalHeaderView
color: Application.styleHints.colorScheme === Qt.Light ? palette.dark : palette.light
}
GridLayout {
id: gridlayout
anchors.fill: parent
anchors.margins: 4
columns: 2
rows: 2
columnSpacing: 3
rowSpacing: 3
ColumnHeaderView {
id: horizontalHeaderView
Layout.row: 0
Layout.column: 1
Layout.fillWidth: true
implicitHeight: 36
clip: true
syncView: tableView
spreadSelectionModel: _spreadSelectionModel
enableShowHideAction: tableView.hiddenColumnCount
onHideRequested: (column) => tableView.hideColumn(column)
onShowRequested: () => tableView.showHiddenColumns()
onResetReorderingRequested: () => tableView.resetColumnReordering()
}
RowHeaderView {
id: verticalHeaderView
Layout.fillHeight: true
implicitWidth: 50
clip: true
syncView: tableView
spreadSelectionModel: _spreadSelectionModel
enableShowHideAction: tableView.hiddenRowCount
onHideRequested: (row) => tableView.hideRow(row)
onShowRequested: () => tableView.showHiddenRows()
onResetReorderingRequested: () => tableView.resetRowReordering()
}
Item {
Layout.fillWidth: true
Layout.fillHeight: true
ScrollView {
id: scrollView
anchors.fill: parent
contentItem: TableView {
id: tableView
property int hiddenColumnCount: 0
property int hiddenRowCount: 0
clip: true
columnSpacing: 2
rowSpacing: 2
boundsBehavior: Flickable.StopAtBounds
selectionBehavior: TableView.SelectCells
selectionMode: TableView.ExtendedSelection
selectionModel: _spreadSelectionModel
model: SpreadModel
function showHiddenColumns()
{
for (let column = 0; column < columns; ++column) {
if (explicitColumnWidth(column) === 0)
setColumnWidth(column, -1)
}
hiddenColumnCount = 0
}
function hideColumn(column)
{
if (column < 0)
return
setColumnWidth(column, 0)
++hiddenColumnCount
}
function showHiddenRows()
{
for (let row = 0; row < rows; ++row) {
if (explicitRowHeight(row) === 0)
setRowHeight(row, -1)
}
hiddenRowCount = 0
}
function hideRow(row)
{
if (row < 0)
return
setRowHeight(row, 0)
++hiddenRowCount
}
function copyToClipboard()
{
mimeDataProvider.reset()
if (_spreadSelectionModel.hasSelection) {
const source_index = _spreadSelectionModel.selectedIndexes[0]
mimeDataProvider.sourceCell = cellAtIndex(source_index)
mimeDataProvider.loadSelectedData()
} else {
const current_index = _spreadSelectionModel.currentIndex
const current_cell = cellAtIndex(current_index)
mimeDataProvider.sourceCell = current_cell
mimeDataProvider.loadDataFromModel(current_cell, current_index, model)
}
}
function cutToClipboard()
{
mimeDataProvider.reset()
if (_spreadSelectionModel.hasSelection) {
const source_index = _spreadSelectionModel.selectedIndexes[0]
mimeDataProvider.sourceCell = cellAtIndex(source_index)
mimeDataProvider.loadSelectedData()
} else {
const current_index = _spreadSelectionModel.currentIndex
const current_cell = cellAtIndex(current_index)
mimeDataProvider.sourceCell = current_cell
mimeDataProvider.loadDataFromModel(current_cell, current_index, model)
}
for (let i = 0; i < mimeDataProvider.size(); ++i) {
const cell_i = mimeDataProvider.cellAt(i)
model.clearItemData(index(cell_i.y, cell_i.x))
}
}
function pasteFromClipboard()
{
visibleCellsConnection.blockConnection(true)
const current_index = _spreadSelectionModel.currentIndex
const current_cell = cellAtIndex(current_index)
let target_cells = new Set()
if (mimeDataProvider.size() === 1) {
if (_spreadSelectionModel.hasSelection) {
for (let i in _spreadSelectionModel.selectedIndexes) {
const selected_index = _spreadSelectionModel.selectedIndexes[i]
mimeDataProvider.saveDataToModel(0, selected_index, model)
target_cells.add(tableView.cellAtIndex(selected_index))
}
} else {
const old_cell = mimeDataProvider.cellAt(0)
let new_cell = Qt.point(old_cell.x, old_cell.y)
new_cell.x += current_cell.x - mimeDataProvider.sourceCell.x
new_cell.y += current_cell.y - mimeDataProvider.sourceCell.y
mimeDataProvider.saveDataToModel(0, index(new_cell.y, new_cell.x), model)
target_cells.add(new_cell)
}
} else if (mimeDataProvider.size() > 1) {
for (let i = 0; i < mimeDataProvider.size(); ++i) {
let cell_i = mimeDataProvider.cellAt(i)
cell_i.x += current_cell.x - mimeDataProvider.sourceCell.x
cell_i.y += current_cell.y - mimeDataProvider.sourceCell.y
mimeDataProvider.saveDataToModel(i, index(cell_i.y, cell_i.x), model)
target_cells.add(cell_i)
}
}
visibleCellsConnection.blockConnection(false)
visibleCellsConnection.updateViewArea()
}
function resetColumnReordering()
{
clearColumnReordering()
model.resetColumnMapping()
}
function resetRowReordering()
{
clearRowReordering()
model.resetRowMapping()
}
rowHeightProvider: function(row) {
const height = explicitRowHeight(row)
if (height === 0)
return 0
else if (height > 0)
return Math.max(height, 30)
return implicitRowHeight(row)
}
columnWidthProvider: function(column) {
const width = explicitColumnWidth(column)
if (width === 0)
return 0
else if (width > 0)
return Math.max(width, 30)
return implicitColumnWidth(column)
}
delegate: TableViewDelegate {
id: tvDelegate
implicitWidth: 90
implicitHeight: 36
leftPadding: 4
topPadding: 4
// This binding is used to avoid reimplementing whole background and
// updates only the background.color when the color scheme has changed
// for the target cells of drop event.
Binding {
target: tvDelegate.background
property: "color"
value: Application.styleHints.colorScheme === Qt.Dark
? tvDelegate.palette.highlight.darker(1.9)
: tvDelegate.palette.highlight.lighter(1.9)
when: tvDelegate.model.highlight ?? false
}
}
Keys.onPressed: function (event) {
if (event.matches(StandardKey.Copy)) {
copyToClipboard()
} else if (event.matches(StandardKey.Cut)) {
cutToClipboard()
} else if (event.matches(StandardKey.Paste)) {
pasteFromClipboard()
} else if (event.matches(StandardKey.Delete)) {
visibleCellsConnection.blockConnection()
if (_spreadSelectionModel.hasSelection)
model.clearItemData(_spreadSelectionModel.selectedIndexes)
else
model.clearItemData(_spreadSelectionModel.currentIndex)
visibleCellsConnection.blockConnection(false)
visibleCellsConnection.updateViewArea()
}
}
Connections {
id: visibleCellsConnection
target: SpreadModel
function onDataChanged(tl, br, roles)
{
updateViewArea()
}
// The model is updated, then the visible area needs to be updated as well.
// Maybe some cells need to get the display data again
// due to their data, if it's a formula.
function updateViewArea()
{
for (let row = tableView.topRow; row <= tableView.bottomRow; ++row) {
for (let column = tableView.leftColumn; column <= tableView.rightColumn; ++column) {
SpreadModel.update(row, column)
}
}
}
// Blocks/unblocks the connection. This function is useful when
// some actions may update a large amount of data in the model,
// or may update a cell which affects other cells,
// for example clipboard actions and drag/drop actions.
// Block the connection, update the model, unblock the connection,
// and the call the updateViewArea() function to update the view.
function blockConnection(block=true)
{
visibleCellsConnection.enabled = !block
}
}
MouseArea {
id: dragArea
property point dragCell: Qt.point(-1, -1)
property bool hadSelection: false
anchors.fill: parent
drag.axis: Drag.XandYAxis
drag.target: dropArea
acceptedButtons: Qt.LeftButton
cursorShape: drag.active ? Qt.ClosedHandCursor : Qt.ArrowCursor
onPressed: function(mouse) {
mouse.accepted = false
// only when Alt modifier is pressed
if (mouse.modifiers !== Qt.AltModifier)
return
// check cell under press position
const position = Qt.point(mouse.x, mouse.y)
const cell = tableView.cellAtPosition(position, true)
if (cell.x < 0 || cell.y < 0)
return
// check selected indexes
const index = tableView.index(cell.y, cell.x)
hadSelection = _spreadSelectionModel.hasSelection
if (!hadSelection)
_spreadSelectionModel.select(index, ItemSelectionModel.Select)
if (!_spreadSelectionModel.isSelected(index))
return
// store selected data
mimeDataProvider.reset()
mimeDataProvider.loadSelectedData()
// accept dragging
if (mimeDataProvider.size() > 0) {
mouse.accepted = true
dragCell = cell
}
dropArea.startDragging()
}
onReleased: {
dropArea.stopDragging()
// reset selection, if dragging caused the selection
if (!hadSelection)
_spreadSelectionModel.clearSelection()
hadSelection = false
dragCell = Qt.point(-1, -1)
}
}
}
}
DropArea {
id: dropArea
property point dropCell: Qt.point(-1, -1)
// This property keeps the interactive value to restore it after
// dragging is finished. The reason is that when the interactive
// mode is true, it steals the events and prevents the drop area
// from working.
property bool restoreInteractiveValue: tableView.interactive
anchors.fill: parent
Drag.active: dragArea.drag.active
enabled: dragArea.drag.active
function startDragging()
{
restoreInteractiveValue = tableView.interactive
tableView.interactive = false
// block updating visible area
visibleCellsConnection.blockConnection()
}
function stopDragging()
{
Drag.drop()
// unblock update visible area
visibleCellsConnection.blockConnection(false)
visibleCellsConnection.updateViewArea() // now update visible area
tableView.interactive = restoreInteractiveValue
}
function isDropPossible(dropCell) {
for (let i = 0; i < mimeDataProvider.size(); ++i) {
let cell = mimeDataProvider.cellAt(i)
cell.x += dropCell.x - dragArea.dragCell.x
cell.y += dropCell.y - dragArea.dragCell.y
const index = tableView.index(cell.y, cell.x)
if (!index.valid)
return false
}
return true
}
onDropped: {
const position = Qt.point(dragArea.mouseX, dragArea.mouseY)
dropCell = tableView.cellAtPosition(position, true)
if (dropCell.x < 0 || dropCell.y < 0)
return
if (dragArea.dragCell === dropCell)
return
if (!isDropPossible(dropCell)) {
tableView.model.clearHighlight()
return
}
tableView.model.clearItemData(_spreadSelectionModel.selectedIndexes)
for (let i = 0; i < mimeDataProvider.size(); ++i) {
let cell = mimeDataProvider.cellAt(i)
cell.x += dropCell.x - dragArea.dragCell.x
cell.y += dropCell.y - dragArea.dragCell.y
const index = tableView.index(cell.y, cell.x)
mimeDataProvider.saveDataToModel(i, index, tableView.model)
}
mimeDataProvider.reset()
_spreadSelectionModel.clearSelection()
const drop_index = tableView.index(dropCell.y, dropCell.x)
_spreadSelectionModel.setCurrentIndex(drop_index, ItemSelectionModel.Current)
tableView.model.clearHighlight()
}
onPositionChanged: {
const position = Qt.point(dragArea.mouseX, dragArea.mouseY)
// cell is the cell that currently mouse is over it
const cell = tableView.cellAtPosition(position, true)
// dropCell is the cell that it was under the mouse's last position
// if the last and current cells are the same, then there is no need
// to update highlight, as nothing is changed since last time.
if (cell === dropCell)
return
if (!isDropPossible(cell)) {
tableView.model.clearHighlight()
return
}
// if something is changed, it means that if the current cell is changed,
// then clear highlighted cells and update the dropCell.
tableView.model.clearHighlight()
dropCell = cell
// if the current cell was invalid (mouse is out side of the TableView)
// then no need to update highlight
if (cell.x < 0 || cell.y < 0)
return
// if dragged cell is the same as the (possibly) dropCell
// then no need to highlight any cells
if (dragArea.dragCell === dropCell)
return
// if the dropCell is not the same as the dragging cell and also
// is not the same as the cell at the mouse's last position
// then highlights the target cells
for (let i in _spreadSelectionModel.selectedIndexes) {
const old_index = _spreadSelectionModel.selectedIndexes[i]
let cell = tableView.cellAtIndex(old_index)
cell.x += dropCell.x - dragArea.dragCell.x
cell.y += dropCell.y - dragArea.dragCell.y
const new_index = tableView.index(cell.y, cell.x)
tableView.model.setHighlight(new_index, true)
}
}
}
}
}
SelectionRectangle {
id: selectionRectangle
target: tableView
selectionMode: SelectionRectangle.Auto
topLeftHandle: Rectangle {
width: 12
height: 12
radius: 6
color: palette.highlight.lighter(1.4)
border.width: 2
border.color: palette.base
visible: SelectionRectangle.control.active
}
bottomRightHandle: Rectangle {
width: 12
height: 12
radius: 6
color: palette.highlight.lighter(1.4)
border.width: 2
border.color: palette.base
visible: SelectionRectangle.control.active
}
}
SpreadSelectionModel {
id: _spreadSelectionModel
behavior: SpreadSelectionModel.SelectCells
}
SpreadMimeDataProvider {
id: mimeDataProvider
property point sourceCell: Qt.point(-1, -1)
function loadSelectedData()
{
for (let i in _spreadSelectionModel.selectedIndexes) {
const index = _spreadSelectionModel.selectedIndexes[i]
const cell = tableView.cellAtIndex(index)
loadDataFromModel(cell, index, tableView.model)
}
}
function resetProvider()
{
sourceCell = Qt.point(-1, -1)
reset()
}
}
HelpDialog {
id: helpDialog
anchors.centerIn: parent
}
}
|