Skip to content

Commit

Permalink
Add styleSheetType to application reportStyleSheet
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Oct 17, 2019
1 parent 05c7c69 commit 30eea3d
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 42 deletions.
19 changes: 14 additions & 5 deletions python/core/auto_generated/qgsapplication.sip.in
Expand Up @@ -76,6 +76,12 @@ to change due to centralization.
%End
public:

enum StyleSheetType
{
Qt,
WebBrowser,
};

static const char *QGIS_ORGANIZATION_NAME;
static const char *QGIS_ORGANIZATION_DOMAIN;
static const char *QGIS_APPLICATION_NAME;
Expand Down Expand Up @@ -503,20 +509,23 @@ Gets application icon
Returns whether this machine uses big or little endian
%End

static QString reportStyleSheet();
static QString reportStyleSheet( const StyleSheetType &styleSheetType = StyleSheetType::Qt );
%Docstring
Returns a standard css style sheet for reports.
Returns a css style sheet for reports, the ``styleSheetType`` argument
determines what type of stylesheet is supported by the widget.

Typically you will use this method by doing:
QString myStyle = QgsApplication.reportStyleSheet();
textBrowserReport->document()->setDefaultStyleSheet(myStyle);
if you are using a QgsWebView you will need to manually inject
the CSS into a <head><script> tag instead.

:return: QString containing the CSS 2.1 compliant stylesheet.
:return: the stylesheet CSS rules.

.. note::

you can use the special Qt extensions too, for example
the gradient fills for backgrounds.
if styleSheetType equals StyleSheetType.Qt you can use the special Qt extensions too,
for example the gradient fills for backgrounds.
%End

static QString showSettings();
Expand Down
15 changes: 1 addition & 14 deletions src/app/qgsrasterlayerproperties.cpp
Expand Up @@ -2052,20 +2052,7 @@ bool QgsRasterLayerProperties::rasterIsMultiBandColor()

void QgsRasterLayerProperties::updateInformationContent()
{
// We are using QgsWebView and the renderer is completely different from QTextBrowser
// add some extra style here
QString myStyle = QgsApplication::reportStyleSheet();
myStyle.append( QStringLiteral( "body { margin: auto; width: 97% } "
"table.tabular-view, table.list-view { border-collapse: collapse; table-layout:fixed; width: 100% !important; } "
"h1 { line-height: inherit; } "
"td, th { word-wrap: break-word; vertical-align: top; } "
".list-view th:first-child, .list-view td:first-child { width: 15%; } "
".list-view.highlight { padding-left: inherit; } "
".tabular-view th:first-child, .tabular-view td:first-child { width: 20%; } "
".tabular-view th.strong { background-color: #eee; }"
".tabular-view th, .tabular-view td { "
" border: solid 1px #eee;"
"} \n " ) );
const QString myStyle = QgsApplication::reportStyleSheet( QgsApplication::StyleSheetType::WebBrowser );
// Inject the stylesheet
const QString html { mRasterLayer->htmlMetadata().replace( QStringLiteral( "<head>" ), QStringLiteral( R"raw(<head><style type="text/css">%1</style>)raw" ) ).arg( myStyle ) };
mMetadataViewer->setHtml( html );
Expand Down
84 changes: 66 additions & 18 deletions src/core/qgsapplication.cpp
Expand Up @@ -1309,7 +1309,7 @@ QString QgsApplication::showSettings()
return myState;
}

QString QgsApplication::reportStyleSheet()
QString QgsApplication::reportStyleSheet( const StyleSheetType &styleSheetType )
{
//
// Make the style sheet desktop preferences aware by using qappliation
Expand Down Expand Up @@ -1376,9 +1376,13 @@ QString QgsApplication::reportStyleSheet()
" padding: 0px 3px; "
" font-size: small;"
"}"
".section {"
"th .strong {"
" font-weight: bold;"
" padding-top:25px;"
"}"
"hr {"
" border: 0;"
" height: 0;"
" border-top: 1px solid black;"
"}"
".list-view .highlight {"
" text-align: right;"
Expand All @@ -1388,24 +1392,68 @@ QString QgsApplication::reportStyleSheet()
" padding-left: 20px;"
" font-weight: bold;"
"}"
"th .strong {"
" font-weight: bold;"
"}"
".tabular-view{ "
" border-collapse: collapse;"
" width: 95%;"
"}"
".tabular-view th, .tabular-view td { "
" border:10px solid black;"
"}"
".tabular-view .odd-row{"
".tabular-view .odd-row {"
" background-color: #f9f9f9;"
"}"
"hr {"
" border: 0;"
" height: 0;"
" border-top: 1px solid black;"
".section {"
" font-weight: bold;"
" padding-top:25px;"
"}" );

// We have some subtle differences between Qt based style and QWebKit style
if ( styleSheetType == StyleSheetType::Qt )
{
myStyle += QStringLiteral(
".tabular-view{ "
" border-collapse: collapse;"
" width: 95%;"
"}"
".tabular-view th, .tabular-view td { "
" border:10px solid black;"
"}" );
}
else
{
myStyle += QStringLiteral(
"body { "
" margin: auto;"
" width: 97%;"
"}"
"table.tabular-view, table.list-view { "
" border-collapse: collapse;"
" table-layout:fixed;"
" width: 100% !important;"
"}"
// Override
"h1 { "
" line-height: inherit;"
"}"
"td, th {"
" word-wrap: break-word; "
" vertical-align: top;"
"}"
// Set first column width
".list-view th:first-child, .list-view td:first-child {"
" width: 15%;"
"}"
".list-view.highlight { "
" padding-left: inherit; "
"}"
// Set first column width for inner tables
".tabular-view th:first-child, .tabular-view td:first-child { "
" width: 20%; "
"}"
// Makes titles bg stand up
".tabular-view th.strong { "
" background-color: #eee; "
"}"
// Give some visual appearance to those ugly nested tables
".tabular-view th, .tabular-view td { "
" border: solid 1px #eee;"
"}"
);
}

return myStyle;
}

Expand Down
27 changes: 22 additions & 5 deletions src/core/qgsapplication.h
Expand Up @@ -126,6 +126,20 @@ class CORE_EXPORT QgsApplication : public QApplication

public:

/**
* The StyleSheetType enum represents the stylesheet type that
* a widget supports.
*
* Is is used by widgets that display HTML content to retrieve
* the standard QGIS stylesheet, maintained according to QGIS
* visual guidelines.
*/
enum StyleSheetType
{
Qt, //! StyleSheet for Qt GUI widgets (based on QLabel or QTextBrowser), supports basic CSS and Qt extensions
WebBrowser, //! StyleSheet for embedded browsers (QtWebKit), supports full standard CSS
};

static const char *QGIS_ORGANIZATION_NAME;
static const char *QGIS_ORGANIZATION_DOMAIN;
static const char *QGIS_APPLICATION_NAME;
Expand Down Expand Up @@ -463,17 +477,20 @@ class CORE_EXPORT QgsApplication : public QApplication
static endian_t endian();

/**
* Returns a standard css style sheet for reports.
* Returns a css style sheet for reports, the \a styleSheetType argument
* determines what type of stylesheet is supported by the widget.
*
* Typically you will use this method by doing:
* QString myStyle = QgsApplication::reportStyleSheet();
* textBrowserReport->document()->setDefaultStyleSheet(myStyle);
* if you are using a QgsWebView you will need to manually inject
* the CSS into a <head><script> tag instead.
*
* \returns QString containing the CSS 2.1 compliant stylesheet.
* \note you can use the special Qt extensions too, for example
* the gradient fills for backgrounds.
* \returns the stylesheet CSS rules.
* \note if styleSheetType equals StyleSheetType::Qt you can use the special Qt extensions too,
* for example the gradient fills for backgrounds.
*/
static QString reportStyleSheet();
static QString reportStyleSheet( const StyleSheetType &styleSheetType = StyleSheetType::Qt );

/**
* Convenience function to get a summary of the paths used in this
Expand Down

0 comments on commit 30eea3d

Please sign in to comment.