summaryrefslogtreecommitdiff
path: root/ui/typedialog.h
blob: 7388a83ce11cf4d3b9a6e85ff1fa7684f77826e3 (plain)
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
#pragma once

#include <QtWidgets/QDialog>
#include <QtWidgets/QLabel>
#include <QtCore/QStringListModel>
#include <QtWidgets/QComboBox>
#include <QtCore/QTimer>
#ifndef BINARYNINJAUI_BINDINGS
	#include <QtCore/QThread>
#endif
#include "binaryninjaapi.h"
#include "uitypes.h"


#ifdef BINARYNINJAUI_BINDINGS
// QThread has issues working in the bindings on some platforms
class GetTypesListThread;
class ParseTypeThread;
#else
class BINARYNINJAUIAPI GetTypesListThread : public QThread
{
	Q_OBJECT

	QStringList m_allTypes;
	std::function<void()> m_completeFunc;
	std::mutex m_mutex;
	bool m_done;
	BinaryViewRef m_view;

  protected:
	virtual void run() override;

  public:
	GetTypesListThread(BinaryViewRef view, const std::function<void()>& completeFunc);
	void cancel();

	const QStringList& getTypes() const { return m_allTypes; }
};

Q_DECLARE_METATYPE(BinaryNinja::QualifiedNameAndType);

//! QThread subclass for handling type string parsing to avoid UI interruptions.
class ParseTypeThread : public QThread
{
	Q_OBJECT

	BinaryViewRef m_view;
	std::string m_text;

	void run() override;

  Q_SIGNALS:
	void parsingComplete(bool valid, BinaryNinja::QualifiedNameAndType type, QString error);

  public:
	ParseTypeThread(BinaryViewRef view, QString text);
	void cancel();
};
#endif

class BINARYNINJAUIAPI TypeDialog : public QDialog
{
	Q_OBJECT

	QComboBox* m_combo;
	QStringListModel* m_model;
	QLabel* m_prompt;
	QString m_promptText;
	BinaryViewRef m_view;
	bool m_resultValid;
	QStringList m_historyEntries;
	int m_historySize;
	GetTypesListThread* m_updateThread;
	QFont m_defaultFont;
	bool m_initialTextSelection;
	BinaryNinja::QualifiedNameAndType m_type;
	QPushButton* m_acceptButton;
	QTimer* m_updateTimer;
	QTimer* m_parseTimer;
	bool m_isParsing;
	std::atomic_bool m_comboBoxTextChanged;
	QPalette m_defaultPalette;
	QString m_parseError;

	void commitHistory();
	void customEvent(QEvent* event);
	void saveLocation();
	void reject();
	void accept();

  private Q_SLOTS:
	void accepted();
	void checkParse();
	void typeParsed(bool valid, BinaryNinja::QualifiedNameAndType type, QString error);
	void updateTimerEvent();

  public:
	TypeDialog(QWidget* parent, BinaryViewRef view, const QString& title = "Specify Type",
	    const QString& prompt = "Enter Type Name", const QString& existing = "");
	~TypeDialog() { delete m_updateThread; }
	BinaryNinja::QualifiedNameAndType getType() const { return m_type; }
};