Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9b9fb51

Browse files
committed
Implemented multiple BLOB cells copy/paste
1 parent a514123 commit 9b9fb51

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

src/ExtendedTableWidget.cpp

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,33 @@ void ExtendedTableWidget::copy()
5757
return;
5858
}
5959

60+
m_buffer.clear();
61+
62+
// If any of the cells contain binary data - we use inner buffer
63+
bool containsBinary = false;
64+
SqliteTableModel* m = qobject_cast<SqliteTableModel*>(model());
65+
foreach (const QModelIndex& index, indices)
66+
if (m->isBinary(index)) {
67+
containsBinary = true;
68+
break;
69+
}
70+
71+
if (containsBinary) {
72+
qApp->clipboard()->clear();
73+
// Copy selected data into inner buffer
74+
int columns = indices.last().column() - indices.first().column() + 1;
75+
while (!indices.isEmpty()) {
76+
QByteArrayList lst;
77+
for (int i = 0; i < columns; ++i) {
78+
lst << indices.first().data(Qt::EditRole).toByteArray();
79+
indices.pop_front();
80+
}
81+
m_buffer.push_back(lst);
82+
}
83+
84+
return;
85+
}
86+
6087
QModelIndex first = indices.first();
6188
QString result;
6289
int currentRow = 0;
@@ -82,8 +109,6 @@ void ExtendedTableWidget::copy()
82109

83110
void ExtendedTableWidget::paste()
84111
{
85-
QString clipboard = qApp->clipboard()->text();
86-
87112
// Get list of selected items
88113
QItemSelectionModel* selection = selectionModel();
89114
QModelIndexList indices = selection->selectedIndexes();
@@ -92,6 +117,42 @@ void ExtendedTableWidget::paste()
92117
if(indices.isEmpty())
93118
return;
94119

120+
SqliteTableModel* m = qobject_cast<SqliteTableModel*>(model());
121+
122+
if (qApp->clipboard()->text().isEmpty() && !m_buffer.isEmpty()) {
123+
// If buffer contains something - use it instead of clipboard
124+
int rows = m_buffer.size();
125+
int columns = m_buffer.first().size();
126+
127+
int firstRow = indices.front().row();
128+
int firstColumn = indices.front().column();
129+
130+
int lastRow = qMin(firstRow + rows - 1, m->rowCount() - 1);
131+
int lastColumn = qMin(firstColumn + columns - 1, m->columnCount() - 1);
132+
133+
int row = firstRow;
134+
135+
foreach(const QByteArrayList& lst, m_buffer) {
136+
int column = firstColumn;
137+
foreach(const QByteArray& ba, lst) {
138+
m->setData(m->index(row, column), ba);
139+
140+
column++;
141+
if (column > lastColumn)
142+
break;
143+
}
144+
145+
row++;
146+
if (row > lastRow)
147+
break;
148+
}
149+
150+
return;
151+
}
152+
153+
154+
QString clipboard = qApp->clipboard()->text();
155+
95156
// Find out end of line character
96157
QString endOfLine;
97158
if(clipboard.endsWith('\n'))
@@ -155,7 +216,6 @@ void ExtendedTableWidget::paste()
155216
// Here we have positive answer even if cliboard is bigger than selection
156217

157218

158-
SqliteTableModel* m = qobject_cast<SqliteTableModel*>(model());
159219
// If last row and column are after table size clamp it
160220
int lastRow = qMin(firstRow + clipboardRows - 1, m->rowCount() - 1);
161221
int lastColumn = qMin(firstColumn + clipboardColumns - 1, m->columnCount() - 1);

src/ExtendedTableWidget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class ExtendedTableWidget : public QTableView
2727
void paste();
2828
int numVisibleRows();
2929

30+
QList<QByteArrayList> m_buffer;
31+
3032
private slots:
3133
void vscrollbarChanged(int value);
3234
void cellClicked(const QModelIndex& index);

0 commit comments

Comments
 (0)