DynExp
Highly flexible laboratory automation for dynamically changing experiments.
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 "TextEditor.h"
6 
7 TextEditor::TextEditor(QWidget* parent, const std::filesystem::path& Filename)
8  : QWidget(parent, Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint),
9  Filename(Filename)
10 {
11  ui.setupUi(this);
12 
13  setWindowTitle(QString("Edit \"") + QString::fromStdString(Filename.string()) + "\"");
14 
15  if (this->Filename.extension().string() == ".py")
16  {
18  }
19 }
20 
21 void TextEditor::showEvent(QShowEvent* event)
22 {
23  // Not just retoring from minimization...
24  if (!event->spontaneous())
25  {
26  std::string Text;
27 
28  try
29  {
30  Text = Util::ReadFromFile(Filename.string());
31  }
32  catch (const Util::FileIOErrorException& e)
33  {
34  QMessageBox::warning(this, "DynExp - Error", e.what());
35 
36  QMetaObject::invokeMethod(this, "close", Qt::QueuedConnection);
37 
38  return;
39  }
40 
41  ui.TEText->setPlainText(QString::fromStdString(Text));
42  }
43 
44  event->accept();
45 }
46 
47 void TextEditor::closeEvent(QCloseEvent* event)
48 {
49  std::string Text;
50 
51  try
52  {
53  Text = Util::ReadFromFile(Filename.string());
54  }
55  catch ([[maybe_unused]] const Util::FileIOErrorException& e)
56  {
57  QWidget::closeEvent(event);
58  }
59 
60  if (ui.TEText->toPlainText().toStdString() != Text)
61  {
62  auto Reply = QMessageBox::question(this, "DynExp - Save changes?",
63  QString::fromStdString("The file \"" + Filename.string() + "\" has been edited. Save the changes?"),
64  QMessageBox::StandardButton::Yes | QMessageBox::StandardButton::No | QMessageBox::StandardButton::Abort,
65  QMessageBox::StandardButton::Abort);
66 
67  if (Reply == QMessageBox::StandardButton::Abort)
68  {
69  event->ignore();
70 
71  return;
72  }
73  else if (Reply == QMessageBox::StandardButton::Yes)
74  {
75  if (!DoSave())
76  {
77  event->ignore();
78 
79  return;
80  }
81  }
82  }
83 
84  QWidget::closeEvent(event);
85 }
86 
88 {
89  if (!Util::SaveToFile(QString::fromStdString(Filename.string()), ui.TEText->toPlainText().toStdString()))
90  {
91  QMessageBox::warning(this, "DynExp - Error", "Error writing data to file.");
92 
93  return false;
94  }
95 
96  return true;
97 }
98 
100 {
101  DoSave();
102 }
103 
105 {
106  close();
107 }
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:7
QSyntaxHighlighter * SyntaxHighlighter
Definition: TextEditor.h:43
virtual void showEvent(QShowEvent *event) override
Definition: TextEditor.cpp:21
Ui::TextEditor ui
Definition: TextEditor.h:39
const std::filesystem::path Filename
Definition: TextEditor.h:41
bool DoSave()
Saves the editor's content to the file Filename.
Definition: TextEditor.cpp:87
virtual void closeEvent(QCloseEvent *event) override
Definition: TextEditor.cpp:47
void OnClose()
Definition: TextEditor.cpp:104
void OnSave()
Definition: TextEditor.cpp:99
Thrown when reading from or writing to a file failed.
Definition: Exception.h:315
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.