aboutsummaryrefslogtreecommitdiff
path: root/works/life/cpp-practicum/Record.cpp
blob: 4d7d4a9d89a5b776dd53fea2af5de4cb283318a5 (plain)
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
#include "Record.hpp"

void Record::WriteTo(QFile file) {
  file.open(QFile::ReadWrite | QFile::Text | QFile::Truncate);
  QTextStream stream(&file);
  stream.setCodec("UTF-8");

  stream << books_.size() << ' ' << vendors_.size() << '\n';
  for (const auto &book : books_) {
    stream << book << '\n';
  }
  for (const auto &vendor : vendors_) {
    stream << vendor << '\n';
  }
}

void Record::ReadFrom(QFile file) {
  file.open(QFile::ReadOnly | QFile::Text);
  QTextStream stream(&file);
  stream.setCodec("UTF-8");

  books_.clear();
  vendors_.clear();

  int book_count, vendor_count;
  stream >> book_count >> vendor_count;
  stream.skipWhiteSpace();
  for (int i = 0; i < book_count; i++) {
    Book book;
    stream >> book;
    books_.push_back(std::move(book));
  }
  for (int i = 0; i < vendor_count; i++) {
    Vendor vendor;
    stream >> vendor;
    vendors_.push_back(std::move(vendor));
  }
}

int BookModel::rowCount(const QModelIndex &parent) const {
  if (parent.isValid())
    return 0;
  return static_cast<int>(record_->GetBooks().size());
}

int BookModel::columnCount(const QModelIndex &parent) const {
  if (parent.isValid())
    return 0;
  return 6;
}

QVariant BookModel::headerData(int section, Qt::Orientation orientation,
                               int role) const {
  if (role != Qt::DisplayRole)
    return QVariant();
  if (orientation == Qt::Horizontal) {
    switch (section) {
    case 0:
      return QStringLiteral("ISBN");
    case 1:
      return QStringLiteral("标题");
    case 2:
      return QStringLiteral("类型");
    case 3:
      return QStringLiteral("作者");
    case 4:
      return QStringLiteral("出版社");
    case 5:
      return QStringLiteral("库存");
    default:
      return QVariant();
    }
  }
  return QVariant();
}

QVariant BookModel::data(const QModelIndex &index, int role) const {
  if (role != Qt::DisplayRole)
    return QVariant();

  if (!index.isValid())
    return QVariant();

  if (index.row() >= static_cast<int>(record_->GetBooks().size()) ||
      index.row() < 0)
    return QVariant();

  int row = index.row();
  const Book &book = record_->GetBooks()[row];

  int col = index.column();
  switch (col) {
  case 0:
    return QString::fromStdU16String(book.GetIsbn());
  case 1:
    return QString::fromStdU16String(book.GetTitle());
  case 2:
    return QString::fromStdU16String(book.GetType());
  case 3:
    return QString::fromStdU16String(book.GetAuthor());
  case 4:
    return QString::fromStdU16String(book.GetPress());
  case 5:
    return book.GetStockCount();
  default:
    return QVariant();
  }
}

bool BookModel::setData(const QModelIndex &index, const QVariant &value,
                        int role) {
  if (index.isValid() && role == Qt::EditRole) {
    int row = index.row();
    Book &book = record_->GetBooks()[row];

    int col = index.column();
    switch (col) {
    case 0:
      if (!value.canConvert<QString>())
        return false;
      book.SetIsbn(value.toString().toStdU16String());
      return true;
    case 1:
      if (!value.canConvert<QString>())
        return false;
      book.SetTitle(value.toString().toStdU16String());
      emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole});
      return true;
    case 2:
      if (!value.canConvert<QString>())
        return false;
      book.SetType(value.toString().toStdU16String());
      emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole});
      return true;
    case 3:
      if (!value.canConvert<QString>())
        return false;
      book.SetAuthor(value.toString().toStdU16String());
      emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole});
      return true;
    case 4:
      if (!value.canConvert<QString>())
        return false;
      book.SetPress(value.toString().toStdU16String());
      emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole});
      return true;
    case 5:
      if (!value.canConvert<int>())
        return false;
      book.SetStockCount(value.toInt());
      emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole});
      return true;
    default:
      return false;
    }
  }
  return false;
}

Qt::ItemFlags BookModel::flags(const QModelIndex &index) const {
  if (!index.isValid())
    return Qt::ItemIsEnabled;

  return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
}

bool BookModel::insertRows(int row, int count, const QModelIndex &parent) {
  beginInsertRows(parent, row, row + count - 1);
  for (int i = 0; i < count; i++) {
    record_->GetBooks().insert(record_->GetBooks().cbegin() + row, Book());
  }
  endInsertRows();
  return true;
}

bool BookModel::removeRows(int row, int count, const QModelIndex &parent) {
  beginRemoveRows(parent, row, row + count - 1);
  record_->GetBooks().erase(record_->GetBooks().cbegin() + row,
                            record_->GetBooks().cbegin() + row + count);
  endRemoveRows();
  return true;
}