Commit 81c32c18 authored by Felix Ernst's avatar Felix Ernst Committed by Nate Graham
Browse files

Port ViewModeSettings from macros to std::variant

Macros should be avoided if possible. Instead this commit replaces
the usage of macros with an std::variant of the ViewModeSettings.

As part of this cleanup, code that belongs to the VideModeSettings
class is moved there. Some parameters are also renamed to be more
in line with typical naming conventions.
parent 1118413c
Pipeline #93556 passed with stage
in 5 minutes and 4 seconds
/*
* SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
* SPDX-FileCopyrightText: 2021 Felix Ernst <fe.a.ernst@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
......@@ -10,121 +11,154 @@
#include "dolphin_detailsmodesettings.h"
#include "dolphin_iconsmodesettings.h"
#define VIEWMODESETTINGS_SET_VALUE(mode, setValue, value) \
switch (mode) { \
case ViewModeSettings::IconsMode: IconsModeSettings::setValue(value); break; \
case ViewModeSettings::CompactMode: CompactModeSettings::setValue(value); break; \
case ViewModeSettings::DetailsMode: DetailsModeSettings::setValue(value); break; \
default: Q_ASSERT(false); break; \
ViewModeSettings::ViewModeSettings(DolphinView::Mode mode)
{
switch (mode) {
case DolphinView::IconsView: m_viewModeSettingsVariant = IconsModeSettings::self(); return;
case DolphinView::CompactView: m_viewModeSettingsVariant = CompactModeSettings::self(); return;
case DolphinView::DetailsView: m_viewModeSettingsVariant = DetailsModeSettings::self(); return;
default:
Q_UNREACHABLE();
}
}
#define VIEWMODESETTINGS_RETURN_VALUE(mode, getValue, type) \
type value; \
switch (m_mode) { \
case IconsMode: value = IconsModeSettings::getValue(); break; \
case CompactMode: value = CompactModeSettings::getValue(); break; \
case DetailsMode: value = DetailsModeSettings::getValue(); break; \
default: value = IconsModeSettings::getValue(); \
Q_ASSERT(false); \
break; \
} \
return value
ViewModeSettings::ViewModeSettings(ViewMode mode) :
m_mode(mode)
ViewModeSettings::ViewModeSettings(ViewSettingsTab::Mode mode)
{
switch (mode) {
case ViewSettingsTab::IconsMode: m_viewModeSettingsVariant = IconsModeSettings::self(); return;
case ViewSettingsTab::CompactMode: m_viewModeSettingsVariant = CompactModeSettings::self(); return;
case ViewSettingsTab::DetailsMode: m_viewModeSettingsVariant = DetailsModeSettings::self(); return;
default:
Q_UNREACHABLE();
}
}
ViewModeSettings::~ViewModeSettings()
ViewModeSettings::ViewModeSettings(KStandardItemListView::ItemLayout itemLayout)
{
switch (itemLayout) {
case KStandardItemListView::IconsLayout: m_viewModeSettingsVariant = IconsModeSettings::self(); return;
case KStandardItemListView::CompactLayout: m_viewModeSettingsVariant = CompactModeSettings::self(); return;
case KStandardItemListView::DetailsLayout: m_viewModeSettingsVariant = DetailsModeSettings::self(); return;
default:
Q_UNREACHABLE();
}
}
void ViewModeSettings::setIconSize(int size) const
void ViewModeSettings::setIconSize(int iconSize)
{
VIEWMODESETTINGS_SET_VALUE(m_mode, setIconSize, size);
std::visit([iconSize](auto &&v) {
v->setIconSize(iconSize);
}, m_viewModeSettingsVariant);
}
int ViewModeSettings::iconSize() const
{
VIEWMODESETTINGS_RETURN_VALUE(m_mode, iconSize, int);
return std::visit([](auto &&v) {
return v->iconSize();
}, m_viewModeSettingsVariant);
}
void ViewModeSettings::setPreviewSize(int size) const
void ViewModeSettings::setPreviewSize(int previewSize)
{
VIEWMODESETTINGS_SET_VALUE(m_mode, setPreviewSize, size);
std::visit([previewSize](auto &&v) {
v->setPreviewSize(previewSize);
}, m_viewModeSettingsVariant);
}
int ViewModeSettings::previewSize() const
{
VIEWMODESETTINGS_RETURN_VALUE(m_mode, previewSize, int);
return std::visit([](auto &&v) {
return v->previewSize();
}, m_viewModeSettingsVariant);
}
void ViewModeSettings::setUseSystemFont(bool flag)
void ViewModeSettings::setUseSystemFont(bool useSystemFont)
{
VIEWMODESETTINGS_SET_VALUE(m_mode, setUseSystemFont, flag);
std::visit([useSystemFont](auto &&v) {
v->setUseSystemFont(useSystemFont);
}, m_viewModeSettingsVariant);
}
bool ViewModeSettings::useSystemFont() const
{
VIEWMODESETTINGS_RETURN_VALUE(m_mode, useSystemFont, bool);
return std::visit([](auto &&v) {
return v->useSystemFont();
}, m_viewModeSettingsVariant);
}
void ViewModeSettings::setFontFamily(const QString& fontFamily)
void ViewModeSettings::setFontFamily(const QString &fontFamily)
{
VIEWMODESETTINGS_SET_VALUE(m_mode, setFontFamily, fontFamily);
std::visit([&fontFamily](auto &&v) {
v->setFontFamily(fontFamily);
}, m_viewModeSettingsVariant);
}
QString ViewModeSettings::fontFamily() const
{
VIEWMODESETTINGS_RETURN_VALUE(m_mode, fontFamily, QString);
return std::visit([](auto &&v) {
return v->fontFamily();
}, m_viewModeSettingsVariant);
}
void ViewModeSettings::setFontSize(qreal fontSize)
{
VIEWMODESETTINGS_SET_VALUE(m_mode, setFontSize, fontSize);
std::visit([fontSize](auto &&v) {
v->setFontSize(fontSize);
}, m_viewModeSettingsVariant);
}
qreal ViewModeSettings::fontSize() const
{
VIEWMODESETTINGS_RETURN_VALUE(m_mode, fontSize, qreal);
return std::visit([](auto &&v) {
return v->fontSize();
}, m_viewModeSettingsVariant);
}
void ViewModeSettings::setItalicFont(bool italic)
{
VIEWMODESETTINGS_SET_VALUE(m_mode, setItalicFont, italic);
std::visit([italic](auto &&v) {
v->setItalicFont(italic);
}, m_viewModeSettingsVariant);
}
bool ViewModeSettings::italicFont() const
{
VIEWMODESETTINGS_RETURN_VALUE(m_mode, italicFont, bool);
return std::visit([](auto &&v) {
return v->italicFont();
}, m_viewModeSettingsVariant);
}
void ViewModeSettings::setFontWeight(int fontWeight)
{
VIEWMODESETTINGS_SET_VALUE(m_mode, setFontWeight, fontWeight);
std::visit([fontWeight](auto &&v) {
v->setFontWeight(fontWeight);
}, m_viewModeSettingsVariant);
}
int ViewModeSettings::fontWeight() const
{
VIEWMODESETTINGS_RETURN_VALUE(m_mode, fontWeight, int);
return std::visit([](auto &&v) {
return v->fontWeight();
}, m_viewModeSettingsVariant);
}
void ViewModeSettings::useDefaults(bool useDefaults)
{
std::visit([useDefaults](auto &&v) {
v->useDefaults(useDefaults);
}, m_viewModeSettingsVariant);
}
void ViewModeSettings::readConfig()
{
switch (m_mode) {
case ViewModeSettings::IconsMode: IconsModeSettings::self()->load(); break;
case ViewModeSettings::CompactMode: CompactModeSettings::self()->load(); break;
case ViewModeSettings::DetailsMode: DetailsModeSettings::self()->load(); break;
default: Q_ASSERT(false); break;
}
std::visit([](auto &&v) {
v->load();
}, m_viewModeSettingsVariant);
}
void ViewModeSettings::save()
{
switch (m_mode) {
case ViewModeSettings::IconsMode: IconsModeSettings::self()->save(); break;
case ViewModeSettings::CompactMode: CompactModeSettings::self()->save(); break;
case ViewModeSettings::DetailsMode: DetailsModeSettings::self()->save(); break;
default: Q_ASSERT(false); break;
}
std::visit([](auto &&v) {
return v->save();
}, m_viewModeSettingsVariant);
}
......@@ -7,8 +7,18 @@
#ifndef VIEWMODESETTINGS_H
#define VIEWMODESETTINGS_H
#include "kitemviews/kstandarditemlistview.h"
#include "viewsettingstab.h"
#include "views/dolphinview.h"
#include <QString>
#include <variant>
class CompactModeSettings;
class DetailsModeSettings;
class IconsModeSettings;
/**
* @short Helper class for accessing similar properties of IconsModeSettings,
* CompactModeSettings and DetailsModeSettings.
......@@ -16,26 +26,20 @@
class ViewModeSettings
{
public:
enum ViewMode
{
IconsMode,
CompactMode,
DetailsMode
};
explicit ViewModeSettings(DolphinView::Mode mode);
explicit ViewModeSettings(ViewSettingsTab::Mode mode);
explicit ViewModeSettings(KStandardItemListView::ItemLayout itemLayout);
explicit ViewModeSettings(ViewMode mode);
virtual ~ViewModeSettings();
void setIconSize(int size) const;
void setIconSize(int iconSize);
int iconSize() const;
void setPreviewSize(int size) const;
void setPreviewSize(int previewSize);
int previewSize() const;
void setUseSystemFont(bool flag);
void setUseSystemFont(bool useSystemFont);
bool useSystemFont() const;
void setFontFamily(const QString& fontFamily);
void setFontFamily(const QString &fontFamily);
QString fontFamily() const;
void setFontSize(qreal fontSize);
......@@ -47,11 +51,13 @@ public:
void setFontWeight(int fontWeight);
int fontWeight() const;
void useDefaults(bool useDefaults);
void readConfig();
void save();
private:
ViewMode m_mode;
std::variant<IconsModeSettings *, CompactModeSettings *, DetailsModeSettings *> m_viewModeSettingsVariant;
};
#endif
......@@ -11,6 +11,7 @@
#include "dolphin_iconsmodesettings.h"
#include "dolphinfontrequester.h"
#include "global.h"
#include "settings/viewmodes/viewmodesettings.h"
#include "views/zoomlevelinfo.h"
#include <KLocalizedString>
......@@ -206,7 +207,7 @@ void ViewSettingsTab::applySettings()
break;
}
ViewModeSettings settings(viewMode());
ViewModeSettings settings(m_mode);
const int iconSize = ZoomLevelInfo::iconSizeForZoomLevel(m_defaultSizeSlider->value());
const int previewSize = ZoomLevelInfo::iconSizeForZoomLevel(m_previewSizeSlider->value());
......@@ -224,17 +225,10 @@ void ViewSettingsTab::applySettings()
void ViewSettingsTab::restoreDefaultSettings()
{
KConfigSkeleton* settings = nullptr;
switch (m_mode) {
case IconsMode: settings = IconsModeSettings::self(); break;
case CompactMode: settings = CompactModeSettings::self(); break;
case DetailsMode: settings = DetailsModeSettings::self(); break;
default: Q_ASSERT(false); break;
}
settings->useDefaults(true);
ViewModeSettings settings(m_mode);
settings.useDefaults(true);
loadSettings();
settings->useDefaults(false);
settings.useDefaults(false);
}
void ViewSettingsTab::loadSettings()
......@@ -266,7 +260,7 @@ void ViewSettingsTab::loadSettings()
break;
}
const ViewModeSettings settings(viewMode());
const ViewModeSettings settings(m_mode);
const QSize iconSize(settings.iconSize(), settings.iconSize());
m_defaultSizeSlider->setValue(ZoomLevelInfo::zoomLevelForIconSize(iconSize));
......@@ -285,23 +279,6 @@ void ViewSettingsTab::loadSettings()
m_fontRequester->setCustomFont(font);
}
ViewModeSettings::ViewMode ViewSettingsTab::viewMode() const
{
ViewModeSettings::ViewMode mode;
switch (m_mode) {
case ViewSettingsTab::IconsMode: mode = ViewModeSettings::IconsMode; break;
case ViewSettingsTab::CompactMode: mode = ViewModeSettings::CompactMode; break;
case ViewSettingsTab::DetailsMode: mode = ViewModeSettings::DetailsMode; break;
default: mode = ViewModeSettings::IconsMode;
Q_ASSERT(false);
break;
}
return mode;
}
void ViewSettingsTab::slotDefaultSliderMoved(int value)
{
showToolTip(m_defaultSizeSlider, value);
......
......@@ -7,8 +7,6 @@
#ifndef VIEWSETTINGSTAB_H
#define VIEWSETTINGSTAB_H
#include "settings/viewmodes/viewmodesettings.h"
#include <QWidget>
class DolphinFontRequester;
......@@ -50,8 +48,6 @@ private:
void loadSettings();
void showToolTip(QSlider* slider, int value);
ViewModeSettings::ViewMode viewMode() const;
private:
Mode m_mode;
QSlider* m_defaultSizeSlider;
......
......@@ -13,6 +13,7 @@
#include "dolphinfileitemlistwidget.h"
#include "kitemviews/kfileitemmodel.h"
#include "kitemviews/kitemlistcontroller.h"
#include "settings/viewmodes/viewmodesettings.h"
#include "views/viewmodecontroller.h"
#include "zoomlevelinfo.h"
......@@ -47,7 +48,7 @@ void DolphinItemListView::setZoomLevel(int level)
m_zoomLevel = level;
ViewModeSettings settings(viewMode());
ViewModeSettings settings(itemLayout());
if (previewsShown()) {
const int previewSize = ZoomLevelInfo::iconSizeForZoomLevel(level);
settings.setPreviewSize(previewSize);
......@@ -66,7 +67,7 @@ int DolphinItemListView::zoomLevel() const
void DolphinItemListView::readSettings()
{
ViewModeSettings settings(viewMode());
ViewModeSettings settings(itemLayout());
settings.readConfig();
beginTransaction();
......@@ -125,7 +126,7 @@ void DolphinItemListView::onVisibleRolesChanged(const QList<QByteArray>& current
void DolphinItemListView::updateFont()
{
const ViewModeSettings settings(viewMode());
const ViewModeSettings settings(itemLayout());
if (settings.useSystemFont()) {
KItemListView::updateFont();
......@@ -145,7 +146,7 @@ void DolphinItemListView::updateFont()
void DolphinItemListView::updateGridSize()
{
const ViewModeSettings settings(viewMode());
const ViewModeSettings settings(itemLayout());
// Calculate the size of the icon
const int iconSize = previewsShown() ? settings.previewSize() : settings.iconSize();
......@@ -221,20 +222,3 @@ void DolphinItemListView::updateGridSize()
setItemSize(QSizeF(itemWidth, itemHeight));
endTransaction();
}
ViewModeSettings::ViewMode DolphinItemListView::viewMode() const
{
ViewModeSettings::ViewMode mode;
switch (itemLayout()) {
case KFileItemListView::IconsLayout: mode = ViewModeSettings::IconsMode; break;
case KFileItemListView::CompactLayout: mode = ViewModeSettings::CompactMode; break;
case KFileItemListView::DetailsLayout: mode = ViewModeSettings::DetailsMode; break;
default: mode = ViewModeSettings::IconsMode;
Q_ASSERT(false);
break;
}
return mode;
}
......@@ -9,7 +9,6 @@
#include "dolphin_export.h"
#include "kitemviews/kfileitemlistview.h"
#include "settings/viewmodes/viewmodesettings.h"
class KFileItemListView;
......@@ -47,8 +46,6 @@ protected:
private:
void updateGridSize();
ViewModeSettings::ViewMode viewMode() const;
private:
int m_zoomLevel;
};
......
......@@ -7,9 +7,6 @@
#include "dolphinview.h"
#include "dolphin_compactmodesettings.h"
#include "dolphin_detailsmodesettings.h"
#include "dolphin_iconsmodesettings.h"
#include "dolphin_generalsettings.h"
#include "dolphinitemlistview.h"
#include "dolphinnewfilemenuobserver.h"
......@@ -21,6 +18,7 @@
#include "kitemviews/kitemlistheader.h"
#include "kitemviews/kitemlistselectionmanager.h"
#include "kitemviews/private/kitemlistroleeditor.h"
#include "settings/viewmodes/viewmodesettings.h"
#include "versioncontrol/versioncontrolobserver.h"
#include "viewproperties.h"
#include "views/tooltips/tooltipmanager.h"
......@@ -1532,28 +1530,10 @@ QUrl DolphinView::openItemAsFolderUrl(const KFileItem& item, const bool browseTh
void DolphinView::resetZoomLevel()
{
// TODO : Switch to using ViewModeSettings after MR #256 is merged
int defaultIconSize = KIconLoader::SizeSmall;
switch (m_mode) {
case IconsView:
IconsModeSettings::self()->useDefaults(true);
defaultIconSize = IconsModeSettings::iconSize();
IconsModeSettings::self()->useDefaults(false);
break;
case DetailsView:
DetailsModeSettings::self()->useDefaults(true);
defaultIconSize = DetailsModeSettings::iconSize();
DetailsModeSettings::self()->useDefaults(false);
break;
case CompactView:
CompactModeSettings::self()->useDefaults(true);
defaultIconSize = CompactModeSettings::iconSize();
CompactModeSettings::self()->useDefaults(false);
break;
default:
Q_ASSERT(false);
break;
}
ViewModeSettings settings{m_mode};
settings.useDefaults(true);
const int defaultIconSize = settings.iconSize();
settings.useDefaults(false);
setZoomLevel(ZoomLevelInfo::zoomLevelForIconSize(QSize(defaultIconSize, defaultIconSize)));
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment