Added Qt stylesheet GUI selection code.

This commit is contained in:
mjbudd77 2021-03-01 21:48:57 -05:00
parent ac0f377873
commit 2a6a31fa48
5 changed files with 264 additions and 27 deletions

View File

@ -90,6 +90,8 @@ consoleWin_t::consoleWin_t(QWidget *parent)
int opt, xWinSize = 256, yWinSize = 240;
int use_SDL_video = false;
int setFullScreen = false;
int useCustomQss = false;
const char *styleSheetEnv = NULL;
std::string guiStyle;
g_config->getOption("SDL.GuiStyle", &guiStyle );
@ -104,6 +106,35 @@ consoleWin_t::consoleWin_t(QWidget *parent)
}
}
styleSheetEnv = ::getenv("FCEUX_QT_STYLESHEET");
if ( styleSheetEnv )
{
g_config->setOption("SDL.UseCustomQss", 1);
g_config->setOption("SDL.QtStyleSheet", styleSheetEnv);
}
g_config->getOption("SDL.UseCustomQss", &useCustomQss);
if ( useCustomQss )
{
g_config->getOption("SDL.QtStyleSheet", &guiStyle );
if ( guiStyle.size() > 0 )
{
QFile File(guiStyle.c_str());
if ( File.open(QFile::ReadOnly) )
{
QString StyleSheet = QLatin1String(File.readAll());
setStyleSheet(StyleSheet);
//printf("Using Qt Stylesheet file '%s'\n", filepath );
}
}
}
createMainMenu();
g_config->getOption( "SDL.VideoDriver", &use_SDL_video );

View File

@ -22,6 +22,7 @@
#include <QTextEdit>
#include <QApplication>
#include <QStyleFactory>
#include <QFileDialog>
#include "Qt/GuiConf.h"
#include "Qt/main.h"
@ -30,6 +31,7 @@
#include "Qt/keyscan.h"
#include "Qt/fceuWrapper.h"
#include "Qt/ConsoleWindow.h"
#include "Qt/ConsoleUtilities.h"
//----------------------------------------------------
GuiConfDialog_t::GuiConfDialog_t(QWidget *parent)
@ -37,12 +39,15 @@ GuiConfDialog_t::GuiConfDialog_t(QWidget *parent)
{
int useNativeFileDialogVal;
int useNativeMenuBarVal;
QVBoxLayout *mainLayout;
int useCustomQssVal;
QVBoxLayout *mainLayout, *vbox1, *vbox2;
QHBoxLayout *hbox;
QPushButton *closeButton;
QPushButton *closeButton, *button;
QLabel *lbl;
QStringList styleKeys;
QString selStyle;
QGroupBox *frame, *qssFrame;
std::string qssFile;
//resize( 512, 600 );
//printf("Style: %s \n", style()->objectName().toStdString().c_str() );
@ -52,6 +57,7 @@ GuiConfDialog_t::GuiConfDialog_t(QWidget *parent)
// sync with config
g_config->getOption("SDL.UseNativeFileDialog", &useNativeFileDialogVal);
g_config->getOption("SDL.UseNativeMenuBar", &useNativeMenuBarVal);
g_config->getOption("SDL.UseCustomQss", &useCustomQssVal);
setWindowTitle(tr("GUI Config"));
@ -66,9 +72,6 @@ GuiConfDialog_t::GuiConfDialog_t(QWidget *parent)
connect(useNativeFileDialog, SIGNAL(stateChanged(int)), this, SLOT(useNativeFileDialogChanged(int)));
connect(useNativeMenuBar, SIGNAL(stateChanged(int)), this, SLOT(useNativeMenuBarChanged(int)));
hbox = new QHBoxLayout();
lbl = new QLabel( tr("Style:") );
styleComboBox = new QComboBox();
styleKeys = QStyleFactory::keys();
@ -85,12 +88,51 @@ GuiConfDialog_t::GuiConfDialog_t(QWidget *parent)
}
connect(styleComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(styleChanged(int)));
frame = new QGroupBox( tr("Style:") );
vbox1 = new QVBoxLayout();
vbox2 = new QVBoxLayout();
hbox = new QHBoxLayout();
frame->setLayout( vbox1 );
vbox1->addLayout( hbox );
lbl = new QLabel( tr("Base:") );
hbox->addWidget( lbl, 1 );
hbox->addWidget( styleComboBox, 10 );
mainLayout->addLayout(hbox);
qssFrame = new QGroupBox( tr("QSS") );
qssFrame->setLayout( vbox2 );
useCustomStyle = new QCheckBox( tr("Use Custom Stylesheet") );
useCustomStyle->setChecked(useCustomQssVal);
connect(useCustomStyle, SIGNAL(stateChanged(int)), this, SLOT(useCustomStyleChanged(int)));
vbox1->addWidget( qssFrame );
vbox2->addWidget( useCustomStyle );
hbox = new QHBoxLayout();
button = new QPushButton(tr("Open"));
button->setIcon(style()->standardIcon(QStyle::SP_FileDialogStart));
connect(button, SIGNAL(clicked(void)), this, SLOT(openQss(void)));
hbox->addWidget(button);
button = new QPushButton(tr("Clear"));
button->setIcon(style()->standardIcon(QStyle::SP_LineEditClearButton));
connect(button, SIGNAL(clicked(void)), this, SLOT(clearQss(void)));
hbox->addWidget(button);
vbox2->addLayout( hbox );
g_config->getOption("SDL.QtStyleSheet", &qssFile);
custom_qss_path = new QLineEdit();
custom_qss_path->setReadOnly(true);
custom_qss_path->setText(qssFile.c_str());
vbox2->addWidget( custom_qss_path );
mainLayout->addWidget(useNativeFileDialog);
mainLayout->addWidget(useNativeMenuBar);
mainLayout->addWidget(frame);
closeButton = new QPushButton( tr("Close") );
closeButton->setIcon(style()->standardIcon(QStyle::SP_DialogCloseButton));
@ -143,6 +185,27 @@ void GuiConfDialog_t::useNativeMenuBarChanged(int state)
consoleWindow->menuBar()->setNativeMenuBar(value);
}
//----------------------------------------------------
void GuiConfDialog_t::useCustomStyleChanged(int state)
{
int value = (state == Qt::Unchecked) ? 0 : 1;
g_config->setOption("SDL.UseCustomQss", value);
if ( consoleWindow != NULL )
{
if ( value )
{
std::string s;
g_config->getOption("SDL.QtStyleSheet", &s);
loadQss( s.c_str() );
}
else
{
consoleWindow->setStyleSheet(NULL);
}
}
}
//----------------------------------------------------
void GuiConfDialog_t::styleChanged(int index)
{
QString s;
@ -163,3 +226,137 @@ void GuiConfDialog_t::styleChanged(int index)
}
}
//----------------------------------------------------
void GuiConfDialog_t::clearQss(void)
{
//g_config->setOption("SDL.Palette", "");
custom_qss_path->setText("");
g_config->setOption("SDL.QtStyleSheet", "");
g_config->save();
if ( consoleWindow != NULL )
{
consoleWindow->setStyleSheet(NULL);
}
}
//----------------------------------------------------
void GuiConfDialog_t::openQss(void)
{
int ret, useNativeFileDialogVal, useCustom;
QString filename;
std::string last, iniPath;
char dir[512];
char exePath[512];
QFileDialog dialog(this, tr("Open Qt Stylesheet (QSS)"));
QList<QUrl> urls;
QDir d;
fceuExecutablePath(exePath, sizeof(exePath));
//urls = dialog.sidebarUrls();
urls << QUrl::fromLocalFile(QDir::rootPath());
urls << QUrl::fromLocalFile(QStandardPaths::standardLocations(QStandardPaths::HomeLocation).first());
urls << QUrl::fromLocalFile(QDir(FCEUI_GetBaseDirectory()).absolutePath());
if (exePath[0] != 0)
{
d.setPath(QString(exePath) + "/../qss");
if (d.exists())
{
urls << QUrl::fromLocalFile(d.absolutePath());
iniPath = d.absolutePath().toStdString();
}
}
#ifdef WIN32
#else
d.setPath("/usr/share/fceux/qss");
if (d.exists())
{
urls << QUrl::fromLocalFile(d.absolutePath());
iniPath = d.absolutePath().toStdString();
}
#endif
dialog.setFileMode(QFileDialog::ExistingFile);
dialog.setNameFilter(tr("Qt Stylesheets (*.qss *.QSS) ;; All files (*)"));
dialog.setViewMode(QFileDialog::List);
dialog.setFilter(QDir::AllEntries | QDir::AllDirs | QDir::Hidden);
dialog.setLabelText(QFileDialog::Accept, tr("Load"));
g_config->getOption("SDL.QtStyleSheet", &last);
if (last.size() == 0)
{
last.assign(iniPath);
}
getDirFromFile(last.c_str(), dir);
dialog.setDirectory(tr(dir));
// Check config option to use native file dialog or not
g_config->getOption("SDL.UseNativeFileDialog", &useNativeFileDialogVal);
dialog.setOption(QFileDialog::DontUseNativeDialog, !useNativeFileDialogVal);
dialog.setSidebarUrls(urls);
ret = dialog.exec();
if (ret)
{
QStringList fileList;
fileList = dialog.selectedFiles();
if (fileList.size() > 0)
{
filename = fileList[0];
}
}
if (filename.isNull())
{
return;
}
qDebug() << "selected file path : " << filename.toUtf8();
custom_qss_path->setText(filename.toStdString().c_str());
g_config->getOption("SDL.UseCustomQss", &useCustom);
if ( useCustom )
{
loadQss( filename.toStdString().c_str() );
}
g_config->setOption("SDL.QtStyleSheet", filename.toStdString().c_str() );
g_config->save();
return;
}
//----------------------------------------------------
void GuiConfDialog_t::loadQss( const char *filepath )
{
if ( consoleWindow != NULL )
{
QFile File(filepath);
if ( File.open(QFile::ReadOnly) )
{
QString StyleSheet = QLatin1String(File.readAll());
consoleWindow->setStyleSheet(StyleSheet);
//printf("Using Qt Stylesheet file '%s'\n", filepath );
}
else
{
//printf("Warning: Could not open Qt Stylesheet file '%s'\n", filepath );
}
}
}
//----------------------------------------------------

View File

@ -28,9 +28,13 @@ public:
protected:
void closeEvent(QCloseEvent *event);
void loadQss( const char *filepath );
QCheckBox *useNativeFileDialog;
QCheckBox *useNativeMenuBar;
QCheckBox *useCustomStyle;
QComboBox *styleComboBox;
QLineEdit *custom_qss_path;
private:
public slots:
@ -38,5 +42,8 @@ public slots:
private slots:
void useNativeFileDialogChanged(int v);
void useNativeMenuBarChanged(int v);
void useCustomStyleChanged(int v);
void styleChanged(int index);
void openQss(void);
void clearQss(void);
};

View File

@ -374,7 +374,9 @@ InitConfig()
config->addOption("_useNativeFileDialog", "SDL.UseNativeFileDialog", false);
config->addOption("_useNativeMenuBar" , "SDL.UseNativeMenuBar", false);
config->addOption("SDL.GuiStyle", "");
config->addOption("SDL.QtStyleSheet", "");
config->addOption("SDL.UseCustomQss", 0);
config->addOption("_setSchedParam" , "SDL.SetSchedParam" , 0);
config->addOption("_emuSchedPolicy" , "SDL.EmuSchedPolicy", 0);
config->addOption("_emuSchedNice" , "SDL.EmuSchedNice" , 0);

View File

@ -74,29 +74,29 @@ int main( int argc, char *argv[] )
int retval;
qInstallMessageHandler(MessageOutput);
QApplication app(argc, argv);
const char *styleSheetEnv = NULL;
//const char *styleSheetEnv = NULL;
//app.setStyle( new MenuStyle() );
styleSheetEnv = ::getenv("FCEUX_QT_STYLESHEET");
if ( styleSheetEnv != NULL )
{
QFile File(styleSheetEnv);
if ( File.open(QFile::ReadOnly) )
{
QString StyleSheet = QLatin1String(File.readAll());
app.setStyleSheet(StyleSheet);
printf("Using Qt Stylesheet file '%s'\n", styleSheetEnv );
}
else
{
printf("Warning: Could not open Qt Stylesheet file '%s'\n", styleSheetEnv );
}
}
//styleSheetEnv = ::getenv("FCEUX_QT_STYLESHEET");
//
//if ( styleSheetEnv != NULL )
//{
// QFile File(styleSheetEnv);
//
// if ( File.open(QFile::ReadOnly) )
// {
// QString StyleSheet = QLatin1String(File.readAll());
//
// app.setStyleSheet(StyleSheet);
//
// printf("Using Qt Stylesheet file '%s'\n", styleSheetEnv );
// }
// else
// {
// printf("Warning: Could not open Qt Stylesheet file '%s'\n", styleSheetEnv );
// }
//}
fceuWrapperInit( argc, argv );