mirror of
https://github.com/jorenchik/mdemory.git
synced 2026-03-22 00:26:21 +00:00
added qscintilla and edit button for mdems
This commit is contained in:
@@ -12,11 +12,15 @@ set(CMAKE_AUTORCC ON)
|
||||
set(CMAKE_AUTOUIC ON)
|
||||
|
||||
find_package(Qt5 COMPONENTS Widgets REQUIRED)
|
||||
|
||||
# find_package(QScintilla REQUIRED)
|
||||
include_directories(/usr/include/qt/Qsci)
|
||||
|
||||
add_executable(
|
||||
MdemoryApp
|
||||
main.cpp
|
||||
trainWindow.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(MdemoryApp Qt5::Widgets api)
|
||||
target_link_libraries(MdemoryApp Qt5::Widgets /usr/lib/libqscintilla2_qt5.so api)
|
||||
target_include_directories(MdemoryApp PRIVATE ${CMAKE_SOURCE_DIR}/include)
|
||||
|
||||
@@ -18,6 +18,9 @@
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include <Qsci/qsciscintilla.h>
|
||||
#include <Qsci/qscilexercpp.h>
|
||||
|
||||
#include <QApplication>
|
||||
#include <QMainWindow>
|
||||
#include <QVBoxLayout>
|
||||
@@ -61,9 +64,11 @@ struct Mdem {
|
||||
QHBoxLayout hFront;
|
||||
QWidget wBack;
|
||||
QVBoxLayout hBack;
|
||||
QVector<QLabel*> backLabels;
|
||||
QToolButton editButton;
|
||||
QToolButton showButton;
|
||||
int labelCount;
|
||||
QVector<QLabel*> backLabels;
|
||||
Question *question;
|
||||
};
|
||||
|
||||
struct ErrorView {
|
||||
@@ -110,6 +115,9 @@ QToolButton *save;
|
||||
QToolButton *load;
|
||||
QToolButton *practice;
|
||||
|
||||
QsciScintilla *editor;
|
||||
QMainWindow* editorWindow;
|
||||
|
||||
const std::regex lastPathElementExp = std::regex("(.+\\/)*(.+)");
|
||||
|
||||
void showBacklabels(Mdem *mdem) {
|
||||
@@ -126,6 +134,64 @@ void showBacklabels(Mdem *mdem) {
|
||||
}
|
||||
}
|
||||
|
||||
std::string outputMdem(std::vector<Question*> questions) {
|
||||
std::stringstream ss;
|
||||
for (auto question: questions) {
|
||||
std::string cooldownPart;
|
||||
if (question->Cooldown != 0) {
|
||||
cooldownPart = std::format("[{:.2f}]", question->Cooldown);
|
||||
}
|
||||
ss << wrapText(
|
||||
std::format("- {} {} >\n",
|
||||
cooldownPart,
|
||||
escapeText(question->QuestionText)),
|
||||
WRAP_WIDTH
|
||||
);
|
||||
if (MultiElementQuestion* mw = dynamic_cast<MultiElementQuestion*>(question)) {
|
||||
for (auto choice: mw->Choices) {
|
||||
char opener;
|
||||
if (choice.IsCorrect) {
|
||||
opener = '+';
|
||||
} else {
|
||||
opener = '-';
|
||||
}
|
||||
std::string orderModifier;
|
||||
if (mw->type == MultiElementType::Order) {
|
||||
orderModifier = "^";
|
||||
}
|
||||
ss <<
|
||||
wrapText(
|
||||
std::format(
|
||||
"\t{}{} {}\n",
|
||||
opener,
|
||||
orderModifier,
|
||||
escapeText(choice.Answer)
|
||||
)
|
||||
, WRAP_WIDTH);
|
||||
}
|
||||
} else if (GroupQuestion* gq = dynamic_cast<GroupQuestion*>(question)) {
|
||||
for (auto group: gq->Groups) {
|
||||
ss << wrapText(
|
||||
std::format(
|
||||
"\t- {}:\n",
|
||||
escapeText(group.name)
|
||||
)
|
||||
, WRAP_WIDTH);
|
||||
for (auto element: group.elements) {
|
||||
ss << wrapText(
|
||||
std::format(
|
||||
"\t\t- {}\n",
|
||||
escapeText(element)
|
||||
)
|
||||
, WRAP_WIDTH);
|
||||
}
|
||||
}
|
||||
}
|
||||
ss << std::endl;
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
Mdem* makeMdem() {
|
||||
auto mdem = new Mdem;
|
||||
mdem->wMdem.setLayout(&mdem->vMdem);
|
||||
@@ -152,6 +218,20 @@ Mdem* makeMdem() {
|
||||
// Add Front Content
|
||||
mdem->hFront.addWidget(&mdem->wFrontText);
|
||||
mdem->hFront.addStretch(1);
|
||||
|
||||
mdem->editButton.setText("Edit");
|
||||
mdem->hFront.addWidget(&mdem->editButton);
|
||||
|
||||
QObject::connect(&mdem->editButton, &QToolButton::clicked, [mdem]() {
|
||||
if (mdem->question) {
|
||||
editor->setText(
|
||||
QString::fromStdString(
|
||||
outputMdem(std::vector<Question*>{mdem->question})
|
||||
)
|
||||
);
|
||||
editorWindow->show();
|
||||
}
|
||||
});
|
||||
|
||||
mdem->showButton.setText("Show");
|
||||
mdem->hFront.addWidget(&mdem->showButton);
|
||||
@@ -211,6 +291,7 @@ void CreateMdems(std::vector<Question*>& questions) {
|
||||
|
||||
|
||||
for (size_t i = 0; i < questions.size(); ++i) {
|
||||
mdems[i]->question = questions[i];
|
||||
if (MultiElementQuestion* mw = dynamic_cast<MultiElementQuestion*>(questions[i])) {
|
||||
mdems[i]->wFrontText.setText(
|
||||
QString::fromStdString(mw->QuestionText)
|
||||
@@ -501,6 +582,7 @@ void pickDirectory(QString directory) {
|
||||
reloadMdem();
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
QApplication app(argc, argv);
|
||||
QMainWindow window;
|
||||
@@ -525,16 +607,43 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
window.setMenuBar(menuBar);
|
||||
|
||||
{ // Setup editor.
|
||||
editorWindow = new QMainWindow;
|
||||
|
||||
editorWindow->setWindowTitle("QScintilla Simple Editor");
|
||||
editorWindow->resize(800, 600);
|
||||
QWidget *wEditor = new QWidget;
|
||||
QVBoxLayout *vlEditor = new QVBoxLayout;
|
||||
wEditor->setLayout(vlEditor);
|
||||
|
||||
editor = new QsciScintilla;
|
||||
QsciLexerCPP *lexer = new QsciLexerCPP;
|
||||
editor->setLexer(lexer);
|
||||
editor->setUtf8(true);
|
||||
editor->setMarginWidth(0, 15);
|
||||
|
||||
QHBoxLayout *buttonLayout = new QHBoxLayout;
|
||||
QWidget *editorButtons = new QWidget;
|
||||
auto save = new QToolButton;
|
||||
editorButtons->setLayout(buttonLayout);
|
||||
save->setText(QString::fromStdString("Save"));
|
||||
buttonLayout->addWidget(save);
|
||||
|
||||
vlEditor->addWidget(editor);
|
||||
vlEditor->addWidget(editorButtons);
|
||||
editorWindow->setCentralWidget(wEditor);
|
||||
}
|
||||
|
||||
QSplitter *hSplitter = new QSplitter();
|
||||
|
||||
// LeftSide
|
||||
QWidget *leftWidget = new QWidget();
|
||||
QVBoxLayout *leftLayout = new QVBoxLayout();
|
||||
QWidget *leftTop = new QWidget();
|
||||
QVBoxLayout *vLeftTop = new QVBoxLayout();
|
||||
mdemList = new QTreeView();
|
||||
mdemLabel = new QLabel();
|
||||
model = new QFileSystemModel();
|
||||
QWidget *leftWidget = new QWidget();
|
||||
QVBoxLayout *leftLayout = new QVBoxLayout();
|
||||
QWidget *leftTop = new QWidget();
|
||||
QVBoxLayout *vLeftTop = new QVBoxLayout();
|
||||
mdemList = new QTreeView();
|
||||
mdemLabel = new QLabel();
|
||||
model = new QFileSystemModel();
|
||||
mdemLabel->setStyleSheet(
|
||||
"font-size: 17px;"
|
||||
"font-weight: 400;"
|
||||
@@ -599,63 +708,8 @@ int main(int argc, char *argv[]) {
|
||||
practice->setText("Practice");
|
||||
QObject::connect(save, &QToolButton::clicked, []() {
|
||||
auto filename = getFilename(currentMdem);
|
||||
std::stringstream ss;
|
||||
for (auto question: questions) {
|
||||
std::string cooldownPart;
|
||||
if (question->Cooldown != 0) {
|
||||
cooldownPart = std::format("[{:.2f}]", question->Cooldown);
|
||||
}
|
||||
ss << wrapText(
|
||||
std::format("- {} {} >\n",
|
||||
cooldownPart,
|
||||
escapeText(question->QuestionText)),
|
||||
WRAP_WIDTH
|
||||
);
|
||||
if (MultiElementQuestion* mw = dynamic_cast<MultiElementQuestion*>(question)) {
|
||||
for (auto choice: mw->Choices) {
|
||||
char opener;
|
||||
if (choice.IsCorrect) {
|
||||
opener = '+';
|
||||
} else {
|
||||
opener = '-';
|
||||
}
|
||||
std::string orderModifier;
|
||||
if (mw->type == MultiElementType::Order) {
|
||||
orderModifier = "^";
|
||||
}
|
||||
ss <<
|
||||
wrapText(
|
||||
std::format(
|
||||
"\t{}{} {}\n",
|
||||
opener,
|
||||
orderModifier,
|
||||
escapeText(choice.Answer)
|
||||
)
|
||||
, WRAP_WIDTH);
|
||||
}
|
||||
} else if (GroupQuestion* gq = dynamic_cast<GroupQuestion*>(question)) {
|
||||
for (auto group: gq->Groups) {
|
||||
ss << wrapText(
|
||||
std::format(
|
||||
"\t- {}:\n",
|
||||
escapeText(group.name)
|
||||
)
|
||||
, WRAP_WIDTH);
|
||||
for (auto element: group.elements) {
|
||||
ss << wrapText(
|
||||
std::format(
|
||||
"\t\t- {}\n",
|
||||
escapeText(element)
|
||||
)
|
||||
, WRAP_WIDTH);
|
||||
}
|
||||
}
|
||||
}
|
||||
ss << std::endl;
|
||||
|
||||
std::ofstream out("generated.mdem");
|
||||
out << ss.str();
|
||||
}
|
||||
std::ofstream out("generated.mdem");
|
||||
out << outputMdem(questions);;
|
||||
});
|
||||
QObject::connect(load, &QToolButton::clicked, &reloadMdem);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user