HAL
python_editor.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved.
4 // Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved.
5 // Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved.
6 // Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved.
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 
26 #pragma once
27 
28 #include "gui/action/action.h"
33 
36 
37 #include <QEvent>
38 #include <QFileSystemWatcher>
39 #include <QMap>
40 #include <QMessageBox>
41 #include <QPushButton>
42 #include <QtCore/QFileInfo>
43 #include <filesystem>
44 
45 class QVBoxLayout;
46 class QTabWidget;
47 
48 namespace hal
49 {
50  class CodeEditor;
51  class Searchbar;
52  class Splitter;
53  class Toolbar;
54 
55  class PythonCodeEditor;
56 
57  class SettingsItemCheckbox;
58  class SettingsItemKeybind;
59  class SettingsItemSpinbox;
60 
62  {
63  struct PythonEditorControlEntry {
64  int tabInx;
65  bool active;
66  std::string restore;
67  std::string filename;
68  };
69 
70  QString mSaveDir;
71 
72  void restoreTabs(const std::filesystem::path& loaddir, const std::string& jsonfile);
73 
74  static bool write_control_file(const std::filesystem::path& savedir, const std::vector<PythonEditorControlEntry>& tabinfo);
75  public:
77 
78  std::string serialize(Netlist* netlist, const std::filesystem::path& savedir, bool isAutosave);
79 
80  void deserialize(Netlist* netlist, const std::filesystem::path& loaddir);
81 
83 
84  std::string serialize_control(const std::filesystem::path& savedir = std::filesystem::path(), bool isAutosave = false);
85 
87  static std::string sControlFileName;
88 
89  };
90 
99  {
100  Q_OBJECT
117 
118  public:
119  explicit PythonEditor(QWidget* parent = nullptr);
120  ~PythonEditor();
121 
126 
132  virtual void setupToolbar(Toolbar* Toolbar) override;
133 
139  virtual QList<QShortcut*> createShortcuts() override;
140 
146  virtual void handleStdout(const QString& output) override;
147 
153  virtual void handleError(const QString& output) override;
154 
158  virtual void clear() override;
159 
164  void handleActionOpenFile();
165 
170  void handleActionSaveFile();
171 
175  void handleActionSaveFileAs();
176 
180  void handleActionRun();
181 
185  void handleActionNewTab();
186 
191  void handleActionTabMenu();
192 
196  void handleActionCloseTab();
197 
203 
209 
215 
221 
225  void handleActionShowFile();
226 
233  void tabLoadFile(u32 index, QString fileName);
234 
235  //added so that the speciallogcontentmanager has access to all the code editors
242 
252  bool saveFile(bool isAutosave, QueryFilenamePolicy queryPolicy, int index = -1);
253 
258  void saveAllTabs(const QString& genericPath, bool isAutosave);
259 
263  void saveControl();
264 
270  QString unnamedFilename(int index) const;
271 
277  QString autosaveFilename(int index);
278 
284  void discardTab(int index);
285 
299  bool confirmDiscardForRange(int start, int end, int exclude = -1);
300 
306  void setToolbarButtonsEnabled(bool enable);
307 
308  // =====================================================================
309  // Q_PROPERTY functions
310  // =====================================================================
311 
315 
321  QString disabledIconStyle() const;
322 
328  QString openIconPath() const;
329 
335  QString openIconStyle() const;
336 
342  QString saveIconPath() const;
343 
349  QString saveIconStyle() const;
350 
356  QString saveAsIconPath() const;
357 
363  QString saveAsIconStyle() const;
364 
370  QString runIconPath() const;
371 
377  QString runIconStyle() const;
378 
384  QString newFileIconPath() const;
385 
391  QString newFileIconStyle() const;
392 
399 
406 
412  QString searchIconPath() const;
413 
419  QString searchIconStyle() const;
420 
427 
433  void setDisabledIconStyle(const QString& style);
434 
440  void setOpenIconPath(const QString& path);
441 
447  void setOpenIconStyle(const QString& style);
448 
454  void setSaveIconPath(const QString& path);
455 
461  void setSaveIconStyle(const QString& style);
462 
468  void setSaveAsIconPath(const QString& path);
469 
475  void setSaveAsIconStyle(const QString& style);
476 
482  void setRunIconPath(const QString& path);
483 
489  void setRunIconStyle(const QString& style);
490 
496  void setNewFileIconPath(const QString& path);
497 
503  void setNewFileIconStyle(const QString& style);
504 
510  void setToggleMinimapIconPath(const QString& path);
511 
518 
524  void setSearchIconPath(const QString& path);
525 
531  void setSearchIconStyle(const QString& style);
532 
539 
549  bool handleDeserializationFromHalFile(const std::filesystem::path& path, Netlist* netlist, rapidjson::Document& document);
550 
552 
553  Q_SIGNALS:
554  void forwardStdout(const QString& output);
555  void forwardError(const QString& output);
556 
557  public Q_SLOTS:
561  void toggleSearchbar();
562 
563  void handleThreadFinished();
564 
570  void handleTabCloseRequested(int index);
571 
576 
580  void handleKeyPressed();
581 
585  void handleTextChanged();
586 
590  void handleSearchbarTextEdited(const QString& text, SearchOptions opts);
591 
597  void handleCurrentTabChanged(int index);
598 
604  PythonCodeEditor* getPythonEditor(int tabIndex);
605 
614  void handleTabFileChanged(QString path);
615 
625 
631 
638  void handleFileOpened(QString fileName);
639 
645  void handleFileAboutToClose(const QString& fileName);
646 
650  void updateSearchIcon();
651 
652  protected:
661  bool eventFilter(QObject* obj, QEvent* event) override;
662 
663  private:
671  QMessageBox::StandardButton askSaveTab(const int tab_index) const;
672 
673  // ============ FUNCTIONS FOR SNAPSHOT HANDLING ============
674 
681  QPair<QString, QString> readSnapshotFile(QFileInfo snapshot_file_path) const;
682 
691  QPair<QMap<QString, QString>, QVector<QString>> loadAllSnapshots();
692 
701  bool writeSnapshotFile(QFileInfo snapshot_file_path, QString original_file_path, QString content) const;
702 
710  QString getSnapshotDirectory(const bool create_if_non_existent = true);
711 
717  void updateSnapshots();
718 
725  void clearAllSnapshots(bool remove_dir = false);
726 
735  bool decideLoadSnapshot(const QMap<QString, QString>& saved_snapshots, const QFileInfo original_path) const;
736 
743  void setSnapshotContent(const int idx, const QString snapshot_content);
744 
753  bool askLoadSnapshot(const QString original_path, const QString original_content, const QString snapshot_content) const;
754 
762  bool askDeleteSnapshots(const QPair<QMap<QString, QString>, QVector<QString>>& snapshots) const;
763 
769  void removeSnapshotFile(PythonCodeEditor* editor) const;
770 
778  QString getDefaultPath() const;
779 
780  QVBoxLayout* mLayout;
781  Toolbar* mToolbar;
782  Splitter* mSplitter;
783 
784  Searchbar* mSearchbar;
785 
786  Action* mActionOpenFile;
787  Action* mActionRun;
788  Action* mActionSave;
789  Action* mActionSaveAs;
790  Action* mActionToggleMinimap;
791  Action* mActionNewFile;
792 
793  QString mDisabledIconStyle;
794 
795  QString mOpenIconStyle;
796  QString mOpenIconPath;
797 
798  QString mSaveIconStyle;
799  QString mSaveIconPath;
800 
801  QString mSaveAsIconStyle;
802  QString mSaveAsIconPath;
803 
804  QString mRunIconStyle;
805  QString mRunIconPath;
806 
807  QString mNewFileIconStyle;
808  QString mNewFileIconPath;
809 
810  QString mToggleMinimapIconStyle;
811  QString mToggleMinimapIconPath;
812 
813  QString mSearchIconPath;
814  QString mSearchIconStyle;
815  QString mSearchActiveIconStyle;
816 
817  QTabWidget* mTabWidget;
818  int mTabRightclicked = -1;
819 
820  QFileSystemWatcher* mFileWatcher;
821  QMap<QString, PythonCodeEditor*> mPathEditorMap;
822 
823  FileModifiedBar* mFileModifiedBar;
824 
825  int mNewFileCounter;
826 
827  long mLastClickTime;
828 
829  PythonSerializer mSerializer;
830  QString mDefaultPath;
831  QString mGenericPath;
832 
837  QMap<PythonCodeEditor*, QString> mTabToSnapshotPath;
838 
839  SettingsItemSpinbox* mSettingFontSize;
840 
841  SettingsItemCheckbox* mSettingLineNumbers;
842  SettingsItemCheckbox* mSettingHighlight;
843  SettingsItemCheckbox* mSettingLineWrap;
844  SettingsItemCheckbox* mSettingMinimap;
845 
846  SettingsItemKeybind* mSettingOpenFile;
847  SettingsItemKeybind* mSettingSaveFile;
848  SettingsItemKeybind* mSettingSaveFileAs;
849  SettingsItemKeybind* mSettingRunFile;
850  SettingsItemKeybind* mSettingCreateFile;
851 
852  QList<u32> mBlockedContextIds;
853  };
854 } // namespace hal
Provides an interface for triggerable functionality that can be inserted into widgets and also connec...
Definition: action.h:42
Abstract class for Widgets within HAL's ContentArea.
A dialog in form of a bar to let the user decide how to handle file changes outside of HAL.
Code editor to write python code.
Interface for handling python outputs.
Main widget that combines all neccessary functionality to develop in python (for hal).
Definition: python_editor.h:99
virtual void clear() override
void handleBaseFileModifiedIgnore()
void setToggleMinimapIconStyle(const QString &style)
void handleCurrentTabChanged(int index)
void tabLoadFile(u32 index, QString fileName)
bool handleDeserializationFromHalFile(const std::filesystem::path &path, Netlist *netlist, rapidjson::Document &document)
void handleSearchbarTextEdited(const QString &text, SearchOptions opts)
QTextDocument::FindFlags getFindFlags()
void setSearchActiveIconStyle(const QString &style)
void handleActionCloseOtherTabs()
void handleTabCloseRequested(int index)
void handleTabFileChanged(QString path)
void setRunIconStyle(const QString &style)
void setRunIconPath(const QString &path)
bool confirmDiscardForRange(int start, int end, int exclude=-1)
void discardTab(int index)
virtual QList< QShortcut * > createShortcuts() override
void setSaveAsIconPath(const QString &path)
void forwardError(const QString &output)
void forwardStdout(const QString &output)
PythonCodeEditor * getPythonEditor(int tabIndex)
void setDisabledIconStyle(const QString &style)
QString searchActiveIconStyle
void setSaveIconStyle(const QString &style)
void setSaveAsIconStyle(const QString &style)
QTabWidget * getTabWidget()
QString newFileIconStyle
void handleFileOpened(QString fileName)
QString toggleMinimapIconPath
void handleActionCloseRightTabs()
PythonEditor(QWidget *parent=nullptr)
void handleBaseFileModifiedReload()
virtual void setupToolbar(Toolbar *Toolbar) override
void setSearchIconPath(const QString &path)
void handleFileAboutToClose(const QString &fileName)
void setSaveIconPath(const QString &path)
void setToggleMinimapIconPath(const QString &path)
virtual void handleError(const QString &output) override
bool eventFilter(QObject *obj, QEvent *event) override
void handleActionCloseAllTabs()
void setSearchIconStyle(const QString &style)
void setNewFileIconStyle(const QString &style)
virtual void handleStdout(const QString &output) override
bool saveFile(bool isAutosave, QueryFilenamePolicy queryPolicy, int index=-1)
void setOpenIconStyle(const QString &style)
void setToolbarButtonsEnabled(bool enable)
void saveAllTabs(const QString &genericPath, bool isAutosave)
QString toggleMinimapIconStyle
void handleActionCloseLeftTabs()
QString autosaveFilename(int index)
QString unnamedFilename(int index) const
void setOpenIconPath(const QString &path)
void handleActionToggleMinimap()
void setNewFileIconPath(const QString &path)
QString disabledIconStyle
std::string serialize(Netlist *netlist, const std::filesystem::path &savedir, bool isAutosave)
static std::string sControlFileName
Definition: python_editor.h:87
QString getDirectory() const
void deserialize(Netlist *netlist, const std::filesystem::path &loaddir)
std::string serialize_control(const std::filesystem::path &savedir=std::filesystem::path(), bool isAutosave=false)
static QString sPythonRelDir
Definition: python_editor.h:86
A QFrame with a QLineEdit that can be used to input a substring to search for.
Definition: searchbar.h:48
A SettingsItem representing a Checkbox.
A SettingsItem to modify keybinds.
A SettingsItem that represents a spinbox.
A QSplitter that can be checked if it is unused or not.
Definition: splitter.h:39
Toolbar for all ContentFrames and ContentWidgets.
Definition: toolbar.h:39
quint32 u32
Q_OBJECTQ_OBJECT
Q_PROPERTY(...)
Q_SIGNALSQ_SIGNALS
Q_SLOTSQ_SLOTS
QObject * parent() const const
typedef FindFlags
virtual bool event(QEvent *event) override
QStyle * style() const const