|
1 | 1 | #include "largetextmodel.h" |
2 | 2 | #include <QDebug> |
3 | 3 |
|
4 | | -ValueEditor::LargeTextWrappingModel::LargeTextWrappingModel(const QString &text, uint chunkSize) |
5 | | - : m_chunkSize(chunkSize) |
6 | | -{ |
7 | | - setText(text); |
| 4 | +ValueEditor::LargeTextWrappingModel::LargeTextWrappingModel(const QString &text, |
| 5 | + uint chunkSize) |
| 6 | + : m_chunkSize(chunkSize) { |
| 7 | + setText(text); |
8 | 8 | } |
9 | 9 |
|
10 | | -ValueEditor::LargeTextWrappingModel::~LargeTextWrappingModel() |
11 | | -{ |
12 | | -} |
| 10 | +ValueEditor::LargeTextWrappingModel::~LargeTextWrappingModel() {} |
13 | 11 |
|
14 | | -QHash<int, QByteArray> ValueEditor::LargeTextWrappingModel::roleNames() const |
15 | | -{ |
16 | | - QHash<int, QByteArray> roles; |
17 | | - roles[Qt::UserRole + 1] = "value"; |
18 | | - return roles; |
| 12 | +QHash<int, QByteArray> ValueEditor::LargeTextWrappingModel::roleNames() const { |
| 13 | + QHash<int, QByteArray> roles; |
| 14 | + roles[Qt::UserRole + 1] = "value"; |
| 15 | + return roles; |
19 | 16 | } |
20 | 17 |
|
21 | | -int ValueEditor::LargeTextWrappingModel::rowCount(const QModelIndex &) const |
22 | | -{ |
23 | | - return m_textRows.size(); |
| 18 | +int ValueEditor::LargeTextWrappingModel::rowCount(const QModelIndex &) const { |
| 19 | + return m_text.size() / m_chunkSize + |
| 20 | + (m_text.size() % m_chunkSize == 0 ? 0 : 1); |
24 | 21 | } |
25 | 22 |
|
26 | | -QVariant ValueEditor::LargeTextWrappingModel::data(const QModelIndex &index, int role) const |
27 | | -{ |
28 | | - if (!isIndexValid(index)) |
29 | | - return QVariant(); |
| 23 | +QVariant ValueEditor::LargeTextWrappingModel::data(const QModelIndex &index, |
| 24 | + int role) const { |
| 25 | + if (!isIndexValid(index)) return QVariant(); |
30 | 26 |
|
31 | | - if (role == Qt::UserRole + 1) { |
32 | | - return m_textRows[index.row()]; |
33 | | - } |
| 27 | + if (role == Qt::UserRole + 1) { |
| 28 | + return m_text.mid(index.row() * m_chunkSize, m_chunkSize); |
| 29 | + } |
34 | 30 |
|
35 | | - return QVariant(); |
| 31 | + return QVariant(); |
36 | 32 | } |
37 | 33 |
|
38 | | -void ValueEditor::LargeTextWrappingModel::setText(const QString &text) |
39 | | -{ |
40 | | - m_textRows.reserve(text.size()/m_chunkSize); |
41 | | - |
42 | | - for (uint chunkIndex=0; chunkIndex < text.size()/m_chunkSize + 1; chunkIndex++) |
43 | | - { |
44 | | - m_textRows.append(text.mid(chunkIndex * m_chunkSize, m_chunkSize)); |
45 | | - } |
| 34 | +void ValueEditor::LargeTextWrappingModel::setText(const QString &text) { |
| 35 | + m_text = text; |
46 | 36 | } |
47 | 37 |
|
48 | | -void ValueEditor::LargeTextWrappingModel::cleanUp() |
49 | | -{ |
50 | | - emit beginRemoveRows(QModelIndex(), 0, rowCount() - 1); |
51 | | - m_textRows.clear(); |
52 | | - emit endRemoveRows(); |
| 38 | +void ValueEditor::LargeTextWrappingModel::cleanUp() { |
| 39 | + emit beginRemoveRows(QModelIndex(), 0, rowCount() - 1); |
| 40 | + m_text.clear(); |
| 41 | + emit endRemoveRows(); |
53 | 42 | } |
54 | 43 |
|
55 | | -QString ValueEditor::LargeTextWrappingModel::getText() |
56 | | -{ |
57 | | - QString result; |
58 | | - result.reserve(m_textRows.size() * m_chunkSize); |
59 | | - |
60 | | - for(auto textRow : m_textRows) |
61 | | - { |
62 | | - result.append(textRow); |
63 | | - } |
64 | | - |
65 | | - return result; |
66 | | -} |
| 44 | +QString ValueEditor::LargeTextWrappingModel::getText() { return m_text; } |
67 | 45 |
|
68 | | -void ValueEditor::LargeTextWrappingModel::setTextChunk(uint row, QString text) |
69 | | -{ |
70 | | - if (row < m_textRows.size()) { |
71 | | - m_textRows[row] = text; |
72 | | - emit dataChanged(createIndex(row, 0), createIndex(row, 0)); |
73 | | - } |
| 46 | +void ValueEditor::LargeTextWrappingModel::setTextChunk(uint row, QString text) { |
| 47 | + m_text.replace(row * m_chunkSize, m_chunkSize, text); |
74 | 48 | } |
75 | 49 |
|
76 | | -bool ValueEditor::LargeTextWrappingModel::isIndexValid(const QModelIndex &index) const |
77 | | -{ |
78 | | - return 0 <= index.row() && index.row() < rowCount(); |
| 50 | +bool ValueEditor::LargeTextWrappingModel::isIndexValid( |
| 51 | + const QModelIndex &index) const { |
| 52 | + return 0 <= index.row() && index.row() < rowCount(); |
79 | 53 | } |
0 commit comments