DynExp
Highly flexible laboratory automation for dynamically changing experiments.
Loading...
Searching...
No Matches
TextEditor.cpp
Go to the documentation of this file.
1// This file is part of DynExp.
2
3#include "stdafx.h"
4#include "moc_TextEditor.cpp"
5#include "ui_TextEditor.h"
6#include "TextEditor.h"
7
8TextEditor::TextEditor(QWidget* parent, const std::filesystem::path& Filename)
9 : QWidget(parent, Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint),
10 ui(std::make_unique<Ui::TextEditor>()),
11 Filename(Filename)
12{
13 ui->setupUi(this);
14
15 setWindowTitle(QString("Edit \"") + QString::fromStdString(Filename.string()) + "\"");
16
17 if (this->Filename.extension().string() == ".py")
18 {
20 }
21}
22
23void TextEditor::showEvent(QShowEvent* event)
24{
25 // Not just retoring from minimization...
26 if (!event->spontaneous())
27 {
28 std::string Text;
29
30 try
31 {
32 Text = Util::ReadFromFile(Filename.string());
33 }
34 catch (const Util::FileIOErrorException& e)
35 {
36 QMessageBox::warning(this, "DynExp - Error", e.what());
37
38 QMetaObject::invokeMethod(this, "close", Qt::QueuedConnection);
39
40 return;
41 }
42
43 ui->TEText->setPlainText(QString::fromStdString(Text));
44 }
45
46 event->accept();
47}
48
49void TextEditor::closeEvent(QCloseEvent* event)
50{
51 std::string Text;
52
53 try
54 {
55 Text = Util::ReadFromFile(Filename.string());
56 }
57 catch ([[maybe_unused]] const Util::FileIOErrorException& e)
58 {
59 QWidget::closeEvent(event);
60 }
61
62 if (ui->TEText->toPlainText().toStdString() != Text)
63 {
64 auto Reply = QMessageBox::question(this, "DynExp - Save changes?",
65 QString::fromStdString("The file \"" + Filename.string() + "\" has been edited. Save the changes?"),
66 QMessageBox::StandardButton::Yes | QMessageBox::StandardButton::No | QMessageBox::StandardButton::Abort,
67 QMessageBox::StandardButton::Abort);
68
69 if (Reply == QMessageBox::StandardButton::Abort)
70 {
71 event->ignore();
72
73 return;
74 }
75 else if (Reply == QMessageBox::StandardButton::Yes)
76 {
77 if (!DoSave())
78 {
79 event->ignore();
80
81 return;
82 }
83 }
84 }
85
86 QWidget::closeEvent(event);
87}
88
90{
91 if (!Util::SaveToFile(QString::fromStdString(Filename.string()), ui->TEText->toPlainText().toStdString()))
92 {
93 QMessageBox::warning(this, "DynExp - Error", "Error writing data to file.");
94
95 return false;
96 }
97
98 return true;
99}
100
102{
103 DoSave();
104}
105
107{
108 close();
109}
Implements a dialog to edit and save small pieces of text or (Python) code.
Implementation of highlighting for Python code.
TextEditor(QWidget *parent, const std::filesystem::path &Filename)
Definition TextEditor.cpp:8
QSyntaxHighlighter * SyntaxHighlighter
Definition TextEditor.h:47
virtual void showEvent(QShowEvent *event) override
std::unique_ptr< Ui::TextEditor > ui
Definition TextEditor.h:43
const std::filesystem::path Filename
Definition TextEditor.h:45
bool DoSave()
Saves the editor's content to the file Filename.
virtual void closeEvent(QCloseEvent *event) override
void OnClose()
void OnSave()
Thrown when reading from or writing to a file failed.
Definition Exception.h:316
bool SaveToFile(const QString &Filename, std::string_view Text)
Saves a std::string_view to a file (using QFile). Creates a new file or truncates an existing file's ...
Definition QtUtil.cpp:236
std::string ReadFromFile(const QString &Filename)
Reads the entire content from a text file.
Definition QtUtil.cpp:250
Accumulates include statements to provide a precompiled header.