Fluent-Qt

Layout

DataGridToolBar

Search and combine filters, then show, pin or reorder DataGrid columns.

Example

DataGridToolBar: Filter and arrange projects

Usage

Filter and arrange projects

Search and combine filters. Use Columns to show, move or pin a column. Save and restore keeps this demo view in memory.

#include <FluentQt/Layout.h>
using namespace fluent::layout;

#ifndef GALLERY_DATAGRIDWORKSPACESAMPLE_H
#define GALLERY_DATAGRIDWORKSPACESAMPLE_H

#include <FluentQt/FluentQt.h>
#include <QHeaderView>
#include <QHBoxLayout>
#include <QSortFilterProxyModel>
#include <QStandardItemModel>
#include <QVBoxLayout>

namespace fluent::gallery {

// Filtering rules and saved views are application policy, outside DataGrid.
// zh_CN: 筛选规则与保存视图属于应用策略,不放入 DataGrid。
class ProjectFilterModel final : public QSortFilterProxyModel {
public:
    using QSortFilterProxyModel::QSortFilterProxyModel;
    void setCriteria(const QString& search, const QVariantMap& filters)
    {
        m_search = search.trimmed();
        m_filters = filters;
        invalidateFilter();
    }

protected:
    bool filterAcceptsRow(int row, const QModelIndex& parent) const override
    {
        bool matchesText = m_search.isEmpty();
        for (int c = 0; c < sourceModel()->columnCount(parent); ++c)
            matchesText |=
                sourceModel()->index(row, c, parent).data().toString().contains(m_search, Qt::CaseInsensitive);
        if (!matchesText)
            return false;
        for (const auto& pair : {qMakePair(QStringLiteral("owner"), 1), qMakePair(QStringLiteral("status"), 2)}) {
            const auto values = m_filters.value(pair.first).toStringList();
            if (!values.isEmpty() && !values.contains(sourceModel()->index(row, pair.second, parent).data().toString()))
                return false;
        }
        return true;
    }

private:
    QString m_search;
    QVariantMap m_filters;
};

class DataGridWorkspaceSample final : public layout::Card {
public:
    explicit DataGridWorkspaceSample(QWidget* parent = nullptr) : Card(parent)
    {
        setObjectName(QStringLiteral("dataGridWorkspace"));
        setBorderVisible(false);
        setMaximumWidth(864);
        setMinimumWidth(300);
        setMinimumHeight(480);
        auto* body = new QVBoxLayout(this);
        body->setContentsMargins(24, 24, 24, 24);
        body->setSpacing(12);
        auto* title = new textfields::Label(QStringLiteral("Projects"), this);
        title->setFluentTypography(Typography::FontRole::Subtitle);
        body->addWidget(title);
        auto* description = new textfields::Label(QStringLiteral("Search, combine filters, and arrange your columns."),
            this);
        description->setFluentTypography(Typography::FontRole::Caption);
        description->setTextColorRole(textfields::Label::TextColorRole::Secondary);
        description->setWordWrap(true);
        body->addWidget(description);

        m_toolbar = new layout::DataGridToolBar(this);
        m_toolbar->searchField()->setPlaceholderText(QStringLiteral("Search projects…"));
        m_toolbar->addFilter(QStringLiteral("status"), QStringLiteral("Status"),
            {QStringLiteral("Active"), QStringLiteral("Review")});
        m_toolbar->addFilter(QStringLiteral("owner"), QStringLiteral("Owner"),
            {QStringLiteral("Maya"), QStringLiteral("Noah"), QStringLiteral("Priya"), QStringLiteral("Riley")});
        body->addWidget(m_toolbar);

        m_model = new QStandardItemModel(this);
        m_model->setHorizontalHeaderLabels(
            {QStringLiteral("Project"), QStringLiteral("Owner"), QStringLiteral("Status"), QStringLiteral("Priority")});
        m_model->setHeaderData(3, Qt::Horizontal, int(Qt::AlignRight | Qt::AlignVCenter), Qt::TextAlignmentRole);
        const QStringList names{QStringLiteral("Aurora"), QStringLiteral("Beacon"), QStringLiteral("Canvas"),
            QStringLiteral("Delta"), QStringLiteral("Ember"), QStringLiteral("Fjord"), QStringLiteral("Grove"),
            QStringLiteral("Harbor")};
        const QStringList owners{QStringLiteral("Maya"), QStringLiteral("Noah"), QStringLiteral("Priya"),
            QStringLiteral("Riley")};
        for (int r = 0; r < names.size(); ++r) {
            auto* priority = new QStandardItem;
            priority->setData(r % 4 + 1, Qt::EditRole);
            priority->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
            m_model->appendRow({new QStandardItem(names.at(r)), new QStandardItem(owners.at(r % 4)),
                new QStandardItem(r % 3 == 0 ? QStringLiteral("Review") : QStringLiteral("Active")), priority});
        }
        m_proxy = new ProjectFilterModel(this);
        m_proxy->setSourceModel(m_model);
        m_grid = new collections::DataGrid(this);
        m_grid->setObjectName(QStringLiteral("workspaceDataGrid"));
        m_grid->setAccessibleName(QStringLiteral("Projects"));
        m_grid->setModel(m_proxy);
        m_grid->setBorderVisible(false);
        m_grid->setPlaceholderText(QStringLiteral("No projects match these filters."));
        m_grid->setSelectionBehavior(QAbstractItemView::SelectRows);
        m_grid->setSelectionMode(collections::SelectionMode::Extended);
        m_grid->setSortingEnabled(true);
        m_grid->sortByColumn(0, Qt::AscendingOrder);
        m_grid->setEditTriggers(QAbstractItemView::DoubleClicked | QAbstractItemView::EditKeyPressed);
        m_grid->setColumnWidth(0, 300);
        m_grid->setColumnWidth(1, 180);
        m_grid->setColumnWidth(2, 160);
        m_grid->setColumnWidth(3, 176);
        m_grid->setMinimumHeight(220);
        m_toolbar->setDataGrid(m_grid);
        body->addWidget(m_grid, 1);

        auto* footer = new QHBoxLayout;
        m_summary = new textfields::Label(this);
        m_summary->setObjectName(QStringLiteral("dataGridSummary"));
        m_summary->setFluentTypography(Typography::FontRole::Caption);
        m_summary->setTextColorRole(textfields::Label::TextColorRole::Secondary);
        m_summary->setWordWrap(true);
        footer->addWidget(m_summary, 1);
        auto* save = new basicinput::Button(QStringLiteral("Save view"), this);
        save->setObjectName(QStringLiteral("dataGridSaveView"));
        auto* restore = new basicinput::Button(QStringLiteral("Restore"), this);
        restore->setObjectName(QStringLiteral("dataGridRestoreView"));
        restore->setEnabled(false);
        footer->addWidget(save);
        footer->addWidget(restore);
        body->addLayout(footer);
        connect(save, &QAbstractButton::clicked, this, [this, restore] {
            m_savedColumns = m_grid->saveColumnState();
            m_savedSearch = m_toolbar->searchText();
            m_savedFilters = m_toolbar->filters();
            restore->setEnabled(true);
            status_info::Toast::showOrUpdateToast(this, QStringLiteral("gallery.action-feedback"),
                QStringLiteral("View saved for this demo"), status_info::Toast::Success, 2200, status_info::Toast::Top,
                QMargins(16, 50, 16, 16));
        });
        connect(restore, &QAbstractButton::clicked, this, [this] {
            m_grid->restoreColumnState(m_savedColumns);
            m_toolbar->clearFilters();
            m_toolbar->setSearchText(m_savedSearch);
            for (auto it = m_savedFilters.cbegin(); it != m_savedFilters.cend(); ++it)
                m_toolbar->setFilterValues(it.key(), it.value().toStringList());
            updateSummary();
            status_info::Toast::showOrUpdateToast(this, QStringLiteral("gallery.action-feedback"),
                QStringLiteral("View restored"), status_info::Toast::Success, 2200, status_info::Toast::Top,
                QMargins(16, 50, 16, 16));
        });
        const auto filter = [this] {
            m_proxy->setCriteria(m_toolbar->searchText(), m_toolbar->filters());
            updateSummary();
        };
        connect(m_toolbar, &layout::DataGridToolBar::searchTextChanged, this, filter);
        connect(m_toolbar, &layout::DataGridToolBar::filtersChanged, this, filter);
        connect(m_grid->selectionModel(), &QItemSelectionModel::selectionChanged, this, [this] { updateSummary(); });
        m_grid->selectRow(2);
        updateSummary();
    }

private:
    void updateSummary()
    {
        m_summary->setText(QStringLiteral("%1 of %2 projects · %3 selected")
                .arg(m_proxy->rowCount())
                .arg(m_model->rowCount())
                .arg(m_grid->selectionModel()->selectedRows().size()));
    }
    layout::DataGridToolBar* m_toolbar = nullptr;
    collections::DataGrid* m_grid = nullptr;
    QStandardItemModel* m_model = nullptr;
    ProjectFilterModel* m_proxy = nullptr;
    textfields::Label* m_summary = nullptr;
    QByteArray m_savedColumns;
    QString m_savedSearch;
    QVariantMap m_savedFilters;
};
} // namespace fluent::gallery
#endif

fluent::layout::DataGridToolBar

Inherits QWidget · FluentElement · QMLPlus

Compact search, facet and column controls for a borrowed DataGrid.

Constructors

Properties

fluent::collections::DataGrid* dataGrid
Read dataGrid()Write setDataGrid()Notify dataGridChanged()
QString searchText
Read searchText()Write setSearchText()Notify searchTextChanged()
QVariantMap filters
Read filters()Notify filtersChanged()

Methods

void setFilterValues(const QString& key, const QStringList& values)

Selects existing option values, ignoring unknown values and duplicates.

Signals

Destructors

Inherited from fluent::FluentElement

Methods

static void setTheme(Theme theme)

Updates the global theme and notifies active FluentElement instances.

static void setThemeDeferred(Theme theme)

Schedules a visible-first batched theme refresh without blocking the current interaction.

static Theme currentTheme()

Returns the currently active global theme.

static bool themeUsesDarkAppearance(Theme theme)

Returns whether a theme uses dark-backed chrome and material tokens.

static void refreshTheme()

Re-broadcasts the current theme to repaint after a design-token change.

static int themeGeneration()

Monotonic counter bumped on every theme change, for staleness checks.

Theme effectiveTheme() const

Resolves the theme used by this element, honoring an inherited QWidget override.

bool effectiveThemeUsesDarkAppearance() const

Returns whether this element's effective theme uses dark-backed chrome.

QJsonObject themeOverrides() const

Returns this element's authored overrides.

void clearThemeOverrides()

Restores all token fields to global theme defaults.

const Colors& themeColorsRef() const

Non-copying access to the active color set (for hot paint paths).

Material::AcrylicToken themeAcrylic() const

Material, elevation, and adaptive layout tokens.

QColor themeBackdrop(bool active) const

Window-chrome backdrop color for the given activation state.

virtual void onThemeUpdated()

Called after the global theme changes.

Enumerations

enum Theme

Global visual theme used when resolving design tokens.

  • Light = 0
  • Dark = 1
  • HighContrast = 2

Types and aliases

struct Colors

Design token snapshots returned to components.

Inherited from fluent::QMLPlus

Methods

AnchorLayout::Anchors* anchors()

Returns mutable anchors for the QWidget host.

void setState(const QString& name)

Applies a named state, or clears state when name is empty.

void addState(const QMLState& state)

Registers or replaces a named state definition.

void evaluateStates()

Reevaluates conditions; the first registered true condition wins.

QMLPlusRuntime* runtime()

Returns the lazy host-owned state observation object.

void setAnchors(const AnchorLayout::Anchors& anchors)

Replaces all host anchors and invalidates the parent layout.

void resetAnchors()

Clears all host anchors and invalidates the parent layout.

void addTransition(const StateTransition& transition)

Registers a state transition in precedence order.

void setBehavior(const char* propertyName, const PropertyAnimation& animation)

Sets the default animation for this host's managed property writes.

void clearBehavior(const char* propertyName)

Removes the default animation for future managed writes.

bool writeProperty(const char* propertyName, const QVariant& value)

Writes a host property through Behavior without removing its binding.

All inherited Qt members:QWidget