//----------------------------------------------------------------------------------------- GenericTextEditor::GenericTextEditor(QString documentIcon, QWidget *parent) : QMdiArea(parent) { mDocumentIcon = documentIcon; mAllowDoubleDisplay = false; setObjectName("GenericTextEditor"); setViewMode(QMdiArea::TabbedView); QTabBar* tabBar = findChildren<QTabBar*>().at(0); tabBar->setTabsClosable(true); connect(tabBar, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int))); connect(tabBar, SIGNAL(currentChanged(int)), this, SLOT(tabChanged(int))); connect(this, SIGNAL(currentChanged(int)), this, SLOT(tabChanged(int))); }
void tst_QTabBar::closeButton() { QTabBar tabbar; QCOMPARE(tabbar.tabsClosable(), false); tabbar.setTabsClosable(true); QCOMPARE(tabbar.tabsClosable(), true); tabbar.addTab("foo"); QTabBar::ButtonPosition closeSide = (QTabBar::ButtonPosition)tabbar.style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition, 0, &tabbar); QTabBar::ButtonPosition otherSide = (closeSide == QTabBar::LeftSide ? QTabBar::RightSide : QTabBar::LeftSide); QVERIFY(tabbar.tabButton(0, otherSide) == 0); QVERIFY(tabbar.tabButton(0, closeSide) != 0); QAbstractButton *button = static_cast<QAbstractButton*>(tabbar.tabButton(0, closeSide)); QVERIFY(button); QSignalSpy spy(&tabbar, SIGNAL(tabCloseRequested(int))); button->click(); QCOMPARE(tabbar.count(), 1); QCOMPARE(spy.count(), 1); }
JKGStudio::JKGStudio(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags) { ui.setupUi(this); QToolBar *bar = addToolBar("Standard"); bar->setFloatable(false); bar->setIconSize(QSize(16,16)); QToolButton *newBtn = new QToolButton(this); newBtn->setMenu(ui.menuNew); newBtn->setIcon(QIcon(":/icons/new.png")); newBtn->setPopupMode(QToolButton::InstantPopup); newBtn->setText("New"); ui.menuNew->menuAction()->setIcon(QIcon(":/icons/new.png")); bar->addWidget(newBtn); bar->addAction(ui.actionOpen); bar->addAction(ui.actionSave); bar->addSeparator(); bar->addAction(ui.actionCut); bar->addAction(ui.actionCopy); bar->addAction(ui.actionPaste); bar->addSeparator(); bar->addAction(ui.actionUndo); bar->addAction(ui.actionRedo); // Setup menu list setupMenuList(); setupExtCommands(); m_mdi = ui.mdiArea; // Create window menu QtWindowListMenu *windowList = new QtWindowListMenu(this); windowList->standardAction(QtWindowListMenu::CloseAction)->setIcon(QIcon(":/icons/close.png")); windowList->standardAction(QtWindowListMenu::CloseAllAction)->setIcon(QIcon(":/icons/closeall.png")); windowList->standardAction(QtWindowListMenu::TileAction)->setIcon(QIcon(":/icons/tile.png")); windowList->standardAction(QtWindowListMenu::CascadeAction)->setIcon(QIcon(":/icons/cascade.png")); windowList->standardAction(QtWindowListMenu::NextAction)->setIcon(QIcon(":/icons/next.png")); windowList->standardAction(QtWindowListMenu::PrevAction)->setIcon(QIcon(":/icons/prev.png")); windowList->attachToMdiArea(m_mdi); menuBar()->insertMenu(ui.menuAbout->menuAction(), windowList); this->setCentralWidget(m_mdi); QTabBar *tabbar = m_mdi->findChild<QTabBar *>(); if (tabbar) tabbar->setExpanding(false); tabbar->setTabsClosable(true); connect(tabbar, SIGNAL(tabCloseRequested(int)), this, SLOT(onClose(void))); tabbar->setMovable(true); delete ui.centralWidget; QDockWidget *propdock = new QDockWidget("Properties", this); m_propbrowser = new QtTreePropertyBrowser(this); m_propmanager = new PropertyManager(m_propbrowser, this); propdock->toggleViewAction()->setIcon(QIcon(":/icons/properties.png")); propdock->setWidget(m_propbrowser); addDockWidget(Qt::RightDockWidgetArea, propdock); QDockWidget *undodock = new QDockWidget("Undo History", this); m_undoview = new QUndoView(this); //propdock->toggleViewAction()->setIcon(QIcon(":/icons/properties.png")); undodock->setWidget(m_undoview); addDockWidget(Qt::RightDockWidgetArea, undodock); QAction *sep = new QAction(this); sep->setSeparator(true); windowList->insertAction(windowList->actions().at(0), sep); windowList->insertAction(windowList->actions().at(0), undodock->toggleViewAction()); windowList->insertAction(windowList->actions().at(0), propdock->toggleViewAction()); MdiInterface::initialize(m_mdi, m_menuList, m_propmanager, m_undoview); m_interface = MdiInterface::get(); onSubWindowActivated(); }
//----------------------------------------------------------------------------------------- GenericTextEditor::GenericTextEditor(QString editorName, QWidget *parent) : QMdiArea(parent) { mParentTabWidget = static_cast<QTabWidget*>(parent->parent()); setObjectName(editorName); setViewMode(QMdiArea::TabbedView); QTabBar* tabBar = findChildren<QTabBar*>().at(0); tabBar->setTabsClosable(true); connect(tabBar, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int))); connect(tabBar, SIGNAL(currentChanged(int)), this, SLOT(tabChanged(int))); connect(this, SIGNAL(currentChanged(int)), this, SLOT(tabChanged(int))); mActSave = new QAction(tr("Save"), this); mActSave->setStatusTip(tr("Save")); mActSave->setIcon( QIcon( ":/icons/filesave.svg" )); mActSave->setEnabled(false); mActEditCopy = new QAction(tr("Copy"), this); mActEditCopy->setStatusTip(tr("Copy Selected")); mActEditCopy->setIcon( QIcon( ":/icons/editcopy.svg")); mActEditCopy->setEnabled(false); mActEditCut = new QAction(tr("Cut"), this); mActEditCut->setStatusTip(tr("Cut Selected")); mActEditCut->setIcon( QIcon( ":/icons/editcut.svg")); mActEditCut->setEnabled(false); mActEditPaste = new QAction(tr("Paste"), this); mActEditPaste->setStatusTip(tr("Paste From Clipboard")); mActEditPaste->setIcon( QIcon( ":/icons/editpaste.svg")); mActEditPaste->setEnabled(false); mMainToolBar = new QToolBar(); mMainToolBar->setObjectName("renderwindowtoolbar"); mMainToolBar->setIconSize(QSize(20,20)); mMainToolBar->setToolButtonStyle(Qt::ToolButtonIconOnly); mMainToolBar->addAction(mActSave); mMainToolBar->addSeparator(); mMainToolBar->addAction(mActEditCut); mMainToolBar->addAction(mActEditCopy); mMainToolBar->addAction(mActEditPaste); QMainWindow *mw = static_cast<QMainWindow*>(this->parentWidget()); mw->addToolBar(Qt::TopToolBarArea, mMainToolBar); mLastDocument = 0; connect(mActEditCut, SIGNAL(triggered()), this, SLOT(pasteAvailable())); connect(mActEditCopy, SIGNAL(triggered()), this, SLOT(pasteAvailable())); connect(mActSave, SIGNAL(triggered()), this, SLOT(onSave())); // Register the standard generic text editor codec extensions GenericTextEditorCodecFactory* genCodecFactory = new GenericTextEditorCodecFactory(); GenericTextEditor::registerCodecFactory("txt", genCodecFactory); GenericTextEditor::registerCodecFactory("xml", genCodecFactory); GenericTextEditor::registerCodecFactory("ogscene", genCodecFactory); GenericTextEditor::registerCodecFactory("html", genCodecFactory); GenericTextEditor::registerCodecFactory("htm", genCodecFactory); GenericTextEditor::registerCodecFactory("scene", genCodecFactory); GenericTextEditor::registerCodecFactory("cfg", genCodecFactory); GenericTextEditor::registerCodecFactory("log", genCodecFactory); Ogitors::EventManager::getSingletonPtr()->connectEvent(Ogitors::EventManager::MODIFIED_STATE_CHANGE, this, true, 0, true, 0, EVENT_CALLBACK(GenericTextEditor, onModifiedStateChanged)); Ogitors::EventManager::getSingletonPtr()->connectEvent(Ogitors::EventManager::LOAD_STATE_CHANGE, this, true, 0, true, 0, EVENT_CALLBACK(GenericTextEditor, onLoadStateChanged)); }
int main(int argc, char *argv[]) { QApplication app(argc, argv); auto *proxyStyle = new TabBarProxyStyle; app.setStyle(proxyStyle); QWidget widget; QStackedWidget stackedWidget; QTabBar tabBar; tabBar.setDocumentMode(true); tabBar.setTabsClosable(true); tabBar.setMovable(true); tabBar.setExpanding(false); // top tabBar.setShape(QTabBar::RoundedNorth); // bottom // tabBar.setShape(QTabBar::RoundedSouth); // left // tabBar.setShape(QTabBar::RoundedWest); // right // tabBar.setShape(QTabBar::RoundedEast); const auto shortLabel = QStringLiteral("Tab %1"); const auto longLabel = QStringLiteral("An Extremely Long Tab Label %1"); QMap<int, QWidget*> tabs; for (int i = 0; i < TabCount; i++) { QString tabNumberString = QString::number(i); QLabel *label = new QLabel(QStringLiteral("Tab %1 content").arg(tabNumberString)); tabs[i] = label; label->setAlignment(Qt::AlignCenter); stackedWidget.addWidget(label); tabBar.addTab(shortLabel.arg(tabNumberString)); } QObject::connect(&tabBar, &QTabBar::tabMoved, [&tabs](int from, int to) { QWidget *thisWidget = tabs[from]; QWidget *thatWidget = tabs[to]; tabs[from] = thatWidget; tabs[to] = thisWidget; }); QObject::connect(&tabBar, &QTabBar::currentChanged, [&stackedWidget, &tabs](int index) { if (index >= 0) stackedWidget.setCurrentWidget(tabs[index]); }); QObject::connect(&tabBar, &QTabBar::tabCloseRequested, [&stackedWidget, &tabBar, &tabs](int index) { QWidget *widget = tabs[index]; tabBar.removeTab(index); for (int i = index + 1; i < TabCount; i++) tabs[i-1] = tabs[i]; int currentIndex = tabBar.currentIndex(); if (currentIndex >= 0) stackedWidget.setCurrentWidget(tabs[currentIndex]); delete widget; }); QLayout *layout; switch (tabBar.shape()) { case QTabBar::RoundedEast: case QTabBar::TriangularEast: tabBar.setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding); layout = new QHBoxLayout(&widget); layout->addWidget(&stackedWidget); layout->addWidget(&tabBar); break; case QTabBar::RoundedWest: case QTabBar::TriangularWest: tabBar.setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding); layout = new QHBoxLayout(&widget); layout->addWidget(&tabBar); layout->addWidget(&stackedWidget); break; case QTabBar::RoundedNorth: case QTabBar::TriangularNorth: tabBar.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum); layout = new QVBoxLayout(&widget); layout->addWidget(&tabBar); layout->addWidget(&stackedWidget); break; case QTabBar::RoundedSouth: case QTabBar::TriangularSouth: tabBar.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum); layout = new QVBoxLayout(&widget); layout->addWidget(&stackedWidget); layout->addWidget(&tabBar); break; } TabBarForm form; layout->addWidget(&form); layout->setAlignment(&form, Qt::AlignHCenter); form.ui->documentModeButton->setChecked(tabBar.documentMode()); QObject::connect(form.ui->documentModeButton, &QCheckBox::toggled, [&] { tabBar.setDocumentMode(form.ui->documentModeButton->isChecked()); // QMacStyle (and maybe other styles) requires a re-polish to get the right font QApplication::sendEvent(&tabBar, new QEvent(QEvent::ThemeChange)); }); form.ui->movableTabsButton->setChecked(tabBar.isMovable()); QObject::connect(form.ui->movableTabsButton, &QCheckBox::toggled, [&] { tabBar.setMovable(form.ui->movableTabsButton->isChecked()); tabBar.update(); }); form.ui->closableTabsButton->setChecked(tabBar.tabsClosable()); QObject::connect(form.ui->closableTabsButton, &QCheckBox::toggled, [&] { tabBar.setTabsClosable(form.ui->closableTabsButton->isChecked()); tabBar.update(); }); form.ui->expandingTabsButton->setChecked(tabBar.expanding()); QObject::connect(form.ui->expandingTabsButton, &QCheckBox::toggled, [&] { tabBar.setExpanding(form.ui->expandingTabsButton->isChecked()); tabBar.update(); }); form.ui->displayIconButton->setChecked(!tabBar.tabIcon(0).isNull()); QObject::connect(form.ui->displayIconButton, &QCheckBox::toggled, [&] { const auto icon = form.ui->displayIconButton->isChecked() ? tabBar.style()->standardIcon(QStyle::SP_ComputerIcon) : QIcon(); for (int i = 0; i < tabBar.count(); i++) tabBar.setTabIcon(i, icon); }); form.ui->longLabelButton->setChecked(false); QObject::connect(form.ui->longLabelButton, &QCheckBox::toggled, [&] { const auto &label = form.ui->longLabelButton->isChecked() ? longLabel : shortLabel; for (int i = 0; i < tabBar.count(); i++) tabBar.setTabText(i, label.arg(i)); }); QObject::connect(form.ui->shapeComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), [&](int index) { Q_UNUSED(index); // TODO }); if (proxyStyle->alignment == Qt::AlignLeft) form.ui->leftAlignedButton->setChecked(true); else if (proxyStyle->alignment == Qt::AlignRight) form.ui->rightAlignedButton->setChecked(true); else form.ui->centeredButton->setChecked(true); QObject::connect(form.ui->textAlignmentGroup, QOverload<QAbstractButton *>::of(&QButtonGroup::buttonClicked), [&](QAbstractButton *b) { proxyStyle->alignment = b == form.ui->leftAlignedButton ? Qt::AlignLeft : b == form.ui->rightAlignedButton ? Qt::AlignRight : Qt::AlignCenter; QApplication::sendEvent(&tabBar, new QEvent(QEvent::StyleChange)); }); layout->setMargin(12); widget.show(); return app.exec(); }