Skip to content

Commit

Permalink
Added option to use Fusion style, with configurable colors
Browse files Browse the repository at this point in the history
This is the plain Fusion style for now. A customized style will follow.
  • Loading branch information
bjorn committed Jul 3, 2016
1 parent ceacaf5 commit 43f98c8
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 29 deletions.
45 changes: 35 additions & 10 deletions src/tiled/colorbutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,21 @@
#include "colorbutton.h"

#include <QColorDialog>
#include <QEvent>
#include <QPainter>
#include <QStyle>

using namespace Tiled;
using namespace Tiled::Internal;

ColorButton::ColorButton(QWidget *parent)
: QToolButton(parent)
{
int defaultIconSize = style()->pixelMetric(QStyle::PM_ButtonIconSize);
setIconSize(QSize(defaultIconSize * 2, defaultIconSize));
setColor(Qt::white);

connect(this, SIGNAL(clicked()), this, SLOT(pickColor()));
connect(this, &QToolButton::clicked, this, &ColorButton::pickColor);
}

void ColorButton::setColor(const QColor &color)
Expand All @@ -41,6 +45,36 @@ void ColorButton::setColor(const QColor &color)

mColor = color;

updateIcon();

emit colorChanged(color);
}

void ColorButton::changeEvent(QEvent *e)
{
QToolButton::changeEvent(e);

switch (e->type()) {
case QEvent::StyleChange: {
int defaultIconSize = style()->pixelMetric(QStyle::PM_ButtonIconSize);
setIconSize(QSize(defaultIconSize * 2, defaultIconSize));
updateIcon();
break;
}
default:
break;
}
}

void ColorButton::pickColor()
{
QColor newColor = QColorDialog::getColor(mColor, this);
if (newColor.isValid())
setColor(newColor);
}

void ColorButton::updateIcon()
{
QSize size(iconSize());
size.rwidth() -= 2;
size.rheight() -= 2;
Expand All @@ -55,13 +89,4 @@ void ColorButton::setColor(const QColor &color)
painter.drawRect(0, 0, pixmap.width() - 1, pixmap.height() - 1);

setIcon(QIcon(pixmap));

emit colorChanged(color);
}

void ColorButton::pickColor()
{
QColor newColor = QColorDialog::getColor(mColor, this);
if (newColor.isValid())
setColor(newColor);
}
7 changes: 5 additions & 2 deletions src/tiled/colorbutton.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ class ColorButton : public QToolButton
signals:
void colorChanged(const QColor &color);

private slots:
void pickColor();
protected:
void changeEvent(QEvent *e) override;

private:
void pickColor();
void updateIcon();

QColor mColor;
};

Expand Down
62 changes: 45 additions & 17 deletions src/tiled/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,50 @@ void CommandLineHandler::startNewInstance()
newInstance = true;
}


static void applyStyle()
{
QString defaultStyle = QApplication::style()->objectName();
QPalette defaultPalette = QApplication::palette();

Preferences *preferences = Preferences::instance();

auto apply = [=]() {
QString desiredStyle;
QPalette desiredPalette;

switch (preferences->applicationStyle()) {
default:
case Preferences::SystemDefaultStyle:
desiredStyle = defaultStyle;
desiredPalette = defaultPalette;
break;
case Preferences::FusionStyle:
desiredStyle = QLatin1String("fusion");
desiredPalette = QPalette(preferences->baseColor());

QColor selectionColor = preferences->selectionColor();
bool selectionIsDark = qGray(selectionColor.rgb()) < 110;
desiredPalette.setColor(QPalette::Highlight, selectionColor);
desiredPalette.setColor(QPalette::HighlightedText, selectionIsDark ? Qt::white : Qt::black);
break;
}

if (QApplication::style()->objectName() != desiredStyle)
QApplication::setStyle(QStyleFactory::create(desiredStyle));

if (QApplication::palette() != desiredPalette)
QApplication::setPalette(desiredPalette);
};

apply();

QObject::connect(preferences, &Preferences::applicationStyleChanged, apply);
QObject::connect(preferences, &Preferences::baseColorChanged, apply);
QObject::connect(preferences, &Preferences::selectionColorChanged, apply);
}


int main(int argc, char *argv[])
{
#if QT_VERSION >= 0x050600
Expand All @@ -193,23 +237,7 @@ int main(int argc, char *argv[])
// Enable support for highres images (added in Qt 5.1, but off by default)
a.setAttribute(Qt::AA_UseHighDpiPixmaps);

#ifndef Q_OS_WIN
QString baseName = QApplication::style()->objectName();
if (baseName == QLatin1String("windows")) {
// Avoid Windows 95 style at all cost
if (QStyleFactory::keys().contains(QLatin1String("Fusion"))) {
baseName = QLatin1String("fusion"); // Qt5
} else { // Qt4
// e.g. if we are running on a KDE4 desktop
QByteArray desktopEnvironment = qgetenv("DESKTOP_SESSION");
if (desktopEnvironment == "kde")
baseName = QLatin1String("plastique");
else
baseName = QLatin1String("cleanlooks");
}
a.setStyle(QStyleFactory::create(baseName));
}
#endif
applyStyle();

LanguageManager *languageManager = LanguageManager::instance();
languageManager->installTranslators();
Expand Down
34 changes: 34 additions & 0 deletions src/tiled/preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ Preferences::Preferences()
mUseOpenGL = boolValue("OpenGL");
mObjectLabelVisibility = static_cast<ObjectLabelVisiblity>
(intValue("ObjectLabelVisibility", AllObjectLabels));
mApplicationStyle = static_cast<ApplicationStyle>
(intValue("ApplicationStyle", SystemDefaultStyle));
mBaseColor = colorValue("BaseColor", Qt::lightGray);
mSelectionColor = colorValue("SelectionColor", QApplication::palette().highlight().color());
mSettings->endGroup();

// Retrieve defined object types
Expand Down Expand Up @@ -158,6 +162,36 @@ void Preferences::setObjectLabelVisibility(ObjectLabelVisiblity visibility)
emit objectLabelVisibilityChanged(visibility);
}

void Preferences::setApplicationStyle(ApplicationStyle style)
{
if (mApplicationStyle == style)
return;

mApplicationStyle = style;
mSettings->setValue(QLatin1String("Interface/ApplicationStyle"), style);
emit applicationStyleChanged(style);
}

void Preferences::setBaseColor(const QColor &color)
{
if (mBaseColor == color)
return;

mBaseColor = color;
mSettings->setValue(QLatin1String("Interface/BaseColor"), color.name());
emit baseColorChanged(color);
}

void Preferences::setSelectionColor(const QColor &color)
{
if (mSelectionColor == color)
return;

mSelectionColor = color;
mSettings->setValue(QLatin1String("Interface/SelectionColor"), color.name());
emit selectionColorChanged(color);
}

void Preferences::setShowGrid(bool showGrid)
{
if (mShowGrid == showGrid)
Expand Down
36 changes: 36 additions & 0 deletions src/tiled/preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ class Preferences : public QObject
ObjectLabelVisiblity objectLabelVisibility() const;
void setObjectLabelVisibility(ObjectLabelVisiblity visiblity);

enum ApplicationStyle {
SystemDefaultStyle,
FusionStyle
};

ApplicationStyle applicationStyle() const;
void setApplicationStyle(ApplicationStyle style);

QColor baseColor() const;
void setBaseColor(const QColor &color);

QColor selectionColor() const;
void setSelectionColor(const QColor &color);

Map::LayerDataFormat layerDataFormat() const;
void setLayerDataFormat(Map::LayerDataFormat layerDataFormat);

Expand Down Expand Up @@ -153,6 +167,10 @@ public slots:
void showTilesetGridChanged(bool showTilesetGrid);
void objectLabelVisibilityChanged(ObjectLabelVisiblity);

void applicationStyleChanged(ApplicationStyle);
void baseColorChanged(const QColor &baseColor);
void selectionColorChanged(const QColor &selectionColor);

void useOpenGLChanged(bool useOpenGL);

void objectTypesChanged();
Expand Down Expand Up @@ -187,6 +205,9 @@ public slots:
bool mShowTilesetGrid;
bool mOpenLastFilesOnStartup;
ObjectLabelVisiblity mObjectLabelVisibility;
ApplicationStyle mApplicationStyle;
QColor mBaseColor;
QColor mSelectionColor;

Map::LayerDataFormat mLayerDataFormat;
Map::RenderOrder mMapRenderOrder;
Expand All @@ -211,6 +232,21 @@ public slots:
};


inline Preferences::ApplicationStyle Preferences::applicationStyle() const
{
return mApplicationStyle;
}

inline QColor Preferences::baseColor() const
{
return mBaseColor;
}

inline QColor Preferences::selectionColor() const
{
return mSelectionColor;
}

inline Preferences::ObjectLabelVisiblity Preferences::objectLabelVisibility() const
{
return mObjectLabelVisibility;
Expand Down
30 changes: 30 additions & 0 deletions src/tiled/preferencesdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ PreferencesDialog::PreferencesDialog(QWidget *parent)
connect(mUi->openGL, &QCheckBox::toggled,
preferences, &Preferences::setUseOpenGL);

connect(mUi->styleCombo, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
this, &PreferencesDialog::styleComboChanged);
connect(mUi->baseColor, &ColorButton::colorChanged,
preferences, &Preferences::setBaseColor);
connect(mUi->selectionColor, &ColorButton::colorChanged,
preferences, &Preferences::setSelectionColor);

connect(mUi->autoUpdateCheckBox, &QPushButton::toggled,
this, &PreferencesDialog::autoUpdateToggled);
connect(mUi->checkForUpdate, &QPushButton::clicked,
Expand Down Expand Up @@ -130,6 +137,7 @@ void PreferencesDialog::languageSelected(int index)
void PreferencesDialog::fromPreferences()
{
const Preferences *prefs = Preferences::instance();

mUi->reloadTilesetImages->setChecked(prefs->reloadTilesetsOnChange());
mUi->enableDtd->setChecked(prefs->dtdEnabled());
mUi->openLastFiles->setChecked(prefs->openLastFilesOnStartup());
Expand All @@ -145,6 +153,15 @@ void PreferencesDialog::fromPreferences()
mUi->gridFine->setValue(prefs->gridFine());
mUi->objectLineWidth->setValue(prefs->objectLineWidth());

mUi->styleCombo->setCurrentIndex(prefs->applicationStyle());
mUi->baseColor->setColor(prefs->baseColor());
mUi->selectionColor->setColor(prefs->selectionColor());
bool isFusionStyle = prefs->applicationStyle() == Preferences::FusionStyle;
mUi->baseColor->setEnabled(isFusionStyle);
mUi->baseColorLabel->setEnabled(isFusionStyle);
mUi->selectionColor->setEnabled(isFusionStyle);
mUi->selectionColorLabel->setEnabled(isFusionStyle);

// Auto-updater settings
auto updater = AutoUpdater::instance();
mUi->autoUpdateCheckBox->setEnabled(updater);
Expand All @@ -163,6 +180,19 @@ void PreferencesDialog::retranslateUi()
mUi->languageCombo->setItemText(0, tr("System default"));
}

void PreferencesDialog::styleComboChanged(int index)
{
Preferences *prefs = Preferences::instance();

prefs->setApplicationStyle(static_cast<Preferences::ApplicationStyle>(index));

bool isFusionStyle = prefs->applicationStyle() == Preferences::FusionStyle;
mUi->baseColor->setEnabled(isFusionStyle);
mUi->baseColorLabel->setEnabled(isFusionStyle);
mUi->selectionColor->setEnabled(isFusionStyle);
mUi->selectionColorLabel->setEnabled(isFusionStyle);
}

void PreferencesDialog::autoUpdateToggled(bool checked)
{
if (auto updater = AutoUpdater::instance())
Expand Down
2 changes: 2 additions & 0 deletions src/tiled/preferencesdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ private slots:

void retranslateUi();

void styleComboChanged(int index);

void autoUpdateToggled(bool checked);
void checkForUpdates();

Expand Down

0 comments on commit 43f98c8

Please sign in to comment.