NavigationView Example

#include <QtGui>
#include <QMessageBox>
#include <QLabel>
#include <QDesktopServices>
#include <QVBoxLayout>
#include <QComboBox>
#include <QColorDialog>
#include <QCalendarWidget>
#include <QTableView>
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
#include <QTextCodec>
#endif
#include <QApplication>
#include "window.h"

class NavigationPage : public QLabel
{
public:
    NavigationPage() : QLabel() {}
    virtual ~NavigationPage() { }
};

static QString load_html_file(const QString& fileName)
{
    QFile file(fileName);
    if (!file.exists() || !file.open(QFile::ReadOnly))
        return QString();
    QByteArray data = file.readAll();
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
    QString ret = QString::fromLocal8Bit(data);
#else
    QTextCodec* codec = Qt::codecForHtml(data);
    QString ret = codec->toUnicode(data);
#endif
    return ret;
}

static void createPageItem(NavigationView* view, const QString& str, const QString& resource, SegoeMDL2Font::IconSymbol symbol)
{
    NavigationPage* page = new NavigationPage();
    page->setMargin(0);
    page->setScaledContents(true);
    //page->setPixmap(QPixmap(resource));
    page->setText(load_html_file(resource));
    view->addPageItem(SegoeMDL2Font::code(symbol), str, page);
}

Window::Window()
    : NavigationMainWindow()
{
    setWindowTitle(tr("NavigationView Application"));

    m_view = new NavigationView(this);
    createPageItem(m_view, tr("News"), QStringLiteral(":/res/page1.html"), SegoeMDL2Font::World);
    createPageItem(m_view, tr("Fantasy Surfer"), QStringLiteral(":/res/page2.html"), SegoeMDL2Font::Walk);

    QCalendarWidget* calendarWidget = new QCalendarWidget();
    QFont f = calendarWidget->font();
    f.setPointSize(8);
    calendarWidget->setFont(f);
    QPalette p = calendarWidget->palette();
    p.setColor(QPalette::Base, Qt::transparent);
    calendarWidget->setPalette(p);
    QTableView* caledarView = calendarWidget->findChild<QTableView *>(QString(), Qt::FindChildrenRecursively);
    calendarWidget->setAutoFillBackground(false);
    calendarWidget->setAttribute(Qt::WA_NoSystemBackground, true);
    caledarView->setAutoFillBackground(false);
    caledarView->setAttribute(Qt::WA_NoSystemBackground, true);
    m_view->addWidgetItem(SegoeMDL2Font::code(SegoeMDL2Font::Calendar), calendarWidget);

    createPageItem(m_view, tr("Surface Water"), QStringLiteral(":/res/page3.html"), SegoeMDL2Font::Color);
    createPageItem(m_view, tr("Best Surfing 2021"), QStringLiteral(":/res/page4.html"), SegoeMDL2Font::FavoriteStar);
    createPageItem(m_view, tr("Video and Forecasting"), QStringLiteral(":/res/page5.html"), SegoeMDL2Font::Video);

    m_view->settingsItem()->setPage(new SettingsWidget(this));
    m_view->settingsItem()->setTransition(Qtitan::Entrance);

    //setContentsMargins(3, 3, 3, 3);
    setCentralWidget(m_view);

    titleBar()->setSysButtonKind(WindowTitleBar::SysButtonHidden);
    titleBar()->setBlurBehindWindowEnabled(true);
    titleBar()->setExtendViewIntoTitleBar(true);
    //titleBar()->setStyledFrame(false);
    titleBar()->setVisible(true);

    m_view->frame()->navigateTo(m_view->settingsItem()->page());

#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
    const QRect availableGeometry = screen()->availableGeometry();
#else
    QRect availableGeometry = QGuiApplication::primaryScreen()->availableGeometry();
#endif
    resize(QSize(2 * availableGeometry.width() / 3, 2 * availableGeometry.height() / 3));
    move(QPoint((availableGeometry.width() - width()) / 2, (availableGeometry.height() - height()) / 2));
}

static QString create_link(const QString& link, const QString& text)
{
    QString ret = QStringLiteral("<a style=\"text-decoration:underline;\" href=\"%1\">%2</a>").arg(link).arg(text);
    return ret;
}

QWidget* Window::createHelpWidget()
{
    QLabel* label = new QLabel();
    QString html = create_link(QStringLiteral("#"), QStringLiteral("Help Topics"));
    html += QStringLiteral("<br><br>");
    html += create_link(QStringLiteral("#"), QStringLiteral("Feedback"));
    html += QStringLiteral("<br><br>");
    html += create_link(QStringLiteral("#"), QStringLiteral("Community"));
    html += QStringLiteral("<br><br>");
    html += create_link(QStringLiteral("#"), QStringLiteral("Legal Information"));
    html += QStringLiteral("<br><br>");
    html += create_link(QStringLiteral("#"), QStringLiteral("Privacy and cookie"));
    html += QStringLiteral("<br><br>");
    label->setText(html);
    return label;
}

void Window::paintEvent(QPaintEvent* event)
{
    NavigationMainWindow::paintEvent(event);
    //QPainter painter(this);
    //QPixmap pixmap(":/image.jpg");
    //painter.drawPixmap(QRect(0, 0, width(), height()), pixmap);
}

void Window::systemButtonClicked()
{
    QMessageBox::information(this, tr("NavigationBar"), tr("Navigation System Button clicked"));
}

void Window::toolButtonClicked()
{
    QAction* action = static_cast<QAction *>(sender());
    QMessageBox::information(this, tr("NavigationBar"), QString(tr("NavigationToolButton clicked: '%1'")).arg(action->text()));
}

void Window::showCompanyWebSite()
{
    QDesktopServices::openUrl(QUrl(QStringLiteral("https://www.devmachines.com")));
}

void Window::projectServiceMenuItemClicked()
{
    QAction* action = static_cast<QAction *>(sender());
    QMessageBox::information(this, tr("NavigationBarMenu"), QString(tr("NavigationMenuItem clicked: '%1'")).arg(action->text()));
}