aboutsummaryrefslogtreecommitdiff
path: root/works/life/cpp-practicum/main.cpp
blob: 399c82c54c2083ba425aedf9614d9ddaa727398a (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
#include "Record.hpp"

#include <QApplication>
#include <QDir>
#include <QFile>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QMainWindow>
#include <QPushButton>
#include <QTableView>
#include <QVBoxLayout>
#include <QWidget>
#include <memory>

int main(int argc, char *argv[]) {
  QApplication application(argc, argv);

  Record record;

  QDir app_dir(application.applicationDirPath());
  QFile data_file = app_dir.filePath("data.txt");
  std::unique_ptr<QTextStream> stream;
  if (data_file.exists()) {
    data_file.open(QFile::ReadWrite);
    stream.reset(new QTextStream(&data_file));
    stream->setCodec("UTF-8");
    try {
      record.ReadFrom(*stream);
    } catch (...) {
    }
  } else {
    data_file.open(QFile::ReadWrite);
    stream.reset(new QTextStream(&data_file));
    stream->setCodec("UTF-8");
  }

  QWidget window;
  QVBoxLayout window_layout;
  window.setLayout(&window_layout);

  QHBoxLayout center_area;
  window_layout.addLayout(&center_area);

  QVBoxLayout book_area;
  QVBoxLayout vendor_area;
  center_area.addLayout(&book_area);
  center_area.addLayout(&vendor_area);

  QHBoxLayout book_top_area;
  QHBoxLayout vendor_top_area;
  book_area.addLayout(&book_top_area);
  vendor_area.addLayout(&vendor_top_area);

  QPushButton book_add_button(QStringLiteral("添加"));
  QPushButton book_remove_button(QStringLiteral("删除"));

  book_top_area.addStretch(1);
  book_top_area.addWidget(&book_add_button);
  book_top_area.addWidget(&book_remove_button);

  QPushButton vendor_add_button(QStringLiteral("添加"));
  QPushButton vendor_remove_button(QStringLiteral("删除"));

  vendor_top_area.addStretch(1);
  vendor_top_area.addWidget(&vendor_add_button);
  vendor_top_area.addWidget(&vendor_remove_button);

  QTableView book_view;
  book_view.verticalHeader()->hide();
  book_view.setSelectionBehavior(QAbstractItemView::SelectRows);
  book_view.setEditTriggers(QAbstractItemView::DoubleClicked);
  book_view.setSelectionMode(QAbstractItemView::SingleSelection);
  book_view.setSortingEnabled(true);

  QTableView vendor_view;
  vendor_view.verticalHeader()->hide();
  vendor_view.setSelectionBehavior(QAbstractItemView::SelectRows);
  vendor_view.setEditTriggers(QAbstractItemView::DoubleClicked);
  vendor_view.setSelectionMode(QAbstractItemView::SingleSelection);

  book_area.addWidget(&book_view, 1);
  vendor_area.addWidget(&vendor_view, 1);

  BookModel book_model(&record);
  VendorModel vendor_model(&record);

  book_view.setModel(&book_model);
  vendor_view.setModel(&vendor_model);

  QObject::connect(&book_add_button, &QPushButton::clicked, [&book_model]() {
    book_model.insertRow(book_model.rowCount());
  });

  QObject::connect(
      &book_remove_button, &QPushButton::clicked, [&book_view, &book_model]() {
        auto selected_rows = book_view.selectionModel()->selectedRows();
        for (const auto &row : selected_rows) {
          book_model.removeRow(row.row());
        }
      });

  QObject::connect(
      &vendor_add_button, &QPushButton::clicked,
      [&vendor_model]() { vendor_model.insertRow(vendor_model.rowCount()); });

  QObject::connect(&vendor_remove_button, &QPushButton::clicked,
                   [&vendor_view, &vendor_model]() {
                     auto selected_rows =
                         vendor_view.selectionModel()->selectedRows();
                     for (const auto &row : selected_rows) {
                       vendor_model.removeRow(row.row());
                     }
                   });

  window.show();

  int result = application.exec();

  record.WriteTo(*stream);

  return result;
}