src/mainwindow.cpp

Thu, 03 Oct 2019 11:45:44 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 03 Oct 2019 11:45:44 +0300
changeset 5
593a658cba8e
parent 3
55a55a9ec2c2
child 6
73e448b2943d
permissions
-rw-r--r--

stuff

0
f9f4d4d6f162 initial commit
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 #include "mainwindow.h"
f9f4d4d6f162 initial commit
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 #include "ui_mainwindow.h"
1
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents: 0
diff changeset
3 #include "version.h"
5
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
4 #include <QLabel>
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
5 #include <QVBoxLayout>
0
f9f4d4d6f162 initial commit
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6
f9f4d4d6f162 initial commit
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 MainWindow::MainWindow(QWidget *parent) :
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents: 1
diff changeset
8 QMainWindow{parent},
5
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
9 ui{std::make_unique<Ui_MainWindow>()},
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
10 documents{this}
0
f9f4d4d6f162 initial commit
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 {
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents: 1
diff changeset
12 ui->setupUi(this);
5
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
13 connect(ui->actionNew, &QAction::triggered, this, &MainWindow::newModel);
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents: 1
diff changeset
14 connect(ui->actionQuit, &QAction::triggered, this, &QMainWindow::close);
5
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
15 updateTitle();
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
16 }
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
17
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
18 MainWindow::~MainWindow()
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
19 {
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
20 }
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
21
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
22 void MainWindow::newModel()
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
23 {
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
24 documents.newModel();
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
25 this->updateTabs();
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
26 }
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
27
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
28 #include <QFileDialog>
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
29 #include <QMessageBox>
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
30 void MainWindow::openModel()
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
31 {
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
32 const QString path = QFileDialog::getOpenFileName(
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
33 this, "Open model", "", "LDraw models (*.ldr *.dat)");
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
34 if (not path.isEmpty())
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
35 {
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
36 QFile file{path};
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
37 const bool open_result = file.open(QIODevice::ReadOnly);
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
38 if (open_result)
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
39 {
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
40
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
41 }
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
42 else
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
43 {
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
44 QMessageBox::critical(this, "Problem opening file",
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
45 QString{"Could not open %1: %2"}
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
46 .arg(path)
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
47 .arg(file.errorString()));
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
48 }
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
49 }
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
50 }
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
51
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
52 /**
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
53 * @brief Creates a new tab widget for the specified model.
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
54 * @param model Model to get a new tab widget for
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
55 * @return widget
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
56 */
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
57 QWidget* MainWindow::createWidgetForModel(Model* model)
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
58 {
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
59 Q_UNUSED(model);
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
60 QWidget* widget = new QWidget(ui->tabs);
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
61 QLabel* label = new QLabel("asdf", widget);
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
62 QVBoxLayout* layout = new QVBoxLayout;
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
63 layout->addWidget(label);
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
64 widget->setLayout(layout);
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
65 return widget;
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
66 }
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
67
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
68 /**
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
69 * @brief Gets a tab widget for the specified model. If it does not exist,
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
70 * it will be created.
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
71 * @param model Model to get a tab widget for
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
72 * @return widget
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
73 */
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
74 QWidget* MainWindow::getWidgetForModel(Model* model)
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
75 {
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
76 QWidget* widget = this->modelWidgets.value(model);
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
77 if (widget == nullptr)
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
78 {
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
79 QWidget* const new_widget = createWidgetForModel(model);
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
80 this->modelWidgets[model] = new_widget;
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
81 return new_widget;
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
82 }
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
83 else
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
84 {
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
85 return widget;
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
86 }
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
87 }
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
88
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
89 /**
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
90 * @brief Updates the tab widget
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
91 */
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
92 void MainWindow::updateTabs()
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
93 {
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
94
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
95 }
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
96
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
97 /**
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
98 * @brief Updates the title of the main window so to contain the app's name
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
99 * and version as well as the open document name.
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
100 */
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
101 void MainWindow::updateTitle()
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
102 {
1
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents: 0
diff changeset
103 QString title = ::appName;
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents: 0
diff changeset
104 title += " ";
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents: 0
diff changeset
105 title += fullVersionString();
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents: 0
diff changeset
106 setWindowTitle(title);
0
f9f4d4d6f162 initial commit
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
107 }

mercurial