ThemeSettingsDialog.cpp 3.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 *	(c) LSIIT, UMR CNRS/UdS
 *	Authors: F. Larue.
 *
 *	See licence.txt for additional information.
 */


#include <UIMainWindow.h>
#include <ThemeSettingsDialog.h>




ThemeSettingsDialog::ThemeSettingsDialog( QWidget *parent ) : QDialog( parent )
{
17
    // Create the widget containing the palette color matrix.
18

19 20 21 22 23 24
    QGridLayout *matrixLayout = new QGridLayout();
    matrixLayout->setSpacing( 0 );
    matrixLayout->setContentsMargins( 0, 0, 0, 0 );

    QWidget *paletteMatrix = new QWidget();
    paletteMatrix->setLayout( matrixLayout );
25

26
    // Define matrix horizontal labels corresponding to color group names.
27 28 29 30 31 32

    int buttonWidth = 0;
    for( int i=0; i<QPalette::NColorGroups; ++i )
    {
        QString groupName = PaletteDictionary::ColorGroupName( (QPalette::ColorGroup) i );
        groupName[0] = groupName[0].toUpper();
33
        matrixLayout->addWidget( new QLabel(groupName), 0, i+1, Qt::AlignHCenter );
34

35
#if QT_VERSION < QT_VERSION_CHECK(5,11,0)
36
        int w = QApplication::fontMetrics().width( groupName ) + 10;
37 38 39
#else
        int w = QApplication::fontMetrics().horizontalAdvance( groupName ) + 10;
#endif
40 41 42 43
        if( w > buttonWidth )
            buttonWidth = w;
    }

44
    // Define matrix vertical labels corresponding to color role names.
45 46 47 48 49 50 51

    for( int i=0; i<QPalette::NColorRoles; ++i )
    {
        QString roleName = PaletteDictionary::ColorRoleName( (QPalette::ColorRole) i );
        for( int j=0; j<roleName.length(); ++j )
            if( j == 0  ||  roleName[j-1] == '-' )
                roleName[j] = roleName[j].toUpper();
52
        matrixLayout->addWidget( new QLabel(roleName.remove('-')), i+1, 0 );
53 54
    }

55
    // Create the button matrix based on the palette colors.
56 57 58 59 60 61 62 63 64 65 66 67

    for( int i=0; i<QPalette::NColorGroups; ++i )
        for( int j=0; j<QPalette::NColorRoles; ++j )
        {
            QPushButton *b = new QPushButton( QString::number(j*QPalette::NColorGroups+i) );
            b->setMinimumWidth( buttonWidth );

            QColor c = QApplication::palette().color( (QPalette::ColorGroup) i, (QPalette::ColorRole) j );
            setButtonColor( b, c );

            connect( b, SIGNAL(clicked(bool)), this, SLOT(onColorButtonTrigger()) );

68
            matrixLayout->addWidget( b, j+1, i+1 );
69
        }
70 71 72 73 74 75 76 77 78 79 80 81 82 83

    // Create dialog "OK" and "Cancel" buttons.

    QDialogButtonBox *buttons = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this );

    connect( buttons, SIGNAL(accepted()), this, SLOT(accept()) );
    connect( buttons, SIGNAL(rejected()), this, SLOT(reject()) );

    // Set the dialog layout.

    QVBoxLayout *l = new QVBoxLayout();
    setLayout( l );
    l->addWidget( paletteMatrix );
    l->addWidget( buttons );
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
}


void ThemeSettingsDialog::setButtonColor( QPushButton *button, const QColor& c )
{
    QString colorString = "rgb(" + QString::number(c.red()) + "," + QString::number(c.green()) + "," + QString::number(c.blue()) + ")";
    QString styleSheet = "QPushButton { border-radius:0; background-color:"+colorString+"; color:"+colorString+"; }";
    button->setStyleSheet( styleSheet );
}


void ThemeSettingsDialog::onColorButtonTrigger()
{
    QPushButton *src = (QPushButton*) sender();

    int n = src->text().toInt();
    QPalette::ColorGroup group = (QPalette::ColorGroup)( n % QPalette::NColorGroups );
    QPalette::ColorRole  role  = (QPalette::ColorRole )( n / QPalette::NColorGroups );

    QPalette palette = QApplication::palette();
    QColor currentColor = palette.color( group, role );

    QColor newColor = QColorDialog::getColor( currentColor, this );
    if( newColor.isValid() )
    {
        setButtonColor( src, newColor );
        palette.setColor( group, role, newColor );
        QApplication::setPalette( palette );

        QApplication* app = (QApplication*) QApplication::instance();
        app->setStyleSheet( app->styleSheet() );
    }
}