summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTomaž Vajngerl <tomaz.vajngerl@collabora.co.uk>2015-05-26 18:49:29 +0900
committerTomaž Vajngerl <tomaz.vajngerl@collabora.co.uk>2015-08-27 08:30:49 +0900
commit48d2dca48d75fef67e3caa61d80f074fdbeb9984 (patch)
treef74d55c1ea4e86af468268a1cfaf02426f0cdc66
parenta0ab7c31a254285856f6520d069570647c56af22 (diff)
Show previews of style presets in sidebar
Change-Id: I0e3d6f00a79bc33e1db1329df217e7e867dfd511
-rw-r--r--include/sfx2/StylePreviewRenderer.hxx14
-rw-r--r--include/svx/CommonStylePreviewRenderer.hxx2
-rw-r--r--svx/source/styles/CommonStylePreviewRenderer.cxx18
-rw-r--r--sw/source/uibase/sidebar/StylePresetsPanel.cxx120
-rw-r--r--sw/source/uibase/sidebar/StylePresetsPanel.hxx10
-rw-r--r--sw/uiconfig/swriter/ui/sidebarstylepresets.ui11
6 files changed, 159 insertions, 16 deletions
diff --git a/include/sfx2/StylePreviewRenderer.hxx b/include/sfx2/StylePreviewRenderer.hxx
index bc77e20792d1..1fc980409e99 100644
--- a/include/sfx2/StylePreviewRenderer.hxx
+++ b/include/sfx2/StylePreviewRenderer.hxx
@@ -26,8 +26,14 @@ protected:
OutputDevice& mrOutputDev;
SfxStyleSheetBase* mpStyle;
long mnMaxHeight;
+ OUString msRenderText;
public:
+ enum class RenderAlign
+ {
+ TOP, CENTER, BOTTOM
+ };
+
StylePreviewRenderer(const SfxObjectShell& rShell,
OutputDevice& rOutputDev,
SfxStyleSheetBase* pStyle,
@@ -36,14 +42,20 @@ public:
, mrOutputDev(rOutputDev)
, mpStyle(pStyle)
, mnMaxHeight(nMaxHeight)
+ , msRenderText()
{}
virtual ~StylePreviewRenderer()
{}
+ void setRenderText(OUString& rRenderText)
+ {
+ msRenderText = rRenderText;
+ }
+
virtual bool recalculate() = 0;
virtual Size getRenderSize() = 0;
- virtual bool render(const Rectangle& aRectangle) = 0;
+ virtual bool render(const Rectangle& aRectangle, RenderAlign eRenderAlign = RenderAlign::CENTER) = 0;
};
} // end namespace sfx2
diff --git a/include/svx/CommonStylePreviewRenderer.hxx b/include/svx/CommonStylePreviewRenderer.hxx
index 1e7d54e0c6ae..49717b574854 100644
--- a/include/svx/CommonStylePreviewRenderer.hxx
+++ b/include/svx/CommonStylePreviewRenderer.hxx
@@ -35,7 +35,7 @@ public:
virtual bool recalculate() SAL_OVERRIDE;
virtual Size getRenderSize() SAL_OVERRIDE;
- virtual bool render(const Rectangle& aRectangle) SAL_OVERRIDE;
+ virtual bool render(const Rectangle& aRectangle, RenderAlign eRenderAlign = RenderAlign::CENTER) SAL_OVERRIDE;
};
} // end namespace svx
diff --git a/svx/source/styles/CommonStylePreviewRenderer.cxx b/svx/source/styles/CommonStylePreviewRenderer.cxx
index ab1271ac8fe0..2d9ac3f5dd10 100644
--- a/svx/source/styles/CommonStylePreviewRenderer.cxx
+++ b/svx/source/styles/CommonStylePreviewRenderer.cxx
@@ -174,8 +174,10 @@ Size CommonStylePreviewRenderer::getRenderSize()
return maPixelSize;
}
-bool CommonStylePreviewRenderer::render(const Rectangle& aRectangle)
+bool CommonStylePreviewRenderer::render(const Rectangle& aRectangle, RenderAlign eRenderAlign)
{
+ const OUString& rText = msRenderText.isEmpty() ? maStyleName : msRenderText;
+
// setup the device & draw
vcl::Font aOldFont(mrOutputDev.GetFont());
Color aOldColor(mrOutputDev.GetTextColor());
@@ -192,10 +194,18 @@ bool CommonStylePreviewRenderer::render(const Rectangle& aRectangle)
mrOutputDev.SetTextColor(maFontColor);
Point aFontDrawPosition = aRectangle.TopLeft();
- if (aRectangle.GetHeight() > maPixelSize.Height())
- aFontDrawPosition.Y() += ( aRectangle.GetHeight() - maPixelSize.Height() ) / 2;
+ if (eRenderAlign == RenderAlign::CENTER)
+ {
+ if (aRectangle.GetHeight() > maPixelSize.Height())
+ aFontDrawPosition.Y() += (aRectangle.GetHeight() - maPixelSize.Height()) / 2;
+ }
+ else if (eRenderAlign == RenderAlign::BOTTOM)
+ {
+ if (aRectangle.GetHeight() > maPixelSize.Height())
+ aFontDrawPosition.Y() += aRectangle.GetHeight() - maPixelSize.Height();
+ }
- mrOutputDev.DrawText(aFontDrawPosition, maStyleName);
+ mrOutputDev.DrawText(aFontDrawPosition, rText);
mrOutputDev.SetFillColor(aOldFillColor);
mrOutputDev.SetTextColor(aOldColor);
diff --git a/sw/source/uibase/sidebar/StylePresetsPanel.cxx b/sw/source/uibase/sidebar/StylePresetsPanel.cxx
index 7a4dbfc2933a..957f0508b008 100644
--- a/sw/source/uibase/sidebar/StylePresetsPanel.cxx
+++ b/sw/source/uibase/sidebar/StylePresetsPanel.cxx
@@ -63,14 +63,18 @@ StylePresetsPanel::StylePresetsPanel(vcl::Window* pParent,
: PanelLayout(pParent, "StylePresetsPanel", "modules/swriter/ui/sidebarstylepresets.ui", rxFrame)
, mpBindings(pBindings)
{
+ get(mpValueSet, "valueset");
- get(mpListBox, "listbox");
+ mpValueSet->SetColCount(2);
- mpListBox->SetDoubleClickHdl(LINK(this, StylePresetsPanel, DoubleClickHdl));
+ mpValueSet->SetDoubleClickHdl(LINK(this, StylePresetsPanel, DoubleClickHdl));
+ RefreshList();
+}
+void StylePresetsPanel::RefreshList()
+{
SfxDocumentTemplates aTemplates;
-
sal_uInt16 nCount = aTemplates.GetRegionCount();
for (sal_uInt16 i = 0; i < nCount; ++i)
{
@@ -81,14 +85,112 @@ StylePresetsPanel::StylePresetsPanel(vcl::Window* pParent,
{
OUString aName = aTemplates.GetName(i,j);
OUString aURL = aTemplates.GetPath(i,j);
- sal_Int32 nIndex = mpListBox->InsertEntry(aName);
+ BitmapEx aPreview = CreatePreview(aURL, aName);
+ mpValueSet->InsertItem(j, Image(aPreview), aName);
maTemplateEntries.push_back(std::unique_ptr<TemplateEntry>(new TemplateEntry(aName, aURL)));
- mpListBox->SetEntryData(nIndex, maTemplateEntries.back().get());
+ mpValueSet->SetItemData(j, maTemplateEntries.back().get());
}
}
}
}
+BitmapEx StylePresetsPanel::CreatePreview(OUString& aUrl, OUString& aName)
+{
+ SfxMedium aMedium(aUrl, STREAM_STD_READWRITE);
+ SfxObjectShell* pObjectShell = SfxObjectShell::Current();
+ SfxObjectShellLock xTemplDoc = pObjectShell->CreateObjectByFactoryName(pObjectShell->GetFactory().GetFactoryName(), SfxObjectCreateMode::ORGANIZER);
+ xTemplDoc->DoInitNew(0);
+ if (xTemplDoc->LoadFrom(aMedium))
+ {
+ return GenerateStylePreview(*xTemplDoc, aName);
+ }
+ return BitmapEx();
+}
+
+void renderPreview(sfx2::StyleManager* pStyleManager, OutputDevice& aOutputDevice,
+ OUString sName, sal_Int32 nHeight, Rectangle& aRect)
+{
+ sfx2::StylePreviewRenderer* pStylePreviewRenderer;
+
+ SfxStyleSheetBase* pStyleSheet = pStyleManager->Search(sName, SFX_STYLE_FAMILY_PARA);
+
+ if (pStyleSheet)
+ {
+ pStylePreviewRenderer = pStyleManager->CreateStylePreviewRenderer(aOutputDevice, pStyleSheet, nHeight);
+ pStylePreviewRenderer->recalculate();
+ pStylePreviewRenderer->render(aRect, sfx2::StylePreviewRenderer::RenderAlign::TOP);
+ }
+}
+
+BitmapEx StylePresetsPanel::GenerateStylePreview(SfxObjectShell& rSource, OUString& aName)
+{
+ sfx2::StyleManager* pStyleManager = rSource.GetStyleManager();
+
+ ScopedVclPtrInstance<VirtualDevice> pVirtualDev(*Application::GetDefaultDevice());
+
+ sal_Int32 nScalingFactor = pVirtualDev->GetDPIScaleFactor();
+
+ sal_Int32 nMargin = 6 * nScalingFactor;
+
+ sal_Int32 nPreviewWidth = 144 * nScalingFactor;
+
+ sal_Int32 nNameHeight = 16 * nScalingFactor;
+ sal_Int32 nTitleHeight = 32 * nScalingFactor;
+ sal_Int32 nHeadingHeight = 24 * nScalingFactor;
+ sal_Int32 nTextBodyHeight = 16 * nScalingFactor;
+ sal_Int32 nBottomMargin = 2 * nScalingFactor;
+
+ sal_Int32 nNameFontSize = 12 * nScalingFactor;
+
+ sal_Int32 nPreviewHeight = nNameHeight + nTitleHeight + nHeadingHeight + nTextBodyHeight + nBottomMargin;
+
+ Size aSize(nPreviewWidth, nPreviewHeight);
+
+ pVirtualDev->SetOutputSizePixel(aSize);
+
+ pVirtualDev->SetLineColor(COL_LIGHTGRAY);
+ pVirtualDev->SetFillColor();
+
+ long y = 0;
+ {
+ pVirtualDev->SetFillColor(COL_LIGHTGRAY);
+ Rectangle aNameRect(0, y, nPreviewWidth, nNameHeight);
+ pVirtualDev->DrawRect(aNameRect);
+
+ vcl::Font aFont;
+ aFont.SetSize(Size(0, nNameFontSize));
+
+ pVirtualDev->SetFont(aFont);
+
+ Size aTextSize(pVirtualDev->GetTextWidth(aName), pVirtualDev->GetTextHeight());
+
+ Point aPoint((aNameRect.GetWidth() / 2.0) - (aTextSize.Width() / 2.0),
+ y + (aNameRect.GetHeight() / 2.0) - (aTextSize.Height() / 2.0));
+
+ pVirtualDev->DrawText(aPoint, aName);
+
+ y += nNameHeight;
+ }
+
+ {
+ Rectangle aRenderRect(Point(nMargin, y), aSize);
+ renderPreview(pStyleManager, *pVirtualDev.get(), "Title", nTitleHeight, aRenderRect);
+ y += nTitleHeight;
+ }
+
+ {
+ Rectangle aRenderRect(Point(nMargin, y), aSize);
+ renderPreview(pStyleManager, *pVirtualDev.get(), "Heading 1", nHeadingHeight, aRenderRect);
+ y += nHeadingHeight;
+ }
+ {
+ Rectangle aRenderRect(Point(nMargin, y), aSize);
+ renderPreview(pStyleManager, *pVirtualDev.get(), "Text Body", nTextBodyHeight, aRenderRect);
+ }
+
+ return pVirtualDev->GetBitmapEx(Point(), aSize);
+}
+
StylePresetsPanel::~StylePresetsPanel()
{
disposeOnce();
@@ -96,15 +198,15 @@ StylePresetsPanel::~StylePresetsPanel()
void StylePresetsPanel::dispose()
{
- mpListBox.disposeAndClear();
+ mpValueSet.disposeAndClear();
PanelLayout::dispose();
}
IMPL_LINK_NOARG(StylePresetsPanel, DoubleClickHdl)
{
- sal_Int32 nIndex = mpListBox->GetSelectEntryPos();
- TemplateEntry* pEntry = static_cast<TemplateEntry*>(mpListBox->GetEntryData(nIndex));
+ sal_Int32 nItemId = mpValueSet->GetSelectItemId();
+ TemplateEntry* pEntry = static_cast<TemplateEntry*>(mpValueSet->GetItemData(nItemId));
SwDocShell* pDocSh = static_cast<SwDocShell*>(SfxObjectShell::Current());
if (pDocSh)
@@ -115,7 +217,7 @@ IMPL_LINK_NOARG(StylePresetsPanel, DoubleClickHdl)
pDocSh->LoadStylesFromFile(pEntry->maURL, aOption, false);
}
- return 1;
+ return 0;
}
void StylePresetsPanel::NotifyItemUpdate(const sal_uInt16 /*nSId*/,
diff --git a/sw/source/uibase/sidebar/StylePresetsPanel.hxx b/sw/source/uibase/sidebar/StylePresetsPanel.hxx
index dd2a7c7be8b6..fd51cf416e20 100644
--- a/sw/source/uibase/sidebar/StylePresetsPanel.hxx
+++ b/sw/source/uibase/sidebar/StylePresetsPanel.hxx
@@ -19,6 +19,8 @@
#include <sfx2/sidebar/ControllerItem.hxx>
+#include <sfx2/objsh.hxx>
+
#include <svx/pageitem.hxx>
#include <svx/rulritem.hxx>
#include <editeng/sizeitem.hxx>
@@ -32,6 +34,8 @@
#include <svl/intitem.hxx>
#include <svl/lstner.hxx>
+#include <svtools/valueset.hxx>
+
#include <svx/fntctrl.hxx>
#include "docstyle.hxx"
@@ -64,6 +68,10 @@ private:
OUString maURL;
};
+ void RefreshList();
+ BitmapEx CreatePreview(OUString& aUrl, OUString& aName);
+ BitmapEx GenerateStylePreview(SfxObjectShell& rSource, OUString& aName);
+
StylePresetsPanel(vcl::Window* pParent,
const css::uno::Reference<css::frame::XFrame>& rxFrame,
SfxBindings* pBindings);
@@ -73,7 +81,7 @@ private:
SfxBindings* mpBindings;
- VclPtr<ListBox> mpListBox;
+ VclPtr<ValueSet> mpValueSet;
std::vector<std::unique_ptr<TemplateEntry>> maTemplateEntries;
diff --git a/sw/uiconfig/swriter/ui/sidebarstylepresets.ui b/sw/uiconfig/swriter/ui/sidebarstylepresets.ui
index 071e9e57ec29..fc64564fea38 100644
--- a/sw/uiconfig/swriter/ui/sidebarstylepresets.ui
+++ b/sw/uiconfig/swriter/ui/sidebarstylepresets.ui
@@ -2,6 +2,7 @@
<!-- Generated with glade 3.18.3 -->
<interface>
<requires lib="gtk+" version="3.0"/>
+ <requires lib="LibreOffice" version="1.0"/>
<object class="GtkGrid" id="StylePresetsPanel">
<property name="visible">True</property>
<property name="can_focus">False</property>
@@ -31,6 +32,16 @@
<property name="top_attach">0</property>
</packing>
</child>
+ <child>
+ <object class="svtlo-ValueSet" id="valueset">
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ </object>
+ <packing>
+ <property name="left_attach">0</property>
+ <property name="top_attach">1</property>
+ </packing>
+ </child>
</object>
</child>
</object>