|
| 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 |
0 commit comments