Skip to content

Commit

Permalink
* Add composite mode in create maps.
Browse files Browse the repository at this point in the history
* Add dash style to lines in create maps.
* Add join style to lines in create maps.
  • Loading branch information
elesdoar committed Jul 16, 2015
1 parent 4e448d3 commit b16e494
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 48 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#### Enhancements

* Remove connection controls in SQL Editor
* Add composite mode in create maps.
* Add dash style to lines in create maps.
* Add join style to lines in create maps.

<a name="0.1.7"></a>
### 0.1.7 (2015-07-10)
Expand Down
181 changes: 134 additions & 47 deletions dialogs/CreateViz.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"""
from PyQt4.QtCore import Qt, QFile, QFileInfo, pyqtSlot, qDebug, QPyNullVariant
from PyQt4.QtGui import QApplication, QAbstractItemView, QDialog, QListWidgetItem, QLabel, QPixmap, QPushButton, QSizePolicy
from PyQt4.QtGui import QClipboard
from PyQt4.QtGui import QClipboard, QPainter

from qgis.core import QGis, QgsMapLayerRegistry, QgsMapLayer, QgsPalLayerSettings
from qgis.gui import QgsMessageBar
Expand Down Expand Up @@ -75,6 +75,11 @@ def __init__(self, toolbar, parent=None):
self.ui.mapList.itemSelectionChanged.connect(self.validateButtons)
self.ui.cancelBT.clicked.connect(self.reject)
self.ui.saveBT.clicked.connect(self.createViz)
self.ui.cartoCssBT.clicked.connect(self.createCartoCss)

# TODO Implement functionality
self.ui.sqlBT.hide()
self.ui.cartoCssBT.hide()

layers = QgsMapLayerRegistry.instance().mapLayers()

Expand Down Expand Up @@ -108,6 +113,15 @@ def getSize(self, layer):

return size

def createCartoCss(self):
item = self.ui.availableList.currentItem()

if item is not None:
widget = self.ui.availableList.itemWidget(item)
layer = widget.layer
cartoCSS = self.convert2CartoCSS(layer)
qDebug('CartoCSS: {}'.format(cartoCSS))

def createViz(self):
self.ui.bar.clearWidgets()
self.ui.bar.pushMessage("Info", QApplication.translate('CartoDBPlugin', 'Creating Map'), level=QgsMessageBar.INFO)
Expand All @@ -131,7 +145,7 @@ def cbGetLayers(self, data):
item = self.ui.mapList.item(0)
widget = self.ui.mapList.itemWidget(item)
layer = widget.layer
cartoCSS = self.convert2cartoCSS(layer)
cartoCSS = self.convert2CartoCSS(layer)
cartoDBApi = CartoDBApi(self.currentUser, self.currentApiKey, self.currentMultiuser)
layer1 = data['layers'][1]
layer1['options']['tile_style'] = cartoCSS
Expand All @@ -144,7 +158,7 @@ def cbGetLayers(self, data):
widget = self.ui.mapList.itemWidget(item)
layer = widget.layer
qDebug('Agregando: {} en pos: {}'.format(layer.tableName(), i))
cartoCSS = self.convert2cartoCSS(layer)
cartoCSS = self.convert2CartoCSS(layer)
# cartoDBApi.fetchContent.connect(self.cbCreateViz)
newLayer = copy.deepcopy(layer1)
newLayer["options"]["table_name"] = layer.tableName()
Expand Down Expand Up @@ -177,7 +191,7 @@ def copyURL():
widget.layout().addWidget(button)
self.ui.bar.pushWidget(widget, QgsMessageBar.INFO)

def convert2cartoCSS(self, layer):
def convert2CartoCSS(self, layer):
renderer = layer.rendererV2()
cartoCSS = ''
labelCSS = ''
Expand All @@ -200,7 +214,7 @@ def convert2cartoCSS(self, layer):
# CSS for single symbols
if renderer.type() == 'singleSymbol':
symbol = renderer.symbol()
cartoCSS = self.simplePolygon(layer, symbol, '#' + layer.tableName())
cartoCSS = self.symbol2CartoCSS(layer, symbol, '#' + layer.tableName())
# CSS for categorized symbols
elif renderer.type() == 'categorizedSymbol':
# qDebug('Categorized: ' + renderer.classAttribute())
Expand All @@ -217,9 +231,9 @@ def convert2cartoCSS(self, layer):
# qDebug('Value {}'.format(value))
styleName = '#{}[{}={}]'.format(layer.tableName(), renderer.classAttribute(), value).decode('utf8')
cartoCSS = cartoCSS + \
self.simplePolygon(layer, symbol, styleName)
self.symbol2CartoCSS(layer, symbol, styleName)
else:
cartoCSS = self.simplePolygon(layer, symbol, '#' + layer.tableName()) + cartoCSS
cartoCSS = self.symbol2CartoCSS(layer, symbol, '#' + layer.tableName()) + cartoCSS
# CSS for graduated symbols
elif renderer.type() == 'graduatedSymbol':
# qDebug('Graduated')
Expand All @@ -237,56 +251,129 @@ def upperValue(ran):
))
'''
cartoCSS = cartoCSS + \
self.simplePolygon(layer, symbol, '#' + layer.tableName() + '[' + renderer.classAttribute() + '<=' + str(ran.upperValue()) + ']')
self.symbol2CartoCSS(layer, symbol, '#' + layer.tableName() + '[' + renderer.classAttribute() + '<=' + str(ran.upperValue()) + ']')

# qDebug('CartoCSS: ' + cartoCSS)
return '/** Styles designed from QGISCartoDB Plugin */\n\n' + cartoCSS + '\n' + labelCSS

def simplePolygon(self, layer, symbol, styleName):
def symbol2CartoCSS(self, layer, symbol, styleName):
cartoCSS = ''
layerOpacity = str(float((100.0 - layer.layerTransparency())/100.0))


blendMode = layer.featureBlendMode()
compositionMode = 'src-over'
if blendMode == QPainter.CompositionMode_Lighten:
compositionMode = 'lighten'
elif blendMode == QPainter.CompositionMode_Screen:
compositionMode = 'screen'
elif blendMode == QPainter.CompositionMode_ColorDodge:
compositionMode = 'color-dodge'
elif blendMode == QPainter.CompositionMode_Plus:
compositionMode = 'plus'
elif blendMode == QPainter.CompositionMode_Darken:
compositionMode = 'darken'
elif blendMode == QPainter.CompositionMode_Multiply:
compositionMode = 'multiply'
elif blendMode == QPainter.CompositionMode_ColorBurn:
compositionMode = 'color-burn'
elif blendMode == QPainter.CompositionMode_Overlay:
compositionMode = 'overlay'
elif blendMode == QPainter.CompositionMode_SoftLight:
compositionMode = 'soft-light'
elif blendMode == QPainter.CompositionMode_HardLight:
compositionMode = 'hard-light'
elif blendMode == QPainter.CompositionMode_Difference:
compositionMode = 'difference'
elif blendMode == QPainter.CompositionMode_Exclusion:
compositionMode = 'exclusion'

if symbol.symbolLayerCount() > 0:
lyr = symbol.symbolLayer(0)

# qDebug("Symbol Type: %s" % (lyr.layerType()))
filein = None
if layer.geometryType() == QGis.Point:
d = {
'layername': styleName,
'fillColor': lyr.fillColor().name(),
# 96 ppi = 3.7795275552 mm
'width': round(3.7795275552 * lyr.size(), 0),
'opacity': layerOpacity,
'borderColor': lyr.outlineColor().name(),
'borderWidth': round(3.7795275552 * lyr.outlineWidth(), 0)
}
filein = open(QgisCartoDB.CartoDBPlugin.PLUGIN_DIR + '/templates/simplepoint.less')
elif layer.geometryType() == QGis.Line:
d = {
'layername': styleName,
'lineColor': lyr.color().name(),
'lineWidth': round(3.7795275552 * lyr.width(), 0),
'opacity': layerOpacity
}
filein = open(QgisCartoDB.CartoDBPlugin.PLUGIN_DIR + '/templates/simpleline.less')
elif layer.geometryType() == QGis.Polygon:
d = {
'layername': styleName,
'fillColor': lyr.fillColor().name(),
'opacity': layerOpacity,
'borderColor': lyr.outlineColor().name(),
'borderWidth': round(3.7795275552 * lyr.borderWidth(), 0)
}
filein = open(QgisCartoDB.CartoDBPlugin.PLUGIN_DIR + '/templates/simplepolygon.less')

cartoCSS = Template(filein.read())
cartoCSS = cartoCSS.substitute(d,
input_encoding='utf-8',
output_encoding='utf-8',
encoding_errors='replace')
lyr = None
for i in range(0, symbol.symbolLayerCount()):
lyr = symbol.symbolLayer(i)
if lyr.layerType().startswith('Simple'):
break

# qDebug("Symbol Type: {}".format(lyr.layerType()))
# qDebug("Symbol Properties: {}".format(lyr.properties()))

if lyr is not None and lyr.layerType().startswith('Simple'):
filein = None
if layer.geometryType() == QGis.Point:
d = {
'layername': styleName,
'fillColor': lyr.fillColor().name(),
# 96 ppi = 3.7795275552 mm
'width': round(3.7795275552 * lyr.size(), 0),
'opacity': layerOpacity,
'borderColor': lyr.outlineColor().name(),
'borderWidth': round(3.7795275552 * lyr.outlineWidth(), 0),
'markerCompOp': compositionMode
}
filein = open(QgisCartoDB.CartoDBPlugin.PLUGIN_DIR + '/templates/simplepoint.less')
elif layer.geometryType() == QGis.Line:
lineWidth = round(3.7795275552 * lyr.width(), 0)
if lyr.penStyle() == Qt.NoPen:
lineWidth = 0

d = {
'layername': styleName,
'lineColor': lyr.color().name(),
'lineWidth': lineWidth,
'opacity': layerOpacity,
'lineCompOp': compositionMode,
'lineJoin': self._getLineJoin(lyr),
'lineDasharray': self._getLineDasharray(lyr.penStyle(), lineWidth)
}
filein = open(QgisCartoDB.CartoDBPlugin.PLUGIN_DIR + '/templates/simpleline.less')
elif layer.geometryType() == QGis.Polygon:
borderWidth = round(3.7795275552 * lyr.borderWidth(), 0)
if lyr.borderStyle() == Qt.NoPen:
borderWidth = 0

d = {
'layername': styleName,
'fillColor': lyr.fillColor().name(),
'opacity': layerOpacity,
'borderColor': lyr.outlineColor().name(),
'borderWidth': borderWidth,
'polygonCompOp': compositionMode,
'lineJoin': self._getLineJoin(lyr),
'lineDasharray': self._getLineDasharray(lyr.borderStyle(), borderWidth)
}
filein = open(QgisCartoDB.CartoDBPlugin.PLUGIN_DIR + '/templates/simplepolygon.less')

cartoCSS = Template(filein.read())
cartoCSS = cartoCSS.substitute(d,
input_encoding='utf-8',
output_encoding='utf-8',
encoding_errors='replace')
else:
# TODO Manage symbols not supported.
qDebug('Symbol type: {} not supported'.format())
return cartoCSS

def validateButtons(self):
enabled = self.ui.mapNameTX.text() != '' and self.ui.mapList.count() > 0
self.ui.saveBT.setEnabled(enabled)

def _getLineJoin(self, lyr):
joinStyle = 'miter'
if lyr.penJoinStyle() == Qt.BevelJoin:
joinStyle = 'bevel'
elif lyr.penJoinStyle() == Qt.RoundJoin:
joinStyle = 'round'
return joinStyle

def _getLineDasharray(self, lineStyle, lineWidth):
lineDasharray = '0'
if lineStyle == Qt.DashLine:
lineDasharray = '5,5'
elif lineStyle == Qt.DotLine:
lineDasharray = '{},{}'.format(lineWidth, lineWidth*5)
elif lineStyle == Qt.DashDotLine:
lineDasharray = '{},{},{},{}'.format(lineWidth*10, lineWidth*10, lineWidth, lineWidth*10)
elif lineStyle == Qt.DashDotDotLine:
lineDasharray = '{},{},{},{},{},{}'.format(lineWidth*5, lineWidth*5, lineWidth, lineWidth*5, lineWidth, lineWidth*5)
return lineDasharray
5 changes: 4 additions & 1 deletion metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ about=
- certifi
[email protected]
changelog=0.1.8
- Remove connection controls in SQL Editor
- Remove connection controls in SQL Editor.
- Add composite mode in create maps.
- Add dash style to lines in create maps.
- Add join style to lines in create maps.
0.1.7
- Do not allow to edit read-only layers.
- Show owner in shared layers.
Expand Down
3 changes: 3 additions & 0 deletions templates/simpleline.less
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ $layername {
line-color: $lineColor;
line-width: $lineWidth;
line-opacity: $opacity;
line-comp-op: $lineCompOp;
line-join: $lineJoin;
line-dasharray: $lineDasharray;
}
1 change: 1 addition & 0 deletions templates/simplepoint.less
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ $layername {
marker-width: $width;
marker-fill: $fillColor;
marker-allow-overlap: true;
marker-comp-op: $markerCompOp;
}
3 changes: 3 additions & 0 deletions templates/simplepolygon.less
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
$layername {
polygon-fill: $fillColor;
polygon-opacity: $opacity;
polygon-comp-op: $polygonCompOp;
line-color: $borderColor;
line-width: $borderWidth;
line-opacity: $opacity;
line-join: $lineJoin;
line-dasharray: $lineDasharray;
}
7 changes: 7 additions & 0 deletions ui/CreateViz.ui
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@
</item>
<item>
<layout class="QHBoxLayout" name="buttonsLayout">
<item>
<widget class="QPushButton" name="cartoCssBT">
<property name="text">
<string>CartoCSS</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="sqlBT">
<property name="text">
Expand Down

0 comments on commit b16e494

Please sign in to comment.