17 #include <QApplication>
19 #include <QPushButton>
23 #include "ui_MapScaleConfigWidget.h"
51 m_scaleBarDistance(0.0),
55 m_scaleInitDone( false ),
56 m_showRatioScale( false ),
63 #endif // Q_WS_MAEMO_5
65 m_minimizeAction =
new QAction(tr(
"Minimize"),
this);
66 m_minimizeAction->setCheckable(
true);
67 m_minimizeAction->setChecked(m_minimized);
68 connect(m_minimizeAction, SIGNAL(triggered()),
this, SLOT(toggleMinimized()));
77 return QStringList(
"mapscale" );
82 return tr(
"Scale Bar");
87 return tr(
"&Scale Bar");
92 return QString(
"scalebar" );
102 return tr(
"This is a float item that provides a map scale.");
107 return "2008, 2010, 2012";
112 return QList<PluginAuthor>()
113 <<
PluginAuthor(
"Torsten Rahn",
"tackat@kde.org", tr(
"Original Developer" ) )
114 <<
PluginAuthor(
"Khanh-Nhan Nguyen",
"khanh.nhan@wpi.edu" )
115 <<
PluginAuthor(
"Illya Kovalevskyy",
"illya.kovalevskyy@gmail.com" );
120 return QIcon(
":/icons/scalebar.png");
135 int viewportWidth = viewport->
width();
139 if ( !( m_radius == viewport->
radius()
140 && viewportWidth == m_viewportWidth
141 && m_target == target
142 && m_scaleInitDone ) )
144 int fontHeight = QFontMetrics(
font() ).ascent();
145 if (m_showRatioScale) {
147 fontHeight + 3 + m_scaleBarHeight + fontHeight + 7 ) );
150 fontHeight + 3 + m_scaleBarHeight ) );
153 m_leftBarMargin = QFontMetrics(
font() ).boundingRect(
"0" ).width() / 2;
154 m_rightBarMargin = QFontMetrics(
font() ).boundingRect(
"0000" ).width() / 2;
156 m_scaleBarWidth =
contentSize().width() - m_leftBarMargin - m_rightBarMargin;
157 m_viewportWidth = viewport->
width();
158 m_radius = viewport->
radius();
159 m_scaleInitDone =
true;
162 (qreal)(viewport->
radius());
170 m_pixel2Length *=
M_PI / 2 * cos( centerLatitude );
173 m_scaleBarDistance = (qreal)(m_scaleBarWidth) * m_pixel2Length;
177 if ( measurementSystem == QLocale::ImperialSystem ) {
178 m_scaleBarDistance *=
KM2MI;
191 painter->setRenderHint( QPainter::Antialiasing,
true );
193 int fontHeight = QFontMetrics(
font() ).ascent();
196 qreal displayMMPerPixel = 1.0 * painter->device()->widthMM() / painter->device()->width();
197 qreal ratio = m_pixel2Length / (displayMMPerPixel *
MM2M);
202 int iRatio = (int)(ratio + 0.5);
203 while (iRatio >= 1000) {
208 m_ratioString.setNum(iRatio);
209 m_ratioString = m_ratioString =
"1 : " + m_ratioString;
211 painter->setPen( QColor( Qt::darkGray ) );
212 painter->setBrush( QColor( Qt::darkGray ) );
213 painter->drawRect( m_leftBarMargin, fontHeight + 3,
217 painter->setPen( QColor( Qt::black ) );
218 painter->setBrush( QColor( Qt::white ) );
219 painter->drawRect( m_leftBarMargin, fontHeight + 3,
220 m_bestDivisor * m_pixelInterval, m_scaleBarHeight );
223 painter->drawLine( m_leftBarMargin + 1, fontHeight + 2 + m_scaleBarHeight,
224 m_leftBarMargin + m_bestDivisor * m_pixelInterval - 1, fontHeight + 2 + m_scaleBarHeight );
225 painter->setPen( QColor( Qt::black ) );
227 painter->setBrush( QColor( Qt::black ) );
230 int lastStringEnds = 0;
231 int currentStringBegin = 0;
233 for (
int j = 0; j <= m_bestDivisor; j += 2 ) {
234 if ( j < m_bestDivisor ) {
235 painter->drawRect( m_leftBarMargin + j * m_pixelInterval,
236 fontHeight + 3, m_pixelInterval - 1,
240 painter->drawLine( m_leftBarMargin + j * m_pixelInterval + 1, fontHeight + 4,
241 m_leftBarMargin + (j + 1) * m_pixelInterval - 1, fontHeight + 4 );
242 painter->setPen( QColor( Qt::black ) );
245 QLocale::MeasurementSystem distanceUnit;
248 QString unit = tr(
"km");
249 switch ( distanceUnit ) {
250 case QLocale::MetricSystem:
251 if ( m_bestDivisor * m_valueInterval > 10000 ) {
253 intervalStr.setNum( j * m_valueInterval / 1000 );
257 intervalStr.setNum( j * m_valueInterval );
261 case QLocale::ImperialSystem:
263 intervalStr.setNum( j * m_valueInterval / 1000 );
265 if ( m_bestDivisor * m_valueInterval > 3800 ) {
266 intervalStr.setNum( j * m_valueInterval / 1000 );
269 intervalStr.setNum( qreal(j * m_valueInterval ) / 1000.0,
'f', 2 );
274 painter->setFont(
font() );
277 painter->drawText( 0, fontHeight,
"0 " + unit );
278 lastStringEnds = QFontMetrics(
font() ).width(
"0 " + unit );
282 if( j == m_bestDivisor ) {
283 currentStringBegin = ( j * m_pixelInterval
284 - QFontMetrics(
font() ).boundingRect( intervalStr ).width() );
287 currentStringBegin = ( j * m_pixelInterval
288 - QFontMetrics(
font() ).width( intervalStr ) / 2 );
291 if ( lastStringEnds < currentStringBegin ) {
292 painter->drawText( currentStringBegin, fontHeight, intervalStr );
293 lastStringEnds = currentStringBegin + QFontMetrics(
font() ).width( intervalStr );
297 int leftRatioIndent = m_leftBarMargin + (m_scaleBarWidth - QFontMetrics(
font() ).width(m_ratioString) ) / 2;
298 painter->drawText( leftRatioIndent, fontHeight + 3 + m_scaleBarHeight + fontHeight + 5, m_ratioString );
303 void MapScaleFloatItem::calcScaleBar()
309 int magValue = (int)( m_scaleBarDistance );
313 while ( magValue >= 100 ) {
319 int bestMagValue = 1;
321 for (
int i = 0; i < magValue; i++ ) {
324 for (
int j = 4; j < 9; j++ ) {
325 if ( ( magValue - i ) % j == 0 ) {
329 bestMagValue = magValue - i;
342 m_pixelInterval = (int)( m_scaleBarWidth * (qreal)( bestMagValue )
343 / (qreal)( magValue ) / m_bestDivisor );
344 m_valueInterval = (int)( bestMagValue * magnitude / m_bestDivisor );
349 if ( !m_configDialog ) {
351 m_configDialog =
new QDialog();
352 ui_configWidget =
new Ui::MapScaleConfigWidget;
353 ui_configWidget->setupUi( m_configDialog );
357 connect( ui_configWidget->m_buttonBox, SIGNAL(accepted()),
358 SLOT(writeSettings()) );
359 connect( ui_configWidget->m_buttonBox, SIGNAL(rejected()),
360 SLOT(readSettings()) );
362 QPushButton *applyButton = ui_configWidget->m_buttonBox->button( QDialogButtonBox::Apply );
363 connect( applyButton, SIGNAL(clicked()) ,
364 this, SLOT(writeSettings()) );
366 return m_configDialog;
371 if ( !m_contextMenu ) {
374 foreach( QAction *
action, m_contextMenu->actions() ) {
375 if ( action->text() == tr(
"&Configure..." ) ) {
376 m_contextMenu->removeAction( action );
381 QAction *toggleAction = m_contextMenu->addAction( tr(
"&Ratio Scale"),
this,
382 SLOT(toggleRatioScaleVisibility()) );
383 toggleAction->setCheckable(
true );
384 toggleAction->setChecked( m_showRatioScale );
386 m_contextMenu->addAction(m_minimizeAction);
389 Q_ASSERT( m_contextMenu );
390 m_contextMenu->exec( w->mapToGlobal( e->pos() ) );
395 QToolTip::showText( e->globalPos(), m_ratioString );
398 void MapScaleFloatItem::readSettings()
400 if ( !m_configDialog )
403 if ( m_showRatioScale ) {
404 ui_configWidget->m_showRatioScaleCheckBox->setCheckState( Qt::Checked );
406 ui_configWidget->m_showRatioScaleCheckBox->setCheckState( Qt::Unchecked );
409 ui_configWidget->m_minimizeCheckBox->setChecked(m_minimized);
412 void MapScaleFloatItem::writeSettings()
414 if ( ui_configWidget->m_showRatioScaleCheckBox->checkState() == Qt::Checked ) {
415 m_showRatioScale =
true;
417 m_showRatioScale =
false;
420 if (m_minimized != ui_configWidget->m_minimizeCheckBox->isChecked()) {
427 void MapScaleFloatItem::toggleRatioScaleVisibility()
429 m_showRatioScale = !m_showRatioScale;
434 void MapScaleFloatItem::toggleMinimized()
436 m_minimized = !m_minimized;
437 ui_configWidget->m_minimizeCheckBox->setChecked(m_minimized);
438 m_minimizeAction->setChecked(m_minimized);
442 if (m_minimized ==
true) {
443 m_widthScaleFactor = 4;
445 m_widthScaleFactor = 2;
453 #include "MapScaleFloatItem.moc"
QLocale::MeasurementSystem measurementSystem() const
QString nameId() const
Returns the unique name of the plugin.
QList< PluginAuthor > pluginAuthors() const
This file contains the headers for MarbleModel.
QStringList backendTypes() const
Returns the name(s) of the backend that the plugin can render This method should return the name of t...
QSizeF contentSize() const
Returns the size of the content of the MarbleGraphicsItem.
const GeoDataLatLonAltBox & viewLatLonAltBox() const
QString name() const
Returns the user-visible name of the plugin.
QAction * action() const
Plugin's menu action.
qreal latitude(GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian) const
retrieves the latitude of the GeoDataCoordinates object use the unit parameter to switch between Radi...
QString copyrightYears() const
This file contains the headers for AbstractProjection.
QDialog * configDialog()
Returns a pointer to the configuration dialog of the plugin.
QString guiString() const
String that should be displayed in GUI.
QColor const aluminumGray4
virtual void toolTipEvent(QHelpEvent *e)
void settingsChanged(QString nameId)
This signal is emitted if the settings of the RenderPlugin changed.
The abstract class for float item plugins.
The class that creates a map scale.
MarbleLocale * locale() const
const AbstractProjection * currentProjection() const
bool isInitialized() const
A public class that controls what is visible in the viewport of a Marble map.
#define Q_EXPORT_PLUGIN2(a, b)
virtual GeoDataCoordinates center() const
returns the center of this box
This file contains the headers for ViewportParams.
static MarbleGlobal * getInstance()
The data model (not based on QAbstractModel) for a MarbleWidget.
QColor const aluminumGray5
virtual void contextMenuEvent(QWidget *w, QContextMenuEvent *e)
QString description() const
Returns a user description of the plugin.
void setPosition(const QPointF &position)
Set the position of the ScreenGraphicsItem.
qreal planetRadius() const
void update()
Marks the item and all parent items as invalid.
void changeViewport(ViewportParams *viewport)
QFont font() const
current font for rendering
void paintContent(QPainter *painter)
Here the items paint their content.
const MarbleModel * marbleModel() const
Access to the MarbleModel.
void setContentSize(const QSizeF &size)
Sets the size of the content of the item.
QIcon icon() const
Returns an icon for the plugin.
virtual SurfaceType surfaceType() const =0