~ubuntu-branches/ubuntu/quantal/qgis/quantal

« back to all changes in this revision

Viewing changes to src/qgssimadialog.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Steve Halasz
  • Date: 2004-12-21 09:46:27 UTC
  • Revision ID: james.westby@ubuntu.com-20041221094627-r9lb6mlz2o3yp8gn
Tags: upstream-0.6.0
ImportĀ upstreamĀ versionĀ 0.6.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                          qgssimadialog.cpp
 
3
                         Single marker renderer dialog
 
4
                             -------------------
 
5
    begin                : March 2004
 
6
    copyright            : (C) 2004 by Marco Hugentobler
 
7
    email                : mhugent@geo.unizh.ch
 
8
***************************************************************************/
 
9
 
 
10
/***************************************************************************
 
11
 *                                                                         *
 
12
 *   This program is free software; you can redistribute it and/or modify  *
 
13
 *   it under the terms of the GNU General Public License as published by  *
 
14
 *   the Free Software Foundation; either version 2 of the License, or     *
 
15
 *   (at your option) any later version.                                   *
 
16
 *                                                                         *
 
17
 ***************************************************************************/
 
18
/* $Id: qgssimadialog.cpp,v 1.29 2004/11/14 08:42:46 telwertowski Exp $ */
 
19
 
 
20
#include "qgssimadialog.h"
 
21
#include "qgssimarenderer.h"
 
22
#include "qgsvectorlayer.h"
 
23
#include "qgsmarkerdialog.h"
 
24
#include "qgsmarkersymbol.h"
 
25
#include "qgsrenderitem.h"
 
26
#include "qgsdlgvectorlayerproperties.h"
 
27
#include "qgslegenditem.h"
 
28
#include "qgssvgcache.h"
 
29
#include <qapplication.h>
 
30
#include <qfile.h>
 
31
#include <qfileinfo.h>
 
32
#include <qfiledialog.h>
 
33
#include <qimage.h>
 
34
#include <qpushbutton.h>
 
35
#include <qlineedit.h>
 
36
#include <qpainter.h>
 
37
#include <qspinbox.h>
 
38
#include <qiconview.h>
 
39
#include <qlabel.h>
 
40
 
 
41
QgsSiMaDialog::QgsSiMaDialog(QgsVectorLayer* vectorlayer): QgsSiMaDialogBase(), mVectorLayer(vectorlayer)
 
42
{
 
43
    if(mVectorLayer)
 
44
    {
 
45
        QgsSiMaRenderer *renderer;
 
46
 
 
47
        //initial settings, use the buffer of the propertiesDialog if possible. If this is not possible, use the renderer of the vectorlayer directly
 
48
        if (mVectorLayer->propertiesDialog())
 
49
        {
 
50
             renderer = dynamic_cast < QgsSiMaRenderer * >(mVectorLayer->propertiesDialog()->getBufferRenderer());
 
51
        }
 
52
        else
 
53
        {
 
54
            renderer = dynamic_cast < QgsSiMaRenderer * >(mVectorLayer->renderer());
 
55
        }
 
56
 
 
57
        if(renderer)
 
58
        {
 
59
            QgsMarkerSymbol* sy=dynamic_cast < QgsMarkerSymbol* >(renderer->item()->getSymbol());
 
60
            if(sy)
 
61
            {
 
62
                double scalefactor=sy->scaleFactor();
 
63
                mScaleSpin->setValue((int)(scalefactor*100.0));
 
64
                mSelectedMarker=sy->picture();
 
65
                pmPreview->setPixmap(QgsSVGCache::instance().
 
66
                                     getPixmap(mSelectedMarker, scalefactor));
 
67
            }
 
68
            else
 
69
            {
 
70
                qWarning("Warning, typecast failed in qgssimadialog.cpp on line 51");
 
71
                mScaleSpin->setValue(100);
 
72
            }
 
73
        }
 
74
        else
 
75
        {
 
76
            qWarning("Warning, typecast failed in qgssimadialog.cpp on line 42 or 46");
 
77
        }
 
78
  
 
79
  //set the dir to the default svg dir
 
80
#if defined(WIN32) || defined(Q_OS_MACX)
 
81
        QString PKGDATAPATH = qApp->applicationDirPath() + "/share/qgis";
 
82
#endif
 
83
        mCurrentDir=QString(PKGDATAPATH)+"/svg/";
 
84
#ifdef QGISDEBUG
 
85
  qWarning("mCurrentDir in constructor: "+mCurrentDir);
 
86
#endif
 
87
        visualizeMarkers(mCurrentDir);
 
88
        mDirectoryEdit->setText(mCurrentDir);
 
89
        //QString(PKGDATAPATH);
 
90
    }
 
91
}
 
92
 
 
93
 
 
94
 
 
95
QgsSiMaDialog::~QgsSiMaDialog()
 
96
{
 
97
    //do nothing
 
98
}
 
99
 
 
100
void QgsSiMaDialog::apply()
 
101
{
 
102
#ifdef QGISDEBUG
 
103
    qWarning("in QgsSiMaDialog::apply()");
 
104
#endif
 
105
 
 
106
    QgsMarkerSymbol* ms= new QgsMarkerSymbol();
 
107
    QString string(mSelectedMarker);
 
108
#ifdef QGISDEBUG
 
109
    qWarning(string);
 
110
#endif
 
111
 
 
112
    ms->setPicture(string);
 
113
    //set the scaled factor at the same time converting units from percentage
 
114
    ms->setScaleFactor(mScaleSpin->value()/100.0);
 
115
 
 
116
    QgsRenderItem* ri = new QgsRenderItem();
 
117
    ri->setSymbol(ms);
 
118
 
 
119
    QgsSiMaRenderer *renderer = dynamic_cast < QgsSiMaRenderer * >(mVectorLayer->renderer());
 
120
 
 
121
    if( renderer )
 
122
    {
 
123
        renderer->addItem(ri);
 
124
    }
 
125
    else
 
126
    {
 
127
        qWarning("typecast failed in QgsSiMaDialog::apply()");
 
128
        return;
 
129
    }
 
130
 
 
131
    //add a pixmap to the legend item
 
132
 
 
133
    //font tor the legend text
 
134
    QFont f("arial", 10, QFont::Normal);
 
135
    QFontMetrics fm(f);
 
136
 
 
137
    QString name;
 
138
    if (mVectorLayer->propertiesDialog())
 
139
    {
 
140
        name = mVectorLayer->propertiesDialog()->displayName();
 
141
    }
 
142
    else
 
143
    {
 
144
        name = "";
 
145
    }
 
146
 
 
147
 
 
148
    QPixmap *pix = mVectorLayer->legendPixmap();
 
149
 
 
150
    //spaces between legend pixmap elements
 
151
    int leftspace=5;
 
152
    int topspace=5;
 
153
    int bottomspace=5;
 
154
    int betweenspace=5;
 
155
    int rightspace=5;
 
156
    
 
157
    // get the marker used, calculate width and height of the legend pixmap
 
158
    QPixmap symbolPix = 
 
159
      QgsSVGCache::instance().getPixmap(string, ms->scaleFactor());
 
160
    int width = symbolPix.width() + fm.width(name) + leftspace + 
 
161
      betweenspace + rightspace;
 
162
    int height = (symbolPix.height() > fm.height() ? 
 
163
                  symbolPix.height() + topspace+bottomspace : 
 
164
                  fm.height() + topspace+bottomspace);
 
165
    pix->resize(width, height);
 
166
    pix->fill();
 
167
    
 
168
    // paint symbol and layer name
 
169
    QPainter p(pix);
 
170
    p.drawPixmap(leftspace, topspace, symbolPix);
 
171
    p.setPen(Qt::black);
 
172
    p.setFont(f);
 
173
    p.drawText(leftspace + betweenspace + symbolPix.width(), 
 
174
               pix->height() - bottomspace,name);
 
175
 
 
176
    mVectorLayer->updateItemPixmap();
 
177
 
 
178
    if (mVectorLayer->propertiesDialog())
 
179
    {
 
180
        mVectorLayer->propertiesDialog()->setRendererDirty(false);
 
181
    }
 
182
    //repaint the map canvas
 
183
    mVectorLayer->triggerRepaint();
 
184
}
 
185
 
 
186
void QgsSiMaDialog::setMarker(const QString& file, double scaleFactor) {
 
187
  QFile f(file);
 
188
  QFileInfo fi(f);
 
189
 
 
190
  if (fi.exists()) {
 
191
    // set the directory
 
192
    mCurrentDir = fi.dir().path() + "/";
 
193
    visualizeMarkers(mCurrentDir);
 
194
    mDirectoryEdit->setText(mCurrentDir);
 
195
  
 
196
    QIconViewItem* item = mIconView->findItem(fi.fileName(), Qt::ExactMatch);
 
197
    if (item) {
 
198
      // set the picture
 
199
      mIconView->setSelected(item, true);
 
200
      
 
201
      // set the scale factor
 
202
      mScaleSpin->setValue(scaleFactor * 100.0);
 
203
    }
 
204
  }
 
205
  emit settingsChanged();
 
206
}
 
207
 
 
208
const QString& QgsSiMaDialog::getPicture() const {
 
209
  return mSelectedMarker;
 
210
}
 
211
 
 
212
double QgsSiMaDialog::getScaleFactor() const {
 
213
  return mScaleSpin->value() / 100.0;
 
214
}
 
215
 
 
216
void QgsSiMaDialog::mIconView_selectionChanged(QIconViewItem * theIconViewItem)
 
217
{
 
218
    mSelectedMarker=mCurrentDir+theIconViewItem->text();
 
219
 
 
220
    //draw the SVG-Image on the button
 
221
    double scalefactor=mScaleSpin->value()/100.0;
 
222
    pmPreview->setPixmap(QgsSVGCache::instance().
 
223
                         getPixmap(mSelectedMarker, scalefactor));    
 
224
    
 
225
    emit settingsChanged();
 
226
}
 
227
 
 
228
void QgsSiMaDialog::mScaleSpin_valueChanged( int theSize)
 
229
{
 
230
#ifdef QGISDEBUG
 
231
    std::cout << "mScaleSpin_valueChanged(" << theSize << ") " << std::endl;
 
232
#endif
 
233
    //draw the SVG-Image on the button
 
234
    if(!mSelectedMarker.isEmpty())
 
235
    {
 
236
        //user enters scaling factor as a percentage
 
237
        double scalefactor=mScaleSpin->value()/100.0;
 
238
        pmPreview->setPixmap(QgsSVGCache::instance().
 
239
                             getPixmap(mSelectedMarker, scalefactor));
 
240
    }
 
241
    
 
242
    emit settingsChanged();
 
243
}
 
244
 
 
245
void QgsSiMaDialog::mBrowseDirectoriesButton_clicked()
 
246
{
 
247
    QString newdir=QFileDialog::getExistingDirectory(mCurrentDir,this,"get existing directory","Choose a directory",TRUE);
 
248
    if (!newdir.isEmpty())
 
249
    {
 
250
        mCurrentDir=newdir;
 
251
        visualizeMarkers(mCurrentDir);
 
252
        mDirectoryEdit->setText(mCurrentDir);
 
253
    }
 
254
}
 
255
 
 
256
void QgsSiMaDialog::visualizeMarkers(QString directory)
 
257
{
 
258
    mIconView->clear();
 
259
 
 
260
    QDir dir(directory);
 
261
    QStringList files=dir.entryList("*.svg;*.SVG");
 
262
 
 
263
    for(QStringList::Iterator it = files.begin(); it != files.end(); ++it )
 
264
    {
 
265
#ifdef QGISDEBUG
 
266
        qWarning(*it);
 
267
#endif
 
268
        QPixmap pix = QgsSVGCache::instance().getPixmap(mCurrentDir + 
 
269
                                                        "/" + (*it), 1);
 
270
        QIconViewItem* ivi=new QIconViewItem(mIconView,*it,pix);
 
271
    }
 
272
}
 
273
 
 
274
QString QgsSiMaDialog::defaultDir()
 
275
{
 
276
#if defined(WIN32) || defined(Q_OS_MACX)
 
277
    QString PKGDATAPATH = qApp->applicationDirPath() + "/share/qgis";
 
278
#endif
 
279
    QString dir = QString(PKGDATAPATH)+"/svg";
 
280
    return dir;
 
281
}
 
282