Skip to content

Commit 053a11e

Browse files
committed
refactor: clean up MainWindow and ToolBar class formatting, improve code readability
1 parent 2548db7 commit 053a11e

File tree

5 files changed

+58
-86
lines changed

5 files changed

+58
-86
lines changed

parser/MainWindow.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#include "MainWindow.h"
22

33
MainWindow::MainWindow(QWidget *parent)
4-
: QMainWindow(parent)
5-
{
4+
: QMainWindow(parent) {
65
// set the window title
76
setWindowTitle("Tiny Parser");
87

@@ -18,18 +17,14 @@ MainWindow::MainWindow(QWidget *parent)
1817

1918
MainWindow::~MainWindow() {}
2019

21-
void MainWindow::initToolbar()
22-
{
20+
void MainWindow::initToolbar() {
2321
// Create a new toolba
2422
Tiny::Widgets::ToolBar *toolbar = new Tiny::Widgets::ToolBar(this);
2523
this->addToolBar(toolbar);
2624
}
2725

28-
void MainWindow::initStatusBar()
29-
{
26+
void MainWindow::initStatusBar() {
3027
// Create a new status bar
3128
QStatusBar *statusBar = new QStatusBar(this);
3229
this->setStatusBar(statusBar);
3330
}
34-
35-

parser/MainWindow.h

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
#ifndef MAINWINDOW_H
22
#define MAINWINDOW_H
33

4-
#include "ToolBar.h"
5-
6-
#include <QMainWindow>
4+
#include <QDebug>
75
#include <QGuiApplication>
8-
#include <QStyle>
6+
#include <QMainWindow>
97
#include <QScreen>
10-
#include <QDebug>
11-
#include <QToolBar>
128
#include <QStatusBar>
9+
#include <QStyle>
10+
#include <QToolBar>
1311

14-
class MainWindow : public QMainWindow
15-
{
12+
#include "ToolBar.h"
13+
14+
class MainWindow : public QMainWindow {
1615
Q_OBJECT
1716

18-
public:
17+
public:
1918
MainWindow(QWidget *parent = nullptr);
2019
~MainWindow();
2120

22-
23-
private:
21+
private:
2422
void initToolbar();
2523
void initStatusBar();
2624
};
27-
#endif // MAINWINDOW_H
25+
#endif // MAINWINDOW_H

parser/Widgets/include/ToolBar.h

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
#ifndef TOOLBAR_H
22
#define TOOLBAR_H
33

4-
#include <QToolBar>
54
#include <QAction>
6-
#include <QIcon>
75
#include <QDebug>
6+
#include <QIcon>
87
#include <QObject>
8+
#include <QToolBar>
99
#include <QVector>
10-
1110
#include <functional>
1211

1312
namespace Tiny::Widgets {
1413

15-
class ToolBar : public QToolBar
16-
{
14+
class ToolBar : public QToolBar {
1715
Q_OBJECT
18-
public:
16+
public:
1917
enum class ActionName {
2018
NewTextFile,
2119
NewTokensFile,
@@ -30,15 +28,14 @@ class ToolBar : public QToolBar
3028
About
3129
};
3230

33-
explicit ToolBar(QWidget *parent = nullptr);
31+
explicit ToolBar(QWidget* parent = nullptr);
3432
~ToolBar();
3533

36-
3734
QAction* getAction(const QString& name);
3835
void setActionEnabled(const QString& name, bool enabled);
3936
void setActionVisible(const QString& name, bool visible);
4037

41-
private:
38+
private:
4239
QVector<QAction*> actions;
4340

4441
void initFileActions();
@@ -58,14 +55,10 @@ class ToolBar : public QToolBar
5855
// lookup table for action enum to string
5956
static const QMap<ActionName, QString> actionNameToString;
6057

58+
public slots:
6159

60+
signals:
6261

63-
public slots:
64-
65-
signals:
66-
67-
68-
69-
}; // class ToolBar
70-
} // namespace Tiny::Widgets
71-
#endif // TOOLBAR_H
62+
}; // class ToolBar
63+
} // namespace Tiny::Widgets
64+
#endif // TOOLBAR_H

parser/Widgets/src/ToolBar.cpp

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
namespace Tiny::Widgets {
44

5-
ToolBar::ToolBar(QWidget *parent)
6-
{
5+
ToolBar::ToolBar(QWidget* parent) {
76
// toolbar configrations
87
setMovable(false);
98
setFloatable(false);
@@ -23,15 +22,13 @@ ToolBar::ToolBar(QWidget *parent)
2322
initHelpActions();
2423
}
2524

26-
ToolBar::~ToolBar()
27-
{
25+
ToolBar::~ToolBar() {
2826
foreach (auto action, this->actions) {
2927
delete action;
3028
}
3129
}
3230

33-
void ToolBar::initFileActions()
34-
{
31+
void ToolBar::initFileActions() {
3532
// new text file action
3633
Action* newFileTextAction = new Action();
3734
newFileTextAction->name = ActionName::NewTextFile;
@@ -83,8 +80,7 @@ void ToolBar::initFileActions()
8380
addToolbarAction(saveAsFileAction);
8481
}
8582

86-
void ToolBar::initViewActions()
87-
{
83+
void ToolBar::initViewActions() {
8884
// view text
8985
Action* viewTextAction = new Action();
9086
viewTextAction->name = ActionName::ViewTextFile;
@@ -126,13 +122,9 @@ void ToolBar::initViewActions()
126122
qDebug() << "View syntax tree";
127123
};
128124
addToolbarAction(viewSyntaxTreeAction, true, true);
129-
130-
131-
132125
}
133126

134-
void ToolBar::initHelpActions()
135-
{
127+
void ToolBar::initHelpActions() {
136128
// help action
137129
Action* helpAction = new Action();
138130
helpAction->name = ActionName::Help;
@@ -154,8 +146,7 @@ void ToolBar::initHelpActions()
154146
addToolbarAction(aboutAction);
155147
}
156148

157-
void ToolBar::addToolbarAction(Action* action, bool onOffAction, bool onByDefault)
158-
{
149+
void ToolBar::addToolbarAction(Action* action, bool onOffAction, bool onByDefault) {
159150
QString actionName = actionNameToString[action->name];
160151
// load icon, it has the same name as the action
161152
QIcon icon(QString(":/resources/%1.png").arg(actionName));
@@ -165,7 +156,7 @@ void ToolBar::addToolbarAction(Action* action, bool onOffAction, bool onByDefaul
165156
}
166157

167158
// create the action
168-
QAction *newAction = new QAction(icon, actionName, this);
159+
QAction* newAction = new QAction(icon, actionName, this);
169160
// set the shortcut if it is not empty
170161
if (!action->shortcut.isEmpty()) {
171162
newAction->setShortcut(action->shortcut);
@@ -198,32 +189,29 @@ void ToolBar::addToolbarAction(Action* action, bool onOffAction, bool onByDefaul
198189
this->actions.append(newAction);
199190
}
200191

201-
void ToolBar::initStyle()
202-
{
192+
void ToolBar::initStyle() {
203193
// set the style of the toolbar
204-
QString style = "QToolBar {"
205-
"background-color: #1e1e1e;"
206-
"border: 1px solid #c0c0c0;"
207-
"padding: 5px;"
208-
"}"
209-
"QToolBar::separator {"
210-
"width: 1px;"
211-
"height: 25px;"
212-
"background-color: #c0c0c0;"
213-
"margin: 0 5px;"
214-
"}"
215-
"QToolButton {"
216-
"color: #c0c0c0;"
217-
"}"
218-
"QToolButton:hover {"
219-
"background-color: #2e2e2e;"
220-
"}"
221-
"QToolButton:checked {"
222-
"background-color: #3e3e3e;"
223-
"}"
224-
;
225-
226-
194+
QString style =
195+
"QToolBar {"
196+
"background-color: #1e1e1e;"
197+
"border: 1px solid #c0c0c0;"
198+
"padding: 5px;"
199+
"}"
200+
"QToolBar::separator {"
201+
"width: 1px;"
202+
"height: 25px;"
203+
"background-color: #c0c0c0;"
204+
"margin: 0 5px;"
205+
"}"
206+
"QToolButton {"
207+
"color: #c0c0c0;"
208+
"}"
209+
"QToolButton:hover {"
210+
"background-color: #2e2e2e;"
211+
"}"
212+
"QToolButton:checked {"
213+
"background-color: #3e3e3e;"
214+
"}";
227215

228216
this->setStyleSheet(style);
229217
}
@@ -239,7 +227,6 @@ const QMap<ToolBar::ActionName, QString> ToolBar::actionNameToString = {
239227
{ToolBar::ActionName::ViewParseTree, "View Parse Tree"},
240228
{ToolBar::ActionName::ViewSyntaxTree, "View Syntax Tree"},
241229
{ToolBar::ActionName::Help, "Help"},
242-
{ToolBar::ActionName::About, "About"}
243-
};
230+
{ToolBar::ActionName::About, "About"}};
244231

245-
}
232+
} // namespace Tiny::Widgets

parser/main.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
#include "MainWindow.h"
2-
31
#include <QApplication>
42

5-
int main(int argc, char *argv[])
6-
{
3+
#include "MainWindow.h"
4+
5+
int main(int argc, char *argv[]) {
76
QApplication a(argc, argv);
87
MainWindow w;
98
w.show();

0 commit comments

Comments
 (0)