ItemLibraryWidget::ItemLibraryWidget(QWidget *parent) : QFrame(parent), m_itemIconSize(24, 24), m_resIconSize(24, 24), m_iconProvider(m_resIconSize), m_itemsView(new QQuickView()), m_resourcesView(new ItemLibraryTreeView(this)), m_filterFlag(QtBasic) { ItemLibraryModel::registerQmlTypes(); setWindowTitle(tr("Library", "Title of library view")); /* create Items view and its model */ m_itemsView->setResizeMode(QQuickView::SizeRootObjectToView); m_itemLibraryModel = new ItemLibraryModel(this); QQmlContext *rootContext = m_itemsView->rootContext(); rootContext->setContextProperty(QStringLiteral("itemLibraryModel"), m_itemLibraryModel.data()); rootContext->setContextProperty(QStringLiteral("itemLibraryIconWidth"), m_itemIconSize.width()); rootContext->setContextProperty(QStringLiteral("itemLibraryIconHeight"), m_itemIconSize.height()); rootContext->setContextProperty(QStringLiteral("rootView"), this); m_itemsView->rootContext()->setContextProperty(QStringLiteral("highlightColor"), Utils::StyleHelper::notTooBrightHighlightColor()); /* create Resources view and its model */ m_resourcesFileSystemModel = new QFileSystemModel(this); m_resourcesFileSystemModel->setIconProvider(&m_iconProvider); m_resourcesView->setModel(m_resourcesFileSystemModel.data()); m_resourcesView->setIconSize(m_resIconSize); /* create image provider for loading item icons */ m_itemsView->engine()->addImageProvider(QStringLiteral("qmldesigner_itemlibrary"), new Internal::ItemLibraryImageProvider); /* other widgets */ QTabBar *tabBar = new QTabBar(this); tabBar->addTab(tr("QML Types", "Title of library QML types view")); tabBar->addTab(tr("Resources", "Title of library resources view")); tabBar->addTab(tr("Imports", "Title of library imports view")); tabBar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); connect(tabBar, SIGNAL(currentChanged(int)), this, SLOT(setCurrentIndexOfStackedWidget(int))); connect(tabBar, SIGNAL(currentChanged(int)), this, SLOT(updateSearch())); m_filterLineEdit = new Utils::FancyLineEdit(this); m_filterLineEdit->setObjectName(QStringLiteral("itemLibrarySearchInput")); m_filterLineEdit->setPlaceholderText(tr("<Filter>", "Library search input hint text")); m_filterLineEdit->setDragEnabled(false); m_filterLineEdit->setMinimumWidth(75); m_filterLineEdit->setTextMargins(0, 0, 20, 0); m_filterLineEdit->setFiltering(true); QWidget *lineEditFrame = new QWidget(this); lineEditFrame->setObjectName(QStringLiteral("itemLibrarySearchInputFrame")); QGridLayout *lineEditLayout = new QGridLayout(lineEditFrame); lineEditLayout->setMargin(2); lineEditLayout->setSpacing(0); lineEditLayout->addItem(new QSpacerItem(5, 3, QSizePolicy::Fixed, QSizePolicy::Fixed), 0, 0, 1, 3); lineEditLayout->addItem(new QSpacerItem(5, 5, QSizePolicy::Fixed, QSizePolicy::Fixed), 1, 0); lineEditLayout->addWidget(m_filterLineEdit.data(), 1, 1, 1, 1); lineEditLayout->addItem(new QSpacerItem(5, 5, QSizePolicy::Fixed, QSizePolicy::Fixed), 1, 2); connect(m_filterLineEdit.data(), SIGNAL(filterChanged(QString)), this, SLOT(setSearchFilter(QString))); QWidget *container = createWindowContainer(m_itemsView.data()); m_stackedWidget = new QStackedWidget(this); m_stackedWidget->addWidget(container); m_stackedWidget->addWidget(m_resourcesView.data()); QWidget *spacer = new QWidget(this); spacer->setObjectName(QStringLiteral("itemLibrarySearchInputSpacer")); spacer->setFixedHeight(4); QGridLayout *layout = new QGridLayout(this); layout->setContentsMargins(0, 0, 0, 0); layout->setSpacing(0); layout->addWidget(tabBar, 0, 0, 1, 1); layout->addWidget(spacer, 1, 0); layout->addWidget(lineEditFrame, 2, 0, 1, 1); layout->addWidget(m_stackedWidget.data(), 3, 0, 1, 1); setResourcePath(QDir::currentPath()); setSearchFilter(QString()); /* style sheets */ setStyleSheet(QString::fromUtf8(Utils::FileReader::fetchQrc(":/qmldesigner/stylesheet.css"))); m_resourcesView->setStyleSheet(QString::fromUtf8(Utils::FileReader::fetchQrc(":/qmldesigner/scrollbar.css"))); m_qmlSourceUpdateShortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_F5), this); connect(m_qmlSourceUpdateShortcut, SIGNAL(activated()), this, SLOT(reloadQmlSource())); // init the first load of the QML UI elements reloadQmlSource(); }
ItemLibraryWidget::ItemLibraryWidget(QWidget *parent) : QFrame(parent), m_iconProvider(m_resIconSize), m_itemIconSize(24, 24), m_resIconSize(24, 24), m_itemsView(new QDeclarativeView(this)), m_resourcesView(new Internal::ItemLibraryTreeView(this)), m_filterFlag(QtBasic) { setWindowTitle(tr("Library", "Title of library view")); /* create Items view and its model */ m_itemsView->setAttribute(Qt::WA_OpaquePaintEvent); m_itemsView->setAttribute(Qt::WA_NoSystemBackground); m_itemsView->setAcceptDrops(false); m_itemsView->setFocusPolicy(Qt::ClickFocus); m_itemsView->setResizeMode(QDeclarativeView::SizeRootObjectToView); m_itemLibraryModel = new Internal::ItemLibraryModel(QDeclarativeEnginePrivate::getScriptEngine(m_itemsView->engine()), this); m_itemLibraryModel->setItemIconSize(m_itemIconSize); QDeclarativeContext *rootContext = m_itemsView->rootContext(); rootContext->setContextProperty(QLatin1String("itemLibraryModel"), m_itemLibraryModel.data()); rootContext->setContextProperty(QLatin1String("itemLibraryIconWidth"), m_itemIconSize.width()); rootContext->setContextProperty(QLatin1String("itemLibraryIconHeight"), m_itemIconSize.height()); QColor highlightColor = palette().highlight().color(); if (0.5*highlightColor.saturationF()+0.75-highlightColor.valueF() < 0) highlightColor.setHsvF(highlightColor.hsvHueF(),0.1 + highlightColor.saturationF()*2.0, highlightColor.valueF()); m_itemsView->rootContext()->setContextProperty(QLatin1String("highlightColor"), highlightColor); // loading the qml has to come after all needed context properties are set m_itemsView->setSource(QUrl("qrc:/ItemLibrary/qml/ItemsView.qml")); QDeclarativeItem *rootItem = qobject_cast<QDeclarativeItem*>(m_itemsView->rootObject()); connect(rootItem, SIGNAL(itemSelected(int)), this, SLOT(showItemInfo(int))); connect(rootItem, SIGNAL(itemDragged(int)), this, SLOT(startDragAndDrop(int))); connect(this, SIGNAL(scrollItemsView(QVariant)), rootItem, SLOT(scrollView(QVariant))); connect(this, SIGNAL(resetItemsView()), rootItem, SLOT(resetView())); /* create Resources view and its model */ m_resourcesFileSystemModel = new QFileSystemModel(this); m_resourcesFileSystemModel->setIconProvider(&m_iconProvider); m_resourcesView->setModel(m_resourcesFileSystemModel.data()); m_resourcesView->setIconSize(m_resIconSize); /* create image provider for loading item icons */ m_itemsView->engine()->addImageProvider(QLatin1String("qmldesigner_itemlibrary"), new Internal::ItemLibraryImageProvider); /* other widgets */ QTabBar *tabBar = new QTabBar(this); tabBar->addTab(tr("Items", "Title of library items view")); tabBar->addTab(tr("Resources", "Title of library resources view")); tabBar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); m_lineEdit = new Utils::FilterLineEdit(this); m_lineEdit->setObjectName(QLatin1String("itemLibrarySearchInput")); m_lineEdit->setPlaceholderText(tr("<Filter>", "Library search input hint text")); m_lineEdit->setDragEnabled(false); m_lineEdit->setMinimumWidth(75); m_lineEdit->setTextMargins(0, 0, 20, 0); QWidget *lineEditFrame = new QWidget(this); lineEditFrame->setObjectName(QLatin1String("itemLibrarySearchInputFrame")); QGridLayout *lineEditLayout = new QGridLayout(lineEditFrame); lineEditLayout->setMargin(2); lineEditLayout->setSpacing(0); lineEditLayout->addItem(new QSpacerItem(5, 3, QSizePolicy::Fixed, QSizePolicy::Fixed), 0, 0, 1, 3); lineEditLayout->addItem(new QSpacerItem(5, 5, QSizePolicy::Fixed, QSizePolicy::Fixed), 1, 0); lineEditLayout->addWidget(m_lineEdit.data(), 1, 1, 1, 1); lineEditLayout->addItem(new QSpacerItem(5, 5, QSizePolicy::Fixed, QSizePolicy::Fixed), 1, 2); connect(m_lineEdit.data(), SIGNAL(filterChanged(QString)), this, SLOT(setSearchFilter(QString))); m_stackedWidget = new QStackedWidget(this); m_stackedWidget->addWidget(m_itemsView.data()); m_stackedWidget->addWidget(m_resourcesView.data()); connect(tabBar, SIGNAL(currentChanged(int)), m_stackedWidget.data(), SLOT(setCurrentIndex(int))); connect(tabBar, SIGNAL(currentChanged(int)), this, SLOT(updateSearch())); QWidget *spacer = new QWidget(this); spacer->setObjectName(QLatin1String("itemLibrarySearchInputSpacer")); spacer->setFixedHeight(4); QGridLayout *layout = new QGridLayout(this); layout->setContentsMargins(0, 0, 0, 0); layout->setSpacing(0); layout->addWidget(tabBar, 0, 0, 1, 1); layout->addWidget(spacer, 1, 0); layout->addWidget(lineEditFrame, 2, 0, 1, 1); layout->addWidget(m_stackedWidget.data(), 3, 0, 1, 1); setResourcePath(QDir::currentPath()); setSearchFilter(QString()); /* style sheets */ setStyleSheet(QLatin1String(Utils::FileReader::fetchQrc(":/qmldesigner/stylesheet.css"))); m_resourcesView->setStyleSheet( QLatin1String(Utils::FileReader::fetchQrc(":/qmldesigner/scrollbar.css"))); }
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(); }