QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsrendererv2widget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrendererv2widget.cpp
3  ---------------------
4  begin : November 2009
5  copyright : (C) 2009 by Martin Dobias
6  email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #include "qgsrendererv2widget.h"
16 #include "qgssymbolv2.h"
17 #include "qgsvectorlayer.h"
18 #include "qgscolordialog.h"
19 #include <QMessageBox>
20 #include <QInputDialog>
21 #include <QMenu>
22 
25 
26 
28  : QWidget(), mLayer( layer ), mStyle( style )
29 {
30  contextMenu = new QMenu( "Renderer Options " );
31 
32  mCopyAction = contextMenu->addAction( tr( "Copy" ), this, SLOT( copy() ) );
33  mCopyAction->setShortcut( QKeySequence( QKeySequence::Copy ) );
34  mPasteAction = contextMenu->addAction( tr( "Paste" ), this, SLOT( paste() ) );
35  mPasteAction->setShortcut( QKeySequence( QKeySequence::Paste ) );
36 
37  contextMenu->addSeparator();
38  contextMenu->addAction( tr( "Change color" ), this, SLOT( changeSymbolColor() ) );
39  contextMenu->addAction( tr( "Change transparency" ), this, SLOT( changeSymbolTransparency() ) );
40  contextMenu->addAction( tr( "Change output unit" ), this, SLOT( changeSymbolUnit() ) );
41 
42  if ( mLayer && mLayer->geometryType() == QGis::Line )
43  {
44  contextMenu->addAction( tr( "Change width" ), this, SLOT( changeSymbolWidth() ) );
45  }
46  else if ( mLayer && mLayer->geometryType() == QGis::Point )
47  {
48  contextMenu->addAction( tr( "Change size" ), this, SLOT( changeSymbolSize() ) );
49  }
50 }
51 
53 {
54  contextMenu->exec( QCursor::pos() );
55 }
56 
58 {
59  QList<QgsSymbolV2*> symbolList = selectedSymbols();
60  if ( symbolList.size() < 1 )
61  {
62  return;
63  }
64 
65  QColor color = QgsColorDialogV2::getColor( symbolList.at( 0 )->color(), this, "Change Symbol Color", true );
66  if ( color.isValid() )
67  {
68  QList<QgsSymbolV2*>::iterator symbolIt = symbolList.begin();
69  for ( ; symbolIt != symbolList.end(); ++symbolIt )
70  {
71  ( *symbolIt )->setColor( color );
72  }
74  }
75 }
76 
78 {
79  QList<QgsSymbolV2*> symbolList = selectedSymbols();
80  if ( symbolList.size() < 1 )
81  {
82  return;
83  }
84 
85  bool ok;
86  double oldTransparency = ( 1 - symbolList.at( 0 )->alpha() ) * 100; // convert to percents
87  double transparency = QInputDialog::getDouble( this, tr( "Transparency" ), tr( "Change symbol transparency [%]" ), oldTransparency, 0.0, 100.0, 0, &ok );
88  if ( ok )
89  {
90  QList<QgsSymbolV2*>::iterator symbolIt = symbolList.begin();
91  for ( ; symbolIt != symbolList.end(); ++symbolIt )
92  {
93  ( *symbolIt )->setAlpha( 1 - transparency / 100 );
94  }
96  }
97 }
98 
100 {
101  QList<QgsSymbolV2*> symbolList = selectedSymbols();
102  if ( symbolList.size() < 1 )
103  {
104  return;
105  }
106 
107  bool ok;
108  int currentUnit = ( symbolList.at( 0 )->outputUnit() == QgsSymbolV2::MM ) ? 0 : 1;
109  QString item = QInputDialog::getItem( this, tr( "Symbol unit" ), tr( "Select symbol unit" ), QStringList() << tr( "Millimeter" ) << tr( "Map unit" ), currentUnit, false, &ok );
110  if ( ok )
111  {
112  QgsSymbolV2::OutputUnit unit = ( item.compare( tr( "Millimeter" ) ) == 0 ) ? QgsSymbolV2::MM : QgsSymbolV2::MapUnit;
113 
114  QList<QgsSymbolV2*>::iterator symbolIt = symbolList.begin();
115  for ( ; symbolIt != symbolList.end(); ++symbolIt )
116  {
117  ( *symbolIt )->setOutputUnit( unit );
118  }
120  }
121 }
122 
124 {
125  QList<QgsSymbolV2*> symbolList = selectedSymbols();
126  if ( symbolList.size() < 1 )
127  {
128  return;
129  }
130 
131  bool ok;
132  QgsLineSymbolV2* line = dynamic_cast<QgsLineSymbolV2*>( symbolList.at( 0 ) ) ;
133  double width = QInputDialog::getDouble( this, tr( "Width" ), tr( "Change symbol width" ), line ? line->width() : 0.0 , 0.0, 999999, 1, &ok );
134  if ( ok )
135  {
136  QList<QgsSymbolV2*>::iterator symbolIt = symbolList.begin();
137  for ( ; symbolIt != symbolList.end(); ++symbolIt )
138  {
139  line = dynamic_cast<QgsLineSymbolV2*>( *symbolIt );
140  if ( line )
141  line->setWidth( width );
142  }
144  }
145 }
146 
148 {
149  QList<QgsSymbolV2*> symbolList = selectedSymbols();
150  if ( symbolList.size() < 1 )
151  {
152  return;
153  }
154 
155  bool ok;
156  QgsMarkerSymbolV2* marker = dynamic_cast<QgsMarkerSymbolV2*>( symbolList.at( 0 ) );
157 
158  double size = QInputDialog::getDouble( this, tr( "Size" ), tr( "Change symbol size" ), marker ? marker->size() : 0.0 , 0.0, 999999, 1, &ok );
159  if ( ok )
160  {
161  QList<QgsSymbolV2*>::iterator symbolIt = symbolList.begin();
162  for ( ; symbolIt != symbolList.end(); ++symbolIt )
163  {
164  marker = dynamic_cast<QgsMarkerSymbolV2*>( *symbolIt );
165  if ( marker )
166  marker->setSize( size );
167  }
169  }
170 }
171 
173 {
174  QgsLegendSymbolList symbols = r->legendSymbolItems();
175 
176  QgsSymbolLevelsV2Dialog dlg( symbols, r->usingSymbolLevels(), this );
177 
178  if ( dlg.exec() )
179  {
180  r->setUsingSymbolLevels( dlg.usingLevels() );
181  }
182 }
183 
184 
186 
187 //#include <QAction>
188 #include "qgsfield.h"
189 #include <QMenu>
190 
191 QgsRendererV2DataDefinedMenus::QgsRendererV2DataDefinedMenus( QMenu* menu, QgsVectorLayer* layer, QString rotationField, QString sizeScaleField, QgsSymbolV2::ScaleMethod scaleMethod )
192  : QObject( menu ), mLayer( layer )
193 {
194  mRotationMenu = new QMenu( tr( "Rotation field" ) );
195  mSizeScaleMenu = new QMenu( tr( "Size scale field" ) );
196 
197  mRotationAttributeActionGroup = new QActionGroup( mRotationMenu );
198  mSizeAttributeActionGroup = new QActionGroup( mSizeScaleMenu );
199  mSizeMethodActionGroup = new QActionGroup( mSizeScaleMenu );
200 
203 
204  mSizeScaleMenu->addSeparator();
205 
206  QAction* aScaleByArea = new QAction( tr( "Scale area" ), mSizeMethodActionGroup );
207  QAction* aScaleByDiameter = new QAction( tr( "Scale diameter" ), mSizeMethodActionGroup );
208 
209  aScaleByArea->setCheckable( true );
210  aScaleByDiameter->setCheckable( true );
211 
212  if ( scaleMethod == QgsSymbolV2::ScaleDiameter )
213  {
214  aScaleByDiameter->setChecked( true );
215  }
216  else
217  {
218  aScaleByArea->setChecked( true );
219  }
220 
221  mSizeScaleMenu->addActions( mSizeMethodActionGroup->actions() );
222 
223  menu->addMenu( mRotationMenu );
224  menu->addMenu( mSizeScaleMenu );
225 
226  connect( mSizeMethodActionGroup, SIGNAL( triggered( QAction* ) ), this, SLOT( scaleMethodSelected( QAction* ) ) );
227  connect( mRotationAttributeActionGroup, SIGNAL( triggered( QAction* ) ), this, SLOT( rotationFieldSelected( QAction* ) ) );
228  connect( mSizeAttributeActionGroup, SIGNAL( triggered( QAction* ) ), this, SLOT( sizeScaleFieldSelected( QAction* ) ) );
229 }
230 
232 {
233  delete mSizeMethodActionGroup;
236  delete mRotationMenu;
237  delete mSizeScaleMenu;
238 }
239 
240 void QgsRendererV2DataDefinedMenus::populateMenu( QMenu* menu, QString fieldName, QActionGroup *actionGroup )
241 {
242  QAction* aExpr = new QAction( tr( "- expression -" ), actionGroup );
243  aExpr->setCheckable( true );
244  menu->addAction( aExpr );
245  menu->addSeparator();
246  QAction* aNo = new QAction( tr( "- no field -" ), actionGroup );
247  aNo->setCheckable( true );
248  menu->addAction( aNo );
249  menu->addSeparator();
250 
251  bool hasField = false;
252  const QgsFields & flds = mLayer->pendingFields();
253  for ( int idx = 0; idx < flds.count(); ++idx )
254  {
255  const QgsField& fld = flds[idx];
256  if ( fld.type() == QVariant::Int || fld.type() == QVariant::Double )
257  {
258  QAction* a = new QAction( fld.name(), actionGroup );
259  a->setCheckable( true );
260  if ( fieldName == fld.name() )
261  {
262  a->setChecked( true );
263  hasField = true;
264  }
265  menu->addAction( a );
266  }
267  }
268 
269  if ( !hasField )
270  {
271  if ( fieldName.isEmpty() )
272  {
273  aNo->setChecked( true );
274  }
275  else
276  {
277  aExpr->setChecked( true );
278  aExpr->setText( tr( "- expression -" ) + fieldName );
279  }
280  }
281 
282 }
283 
285 {
286  if ( a == NULL )
287  return;
288 
289  QString fldName = a->text();
290 #if 0
291  updateMenu( mRotationAttributeActionGroup, fldName );
292 #endif
293  if ( fldName == tr( "- no field -" ) )
294  {
295  fldName = QString();
296  }
297  else if ( fldName.startsWith( tr( "- expression -" ) ) )
298  {
299  QString expr( fldName );
300  expr.replace( 0, tr( "- expression -" ).length(), "" );
301  QgsExpressionBuilderDialog dialog( mLayer, expr );
302  if ( !dialog.exec() ) return;
303  fldName = dialog.expressionText();
304  Q_ASSERT( !QgsExpression( fldName ).hasParserError() );
305  a->setText( tr( "- expression -" ) + fldName );
306  }
307 
308  emit rotationFieldChanged( fldName );
309 }
310 
312 {
313  if ( a == NULL )
314  return;
315 
316  QString fldName = a->text();
317 #if 0
318  updateMenu( mSizeAttributeActionGroup, fldName );
319 #endif
320  if ( fldName == tr( "- no field -" ) )
321  {
322  fldName = QString();
323  }
324  else if ( fldName.startsWith( tr( "- expression -" ) ) )
325  {
326  QString expr( fldName );
327  expr.replace( 0, tr( "- expression -" ).length(), "" );
328  QgsExpressionBuilderDialog dialog( mLayer, expr );
329  if ( !dialog.exec() ) return;
330  fldName = dialog.expressionText();
331  Q_ASSERT( !QgsExpression( fldName ).hasParserError() );
332  a->setText( tr( "- expression -" ) + fldName );
333  }
334 
335  emit sizeScaleFieldChanged( fldName );
336 }
337 
339 {
340  if ( a == NULL )
341  return;
342 
343  if ( a->text() == tr( "Scale area" ) )
344  {
346  }
347  else if ( a->text() == tr( "Scale diameter" ) )
348  {
350  }
351 }
352 #if 0 // MK: is there any reason for this?
353 void QgsRendererV2DataDefinedMenus::updateMenu( QActionGroup* actionGroup, QString fieldName )
354 {
355  foreach ( QAction* a, actionGroup->actions() )
356  {
357  a->setChecked( a->text() == fieldName );
358  }
359 }
360 #endif