241 lines
6.6 KiB
C++
Raw Normal View History

2020-08-11 17:56:53 +02:00
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* This file may be used under the terms of the 3-clause BSD License
*****************************************************************************/
#include "label/LabelPage.h"
#include "progressbar/ProgressBarPage.h"
2023-02-27 09:56:41 +01:00
#include "inputs/InputPage.h"
2022-04-01 14:54:31 +02:00
#include "button/ButtonPage.h"
#include "selector/SelectorPage.h"
2022-07-05 15:45:06 +02:00
#include "dialog/DialogPage.h"
2020-08-11 17:56:53 +02:00
#include <SkinnyShortcut.h>
#include <SkinnyShapeProvider.h>
2022-04-01 15:58:47 +02:00
#include <SkinnyNamespace.h>
2020-08-11 17:56:53 +02:00
2022-07-12 16:51:39 +02:00
#include <QskApplicationView.h>
2020-08-11 17:56:53 +02:00
#include <QskFocusIndicator.h>
#include <QskObjectCounter.h>
#include <QskTabView.h>
2022-04-01 14:54:31 +02:00
#include <QskTextLabel.h>
#include <QskSwitchButton.h>
2022-04-01 15:58:47 +02:00
#include <QskPushButton.h>
2022-04-05 10:41:36 +02:00
#include <QskMenu.h>
2020-08-11 17:56:53 +02:00
#include <QskWindow.h>
2022-04-19 08:42:53 +02:00
#include <QskDialog.h>
2022-07-14 13:19:25 +02:00
#include <QskSkinManager.h>
#include <QskSkin.h>
#include <QskSkinTransition.h>
#include <QskAnimationHint.h>
#include <QskSetup.h>
2020-08-11 17:56:53 +02:00
#include <QGuiApplication>
2021-12-06 19:20:59 +01:00
namespace
{
class TabView : public QskTabView
{
public:
TabView( QQuickItem* parent = nullptr )
: QskTabView( parent )
{
setAutoFitTabs( true );
}
2022-04-01 14:54:31 +02:00
void setTabsEnabled( bool on )
{
for ( int i = 0; i < count(); i++ )
itemAt( i )->setEnabled( on );
}
};
2022-04-05 10:41:36 +02:00
class MenuButton : public QskPushButton
{
public:
MenuButton( const QString& text, QQuickItem* parent = nullptr )
: QskPushButton( text, parent )
{
connect( this, &QskPushButton::pressed, this, &MenuButton::openMenu );
}
private:
void openMenu()
{
2022-04-05 11:38:23 +02:00
auto menu = new QskMenu( window()->contentItem() );
2022-04-05 10:41:36 +02:00
2022-07-14 13:19:25 +02:00
populateMenu( menu );
menu->setOrigin( geometry().bottomLeft() );
menu->open();
}
virtual void populateMenu( QskMenu* ) = 0;
};
class SkinButton final : public MenuButton
{
public:
SkinButton( const QString& text, QQuickItem* parent = nullptr )
: MenuButton( text, parent )
{
}
private:
void populateMenu( QskMenu* menu ) override
{
const auto names = qskSkinManager->skinNames();
for ( const auto& name : names )
menu->addOption( QUrl(), name );
if ( const auto index = names.indexOf( qskSetup->skinName() ) )
menu->setCurrentIndex( index );
connect( menu, &QskMenu::triggered, this, &SkinButton::changeSkin );
}
void changeSkin( int index )
{
const auto names = qskSkinManager->skinNames();
if ( index < 0 || index >= names.size() )
return;
if ( index == names.indexOf( qskSetup->skinName() ) )
return;
auto oldSkin = qskSetup->skin();
if ( oldSkin->parent() == qskSetup )
oldSkin->setParent( nullptr ); // otherwise setSkin deletes it
if ( auto newSkin = qskSetup->setSkin( names[ index ] ) )
{
QskSkinTransition transition;
transition.setSourceSkin( oldSkin );
transition.setTargetSkin( newSkin );
transition.setAnimation( 500 );
transition.process();
if ( oldSkin->parent() == nullptr )
delete oldSkin;
}
}
};
class FileButton final : public MenuButton
{
public:
FileButton( const QString& text, QQuickItem* parent = nullptr )
: MenuButton( text, parent )
{
}
private:
void populateMenu( QskMenu* menu ) override
{
2022-04-05 11:38:23 +02:00
menu->addOption( "image://shapes/Rectangle/White", "Print" );
menu->addOption( "image://shapes/Diamond/Yellow", "Save As" );
menu->addOption( "image://shapes/Ellipse/Red", "Setup" );
menu->addSeparator();
2022-05-30 07:49:58 +02:00
menu->addOption( "image://shapes/Hexagon/PapayaWhip", "Quit" );
// see https://github.com/uwerat/qskinny/issues/192
connect( menu, &QskMenu::triggered,
[]( int index ) { if ( index == 3 ) qApp->quit(); } );
2022-04-05 10:41:36 +02:00
}
};
2022-07-14 13:19:25 +02:00
2022-04-01 14:54:31 +02:00
/*
Once QskApplicationView and friends are implemented we can replace
Header/ApplicationWindow with it. TODO ...
*/
class Header : public QskLinearBox
{
Q_OBJECT
public:
Header( QQuickItem* parent = nullptr )
: QskLinearBox( Qt::Horizontal, parent )
{
initSizePolicy( QskSizePolicy::Ignored, QskSizePolicy::Fixed );
setPanel( true );
2022-04-01 15:58:47 +02:00
2022-07-14 13:19:25 +02:00
new FileButton( "File", this );
new SkinButton( "Skin", this );
2022-04-05 10:41:36 +02:00
2022-04-01 14:54:31 +02:00
addStretch( 10 );
2022-04-01 15:58:47 +02:00
{
new QskTextLabel( "Enabled", this );
2022-04-01 14:54:31 +02:00
2022-04-01 15:58:47 +02:00
auto button = new QskSwitchButton( this );
button->setChecked( true );
2022-04-01 14:54:31 +02:00
2022-04-01 15:58:47 +02:00
connect( button, &QskSwitchButton::toggled,
this, &Header::enabledToggled );
}
2022-04-01 14:54:31 +02:00
}
Q_SIGNALS:
void enabledToggled( bool );
};
2022-07-12 16:51:39 +02:00
class ApplicationView : public QskApplicationView
2022-04-01 14:54:31 +02:00
{
public:
ApplicationView( QQuickItem* parent = nullptr )
2022-07-12 16:51:39 +02:00
: QskApplicationView( parent )
2022-04-01 14:54:31 +02:00
{
auto header = new Header( this );
auto tabView = new TabView( this );
tabView->addTab( "Buttons", new ButtonPage() );
2022-07-15 13:13:14 +02:00
tabView->addTab( "Labels", new LabelPage() );
2023-02-27 09:56:41 +01:00
tabView->addTab( "Inputs", new InputPage() );
2022-04-01 14:54:31 +02:00
tabView->addTab( "Progress\nBars", new ProgressBarPage() );
tabView->addTab( "Selectors", new SelectorPage() );
2022-07-05 15:45:06 +02:00
tabView->addTab( "Dialogs", new DialogPage() );
2022-04-01 14:54:31 +02:00
connect( header, &Header::enabledToggled,
tabView, &TabView::setTabsEnabled );
2022-07-12 16:51:39 +02:00
setHeader( header );
2022-07-15 13:13:14 +02:00
setBody( tabView );
2022-04-01 14:54:31 +02:00
}
2021-12-06 19:20:59 +01:00
};
}
2020-08-11 17:56:53 +02:00
int main( int argc, char* argv[] )
{
#ifdef ITEM_STATISTICS
QskObjectCounter counter( true );
#endif
Qsk::addGraphicProvider( "shapes", new SkinnyShapeProvider() );
2022-04-19 08:42:53 +02:00
// dialogs in faked windows -> QskSubWindow
QskDialog::instance()->setPolicy( QskDialog::EmbeddedBox );
2020-08-11 17:56:53 +02:00
QGuiApplication app( argc, argv );
SkinnyShortcut::enable( SkinnyShortcut::AllShortcuts );
2022-04-01 14:54:31 +02:00
auto mainView = new ApplicationView();
2020-08-11 17:56:53 +02:00
QSize size( 800, 600 );
2022-04-01 14:54:31 +02:00
size = size.expandedTo( mainView->sizeHint().toSize() );
2020-08-11 17:56:53 +02:00
QskWindow window;
2022-04-01 14:54:31 +02:00
window.addItem( mainView );
2020-08-11 17:56:53 +02:00
window.addItem( new QskFocusIndicator() );
window.resize( size );
window.show();
return app.exec();
}
2022-04-01 14:54:31 +02:00
#include "main.moc"