Skip to content

Commit af7af6c

Browse files
committed
[FEATURE] Add option to use colour preview modes (grayscale, colour blindess) in map canvas (fix #9886)
1 parent 9551349 commit af7af6c

6 files changed

Lines changed: 241 additions & 0 deletions

File tree

‎python/gui/qgsmapcanvas.sip‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,36 @@ class QgsMapCanvas : QGraphicsView
263263

264264
//! returns last position of mouse cursor
265265
QPoint mouseLastXY();
266+
267+
/** Enables a preview mode for the map canvas
268+
* @param previewEnabled set to true to enable a preview mode
269+
* @see setPreviewMode
270+
* @note added in 2.3 */
271+
void setPreviewModeEnabled( bool previewEnabled );
272+
273+
/** Returns whether a preview mode is enabled for the map canvas
274+
* @returns true if a preview mode is currently enabled
275+
* @see setPreviewModeEnabled
276+
* @see previewMode
277+
* @note added in 2.3 */
278+
bool previewModeEnabled() const;
279+
280+
/** Sets a preview mode for the map canvas. This setting only has an effect if
281+
* previewModeEnabled is true.
282+
* @param mode preview mode for the canvas
283+
* @see previewMode
284+
* @see setPreviewModeEnabled
285+
* @see previewModeEnabled
286+
* @note added in 2.3 */
287+
void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
288+
289+
/** Returns the current preview mode for the map canvas. This setting only has an effect if
290+
* previewModeEnabled is true.
291+
* @returns preview mode for map canvas
292+
* @see setPreviewMode
293+
* @see previewModeEnabled
294+
* @note added in 2.3 */
295+
QgsPreviewEffect::PreviewMode previewMode() const;
266296

267297
public slots:
268298

‎src/app/qgisapp.cpp‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,16 @@ void QgisApp::createActionGroups()
12871287
mMapToolGroup->addAction( mActionMoveLabel );
12881288
mMapToolGroup->addAction( mActionRotateLabel );
12891289
mMapToolGroup->addAction( mActionChangeLabelProperties );
1290+
1291+
//
1292+
// Preview Modes Group
1293+
QActionGroup* mPreviewGroup = new QActionGroup( this );
1294+
mPreviewGroup->setExclusive( true );
1295+
mActionPreviewModeOff->setActionGroup( mPreviewGroup );
1296+
mActionPreviewModeGrayscale->setActionGroup( mPreviewGroup );
1297+
mActionPreviewModeMono->setActionGroup( mPreviewGroup );
1298+
mActionPreviewProtanope->setActionGroup( mPreviewGroup );
1299+
mActionPreviewDeuteranope->setActionGroup( mPreviewGroup );
12901300
}
12911301

12921302
void QgisApp::setAppStyleSheet( const QString& stylesheet )
@@ -1960,6 +1970,13 @@ void QgisApp::setupConnections()
19601970
connect( this, SIGNAL( projectRead() ),
19611971
this, SLOT( fileOpenedOKAfterLaunch() ) );
19621972

1973+
// connect preview modes actions
1974+
connect( mActionPreviewModeOff, SIGNAL( triggered() ), this, SLOT( disablePreviewMode() ) );
1975+
connect( mActionPreviewModeGrayscale, SIGNAL( triggered() ), this, SLOT( activateGrayscalePreview() ) );
1976+
connect( mActionPreviewModeMono, SIGNAL( triggered() ), this, SLOT( activateMonoPreview() ) );
1977+
connect( mActionPreviewProtanope, SIGNAL( triggered() ), this, SLOT( activateProtanopePreview() ) );
1978+
connect( mActionPreviewDeuteranope, SIGNAL( triggered() ), this, SLOT( activateDeuteranopePreview() ) );
1979+
19631980
// handle deprecated labels in project for QGIS 2.0
19641981
connect( this, SIGNAL( newProject() ),
19651982
this, SLOT( checkForDeprecatedLabelsInProject() ) );
@@ -3959,6 +3976,35 @@ void QgisApp::deleteComposerManager()
39593976
mComposerManager = 0;
39603977
}
39613978

3979+
void QgisApp::disablePreviewMode()
3980+
{
3981+
mMapCanvas->setPreviewModeEnabled( false );
3982+
}
3983+
3984+
void QgisApp::activateGrayscalePreview()
3985+
{
3986+
mMapCanvas->setPreviewModeEnabled( true );
3987+
mMapCanvas->setPreviewMode( QgsPreviewEffect::PreviewGrayscale );
3988+
}
3989+
3990+
void QgisApp::activateMonoPreview()
3991+
{
3992+
mMapCanvas->setPreviewModeEnabled( true );
3993+
mMapCanvas->setPreviewMode( QgsPreviewEffect::PreviewMono );
3994+
}
3995+
3996+
void QgisApp::activateProtanopePreview()
3997+
{
3998+
mMapCanvas->setPreviewModeEnabled( true );
3999+
mMapCanvas->setPreviewMode( QgsPreviewEffect::PreviewProtanope );
4000+
}
4001+
4002+
void QgisApp::activateDeuteranopePreview()
4003+
{
4004+
mMapCanvas->setPreviewModeEnabled( true );
4005+
mMapCanvas->setPreviewMode( QgsPreviewEffect::PreviewDeuteranope );
4006+
}
4007+
39624008
void QgisApp::saveMapAsImage()
39634009
{
39644010
QPair< QString, QString> myFileNameAndFilter = QgisGui::getSaveAsImageName( this, tr( "Choose a file name to save the map image as" ) );

‎src/app/qgisapp.h‎

100644100755
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,22 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
11891189
//! Deletes the active QgsComposerManager instance
11901190
void deleteComposerManager();
11911191

1192+
/** Disable any preview modes shown on the map canvas
1193+
* @note added in 2.3 */
1194+
void disablePreviewMode();
1195+
/** Enable a grayscale preview mode on the map canvas
1196+
* @note added in 2.3 */
1197+
void activateGrayscalePreview();
1198+
/** Enable a monochrome preview mode on the map canvas
1199+
* @note added in 2.3 */
1200+
void activateMonoPreview();
1201+
/** Enable a color blindness (protanope) preview mode on the map canvas
1202+
* @note added in 2.3 */
1203+
void activateProtanopePreview();
1204+
/** Enable a color blindness (deuteranope) preview mode on the map canvas
1205+
* @note added in 2.3 */
1206+
void activateDeuteranopePreview();
1207+
11921208
signals:
11931209
/** emitted when a key is pressed and we want non widget sublasses to be able
11941210
to pick up on this (e.g. maplayer) */
@@ -1343,6 +1359,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
13431359

13441360
// action groups ----------------------------------
13451361
QActionGroup *mMapToolGroup;
1362+
QActionGroup *mPreviewGroup;
13461363

13471364
// menus ------------------------------------------
13481365

‎src/gui/qgsmapcanvas.cpp‎

100644100755
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ QgsMapCanvas::QgsMapCanvas( QWidget * parent, const char *name )
163163
, mUseParallelRendering( false )
164164
, mDrawRenderingStats( false )
165165
, mCache( 0 )
166+
, mPreviewEffect( 0 )
166167
{
167168
setObjectName( name );
168169
mScene = new QGraphicsScene();
@@ -227,6 +228,9 @@ QgsMapCanvas::QgsMapCanvas( QWidget * parent, const char *name )
227228
viewport()->setAttribute( Qt::WA_AcceptTouchEvents );
228229
#endif
229230

231+
mPreviewEffect = new QgsPreviewEffect( this );
232+
viewport()->setGraphicsEffect( mPreviewEffect );
233+
230234
refresh();
231235

232236
} // QgsMapCanvas ctor
@@ -1606,6 +1610,46 @@ QPoint QgsMapCanvas::mouseLastXY()
16061610
return mCanvasProperties->mouseLastXY;
16071611
}
16081612

1613+
void QgsMapCanvas::setPreviewModeEnabled( bool previewEnabled )
1614+
{
1615+
if ( !mPreviewEffect )
1616+
{
1617+
return;
1618+
}
1619+
1620+
mPreviewEffect->setEnabled( previewEnabled );
1621+
}
1622+
1623+
bool QgsMapCanvas::previewModeEnabled() const
1624+
{
1625+
if ( !mPreviewEffect )
1626+
{
1627+
return false;
1628+
}
1629+
1630+
return mPreviewEffect->isEnabled();
1631+
}
1632+
1633+
void QgsMapCanvas::setPreviewMode( QgsPreviewEffect::PreviewMode mode )
1634+
{
1635+
if ( !mPreviewEffect )
1636+
{
1637+
return;
1638+
}
1639+
1640+
mPreviewEffect->setMode( mode );
1641+
}
1642+
1643+
QgsPreviewEffect::PreviewMode QgsMapCanvas::previewMode() const
1644+
{
1645+
if ( !mPreviewEffect )
1646+
{
1647+
return QgsPreviewEffect::PreviewGrayscale;
1648+
}
1649+
1650+
return mPreviewEffect->mode();
1651+
}
1652+
16091653
void QgsMapCanvas::readProject( const QDomDocument & doc )
16101654
{
16111655
QDomNodeList nodes = doc.elementsByTagName( "mapcanvas" );

‎src/gui/qgsmapcanvas.h‎

100644100755
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <QtCore>
3434

3535
#include "qgsmapsettings.h" // TEMPORARY
36+
#include "qgsprevieweffect.h" //for QgsPreviewEffect::PreviewMode
3637

3738
#ifdef HAVE_TOUCH
3839
#include <QGestureEvent>
@@ -330,6 +331,36 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
330331
//! returns last position of mouse cursor
331332
QPoint mouseLastXY();
332333

334+
/** Enables a preview mode for the map canvas
335+
* @param previewEnabled set to true to enable a preview mode
336+
* @see setPreviewMode
337+
* @note added in 2.3 */
338+
void setPreviewModeEnabled( bool previewEnabled );
339+
340+
/** Returns whether a preview mode is enabled for the map canvas
341+
* @returns true if a preview mode is currently enabled
342+
* @see setPreviewModeEnabled
343+
* @see previewMode
344+
* @note added in 2.3 */
345+
bool previewModeEnabled() const;
346+
347+
/** Sets a preview mode for the map canvas. This setting only has an effect if
348+
* previewModeEnabled is true.
349+
* @param mode preview mode for the canvas
350+
* @see previewMode
351+
* @see setPreviewModeEnabled
352+
* @see previewModeEnabled
353+
* @note added in 2.3 */
354+
void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
355+
356+
/** Returns the current preview mode for the map canvas. This setting only has an effect if
357+
* previewModeEnabled is true.
358+
* @returns preview mode for map canvas
359+
* @see setPreviewMode
360+
* @see previewModeEnabled
361+
* @note added in 2.3 */
362+
QgsPreviewEffect::PreviewMode previewMode() const;
363+
333364
public slots:
334365

335366
/**Repaints the canvas map*/
@@ -590,6 +621,9 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
590621

591622

592623
QTimer *mResizeTimer;
624+
625+
QgsPreviewEffect* mPreviewEffect;
626+
593627
}; // class QgsMapCanvas
594628

595629

‎src/ui/qgisapp.ui‎

100644100755
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@
8888
<addaction name="mActionDecorationNorthArrow"/>
8989
<addaction name="mActionDecorationCopyright"/>
9090
</widget>
91+
<widget class="QMenu" name="menuPreview_Mode">
92+
<property name="title">
93+
<string>Preview Mode</string>
94+
</property>
95+
<addaction name="mActionPreviewModeOff"/>
96+
<addaction name="mActionPreviewModeGrayscale"/>
97+
<addaction name="mActionPreviewModeMono"/>
98+
<addaction name="mActionPreviewProtanope"/>
99+
<addaction name="mActionPreviewDeuteranope"/>
100+
</widget>
91101
<addaction name="mActionPan"/>
92102
<addaction name="mActionPanToSelected"/>
93103
<addaction name="mActionZoomIn"/>
@@ -105,10 +115,12 @@
105115
<addaction name="mActionZoomActualSize"/>
106116
<addaction name="separator"/>
107117
<addaction name="menuDecorations"/>
118+
<addaction name="menuPreview_Mode"/>
108119
<addaction name="mActionMapTips"/>
109120
<addaction name="mActionNewBookmark"/>
110121
<addaction name="mActionShowBookmarks"/>
111122
<addaction name="mActionDraw"/>
123+
<addaction name="separator"/>
112124
</widget>
113125
<widget class="QMenu" name="mLayerMenu">
114126
<property name="title">
@@ -2168,6 +2180,64 @@ Acts on currently active editable layer</string>
21682180
<string>Save As Layer Definition File...</string>
21692181
</property>
21702182
</action>
2183+
<action name="mActionPreviewModeOff">
2184+
<property name="checkable">
2185+
<bool>true</bool>
2186+
</property>
2187+
<property name="checked">
2188+
<bool>true</bool>
2189+
</property>
2190+
<property name="text">
2191+
<string>Normal</string>
2192+
</property>
2193+
<property name="toolTip">
2194+
<string>Normal preview mode</string>
2195+
</property>
2196+
</action>
2197+
<action name="mActionPreviewModeGrayscale">
2198+
<property name="checkable">
2199+
<bool>true</bool>
2200+
</property>
2201+
<property name="text">
2202+
<string>Simulate photocopy (grayscale)</string>
2203+
</property>
2204+
<property name="toolTip">
2205+
<string>Simulate Photocopy (Grayscale)</string>
2206+
</property>
2207+
</action>
2208+
<action name="mActionPreviewModeMono">
2209+
<property name="checkable">
2210+
<bool>true</bool>
2211+
</property>
2212+
<property name="text">
2213+
<string>Simulate fax (mono)</string>
2214+
</property>
2215+
<property name="toolTip">
2216+
<string>Simulate Fax (Mono)</string>
2217+
</property>
2218+
</action>
2219+
<action name="mActionPreviewProtanope">
2220+
<property name="checkable">
2221+
<bool>true</bool>
2222+
</property>
2223+
<property name="text">
2224+
<string>Simulate color blindness (Protanope)</string>
2225+
</property>
2226+
<property name="toolTip">
2227+
<string>Simulate Color Blindness (Protanope)</string>
2228+
</property>
2229+
</action>
2230+
<action name="mActionPreviewDeuteranope">
2231+
<property name="checkable">
2232+
<bool>true</bool>
2233+
</property>
2234+
<property name="text">
2235+
<string>Simulate color blindness (Deuteranope)</string>
2236+
</property>
2237+
<property name="toolTip">
2238+
<string>Simulate Color Blindness (Deuteranope)</string>
2239+
</property>
2240+
</action>
21712241
</widget>
21722242
<resources>
21732243
<include location="../../images/images.qrc"/>

0 commit comments

Comments
 (0)