7
7
#include " qgsvectorlayer.h"
8
8
#include " qgsvectorlayerlabeling.h"
9
9
10
+ #include < QClipboard>
10
11
#include < QMessageBox>
11
12
12
13
QgsRuleBasedLabelingWidget::QgsRuleBasedLabelingWidget ( QgsVectorLayer* layer, QgsMapCanvas* canvas, QWidget* parent )
@@ -22,11 +23,25 @@ QgsRuleBasedLabelingWidget::QgsRuleBasedLabelingWidget( QgsVectorLayer* layer, Q
22
23
btnEditRule->setIcon ( QIcon ( QgsApplication::iconPath ( " symbologyEdit.png" ) ) );
23
24
btnRemoveRule->setIcon ( QIcon ( QgsApplication::iconPath ( " symbologyRemove.svg" ) ) );
24
25
26
+ mCopyAction = new QAction ( tr ( " Copy" ), this );
27
+ mCopyAction ->setShortcut ( QKeySequence ( QKeySequence::Copy ) );
28
+ mPasteAction = new QAction ( tr ( " Paste" ), this );
29
+ mPasteAction ->setShortcut ( QKeySequence ( QKeySequence::Paste ) );
30
+ mDeleteAction = new QAction ( tr ( " Remove Rule" ), this );
31
+ mDeleteAction ->setShortcut ( QKeySequence ( QKeySequence::Delete ) );
32
+
33
+ viewRules->addAction ( mDeleteAction );
34
+ viewRules->addAction ( mCopyAction );
35
+ viewRules->addAction ( mPasteAction );
36
+
25
37
connect ( viewRules, SIGNAL ( doubleClicked ( const QModelIndex & ) ), this , SLOT ( editRule ( const QModelIndex & ) ) );
26
38
27
39
connect ( btnAddRule, SIGNAL ( clicked () ), this , SLOT ( addRule () ) );
28
40
connect ( btnEditRule, SIGNAL ( clicked () ), this , SLOT ( editRule () ) );
29
41
connect ( btnRemoveRule, SIGNAL ( clicked () ), this , SLOT ( removeRule () ) );
42
+ connect ( mCopyAction , SIGNAL ( triggered ( bool ) ), this , SLOT ( copy () ) );
43
+ connect ( mPasteAction , SIGNAL ( triggered ( bool ) ), this , SLOT ( paste () ) );
44
+ connect ( mDeleteAction , SIGNAL ( triggered ( bool ) ), this , SLOT ( removeRule () ) );
30
45
31
46
if ( mLayer ->labeling () && mLayer ->labeling ()->type () == " rule-based" )
32
47
{
@@ -118,6 +133,30 @@ void QgsRuleBasedLabelingWidget::removeRule()
118
133
// TODO mModel->clearFeatureCounts();
119
134
}
120
135
136
+ void QgsRuleBasedLabelingWidget::copy ()
137
+ {
138
+ QModelIndexList indexlist = viewRules->selectionModel ()->selectedRows ();
139
+ QgsDebugMsg ( QString ( " %1" ).arg ( indexlist.count () ) );
140
+
141
+ if ( indexlist.isEmpty () )
142
+ return ;
143
+
144
+ QMimeData* mime = mModel ->mimeData ( indexlist );
145
+ QApplication::clipboard ()->setMimeData ( mime );
146
+ }
147
+
148
+ void QgsRuleBasedLabelingWidget::paste ()
149
+ {
150
+ const QMimeData* mime = QApplication::clipboard ()->mimeData ();
151
+ QModelIndexList indexlist = viewRules->selectionModel ()->selectedRows ();
152
+ QModelIndex index;
153
+ if ( indexlist.isEmpty () )
154
+ index = mModel ->index ( mModel ->rowCount (), 0 );
155
+ else
156
+ index = indexlist.first ();
157
+ mModel ->dropMimeData ( mime, Qt::CopyAction, index.row (), index.column (), index.parent () );
158
+ }
159
+
121
160
QgsRuleBasedLabeling::Rule* QgsRuleBasedLabelingWidget::currentRule ()
122
161
{
123
162
QItemSelectionModel* sel = viewRules->selectionModel ();
@@ -175,7 +214,7 @@ QVariant QgsRuleBasedLabelingModel::data( const QModelIndex& index, int role ) c
175
214
{
176
215
switch ( index.column () )
177
216
{
178
- case 0 : return rule->label ();
217
+ case 0 : return rule->description ();
179
218
case 1 :
180
219
if ( rule->isElse () )
181
220
{
@@ -187,6 +226,7 @@ QVariant QgsRuleBasedLabelingModel::data( const QModelIndex& index, int role ) c
187
226
}
188
227
case 2 : return rule->dependsOnScale () ? _formatScale ( rule->scaleMaxDenom () ) : QVariant ();
189
228
case 3 : return rule->dependsOnScale () ? _formatScale ( rule->scaleMinDenom () ) : QVariant ();
229
+ case 4 : return rule->settings () ? rule->settings ()->fieldName : QVariant ();
190
230
#if 0 // TODO: feature counts?
191
231
case 4:
192
232
if ( mFeatureCountMap.count( rule ) == 1 )
@@ -248,10 +288,11 @@ QVariant QgsRuleBasedLabelingModel::data( const QModelIndex& index, int role ) c
248
288
{
249
289
switch ( index.column () )
250
290
{
251
- case 0 : return rule->label ();
291
+ case 0 : return rule->description ();
252
292
case 1 : return rule->filterExpression ();
253
293
case 2 : return rule->scaleMaxDenom ();
254
294
case 3 : return rule->scaleMinDenom ();
295
+ case 4 : return rule->settings () ? rule->settings ()->fieldName : QVariant ();
255
296
default : return QVariant ();
256
297
}
257
298
}
@@ -269,7 +310,7 @@ QVariant QgsRuleBasedLabelingModel::headerData( int section, Qt::Orientation ori
269
310
{
270
311
if ( orientation == Qt::Horizontal && role == Qt::DisplayRole && section >= 0 && section < 5 )
271
312
{
272
- QStringList lst; lst << tr ( " Label" ) << tr ( " Rule" ) << tr ( " Min. scale" ) << tr ( " Max. scale" ); // << tr( "Count" ) << tr( "Duplicate count" );
313
+ QStringList lst; lst << tr ( " Label" ) << tr ( " Rule" ) << tr ( " Min. scale" ) << tr ( " Max. scale" ) << tr ( " Text " ) ; // << tr( "Count" ) << tr( "Duplicate count" );
273
314
return lst[section];
274
315
}
275
316
else if ( orientation == Qt::Horizontal && role == Qt::ToolTipRole )
@@ -300,7 +341,7 @@ int QgsRuleBasedLabelingModel::rowCount( const QModelIndex& parent ) const
300
341
301
342
int QgsRuleBasedLabelingModel::columnCount ( const QModelIndex& ) const
302
343
{
303
- return 4 ;
344
+ return 5 ;
304
345
}
305
346
306
347
QModelIndex QgsRuleBasedLabelingModel::index ( int row, int column, const QModelIndex& parent ) const
@@ -350,8 +391,8 @@ bool QgsRuleBasedLabelingModel::setData( const QModelIndex& index, const QVarian
350
391
351
392
switch ( index.column () )
352
393
{
353
- case 0 : // label
354
- rule->setLabel ( value.toString () );
394
+ case 0 : // description
395
+ rule->setDescription ( value.toString () );
355
396
break ;
356
397
case 1 : // filter
357
398
rule->setFilterExpression ( value.toString () );
@@ -362,6 +403,11 @@ bool QgsRuleBasedLabelingModel::setData( const QModelIndex& index, const QVarian
362
403
case 3 : // scale max
363
404
rule->setScaleMinDenom ( value.toInt () );
364
405
break ;
406
+ case 4 : // label text
407
+ if ( !rule->settings () )
408
+ return false ;
409
+ rule->settings ()->fieldName = value.toString ();
410
+ break ;
365
411
default :
366
412
return false ;
367
413
}
@@ -382,6 +428,22 @@ QStringList QgsRuleBasedLabelingModel::mimeTypes() const
382
428
return types;
383
429
}
384
430
431
+ // manipulate DOM before dropping it so that rules are more useful
432
+ void _renderer2labelingRules ( QDomElement& ruleElem )
433
+ {
434
+ // labeling rules recognize only "description"
435
+ if ( ruleElem.hasAttribute ( " label" ) )
436
+ ruleElem.setAttribute ( " description" , ruleElem.attribute ( " label" ) );
437
+
438
+ // run recursively
439
+ QDomElement childRuleElem = ruleElem.firstChildElement ( " rule" );
440
+ while ( !childRuleElem.isNull () )
441
+ {
442
+ _renderer2labelingRules ( childRuleElem );
443
+ childRuleElem = childRuleElem.nextSiblingElement ( " rule" );
444
+ }
445
+ }
446
+
385
447
QMimeData*QgsRuleBasedLabelingModel::mimeData ( const QModelIndexList& indexes ) const
386
448
{
387
449
QMimeData *mimeData = new QMimeData ();
@@ -401,6 +463,7 @@ QMimeData*QgsRuleBasedLabelingModel::mimeData( const QModelIndexList& indexes )
401
463
QDomDocument doc;
402
464
403
465
QDomElement rootElem = doc.createElement ( " rule_mime" );
466
+ rootElem.setAttribute ( " type" , " labeling" ); // for determining whether rules are from renderer or labeling
404
467
QDomElement rulesElem = rule->save ( doc );
405
468
rootElem.appendChild ( rulesElem );
406
469
doc.appendChild ( rootElem );
@@ -449,6 +512,8 @@ bool QgsRuleBasedLabelingModel::dropMimeData( const QMimeData* data, Qt::DropAct
449
512
if ( rootElem.tagName () != " rule_mime" )
450
513
continue ;
451
514
QDomElement ruleElem = rootElem.firstChildElement ( " rule" );
515
+ if ( rootElem.attribute ( " type" ) == " renderer" )
516
+ _renderer2labelingRules ( ruleElem ); // do some modifications so that we load the rules more nicely
452
517
QgsRuleBasedLabeling::Rule* rule = QgsRuleBasedLabeling::Rule::create ( ruleElem );
453
518
454
519
insertRule ( parent, row + rows, rule );
@@ -526,7 +591,6 @@ QgsLabelingRulePropsDialog::QgsLabelingRulePropsDialog( QgsRuleBasedLabeling::Ru
526
591
527
592
editFilter->setText ( mRule ->filterExpression () );
528
593
editFilter->setToolTip ( mRule ->filterExpression () );
529
- editLabel->setText ( mRule ->label () );
530
594
editDescription->setText ( mRule ->description () );
531
595
editDescription->setToolTip ( mRule ->description () );
532
596
@@ -653,7 +717,6 @@ void QgsLabelingRulePropsDialog::buildExpression()
653
717
void QgsLabelingRulePropsDialog::accept ()
654
718
{
655
719
mRule ->setFilterExpression ( editFilter->text () );
656
- mRule ->setLabel ( editLabel->text () );
657
720
mRule ->setDescription ( editDescription->text () );
658
721
// caution: rule uses scale denom, scale widget uses true scales
659
722
mRule ->setScaleMinDenom ( groupScale->isChecked () ? mScaleRangeWidget ->minimumScaleDenom () : 0 );
0 commit comments