Skip to content

Commit

Permalink
Fix for ticket #111.
Browse files Browse the repository at this point in the history
Changed the code from using text to manipulate the scale bar and north
arrow to using ints as this is more robust, especially when the user
changes languages and saves a project.

Note: this commit will break saved project files if they include scale
bar or north arrow sections (the scale bar and north arrow will end up
in the default locations rather than those when the project was saved).



git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@5444 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
g_j_m committed May 12, 2006
1 parent 94135f7 commit 362f736
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 97 deletions.
49 changes: 27 additions & 22 deletions src/plugins/north_arrow/plugin.cpp
Expand Up @@ -74,11 +74,13 @@ QgsNorthArrowPlugin::QgsNorthArrowPlugin(QgisApp * theQGisApp,
QgisIface * theQgisInterFace):
QgisPlugin(name_,description_,version_,type_),
qgisMainWindowPointer(theQGisApp),
qGisInterface(theQgisInterFace)
qGisInterface(theQgisInterFace),
mPlacementIndex(1)
{
mRotationInt=0;
mAutomatic=true;
mPlacement=tr("Bottom Left");
mPlacementLabels << tr("Bottom Left") << tr("Top Left") << tr("Top Right") << tr("Bottom Right");
mPlacementIndex = 0;
}

QgsNorthArrowPlugin::~QgsNorthArrowPlugin()
Expand Down Expand Up @@ -119,7 +121,7 @@ void QgsNorthArrowPlugin::projectRead()
//default text to start with - try to fetch it from qgsproject

mRotationInt = QgsProject::instance()->readNumEntry("NorthArrow","/Rotation",0);
mPlacement = QgsProject::instance()->readEntry("NorthArrow","/Placement","Bottom Left");
mPlacementIndex = QgsProject::instance()->readNumEntry("NorthArrow","/Placement",0);
mEnable = QgsProject::instance()->readBoolEntry("NorthArrow","/Enabled",true);
mAutomatic = QgsProject::instance()->readBoolEntry("NorthArrow","/Automatic",true);
}
Expand All @@ -134,15 +136,16 @@ void QgsNorthArrowPlugin::help()
void QgsNorthArrowPlugin::run()
{
QgsNorthArrowPluginGui *myPluginGui = new QgsNorthArrowPluginGui(qgisMainWindowPointer, QgisGui::ModalDialogFlags);
//overides function byt the same name created in .ui
//overides function by the same name created in .ui
myPluginGui->setRotation(mRotationInt);
myPluginGui->setPlacement(mPlacement);
myPluginGui->setPlacementLabels(mPlacementLabels);
myPluginGui->setPlacement(mPlacementIndex);
myPluginGui->setEnabled(mEnable);
myPluginGui->setAutomatic(mAutomatic);

//listen for when the layer has been made so we can draw it
connect(myPluginGui, SIGNAL(rotationChanged(int)), this, SLOT(rotationChanged(int)));
connect(myPluginGui, SIGNAL(changePlacement(QString)), this, SLOT(setPlacement(QString)));
connect(myPluginGui, SIGNAL(changePlacement(int)), this, SLOT(setPlacement(int)));
connect(myPluginGui, SIGNAL(enableAutomatic(bool)), this, SLOT(setAutomatic(bool)));
connect(myPluginGui, SIGNAL(enableNorthArrow(bool)), this, SLOT(setEnabled(bool)));
connect(myPluginGui, SIGNAL(needToRefresh()), this, SLOT(refreshCanvas()));
Expand Down Expand Up @@ -202,26 +205,28 @@ void QgsNorthArrowPlugin::renderNorthArrow(QPainter * theQPainter)
int myWidth = theQPainter->device()->width();

#ifdef QGISDEBUG
std::cout << "Rendering n-arrow at " << mPlacement.toLocal8Bit().data() << std::endl;
std::cout << "Rendering n-arrow at " << mPlacementLabels.at(mPlacementIndex).toLocal8Bit().data() << std::endl;
#endif
//Determine placement of label from form combo box
if (mPlacement==tr("Bottom Left"))
switch (mPlacementIndex)
{
case 0: // Bottom Left
theQPainter->translate(0,myHeight-myQPixmap.height());
}
else if (mPlacement==tr("Top Right"))
{
break;
case 1: // Top Left
//no need to translate for TL corner because we're already at the origin
theQPainter->translate(0, 0);
break;
case 2: // Top Right
theQPainter->translate(myWidth-myQPixmap.width(),0);
}
else if (mPlacement==tr("Bottom Right"))
{
break;
case 3: // Bottom Right
theQPainter->translate(myWidth-myQPixmap.width(),
myHeight-myQPixmap.height());
}
else // defaulting to top left
{
//no need to translate for TL corner because we're already at the origin
theQPainter->translate(0, 0);
break;
default:
std::cout << "Unable to determine where to put north arrow so defaulting to top left"
<< std::endl;
}
//rotate the canvas by the north arrow rotation amount
theQPainter->rotate( mRotationInt );
Expand Down Expand Up @@ -264,10 +269,10 @@ void QgsNorthArrowPlugin::rotationChanged(int theInt)
}

//! set placement of north arrow
void QgsNorthArrowPlugin::setPlacement(QString theQString)
void QgsNorthArrowPlugin::setPlacement(int placementIndex)
{
mPlacement = theQString;
QgsProject::instance()->writeEntry("NorthArrow","/Placement", mPlacement);
mPlacementIndex = placementIndex;
QgsProject::instance()->writeEntry("NorthArrow","/Placement", mPlacementIndex);
}

void QgsNorthArrowPlugin::setEnabled(bool theBool)
Expand Down
7 changes: 4 additions & 3 deletions src/plugins/north_arrow/plugin.h
Expand Up @@ -59,7 +59,7 @@ class QgsNorthArrowPlugin:public QObject, public QgisPlugin
//! show the help document
void help();
//! set north arrow placement
void setPlacement(QString);
void setPlacement(int);
//! enable or disable north arrow
void setEnabled(bool);
//! enable or disable the automatic setting of the arrow direction
Expand All @@ -83,8 +83,9 @@ class QgsNorthArrowPlugin:public QObject, public QgisPlugin
bool mEnable;
//! enable or disable the automatic setting of the arrow direction
bool mAutomatic;
// The placement string
QString mPlacement;
// The placement index and translated text
int mPlacementIndex;
QStringList mPlacementLabels;
//! Id of the plugin's menu. Used for unloading
int menuId;
//! Pointer to our toolbar
Expand Down
19 changes: 13 additions & 6 deletions src/plugins/north_arrow/plugingui.cpp
Expand Up @@ -44,7 +44,7 @@ void QgsNorthArrowPluginGui::on_pbnOK_clicked()
//close the dialog
emit rotationChanged(sliderRotation->value());
emit enableAutomatic(cboxAutomatic->isChecked());
emit changePlacement(cboPlacement->currentText());
emit changePlacement(cboPlacement->currentIndex());
emit enableNorthArrow(cboxShow->isChecked());
emit needToRefresh();

Expand All @@ -64,9 +64,15 @@ void QgsNorthArrowPluginGui::setRotation(int theInt)
spinAngle->setValue(theInt);
}

void QgsNorthArrowPluginGui::setPlacement(QString thePlacementQString)
void QgsNorthArrowPluginGui::setPlacementLabels(QStringList& labels)
{
cboPlacement->setCurrentText(tr(thePlacementQString));
cboPlacement->clear();
cboPlacement->addItems(labels);
}

void QgsNorthArrowPluginGui::setPlacement(int placementIndex)
{
cboPlacement->setCurrentIndex(placementIndex);
}

void QgsNorthArrowPluginGui::setEnabled(bool theBool)
Expand Down Expand Up @@ -153,17 +159,18 @@ void QgsNorthArrowPluginGui::rotatePixmap(int theRotationInt)
myQPainter.end();
pixmapLabel->setPixmap(myPainterPixmap);
}

}

// Called when the widget needs to be updated.
//

/*
void QgsNorthArrowPluginGui::paintEvent( QPaintEvent * thePaintEvent)
{
std::cerr<<__FILE__<<__LINE__<<'\n';
rotatePixmap(sliderRotation->value());
std::cerr<<__FILE__<<__LINE__<<'\n';
}

*/
//
// Called when the widget has been resized.
//
Expand Down
7 changes: 4 additions & 3 deletions src/plugins/north_arrow/plugingui.h
Expand Up @@ -31,21 +31,22 @@ public slots:

private:
void rotatePixmap(int theRotationInt);
void paintEvent( QPaintEvent * );//overloads qwidget
// void paintEvent( QPaintEvent * );//overloads qwidget
void resizeEvent(QResizeEvent *); //overloads qwidget
signals:
//void drawRasterLayer(QString);
//void drawVectorrLayer(QString,QString,QString);
void rotationChanged(int);
void changePlacement(QString);
void changePlacement(int);
// enable NorthArrow
void enableNorthArrow(bool);
void enableAutomatic(bool);
void needToRefresh();

public slots:
void setRotation(int);
void setPlacement(QString thePlacementQString);
void setPlacementLabels(QStringList&);
void setPlacement(int);
void setEnabled(bool);
void setAutomatic(bool);
void setAutomaticDisabled();
Expand Down
73 changes: 42 additions & 31 deletions src/plugins/scale_bar/plugin.cpp
Expand Up @@ -77,9 +77,12 @@ QgsScaleBarPlugin::QgsScaleBarPlugin(QgisApp * theQGisApp,
qgisMainWindowPointer(theQGisApp),
qGisInterface(theQgisInterFace)
{
mPlacementLabels << tr("Top Left") << tr("Bottom Left") << tr("Top Right") << tr("Bottom Right");
mStyleLabels << tr("Tick Down") << tr("Tick Up") << tr("Bar") << tr("Box");

mPreferredSize = 30;
mPlacement = "Top Left";
mStyle = "Tick Down";
mPlacementIndex = 0;
mStyleIndex = 0;
mEnabled = true;
mSnapping = true;
mColour = Qt::black;
Expand Down Expand Up @@ -125,8 +128,8 @@ void QgsScaleBarPlugin::projectRead()


mPreferredSize = QgsProject::instance()->readNumEntry("ScaleBar","/PreferredSize",30);
mStyle = QgsProject::instance()->readEntry("ScaleBar","/Style","Tick Down");
mPlacement = QgsProject::instance()->readEntry("ScaleBar","/Placement","Top Left");
mStyleIndex = QgsProject::instance()->readNumEntry("ScaleBar","/Style",0);
mPlacementIndex = QgsProject::instance()->readNumEntry("ScaleBar","/Placement",0);
mEnabled = QgsProject::instance()->readBoolEntry("ScaleBar","/Enabled",true);
mSnapping = QgsProject::instance()->readBoolEntry("ScaleBar","/Snapping",true);
int myRedInt = QgsProject::instance()->readNumEntry("ScaleBar","/ColorRedPart",0);
Expand All @@ -146,16 +149,18 @@ void QgsScaleBarPlugin::run()
QgsScaleBarPluginGui *myPluginGui=new QgsScaleBarPluginGui(qgisMainWindowPointer, QgisGui::ModalDialogFlags);
myPluginGui->setPreferredSize(mPreferredSize);
myPluginGui->setSnapping(mSnapping);
myPluginGui->setPlacement(mPlacement);
myPluginGui->setPlacementLabels(mPlacementLabels);
myPluginGui->setPlacement(mPlacementIndex);
myPluginGui->setEnabled(mEnabled);
myPluginGui->setStyle(mStyle);
myPluginGui->setStyleLabels(mStyleLabels);
myPluginGui->setStyle(mStyleIndex);
myPluginGui->setColour(mColour);

connect(myPluginGui, SIGNAL(changePreferredSize(int)), this, SLOT(setPreferredSize(int)));
connect(myPluginGui, SIGNAL(changeSnapping(bool)), this, SLOT(setSnapping(bool)));
connect(myPluginGui, SIGNAL(changePlacement(QString)), this, SLOT(setPlacement(QString)));
connect(myPluginGui, SIGNAL(changePlacement(int)), this, SLOT(setPlacement(int)));
connect(myPluginGui, SIGNAL(changeEnabled(bool)), this, SLOT(setEnabled(bool)));
connect(myPluginGui, SIGNAL(changeStyle(QString)), this, SLOT(setStyle(QString)));
connect(myPluginGui, SIGNAL(changeStyle(int)), this, SLOT(setStyle(int)));
connect(myPluginGui, SIGNAL(changeColour(QColor)), this, SLOT(setColour(QColor)));
connect(myPluginGui, SIGNAL(refreshCanvas()), this, SLOT(refreshCanvas()));
myPluginGui->show();
Expand Down Expand Up @@ -294,28 +299,25 @@ void QgsScaleBarPlugin::renderScaleBar(QPainter * theQPainter)
//determine the origin of scale bar depending on placement selected
int myOriginX=myMargin;
int myOriginY=myMargin;
if (mPlacement==tr("Top Left"))
switch (mPlacementIndex)
{
case 0: // Top Left
myOriginX=myMargin;
myOriginY=myMargin;
}
else if (mPlacement==tr("Bottom Left"))
{
break;
case 1: // Bottom Left
myOriginX=myMargin;
myOriginY=myCanvasHeight - myMargin;
}
else if (mPlacement==tr("Top Right"))
{
break;
case 2: // Top Right
myOriginX=myCanvasWidth - ((int) myTotalScaleBarWidth) - myMargin;
myOriginY=myMargin;
}
else if (mPlacement==tr("Bottom Right"))
{
break;
case 3: // Bottom Right
myOriginX=myCanvasWidth - ((int) myTotalScaleBarWidth) - myMargin;
myOriginY=myCanvasHeight - myMargin;
}
else
{
break;
default:
std::cout << "Unable to determine where to put scale bar so defaulting to top left" << std::endl;
}

Expand All @@ -327,7 +329,9 @@ void QgsScaleBarPlugin::renderScaleBar(QPainter * theQPainter)
int myScaleBarWidthInt = (int) myScaleBarWidth;

//Create array of vertices for scale bar depending on style
if (mStyle==tr("Tick Down"))
switch (mStyleIndex)
{
case 0: // Tick Down
{
QPolygon myTickDownArray(4);
//draw a buffer first so bar shows up on dark images
Expand All @@ -348,8 +352,9 @@ void QgsScaleBarPlugin::renderScaleBar(QPainter * theQPainter)
(myScaleBarWidthInt + myOriginX), (myOriginY + myMajorTickSize)
);
theQPainter->drawPolyline(myTickDownArray);
break;
}
else if (mStyle==tr("Tick Up"))
case 1: // tick up
{
QPolygon myTickUpArray(4);
//draw a buffer first so bar shows up on dark images
Expand All @@ -370,8 +375,9 @@ void QgsScaleBarPlugin::renderScaleBar(QPainter * theQPainter)
(myScaleBarWidthInt + myOriginX), myOriginY
);
theQPainter->drawPolyline(myTickUpArray);
break;
}
else if (mStyle==tr("Bar"))
case 2: // Bar
{
QPolygon myBarArray(2);
//draw a buffer first so bar shows up on dark images
Expand All @@ -388,8 +394,9 @@ void QgsScaleBarPlugin::renderScaleBar(QPainter * theQPainter)
(myScaleBarWidthInt + myOriginX), (myOriginY + (myMajorTickSize/2))
);
theQPainter->drawPolyline(myBarArray);
break;
}
else if (mStyle==tr("Box"))
case 3: // box
{
// Want square corners for a box
myBackgroundPen.setJoinStyle( Qt::MiterJoin );
Expand Down Expand Up @@ -427,6 +434,10 @@ void QgsScaleBarPlugin::renderScaleBar(QPainter * theQPainter)
midPointX , myOriginY
);
theQPainter->drawPolygon(myBoxArray);
break;
}
default:
std::cerr << "Unknown style\n";
}

//Do actual drawing of scale bar
Expand Down Expand Up @@ -529,10 +540,10 @@ void QgsScaleBarPlugin::unload()
}

//! set placement of scale bar
void QgsScaleBarPlugin::setPlacement(QString theQString)
void QgsScaleBarPlugin::setPlacement(int placementIndex)
{
mPlacement = theQString;
QgsProject::instance()->writeEntry("ScaleBar","/Placement",mPlacement);
mPlacementIndex = placementIndex;
QgsProject::instance()->writeEntry("ScaleBar","/Placement",mPlacementIndex);
}

//! set preferred size of scale bar
Expand All @@ -556,10 +567,10 @@ void QgsScaleBarPlugin::setEnabled(bool theBool)
QgsProject::instance()->writeEntry("ScaleBar","/Enabled",mEnabled);
}
//! set scale bar enable
void QgsScaleBarPlugin::setStyle(QString theStyleQString)
void QgsScaleBarPlugin::setStyle(int styleIndex)
{
mStyle = theStyleQString;
QgsProject::instance()->writeEntry("ScaleBar","/Style",mStyle);
mStyleIndex = styleIndex;
QgsProject::instance()->writeEntry("ScaleBar","/Style",mStyleIndex);
}
//! set the scale bar colour
void QgsScaleBarPlugin::setColour(QColor theQColor)
Expand Down

0 comments on commit 362f736

Please sign in to comment.