Skip to content

Commit f02a7ce

Browse files
committed
add tab widget
1 parent 8d12e86 commit f02a7ce

File tree

9 files changed

+307
-2
lines changed

9 files changed

+307
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.vscode/
2+
parser/CMakeLists.txt.user

parser_gui/MainWindow.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ MainWindow::MainWindow(QWidget *parent)
1313

1414
// initialize the status bar
1515
initStatusBar();
16+
17+
// initialize the tab widget
18+
initTabWidget();
1619
}
1720

1821
MainWindow::~MainWindow() {}
@@ -28,3 +31,16 @@ void MainWindow::initStatusBar() {
2831
QStatusBar *statusBar = new QStatusBar(this);
2932
this->setStatusBar(statusBar);
3033
}
34+
35+
void MainWindow::initTabWidget() {
36+
// Create a new tab widget
37+
tabWidget = new Tiny::Widgets::TabWidget(this);
38+
this->setCentralWidget(tabWidget);
39+
40+
// get the toolbar
41+
Tiny::Widgets::ToolBar *toolbar = this->findChild<Tiny::Widgets::ToolBar *>();
42+
43+
// connect tolbar buttons to tab widget slots
44+
connect(toolbar, &Tiny::Widgets::ToolBar::newTextTab, tabWidget, &Tiny::Widgets::TabWidget::newTextTab);
45+
connect(toolbar, &Tiny::Widgets::ToolBar::newTokensTab, tabWidget, &Tiny::Widgets::TabWidget::newTokensTab);
46+
}

parser_gui/MainWindow.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <QStyle>
1010
#include <QToolBar>
1111

12+
#include "TabWidget.h"
1213
#include "ToolBar.h"
1314

1415
class MainWindow : public QMainWindow {
@@ -19,7 +20,10 @@ class MainWindow : public QMainWindow {
1920
~MainWindow();
2021

2122
private:
23+
Tiny::Widgets::TabWidget *tabWidget;
24+
2225
void initToolbar();
2326
void initStatusBar();
27+
void initTabWidget();
2428
};
2529
#endif // MAINWINDOW_H
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#ifndef TABWIDGET_H
2+
#define TABWIDGET_H
3+
4+
#include <QDebug>
5+
#include <QHBoxLayout>
6+
#include <QLabel>
7+
#include <QPushButton>
8+
#include <QTabWidget>
9+
#include <QVBoxLayout>
10+
11+
namespace Tiny::Widgets {
12+
13+
class TabWidget : public QTabWidget {
14+
Q_OBJECT
15+
public:
16+
explicit TabWidget(QWidget* parent = nullptr);
17+
18+
signals:
19+
20+
public slots:
21+
void addTabRequest(bool tokenOnly = false);
22+
23+
void newTextTab();
24+
void newTokensTab();
25+
26+
void openTextTab();
27+
void openTokensTab();
28+
29+
private:
30+
QWidget* defaultTab;
31+
size_t counter = 0;
32+
33+
void initStyle();
34+
void initConfig();
35+
36+
void putDefaultTab();
37+
38+
void defaultTabLayout();
39+
void createDefaultTab();
40+
41+
private slots:
42+
void closeTab(int index);
43+
}; // class TabWidget
44+
45+
} // namespace Tiny::Widgets
46+
47+
#endif // TABWIDGET_H

parser_gui/Widgets/include/ToolBar.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ class ToolBar : public QToolBar {
5858
public slots:
5959

6060
signals:
61+
void newTextTab();
62+
void newTokensTab();
6163

6264
}; // class ToolBar
6365
} // namespace Tiny::Widgets
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef TREEVISUALISER_H
2+
#define TREEVISUALISER_H
3+
4+
#include <QWidget>
5+
6+
class TreeVisualiser : public QWidget {
7+
Q_OBJECT
8+
public:
9+
TreeVisualiser(QWidget *parent = nullptr);
10+
};
11+
12+
#endif // TREEVISUALISER_H

parser_gui/Widgets/src/TabWidget.cpp

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
#include "TabWidget.h"
2+
3+
namespace Tiny::Widgets {
4+
5+
TabWidget::TabWidget(QWidget* parent)
6+
: QTabWidget(parent), defaultTab(nullptr) {
7+
initStyle();
8+
initConfig();
9+
10+
putDefaultTab();
11+
12+
// add a tab and remove the default tab
13+
addTab(new QWidget(), "New Tab");
14+
removeTab(0);
15+
// set the tab tool tip
16+
setTabToolTip(0, "New Tab");
17+
// set the close and move actions
18+
setTabsClosable(true);
19+
setMovable(true);
20+
21+
// connect the tab close signal
22+
connect(this, &TabWidget::tabCloseRequested, this, &TabWidget::closeTab);
23+
}
24+
25+
void TabWidget::addTabRequest(bool tokenOnly) {
26+
// check that the tab is not the default tab
27+
// get the tab at the current index
28+
QWidget* currentTab = widget(0);
29+
if (currentTab == defaultTab) {
30+
removeTab(0);
31+
// set close and move actions
32+
setTabsClosable(true);
33+
setMovable(true);
34+
}
35+
36+
QWidget* newTab = new QWidget(this);
37+
QVBoxLayout* layout = new QVBoxLayout();
38+
newTab->setLayout(layout);
39+
40+
// add a label for the type
41+
QLabel* typeLabel = new QLabel(tokenOnly ? "Tokens" : "Text");
42+
43+
// set the label style
44+
typeLabel->setAlignment(Qt::AlignCenter);
45+
typeLabel->setStyleSheet("font-size: 20px; color: #c0c0c0;");
46+
47+
// add the label to the layout
48+
layout->addWidget(typeLabel);
49+
50+
// add the new tab
51+
addTab(newTab, (tokenOnly ? "Tokens" : "Text") + QString::number(counter));
52+
53+
// set the tab tool tip
54+
setTabToolTip(count() - 1, (tokenOnly ? "Tokens" : "Text") + QString::number(counter));
55+
56+
counter++;
57+
}
58+
59+
void TabWidget::newTextTab() {
60+
addTabRequest(false);
61+
}
62+
63+
void TabWidget::newTokensTab() {
64+
addTabRequest(true);
65+
}
66+
67+
void TabWidget::openTextTab() {
68+
addTabRequest(false);
69+
}
70+
71+
void TabWidget::openTokensTab() {
72+
addTabRequest(true);
73+
}
74+
75+
void TabWidget::initStyle() {
76+
// Set the tab widget style
77+
setStyleSheet(
78+
"QTabWidget::pane {"
79+
" border: 1px solid #C2C7CB;"
80+
" border-top-left-radius: 4px;"
81+
" border-top-right-radius: 4px;"
82+
" margin-top: -1px;"
83+
"}"
84+
"QTabWidget::tab-bar {"
85+
" alignment: left;"
86+
"}"
87+
"QTabBar::tab {"
88+
" background-color: #1e1e1e;"
89+
" border: 1px solid #C4C4C3;"
90+
" border-top-left-radius: 4px;"
91+
" border-top-right-radius: 4px;"
92+
" min-width: 8ex;"
93+
" padding: 2px;"
94+
"}"
95+
"QTabBar::tab:selected {"
96+
" background-color: #3e3e3e;"
97+
" border-color: #9B9B9B;"
98+
"}"
99+
"QTabBar::tab:!selected {"
100+
" background-color: #1e1e1e;"
101+
" border: 1px solid #C4C4C3;"
102+
" border-bottom-color: #C2C7CB;"
103+
"}"
104+
"QTabBar::tab:!selected:hover {"
105+
" background-color: #1e1e1e;"
106+
"}"
107+
"QTabBar::tab:selected:hover {"
108+
" background-color: #2e2e2e;"
109+
"}");
110+
}
111+
112+
void TabWidget::initConfig() {
113+
setTabShape(QTabWidget::Rounded);
114+
setMovable(true);
115+
setTabsClosable(true);
116+
setTabBarAutoHide(false);
117+
setIconSize(QSize(15, 15));
118+
}
119+
120+
void TabWidget::putDefaultTab() {
121+
if (count() == 0) {
122+
if (defaultTab == nullptr) {
123+
createDefaultTab();
124+
}
125+
126+
addTab(defaultTab, "Welcome");
127+
// remove the close button
128+
setTabsClosable(false);
129+
// set the tab tool tip
130+
setTabToolTip(0, "Welcome");
131+
setMovable(false);
132+
}
133+
}
134+
135+
void Tiny::Widgets::TabWidget::defaultTabLayout() {
136+
QVBoxLayout* layout = new QVBoxLayout();
137+
defaultTab->setLayout(layout);
138+
139+
layout->setAlignment(Qt::AlignTop);
140+
layout->setContentsMargins(15, 15, 15, 15);
141+
142+
// add a label for the welcome message
143+
QLabel* welcomeLabel = new QLabel("Welcome to Tiny Editor");
144+
welcomeLabel->setAlignment(Qt::AlignCenter);
145+
welcomeLabel->setStyleSheet("font-size: 30px; color: #c0c0c0;");
146+
layout->addWidget(welcomeLabel);
147+
148+
layout->addSpacing(20);
149+
150+
// add label for text options
151+
QLabel* textOptionsLabel = new QLabel("Text options");
152+
textOptionsLabel->setAlignment(Qt::AlignCenter);
153+
textOptionsLabel->setStyleSheet("font-size: 20px; color: #c0c0c0;");
154+
layout->addWidget(textOptionsLabel);
155+
156+
// add 2 buttons in a horizontal layout
157+
QHBoxLayout* buttonLayout = new QHBoxLayout();
158+
QPushButton* newTextButton = new QPushButton("New Text File");
159+
QPushButton* openTextButton = new QPushButton("Open Text File");
160+
161+
// set the button style
162+
newTextButton->setStyleSheet("background-color: #2e2e2e; color: #c0c0c0;");
163+
openTextButton->setStyleSheet("background-color: #2e2e2e; color: #c0c0c0;");
164+
165+
// buttons actions
166+
connect(newTextButton, &QPushButton::clicked, this, &TabWidget::newTextTab);
167+
connect(openTextButton, &QPushButton::clicked, this, &TabWidget::openTextTab);
168+
169+
// add the buttons to the layout
170+
buttonLayout->addWidget(newTextButton);
171+
buttonLayout->addWidget(openTextButton);
172+
173+
// add the layout to the main layout
174+
layout->addLayout(buttonLayout);
175+
layout->addSpacing(20);
176+
177+
// add label for tokens options
178+
QLabel* tokensOptionsLabel = new QLabel("Tokens options");
179+
tokensOptionsLabel->setAlignment(Qt::AlignCenter);
180+
tokensOptionsLabel->setStyleSheet("font-size: 20px; color: #c0c0c0;");
181+
layout->addWidget(tokensOptionsLabel);
182+
183+
// add 2 buttons in a horizontal layout
184+
QHBoxLayout* tokensButtonLayout = new QHBoxLayout();
185+
QPushButton* newTokensButton = new QPushButton("New Tokens File");
186+
QPushButton* openTokensButton = new QPushButton("Open Tokens File");
187+
188+
// set the button style
189+
newTokensButton->setStyleSheet("background-color: #2e2e2e; color: #c0c0c0;");
190+
openTokensButton->setStyleSheet("background-color: #2e2e2e; color: #c0c0c0;");
191+
192+
// buttons actions
193+
connect(newTokensButton, &QPushButton::clicked, this, &TabWidget::newTokensTab);
194+
connect(openTokensButton, &QPushButton::clicked, this, &TabWidget::openTokensTab);
195+
196+
// add the buttons to the layout
197+
tokensButtonLayout->addWidget(newTokensButton);
198+
tokensButtonLayout->addWidget(openTokensButton);
199+
200+
// add the layout to the main layout
201+
layout->addLayout(tokensButtonLayout);
202+
layout->addSpacing(20);
203+
}
204+
205+
void TabWidget::createDefaultTab() {
206+
defaultTab = new QWidget(this);
207+
defaultTabLayout();
208+
}
209+
210+
void TabWidget::closeTab(int index) {
211+
removeTab(index);
212+
if (count() == 0) {
213+
putDefaultTab();
214+
}
215+
}
216+
217+
} // namespace Tiny::Widgets

parser_gui/Widgets/src/ToolBar.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ void ToolBar::initFileActions() {
3434
newFileTextAction->name = ActionName::NewTextFile;
3535
newFileTextAction->statusTip = "Create a new text file";
3636
newFileTextAction->shortcut = QKeySequence::New;
37-
newFileTextAction->slot = []() {
37+
newFileTextAction->slot = [this]() {
3838
qDebug() << "New text file";
39+
emit newTextTab();
3940
};
4041
addToolbarAction(newFileTextAction);
4142

@@ -44,8 +45,9 @@ void ToolBar::initFileActions() {
4445
newTokensFileAction->name = ActionName::NewTokensFile;
4546
newTokensFileAction->statusTip = "Create a new tokens file";
4647
newTokensFileAction->shortcut = QKeySequence("Ctrl+Shift+N");
47-
newTokensFileAction->slot = []() {
48+
newTokensFileAction->slot = [this]() {
4849
qDebug() << "New tokens file";
50+
emit newTokensTab();
4951
};
5052
addToolbarAction(newTokensFileAction);
5153

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include "TreeVisualiser.h"
2+
3+
TreeVisualiser::TreeVisualiser(QWidget *parent) : QWidget(parent) {
4+
}

0 commit comments

Comments
 (0)