Skip to content

Commit

Permalink
HighDPI: Pointer crosshair color
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanuan committed Jul 23, 2020
1 parent 12d9fb4 commit a3af118
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 16 deletions.
20 changes: 16 additions & 4 deletions src/Gui/BitmapFactory.cpp
Expand Up @@ -23,6 +23,7 @@

#include "PreCompiled.h"
#ifndef _PreComp_
# include <QString>
# include <QApplication>
# include <QBitmap>
# include <QDir>
Expand All @@ -36,7 +37,6 @@
# include <QStyleOption>
# include <sstream>
#endif

#if defined (FC_OS_WIN32) && QT_VERSION < 0x050000
#define QTWEBKIT
#endif
Expand Down Expand Up @@ -296,7 +296,8 @@ QPixmap BitmapFactoryInst::pixmap(const char* name) const
return QPixmap(px);
}

QPixmap BitmapFactoryInst::pixmapFromSvg(const char* name, const QSize& size) const
QPixmap BitmapFactoryInst::pixmapFromSvg(const char* name, const QSize& size,
const std::map<unsigned long, unsigned long> colorMapping) const
{
// If an absolute path is given
QPixmap icon;
Expand Down Expand Up @@ -325,15 +326,26 @@ QPixmap BitmapFactoryInst::pixmapFromSvg(const char* name, const QSize& size) co
QFile file(iconPath);
if (file.open(QFile::ReadOnly | QFile::Text)) {
QByteArray content = file.readAll();
icon = pixmapFromSvg(content, size);
icon = pixmapFromSvg(content, size, colorMapping);
}
}

return icon;
}

QPixmap BitmapFactoryInst::pixmapFromSvg(const QByteArray& contents, const QSize& size) const
QPixmap BitmapFactoryInst::pixmapFromSvg(const QByteArray& originalContents, const QSize& size,
const std::map<unsigned long, unsigned long> colorMapping) const
{
QString stringContents = QString::fromUtf8(originalContents);
for ( const auto &colorToColor : colorMapping ) {
ulong fromColor = colorToColor.first;
ulong toColor = colorToColor.second;
QString fromColorString = QString::fromLatin1(":#%1;").arg(fromColor, (int)6, (int)16, QChar::fromLatin1('0'));
QString toColorString = QString::fromLatin1(":#%1;").arg(toColor, (int)6, (int)16, QChar::fromLatin1('0'));
stringContents = stringContents.replace(fromColorString, toColorString);
}
QByteArray contents = stringContents.toLatin1();

#ifdef QTWEBKIT
// There is a crash when using the Webkit engine in debug mode
// for a couple of SVG files. Thus, use the qsvg plugin.
Expand Down
6 changes: 4 additions & 2 deletions src/Gui/BitmapFactory.h
Expand Up @@ -78,12 +78,14 @@ class GuiExport BitmapFactoryInst : public Base::Factory
/** Retrieves a pixmap by name and size created by an
* scalable vector graphics (SVG).
*/
QPixmap pixmapFromSvg(const char* name, const QSize& size) const;
QPixmap pixmapFromSvg(const char* name, const QSize& size,
const std::map<unsigned long, unsigned long> colorMapping = std::map<unsigned long, unsigned long>()) const;
/** This method is provided for convenience and does the same
* as the method above except that it creates the pixmap from
* a byte array.
*/
QPixmap pixmapFromSvg(const QByteArray& contents, const QSize& size) const;
QPixmap pixmapFromSvg(const QByteArray& contents, const QSize& size,
const std::map<unsigned long, unsigned long> colorMapping = std::map<unsigned long, unsigned long>()) const;
/** Returns the names of all registered pixmaps.
* To get the appropriate pixmaps call pixmap() for each name.
*/
Expand Down
15 changes: 12 additions & 3 deletions src/Mod/Sketcher/Gui/CommandCreateGeo.cpp
Expand Up @@ -234,14 +234,20 @@ static const char cursor_crosshair_color_fmt[] = "+ c #%06lX";
char cursor_crosshair_color[12];

void DrawSketchHandler::setCrosshairColor()
{
unsigned long color = getCrosshairColor();
sprintf(cursor_crosshair_color, cursor_crosshair_color_fmt, color);
}

unsigned long DrawSketchHandler::getCrosshairColor()
{
unsigned long color = 0xFFFFFFFF; // white
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath
("User parameter:BaseApp/Preferences/View");
color = hGrp->GetUnsigned("CursorCrosshairColor", color);
// from rgba to rgb
color = (color >> 8) & 0xFFFFFF;
sprintf(cursor_crosshair_color, cursor_crosshair_color_fmt, color);
return color;
}

/* XPM */
Expand Down Expand Up @@ -6858,9 +6864,12 @@ class DrawSketchHandlerSlot: public DrawSketchHandler

virtual void activated(ViewProviderSketch *)
{
setCrosshairColor(); // TODO handle when rendering SVG
QString cursorName = QString::fromLatin1("Sketcher_Pointer_Slot");
setSvgCursor(cursorName,7,7);
const unsigned long defaultCrosshairColor = 0xFFFFFF;
unsigned long color = getCrosshairColor();
auto colorMapping = std::map<unsigned long, unsigned long>();
colorMapping[defaultCrosshairColor] = color;
setSvgCursor(cursorName,7,7, colorMapping);
}

virtual void mouseMove(Base::Vector2d onSketchPos)
Expand Down
5 changes: 3 additions & 2 deletions src/Mod/Sketcher/Gui/DrawSketchHandler.cpp
Expand Up @@ -92,7 +92,7 @@ int DrawSketchHandler::getHighestCurveIndex(void)
}


void DrawSketchHandler::setSvgCursor(const QString & cursorName, int x, int y)
void DrawSketchHandler::setSvgCursor(const QString & cursorName, int x, int y, std::map<unsigned long, unsigned long> colorMapping)
{
qreal pRatio = 1;
#if QT_VERSION >= 0x050000
Expand All @@ -107,7 +107,7 @@ void DrawSketchHandler::setSvgCursor(const QString & cursorName, int x, int y)
qreal hotY = y * pRatio;
qreal cursorSize = defaultCursorSize * pRatio;

QPixmap pointer = Gui::BitmapFactory().pixmapFromSvg(cursorName.toStdString().c_str(), QSize(cursorSize, cursorSize));
QPixmap pointer = Gui::BitmapFactory().pixmapFromSvg(cursorName.toStdString().c_str(), QSize(cursorSize, cursorSize), colorMapping);
#if QT_VERSION >= 0x050000
pointer.setDevicePixelRatio(pRatio);
#endif
Expand All @@ -126,6 +126,7 @@ void DrawSketchHandler::setCursor(const QPixmap &p,int x,int y, bool autoScale)

QCursor cursor;
if (autoScale) {
// TODO remove after all cursors are SVG-based
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
QPixmap p1(p);
qreal pRatio = viewer->devicePixelRatio();
Expand Down
4 changes: 3 additions & 1 deletion src/Mod/Sketcher/Gui/DrawSketchHandler.h
Expand Up @@ -101,12 +101,14 @@ class SketcherGuiExport DrawSketchHandler
* \param autoScale - set this to false if pixmap already scaled for HiDPI
**/
void setCursor( const QPixmap &pixmap, int x,int y, bool autoScale=true );
void setSvgCursor( const QString &svgName, int x, int y);
void setSvgCursor( const QString &svgName, int x, int y,
std::map<unsigned long, unsigned long> colorMapping = std::map<unsigned long, unsigned long>());
void addCursorTail(std::vector<QPixmap> &pixmaps);
void unsetCursor(void);
void applyCursor(void);
void applyCursor(QCursor &newCursor);
void setCrosshairColor();
unsigned long getCrosshairColor();

/**
* Returns contraints icons scaled to width.
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a3af118

Please sign in to comment.