18 #include <QDesktopServices>
22 #include <QMouseEvent>
27 #include <QStyleOptionButton>
28 #include <QDesktopServices>
31 #include <QWebElement>
32 #include <QTextDocument>
51 class MarbleLegendBrowserPrivate
54 MarbleModel *m_marbleModel;
55 QMap<QString, bool> m_checkBoxMap;
56 QMap<QString, QPixmap> m_symbolMap;
57 bool m_isLegendLoaded;
59 bool m_suppressSelection;
60 #endif // Q_WS_MAEMO_5
69 d( new MarbleLegendBrowserPrivate )
71 d->m_isLegendLoaded =
false;
74 d->m_suppressSelection =
false;
75 #endif // Q_WS_MAEMO_5
77 QWebFrame *frame = page()->mainFrame();
78 connect(frame, SIGNAL(javaScriptWindowObjectCleared()),
79 this, SLOT(injectCheckBoxChecker()));
80 page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks );
81 connect(
this, SIGNAL(linkClicked(QUrl)),
this, SLOT(openLinkExternally(QUrl)) );
92 d->m_marbleModel = marbleModel;
94 if ( d->m_marbleModel ) {
95 connect ( d->m_marbleModel, SIGNAL(themeChanged(QString)),
96 this, SLOT(initTheme()) );
102 return QSize( 180, 320 );
105 void MarbleLegendBrowser::initTheme()
108 if ( d->m_marbleModel != 0 && d->m_marbleModel->mapTheme() != 0 )
112 d->m_checkBoxMap.clear();
116 d->m_checkBoxMap[
property->name() ] =
property->value();
120 disconnect ( currentMapTheme, SIGNAL(valueChanged(QString,
bool)), 0, 0 );
121 connect ( currentMapTheme, SIGNAL(valueChanged(QString,
bool)),
129 d->m_isLegendLoaded =
false;
133 void MarbleLegendBrowser::loadLegend()
140 if ( d->m_marbleModel != 0 && d->m_marbleModel->mapTheme() != 0 )
142 const GeoSceneDocument *currentMapTheme = d->m_marbleModel->mapTheme();
145 currentMapTheme->head()->target() +
'/' +
146 currentMapTheme->head()->theme() +
"/legend.html" );
148 if ( legendPath.isEmpty() ) {
152 QString finalHtml =
readHtml( QUrl::fromLocalFile( legendPath ) );
154 TemplateDocument doc(finalHtml);
155 finalHtml = doc.finalText();
157 reverseSupportCheckboxes(finalHtml);
163 finalHtml.replace( QString(
"<!-- ##customLegendEntries:all## -->" ), sectionsHtml );
167 QUrl baseUrl = QUrl::fromLocalFile( legendPath );
170 setHtml(finalHtml, baseUrl);
172 if ( d->m_marbleModel ) {
173 QTextDocument *document =
new QTextDocument(page()->mainFrame()->toHtml());
174 d->m_marbleModel->setLegend( document );
178 void MarbleLegendBrowser::injectCheckBoxChecker()
180 QWebFrame *frame = page()->mainFrame();
181 frame->addToJavaScriptWindowObject(
"Marble",
this );
184 void MarbleLegendBrowser::openLinkExternally(
const QUrl &url )
186 QDesktopServices::openUrl( url );
192 if ( event->type() == QEvent::Show ) {
193 if ( !d->m_isLegendLoaded ) {
198 else if ( event->type() == QEvent::MouseButtonPress ) {
199 if ( static_cast<QMouseEvent *>( event )->button() == Qt::LeftButton ) {
200 d->m_suppressSelection =
true;
203 else if ( event->type() == QEvent::MouseButtonRelease ) {
204 if ( static_cast<QMouseEvent *>( event )->button() == Qt::LeftButton ) {
205 d->m_suppressSelection =
false;
208 else if ( event->type() == QEvent::MouseMove ) {
209 if ( d->m_suppressSelection ) {
214 #endif // Q_WS_MAEMO_5
216 return MarbleWebView::event( event );
223 QFile data( name.toLocalFile() );
224 if ( data.open( QFile::ReadOnly ) ) {
225 QTextStream in( &data );
236 QString s = html.remove( 0, html.indexOf(
"<body>" ) );
237 QRegExp rx(
"</?\\w+((\\s+\\w+(\\s*=\\s*(?:\".*\"|'.*'|[^'\">\\s]+))?)+\\s*|\\s*)/?>" );
238 rx.setMinimal(
true );
239 s.replace( rx,
"\n" );
240 s.replace( QRegExp(
"\\s*\n\\s*" ),
"\n" );
241 QStringList words = s.split(
'\n', QString::SkipEmptyParts );
243 QStringList::const_iterator i = words.constBegin();
244 QStringList::const_iterator
const end = words.constEnd();
245 for (; i != end; ++i )
246 html.replace( *i, tr( (*i).toUtf8() ) );
249 void MarbleLegendBrowser::reverseSupportCheckboxes(QString &html)
251 const QString old =
"<H4> <!--Locations checkbox--><a href=\"checkbox:cities\"><span style=\"text-decoration: none\"><img src=\"checkbox:cities\"> </span></a> Populated Places</H4>";
253 QString checked =
"";
254 if (d->m_checkBoxMap[
"cities"])
257 const QString repair =
""
258 "<div class=\"spec\"><h3><input type=\"checkbox\" "
259 "onchange=\"Marble.setCheckedProperty(this.name, this.checked);\" "
260 + checked +
" name=\"cities\" />Populated Places</h3></div>\n";
262 html.replace(old, repair);
269 QString customLegendString;
271 if ( d->m_marbleModel == 0 || d->m_marbleModel->mapTheme() == 0 )
276 d->m_symbolMap.clear();
281 QString bitStyle =
"<style>"
283 " background-color: #fcffdb;"
284 " padding-top: 10px;"
285 " padding-bottom: 5px;"
289 " margin-bottom: -10px!important;"
291 ".well-legend .section-head {"
292 " color: #b3925d!important;"
294 ".legend-entry .image-pic {"
296 " margin-right: 20px;"
299 ".legend-entry .notation {"
300 " line-height: 23px;"
303 " line-height: 30px;"
307 " margin-right: 5px!important;"
311 customLegendString += bitStyle;
321 customLegendString +=
"<div class=\"well well-small well-legend\">";
323 customLegendString +=
"<h4 class=\"section-head\">"+section->
heading()+
"</h4>";
325 QString checkBoxString;
328 QString checked =
"";
329 if (d->m_checkBoxMap[section->
connectTo()])
337 "<label class=\"checkbox\">"
338 "<input type=\"checkbox\" "
339 "onchange=\"Marble.setCheckedProperty(this.name, this.checked);\" " +
340 checked +
" name=\"" + section->
connectTo() +
"\" /><span>"
343 customLegendString += checkBoxString;
348 int pixmapWidth = 24;
349 int pixmapHeight = 12;
354 const QPixmap oncePixmap(path);
355 pixmapWidth = oncePixmap.width();
356 pixmapHeight = oncePixmap.height();
359 QString styleDiv =
"";
360 if (color != Qt::transparent) {
361 styleDiv =
"width: " + QString::number(pixmapWidth) +
"px; height: " +
362 QString::number(pixmapHeight) +
"px; background-color: "
363 + color.name() +
";";
365 styleDiv =
"width: " + QString::number(pixmapWidth) +
"px; height: " +
366 QString::number(pixmapHeight) +
"px;";
368 QString src = QUrl::fromLocalFile( path ).toString();
370 "<div class=\"legend-entry\">"
371 " <img class=\"image-pic\""
372 " src=\"" + src +
"\" style=\"" + styleDiv +
"\" />"
373 " <p class=\"notation\">" + item->
text() +
"</p>"
375 customLegendString += html;
377 customLegendString +=
"</div>";
380 return customLegendString;
385 QWebElement box = page()->mainFrame()->findFirstElement(
"input[name="+name+
"]");
387 if (checked != d->m_checkBoxMap[name]) {
388 d->m_checkBoxMap[name] = checked;
398 #include "MarbleLegendBrowser.moc"
The section item in a legend of a GeoScene document.
void toggledShowProperty(QString, bool)
Settings property within a GeoScene document.
static QString path(const QString &relativePath)
const GeoSceneIcon * icon() const
This file contains the headers for MarbleModel.
const GeoSceneLegend * legend() const
void translateHtml(QString &html)
void setCheckedProperty(const QString &name, bool checked)
QString connectTo() const
MarbleLegendBrowser(QWidget *parent)
A container for features parsed from the DGML file.
The data model (not based on QAbstractModel) for a MarbleWidget.
Section of a GeoScene document.
QString readHtml(const QUrl &name)
QString generateSectionsHtml()
QVector< const GeoSceneSection * > sections() const
QVector< GeoSceneItem * > items() const
void setMarbleModel(MarbleModel *marbleModel)
QVector< GeoSceneProperty * > allProperties()
Get the whole list of properties stored in the settings.
const GeoSceneSettings * settings() const
bool event(QEvent *event)