qgis_legendiface.patch

Andres Manz -, 2009-12-03 03:58 AM

Download (19 KB)

View differences:

python/gui/qgslegendinterface.sip (revision 0)
1
/**
2
 * \class QgsLegendInterface
3
 * \brief Abstract base class to make QgsLegend available to plugins.
4
 */
5
class QgsLegendInterface : QObject
6
{
7
%TypeHeaderCode
8
#include <qgslegendinterface.h>
9
%End
10

  
11
  public:
12

  
13
    /** Constructor */
14
    QgsLegendInterface();
15

  
16
    /** Virtual destructor */
17
    ~QgsLegendInterface();
18

  
19
    virtual QStringList groups() =0;
20

  
21
  signals:
22

  
23
    void groupIndexChanged( int oldIndex, int newIndex );
24

  
25
  public slots:
26

  
27
    //! Add a new group
28
    virtual int addGroup( QString name = "group", bool expand = true ) =0;
29

  
30
    virtual void removeGroup( int groupIndex ) =0;
31

  
32
    virtual void moveLayer( QgsMapLayer * layer, int groupIndex ) =0;
33
};
34

  
python/gui/qgisinterface.sip (working copy)
25 25
    /** Virtual destructor */
26 26
    virtual ~QgisInterface();
27 27
    
28
    virtual QgsLegendInterface* legendInterface()=0;
28 29

  
29 30
  public slots: // TODO: do these functions really need to be slots?
30 31

  
python/gui/gui.sip (working copy)
7 7

  
8 8
%Import core/core.sip
9 9

  
10
%Include qgslegendinterface.sip
10 11
%Include qgisinterface.sip
11 12
%Include qgscomposerview.sip
12 13
%Include qgsencodingfiledialog.sip
src/app/legend/qgsapplegendinterface.h (revision 0)
1
/***************************************************************************
2
    qgsapplegendinterface.h - description
3
     --------------------------------------
4
    Date                 : 23-Nov-2009
5
    Copyright            : (C) 2009 by Andres Manz
6
    Email                : manz dot andres at gmail dot com
7
****************************************************************************/
8
/***************************************************************************
9
 *                                                                         *
10
 *   This program is free software; you can redistribute it and/or modify  *
11
 *   it under the terms of the GNU General Public License as published by  *
12
 *   the Free Software Foundation; either version 2 of the License, or     *
13
 *   (at your option) any later version.                                   *
14
 *                                                                         *
15
 ***************************************************************************/
16
/* $Id$ */
17

  
18
#ifndef QGSLEGENDAPPIFACE_H
19
#define QGSLEGENDAPPIFACE_H
20

  
21
#include "qgslegendinterface.h"
22
#include "qgslegend.h"
23
#include "qgsmaplayer.h"
24

  
25
/** \ingroup gui
26
 * QgsLegendInterface
27
 * Abstract base class to make QgsLegend available to plugins.
28
 */
29
class QgsAppLegendInterface : public QgsLegendInterface
30
{
31
    Q_OBJECT
32

  
33
  public:
34

  
35
    /** Constructor */
36
    explicit QgsAppLegendInterface( QgsLegend * legend );
37

  
38
    /** Virtual destructor */
39
    ~QgsAppLegendInterface();
40

  
41
    //! Return a string list of groups
42
    QStringList groups();
43

  
44
  public slots:
45

  
46
    //! Add a new group
47
    int addGroup( QString name = "group", bool expand = true );
48

  
49
    //! Remove all groups with the given name
50
    void removeGroup( int groupIndex );
51

  
52
    //! Move a layer to a group
53
    void moveLayer( QgsMapLayer * ml, int groupIndex );
54

  
55
    //! Update an index
56
    void updateIndex( const QModelIndex &oldIndex, const QModelIndex &newIndex );
57

  
58
  private:
59

  
60
    //! Pointer to QgsLegend object
61
    QgsLegend *mLegend;
62
};
63

  
64
#endif //QGSLEGENDAPPIFACE_H
src/app/legend/qgslegend.cpp (working copy)
116 116
  emit currentLayerChanged( layer );
117 117
}
118 118

  
119
void QgsLegend::addGroup()
119
int QgsLegend::addGroup( QString name, bool expand )
120 120
{
121
  QgsLegendGroup* group = new QgsLegendGroup( this, tr( "group" ) );
121
  QgsLegendGroup* group = new QgsLegendGroup( this, name );
122 122
  group->setData( 0, Qt::UserRole, Qt::Checked );
123
  setExpanded( indexFromItem( group ), true );
123
  QModelIndex groupIndex = indexFromItem( group );
124
  setExpanded( groupIndex, expand );
125
  return groupIndex.row();
124 126
}
125 127

  
126 128
void QgsLegend::removeAll()
......
161 163
  mMapCanvas->setRenderFlag( renderFlagState );
162 164
}
163 165

  
166
void QgsLegend::removeGroup( int groupIndex )
167
{
168
  QgsLegendGroup * lg = dynamic_cast<QgsLegendGroup *>( topLevelItem( groupIndex ) );
169
  if( lg )
170
  {
171
    removeGroup( lg );
172
  }
173
}
174

  
164 175
void QgsLegend::removeLayer( QString layer_key )
165 176
{
166 177
  if ( !mMapCanvas || mMapCanvas->isDrawing() )
......
355 366

  
356 367
  QgsLegendItem* origin = dynamic_cast<QgsLegendItem *>( mItemBeingMoved );
357 368
  mItemBeingMoved = NULL;
369
  QModelIndex oldIndex = indexFromItem( origin );
358 370

  
359 371
  QgsLegendItem* dest = dynamic_cast<QgsLegendItem *>( destItem );
360 372

  
......
375 387
      {
376 388
        moveItem( origin, dest );
377 389
        setCurrentItem( origin );
390
        emit itemMoved( oldIndex, indexFromItem( origin ) );
378 391
      }
379 392
    }
380 393
    else if ( mDropAction == BEFORE )// below center of item
......
385 398
        moveItem( origin, dest ); // Insert after, as above...
386 399
        moveItem( dest, origin ); // ... and then switch places!
387 400
        setCurrentItem( origin );
401
        emit itemMoved( oldIndex, indexFromItem( origin ) );
388 402
      }
389 403
    }
390 404
    else if ( mDropAction == INTO_GROUP )
......
394 408
      {
395 409
        insertItem( origin, dest );
396 410
        setCurrentItem( origin );
411
        emit itemMoved( oldIndex, indexFromItem( origin ) );
397 412
      }
398 413
    }
399 414
    else//no action
......
578 593
  QgsLegendGroup* lg = dynamic_cast<QgsLegendGroup *>( currentItem() );
579 594
  if ( lg )
580 595
  {
581
    //delete the legend layers first
582
    QTreeWidgetItem * child = lg->child( 0 );
583
    while ( child )
584
    {
585
      setCurrentItem( child );
586
      removeCurrentLayer();
587
      child = lg->child( 0 );
588
    }
589
    delete lg;
590
    adjustIconSize();
596
    removeGroup( lg );
591 597
  }
592 598
}
593 599

  
600
void QgsLegend::removeGroup( QgsLegendGroup * lg )
601
{
602
  if ( !mMapCanvas || mMapCanvas->isDrawing() )
603
  {
604
    return;
605
  }
606

  
607
  //delete the legend layers first
608
  QTreeWidgetItem * child = lg->child( 0 );
609
  while ( child )
610
  {
611
    setCurrentItem( child );
612
    removeCurrentLayer();
613
    child = lg->child( 0 );
614
  }
615
  delete lg;
616
  adjustIconSize();
617
}
618

  
594 619
void QgsLegend::removeCurrentLayer()
595 620
{
596 621
  if ( !mMapCanvas || mMapCanvas->isDrawing() )
......
661 686
  return true;
662 687
}
663 688

  
689
void QgsLegend::moveLayer( QgsMapLayer * ml, int groupIndex )
690
{
691
    QgsLegendLayer *layer = findLegendLayer( ml->getLayerID() );
692
    QgsLegendGroup *group = dynamic_cast<QgsLegendGroup*>( topLevelItem( groupIndex ) );
693
    if ( layer && group )
694
    {
695
      insertItem( layer, group );
696
    }
697
}
664 698

  
665

  
666 699
void QgsLegend::legendLayerShowProperties()
667 700
{
668 701
  if ( !mMapCanvas || mMapCanvas->isDrawing() )
......
1180 1213
  }
1181 1214
}
1182 1215

  
1216
bool QgsLegend::isLegendGroup( const QModelIndex &index )
1217
{
1218
  return dynamic_cast<QgsLegendGroup *>( itemFromIndex( index ) );
1219
}
1220

  
1221
QStringList QgsLegend::groups()
1222
{
1223
  QStringList groupList;
1224
  QTreeWidgetItem *current = firstItem();
1225

  
1226
  while ( current )
1227
  {
1228
    QgsLegendGroup *group = dynamic_cast<QgsLegendGroup *>( current );
1229
    if ( group )
1230
    {
1231
      groupList.append( group->text( 0 ) );
1232
    }
1233

  
1234
    current = nextItem( current );
1235
  }
1236

  
1237
  return groupList;
1238
}
1239

  
1183 1240
/**Returns the first item in the hierarchy*/
1184 1241
QTreeWidgetItem* QgsLegend::firstItem()
1185 1242
{
src/app/legend/qgslegend.h (working copy)
25 25
#include <set>
26 26
#include <QTreeWidget>
27 27

  
28
class QgsLegendGroup;
28 29
class QgsLegendLayer;
29 30
class QgsLegendItem;
30 31
class QgsMapLayer;
......
112 113
    /**Returns true, if the y-coordinate is >= the center of the item*/
113 114
    bool yCoordAboveCenter( QgsLegendItem* it, int ycoord );
114 115

  
116
    /**Returns true, if the item at index is a QgsLegendGroup*/
117
    bool isLegendGroup( const QModelIndex &index );
118

  
119
    /**Returns a string list of groups*/
120
    QStringList groups();
121

  
115 122
    /**Returns the first item in the hierarchy*/
116 123
    QTreeWidgetItem* firstItem();
117 124

  
......
198 205
    /*!
199 206
     * Slot called when user wishes to add a new empty layer group to the legend.
200 207
     * The user will be prompted for the name of the newly added group.
208
     * @param name name of the new group
209
     * @param expand expand the group
201 210
     * @return void
202 211
     */
203
    void addGroup();
212
    int addGroup( QString name = "group", bool expand = true );
213

  
214
    /*!
215
     * Removes all groups with the given name.
216
     * @param name name of the groups to remove
217
     * @return void
218
     */
219
    void removeGroup( int groupIndex );
220

  
204 221
    void removeLayer( QString );
205 222

  
206 223
    /** called to read legend settings from project */
......
219 236
      @param return false if canceled or in case of error, true else*/
220 237
    bool removeLayer( QgsMapLayer* ml, bool askCancelOnEditable );
221 238

  
239
    /*!
240
     * Moves a layer to a group.
241
     * @param ml the maplayer to move
242
     * @param groupIndex index of group
243
     * @return false if the group does not exist, false otherwise
244
     */
245
    void moveLayer( QgsMapLayer* ml, int groupIndex );
246

  
222 247
    /**Toggle show in overview for current layer*/
223 248
    void legendLayerShowInOverview();
224 249

  
......
331 356
    void handleRightClickEvent( QTreeWidgetItem* item, const QPoint& position );
332 357
    /**Removes the current legend group*/
333 358
    void legendGroupRemove();
359
    /**Removes a legend group and its layers*/
360
    void removeGroup( QgsLegendGroup * lg );
334 361
    /**Sets all listview items to open*/
335 362
    void expandAll();
336 363
    /**Sets all listview items to closed*/
......
437 464
    QWidget *mInsertionLine;
438 465

  
439 466
  signals:
467
    void itemMoved( QModelIndex oldIndex, QModelIndex newIndex );
468

  
440 469
    void zOrderChanged( QgsLegend * lv );
441 470

  
442 471
    //! Emited whenever current (selected) layer changes
src/app/legend/qgsapplegendinterface.cpp (revision 0)
1
/***************************************************************************
2
                    qgslegendinterface.h - description
3
     --------------------------------------
4
    Date                 : 19-Nov-2009
5
    Copyright            : (C) 2009 by Andres Manz
6
    Email                : manz dot andres at gmail dot com
7
****************************************************************************/
8
/***************************************************************************
9
 *                                                                         *
10
 *   This program is free software; you can redistribute it and/or modify  *
11
 *   it under the terms of the GNU General Public License as published by  *
12
 *   the Free Software Foundation; either version 2 of the License, or     *
13
 *   (at your option) any later version.                                   *
14
 *                                                                         *
15
 ***************************************************************************/
16
/* $Id$ */
17

  
18
#include "qgsapplegendinterface.h"
19

  
20
QgsAppLegendInterface::QgsAppLegendInterface( QgsLegend * legend )
21
    : mLegend( legend )
22
{
23
}
24

  
25
QgsAppLegendInterface::~QgsAppLegendInterface()
26
{
27
}
28

  
29
int QgsAppLegendInterface::addGroup( QString name, bool expand )
30
{
31
  return mLegend->addGroup( name, expand );
32
}
33

  
34
void QgsAppLegendInterface::removeGroup( int groupIndex )
35
{
36
  mLegend->removeGroup( groupIndex );
37
}
38

  
39
void QgsAppLegendInterface::moveLayer( QgsMapLayer * ml, int groupIndex )
40
{
41
  mLegend->moveLayer( ml, groupIndex );
42
}
43

  
44
void QgsAppLegendInterface::updateIndex( const QModelIndex &oldIndex, const QModelIndex& newIndex)
45
{
46
  if ( mLegend->isLegendGroup( newIndex ) )
47
  {
48
    emit groupIndexChanged( oldIndex.row(), newIndex.row() );
49
  }
50
}
51

  
52
QStringList QgsAppLegendInterface::groups()
53
{
54
  return mLegend->groups();
55
}
src/app/qgisappinterface.h (working copy)
20 20
#define QGISIFACE_H
21 21

  
22 22
#include "qgisinterface.h"
23
#include "qgsapplegendinterface.h"
23 24

  
24 25
class QgisApp;
25 26

  
......
42 43
    QgisAppInterface( QgisApp *qgisapp );
43 44
    ~QgisAppInterface();
44 45

  
46
    QgsLegendInterface* legendInterface();
47

  
45 48
    /* Exposed functions */
46 49
    //! Zoom map to full extent
47 50
    void zoomFull();
......
258 261

  
259 262
    //! Pointer to the QgisApp object
260 263
    QgisApp *qgis;
264

  
265
    //! Pointer to the LegendInterface object
266
    QgsAppLegendInterface legendIface;
261 267
};
262 268

  
263 269

  
src/app/CMakeLists.txt (working copy)
88 88

  
89 89
  legend/qgslegendgroup.cpp
90 90
  legend/qgslegend.cpp
91
  legend/qgsapplegendinterface.cpp
91 92
  legend/qgslegenditem.cpp
92 93
  legend/qgslegendlayer.cpp
93 94
  legend/qgslegendpropertygroup.cpp
......
185 186
  composer/qgsitempositiondialog.h
186 187

  
187 188
  legend/qgslegend.h
189
  legend/qgsapplegendinterface.h
188 190
  legend/qgslegendlayer.h
189 191
  
190 192
  ogr/qgsopenvectorlayerdialog.h
src/app/qgisappinterface.cpp (working copy)
32 32
#include "qgsshortcutsmanager.h"
33 33

  
34 34
QgisAppInterface::QgisAppInterface( QgisApp * _qgis )
35
    : qgis( _qgis )
35
    : qgis( _qgis ),
36
      legendIface( _qgis->legend() )
36 37
{
37 38
  // connect signals
38 39
  connect( qgis->legend(), SIGNAL( currentLayerChanged( QgsMapLayer * ) ),
......
46 47
{
47 48
}
48 49

  
50
QgsLegendInterface* QgisAppInterface::legendInterface()
51
{
52
    return &legendIface;
53
}
54

  
49 55
void QgisAppInterface::zoomFull()
50 56
{
51 57
  qgis->zoomFull();
src/gui/qgslegendinterface.h (revision 0)
1
/***************************************************************************
2
    qgslegendinterface.h - description
3
     --------------------------------------
4
    Date                 : 19-Nov-2009
5
    Copyright            : (C) 2009 by Andres Manz
6
    Email                : manz dot andres at gmail dot com
7
****************************************************************************/
8
/***************************************************************************
9
 *                                                                         *
10
 *   This program is free software; you can redistribute it and/or modify  *
11
 *   it under the terms of the GNU General Public License as published by  *
12
 *   the Free Software Foundation; either version 2 of the License, or     *
13
 *   (at your option) any later version.                                   *
14
 *                                                                         *
15
 ***************************************************************************/
16
/* $Id$ */
17

  
18
#ifndef QGSLEGENDINTERFACE_H
19
#define QGSLEGENDINTERFACE_H
20

  
21
#include <QObject>
22
#include <QStringList>
23

  
24
class QgsMapLayer;
25

  
26
/** \ingroup gui
27
 * QgsLegendInterface
28
 * Abstract base class to make QgsLegend available to plugins.
29
 */
30
class GUI_EXPORT QgsLegendInterface : public QObject
31
{
32
    Q_OBJECT
33

  
34
  public:
35

  
36
    /** Constructor */
37
    QgsLegendInterface();
38

  
39
    /** Virtual destructor */
40
    virtual ~QgsLegendInterface();
41

  
42
    //! Return a string list of groups
43
    virtual QStringList groups() = 0;
44

  
45
  signals:
46

  
47
    void groupIndexChanged( int oldIndex, int newIndex );
48

  
49
  public slots:
50

  
51
    //! Add a new group
52
    virtual int addGroup( QString name = "group", bool expand = true ) = 0;
53

  
54
    //! Remove group on index
55
    virtual void removeGroup( int groupIndex ) = 0;
56

  
57
    //! Move a layer to a group
58
    virtual void moveLayer( QgsMapLayer * ml, int groupIndex ) = 0;
59
};
60

  
61
#endif
src/gui/qgslegendinterface.cpp (revision 0)
1
/***************************************************************************
2
                    qgslegendinterface.h - description
3
     --------------------------------------
4
    Date                 : 19-Nov-2009
5
    Copyright            : (C) 2009 by Andres Manz
6
    Email                : manz dot andres at gmail dot com
7
****************************************************************************/
8
/***************************************************************************
9
 *                                                                         *
10
 *   This program is free software; you can redistribute it and/or modify  *
11
 *   it under the terms of the GNU General Public License as published by  *
12
 *   the Free Software Foundation; either version 2 of the License, or     *
13
 *   (at your option) any later version.                                   *
14
 *                                                                         *
15
 ***************************************************************************/
16
/* $Id$ */
17

  
18
#include "qgslegendinterface.h"
19
#include "../app/legend/qgslegend.h"
20

  
21
QgsLegendInterface::QgsLegendInterface()
22
{
23
}
24

  
25
QgsLegendInterface::~QgsLegendInterface()
26
{
27
}
28

  
src/gui/CMakeLists.txt (working copy)
19 19

  
20 20
qgisgui.cpp
21 21
qgisinterface.cpp
22
qgslegendinterface.cpp
22 23
qgscolorbutton.cpp
23 24
qgscomposerview.cpp
24 25
qgscursors.cpp
......
64 65
qgscomposerview.h
65 66
qgsdetaileditemdelegate.h
66 67
qgsdetaileditemwidget.h
68
qgslegendinterface.h
67 69
qgisinterface.h
68 70
qgsencodingfiledialog.h
69 71
qgsgenericprojectionselector.h
src/gui/qgisinterface.h (working copy)
37 37
class QgsMapCanvas;
38 38
class QgsRasterLayer;
39 39
class QgsVectorLayer;
40
class QgsLegendInterface;
40 41

  
41 42
/** \ingroup gui
42 43
 * QgisInterface
......
62 63
    /** Virtual destructor */
63 64
    virtual ~QgisInterface();
64 65

  
66
    virtual QgsLegendInterface* legendInterface() = 0;
65 67

  
68

  
66 69
  public slots: // TODO: do these functions really need to be slots?
67 70

  
68 71
    //! Zoom to full extent of map layers