-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
142 lines (125 loc) · 5.27 KB
/
mainwindow.cpp
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
#include "mainwindow.h"
#include <QFile>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
databaseName = "EvoMemora.rox";
setupMenu();
restorePreviousSettings();
flashcardManager = new FlashcardManager(flashCardPath, databaseName);
flashCardViewWidget = new FlashCardViewWidget(flashcardManager);
addToolBar(flashCardViewWidget->getToolBar());
flashCardViewWidget->getToolBar()->hide();
flashCardEditWindow = new FlashCardEditWindow;
flashCardAddNewWindow = new FlashCardAddNewWindow(flashcardManager);
addToolBar(flashCardAddNewWindow->getToolBar());
flashCardAddNewWindow->getToolBar()->hide();
homeWidget = new HomeWidget;
stackedWidget = new QStackedWidget;
stackedWidget->addWidget(homeWidget);
stackedWidget->addWidget(flashCardViewWidget);
stackedWidget->addWidget(flashCardAddNewWindow);
stackedWidget->addWidget(flashCardEditWindow);
setCentralWidget(stackedWidget);
connect(homeWidget->reviewFlashcardsButton, &QPushButton::released, this, [&]() {
flashCardViewWidget->getToolBar()->show();
stackedWidget->setCurrentWidget(flashCardViewWidget);
});
connect(flashCardViewWidget, &FlashCardViewWidget::homeActionTriggered, this, [&](){
flashCardViewWidget->getToolBar()->hide();
stackedWidget->setCurrentWidget(homeWidget);
});
connect(homeWidget->addNewFlashcardsButton, &QPushButton::released, this, [&]() {
flashCardAddNewWindow->getToolBar()->show();
flashCardAddNewWindow->setRoot(flashCardPath);
stackedWidget->setCurrentWidget(flashCardAddNewWindow);
});
connect(flashCardAddNewWindow, &FlashCardAddNewWindow::homeActionTriggered, this, [&](){
flashCardAddNewWindow->getToolBar()->hide();
stackedWidget->setCurrentWidget(homeWidget);
});
connect(flashCardEditWindow, &FlashCardEditWindow::updateView,
flashCardViewWidget->getFlashCardView(), &FlashCardView::reloadFlashCard);
connect(flashCardViewWidget, &FlashCardViewWidget::editActionTriggered, this, [&](){
auto root = flashCardViewWidget->getFlashCardView()->getRoot();
auto flashcard = flashCardViewWidget->getFlashCardView()->getName();
QString path = QFileInfo(root, flashcard).absoluteFilePath();
flashCardViewWidget->getToolBar()->hide();
stackedWidget->setCurrentWidget(flashCardEditWindow);
flashCardEditWindow->loadFlashCard(flashCardViewWidget->getFlashCardView()->getFlashCard());
});
connect(flashCardEditWindow, &FlashCardEditWindow::updateView, this, [&](){
flashCardViewWidget->getToolBar()->show();
stackedWidget->setCurrentWidget(flashCardViewWidget);
});
connect(flashCardEditWindow, &FlashCardEditWindow::cancel, this, [&](){
flashCardViewWidget->getToolBar()->show();
stackedWidget->setCurrentWidget(flashCardViewWidget);
});
connect(homeWidget->syncFlashcardsButton, &QPushButton::released, this, [&]() {
this->setEnabled(false);
overlay = new BlurOverlay(homeWidget);
flashcardManager->sync();
flashCardViewWidget->loadOverDueFlashcards();
qDebug() << "after sync";
delete overlay;
this->setEnabled(true);
});
}
MainWindow::~MainWindow()
{
delete flashcardManager;
}
void MainWindow::setupMenu()
{
QMenuBar* menuBar = new QMenuBar(this);
setMenuBar(menuBar);
QMenu* fileMenu = menuBar->addMenu("File");
QAction* openAction = new QAction("Open FlashCards Folder", this);
QAction *exitAction = new QAction("Exit", this);
exitAction->setShortcut(Qt::CTRL | Qt::Key_Q);
connect(exitAction, &QAction::triggered, this, &MainWindow::close);
connect(openAction, &QAction::triggered, this, &MainWindow::openAction);
QAction* editFlashCardAction = new QAction("Edit Current FlashCard", this);
connect(editFlashCardAction, &QAction::triggered, this, [&]() {
flashCardEditWindow->show();
flashCardEditWindow->raise();
flashCardEditWindow->loadFlashCard(flashCardView->getFlashCard());
});
fileMenu->addAction(openAction);
fileMenu->addAction(exitAction);
}
void MainWindow::restorePreviousSettings()
{
QSettings settings("Roxane", databaseName);
const QByteArray previousSize =
settings.value("MainWindow/Size", QByteArray()).toByteArray();
if (!previousSize.isEmpty()) restoreGeometry(previousSize);
else resize(900,700);
flashCardPath = settings.value("flashCardPath").toString();
}
void MainWindow::closeEvent(QCloseEvent *event)
{
Q_UNUSED(event);
QSettings settings("Roxane", databaseName);
settings.setValue("MainWindow/Size", saveGeometry());
settings.setValue("flashCardPath", flashCardPath);
settings.setValue("FlashCardPath", flashCardPath);
flashcardManager->save();
}
void MainWindow::openAction()
{
QString folderPath = QFileDialog::getExistingDirectory(
nullptr,
"Select a folder",
flashCardPath,
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks
);
// Check if a folder was selected
if (folderPath.isEmpty()) return;
flashCardPath = folderPath;
emit flashCardPathUpdated();
flashcardManager->save();
delete flashcardManager;
flashcardManager = new FlashcardManager(flashCardPath, databaseName);
}