Support for Plasma::Theme-themed stylesheets

This new API returns a stylesheet in Plasma theme colors and
can be used for hybrid widgets

svn path=/trunk/KDE/kdelibs/; revision=1125362
This commit is contained in:
Sebastian Kügler 2010-05-11 08:23:41 +00:00
parent 79d8b41bda
commit 4e07d87284
2 changed files with 95 additions and 0 deletions

View File

@ -135,6 +135,8 @@ public:
void processWallpaperSettings(KConfigBase *metadata);
void processAnimationSettings(const QString &theme, KConfigBase *metadata);
const QString processStyleSheet(const QString &css);
static const char *defaultTheme;
static const char *themeRcFile;
static PackageStructure::Ptr packageStructure;
@ -273,6 +275,54 @@ void ThemePrivate::colorsChanged()
emit q->themeChanged();
}
const QString ThemePrivate::processStyleSheet(const QString &css)
{
QString stylesheet;
if (css.isEmpty()) {
stylesheet = QString("\n\
body {\n\
color: %textcolor;\n\
font-size: %fontsize;\n\
font-family: %fontfamily;\n\
}\n\
a:active { color: %activatedlink; }\n\
a:link { color: %link; }\n\
a:visited { color: %visitedlink; }\n\
a:hover { color: %hoveredlink; text-decoration: none; }\n\
");
} else {
stylesheet = css;
}
QHash<QString, QString> elements;
// If you add elements here, make sure their names are sufficiently unique to not cause
// clashes between element keys
elements["%textcolor"] = Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor).name();
elements["%backgroundcolor"] =
Plasma::Theme::defaultTheme()->color(Plasma::Theme::BackgroundColor).name();
elements["%visitedlink"] =
Plasma::Theme::defaultTheme()->color(Plasma::Theme::VisitedLinkColor).name();
elements["%activatedlink"] =
Plasma::Theme::defaultTheme()->color(Plasma::Theme::HighlightColor).name();
elements["%hoveredlink"] =
Plasma::Theme::defaultTheme()->color(Plasma::Theme::HighlightColor).name();
elements["%link"] = Plasma::Theme::defaultTheme()->color(Plasma::Theme::LinkColor).name();
elements["%buttonbackgroundcolor"] =
Plasma::Theme::defaultTheme()->color(Plasma::Theme::ButtonBackgroundColor).name();
elements["%smallfontsize"] =
QString("%1pt").arg(KGlobalSettings::smallestReadableFont().pointSize());
QFont font = Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont);
elements["%fontsize"] =
QString("%1pt").arg(font.pointSize());
elements["%fontfamily"] = font.family();
foreach (const QString &k, elements.keys()) {
stylesheet.replace(k, elements[k]);
}
return stylesheet;
}
class ThemeSingleton
{
public:
@ -580,6 +630,11 @@ QString Theme::imagePath(const QString &name) const
return path;
}
QString Theme::styleSheet(const QString &css) const
{
return d->processStyleSheet(css);
}
QString Theme::animationPath(const QString &name) const
{
const QString path = d->animationMapping.value(name);

40
theme.h
View File

@ -215,6 +215,46 @@ class PLASMA_EXPORT Theme : public QObject
*/
bool useNativeWidgetStyle() const;
/**
* Provides a Plasma::Theme-themed stylesheet for hybrid (web / native Plasma) widgets.
*
* You can use this method to retrieve a basic default stylesheet, or to theme your
* custom stylesheet you use for example in Plasma::WebView. The QString you can pass
* into this method does not have to be a valid stylesheet, in fact you can use this
* method to replace color placeholders with the theme's color in any QString.
*
* In order to use this method with a custom stylesheet, just put for example %textcolor
* in your QString and it will be replaced with the theme's text (or foreground) color.
*
* Just like in many other methods for retrieving theme information, do not forget to
* update your stylesheet upon the themeChanged() signal.
*
* The following tags will be replaced by corresponding colors from Plasma::Theme:
*
* %textcolor
* %backgroundcolor
* %buttonbackgroundcolor
*
* %link
* %activatedlink
* %hoveredlink
* %visitedlink
*
* %fontfamily
* %fontsize
* %smallfontsize
*
* @param css a stylesheet to theme, leave empty for a default stylesheet containing
* theming for some commonly used elements, body text and links, for example.
*
* @return a piece of CSS that sets the most commonly used style elements to a theme
* matching Plasma::Theme.
*
* @since 4.5
*/
Q_INVOKABLE QString styleSheet(const QString &css = QString()) const;
/**
* Tries to load pixmap with the specified key from cache.
*